File: catchPanda.cpp

package info (click to toggle)
visp 3.7.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 166,384 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 207; objc: 145; makefile: 118
file content (326 lines) | stat: -rw-r--r-- 10,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test vpCameraParameters JSON parse / save.
 */

/*!
  \example catchPanda.cpp

  Test saving and parsing JSON configuration for Panda 3D renderer.
*/

#include <visp3/core/vpConfig.h>

#if defined(VISP_HAVE_PANDA3D) && defined(VISP_HAVE_CATCH2)
#include <visp3/core/vpCameraParameters.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/ar/vpPanda3DBaseRenderer.h>
#include <visp3/ar/vpPanda3DRGBRenderer.h>
#include <visp3/ar/vpPanda3DFrameworkManager.h>
#include <visp3/ar/vpPanda3DRendererSet.h>
#include <visp3/ar/vpPanda3DGeometryRenderer.h>
#include <catch_amalgamated.hpp>

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

#include <random>

const std::string objCube =
"o Cube\n"
"v -0.050000 -0.050000 0.050000\n"
"v -0.050000 0.050000 0.050000\n"
"v -0.050000 -0.050000 -0.050000\n"
"v -0.050000 0.050000 -0.050000\n"
"v 0.050000 -0.050000 0.050000\n"
"v 0.050000 0.050000 0.050000\n"
"v 0.050000 -0.050000 -0.050000\n"
"v 0.050000 0.050000 -0.050000\n"
"f 2/4/1 3/8/1 1/1/1\n"
"f 4/9/2 7/13/2 3/8/2\n"
"f 8/14/3 5/11/3 7/13/3\n"
"f 6/12/4 1/2/4 5/11/4\n"
"f 7/13/5 1/3/5 3/7/5\n"
"f 4/10/6 6/12/6 8/14/6\n"
"f 2/4/1 4/9/1 3/8/1\n"
"f 4/9/2 8/14/2 7/13/2\n"
"f 8/14/3 6/12/3 5/11/3\n"
"f 6/12/4 2/5/4 1/2/4\n"
"f 7/13/5 5/11/5 1/3/5\n"
"f 4/10/6 2/6/6 6/12/6\n";


std::string createObjFile()
{
  const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_rbt_obj");
  const std::string objFile = vpIoTools::createFilePath(tempDir, "cube.obj");
  std::ofstream f(objFile);
  std::cout << objFile << std::endl;
  f << objCube;
  f.close();

  return objFile;
}

vpPanda3DRenderParameters defaultRenderParams()
{
  vpCameraParameters cam(600, 600, 160, 120);
  return vpPanda3DRenderParameters(cam, 240, 320, 0.001, 1.0);
}

bool opt_no_display = false; // If true, disable display or tests requiring display

