All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Gf.h
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 #ifndef _GUSD_UT_GF_H_
25 #define _GUSD_UT_GF_H_
26 
27 #include "pxr/pxr.h"
28 
29 #include "UT_TypeTraits.h"
30 
31 // Conversion specializations require full defs for some types.
32 #include "pxr/base/gf/quaternion.h"
33 #include "pxr/base/gf/quatd.h"
34 #include "pxr/base/gf/quatf.h"
35 #include "pxr/base/gf/quath.h"
36 #include "pxr/base/gf/vec4d.h"
37 #include "pxr/base/gf/vec4f.h"
38 
39 #include <SYS/SYS_TypeTraits.h>
40 #include <SYS/SYS_Version.h>
41 #include <UT/UT_VectorTypes.h>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
46 struct GusdUT_Gf
47 {
59  template <class GF_OR_UT_TYPE>
61  {
62  static const bool isSpecialized = false;
63  };
64 
68  template <class GF_OR_UT_TYPE>
69  struct Castable
70  {
71  static const bool value = true;
72  };
73 
109  template <class T>
110  static inline const typename
111  TypeEquivalence<T>::AltType* Cast(const T* val);
112 
113  template <class T>
114  static inline typename
116 
117  template <class T>
118  static inline const typename
119  TypeEquivalence<T>::AltType& Cast(const T& val);
120 
121  template <class T>
122  static inline typename
125 
135  template <class FROM, class TO>
136  static inline const TO* Cast(const FROM* val);
137 
138  template <class FROM, class TO>
139  static inline TO* Cast(FROM* val);
140 
141  template <class FROM, class TO>
142  static inline const TO& Cast(const FROM& val);
143 
144  template <class FROM, class TO>
145  static inline TO& Cast(FROM& val);
147 
148 
154  template <class FROM, class TO>
155  static inline void Convert(const FROM& from, TO& to);
156 
157 
166  template <class T>
167  static inline void Convert(const GfQuaternion& from, UT_QuaternionT<T>& to);
168 
169  template <class T>
170  static inline void Convert(const GfQuatd& from, UT_QuaternionT<T>& to);
171 
172  template <class T>
173  static inline void Convert(const GfQuatf& from, UT_QuaternionT<T>& to);
174 
175  template <class T>
176  static inline void Convert(const GfQuath& from, UT_QuaternionT<T>& to);
177 
178  template <class T>
179  static inline void Convert(const GfVec4d& from, UT_QuaternionT<T>& to);
180 
181  template <class T>
182  static inline void Convert(const GfVec4f& from, UT_QuaternionT<T>& to);
183 
184  template <class T>
185  static inline void Convert(const UT_QuaternionT<T>& from, GfQuaternion& to);
186 
187  template <class T>
188  static inline void Convert(const UT_QuaternionT<T>& from, GfQuatd& to);
189 
190  template <class T>
191  static inline void Convert(const UT_QuaternionT<T>& from, GfQuatf& to);
192 
193  template <class T>
194  static inline void Convert(const UT_QuaternionT<T>& from, GfQuath& to);
195 
196  template <class T>
197  static inline void Convert(const UT_QuaternionT<T>& from, GfVec4d& to);
198 
199  template <class T>
200  static inline void Convert(const UT_QuaternionT<T>& from, GfVec4f& to);
202 
203 private:
204  template <class T, class GFQUAT>
205  static inline void _ConvertQuat(const GFQUAT& from, UT_QuaternionT<T>& to);
206 
207  template <class T>
208  static inline void _AssertIsPodTuple();
209 
210  template <class FROM, class TO>
211  static inline void _AssertCanCast();
212 
213  // Our casting tricks assume that the typedefs set in SYS_Types are
214  // referencing the types we think they are. Verify our assumptions.
215  static_assert(std::is_same<fpreal32,float>::value,
216  "std::is_same<fpreal32,float>::value");
217  static_assert(std::is_same<fpreal64,double>::value,
218  "std::is_same<fpreal64,double>::value");
219 };
220 
221 
223 #define _GUSDUT_DECLARE_UNCASTABLE(TYPE) \
224  template <> \
225  struct GusdUT_Gf::Castable<TYPE> \
226  { \
227  static const bool value = false; \
228  };
229 
230 
233 #define _GUSDUT_DECLARE_PARTIAL_EQUIVALENCE(TYPE,GFTYPE,UTTYPE,ALTTYPE) \
234  template <> \
235  struct GusdUT_Gf::TypeEquivalence<TYPE> { \
236  static const bool isSpecialized = true; \
237  using GfType = GFTYPE; \
238  using UtType = UTTYPE; \
239  using AltType = ALTTYPE; \
240  };
241 
246 #define _GUSDUT_DECLARE_EQUIVALENCE(GFTYPE,UTTYPE) \
247  _GUSDUT_DECLARE_PARTIAL_EQUIVALENCE(GFTYPE,GFTYPE,UTTYPE,UTTYPE); \
248  _GUSDUT_DECLARE_PARTIAL_EQUIVALENCE(UTTYPE,GFTYPE,UTTYPE,GFTYPE);
249 
251 GUSDUT_DECLARE_POD_TUPLE(class GfVec2h, fpreal16, 2);
252 GUSDUT_DECLARE_POD_TUPLE(class GfVec3h, fpreal16, 3);
253 GUSDUT_DECLARE_POD_TUPLE(class GfVec4h, fpreal16, 4);
254 
255 GUSDUT_DECLARE_POD_TUPLE(class GfVec2f, fpreal32, 2);
256 GUSDUT_DECLARE_POD_TUPLE(class GfVec3f, fpreal32, 3);
257 GUSDUT_DECLARE_POD_TUPLE(class GfVec4f, fpreal32, 4);
258 
259 GUSDUT_DECLARE_POD_TUPLE(class GfVec2d, fpreal64, 2);
260 GUSDUT_DECLARE_POD_TUPLE(class GfVec3d, fpreal64, 3);
261 GUSDUT_DECLARE_POD_TUPLE(class GfVec4d, fpreal64, 4);
262 
263 GUSDUT_DECLARE_POD_TUPLE(class GfVec2i, int32, 2);
264 GUSDUT_DECLARE_POD_TUPLE(class GfVec3i, int32, 3);
265 GUSDUT_DECLARE_POD_TUPLE(class GfVec4i, int32, 4);
266 
267 GUSDUT_DECLARE_POD_TUPLE(class GfQuath, fpreal16, 4);
268 GUSDUT_DECLARE_POD_TUPLE(class GfQuatf, fpreal32, 4);
269 GUSDUT_DECLARE_POD_TUPLE(class GfQuatd, fpreal64, 4);
270 
271 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix2f, fpreal32, 4);
272 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix3f, fpreal32, 9);
273 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix4f, fpreal32, 16);
274 
275 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix2d, fpreal64, 4);
276 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix3d, fpreal64, 9);
277 GUSDUT_DECLARE_POD_TUPLE(class GfMatrix4d, fpreal64, 16);
278 
279 PXR_NAMESPACE_CLOSE_SCOPE
280 
284 
285 SYS_DECLARE_IS_POD(PXR_NS::GfHalf);
286 
287 SYS_DECLARE_IS_POD(PXR_NS::GfVec2h);
288 SYS_DECLARE_IS_POD(PXR_NS::GfVec3h);
289 SYS_DECLARE_IS_POD(PXR_NS::GfVec4h);
290 
291 SYS_DECLARE_IS_POD(PXR_NS::GfVec2f);
292 SYS_DECLARE_IS_POD(PXR_NS::GfVec3f);
293 SYS_DECLARE_IS_POD(PXR_NS::GfVec4f);
294 
295 SYS_DECLARE_IS_POD(PXR_NS::GfVec2d);
296 SYS_DECLARE_IS_POD(PXR_NS::GfVec3d);
297 SYS_DECLARE_IS_POD(PXR_NS::GfVec4d);
298 
299 SYS_DECLARE_IS_POD(PXR_NS::GfVec2i);
300 SYS_DECLARE_IS_POD(PXR_NS::GfVec3i);
301 SYS_DECLARE_IS_POD(PXR_NS::GfVec4i);
302 
303 SYS_DECLARE_IS_POD(PXR_NS::GfQuath);
304 SYS_DECLARE_IS_POD(PXR_NS::GfQuatf);
305 SYS_DECLARE_IS_POD(PXR_NS::GfQuatd);
306 
307 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix2f);
308 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix3f);
309 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix4f);
310 
311 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix2d);
312 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix3d);
313 SYS_DECLARE_IS_POD(PXR_NS::GfMatrix4d);
314 
315 PXR_NAMESPACE_OPEN_SCOPE
316 
317 // No casting on quaternions; real component is ordered
318 // in a different way between UT and Gf.
319 _GUSDUT_DECLARE_UNCASTABLE(class GfQuaternion);
320 _GUSDUT_DECLARE_UNCASTABLE(class GfQuatf);
321 _GUSDUT_DECLARE_UNCASTABLE(class GfQuatd);
322 _GUSDUT_DECLARE_UNCASTABLE(UT_QuaternionF);
323 _GUSDUT_DECLARE_UNCASTABLE(UT_QuaternionD);
324 #if SYS_VERSION_FULL_INT >= 0x11000000
325 _GUSDUT_DECLARE_UNCASTABLE(UT_QuaternionH);
326 #endif
327 
328 // Declare type correspondances between Gf and UT.
329 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec2h, UT_Vector2H);
330 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec3h, UT_Vector3H);
331 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec4h, UT_Vector4H);
332 
333 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec2d, UT_Vector2D);
334 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec3d, UT_Vector3D);
335 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec4d, UT_Vector4D);
336 
337 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec2f, UT_Vector2F);
338 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec3f, UT_Vector3F);
339 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec4f, UT_Vector4F);
340 
341 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec2i, UT_Vector2i);
342 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec3i, UT_Vector3i);
343 _GUSDUT_DECLARE_EQUIVALENCE(class GfVec4i, UT_Vector4i);
344 
345 _GUSDUT_DECLARE_EQUIVALENCE(class GfQuatd, UT_QuaternionD);
346 _GUSDUT_DECLARE_EQUIVALENCE(class GfQuatf, UT_QuaternionF);
347 #if SYS_VERSION_FULL_INT >= 0x11000000
348 _GUSDUT_DECLARE_EQUIVALENCE(class GfQuath, UT_QuaternionH);
349 #endif
350 
351 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix2d, UT_Matrix2D);
352 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix3d, UT_Matrix3D);
353 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix4d, UT_Matrix4D);
354 
355 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix2f, UT_Matrix2F);
356 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix3f, UT_Matrix3F);
357 _GUSDUT_DECLARE_EQUIVALENCE(class GfMatrix4f, UT_Matrix4F);
358 
359 
360 template <class T>
361 void
362 GusdUT_Gf::_AssertIsPodTuple()
363 {
364  static_assert(GusdIsPodTuple<T>(), "Type is not declared as a POD-tuple");
365 }
366 
367 
368 template <class FROM, class TO>
369 void
370 GusdUT_Gf::_AssertCanCast()
371 {
372  _AssertIsPodTuple<FROM>();
373  _AssertIsPodTuple<TO>();
374  static_assert(Castable<FROM>::value, "Source is not castable");
375  static_assert(Castable<TO>::value, "Output is not castable");
376  static_assert(GusdPodTuplesAreBitwiseCompatible<FROM,TO>(),
377  "Types in cast are not bitwise compatible");
378 }
379 
380 
381 template <class FROM, class TO>
382 const TO*
383 GusdUT_Gf::Cast(const FROM* val)
384 {
385  _AssertCanCast<FROM,TO>();
386  return reinterpret_cast<const TO*>(val);
387 }
388 
389 
390 template <class FROM, class TO>
391 TO*
392 GusdUT_Gf::Cast(FROM* val)
393 {
394  _AssertCanCast<FROM,TO>();
395  return reinterpret_cast<TO*>(val);
396 }
397 
398 
399 template <class FROM, class TO>
400 const TO&
401 GusdUT_Gf::Cast(const FROM& val)
402 {
403  _AssertCanCast<FROM,TO>();
404  return reinterpret_cast<const TO&>(val);
405 }
406 
407 
408 template <class FROM, class TO>
409 TO&
410 GusdUT_Gf::Cast(FROM& val)
411 {
412  _AssertCanCast<FROM,TO>();
413  return reinterpret_cast<TO&>(val);
414 }
415 
416 
417 template <class T>
419 GusdUT_Gf::Cast(T& val)
420 {
421  _AssertIsPodTuple<T>();
422  return Cast<T,typename TypeEquivalence<T>::AltType>(val);
423 }
424 
425 
426 template <class T>
428 GusdUT_Gf::Cast(const T& val)
429 {
430  _AssertIsPodTuple<T>();
431  return Cast<T, typename TypeEquivalence<T>::AltType>(val);
432 }
433 
434 
435 template <class T>
437 GusdUT_Gf::Cast(T* val)
438 {
439  _AssertIsPodTuple<T>();
440  return Cast<T, typename TypeEquivalence<T>::AltType>(val);
441 }
442 
443 
444 template <class T>
446 GusdUT_Gf::Cast(const T* val)
447 {
448  _AssertIsPodTuple<T>();
449  return Cast<T, typename TypeEquivalence<T>::AltType>(val);
450 }
451 
452 
453 template <class FROM, class TO>
454 void
455 GusdUT_Gf::Convert(const FROM& from, TO& to)
456 {
457  using FromPodType = typename GusdPodTupleTraits<FROM>::ValueType;
458  using ToPodType = typename GusdPodTupleTraits<TO>::ValueType;
459 
460  _AssertIsPodTuple<FROM>();
461  _AssertIsPodTuple<TO>();
462  static_assert(GusdPodTuplesAreCompatible<FROM,TO>(),
463  "Types are not compatible (mismatched tuple sizes)");
464 
465  const auto* src = reinterpret_cast<const FromPodType*>(&from);
466  ToPodType* dst = reinterpret_cast<ToPodType*>(&to);
467 
468  for (int i = 0; i < GusdGetTupleSize<FROM>(); ++i) {
469  dst[i] = static_cast<ToPodType>(src[i]);
470  }
471 }
472 
473 
474 template <class T, class GFQUAT>
475 void
476 GusdUT_Gf::_ConvertQuat(const GFQUAT& from, UT_QuaternionT<T>& to)
477 {
478  reinterpret_cast<UT_Vector3T<T>&>(to) = Cast(from.GetImaginary());
479  to(3) = from.GetReal();
480 }
481 
482 
483 template <class T>
484 void
485 GusdUT_Gf::Convert(const GfQuaternion& from, UT_QuaternionT<T>& to)
486 {
487  return _ConvertQuat(from, to);
488 }
489 
490 
491 template <class T>
492 void
493 GusdUT_Gf::Convert(const GfQuatd& from, UT_QuaternionT<T>& to)
494 {
495  return _ConvertQuat(from, to);
496 }
497 
498 
499 template <class T>
500 void
501 GusdUT_Gf::Convert(const GfQuatf& from, UT_QuaternionT<T>& to)
502 {
503  return _ConvertQuat(from, to);
504 }
505 
506 
507 template <class T>
508 void
509 GusdUT_Gf::Convert(const GfQuath& from, UT_QuaternionT<T>& to)
510 {
511  return _ConvertQuat(from, to);
512 }
513 
514 
515 template <class T>
516 void
517 GusdUT_Gf::Convert(const GfVec4d& from, UT_QuaternionT<T>& to)
518 {
519  to = UT_QuaternionT<T>(from[1],from[2],from[3],from[0]);
520 }
521 
522 
523 template <class T>
524 void
525 GusdUT_Gf::Convert(const GfVec4f& from, UT_QuaternionT<T>& to)
526 {
527  to = UT_QuaternionT<T>(from[1],from[2],from[3],from[0]);
528 }
529 
530 
531 template <class T>
532 void
533 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfQuaternion& to)
534 {
535  to.SetReal(from.w());
536  to.SetImaginary(GfVec3d(from.x(), from.y(), from.z()));
537 }
538 
539 
540 template <class T>
541 void
542 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfQuatd& to)
543 {
544  to.SetReal(from.w());
545  to.SetImaginary(GfVec3d(from.x(), from.y(), from.z()));
546 }
547 
548 
549 template <class T>
550 void
551 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfQuatf& to)
552 {
553  to.SetReal(from.w());
554  to.SetImaginary(GfVec3f(from.x(), from.y(), from.z()));
555 }
556 
557 
558 template <class T>
559 void
560 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfQuath& to)
561 {
562  to.SetReal(GfHalf(from.w()));
563  to.SetImaginary(
564  GfVec3h(GfHalf(from.x()), GfHalf(from.y()), GfHalf(from.z())));
565 }
566 
567 
568 template <class T>
569 void
570 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfVec4d& to)
571 {
572  to = GfVec4d(from.w(), from.x(), from.y(), from.z());
573 }
574 
575 
576 template <class T>
577 void
578 GusdUT_Gf::Convert(const UT_QuaternionT<T>& from, GfVec4f& to)
579 {
580  to = GfVec4f(from.w(), from.x(), from.y(), from.z());
581 }
582 
583 
584 #undef _GUSDUT_DECLARE_UNCASTABLE
585 #undef _GUSDUT_DECLARE_PARTIAL_EQUIVALENCE
586 #undef _GUSDUT_DECLARE_EQUIVALENCE
587 
588 PXR_NAMESPACE_CLOSE_SCOPE
589 
590 #endif /*_GUSD_UT_GF_H_*/
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quath.h:61
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
Stores a 4x4 matrix of float elements.
Definition: matrix4f.h:88
Helpers for working with Gf types (vectors, matrices, etc.) within the HDK.
Definition: UT_Gf.h:46
static void Convert(const FROM &from, TO &to)
Convert between UT and Gf types.
Definition: UT_Gf.h:455
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:42
void SetReal(GfHalf real)
Set the real coefficient.
Definition: quath.h:106
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
void SetImaginary(const GfVec3d &imaginary)
Set the imaginary coefficients.
Definition: quatd.h:111
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
Basic type: complex number with scalar real part and vector imaginary part.
Definition: quaternion.h:50
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quatf.h:60
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
void SetReal(float real)
Set the real coefficient.
Definition: quatf.h:105
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:64
Stores a 2x2 matrix of double elements.
Definition: matrix2d.h:62
Stores a 3x3 matrix of float elements.
Definition: matrix3f.h:82
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
void SetImaginary(const GfVec3f &imaginary)
Set the imaginary coefficients.
Definition: quatf.h:111
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
void SetReal(double real)
Set the real coefficient.
Definition: quatd.h:105
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Struct providing info about type equivalence between UT and Gf types.
Definition: UT_Gf.h:60
void SetImaginary(const GfVec3h &imaginary)
Set the imaginary coefficients.
Definition: quath.h:112
static const TypeEquivalence< T >::AltType * Cast(const T *val)
Helpers for casting between UT and Gf types.
Definition: UT_Gf.h:446
void SetReal(double real)
Sets the real part of the quaternion.
Definition: quaternion.h:75
Stores a 3x3 matrix of double elements.
Definition: matrix3d.h:82
void SetImaginary(const GfVec3d &imaginary)
Sets the imaginary part of the quaternion.
Definition: quaternion.h:80
Struct defining whether or not a type is valid for direct casting to other types. ...
Definition: UT_Gf.h:69
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quatd.h:60
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:64
Stores a 2x2 matrix of float elements.
Definition: matrix2f.h:62