-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathUSDTransforms.cc
342 lines (308 loc) · 10.4 KB
/
USDTransforms.cc
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
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "sdf/usd/usd_parser/USDTransforms.hh"
#include <optional>
#include <utility>
#include <ignition/math/Pose3.hh>
#include <ignition/math/Vector3.hh>
#include "sdf/usd/usd_parser/USDData.hh"
namespace sdf
{
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
const char kXFormOpTranslate[] = {"xformOp:translate"};
const char kXFormOpOrient[] = {"xformOp:orient"};
const char kXFormOpTransform[] = {"xformOp:transform"};
const char kXFormOpScale[] = {"xformOp:scale"};
const char kXFormOpRotateXYZ[] = {"xformOp:rotateXYZ"};
const char kXFormOpRotateZYX[] = {"xformOp:rotateZYX"};
const char kGfVec3fString[] = {"GfVec3f"};
const char kGfVec3dString[] = {"GfVec3d"};
const char kGfQuatfString[] = {"GfQuatf"};
const char kGfQuatdString[] = {"GfQuatd"};
/// \brief Private altimeter data.
class UDSTransforms::Implementation
{
/// \brief Scale of the schema
public: ignition::math::Vector3d scale{1, 1, 1};
/// \brief Rotation of the schema
public: std::optional<ignition::math::Quaterniond> q = std::nullopt;
/// \brief Translation of the schema
public: ignition::math::Vector3d translate{0, 0, 0};
};
/////////////////////////////////////////////////
UDSTransforms::UDSTransforms()
: dataPtr(ignition::utils::MakeImpl<Implementation>())
{
}
//////////////////////////////////////////////////
const ignition::math::Vector3d UDSTransforms::Translation() const
{
return this->dataPtr->translate;
}
//////////////////////////////////////////////////
const ignition::math::Vector3d UDSTransforms::Scale() const
{
return this->dataPtr->scale;
}
//////////////////////////////////////////////////
const std::optional<ignition::math::Quaterniond> UDSTransforms::Rotation() const
{
return this->dataPtr->q;
}
//////////////////////////////////////////////////
void UDSTransforms::SetTranslation(
const ignition::math::Vector3d &_translate)
{
this->dataPtr->translate = _translate;
}
//////////////////////////////////////////////////
void UDSTransforms::SetScale(
const ignition::math::Vector3d &_scale)
{
this->dataPtr->scale = _scale;
}
//////////////////////////////////////////////////
void UDSTransforms::SetRotation(
const ignition::math::Quaterniond &_q)
{
this->dataPtr->q = _q;
}
//////////////////////////////////////////////////
/// \brief This function will parse all the parent transforms of a prim.
/// \param[in] _prim Initial prim to read the transform
/// \param[in] _usdData USDData structure to get info about the prim, for
/// example: metersperunit
/// \param[out] _tfs A vector with all the transforms
/// \param[out] _scale The scale of the prims
/// \param[in] _schemaToStop Name of the prim where the loop will stop
/// reading transforms
void GetAllTransforms(
const pxr::UsdPrim &_prim,
const USDData &_usdData,
std::vector<ignition::math::Pose3d> &_tfs,
ignition::math::Vector3d &_scale,
const std::string &_schemaToStop)
{
pxr::UsdPrim parent = _prim;
double metersPerUnit = 1.0;
std::string upAxis = "Y";
// this assumes that there can only be one stage
const auto stageData = _usdData.FindStage(parent.GetPath().GetName());
if (stageData.second) {
metersPerUnit = stageData.second->MetersPerUnit();
upAxis = stageData.second->UpAxis();
}
while (parent)
{
if (pxr::TfStringify(parent.GetPath()) == _schemaToStop)
{
return;
}
UDSTransforms t = ParseUSDTransform(parent);
ignition::math::Pose3d pose;
_scale *= t.Scale();
pose.Pos() = t.Translation() * metersPerUnit;
// scaling is lost when we convert to pose, so we pre-scale the
// translation to make them match the scaled values.
if (!_tfs.empty()) {
auto& child = _tfs.back();
child.Pos().Set(
child.Pos().X() * t.Scale()[0],
child.Pos().Y() * t.Scale()[1],
child.Pos().Z() * t.Scale()[2]);
}
if (t.Rotation())
{
pose.Rot() = t.Rotation().value();
}
_tfs.push_back(pose);
parent = parent.GetParent();
}
if (upAxis == "Y")
{
// Add additional rotation to match with Z up Axis.
// TODO(anyone) handle upAxis == "X". This is a case that is rarely
// used by other renderers
ignition::math::Pose3d poseUpAxis = ignition::math::Pose3d(
ignition::math::Vector3d(0, 0, 0),
ignition::math::Quaterniond(IGN_PI_2, 0, 0));
_tfs.push_back(poseUpAxis);
}
}
//////////////////////////////////////////////////
void GetTransform(
const pxr::UsdPrim &_prim,
const USDData &_usdData,
ignition::math::Pose3d &_pose,
ignition::math::Vector3d &_scale,
const std::string &_schemaToStop)
{
std::vector<ignition::math::Pose3d> tfs;
GetAllTransforms(_prim, _usdData, tfs, _scale, _schemaToStop);
for (auto & rt : tfs)
{
_pose = rt * _pose;
}
}
//////////////////////////////////////////////////
UDSTransforms ParseUSDTransform(const pxr::UsdPrim &_prim)
{
auto variantGeom = pxr::UsdGeomGprim(_prim);
auto transforms = variantGeom.GetXformOpOrderAttr();
pxr::GfVec3f scale(1, 1, 1);
pxr::GfVec3f translate(0, 0, 0);
pxr::GfQuatf rotationQuad(1, 0, 0, 0);
UDSTransforms t;
pxr::VtTokenArray xformOpOrder;
transforms.Get(&xformOpOrder);
for (const auto &op : xformOpOrder)
{
if (op == kXFormOpScale)
{
auto attribute = _prim.GetAttribute(pxr::TfToken(kXFormOpScale));
if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3fString)
{
attribute.Get(&scale);
}
else if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3dString)
{
pxr::GfVec3d scaleTmp(1, 1, 1);
attribute.Get(&scaleTmp);
scale[0] = static_cast<float>(scaleTmp[0]);
scale[1] = static_cast<float>(scaleTmp[1]);
scale[2] = static_cast<float>(scaleTmp[2]);
}
t.SetScale(ignition::math::Vector3d(scale[0], scale[1], scale[2]));
}
else if (op == kXFormOpRotateZYX || op == kXFormOpRotateXYZ)
{
pxr::GfVec3f rotationEuler(0, 0, 0);
pxr::UsdAttribute attribute;
if (op == kXFormOpRotateZYX)
{
attribute = _prim.GetAttribute(pxr::TfToken(kXFormOpRotateZYX));
}
else
{
attribute = _prim.GetAttribute(pxr::TfToken(kXFormOpRotateXYZ));
}
if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3fString)
{
attribute.Get(&rotationEuler);
}
else if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3dString)
{
pxr::GfVec3d rotationEulerTmp(0, 0, 0);
attribute.Get(&rotationEulerTmp);
rotationEuler[0] = static_cast<float>(rotationEulerTmp[0]);
rotationEuler[1] = static_cast<float>(rotationEulerTmp[1]);
rotationEuler[2] = static_cast<float>(rotationEulerTmp[2]);
}
ignition::math::Quaterniond qX, qY, qZ;
ignition::math::Angle angleX(IGN_DTOR(rotationEuler[0]));
ignition::math::Angle angleY(IGN_DTOR(rotationEuler[1]));
ignition::math::Angle angleZ(IGN_DTOR(rotationEuler[2]));
qX = ignition::math::Quaterniond(angleX.Normalized().Radian(), 0, 0);
qY = ignition::math::Quaterniond(0, angleY.Normalized().Radian(), 0);
qZ = ignition::math::Quaterniond(0, 0, angleZ.Normalized().Radian());
// TODO(ahcorde) This part should be reviewed, revisit how rotateXYZ
// and rotateZYX are handle.
// Related issue https://github.com/ignitionrobotics/sdformat/issues/926
// if (op == kXFormOpRotateZYX)
// {
// std::swap(angleX, angleZ);
// }
t.SetRotation((qX * qY) * qZ);
}
else if (op == kXFormOpTranslate)
{
auto attribute = _prim.GetAttribute(pxr::TfToken(kXFormOpTranslate));
if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3fString)
{
attribute.Get(&translate);
}
else if (attribute.GetTypeName().GetCPPTypeName() == kGfVec3dString)
{
pxr::GfVec3d translateTmp(0, 0, 0);
attribute.Get(&translateTmp);
translate[0] = static_cast<float>(translateTmp[0]);
translate[1] = static_cast<float>(translateTmp[1]);
translate[2] = static_cast<float>(translateTmp[2]);
}
t.SetTranslation(ignition::math::Vector3d(
translate[0],
translate[1],
translate[2]));
}
else if (op == kXFormOpOrient)
{
auto attribute = _prim.GetAttribute(pxr::TfToken(kXFormOpOrient));
if (attribute.GetTypeName().GetCPPTypeName() == kGfQuatfString)
{
attribute.Get(&rotationQuad);
}
else if (attribute.GetTypeName().GetCPPTypeName() == kGfQuatdString)
{
pxr::GfQuatd rotationQuadTmp;
attribute.Get(&rotationQuadTmp);
rotationQuad.SetImaginary(
rotationQuadTmp.GetImaginary()[0],
rotationQuadTmp.GetImaginary()[1],
rotationQuadTmp.GetImaginary()[2]);
rotationQuad.SetReal(rotationQuadTmp.GetReal());
}
ignition::math::Quaterniond q(
rotationQuad.GetReal(),
rotationQuad.GetImaginary()[0],
rotationQuad.GetImaginary()[1],
rotationQuad.GetImaginary()[2]);
t.SetRotation(q);
}
if (op == kXFormOpTransform)
{
// TODO(koonpeng) Shear is lost (does sdformat support it?).
pxr::GfMatrix4d transform;
_prim.GetAttribute(pxr::TfToken(kXFormOpTransform)).Get(&transform);
const auto rot = transform.RemoveScaleShear();
const auto scaleShear = transform * rot.GetInverse();
t.SetScale(ignition::math::Vector3d(
scaleShear[0][0],
scaleShear[1][1],
scaleShear[2][2]));
const auto rotQuat = rot.ExtractRotationQuat();
t.SetTranslation(ignition::math::Vector3d(
transform[3][0],
transform[3][1],
transform[3][2]));
ignition::math::Quaterniond q(
rotQuat.GetReal(),
rotQuat.GetImaginary()[0],
rotQuat.GetImaginary()[1],
rotQuat.GetImaginary()[2]
);
t.SetRotation(q);
}
}
return t;
}
}
}
}