1 //
2 // Copyright 2016 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 #include "pxr/usd/usdGeom/hermiteCurves.h"
25 #include "pxr/usd/usd/schemaRegistry.h"
26 #include "pxr/usd/usd/typed.h"
27 
28 #include "pxr/usd/sdf/types.h"
29 #include "pxr/usd/sdf/assetPath.h"
30 
31 PXR_NAMESPACE_OPEN_SCOPE
32 
33 // Register the schema with the TfType system.
TF_REGISTRY_FUNCTION(TfType)34 TF_REGISTRY_FUNCTION(TfType)
35 {
36     TfType::Define<UsdGeomHermiteCurves,
37         TfType::Bases< UsdGeomCurves > >();
38 
39     // Register the usd prim typename as an alias under UsdSchemaBase. This
40     // enables one to call
41     // TfType::Find<UsdSchemaBase>().FindDerivedByName("HermiteCurves")
42     // to find TfType<UsdGeomHermiteCurves>, which is how IsA queries are
43     // answered.
44     TfType::AddAlias<UsdSchemaBase, UsdGeomHermiteCurves>("HermiteCurves");
45 }
46 
47 /* virtual */
~UsdGeomHermiteCurves()48 UsdGeomHermiteCurves::~UsdGeomHermiteCurves()
49 {
50 }
51 
52 /* static */
53 UsdGeomHermiteCurves
Get(const UsdStagePtr & stage,const SdfPath & path)54 UsdGeomHermiteCurves::Get(const UsdStagePtr &stage, const SdfPath &path)
55 {
56     if (!stage) {
57         TF_CODING_ERROR("Invalid stage");
58         return UsdGeomHermiteCurves();
59     }
60     return UsdGeomHermiteCurves(stage->GetPrimAtPath(path));
61 }
62 
63 /* static */
64 UsdGeomHermiteCurves
Define(const UsdStagePtr & stage,const SdfPath & path)65 UsdGeomHermiteCurves::Define(
66     const UsdStagePtr &stage, const SdfPath &path)
67 {
68     static TfToken usdPrimTypeName("HermiteCurves");
69     if (!stage) {
70         TF_CODING_ERROR("Invalid stage");
71         return UsdGeomHermiteCurves();
72     }
73     return UsdGeomHermiteCurves(
74         stage->DefinePrim(path, usdPrimTypeName));
75 }
76 
77 /* virtual */
_GetSchemaKind() const78 UsdSchemaKind UsdGeomHermiteCurves::_GetSchemaKind() const
79 {
80     return UsdGeomHermiteCurves::schemaKind;
81 }
82 
83 /* static */
84 const TfType &
_GetStaticTfType()85 UsdGeomHermiteCurves::_GetStaticTfType()
86 {
87     static TfType tfType = TfType::Find<UsdGeomHermiteCurves>();
88     return tfType;
89 }
90 
91 /* static */
92 bool
_IsTypedSchema()93 UsdGeomHermiteCurves::_IsTypedSchema()
94 {
95     static bool isTyped = _GetStaticTfType().IsA<UsdTyped>();
96     return isTyped;
97 }
98 
99 /* virtual */
100 const TfType &
_GetTfType() const101 UsdGeomHermiteCurves::_GetTfType() const
102 {
103     return _GetStaticTfType();
104 }
105 
106 UsdAttribute
GetTangentsAttr() const107 UsdGeomHermiteCurves::GetTangentsAttr() const
108 {
109     return GetPrim().GetAttribute(UsdGeomTokens->tangents);
110 }
111 
112 UsdAttribute
CreateTangentsAttr(VtValue const & defaultValue,bool writeSparsely) const113 UsdGeomHermiteCurves::CreateTangentsAttr(VtValue const &defaultValue, bool writeSparsely) const
114 {
115     return UsdSchemaBase::_CreateAttr(UsdGeomTokens->tangents,
116                        SdfValueTypeNames->Vector3fArray,
117                        /* custom = */ false,
118                        SdfVariabilityVarying,
119                        defaultValue,
120                        writeSparsely);
121 }
122 
123 namespace {
124 static inline TfTokenVector
_ConcatenateAttributeNames(const TfTokenVector & left,const TfTokenVector & right)125 _ConcatenateAttributeNames(const TfTokenVector& left,const TfTokenVector& right)
126 {
127     TfTokenVector result;
128     result.reserve(left.size() + right.size());
129     result.insert(result.end(), left.begin(), left.end());
130     result.insert(result.end(), right.begin(), right.end());
131     return result;
132 }
133 }
134 
135 /*static*/
136 const TfTokenVector&
GetSchemaAttributeNames(bool includeInherited)137 UsdGeomHermiteCurves::GetSchemaAttributeNames(bool includeInherited)
138 {
139     static TfTokenVector localNames = {
140         UsdGeomTokens->tangents,
141     };
142     static TfTokenVector allNames =
143         _ConcatenateAttributeNames(
144             UsdGeomCurves::GetSchemaAttributeNames(true),
145             localNames);
146 
147     if (includeInherited)
148         return allNames;
149     else
150         return localNames;
151 }
152 
153 PXR_NAMESPACE_CLOSE_SCOPE
154 
155 // ===================================================================== //
156 // Feel free to add custom code below this line. It will be preserved by
157 // the code generator.
158 //
159 // Just remember to wrap code in the appropriate delimiters:
160 // 'PXR_NAMESPACE_OPEN_SCOPE', 'PXR_NAMESPACE_CLOSE_SCOPE'.
161 // ===================================================================== //
162 // --(BEGIN CUSTOM CODE)--
163 
164 PXR_NAMESPACE_OPEN_SCOPE
165 
PointAndTangentArrays(const VtArray<GfVec3f> & interleaved)166 UsdGeomHermiteCurves::PointAndTangentArrays::PointAndTangentArrays(
167     const VtArray<GfVec3f>& interleaved)
168 {
169     if (interleaved.empty()){
170         return;
171     }
172     if (interleaved.size() % 2 != 0) {
173         TF_CODING_ERROR(
174             "Cannot separate odd-shaped interleaved points and tangents "
175             "data.");
176         return;
177     }
178     _points.resize(interleaved.size() / 2);
179     _tangents.resize(interleaved.size() / 2);
180     auto interleavedIt = interleaved.cbegin();
181     auto pointsIt = _points.begin();
182     auto tangentsIt = _tangents.begin();
183     while (interleavedIt != interleaved.end()) {
184         *pointsIt = *interleavedIt;
185         std::advance(pointsIt, 1);
186         std::advance(interleavedIt, 1);
187         *tangentsIt = *interleavedIt;
188         std::advance(tangentsIt, 1);
189         std::advance(interleavedIt, 1);
190     }
191     TF_VERIFY(pointsIt == _points.end());
192     TF_VERIFY(tangentsIt == _tangents.end());
193 }
194 
Interleave() const195 VtVec3fArray UsdGeomHermiteCurves::PointAndTangentArrays::Interleave() const
196 {
197     if (IsEmpty()) {
198         return VtVec3fArray();
199     }
200     VtVec3fArray interleaved(GetPoints().size() * 2);
201     auto interleavedIt = interleaved.begin();
202     auto pointsIt = GetPoints().cbegin();
203     auto tangentsIt = GetTangents().cbegin();
204     while (interleavedIt != interleaved.end()) {
205         *interleavedIt = *pointsIt;
206         std::advance(pointsIt, 1);
207         std::advance(interleavedIt, 1);
208         *interleavedIt = *tangentsIt;
209         std::advance(tangentsIt, 1);
210         std::advance(interleavedIt, 1);
211     }
212     TF_VERIFY(pointsIt == GetPoints().cend());
213     TF_VERIFY(tangentsIt == GetTangents().cend());
214     return interleaved;
215 }
216 
217 PXR_NAMESPACE_CLOSE_SCOPE
218