HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_XformOrder.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Utility Library (C++)
7  *
8  * COMMENTS:
9  * This class stores a transformation order of scales, rotates, and
10  * translates.
11  */
12 
13 #ifndef __UT_XformOrder_h__
14 #define __UT_XformOrder_h__
15 
16 #include "UT_API.h"
17 #include "UT_Assert.h"
18 
19 #include <stddef.h>
20 #include <iosfwd>
21 
22 /// Transformation order of scales, rotates, and translates.
24 {
25 public:
26  // The xforms. Their values are used as indices in an array, so they
27  // should start at 0 and increase by 1.
28  enum xform { T=0, S=1, RX=2, RY=3, RZ=4};
29 
30  // Xform ordering for R,S,T and for the rotates respectively. If you
31  // change any of these, make sure you update the ...FromString()
32  // methods in the private section.
33  enum rstOrder { TRS, TSR, RTS, RST, STR, SRT };
34  enum xyzOrder { XYZ, XZY, YXZ, YZX, ZXY, ZYX };
35 
36  /// Construct from one of the RST permutations and one of the
37  /// rotation permutation
40  {
41  reorder(rst, rot);
42  }
43 
44  /// Full flexibility in constructing from specified component order.
45  /// Avoid using this as you can specify orders here that lead to undefined
46  /// behaviour for mainOrder() and rotOrder().
48  xform xf3 = UT_XformOrder::RX,
51  {
52  reorder(xf1, xf2, xf3, xf4, xf5);
53  }
54 
55  /// Constructor that takes strings specifying the xform order. Any
56  /// combination of lower and uppercase is acceptable ("TsR", "XZY", "rst").
57  /// We do NOT check the string lengths, but we do check for null pointers.
58  UT_XformOrder(const char *rst, const char *rot = "xyz");
59 
60  UT_XformOrder(const UT_XformOrder &xf) = default;
61  UT_XformOrder &operator=(const UT_XformOrder &xf) = default;
62 
63  /// Shuffle the order of the xforms. If all you'll ever do is traverse the
64  /// xform order to build a transformation matrix, you can even have more
65  /// xforms of the same type in the argument list; then, however, the
66  /// behaviour of mainOrder() and rotOrder() will be undefined.
67  /// @{
68  void reorder(UT_XformOrder::xform xf1,
71  void reorder(UT_XformOrder::rstOrder rst,
73  /// @}
74 
75  /// Query or set the main xform order or the rotation order respectively.
76  /// These methods assume the rotates are bundled together, and search for
77  /// the position of the first rotate they find. The methods that take
78  /// strings accept lower and uppercase combinations, check to see whether
79  /// the argument is zero, but don't check its length.
80  /// @{
81  UT_XformOrder::rstOrder mainOrder() const;
82  void mainOrder(UT_XformOrder::rstOrder rst);
83  bool mainOrder(const char *rst);
84  UT_XformOrder::xyzOrder rotOrder() const;
85  void rotOrder(UT_XformOrder::xyzOrder rot);
86  bool rotOrder(const char *rot);
87  /// @}
88 
89  /// Compute a mapping from the source to destination rotation order by
90  /// filling in the given 'map' array.
91  static void getRotOrderMapping(xyzOrder src, xyzOrder dst,
92  int map[3]);
93 
94  /// Swap two xform components
96  {
97  if (xf1 != xf2)
98  {
99  unsigned short i1 = prefList[xf1];
100  unsigned short i2 = prefList[xf2];
101  prefList[xf1] = i2; permList[i1] = xf2;
102  prefList[xf2] = i1; permList[i2] = xf1;
103  }
104  }
105 
106  /// Reverse the order
107  void invert();
108 
109  /// Two operator: operator() returns the index of a given transform without
110  /// having to check any bounds; operator[] returns the i'th transform and
111  /// checks if it's between valid bounds.
112  /// @{
113  unsigned short operator()(UT_XformOrder::xform xf) const
114  {
115  return prefList[xf];
116  }
117  UT_XformOrder::xform operator[](unsigned short i) const
118  {
119  UT_ASSERT_P(i < 5);
120  return (i<5)
121  ? (UT_XformOrder::xform)permList[i]
122  : (UT_XformOrder::xform)permList[4];
123  }
124  /// @}
125 
126  /// Return the number of transforms we store, this bounds the indices used
127  /// for operator() and operator[].
128  int count() const { return 5; }
129 
130  bool operator==(const UT_XformOrder &order) const
131  {
132  for (int i = 0; i < 5; ++i)
133  {
134  if (prefList[i] != order(xform(i)))
135  return false;
136  }
137  return true;
138  }
139  bool operator!=(const UT_XformOrder &order) const
140  { return !(*this == order); }
141 
142  /// Returns true if order says scales are before rotates
143  bool isScaleBeforeRotate() const
144  {
145  return (prefList[S] < prefList[RX]);
146  }
147 
148  /// Save the xform order. We save the sequence of transforms in the
149  /// order in which they appear in the permutation list.
150  int save(std::ostream &os, int binary=0) const;
151 
152  friend std::ostream &operator<<(std::ostream &os, const UT_XformOrder &t)
153  {
154  t.save(os);
155  return os;
156  }
157 
158  /// User-facing labels for transform/rotate orders
159  /// @{
160  const char *mainOrderLabel() const;
161  const char *rotOrderLabel() const;
162  /// @}
163 
164 private:
165  // The permulation list. Should be of type UT_XformOrder::xform, but chars
166  // are more compact.
167  char permList[5];
168 
169  // Permutation reference: stores the index of each transform in the
170  // permutation list. It's handy for locating and shuffling xforms fast.
171  unsigned short prefList[5];
172 
173  // Translate a string into an xform order, returning whether or not the
174  // conversion was successful. If the conversion wasn't successful,
175  // the xform order will be set to the "default" order.
176  static bool rstFromString(const char *rst,
178  static bool xyzFromString(const char *rot,
180 
181  static const char * theXYZNames[6];
182  static const char * theXYZLabels[6];
183  static const char * theTRSLabels[6];
184 };
185 
186 // Custom overflow for UTformat()
187 UT_API size_t format(char *buffer, size_t buffer_size, const UT_XformOrder &v);
188 
189 #endif
unsigned short operator()(UT_XformOrder::xform xf) const
GLboolean invert
Definition: glcorearb.h:549
const GLdouble * v
Definition: glcorearb.h:837
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
GA_API const UT_StringHolder rot
GLint GLint i2
Definition: glad.h:2724
bool isScaleBeforeRotate() const
Returns true if order says scales are before rotates.
UT_API size_t format(char *buffer, size_t buffer_size, const UT_XformOrder &v)
#define UT_API
Definition: UT_API.h:14
**But if you need a result
Definition: thread.h:613
bool operator==(const UT_XformOrder &order) const
void swap(UT_XformOrder::xform xf1, UT_XformOrder::xform xf2)
Swap two xform components.
Definition: UT_XformOrder.h:95
bool operator!=(const UT_XformOrder &order) const
Definition: core.h:760
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
GLint i1
Definition: glad.h:2724
int save(std::ostream &os, int binary=0) const
GLdouble t
Definition: glad.h:2397
UT_XformOrder(rstOrder rst=UT_XformOrder::TSR, xyzOrder rot=UT_XformOrder::XYZ)
Definition: UT_XformOrder.h:38
friend std::ostream & operator<<(std::ostream &os, const UT_XformOrder &t)
GLenum GLenum dst
Definition: glcorearb.h:1793
UT_XformOrder::xform operator[](unsigned short i) const
int count() const
UT_XformOrder(xform xf1, xform xf2, xform xf3=UT_XformOrder::RX, xform xf4=UT_XformOrder::RY, xform xf5=UT_XformOrder::RZ)
Definition: UT_XformOrder.h:47
GLenum src
Definition: glcorearb.h:1793