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  /// @{
82  {
83  int rIdx = prefList[RX];
84  if (prefList[RY] < rIdx) rIdx = prefList[RY];
85  if (prefList[RZ] < rIdx) rIdx = prefList[RZ];
86 
87  if (prefList[T] < prefList[S] && prefList[T] < rIdx)
88  return (rIdx < prefList[S]) ? TRS : TSR;
89  if (prefList[S] < prefList[T] && prefList[S] < rIdx)
90  return (rIdx < prefList[T]) ? SRT : STR;
91  if (rIdx < prefList[S] && rIdx < prefList[T])
92  return (prefList[S] < prefList[T]) ? RST : RTS;
93 
94  return RST; // should never happen
95  }
96  void mainOrder(UT_XformOrder::rstOrder rst);
97  bool mainOrder(const char *rst);
98 
100  {
101  if (prefList[RX] < prefList[RY] && prefList[RX] < prefList[RZ])
102  return (prefList[RY] < prefList[RZ]) ? XYZ : XZY;
103  if (prefList[RY] < prefList[RX] && prefList[RY] < prefList[RZ])
104  return (prefList[RX] < prefList[RZ]) ? YXZ : YZX;
105  if (prefList[RZ] < prefList[RX] && prefList[RZ] < prefList[RY])
106  return (prefList[RX] < prefList[RY]) ? ZXY : ZYX;
107  return XYZ; // should never happen
108  }
109  void rotOrder(UT_XformOrder::xyzOrder rot);
110  bool rotOrder(const char *rot);
111  /// @}
112 
113  /// Compute a mapping from the source to destination rotation order by
114  /// filling in the given 'map' array.
115  static void getRotOrderMapping(xyzOrder src, xyzOrder dst,
116  int map[3]);
117 
118  /// Swap two xform components
120  {
121  if (xf1 != xf2)
122  {
123  unsigned short i1 = prefList[xf1];
124  unsigned short i2 = prefList[xf2];
125  prefList[xf1] = i2; permList[i1] = xf2;
126  prefList[xf2] = i1; permList[i2] = xf1;
127  }
128  }
129 
130  /// Reverse the order
131  void invert();
132 
133  /// Two operator: operator() returns the index of a given transform without
134  /// having to check any bounds; operator[] returns the i'th transform and
135  /// checks if it's between valid bounds.
136  /// @{
137  unsigned short operator()(UT_XformOrder::xform xf) const
138  {
139  return prefList[xf];
140  }
141  UT_XformOrder::xform operator[](unsigned short i) const
142  {
143  UT_ASSERT_P(i < 5);
144  return (i<5)
145  ? (UT_XformOrder::xform)permList[i]
146  : (UT_XformOrder::xform)permList[4];
147  }
148  /// @}
149 
150  /// Return the number of transforms we store, this bounds the indices used
151  /// for operator() and operator[].
152  int count() const { return 5; }
153 
154  bool operator==(const UT_XformOrder &order) const
155  {
156  for (int i = 0; i < 5; ++i)
157  {
158  if (prefList[i] != order(xform(i)))
159  return false;
160  }
161  return true;
162  }
163  bool operator!=(const UT_XformOrder &order) const
164  { return !(*this == order); }
165 
166  /// Returns true if order says scales are before rotates
167  bool isScaleBeforeRotate() const
168  {
169  return (prefList[S] < prefList[RX]);
170  }
171 
172  /// Save the xform order. We save the sequence of transforms in the
173  /// order in which they appear in the permutation list.
174  int save(std::ostream &os, int binary=0) const;
175 
176  friend std::ostream &operator<<(std::ostream &os, const UT_XformOrder &t)
177  {
178  t.save(os);
179  return os;
180  }
181 
182  /// User-facing labels for transform/rotate orders
183  /// @{
184  const char *mainOrderLabel() const;
185  const char *rotOrderLabel() const;
186  /// @}
187 
188 private:
189  // The permulation list. Should be of type UT_XformOrder::xform, but chars
190  // are more compact.
191  char permList[5];
192 
193  // Permutation reference: stores the index of each transform in the
194  // permutation list. It's handy for locating and shuffling xforms fast.
195  unsigned short prefList[5];
196 
197  // Translate a string into an xform order, returning whether or not the
198  // conversion was successful. If the conversion wasn't successful,
199  // the xform order will be set to the "default" order.
200  static bool rstFromString(const char *rst,
202  static bool xyzFromString(const char *rot,
204 
205  static const char * theXYZNames[6];
206  static const char * theXYZLabels[6];
207  static const char * theTRSLabels[6];
208 };
209 
210 // Custom overflow for UTformat()
211 UT_API size_t format(char *buffer, size_t buffer_size, const UT_XformOrder &v);
212 
213 #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.
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
UT_XformOrder::xyzOrder rotOrder() const
Definition: UT_XformOrder.h:99
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::rstOrder mainOrder() const
Definition: UT_XformOrder.h:81
LeafData & operator=(const LeafData &)=delete
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