File: TestSDKCamera.cxx

package info (click to toggle)
f3d 3.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,668 kB
  • sloc: cpp: 99,109; python: 811; sh: 342; xml: 238; java: 101; javascript: 95; makefile: 25
file content (316 lines) | stat: -rw-r--r-- 11,133 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
#include <camera.h>
#include <engine.h>
#include <log.h>
#include <window.h>

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>

// TODO these methods should be put in types.h at some point.
// https://github.com/f3d-app/f3d/issues/361
bool compareDouble(double a, double b)
{
  return std::fabs(a - b) < 128 * std::numeric_limits<double>::epsilon();
}

bool compareVec(const f3d::vector3_t& vec1, const f3d::vector3_t& vec2)
{
  return compareDouble(vec1[0], vec2[0]) && compareDouble(vec1[1], vec2[1]) &&
    compareDouble(vec1[2], vec2[2]);
}

bool comparePoint(const f3d::point3_t& vec1, const f3d::point3_t& vec2)
{
  return compareDouble(vec1[0], vec2[0]) && compareDouble(vec1[1], vec2[1]) &&
    compareDouble(vec1[2], vec2[2]);
}

class testFailure : public std::runtime_error
{
public:
  explicit testFailure(const std::string& what = "")
    : std::runtime_error(what)
  {
  }
};

void checkVec3(const std::array<double, 3>& actual, const std::array<double, 3>& expected,
  const std::string& label)
{
  if (!compareDouble(actual[0], expected[0]) || !compareDouble(actual[1], expected[1]) ||
    !compareDouble(actual[2], expected[2]))
  {
    std::stringstream ss;
    ss << label << ": ";
    ss << std::setprecision(12);
    ss << "(" << actual[0] << "," << actual[1] << "," << actual[2] << ")";
    ss << " != ";
    ss << "(" << expected[0] << "," << expected[1] << "," << expected[2] << ")";
    throw testFailure(ss.str());
  }
}

void checkDouble(const double actual, const double expected, const std::string& label)
{
  if (!compareDouble(actual, expected))
  {
    std::stringstream ss;
    ss << label << ": ";
    ss << std::setprecision(12);
    ss << actual << " != " << expected;
    throw testFailure(ss.str());
  }
}

