-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.cpp
740 lines (652 loc) · 28.3 KB
/
test.cpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
#include "test.h"
#include <gtest/gtest.h>
#include <pxr/base/arch/fileSystem.h>
#include <pxr/base/tf/fileUtils.h>
#include <pxr/base/tf/pathUtils.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usdGeom/primvarsAPI.h>
#include <pxr/usd/usdGeom/tokens.h>
#include <pxr/usd/usdLux/diskLight.h>
#include <pxr/usd/usdLux/distantLight.h>
#include <pxr/usd/usdLux/domeLight.h>
#include <pxr/usd/usdLux/rectLight.h>
#include <pxr/usd/usdLux/shapingAPI.h>
#include <pxr/usd/usdLux/sphereLight.h>
#include <pxr/usd/usdGeom/camera.h>
#include <pxr/usd/usdShade/material.h>
PXR_NAMESPACE_OPEN_SCOPE
TF_DEFINE_PUBLIC_TOKENS(TestTokens, TEST_TOKENS);
PXR_NAMESPACE_CLOSE_SCOPE
using namespace PXR_NS;
int
archSystem(const std::string& command)
{
#if defined(_WIN32)
return _wsystem(ArchWindowsUtf8ToUtf16(command).c_str());
#else
return system(command.c_str());
#endif
}
template<typename T>
struct Primvar
{
PXR_NS::TfToken interpolation;
PXR_NS::VtArray<T> values;
PXR_NS::VtIntArray indices;
};
template<typename T>
static bool
readPrimvar(UsdGeomPrimvarsAPI& api, const TfToken& name, Primvar<T>& primvar)
{
UsdGeomPrimvar pv = api.GetPrimvar(name);
if (pv.IsDefined()) {
pv.Get(&primvar.values);
pv.GetIndices(&primvar.indices);
primvar.interpolation = pv.GetInterpolation();
return true;
}
return false;
}
#define ASSERT_ARRAY(...) assertArray(__VA_ARGS__)
template<typename T>
void
assertArray(VtArray<T>& actual, const ArrayData<T>& expected, const std::string& name) // test a subset of the array
{
ASSERT_EQ(actual.size(), expected.size);
ASSERT_GE(actual.size(), expected.values.size());
bool arraysMatch = true;
size_t i;
for (i = 0; i < expected.values.size(); i++) {
if (actual[i] != expected.values[i]) {
arraysMatch = false;
break;
}
}
ASSERT_TRUE(arraysMatch) << "Variable: " << name << ". Elements at [" << i << "] differ. Actual = " << actual[i]
<< ", Expected = " << expected.values[i];
}
bool
floatsEqual(float a, float b, float epsilon = 1e-6)
{
// Ensure that floating point comparison doesn't result in a false negative
return std::abs(a - b) < epsilon;
}
bool
doublesEqual(double a, double b, double epsilon = 1e-6)
{
// Ensure that floating point comparison doesn't result in a false negative
return std::abs(a - b) < epsilon;
}
#define ASSERT_VEC2F(...) assertVec2f(__VA_ARGS__)
void
assertVec2f(const PXR_NS::GfVec2f& actual,
const PXR_NS::GfVec2f& expected,
std::string msg = "") // test a vector of 2 floats
{
bool valuesMatch = true;
size_t i;
for (i = 0; i < 2; ++i) {
if (!floatsEqual(actual[i], expected[i])) {
valuesMatch = false;
break;
}
}
if (msg != "") {
// Add a space after the message if it's not empty
msg += ": ";
}
ASSERT_TRUE(valuesMatch) << msg << "Elements at [" << i << "] differ. Actual = " << actual[i]
<< ", Expected = " << expected[i];
}
#define ASSERT_QUATF(...) assertQuatf(__VA_ARGS__)
void
assertQuatf(const PXR_NS::GfQuatf& actual,
const PXR_NS::GfQuatf& expected,
std::string msg = "") // test a quaternion of floats
{
bool valuesMatch = true;
if (msg != "") {
// Add a space after the message if it's not empty
msg += ": ";
}
ASSERT_TRUE(floatsEqual(actual.GetReal(), expected.GetReal()))
<< msg << "Real elements differ. Actual = " << actual.GetReal()
<< ", Expected = " << expected.GetReal();
size_t i;
for (i = 0; i < 3; ++i) {
if (!floatsEqual(actual.GetImaginary()[i], expected.GetImaginary()[i])) {
valuesMatch = false;
break;
}
}
ASSERT_TRUE(valuesMatch) << msg << "Imaginary elements at [" << i
<< "] differ. Actual = " << actual.GetImaginary()[i]
<< ", Expected = " << expected.GetImaginary()[i];
}
#define ASSERT_VEC3F(...) assertVec3f(__VA_ARGS__)
void
assertVec3f(const PXR_NS::GfVec3f& actual,
const PXR_NS::GfVec3f& expected,
std::string msg = "") // test a vector of 3 floats
{
bool valuesMatch = true;
size_t i;
for (i = 0; i < 3; ++i) {
if (!floatsEqual(actual[i], expected[i])) {
valuesMatch = false;
break;
}
}
if (msg != "") {
// Add a space after the message if it's not empty
msg += ": ";
}
ASSERT_TRUE(valuesMatch) << msg << "Elements at [" << i << "] differ. Actual = " << actual[i]
<< ", Expected = " << expected[i];
}
#define ASSERT_VEC3D(...) assertVec3d(__VA_ARGS__)
void
assertVec3d(const PXR_NS::GfVec3d& actual,
const PXR_NS::GfVec3d& expected,
std::string msg = "") // test a vector of 3 doubles
{
bool valuesMatch = true;
size_t i;
for (i = 0; i < 3; ++i) {
if (!doublesEqual(actual[i], expected[i])) {
valuesMatch = false;
break;
}
}
if (msg != "") {
// Add a space after the message if it's not empty
msg += ": ";
}
ASSERT_TRUE(valuesMatch) << msg << "Elements at [" << i << "] differ. Actual = " << actual[i]
<< ", Expected = " << expected[i];
}
void
assertPrim(PXR_NS::UsdStageRefPtr stage, const std::string& path)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
}
void
assertNode(UsdStageRefPtr stage, const std::string& path)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
UsdGeomXform xform(prim);
ASSERT_TRUE(xform);
}
void
assertMesh(PXR_NS::UsdStageRefPtr stage, const std::string& path, const MeshData& data)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
UsdGeomMesh geomMesh(prim);
ASSERT_TRUE(geomMesh);
UsdGeomPrimvarsAPI primvarsAPI(geomMesh);
VtIntArray faceVertexCounts;
VtIntArray faceVertexIndices;
VtVec3fArray points;
Primvar<GfVec3f> normals;
Primvar<GfVec2f> uvs;
Primvar<PXR_NS::GfVec3f> displayColor;
Primvar<float> displayOpacity;
geomMesh.GetFaceVertexCountsAttr().Get(&faceVertexCounts, 0);
geomMesh.GetFaceVertexIndicesAttr().Get(&faceVertexIndices, 0);
geomMesh.GetPointsAttr().Get(&points, 0);
UsdAttribute normalsAttr = geomMesh.GetNormalsAttr();
if (readPrimvar(primvarsAPI, UsdGeomTokens->normals, normals)) {
} else if (normalsAttr.IsAuthored()) {
normalsAttr.Get(&normals.values, 0);
normals.interpolation = geomMesh.GetNormalsInterpolation();
}
if (readPrimvar(primvarsAPI, TfToken("st"), uvs)) {
} else if (readPrimvar(primvarsAPI, TfToken("st0"), uvs)) {
} else if (readPrimvar(primvarsAPI, TfToken("UVMap"), uvs)) {
}
readPrimvar(primvarsAPI, UsdGeomTokens->primvarsDisplayColor, displayColor);
readPrimvar(primvarsAPI, UsdGeomTokens->primvarsDisplayOpacity, displayOpacity);
ASSERT_ARRAY(faceVertexCounts, data.faceVertexCounts, "faceVertexCounts");
ASSERT_ARRAY(faceVertexIndices, data.faceVertexIndices, "faceVertexIndices");
ASSERT_ARRAY(points, data.points, "points");
ASSERT_ARRAY(normals.values, data.normals.values, "normals.values");
ASSERT_ARRAY(normals.indices, data.normals.indices, "normals.indices");
ASSERT_ARRAY(uvs.values, data.uvs.values, "uvs.values");
ASSERT_ARRAY(uvs.indices, data.uvs.indices, "uvs.indices");
ASSERT_ARRAY(displayColor.values, data.displayColor.values, "displayColor.values");
ASSERT_ARRAY(displayColor.indices, data.displayColor.indices, "displayColor.indices");
ASSERT_ARRAY(displayOpacity.values, data.displayOpacity.values, "displayOpacity.values");
ASSERT_ARRAY(displayOpacity.indices, data.displayOpacity.indices, "displayOpacity.indices");
if (normals.indices.size()) {
ASSERT_EQ(normals.interpolation, data.normals.interpolation);
}
if (uvs.indices.size()) {
ASSERT_EQ(uvs.interpolation, data.uvs.interpolation);
}
if (displayColor.indices.size()) {
ASSERT_EQ(displayColor.interpolation, data.displayColor.interpolation);
}
if (displayOpacity.indices.size()) {
ASSERT_EQ(displayOpacity.interpolation, data.displayOpacity.interpolation);
}
}
void
assertAnimation(PXR_NS::UsdStageRefPtr stage, const std::string& path, const AnimationData& data)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
// We only test the animation samples given in the data. This means that the data can have fewer
// samples than the actual USD, so that the entire animation doesn't have to be copied into the
// test
for (auto pair = data.orient.begin(); pair != data.orient.end(); pair++) {
float time = pair->first;
GfQuatf orientValue;
extractUsdAttribute<GfQuatf>(
prim, TfToken("xformOp:orient"), &orientValue, UsdTimeCode(time));
ASSERT_QUATF(orientValue,
data.orient.at(time),
std::string("xformOp:orient[") + std::to_string(time) + "]");
}
for (auto pair = data.scale.begin(); pair != data.scale.end(); pair++) {
float time = pair->first;
GfVec3f scaleValue;
extractUsdAttribute<GfVec3f>(
prim, TfToken("xformOp:scale"), &scaleValue, UsdTimeCode(time));
ASSERT_VEC3F(scaleValue,
data.scale.at(time),
std::string("xformOp:scale[") + std::to_string(time) + "]");
}
for (auto pair = data.translate.begin(); pair != data.translate.end(); pair++) {
float time = pair->first;
GfVec3d translateValue;
extractUsdAttribute<GfVec3d>(
prim, TfToken("xformOp:translate"), &translateValue, UsdTimeCode(time));
ASSERT_VEC3D(translateValue,
data.translate.at(time),
std::string("xformOp:translate[") + std::to_string(time) + "]");
}
}
void
assertCamera(PXR_NS::UsdStageRefPtr stage, const std::string& path, const CameraData& cameraData)
{
const bool WARN_IF_ATTRIBUTE_NOT_FOUND = false;
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
// The transformations for cameras in our USD assets tend to be stored by a parent node. We
// first verify that the camera's transform is correct by extracting it from the prim's parent
UsdPrim parent = prim.GetParent();
ASSERT_TRUE(parent);
GfVec3d translation;
GfQuatf rotation;
GfVec3f scale;
if (extractUsdAttribute<GfVec3d>(parent, TfToken("xformOp:translate"), &translation)) {
ASSERT_VEC3D(
translation, cameraData.translate, path + "'s parent translation does not match\n");
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No translation attribute found for %s\n", path.c_str());
}
if (extractUsdAttribute<GfQuatf>(parent, TfToken("xformOp:orient"), &rotation)) {
ASSERT_QUATF(rotation, cameraData.orient, path + "'s parent rotation does not match\n");
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No rotation attribute found for %s\n", path.c_str());
}
if (extractUsdAttribute<GfVec3f>(parent, TfToken("xformOp:scale"), &scale)) {
ASSERT_VEC3F(scale, cameraData.scale, path + "'s parent scale does not match\n");
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No scale attribute found for %s\n", path.c_str());
}
// Next, we check the camera data itself
UsdGeomCamera camera(prim);
ASSERT_TRUE(camera) << path << " could not be cast to camera\n";
GfVec2f clippingRange;
if (camera.GetClippingRangeAttr().Get(&clippingRange)) {
ASSERT_VEC2F(clippingRange, cameraData.clippingRange, path + "'s clipping range does not match\n");
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No clipping range attribute found for %s\n", path.c_str());
}
float focalLength;
if (camera.GetFocalLengthAttr().Get(&focalLength)) {
ASSERT_FLOAT_EQ(focalLength, cameraData.focalLength) << path << " focal length does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No focal length attribute found for %s\n", path.c_str());
}
float focusDistance;
if (camera.GetFocusDistanceAttr().Get(&focusDistance)) {
ASSERT_FLOAT_EQ(focusDistance, cameraData.focusDistance) << path << " focus distance does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No focus distance attribute found for %s\n", path.c_str());
}
float fStop;
if (camera.GetFStopAttr().Get(&fStop)) {
ASSERT_FLOAT_EQ(fStop, cameraData.fStop) << path << " fStop does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No fStop attribute found for %s\n", path.c_str());
}
float horizontalAperture;
if (camera.GetHorizontalApertureAttr().Get(&horizontalAperture)) {
ASSERT_FLOAT_EQ(horizontalAperture, cameraData.horizontalAperture)
<< path << " horizontal aperture does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No horizontal aperture attribute found for %s\n", path.c_str());
}
TfToken projection;
if (camera.GetProjectionAttr().Get(&projection)) {
ASSERT_EQ(projection, TfToken(cameraData.projection)) << path << " projection does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No projection attribute found for %s\n", path.c_str());
}
float verticalAperture;
if (camera.GetVerticalApertureAttr().Get(&verticalAperture)) {
ASSERT_FLOAT_EQ(verticalAperture, cameraData.verticalAperture)
<< path << " vertical aperture does not match\n";
} else if (WARN_IF_ATTRIBUTE_NOT_FOUND) {
TF_WARN("No vertical aperture attribute found for %s\n", path.c_str());
}
}
void
assertLight(PXR_NS::UsdStageRefPtr stage, const std::string& path, const LightData& lightData)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
// The transformations for lights in our USD assets tend to be stored by a parent node. We
// first verify that the light's transform is correct by extracting it from the prim's parent
UsdPrim parent = prim.GetParent();
ASSERT_TRUE(parent);
GfVec3d translation;
GfQuatf rotation;
GfVec3f scale;
// The transformations for lights in our USD assets tend to be stored by a parent node
if (lightData.translation) {
ASSERT_TRUE(
extractUsdAttribute<GfVec3d>(parent, TfToken("xformOp:translate"), &translation))
<< "Expected translation attribute not found for " << path << "\n";
ASSERT_VEC3D(translation,
lightData.translation.value(),
path + "'s parent translation does not match\n");
}
if (lightData.rotation) {
ASSERT_TRUE(extractUsdAttribute<GfQuatf>(parent, TfToken("xformOp:orient"), &rotation))
<< "Expected orient attribute not found for " << path << "\n";
ASSERT_QUATF(
rotation, lightData.rotation.value(), path + "'s parent rotation does not match\n");
}
if (lightData.scale) {
ASSERT_TRUE(extractUsdAttribute<GfVec3f>(parent, TfToken("xformOp:scale"), &scale))
<< "Expected scale attribute not found for " << path << "\n";
ASSERT_VEC3F(scale, lightData.scale.value(), path + "'s parent scale does not match\n");
}
// Next, we check the light data itself
if (prim.IsA<UsdLuxSphereLight>()) {
UsdLuxSphereLight sphereLight(prim);
ASSERT_TRUE(sphereLight) << path << " could not be cast to sphere light\n";
if (lightData.color) {
PXR_NS::GfVec3f color;
ASSERT_TRUE(sphereLight.GetColorAttr().Get(&color))
<< path << " is missing expected color attribute\n";
ASSERT_VEC3F(color, lightData.color.value(), path + " color does not match\n");
}
if (lightData.intensity) {
float intensity;
ASSERT_TRUE(sphereLight.GetIntensityAttr().Get(&intensity))
<< path << " is missing expected intensity attribute\n";
ASSERT_FLOAT_EQ(intensity, lightData.intensity.value())
<< path << " intensity does not match\n";
}
if (lightData.radius) {
float radius;
ASSERT_TRUE(sphereLight.GetRadiusAttr().Get(&radius))
<< path << " is missing expected radius attribute\n";
ASSERT_FLOAT_EQ(radius, lightData.radius.value()) << path << " radius does not match\n";
}
} else if (prim.IsA<UsdLuxDistantLight>()) {
UsdLuxDistantLight distantLight(prim);
ASSERT_TRUE(distantLight) << path << " could not be cast to distant light\n";
if (lightData.color) {
PXR_NS::GfVec3f color;
ASSERT_TRUE(distantLight.GetColorAttr().Get(&color))
<< path << " is missing expected color attribute\n";
ASSERT_VEC3F(color, lightData.color.value(), path + " color does not match\n");
}
if (lightData.intensity) {
float intensity;
ASSERT_TRUE(distantLight.GetIntensityAttr().Get(&intensity))
<< path << " is missing expected intensity attribute\n";
ASSERT_FLOAT_EQ(intensity, lightData.intensity.value())
<< path << " intensity does not match\n";
}
// Distant lights don't have a radius
} else if (prim.IsA<UsdLuxDiskLight>()) {
UsdLuxDiskLight diskLight(prim);
ASSERT_TRUE(diskLight) << path << " could not be cast to disk light\n";
if (lightData.color) {
PXR_NS::GfVec3f color;
ASSERT_TRUE(diskLight.GetColorAttr().Get(&color))
<< path << " is missing expected color attribute\n";
ASSERT_VEC3F(color, lightData.color.value(), path + " color does not match\n");
}
if (lightData.intensity) {
float intensity;
ASSERT_TRUE(diskLight.GetIntensityAttr().Get(&intensity))
<< path << " is missing expected intensity attribute\n";
ASSERT_FLOAT_EQ(intensity, lightData.intensity.value())
<< path << " intensity does not match\n";
}
if (lightData.radius) {
float radius;
ASSERT_TRUE(diskLight.GetRadiusAttr().Get(&radius))
<< path << " is missing expected radius attribute\n";
ASSERT_FLOAT_EQ(radius, lightData.radius.value()) << path << " radius does not match\n";
}
} else if (prim.IsA<UsdLuxRectLight>()) {
ASSERT_TRUE(false) << "Rectangle lights are not supported yet on import\n";
/*
Uncomment this once we support import of rectangle lights
UsdLuxRectLight rectLight(prim);
ASSERT_TRUE(rectLight);
ASSERT_VEC3F(rectLight.color, lightData.color);
ASSERT_FLOAT_EQ(rectLight.intensity, lightData.intensity);
// Rectangle specific attributes
ASSERT_FLOAT_EQ(rectLight.length[0], lightData.length[0]);
ASSERT_FLOAT_EQ(rectLight.length[1], lightData.length[1]);
*/
} else if (prim.IsA<UsdLuxDomeLight>()) {
ASSERT_TRUE(false) << "Dome lights are not supported yet on import\n";
/*
Uncomment this once we support import of dome lights
UsdLuxDomeLight domeLight(prim);
ASSERT_TRUE(domeLight);
ASSERT_VEC3F(domeLight.color, lightData.color);
ASSERT_FLOAT_EQ(domeLight.intensity, lightData.intensity);
// Add texture test once we support this on import
*/
} else {
ASSERT_TRUE(false) << "Expected a supported light, but encountered a prim of type \""
<< prim.GetTypeName().GetString() << "\" at \"" << path << "\"\n";
}
// Spotlights use shaping APIs
if (prim.HasAPI<UsdLuxShapingAPI>()) {
UsdLuxShapingAPI shapingAPI(prim);
// Shaping API specific attributes
if (lightData.coneAngle) {
float coneAngle;
ASSERT_TRUE(shapingAPI.GetShapingConeAngleAttr().Get(&coneAngle))
<< path << " is missing expected angle attribute\n";
ASSERT_FLOAT_EQ(coneAngle, lightData.coneAngle.value())
<< path << " cone angle does not match\n";
}
if (lightData.coneFalloff) {
float coneFalloff;
ASSERT_TRUE(shapingAPI.GetShapingConeSoftnessAttr().Get(&coneFalloff))
<< path << " is missing expected softness attribute\n";
ASSERT_FLOAT_EQ(coneFalloff, lightData.coneFalloff.value())
<< path << " cone falloff does not match\n";
}
}
}
void
assertPoints(PXR_NS::UsdStageRefPtr stage, const std::string& path, const PointsData& data)
{
UsdPrim prim = stage->GetPrimAtPath(SdfPath(path));
ASSERT_TRUE(prim);
UsdGeomPoints geomPoints(prim);
ASSERT_TRUE(geomPoints);
VtVec3fArray points;
geomPoints.GetPointsAttr().Get(&points, 0);
ASSERT_EQ(points.size(), data.pointsCount);
}
#define ASSERT_INPUT(...) assertInput(__VA_ARGS__)
#define ASSERT_INPUT_FIELD(...) assertInputField(__VA_ARGS__)
#define ASSERT_INPUT_PATH(...) assertInputPath(__VA_ARGS__)
template<typename T>
void
assertInputField(const UsdShadeShader& shader, const std::string& name, const T& value)
{
auto attr = shader.GetInput(TfToken(name));
if (attr) {
auto valueAttrs = attr.GetValueProducingAttributes();
if (valueAttrs.size()) {
T actual;
valueAttrs.front().Get(&actual);
ASSERT_EQ(actual, value);
return;
}
}
// TODO check if attr missing
}
// Cannot reuse assertInputField for this since SdfAssetPath equality fails for ::resolvedPath,
// so here we match only the ::assetPath.
void
assertInputPath(const UsdShadeShader& shader, const std::string& name, const std::string& value)
{
auto attr = shader.GetInput(TfToken(name));
if (attr) {
auto valueAttrs = attr.GetValueProducingAttributes();
if (valueAttrs.size()) {
SdfAssetPath actualAssetPath;
valueAttrs.front().Get(&actualAssetPath);
const std::string actual = TfNormPath(actualAssetPath.GetAssetPath());
ASSERT_EQ(actual, value);
return;
}
}
// TODO check if attr missing
}
void
assertMaterial(PXR_NS::UsdStageRefPtr stage, const std::string& path, const MaterialData& data)
{
const std::string currentDir = TfAbsPath(".");
const SdfPath materialPath = SdfPath(path);
UsdPrim prim = stage->GetPrimAtPath(materialPath);
ASSERT_TRUE(prim);
UsdShadeMaterial material(prim);
ASSERT_TRUE(material);
SdfPathVector connections;
UsdShadeShader shader;
UsdAttribute surface = material.GetSurfaceAttr();
surface.GetConnections(&connections);
ASSERT_TRUE(connections.size() > 0);
const SdfPath shaderPath = connections[0].GetPrimPath();
shader = UsdShadeShader(stage->GetPrimAtPath(shaderPath));
TfToken shaderId;
shader.GetShaderId(&shaderId);
ASSERT_TRUE(shaderId == TfToken("UsdPreviewSurface"));
auto assertInput = [&](const TfToken& name, const InputData& data) {
UsdShadeInput shadeInput = shader.GetInput(name);
if (!shadeInput)
return;
if (shadeInput.HasConnectedSource()) {
UsdShadeInput::SourceInfoVector sources = shadeInput.GetConnectedSources();
for (UsdShadeConnectionSourceInfo source : sources) {
const SdfPath sourcePath = source.source.GetPath();
const UsdShadeShader textureShader =
UsdShadeShader(stage->GetPrimAtPath(sourcePath));
if (sourcePath == materialPath) {
ASSERT_INPUT_FIELD(textureShader, name, data.value);
} else {
const SdfPath textureShaderPath = textureShader.GetPath();
TfToken uvTextureId;
textureShader.GetShaderId(&uvTextureId);
ASSERT_EQ(uvTextureId.GetString(), std::string("UsdUVTexture"));
const std::string assetPath = TfNormPath(currentDir + "/" + data.file);
ASSERT_INPUT_PATH(textureShader, "file", assetPath);
// TODO? ASSERT_IMAGE(ctx, assetPath, input.image);
ASSERT_INPUT_FIELD(textureShader, "wrapS", data.wrapS);
ASSERT_INPUT_FIELD(textureShader, "wrapT", data.wrapT);
ASSERT_INPUT_FIELD(textureShader, "scale", data.scale);
ASSERT_INPUT_FIELD(textureShader, "bias", data.bias);
ASSERT_INPUT_FIELD(textureShader, "fallback", data.value);
ASSERT_EQ(data.channel, source.sourceName);
UsdShadeInput stInput = textureShader.GetInput(TestTokens->st);
if (!stInput)
return;
if (stInput.HasConnectedSource()) {
UsdShadeInput::SourceInfoVector sources = stInput.GetConnectedSources();
for (UsdShadeConnectionSourceInfo source : sources) {
const SdfPath sourcePath = source.source.GetPath();
UsdShadeShader stShader =
UsdShadeShader(stage->GetPrimAtPath(sourcePath));
TfToken shaderId;
stShader.GetShaderId(&shaderId);
if (!data.transformRotation.IsEmpty() ||
!data.transformScale.IsEmpty() ||
!data.transformTranslation.IsEmpty()) {
ASSERT_TRUE(shaderId == TestTokens->UsdTransform2d);
ASSERT_INPUT_FIELD(stShader, "rotation", data.transformRotation);
ASSERT_INPUT_FIELD(stShader, "scale", data.transformScale);
ASSERT_INPUT_FIELD(
stShader, "translation", data.transformTranslation);
} else {
std::string shaderName = stShader.GetPrim().GetName().GetString();
if (shaderName == "texCoordReader") {
ASSERT_TRUE(shaderId == TestTokens->UsdPrimvarReader_float2);
} else {
ASSERT_TRUE(shaderId == TestTokens->UsdTransform2d);
}
}
}
}
}
}
} else {
VtValue actualValue;
shadeInput.Get(&actualValue);
ASSERT_TRUE(actualValue == data.value);
}
};
ASSERT_INPUT(TestTokens->useSpecularWorkflow, data.useSpecularWorkflow);
ASSERT_INPUT(TestTokens->diffuseColor, data.diffuseColor);
ASSERT_INPUT(TestTokens->emissiveColor, data.emissiveColor);
ASSERT_INPUT(TestTokens->specularColor, data.specularColor);
ASSERT_INPUT(TestTokens->normal, data.normal);
ASSERT_INPUT(TestTokens->metallic, data.metallic);
ASSERT_INPUT(TestTokens->roughness, data.roughness);
ASSERT_INPUT(TestTokens->clearcoat, data.clearcoat);
ASSERT_INPUT(TestTokens->clearcoatRoughness, data.clearcoatRoughness);
ASSERT_INPUT(TestTokens->opacity, data.opacity);
ASSERT_INPUT(TestTokens->opacityThreshold, data.opacityThreshold);
ASSERT_INPUT(TestTokens->displacement, data.displacement);
ASSERT_INPUT(TestTokens->occlusion, data.occlusion);
ASSERT_INPUT(TestTokens->ior, data.ior);
}
void
assertRender(const std::string& filename, const std::string& imageFilename)
{
const std::string imageParentPath = TfGetPathName(imageFilename);
TfMakeDirs(imageParentPath, -1, true);
const std::string command = "usdrecord \"" + filename + "\" \"" + imageFilename + "\"";
int result = archSystem(command);
ASSERT_EQ(result, 0);
}