SCENARIO("Instanciating multiple Panda3D renderers", "[Panda3D]")
{
  if (opt_no_display) {
    std::cout << "Display is disabled for tests, skipping..." << std::endl;
  }
  else {
    GIVEN("A single renderer")
    {
      vpPanda3DGeometryRenderer r1(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
      r1.setRenderParameters(defaultRenderParams());
      r1.initFramework();
      r1.renderFrame();
      vpImage<float> depth;
      r1.getRender(depth);

      THEN("Creating another, uncoupled renderer is ok and its destruction does not raise an error")
      {
        vpPanda3DGeometryRenderer r2(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
        r2.setRenderParameters(defaultRenderParams());
        r2.initFramework();
        r2.renderFrame();
      }
      r1.renderFrame();

      r1.getRender(depth);
    }
  }
}


SCENARIO("Sequentially instanciating and destroying Panda3D renderers", "[Panda3D]")
{
  if (opt_no_display) {
    std::cout << "Display is disabled for tests, skipping..." << std::endl;
  }
  else {
    vpPanda3DGeometryRenderer r3(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
    r3.setRenderParameters(defaultRenderParams());
    r3.initFramework();
    r3.renderFrame();
    vpImage<float> depth;
    r3.getRender(depth);

    {
      vpPanda3DGeometryRenderer r1(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
      r1.setRenderParameters(defaultRenderParams());
      r1.initFramework();
      r1.renderFrame();
      r1.getRender(depth);
    }

    {
      vpPanda3DGeometryRenderer r2(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
      r2.setRenderParameters(defaultRenderParams());
      r2.initFramework();
      vpImage<float> depth;
      r2.renderFrame();
      r2.getRender(depth);

    }
  }
}

SCENARIO("Sequentially instanciating and destroying Panda3D renderer sets", "[Panda3D]")
{
  if (opt_no_display) {
    std::cout << "Display is disabled for tests, skipping..." << std::endl;
  }
  else {
    {
      vpPanda3DRendererSet r1(defaultRenderParams());
      r1.addSubRenderer(std::make_shared<vpPanda3DGeometryRenderer>(vpPanda3DGeometryRenderer::CAMERA_NORMALS));
      r1.addSubRenderer(std::make_shared<vpPanda3DRGBRenderer>(true));
      r1.initFramework();
      r1.renderFrame();
    }

    {
      vpPanda3DRendererSet r1(defaultRenderParams());
      r1.addSubRenderer(std::make_shared<vpPanda3DGeometryRenderer>(vpPanda3DGeometryRenderer::CAMERA_NORMALS));
      r1.addSubRenderer(std::make_shared<vpPanda3DRGBRenderer>(true));
      r1.initFramework();
      r1.renderFrame();
    }
  }
}

SCENARIO("Using multiple panda3d renderers in parallel", "[Panda3D]")
{
  if (opt_no_display) {
    std::cout << "Display is disabled for tests, skipping..." << std::endl;
  }
  else {
    vpPanda3DRGBRenderer r3(true);
    r3.setRenderParameters(defaultRenderParams());
    r3.initFramework();
    r3.renderFrame();

    vpPanda3DGeometryRenderer r1(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
    r1.setRenderParameters(defaultRenderParams());
    r1.initFramework();
    r1.renderFrame();
    vpImage<float> depth;
    r1.getRender(depth);

    vpPanda3DGeometryRenderer r2(vpPanda3DGeometryRenderer::CAMERA_NORMALS);
    r2.setRenderParameters(defaultRenderParams());
    r2.initFramework();
    r2.renderFrame();
    r2.getRender(depth);


    r1.renderFrame();
    r1.getRender(depth);
  }
}

TEST_CASE("Testing Geometry renderer", "[panda3D]")
{
  if (opt_no_display) {
    std::cout << "Display is disabled for tests, skipping..." << std::endl;
    return;
  }
  vpPanda3DGeometryRenderer r1(vpPanda3DGeometryRenderer::CAMERA_NORMALS, false), r2(vpPanda3DGeometryRenderer::CAMERA_NORMALS, true);
  vpPanda3DGeometryRenderer r3(vpPanda3DGeometryRenderer::OBJECT_NORMALS, false), r4(vpPanda3DGeometryRenderer::OBJECT_NORMALS, true);


  std::vector<vpPanda3DGeometryRenderer *> renderers = { &r1, &r2, &r3, &r4 };

  std::vector<vpHomogeneousMatrix> cameraPoses = {
    vpHomogeneousMatrix(0.0, 0.0, 0.5, 0.0, vpMath::rad(15), 0.0),
    vpHomogeneousMatrix(0.05, 0.0, 0.4, vpMath::rad(5), 0.0, 0.0),
  };

  for (const vpHomogeneousMatrix &p: cameraPoses) {
    std::vector<vpImage<vpRGBf>> normalsImages;
    std::vector<vpImage<float>> depthImages;

    vpPanda3DRenderParameters params = defaultRenderParams();
    const std::string objId = "object";
    for (const auto r: renderers) {
      r->setRenderParameters(params);
      r->initFramework();
      r->addNodeToScene(r->loadObject(objId, createObjFile()));
      r->setNodePose(objId, vpHomogeneousMatrix(0, 0, 0, 0, 0, 0));
      r->setCameraPose(p.inverse());
      r->renderFrame();

      vpImage<float> depth, depthBB, depthSolo;
      vpImage<vpRGBf> normals(params.getImageHeight(), params.getImageWidth(), vpRGBf(0, 0, 0)), normalsBB, normalsSolo;

      r->getRender(normals, depth);
      r->getRender(normalsSolo);
      r->getRender(depthSolo);
      r->getRender(normalsBB, depthBB, vpRect(0, 0, params.getImageWidth(), params.getImageHeight()), params.getImageHeight(), params.getImageWidth());
      // Using different getter lead to the same output
      REQUIRE((depth.getMinValue() == 0 && depth.getMaxValue() > 0.0));
      REQUIRE(depth == depthSolo);
      REQUIRE(depth == depthBB);
      REQUIRE((normals.getSum() != 0.0));
      REQUIRE(normals == normalsSolo);
      REQUIRE(normals == normalsBB);
      normalsImages.push_back(normals);
      depthImages.push_back(depth);
    }
    // Fast rendering difference is small enough
    const float thresholdN = 1 / 127.5f;
    const float thresholdDepth = params.getFarClippingDistance() / 255.f;
    for (unsigned int i = 0; i < normalsImages.size() - 1; ++i) {
      for (unsigned int j = i + 1; j < normalsImages.size(); ++j) {
        if (renderers[i]->getRenderType() == renderers[j]->getRenderType()) {

          std::cout << "Comparing renderers" << i << " and " << j << std::endl;

          for (unsigned int b = 0; b < depthImages[i].getSize(); ++b) {

            float depthDiff = fabs(depthImages[i].bitmap[b] - depthImages[j].bitmap[b]);
            if (depthDiff > thresholdDepth) {
              FAIL("Depth error is too great");
            }
            if (depthImages[i].bitmap[b] > 0.0) {
              const vpRGBf c1 = normalsImages[i].bitmap[b];
              const vpRGBf c2 = normalsImages[j].bitmap[b];


              if (fabs(c1.R - c2.R) > thresholdN ||  fabs(c1.G - c2.G) > thresholdN ||  fabs(c1.B - c2.B) > thresholdN) {
                std::cout << c1 << c2 << std::endl;
                FAIL("Normal error is too great");
              }
            }
          }
        }
      }
    }

  }

}


int main(int argc, char *argv[])
{
  Catch::Session session; // There must be exactly one instance
  auto cli = session.cli()
    | Catch::Clara::Opt(opt_no_display)["--no-display"]("Disable display");
  session.cli(cli);

  const int returnCode = session.applyCommandLine(argc, argv);
  if (returnCode != 0) { // Indicates a command line error
    return returnCode;
  }

  const int numFailed = session.run();
  vpPanda3DFrameworkManager::getInstance().exit();

  return numFailed;
}

#else

#include <stdlib.h>

int main() { return EXIT_SUCCESS; }

#endif