1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "pxr/pxr.h"
26 #include "pxr/usd/pcp/mapExpression.h"
27 #include "pxr/usd/pcp/mapFunction.h"
28 
29 PXR_NAMESPACE_USING_DIRECTIVE
30 
31 static PcpMapFunction
_GetArcFunction(const std::string & source,const std::string & target)32 _GetArcFunction(const std::string & source, const std::string & target)
33 {
34     PcpMapFunction::PathMap pathMap;
35     pathMap[SdfPath(source)] = SdfPath(target);
36     return PcpMapFunction::Create(pathMap, SdfLayerOffset());
37 }
38 
39 int
main(int argc,char ** argv)40 main(int argc, char** argv)
41 {
42     // Here we focus on testing the core PcpMapExpression API;
43     // We don't bother testing convenience API that passes
44     // queries onto the MapFunction value.
45 
46     // Accumulate unique test expressions.
47     std::vector<PcpMapExpression> testExprs;
48 
49     // Null
50     PcpMapExpression nullExpr;
51     testExprs.push_back(nullExpr);
52     TF_AXIOM(nullExpr.IsNull());
53     TF_AXIOM(nullExpr.Evaluate() == PcpMapFunction());
54 
55     // Identity
56     const PcpMapExpression identityExpr = PcpMapExpression::Identity();
57     testExprs.push_back(identityExpr);
58     TF_AXIOM(!identityExpr.IsNull());
59     TF_AXIOM(identityExpr.Evaluate() == PcpMapFunction::Identity());
60 
61     // Swap
62     PcpMapExpression a;
63     PcpMapExpression b = PcpMapExpression::Identity();
64     TF_AXIOM(a.IsNull());
65     TF_AXIOM(!b.IsNull());
66     a.Swap(b);
67     TF_AXIOM(!a.IsNull());
68     TF_AXIOM(b.IsNull());
69     a.Swap(a);
70     TF_AXIOM(!a.IsNull());
71 
72     // Constant (a typical model reference)
73     const PcpMapFunction refFunc =
74         _GetArcFunction("/Model", "/World/anim/Model_1");
75     const PcpMapExpression refExpr = PcpMapExpression::Constant(refFunc);
76     testExprs.push_back(refExpr);
77     TF_AXIOM(refExpr.Evaluate() == refFunc);
78 
79     // Operation: Inverse
80     const PcpMapExpression refExprInverse = refExpr.Inverse();
81     testExprs.push_back(refExprInverse);
82     TF_AXIOM(!refExprInverse.IsNull());
83     TF_AXIOM(refExprInverse.Evaluate() == refFunc.GetInverse());
84 
85     // Operation: AddRootIdentity
86     const PcpMapExpression rootIdentityExpr = refExpr.AddRootIdentity();
87     testExprs.push_back(rootIdentityExpr);
88     TF_AXIOM(refExpr.MapSourceToTarget(SdfPath("/Foo")) == SdfPath());
89     TF_AXIOM(rootIdentityExpr.MapSourceToTarget(SdfPath("/Foo"))
90            == SdfPath("/Foo"));
91 
92     // Operation: Compose
93     const PcpMapExpression rigExpr = PcpMapExpression::Constant(
94         _GetArcFunction("/Rig", "/Model/Rig"));
95     const PcpMapExpression composedExpr = refExpr.Compose(rigExpr);
96     testExprs.push_back(composedExpr);
97     TF_AXIOM(composedExpr.Evaluate() ==
98            _GetArcFunction("/Rig", "/World/anim/Model_1/Rig"));
99 
100     // Operation: Compose + Inverse
101     TF_AXIOM(composedExpr.Inverse().Evaluate() ==
102         _GetArcFunction("/World/anim/Model_1/Rig", "/Rig"));
103 
104     // Variable
105     {
106         // Variable will initial empty function
107         PcpMapExpression::VariableUniquePtr var =
108             PcpMapExpression::NewVariable( PcpMapFunction() );
109         const PcpMapExpression varExpr = var->GetExpression();
110         testExprs.push_back(varExpr);
111         TF_AXIOM(!varExpr.IsNull());
112         TF_AXIOM(varExpr.Evaluate() == var->GetValue());
113         TF_AXIOM(varExpr.Evaluate() == PcpMapFunction());
114 
115         // Test changing value
116         const PcpMapFunction testValue =
117             _GetArcFunction("/A", "/B");
118         var->SetValue(PcpMapFunction(testValue));
119         TF_AXIOM(varExpr.Evaluate() == var->GetValue());
120         TF_AXIOM(varExpr.Evaluate() == testValue);
121 
122         // Test using a variable in a derived expression
123         const PcpMapExpression invVarExpr = var->GetExpression().Inverse();
124         TF_AXIOM(invVarExpr.Evaluate() == testValue.GetInverse());
125 
126         // Test invalidation on changing a variable
127         const PcpMapFunction testValue2 =
128             _GetArcFunction("/A2", "/B2");
129         var->SetValue(PcpMapFunction(testValue2));
130         TF_AXIOM(varExpr.Evaluate() == testValue2);
131         TF_AXIOM(invVarExpr.Evaluate() == testValue2.GetInverse());
132 
133         // Test variable lifetime.
134         // Change the variable value, discard it, and then
135         // re-evaluate derived expressions.
136         const PcpMapFunction testValue3 =
137             _GetArcFunction("/A3", "/B3");
138         var->SetValue(PcpMapFunction(testValue3));
139         var.reset();
140         TF_AXIOM(varExpr.Evaluate() == testValue3);
141         TF_AXIOM(invVarExpr.Evaluate() == testValue3.GetInverse());
142     }
143 
144     // Semi-tricky AddRootIdentity scenario:
145     //
146     // Composing one expression over another expression with an
147     // AddRootIdentity() component can cause there to not be a
148     // root identity mapping in the result.
149     {
150         const PcpMapFunction a_to_b = _GetArcFunction("/A", "/B");
151         const PcpMapFunction b_to_c = _GetArcFunction("/B", "/C");
152         const PcpMapFunction a_to_c = _GetArcFunction("/A", "/C");
153         const PcpMapExpression exp =
154             PcpMapExpression::Constant(b_to_c)
155             .Compose( PcpMapExpression::Constant(a_to_b).AddRootIdentity() );
156         TF_AXIOM(exp.Evaluate() == a_to_c);
157 
158 
159         const PcpMapExpression a_to_c_with_id =
160             PcpMapExpression::Constant(a_to_c).AddRootIdentity();
161         const PcpMapExpression exp_with_id =
162             exp.AddRootIdentity();
163         TF_AXIOM(exp_with_id.Evaluate() == a_to_c_with_id.Evaluate());
164     }
165 
166     // TODO: test equality/inequality for testExprs
167     // XXX do this after flyweighting is in place
168     // also demonstrate that two exprs that evaluate two the
169     // same value won't compare equal if their structure is different
170 }
171