Simbody  3.4
 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