Simbody  3.5
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)
58  { assert(isIndexInRange(i)); }
59 
61  operator int() const {return m_myAxisId;}
62 
68  { return CoordinateAxis((m_myAxisId+1) % 3); }
69 
75  { return CoordinateAxis((m_myAxisId+2) % 3); }
76 
84  CoordinateAxis getThirdAxis( const CoordinateAxis& axis2 ) const {
85  assert( isDifferentAxis(axis2) );
86  CoordinateAxis nextAxis = getNextAxis();
87  return nextAxis.isDifferentAxis(axis2) ? nextAxis : axis2.getNextAxis();
88  }
89 
91  bool isXAxis() const {return m_myAxisId == 0;}
93  bool isYAxis() const {return m_myAxisId == 1;}
95  bool isZAxis() const {return m_myAxisId == 2;}
98  bool isNextAxis( const CoordinateAxis& axis2 ) const
99  { return int(getNextAxis()) == int(axis2); }
102  bool isPreviousAxis( const CoordinateAxis& axis2 ) const
103  { return int(getPreviousAxis()) == int(axis2); }
106  bool isSameAxis( const CoordinateAxis& axis2 ) const
107  { return m_myAxisId == int(axis2); }
109  bool areAllSameAxes( const CoordinateAxis& axis2,
110  const CoordinateAxis &axis3 ) const
111  { return isSameAxis(axis2) && isSameAxis(axis3); }
114  bool isDifferentAxis( const CoordinateAxis& axis2 ) const
115  { return m_myAxisId != int(axis2); }
119  bool areAllDifferentAxes( const CoordinateAxis& axis2,
120  const CoordinateAxis& axis3 ) const
121  { return isDifferentAxis(axis2) && isDifferentAxis(axis3)
122  && axis2.isDifferentAxis(axis3); }
126  bool isForwardCyclical( const CoordinateAxis& axis2 ) const
127  { return isNextAxis(axis2); }
131  bool isReverseCyclical( const CoordinateAxis& axis2 ) const
132  { return isPreviousAxis(axis2); }
133 
137  int dotProduct( const CoordinateAxis& axis2 ) const
138  { return isSameAxis(axis2) ? 1 : 0; }
144  int crossProductSign( const CoordinateAxis& axis2 ) const
145  { return isSameAxis(axis2) ? 0 : (isNextAxis(axis2) ? 1 : -1); }
154  { return isSameAxis(axis2) ? CoordinateAxis(m_myAxisId)
155  : getThirdAxis(axis2); }
163  CoordinateAxis crossProduct( const CoordinateAxis& axis2, int& sign ) const
164  { sign = crossProductSign(axis2); return crossProductAxis(axis2); }
165 
168  static const CoordinateAxis& getCoordinateAxis( int i );
169 
173  static bool isIndexInRange( int i ) { return 0<=i && i<=2; }
174 
175  // Forward declarations for subsequent helper classes
176  class XCoordinateAxis; class YCoordinateAxis; class ZCoordinateAxis;
177 protected: // turn off doxygen here; these aren't for users
179  class XTypeAxis{};
180  class YTypeAxis{};
181  class ZTypeAxis{};
182 
183  CoordinateAxis( const XTypeAxis& ) : m_myAxisId(0) {}
184  CoordinateAxis( const YTypeAxis& ) : m_myAxisId(1) {}
185  CoordinateAxis( const ZTypeAxis& ) : m_myAxisId(2) {}
187 private:
188 
189  int m_myAxisId;
190 };
191 
192 
193 // Helper classes that allow compile time recognition of axis directions.
195  public: XCoordinateAxis() : CoordinateAxis(XTypeAxis()) {}
196 };
198  public: YCoordinateAxis() : CoordinateAxis(YTypeAxis()) {}
199 };
201  public: ZCoordinateAxis() : CoordinateAxis(ZTypeAxis()) {}
202 };
203 
213 
215  assert(isIndexInRange(i));
216  return (i==0 ? static_cast<const CoordinateAxis&>(XAxis)
217  : (i==1 ? static_cast<const CoordinateAxis&>(YAxis)
218  : static_cast<const CoordinateAxis&>(ZAxis)));
219 }
220 
222 inline bool operator==(const CoordinateAxis& a1, const CoordinateAxis& a2)
223 { return a1.isSameAxis(a2); }
224 
226 inline bool operator!=(const CoordinateAxis& a1, const CoordinateAxis& a2)
227 { return a1.isDifferentAxis(a2); }
228 
229 
245 public:
248  class Negative {};
249 
253  : m_axis(axis), m_direction(1) {}
254 
258  : m_axis(axis), m_direction(-1) {}
259 
267  CoordinateDirection(const CoordinateAxis& axis, int direction)
268  : m_axis(axis), m_direction(direction)
269  { assert(direction==1 || direction==-1); }
270 
274  CoordinateAxis getAxis() const {return m_axis;}
277  int getDirection() const {return m_direction;}
278 
281  bool hasSameAxis(const CoordinateDirection& dir2) const
282  { return m_axis.isSameAxis(dir2.getAxis()); }
283 
288  { return m_axis==dir2.getAxis() && m_direction==dir2.getDirection(); }
289 
293  int dotProduct( const CoordinateDirection& dir2 ) const
294  { if (m_axis != dir2.getAxis()) return 0;
295  return m_direction == dir2.getDirection() ? 1 : -1; }
296 
302  int crossProductSign( const CoordinateDirection& dir2 ) const
303  { if (m_axis == dir2.getAxis()) return 0;
304  return m_axis.crossProductSign(dir2.getAxis())
305  * m_direction * dir2.getDirection(); }
306 
315  { return m_axis.crossProductAxis(dir2.getAxis()); }
316 
325  int& sign ) const
326  { sign = crossProductSign(dir2); return crossProductAxis(dir2); }
327 
328  // Local class declarations for helper classes.
329  class NegXDirection; class NegYDirection; class NegZDirection;
330 private:
331  CoordinateAxis m_axis; // XAxis, YAxis, or ZAxis
332  int m_direction; // 1 or -1
333 };
334 
335 
336 // Helper classes that allow compile time recognition of negative axis
337 // directions.
340 };
343 };
346 };
347 
348 // Predefine constants for the negative X,Y,Z directions.
350  NegXAxis;
352  NegYAxis;
354  NegZAxis;
355 
357 inline bool operator==(const CoordinateDirection& d1,
358  const CoordinateDirection& d2)
359 { return d1.isSameAxisAndDirection(d2); }
360 
362 inline bool operator!=(const CoordinateDirection& d1,
363  const CoordinateDirection& d2)
364 { return !d1.isSameAxisAndDirection(d2); }
365 
378 
381 inline CoordinateDirection
384 
387 inline CoordinateDirection
389 { return CoordinateDirection(axis); }
390 
403 
406 inline CoordinateDirection
408 { return CoordinateDirection(dir.getAxis(), -dir.getDirection()); }
409 
410 } // End of namespace
411 
412 #endif // SimTK_SimTKCOMMON_COORDINATE_AXIS_H_
413 
414 
415 
const CoordinateDirection::NegZDirection NegZAxis
Global constant indicating -Z coordinate direction.
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:218
NegZDirection()
Definition: CoordinateAxis.h:345
const CoordinateAxis::YCoordinateAxis & operator-(const CoordinateDirection::NegYDirection &)
Create the YAxis direction by negating NegYAxis.
Definition: CoordinateAxis.h:398
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:153
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:281
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:302
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:144
CoordinateDirection operator-(const CoordinateDirection &dir)
Create the opposite direction from the given direction.
Definition: CoordinateAxis.h:407
const CoordinateDirection::NegXDirection NegXAxis
Global constant indicating -X coordinate direction.
YCoordinateAxis()
Definition: CoordinateAxis.h:198
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
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:214
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:106
bool operator!=(const CoordinateDirection &d1, const CoordinateDirection &d2)
Compare two CoordinateDirection objects.
Definition: CoordinateAxis.h:362
Definition: CoordinateAxis.h:194
A CoordinateDirection is a CoordinateAxis plus a direction indicating the positive or negative direct...
Definition: CoordinateAxis.h:244
Definition: CoordinateAxis.h:197
Definition: CoordinateAxis.h:200
CoordinateAxis getPreviousAxis() const
Return the "previous" coordinate axis before this one:
Definition: CoordinateAxis.h:74
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:314
XCoordinateAxis()
Definition: CoordinateAxis.h:195
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:267
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:173
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:222
CoordinateAxis getThirdAxis(const CoordinateAxis &axis2) const
Given this coordinate axis and one other, return the missing one:
Definition: CoordinateAxis.h:84
bool isYAxis() const
Return true if this is the Y axis.
Definition: CoordinateAxis.h:93
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:274
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:126
const CoordinateAxis::ZCoordinateAxis & operator-(const CoordinateDirection::NegZDirection &)
Create the ZAxis direction by negating NegZAxis.
Definition: CoordinateAxis.h:402
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:293
CoordinateDirection(const CoordinateAxis &axis)
Implicit conversion of a CoordinateAxis to a positive CoordinateDirection along that axis...
Definition: CoordinateAxis.h:252
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:102
const CoordinateDirection::NegYDirection & operator-(const CoordinateAxis::YCoordinateAxis &)
Create the NegYAxis direction by negating YAxis.
Definition: CoordinateAxis.h:373
Use for compile-time construction of a negative CoordinateDirection along one of the coordinate axes...
Definition: CoordinateAxis.h:248
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:98
CoordinateDirection operator-(const CoordinateAxis &axis)
Create the negative direction along the given axis.
Definition: CoordinateAxis.h:382
bool operator==(const CoordinateDirection &d1, const CoordinateDirection &d2)
Compare two CoordinateDirection objects.
Definition: CoordinateAxis.h:357
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:324
CoordinateAxis getNextAxis() const
Return the "next" coordinate axis after this one:
Definition: CoordinateAxis.h:67
bool isXAxis() const
Return true if this is the X axis.
Definition: CoordinateAxis.h:91
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:114
bool isZAxis() const
Return true if this is the Z axis.
Definition: CoordinateAxis.h:95
ZCoordinateAxis()
Definition: CoordinateAxis.h:201
CoordinateDirection(const CoordinateAxis &axis, Negative)
Explicit creation of a negative CoordinateDirection from a CoordinateAxis.
Definition: CoordinateAxis.h:257
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:131
int getDirection() const
Returns 1 or -1 to indicate the direction along the coordinate axis returned by getAxis().
Definition: CoordinateAxis.h:277
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:394
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:137
const CoordinateDirection::NegZDirection & operator-(const CoordinateAxis::ZCoordinateAxis &)
Create the NegZAxis direction by negating ZAxis.
Definition: CoordinateAxis.h:377
CoordinateDirection operator+(const CoordinateAxis &axis)
Create the positive direction along the given axis.
Definition: CoordinateAxis.h:388
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:287
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:119
NegXDirection()
Definition: CoordinateAxis.h:339
const CoordinateDirection::NegXDirection & operator-(const CoordinateAxis::XCoordinateAxis &)
Create the NegXAxis direction by negating XAxis.
Definition: CoordinateAxis.h:369
NegYDirection()
Definition: CoordinateAxis.h:342
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:163
Definition: CoordinateAxis.h:344
bool operator!=(const CoordinateAxis &a1, const CoordinateAxis &a2)
Compare two CoordinateAxis objects.
Definition: CoordinateAxis.h:226
Definition: CoordinateAxis.h:341
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:109
Definition: CoordinateAxis.h:338
const CoordinateAxis::XCoordinateAxis XAxis
Constant representing the X coordinate axis; will implicitly convert to the integer 0 when used in a ...