Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoordinateAxis.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_COORDINATE_AXIS_H_
2 #define SimTK_SimTKCOMMON_COORDINATE_AXIS_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
31 #include <cassert>
32 
33 namespace SimTK {
34 
54 public:
57  explicit CoordinateAxis( int i ) : m_myAxisId(i) {assertIndexIsInRange(i);}
58 
60  operator int() const {return m_myAxisId;}
61 
67  { return CoordinateAxis((m_myAxisId+1) % 3); }
68 
74  { return CoordinateAxis((m_myAxisId+2) % 3); }
75 
83  CoordinateAxis getThirdAxis( const CoordinateAxis& axis2 ) const {
84  assert( isDifferentAxis(axis2) );
85  CoordinateAxis nextAxis = getNextAxis();
86  return nextAxis.isDifferentAxis(axis2) ? nextAxis : axis2.getNextAxis();
87  }
88 
90  bool isXAxis() const {return m_myAxisId == 0;}
92  bool isYAxis() const {return m_myAxisId == 1;}
94  bool isZAxis() const {return m_myAxisId == 2;}
97  bool isNextAxis( const CoordinateAxis& axis2 ) const
98  { return int(getNextAxis()) == int(axis2); }
101  bool isPreviousAxis( const CoordinateAxis& axis2 ) const
102  { return int(getPreviousAxis()) == int(axis2); }
105  bool isSameAxis( const CoordinateAxis& axis2 ) const
106  { return m_myAxisId == int(axis2); }
108  bool areAllSameAxes( const CoordinateAxis& axis2,
109  const CoordinateAxis &axis3 ) const
110  { return isSameAxis(axis2) && isSameAxis(axis3); }
113  bool isDifferentAxis( const CoordinateAxis& axis2 ) const
114  { return m_myAxisId != int(axis2); }
118  bool areAllDifferentAxes( const CoordinateAxis& axis2,
119  const CoordinateAxis& axis3 ) const
120  { return isDifferentAxis(axis2) && isDifferentAxis(axis3)
121  && axis2.isDifferentAxis(axis3); }
125  bool isForwardCyclical( const CoordinateAxis& axis2 ) const
126  { return isNextAxis(axis2); }
130  bool isReverseCyclical( const CoordinateAxis& axis2 ) const
131  { return isPreviousAxis(axis2); }
132 
136  int dotProduct( const CoordinateAxis& axis2 ) const
137  { return isSameAxis(axis2) ? 1 : 0; }
143  int crossProductSign( const CoordinateAxis& axis2 ) const
144  { return isSameAxis(axis2) ? 0 : (isNextAxis(axis2) ? 1 : -1); }
153  { return isSameAxis(axis2) ? CoordinateAxis(m_myAxisId)
154  : getThirdAxis(axis2); }
162  CoordinateAxis crossProduct( const CoordinateAxis& axis2, int& sign ) const
163  { sign = crossProductSign(axis2); return crossProductAxis(axis2); }
164 
167  static const CoordinateAxis& getCoordinateAxis( int i );
168 
172  static bool isIndexInRange( int i ) { return 0<=i && i<=2; }
175  static void assertIndexIsInRange( int i ) { assert( isIndexInRange(i) ); }
176 
177  // Forward declarations for subsequent helper classes
178  class XCoordinateAxis; class YCoordinateAxis; class ZCoordinateAxis;
179 protected: // turn off doxygen here; these aren't for users
181  class XTypeAxis{};
182  class YTypeAxis{};
183  class ZTypeAxis{};
184 
185  CoordinateAxis( const XTypeAxis& ) : m_myAxisId(0) {}
186  CoordinateAxis( const YTypeAxis& ) : m_myAxisId(1) {}
187  CoordinateAxis( const ZTypeAxis& ) : m_myAxisId(2) {}
189 private:
190 
191  int m_myAxisId;
192 };
193 
194 
195 // Helper classes that allow compile time recognition of axis directions.
197  public: XCoordinateAxis() : CoordinateAxis(XTypeAxis()) {}
198 };
200  public: YCoordinateAxis() : CoordinateAxis(YTypeAxis()) {}
201 };
203  public: ZCoordinateAxis() : CoordinateAxis(ZTypeAxis()) {}
204 };
205 
215 
218  return (i==0 ? static_cast<const CoordinateAxis&>(XAxis)
219  : (i==1 ? static_cast<const CoordinateAxis&>(YAxis)
220  : static_cast<const CoordinateAxis&>(ZAxis)));
221 }
222 
224 inline bool operator==(const CoordinateAxis& a1, const CoordinateAxis& a2)
225 { return a1.isSameAxis(a2); }
226 
228 inline bool operator!=(const CoordinateAxis& a1, const CoordinateAxis& a2)
229 { return a1.isDifferentAxis(a2); }
230 
231 
247 public:
250  class Negative {};
251 
255  : m_axis(axis), m_direction(1) {}
256 
260  : m_axis(axis), m_direction(-1) {}
261 
269  CoordinateDirection(const CoordinateAxis& axis, int direction)
270  : m_axis(axis), m_direction(direction)
271  { assert(direction==1 || direction==-1); }
272 
276  CoordinateAxis getAxis() const {return m_axis;}
279  int getDirection() const {return m_direction;}
280 
283  bool hasSameAxis(const CoordinateDirection& dir2) const
284  { return m_axis.isSameAxis(dir2.getAxis()); }
285 
290  { return m_axis==dir2.getAxis() && m_direction==dir2.getDirection(); }
291 
295  int dotProduct( const CoordinateDirection& dir2 ) const
296  { if (m_axis != dir2.getAxis()) return 0;
297  return m_direction == dir2.getDirection() ? 1 : -1; }
298 
304  int crossProductSign( const CoordinateDirection& dir2 ) const
305  { if (m_axis == dir2.getAxis()) return 0;
306  return m_axis.crossProductSign(dir2.getAxis())
307  * m_direction * dir2.getDirection(); }
308 
317  { return m_axis.crossProductAxis(dir2.getAxis()); }
318 
327  int& sign ) const
328  { sign = crossProductSign(dir2); return crossProductAxis(dir2); }
329 
330  // Local class declarations for helper classes.
331  class NegXDirection; class NegYDirection; class NegZDirection;
332 private:
333  CoordinateAxis m_axis; // XAxis, YAxis, or ZAxis
334  int m_direction; // 1 or -1
335 };
336 
337 
338 // Helper classes that allow compile time recognition of negative axis
339 // directions.
342 };
345 };
348 };
349 
350 // Predefine constants for the negative X,Y,Z directions.
352  NegXAxis;
354  NegYAxis;
356  NegZAxis;
357 
359 inline bool operator==(const CoordinateDirection& d1,
360  const CoordinateDirection& d2)
361 { return d1.isSameAxisAndDirection(d2); }
362 
364 inline bool operator!=(const CoordinateDirection& d1,
365  const CoordinateDirection& d2)
366 { return !d1.isSameAxisAndDirection(d2); }
367 
380 
383 inline CoordinateDirection
386 
389 inline CoordinateDirection
391 { return CoordinateDirection(axis); }
392 
405 
408 inline CoordinateDirection
410 { return CoordinateDirection(dir.getAxis(), -dir.getDirection()); }
411 
412 } // End of namespace
413 
414 #endif // SimTK_SimTKCOMMON_COORDINATE_AXIS_H_
415 
416 
417 
const CoordinateDirection::NegZDirection NegZAxis
Global constant indicating -Z coordinate direction.
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:202
NegZDirection()
Definition: CoordinateAxis.h:347
const CoordinateAxis::YCoordinateAxis & operator-(const CoordinateDirection::NegYDirection &)
Create the YAxis direction by negating NegYAxis.
Definition: CoordinateAxis.h:400
CoordinateAxis crossProductAxis(const CoordinateAxis &axis2) const
Return the coordinate axis along which the cross product of this axis and axis2 would lie: same as th...
Definition: CoordinateAxis.h:152
bool hasSameAxis(const CoordinateDirection &dir2) const
Return true if this direction and dir2 are along the same axis, even if the direction along that axis...
Definition: CoordinateAxis.h:283
int crossProductSign(const CoordinateDirection &dir2) const
Return the sign that would result from a cross product between this coordinate direction and dir2: 0 ...
Definition: CoordinateAxis.h:304
int crossProductSign(const CoordinateAxis &axis2) const
Return the sign that would result from a cross product between this axis and axis2: zero if axis2 is ...
Definition: CoordinateAxis.h:143
CoordinateDirection operator-(const CoordinateDirection &dir)
Create the opposite direction from the given direction.
Definition: CoordinateAxis.h:409
const CoordinateDirection::NegXDirection NegXAxis
Global constant indicating -X coordinate direction.
YCoordinateAxis()
Definition: CoordinateAxis.h:200
static const CoordinateAxis & getCoordinateAxis(int i)
Return a reference to the CoordinateAxis constant XAxis, YAxis, or ZAxis corresponding to the given i...
Definition: CoordinateAxis.h:216
bool isSameAxis(const CoordinateAxis &axis2) const
Return true if the given axis2 is the same as this one. You can use operator==() to perform the same ...
Definition: CoordinateAxis.h:105
bool operator!=(const CoordinateDirection &d1, const CoordinateDirection &d2)
Compare two CoordinateDirection objects.
Definition: CoordinateAxis.h:364
Definition: CoordinateAxis.h:196
A CoordinateDirection is a CoordinateAxis plus a direction indicating the positive or negative direct...
Definition: CoordinateAxis.h:246
Definition: CoordinateAxis.h:199
Definition: CoordinateAxis.h:202
static void assertIndexIsInRange(int i)
When in Debug mode, throw an assertion if the given integer is not suited as a coordinate axis...
Definition: CoordinateAxis.h:175
CoordinateAxis getPreviousAxis() const
Return the "previous" coordinate axis before this one:
Definition: CoordinateAxis.h:73
CoordinateAxis crossProductAxis(const CoordinateDirection &dir2) const
Return the coordinate axis along which the cross product of this coordinate direction and dir2 would ...
Definition: CoordinateAxis.h:316
XCoordinateAxis()
Definition: CoordinateAxis.h:197
const CoordinateAxis::ZCoordinateAxis ZAxis
Constant representing the Z coordinate axis; will implicitly convert to the integer 2 when used in a ...
This class, along with its sister class CoordinateDirection, provides convenient manipulation of the ...
Definition: CoordinateAxis.h:53
CoordinateDirection(const CoordinateAxis &axis, int direction)
Explicit creation of a CoordinateDirection from a CoordinateAxis and a direction calculated at run ti...
Definition: CoordinateAxis.h:269
const CoordinateDirection::NegYDirection NegYAxis
Global constant indicating -Y coordinate direction.
static bool isIndexInRange(int i)
Return true if the given integer is suitable as a coordinate axis, meaning it is one of 0...
Definition: CoordinateAxis.h:172
const CoordinateAxis::YCoordinateAxis YAxis
Constant representing the Y coordinate axis; will implicitly convert to the integer 1 when used in a ...
bool operator==(const CoordinateAxis &a1, const CoordinateAxis &a2)
Compare two CoordinateAxis objects.
Definition: CoordinateAxis.h:224
CoordinateAxis getThirdAxis(const CoordinateAxis &axis2) const
Given this coordinate axis and one other, return the missing one:
Definition: CoordinateAxis.h:83
bool isYAxis() const
Return true if this is the Y axis.
Definition: CoordinateAxis.h:92
CoordinateAxis getAxis() const
This is the coordinate axis XAxis, YAxis, or ZAxis contained in this CoordinateDirection. Use getDirection() to determine whether this is the positive or negative direction.
Definition: CoordinateAxis.h:276
CoordinateAxis(int i)
Explicit construction of a CoordinateAxis from a calculated integer that must be 0, 1, or 2 representing XAxis, YAxis, or ZAxis.
Definition: CoordinateAxis.h:57
bool isForwardCyclical(const CoordinateAxis &axis2) const
Return true if the given axis2 is the one following this one in a forward cyclical direction...
Definition: CoordinateAxis.h:125
const CoordinateAxis::ZCoordinateAxis & operator-(const CoordinateDirection::NegZDirection &)
Create the ZAxis direction by negating NegZAxis.
Definition: CoordinateAxis.h:404
int dotProduct(const CoordinateDirection &dir2) const
Perform a specialized dot product between this coordinate direction and dir2; returning 1 or -1 if th...
Definition: CoordinateAxis.h:295
CoordinateDirection(const CoordinateAxis &axis)
Implicit conversion of a CoordinateAxis to a positive CoordinateDirection along that axis...
Definition: CoordinateAxis.h:254
bool isPreviousAxis(const CoordinateAxis &axis2) const
Return true if the given axis2 is the one preceding this one as would be reported by getPreviousAxis(...
Definition: CoordinateAxis.h:101
const CoordinateDirection::NegYDirection & operator-(const CoordinateAxis::YCoordinateAxis &)
Create the NegYAxis direction by negating YAxis.
Definition: CoordinateAxis.h:375
Use for compile-time construction of a negative CoordinateDirection along one of the coordinate axes...
Definition: CoordinateAxis.h:250
bool isNextAxis(const CoordinateAxis &axis2) const
Return true if the given axis2 is the one following this one as would be reported by getNextAxis()...
Definition: CoordinateAxis.h:97
CoordinateDirection operator-(const CoordinateAxis &axis)
Create the negative direction along the given axis.
Definition: CoordinateAxis.h:384
bool operator==(const CoordinateDirection &d1, const CoordinateDirection &d2)
Compare two CoordinateDirection objects.
Definition: CoordinateAxis.h:359
CoordinateAxis crossProduct(const CoordinateDirection &dir2, int &sign) const
Return the axis and sign along that axis that would result from a cross product between this coordina...
Definition: CoordinateAxis.h:326
CoordinateAxis getNextAxis() const
Return the "next" coordinate axis after this one:
Definition: CoordinateAxis.h:66
bool isXAxis() const
Return true if this is the X axis.
Definition: CoordinateAxis.h:90
Mandatory first inclusion for any Simbody source or header file.
bool isDifferentAxis(const CoordinateAxis &axis2) const
Return true if the given axis2 is not the same one as this one. You can use operator!=() to perform t...
Definition: CoordinateAxis.h:113
bool isZAxis() const
Return true if this is the Z axis.
Definition: CoordinateAxis.h:94
ZCoordinateAxis()
Definition: CoordinateAxis.h:203
CoordinateDirection(const CoordinateAxis &axis, Negative)
Explicit creation of a negative CoordinateDirection from a CoordinateAxis.
Definition: CoordinateAxis.h:259
bool isReverseCyclical(const CoordinateAxis &axis2) const
Return true if the given axis2 is the one following this one in a reverse cyclical direction...
Definition: CoordinateAxis.h:130
int getDirection() const
Returns 1 or -1 to indicate the direction along the coordinate axis returned by getAxis().
Definition: CoordinateAxis.h:279
unsigned int sign(unsigned char u)
Definition: Scalar.h:311
const CoordinateAxis::XCoordinateAxis & operator-(const CoordinateDirection::NegXDirection &)
Create the XAxis direction by negating NegXAxis.
Definition: CoordinateAxis.h:396
int dotProduct(const CoordinateAxis &axis2) const
Perform a specialized dot product between this axis and axis2; returning one if they are the same axi...
Definition: CoordinateAxis.h:136
const CoordinateDirection::NegZDirection & operator-(const CoordinateAxis::ZCoordinateAxis &)
Create the NegZAxis direction by negating ZAxis.
Definition: CoordinateAxis.h:379
CoordinateDirection operator+(const CoordinateAxis &axis)
Create the positive direction along the given axis.
Definition: CoordinateAxis.h:390
bool isSameAxisAndDirection(const CoordinateDirection &dir2) const
Return true if this direction and dir2 are along the same axis, and in the same direction along that ...
Definition: CoordinateAxis.h:289
bool areAllDifferentAxes(const CoordinateAxis &axis2, const CoordinateAxis &axis3) const
Return true if neither axis2 nor axis3 is the same as this axis nor each other; that is...
Definition: CoordinateAxis.h:118
NegXDirection()
Definition: CoordinateAxis.h:341
const CoordinateDirection::NegXDirection & operator-(const CoordinateAxis::XCoordinateAxis &)
Create the NegXAxis direction by negating XAxis.
Definition: CoordinateAxis.h:371
NegYDirection()
Definition: CoordinateAxis.h:344
CoordinateAxis crossProduct(const CoordinateAxis &axis2, int &sign) const
Return the axis and sign along that axis that would result from a cross product between this axis and...
Definition: CoordinateAxis.h:162
Definition: CoordinateAxis.h:346
bool operator!=(const CoordinateAxis &a1, const CoordinateAxis &a2)
Compare two CoordinateAxis objects.
Definition: CoordinateAxis.h:228
Definition: CoordinateAxis.h:343
bool areAllSameAxes(const CoordinateAxis &axis2, const CoordinateAxis &axis3) const
Return true if both axis2 and axis3 are the same as this one.
Definition: CoordinateAxis.h:108
Definition: CoordinateAxis.h:340
const CoordinateAxis::XCoordinateAxis XAxis
Constant representing the X coordinate axis; will implicitly convert to the integer 0 when used in a ...