Debian Codegraph @

src/ImathTest/testInterop.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <ImathFun.h>
11 #include <ImathVec.h>
12 #include <ImathMatrix.h>
13 #include <array>
14 #include <cassert>
15 #include <cmath>
16 #include <iostream>
17 #include <vector>
18 #include "testVec.h"
19
20 using namespace std;
21 using namespace IMATH_INTERNAL_NAMESPACE;
22
23 #if IMATH_FOREIGN_VECTOR_INTEROP
24
25 // Imath::has_subscript fails for std::vector because its length does not
26 // appear to be the length of N elements. Carve out an exception here that
27 // allows this to work.
28 IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
29 template<typename T, int N>
30 struct has_subscript<std::vector<T>, T, N> : public std::true_type { };
31 IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT
32
33
34
35 namespace
36 {
37
38 // Extra simple vector that does nothing but allow element access, as an
39 // example of a vector type that an app might use and want interop with
40 // our vectors.
41 template <typename T, int N>
42 struct SimpleVec {
43 T elements[N];
44
45 SimpleVec(T val = T(0)) {
46 for (int i = 0; i < N; ++i)
47 elements[i] = val;
48 }
49 ~SimpleVec() = default;
50 constexpr T operator[](int i) const { return elements[i]; }
51 T& operator[](int i) { return elements[i]; }
52 };
53
54
55
56 // Extra simple matrix that does nothing but allow element access, as an
57 // example of a matrix type that an app might use and want interop with
58 // our vectors.
59 template <typename T, int N>
60 struct SimpleMx {
61 T elements[N][N];
62
63 SimpleMx(T val = T(0)) {
64 for (int j = 0; j < N; ++j)
65 for (int i = 0; i < N; ++i)
66 elements[j][i] = val;
67 }
68 ~SimpleMx() = default;
69 const T* operator[](int i) const { return elements[i]; }
70 T* operator[](int i) { return elements[i]; }
71 };
72
73
74
75 // Small structs containing just x,y,z,w elements.
76 template<typename T>
77 struct xy {
78 T x, y;
79 };
80
81
82 template<typename T>
83 struct xyz {
84 T x, y, z;
85 };
86
87
88 template<typename T>
89 struct xyzw {
90 T x, y, z, w;
91 };
92
93
94 template<typename T>
95 struct xyz_wrong {
96 T x() { return 0; }
97 T y() { return 1; }
98 T z() { return 2; }
99 };
100
101
102
103 // A class that has *both* subscripting and named members
104 template<typename T>
105 struct ComboVec2 {
106 union {
107 T elements[2];
108 struct { T x, y; };
109 };
110
111 ComboVec2(T val = T(0)) : x(val), y(val) { }
112 ~ComboVec2() = default;
113 constexpr T operator[](int i) const { return elements[i]; }
114 T& operator[](int i) { return elements[i]; }
115 };
116
117
118
119 // A class that has *both* subscripting and named members
120 template<typename T>
121 struct ComboVec3 {
122 union {
123 T elements[3];
124 struct { T x, y, z; };
125 };
126
127 ComboVec3(T val = T(0)) : x(val), y(val), z(val) { }
128 ~ComboVec3() = default;
129 constexpr T operator[](int i) const { return elements[i]; }
130 T& operator[](int i) { return elements[i]; }
131 };
132
133
134
135 // A class that has *both* subscripting and named members
136 template<typename T>
137 struct ComboVec4 {
138 union {
139 T elements[4];
140 struct { T x, y, z, w; };
141 };
142
143 ComboVec4(T val = T(0)) : x(val), y(val), z(val), w(val) { }
144 ~ComboVec4() = default;
145 constexpr T operator[](int i) const { return elements[i]; }
146 T& operator[](int i) { return elements[i]; }
147 };
148
149
150
151 // Test whether a Vec contains the given elements.
152 void
153 testVecVal2f(const V2f& v, float a, float b)
154 {
155 assert(v[0] == a && v[1] == b);
156 }
157
158 void
159 testVecVal3f(const V3f& v, float a, float b, float c)
160 {
161 assert(v[0] == a && v[1] == b && v[2] == c);
162 }
163
164 void
165 testVecVal4f(const V4f& v, float a, float b, float c, float d)
166 {
167 assert(v[0] == a && v[1] == b && v[2] == c && v[3] == d);
168 }
169
170
171
172
173 void
174 testInteropVec2()
175 {
176 std::cout << "has_xy<SimpleVec<float,2>, float>::value = "
177 << has_xy<SimpleVec<float,2>, float>::value << "\n";
178 std::cout << "has_xy<SimpleVec<int,2>, float>::value = "
179 << has_xy<SimpleVec<int,2>, float>::value << "\n";
180 std::cout << "has_xy<SimpleVec<float,4>, float>::value = "
181 << has_xy<SimpleVec<float,4>, float>::value << "\n";
182 std::cout << "has_xy<xyz<float>, float>::value = "
183 << has_xy<xyz<float>, float>::value << "\n";
184 std::cout << "has_xy<xyzw<float>, float>::value = "
185 << has_xy<xyzw<float>, float>::value << "\n";
186 std::cout << "has_xy<xy<float>, float>::value = "
187 << has_xy<xy<float>, float>::value << "\n";
188 std::cout << "has_xy<xyz<int>, float>::value = "
189 << has_xy<xyz<int>, float>::value << "\n";
190 std::cout << "has_xy<xyz_wrong<float>, float>::value = "
191 << has_xy<xyz_wrong<float>, float>::value << "\n";
192 std::cout << "\n";
193 std::cout << "has_subscript<SimpleVec<float,2>, float, 2>::value = "
194 << has_subscript<SimpleVec<float,2>, float, 2>::value << "\n";
195 std::cout << "has_subscript<SimpleVec<int,2>, float, 2>::value = "
196 << has_subscript<SimpleVec<int,2>, float, 2>::value << "\n";
197 std::cout << "has_subscript<SimpleVec<float,4>, float, 2>::value = "
198 << has_subscript<SimpleVec<float,4>, float, 2>::value << "\n";
199 std::cout << "has_subscript<xyz<float>, float, 2>::value = "
200 << has_subscript<xyz<float>, float, 2>::value << "\n";
201 std::cout << "has_subscript<xyzw<float>, float, 2>::value = "
202 << has_subscript<xyzw<float>, float, 2>::value << "\n";
203 std::cout << "has_subscript<xy<float>, float, 2>::value = "
204 << has_subscript<xy<float>, float, 2>::value << "\n";
205 std::cout << "has_subscript<xyz<int>, float, 2>::value = "
206 << has_subscript<xyz<int>, float, 2>::value << "\n";
207 std::cout << "has_subscript<xyz_wrong<float>, float, 2>::value = "
208 << has_subscript<xyz_wrong<float>, float, 2>::value << "\n";
209 std::cout << "has_subscript<std::array<float, 2>, float, 2>::value = "
210 << has_subscript<std::array<float, 2>, float, 2>::value << "\n";
211 std::cout << "has_subscript<std::array<int, 2>, float, 2>::value = "
212 << has_subscript<std::array<int, 2>, float, 2>::value << "\n";
213 std::cout << "has_subscript<std::array<float, 4>, float, 2>::value = "
214 << has_subscript<std::array<float, 4>, float, 2>::value << "\n";
215 std::cout << "has_subscript<std::vector<float>, float, 2>::value = "
216 << has_subscript<std::vector<float>, float, 2>::value << "\n";
217 std::cout << "\n";
218 std::cout << "has_xy<ComboVec3<float>, float, 2>::value = "
219 << has_xy<ComboVec3<float>, float>::value << "\n";
220 std::cout << "has_subscript<ComboVec3<float>, float, 2>::value = "
221 << has_subscript<ComboVec3<float>, float, 2>::value << "\n";
222 std::cout << "\n";
223
224 assert((!has_xy<SimpleVec<float,2>, float>::value));
225 assert((has_xy<xy<float>, float>::value));
226 assert((!has_xy<xyz<float>, float>::value));
227
228 assert((has_subscript<SimpleVec<float,2>, float, 2>::value));
229 assert((!has_subscript<SimpleVec<float,4>, float, 2>::value));
230 assert((!has_subscript<SimpleVec<float,2>, int, 2>::value));
231 assert((!has_subscript<xyz<float>, float, 2>::value));
232 assert((!has_subscript<xy<float>, float, 2>::value));
233
234 // Test construction/assignment/paramater pass of a vector type with
235 // subscripting to access components.
236 {
237 SimpleVec<float,2> s;
238 s[0] = 1;
239 s[1] = 2;
240 Vec2<float> v(s);
241 assert(v[0] == 1 && v[1] == 2);
242 s[1] = 42;
243 v = s;
244 assert(v[0] == 1 && v[1] == 42);
245 testVecVal2f(V2f(s), 1.0f, 42.0f);
246 }
247 // Test construction/assignment/paramater pass of a vector type with
248 // explicit .y, .y, .z components but no subscripting.
249 {
250 xy<float> s { 1, 2 };
251 Vec2<float> v(s);
252 assert(v[0] == 1 && v[1] == 2);
253 s.y = 42;
254 v = s;
255 assert(v[0] == 1 && v[1] == 42);
256 testVecVal2f(V2f(s), 1.0f, 42.0f);
257 }
258 // Test construction/assignment/paramater pass of a std::vector of length 3
259 {
260 std::vector<float> s { 1, 2 };
261 Vec2<float> v(s);
262 assert(v[0] == 1 && v[1] == 2);
263 s[1] = 42;
264 v = s;
265 assert(v[0] == 1 && v[1] == 42);
266 testVecVal2f(V2f(s), 1.0f, 42.0f);
267 }
268 // Test construction/assignment/paramater pass of a std::array of length 3
269 {
270 std::array<float, 2> s { 1, 2 };
271 Vec2<float> v(s);
272 assert(v[0] == 1 && v[1] == 2);
273 s[1] = 42;
274 v = s;
275 assert(v[0] == 1 && v[1] == 42);
276 testVecVal2f(V2f(s), 1.0f, 42.0f);
277 }
278 // Test construction/assignment/paramater pass of initializer lists.
279 {
280 Vec2<float> v({ 1.0f, 2.0f });
281 assert(v[0] == 1 && v[1] == 2);
282 v = { 1.0f, 42.0f };
283 assert(v[0] == 1 && v[1] == 42);
284 testVecVal2f({ 1.0f, 42.0f}, 1.0f, 42.0f);
285 }
286 // Test construction/assignment/paramater pass of a C array
287 {
288 float s[2] = { 1, 2 };
289 Vec2<float> v(s);
290 assert(v[0] == 1 && v[1] == 2);
291 s[1] = 42;
292 v = s;
293 assert(v[0] == 1 && v[1] == 42);
294 testVecVal2f(V2f(s), 1.0f, 42.0f);
295 }
296 }
297
298
299 //---------
300
301 void
302 testInteropVec3()
303 {
304 std::cout << "has_xyz<SimpleVec<float,3>, float>::value = "
305 << has_xyz<SimpleVec<float,3>, float>::value << "\n";
306 std::cout << "has_xyz<SimpleVec<int,3>, float>::value = "
307 << has_xyz<SimpleVec<int,3>, float>::value << "\n";
308 std::cout << "has_xyz<SimpleVec<float,4>, float>::value = "
309 << has_xyz<SimpleVec<float,4>, float>::value << "\n";
310 std::cout << "has_xyz<xyz<float>, float>::value = "
311 << has_xyz<xyz<float>, float>::value << "\n";
312 std::cout << "has_xyz<xyzw<float>, float>::value = "
313 << has_xyz<xyzw<float>, float>::value << "\n";
314 std::cout << "has_xyz<xy<float>, float>::value = "
315 << has_xyz<xy<float>, float>::value << "\n";
316 std::cout << "has_xyz<xyz<int>, float>::value = "
317 << has_xyz<xyz<int>, float>::value << "\n";
318 std::cout << "has_xyz<xyz_wrong<float>, float>::value = "
319 << has_xyz<xyz_wrong<float>, float>::value << "\n";
320 std::cout << "\n";
321 std::cout << "has_subscript<SimpleVec<float,3>, float, 3>::value = "
322 << has_subscript<SimpleVec<float,3>, float, 3>::value << "\n";
323 std::cout << "has_subscript<SimpleVec<int,3>, float, 3>::value = "
324 << has_subscript<SimpleVec<int,3>, float, 3>::value << "\n";
325 std::cout << "has_subscript<SimpleVec<float,4>, float, 3>::value = "
326 << has_subscript<SimpleVec<float,4>, float, 3>::value << "\n";
327 std::cout << "has_subscript<xyz<float>, float, 3>::value = "
328 << has_subscript<xyz<float>, float, 3>::value << "\n";
329 std::cout << "has_subscript<xyzw<float>, float, 3>::value = "
330 << has_subscript<xyzw<float>, float, 3>::value << "\n";
331 std::cout << "has_subscript<xy<float>, float, 3>::value = "
332 << has_subscript<xy<float>, float, 3>::value << "\n";
333 std::cout << "has_subscript<xyz<int>, float, 3>::value = "
334 << has_subscript<xyz<int>, float, 3>::value << "\n";
335 std::cout << "has_subscript<xyz_wrong<float>, float, 3>::value = "
336 << has_subscript<xyz_wrong<float>, float, 3>::value << "\n";
337 std::cout << "has_subscript<std::array<float, 3>, float, 3>::value = "
338 << has_subscript<std::array<float, 3>, float, 3>::value << "\n";
339 std::cout << "has_subscript<std::array<int, 3>, float, 3>::value = "
340 << has_subscript<std::array<int, 3>, float, 3>::value << "\n";
341 std::cout << "has_subscript<std::array<float, 4>, float, 3>::value = "
342 << has_subscript<std::array<float, 4>, float, 3>::value << "\n";
343 std::cout << "has_subscript<std::vector<float>, float, 3>::value = "
344 << has_subscript<std::vector<float>, float, 3>::value << "\n";
345 std::cout << "\n";
346 std::cout << "has_xyz<ComboVec3<float>, float, 3>::value = "
347 << has_xyz<ComboVec3<float>, float>::value << "\n";
348 std::cout << "has_subscript<ComboVec3<float>, float, 3>::value = "
349 << has_subscript<ComboVec3<float>, float, 3>::value << "\n";
350 std::cout << "\n";
351
352 assert((!has_xyz<SimpleVec<float,3>, float>::value));
353 assert((has_xyz<xyz<float>, float>::value));
354 assert((!has_xyz<xy<float>, float>::value));
355
356 assert((has_subscript<SimpleVec<float,3>, float, 3>::value));
357 assert((!has_subscript<SimpleVec<float,4>, float, 3>::value));
358 assert((!has_subscript<SimpleVec<float,3>, int, 3>::value));
359 assert((!has_subscript<xyz<float>, float, 3>::value));
360 assert((!has_subscript<xy<float>, float, 3>::value));
361
362 // Test construction/assignment/paramater pass of a vector type with
363 // subscripting to access components.
364 {
365 SimpleVec<float,3> s;
366 s[0] = 1;
367 s[1] = 2;
368 s[2] = 3;
369 Vec3<float> v(s);
370 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
371 s[1] = 42;
372 v = s;
373 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
374 testVecVal3f(V3f(s), 1.0f, 42.0f, 3.0f);
375 }
376 // Test construction/assignment/paramater pass of a vector type with
377 // explicit .y, .y, .z components but no subscripting.
378 {
379 xyz<float> s { 1, 2, 3 };
380 Vec3<float> v(s);
381 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
382 s.y = 42;
383 v = s;
384 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
385 testVecVal3f(V3f(s), 1.0f, 42.0f, 3.0f);
386 }
387 // Test construction/assignment/paramater pass of a std::vector of length 3
388 {
389 std::vector<float> s { 1, 2, 3 };
390 Vec3<float> v(s);
391 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
392 s[1] = 42;
393 v = s;
394 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
395 testVecVal3f(V3f(s), 1.0f, 42.0f, 3.0f);
396 }
397 // Test construction/assignment/paramater pass of a std::array of length 3
398 {
399 std::array<float, 3> s { 1, 2, 3 };
400 Vec3<float> v(s);
401 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
402 s[1] = 42;
403 v = s;
404 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
405 testVecVal3f(V3f(s), 1.0f, 42.0f, 3.0f);
406 }
407 // Test construction/assignment/paramater pass of initializer lists.
408 {
409 Vec3<float> v({ 1.0f, 2.0f, 3.0f });
410 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
411 v = { 1.0f, 42.0f, 3.0f };
412 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
413 testVecVal3f({ 1.0f, 42.0f, 3.0f }, 1, 42, 3);
414 }
415 // Test construction/assignment/paramater pass of a C array
416 {
417 float s[3] = { 1, 2, 3 };
418 Vec3<float> v(s);
419 assert(v[0] == 1 && v[1] == 2 && v[2] == 3);
420 s[1] = 42;
421 v = s;
422 assert(v[0] == 1 && v[1] == 42 && v[2] == 3);
423 testVecVal3f(V3f(s), 1.0f, 42.0f, 3.0f);
424 }
425 }
426
427
428 //---------
429
430 void
431 testInteropVec4()
432 {
433 std::cout << "has_xyzw<SimpleVec<float,4>, float>::value = "
434 << has_xyzw<SimpleVec<float,4>, float>::value << "\n";
435 std::cout << "has_xyzw<SimpleVec<int,4>, float>::value = "
436 << has_xyzw<SimpleVec<int,4>, float>::value << "\n";
437 std::cout << "has_xyzw<SimpleVec<float,4>, float>::value = "
438 << has_xyzw<SimpleVec<float,4>, float>::value << "\n";
439 std::cout << "has_xyzw<xyzw<float>, float>::value = "
440 << has_xyzw<xyzw<float>, float>::value << "\n";
441 std::cout << "has_xyzw<xyzw<float>, float>::value = "
442 << has_xyzw<xyzw<float>, float>::value << "\n";
443 std::cout << "has_xyzw<xy<float>, float>::value = "
444 << has_xyzw<xy<float>, float>::value << "\n";
445 std::cout << "has_xyzw<xyzw<int>, float>::value = "
446 << has_xyzw<xyzw<int>, float>::value << "\n";
447 std::cout << "has_xyzw<xyz_wrong<float>, float>::value = "
448 << has_xyzw<xyz_wrong<float>, float>::value << "\n";
449 std::cout << "\n";
450 std::cout << "has_subscript<SimpleVec<float,4>, float, 4>::value = "
451 << has_subscript<SimpleVec<float,4>, float, 4>::value << "\n";
452 std::cout << "has_subscript<SimpleVec<int,4>, float, 4>::value = "
453 << has_subscript<SimpleVec<int,4>, float, 4>::value << "\n";
454 std::cout << "has_subscript<SimpleVec<float,4>, float, 4>::value = "
455 << has_subscript<SimpleVec<float,4>, float, 4>::value << "\n";
456 std::cout << "has_subscript<xyzw<float>, float, 4>::value = "
457 << has_subscript<xyzw<float>, float, 4>::value << "\n";
458 std::cout << "has_subscript<xyzw<float>, float, 4>::value = "
459 << has_subscript<xyzw<float>, float, 4>::value << "\n";
460 std::cout << "has_subscript<xy<float>, float, 4>::value = "
461 << has_subscript<xy<float>, float, 4>::value << "\n";
462 std::cout << "has_subscript<xyzw<int>, float, 4>::value = "
463 << has_subscript<xyzw<int>, float, 4>::value << "\n";
464 std::cout << "has_subscript<xyz_wrong<float>, float, 4>::value = "
465 << has_subscript<xyz_wrong<float>, float, 4>::value << "\n";
466 std::cout << "has_subscript<std::array<float, 3>, float, 4>::value = "
467 << has_subscript<std::array<float, 3>, float, 4>::value << "\n";
468 std::cout << "has_subscript<std::array<int, 3>, float, 4>::value = "
469 << has_subscript<std::array<int, 3>, float, 4>::value << "\n";
470 std::cout << "has_subscript<std::array<float, 4>, float, 4>::value = "
471 << has_subscript<std::array<float, 4>, float, 4>::value << "\n";
472 std::cout << "has_subscript<std::vector<float>, float, 4>::value = "
473 << has_subscript<std::vector<float>, float, 4>::value << "\n";
474 std::cout << "\n";
475 std::cout << "has_xyzw<ComboVec4<float>, float, 4>::value = "
476 << has_xyzw<ComboVec4<float>, float>::value << "\n";
477 std::cout << "has_subscript<ComboVec4<float>, float, 4>::value = "
478 << has_subscript<ComboVec4<float>, float, 4>::value << "\n";
479 std::cout << "\n";
480
481 assert((!has_xyzw<SimpleVec<float,4>, float>::value));
482 assert((has_xyzw<xyzw<float>, float>::value));
483 assert((!has_xyzw<xyz<float>, float>::value));
484
485 assert((has_subscript<SimpleVec<float,4>, float, 4>::value));
486 assert((!has_subscript<SimpleVec<float,4>, float, 3>::value));
487 assert((!has_subscript<SimpleVec<float,4>, int, 4>::value));
488 assert((!has_subscript<xyzw<float>, float, 4>::value));
489 assert((!has_subscript<xy<float>, float, 4>::value));
490
491 // Test construction/assignment/paramater pass of a vector type with
492 // subscripting to access components.
493 {
494 SimpleVec<float,4> s;
495 s[0] = 1;
496 s[1] = 2;
497 s[2] = 3;
498 s[3] = 4;
499 Vec4<float> v(s);
500 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
501 s[1] = 42;
502 v = s;
503 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
504 testVecVal4f(V4f(s), 1.0f, 42.0f, 3.0f, 4.0f);
505 }
506 // Test construction/assignment/paramater pass of a vector type with
507 // explicit .y, .y, .z components but no subscripting.
508 {
509 xyzw<float> s { 1, 2, 3, 4 };
510 Vec4<float> v(s);
511 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
512 s.y = 42;
513 v = s;
514 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
515 testVecVal4f(V4f(s), 1.0f, 42.0f, 3.0f, 4.0f);
516 }
517 // Test construction/assignment/paramater pass of a std::vector of length 3
518 {
519 std::vector<float> s { 1, 2, 3, 4 };
520 Vec4<float> v(s);
521 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
522 s[1] = 42;
523 v = s;
524 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
525 testVecVal4f(V4f(s), 1.0f, 42.0f, 3.0f, 4.0f);
526 }
527 // Test construction/assignment/paramater pass of a std::array of length 3
528 {
529 std::array<float, 4> s { 1, 2, 3, 4 };
530 Vec4<float> v(s);
531 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
532 s[1] = 42;
533 v = s;
534 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
535 testVecVal4f(V4f(s), 1.0f, 42.0f, 3.0f, 4.0f);
536 }
537 // Test construction/assignment/paramater pass of initializer lists.
538 {
539 Vec4<float> v({ 1.0f, 2.0f, 3.0f, 4.0f });
540 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
541 v = { 1.0f, 42.0f, 3.0f, 4.0f };
542 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
543 testVecVal4f({ 1.0f, 42.0f, 3.0f, 4.0f }, 1, 42, 3, 4);
544 }
545 // Test construction/assignment/paramater pass of a C array
546 {
547 float s[4] = { 1, 2, 3, 4 };
548 Vec4<float> v(s);
549 assert(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
550 s[1] = 42;
551 v = s;
552 assert(v[0] == 1 && v[1] == 42 && v[2] == 3 && v[3] == 4);
553 testVecVal4f(V4f(s), 1.0f, 42.0f, 3.0f, 4.0f);
554 }
555 }
556
557
558 //---------
559
560 void
561 testInteropMx2()
562 {
563 std::cout << "has_double_subscript<SimpleMx<float,2>, float, 2, 2>::value = "
564 << has_double_subscript<SimpleMx<float,2>, float, 2, 2>::value << "\n";
565 std::cout << "has_double_subscript<SimpleMx<int,2>, float, 2, 2>::value = "
566 << has_double_subscript<SimpleMx<int,2>, float, 2, 2>::value << "\n";
567 std::cout << "has_double_subscript<SimpleMx<float,2>, float, 4, 4>::value = "
568 << has_double_subscript<SimpleMx<float,2>, float, 4, 4>::value << "\n";
569 std::cout << "has_double_subscript<xyzw<float>, float, 2, 2>::value = "
570 << has_double_subscript<xyzw<float>, float, 2, 2>::value << "\n";
571 std::cout << "has_double_subscript<xyz_wrong<float>, float, 2, 2>::value = "
572 << has_double_subscript<xyz_wrong<float>, float, 2, 2>::value << "\n";
573 std::cout << "\n";
574
575 assert((has_double_subscript<SimpleMx<float,2>, float, 2, 2>::value));
576 assert((!has_double_subscript<SimpleMx<float,2>, float, 4, 4>::value));
577 assert((!has_double_subscript<SimpleMx<float,2>, int, 2, 2>::value));
578 assert((!has_double_subscript<xyzw<float>, float, 2, 2>::value));
579 assert((!has_double_subscript<xy<float>, float, 2, 2>::value));
580
581 // Test construction/assignment/paramater pass of a vector type with
582 // subscripting to access components.
583 {
584 Matrix22<float> ref;
585 SimpleMx<float,2> s;
586 for (int j = 0; j < 2; ++j)
587 for (int i = 0; i < 2; ++i)
588 {
589 s[j][i] = float(i + j * 2);
590 ref[j][i] = float(i + j * 2);
591 }
592 Matrix22<float> v(s);
593 assert(v[0][0] == 0 && v[0][1] == 1 &&
594 v[1][0] == 2 && v[1][1] == 3);
595 s[1][1] = 42;
596 v = s;
597 assert(v[0][0] == 0 && v[0][1] == 1 &&
598 v[1][0] == 2 && v[1][1] == 42);
599 }
600 }
601
602
603
604 void
605 testInteropMx3()
606 {
607 std::cout << "has_double_subscript<SimpleMx<float,3>, float, 3, 3>::value = "
608 << has_double_subscript<SimpleMx<float,3>, float, 3, 3>::value << "\n";
609 std::cout << "has_double_subscript<SimpleMx<int,3>, float, 3, 3>::value = "
610 << has_double_subscript<SimpleMx<int,3>, float, 3, 3>::value << "\n";
611 std::cout << "has_double_subscript<SimpleMx<float,3>, float, 4, 4>::value = "
612 << has_double_subscript<SimpleMx<float,3>, float, 4, 4>::value << "\n";
613 std::cout << "has_double_subscript<xyzw<float>, float, 3, 3>::value = "
614 << has_double_subscript<xyzw<float>, float, 3, 3>::value << "\n";
615 std::cout << "has_double_subscript<xyz_wrong<float>, float, 3, 3>::value = "
616 << has_double_subscript<xyz_wrong<float>, float, 3, 3>::value << "\n";
617 std::cout << "\n";
618
619 assert((has_double_subscript<SimpleMx<float,3>, float, 3, 3>::value));
620 assert((!has_double_subscript<SimpleMx<float,3>, float, 4, 4>::value));
621 assert((!has_double_subscript<SimpleMx<float,3>, int, 3, 3>::value));
622 assert((!has_double_subscript<xyzw<float>, float, 3, 3>::value));
623 assert((!has_double_subscript<xy<float>, float, 3, 3>::value));
624
625 // Test construction/assignment/paramater pass of a vector type with
626 // subscripting to access components.
627 {
628 Matrix33<float> ref;
629 SimpleMx<float,3> s;
630 for (int j = 0; j < 3; ++j)
631 for (int i = 0; i < 3; ++i)
632 {
633 s[j][i] = float(i + j * 3);
634 ref[j][i] = float(i + j * 3);
635 }
636 Matrix33<float> v(s);
637 assert(v[0][0] == 0 && v[0][1] == 1 && v[0][2] == 2 &&
638 v[1][0] == 3 && v[1][1] == 4 && v[1][2] == 5 &&
639 v[2][0] == 6 && v[2][1] == 7 && v[2][2] == 8);
640 s[1][1] = 42;
641 v = s;
642 assert(v[0][0] == 0 && v[0][1] == 1 && v[0][2] == 2 &&
643 v[1][0] == 3 && v[1][1] == 42 && v[1][2] == 5 &&
644 v[2][0] == 6 && v[2][1] == 7 && v[2][2] == 8);
645 }
646 }
647
648
649
650 void
651 testInteropMx4()
652 {
653 std::cout << "has_double_subscript<SimpleMx<float,4>, float, 4, 4>::value = "
654 << has_double_subscript<SimpleMx<float,4>, float, 4, 4>::value << "\n";
655 std::cout << "has_double_subscript<SimpleMx<int,4>, float, 4, 4>::value = "
656 << has_double_subscript<SimpleMx<int,4>, float, 4, 4>::value << "\n";
657 std::cout << "has_double_subscript<SimpleMx<float,4>, float, 4, 4>::value = "
658 << has_double_subscript<SimpleMx<float,4>, float, 4, 4>::value << "\n";
659 std::cout << "has_double_subscript<xyzw<float>, float, 4, 4>::value = "
660 << has_double_subscript<xyzw<float>, float, 4, 4>::value << "\n";
661 std::cout << "has_double_subscript<xyz_wrong<float>, float, 4, 4>::value = "
662 << has_double_subscript<xyz_wrong<float>, float, 4, 4>::value << "\n";
663 std::cout << "\n";
664
665 assert((has_double_subscript<SimpleMx<float,4>, float, 4, 4>::value));
666 assert((!has_double_subscript<SimpleMx<float,4>, float, 3, 3>::value));
667 assert((!has_double_subscript<SimpleMx<float,4>, int, 4, 4>::value));
668 assert((!has_double_subscript<xyzw<float>, float, 4, 4>::value));
669 assert((!has_double_subscript<xy<float>, float, 4, 4>::value));
670
671 // Test construction/assignment/paramater pass of a vector type with
672 // subscripting to access components.
673 {
674 Matrix44<float> ref;
675 SimpleMx<float,4> s;
676 for (int j = 0; j < 4; ++j)
677 for (int i = 0; i < 4; ++i)
678 {
679 s[j][i] = float(i + j * 4);
680 ref[j][i] = float(i + j * 4);
681 }
682 Matrix44<float> v(s);
683 assert(v[0][0] == 0 && v[0][1] == 1 && v[0][2] == 2 && v[0][3] == 3 &&
684 v[1][0] == 4 && v[1][1] == 5 && v[1][2] == 6 && v[1][3] == 7 &&
685 v[2][0] == 8 && v[2][1] == 9 && v[2][2] == 10 && v[2][3] == 11 &&
686 v[3][0] == 12 && v[3][1] == 13 && v[3][2] == 14 && v[3][3] == 15);
687 s[1][1] = 42;
688 v = s;
689 assert(v[0][0] == 0 && v[0][1] == 1 && v[0][2] == 2 && v[0][3] == 3 &&
690 v[1][0] == 4 && v[1][1] == 42 && v[1][2] == 6 && v[1][3] == 7 &&
691 v[2][0] == 8 && v[2][1] == 9 && v[2][2] == 10 && v[2][3] == 11 &&
692 v[3][0] == 12 && v[3][1] == 13 && v[3][2] == 14 && v[3][3] == 15);
693 }
694 }
695
696 } // namespace
697 #endif
698
699
700 void
701 testInterop()
702 {
703 #if IMATH_FOREIGN_VECTOR_INTEROP
704
705 cout << "Testing interoperability with foreign types" << endl;
706
707 testInteropVec2 ();
708 testInteropVec3 ();
709 testInteropVec4 ();
710
711 testInteropMx2();
712 testInteropMx3();
713 testInteropMx4();
714
715 cout << "ok\n" << endl;
716 #else
717 cout << "Foreign vector type interopability is disabled.\n";
718 #endif
719 }