int TestSDKCamera(int argc, char* argv[])
{
  f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);
  f3d::engine eng = f3d::engine::create(true);
  f3d::window& win = eng.getWindow();
  f3d::camera& cam = win.getCamera();

  // check coordinates conversion
  f3d::point3_t point = { 0.1, 0.1, 0.1 };
  f3d::point3_t pointDC = win.getDisplayFromWorld(point);
  if (!comparePoint(point, win.getWorldFromDisplay(pointDC)))
  {
    std::cerr << "coordinates conversion is not behaving as expected" << std::endl;
    return EXIT_FAILURE;
  }

  // Test position
  f3d::point3_t testPos = { 0., 0., 10. };
  f3d::point3_t pos = cam.setPosition(testPos).getPosition();
  if (pos != testPos)
  {
    std::cerr << "set/get position is not behaving as expected: " << pos[0] << "," << pos[1] << ","
              << pos[2] << std::endl;
    return EXIT_FAILURE;
  }

  // Test focal point
  f3d::point3_t testFoc = { 0., 0., -1. };
  f3d::point3_t foc = cam.setFocalPoint(testFoc).getFocalPoint();
  if (foc != testFoc)
  {
    std::cerr << "set/get focal point is not behaving as expected: " << foc[0] << "," << foc[1]
              << "," << foc[2] << std::endl;
    return EXIT_FAILURE;
  }

  // Test view up
  f3d::vector3_t testUp = { 1., 0., 0. };
  f3d::vector3_t up = cam.setViewUp(testUp).getViewUp();
  if (up != testUp)
  {
    std::cerr << "set/get view up is not behaving as expected: " << up[0] << "," << up[1] << ","
              << up[2] << std::endl;
    return EXIT_FAILURE;
  }

  // Test view angle
  f3d::angle_deg_t testAngle = 20;
  f3d::angle_deg_t angle = cam.setViewAngle(testAngle).getViewAngle();
  if (angle != testAngle)
  {
    std::cerr << "set/get view angle is not behaving as expected: " << angle << std::endl;
    return EXIT_FAILURE;
  }

  // Test azimuth
  cam.azimuth(90);
  f3d::point3_t expectedPos = { 0., -11., -1. };
  f3d::point3_t expectedFoc = { 0., 0., -1. };
  f3d::vector3_t expectedUp = { 1., 0., 0. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Azimuth is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  // Test roll
  cam.roll(90);
  expectedUp = { 0., 0., -1. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Roll is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  // Test yaw
  cam.yaw(90);
  expectedFoc = { 11., -11., -1. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Yaw is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  // Test elevation
  cam.elevation(90);
  expectedPos = { 11., -11., -12. };
  expectedUp = { 1., 0., 0. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Elevation is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  // Test pitch
  cam.pitch(90);
  expectedFoc = { 22., -11., -12. };
  expectedUp = { 0., 0., -1. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Pitch is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  // Test dolly
  cam.dolly(10);
  expectedPos = { 20.9, -11., -12. };
  pos = cam.getPosition();
  foc = cam.getFocalPoint();
  up = cam.getViewUp();
  if (!comparePoint(pos, expectedPos) || !comparePoint(foc, expectedFoc) ||
    !compareVec(up, expectedUp))
  {
    std::cerr << "Dolly is not behaving as expected: " << std::endl;
    std::cerr << std::setprecision(12) << "position: " << pos[0] << "," << pos[1] << "," << pos[2]
              << std::endl;
    std::cerr << std::setprecision(12) << "focal point: " << foc[0] << "," << foc[1] << ","
              << foc[2] << std::endl;
    std::cerr << std::setprecision(12) << "view up: " << up[0] << "," << up[1] << "," << up[2]
              << std::endl;
    return EXIT_FAILURE;
  }

  try
  {
    cam.setPosition({ 1, 2, 3 });
    cam.setFocalPoint({ 1, 2, 13 });
    cam.setViewUp({ 0, 1, 0 });
    cam.pan(1, 2);
    checkVec3(cam.getPosition(), { 0, 4, 3 }, "pos after pan");
    checkVec3(cam.getFocalPoint(), { 0, 4, 13 }, "foc after pan");
    checkVec3(cam.getViewUp(), { 0, 1, 0 }, "up after pan");

    cam.setPosition({ 1, 2, 3 });
    cam.setFocalPoint({ 1, -2, 3 });
    cam.setViewUp({ 0, 0, 1 });
    cam.pan(3, 4, 5);
    checkVec3(cam.getPosition(), { -2, -3, 7 }, "pos after pan");
    checkVec3(cam.getFocalPoint(), { -2, -7, 7 }, "foc after pan");
    checkVec3(cam.getViewUp(), { 0, 0, 1 }, "up after pan");

    cam.setPosition({ 1, 2, 3 });
    cam.setFocalPoint({ 1, 2, 13 });
    cam.setViewUp({ 0, 1, 0 });
    cam.setViewAngle(25);
    cam.zoom(1.5);
    checkVec3(cam.getPosition(), { 1, 2, 3 }, "pos after zoom");
    checkVec3(cam.getFocalPoint(), { 1, 2, 13 }, "foc after zoom");
    checkVec3(cam.getViewUp(), { 0, 1, 0 }, "up after zoom");
    checkDouble(cam.getViewAngle(), 25 / 1.5, "angle after zoom");

    cam.setPosition({ 1, 0, 0 });
    cam.setFocalPoint({ 0, 0, 0 });
    cam.setViewUp({ 1, 0, 0 });
    checkVec3(
      cam.getPosition(), { 1, 0, 0 }, "pos when cross product of pos->foc and up is 0 - test 1");
    checkVec3(
      cam.getFocalPoint(), { 0, 0, 0 }, "foc when cross product of pos->foc and up is 0 - test 1");
    checkVec3(
      cam.getViewUp(), { 0, 1, 0 }, "up when cross product of pos->foc and up is 0 - test 1");

    cam.setPosition({ 0, 1, 0 });
    cam.setFocalPoint({ 0, 0, 0 });
    cam.setViewUp({ 0, 1, 0 });
    checkVec3(
      cam.getPosition(), { 0, 1, 0 }, "pos when cross product of pos->foc and up is 0 - test 2");
    checkVec3(
      cam.getFocalPoint(), { 0, 0, 0 }, "foc when cross product of pos->foc and up is 0 - test 2");
    checkVec3(
      cam.getViewUp(), { 1, 0, 0 }, "up when cross product of pos->foc and up is 0 - test 2");

    cam.setPosition({ 0, 0, 1 });
    cam.setFocalPoint({ 0, 0, 0 });
    cam.setViewUp({ 0, 0, 1 });
    checkVec3(
      cam.getPosition(), { 0, 0, 1 }, "pos when cross product of pos->foc and up is 0 - test 3");
    checkVec3(
      cam.getFocalPoint(), { 0, 0, 0 }, "foc when cross product of pos->foc and up is 0 - test 3");
    checkVec3(
      cam.getViewUp(), { 1, 0, 0 }, "up when cross product of pos->foc and up is 0 - test 3");

    cam.setPosition({ 5, 0, 0 });
    cam.setFocalPoint({ 1, 0, 0 });
    cam.setViewUp({ 1, 0, 0 });
    checkVec3(
      cam.getPosition(), { 5, 0, 0 }, "pos when cross product of pos->foc and up is 0 - test 4");
    checkVec3(
      cam.getFocalPoint(), { 1, 0, 0 }, "foc when cross product of pos->foc and up is 0 - test 4");
    checkVec3(
      cam.getViewUp(), { 0, 1, 0 }, "up when cross product of pos->foc and up is 0 - test 4");
  }
  catch (testFailure& e)
  {
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}