Simbody  3.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UnitVec.h
Go to the documentation of this file.
1 #ifndef SimTK_UNITVEC_H
2 #define SimTK_UNITVEC_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: Paul Mitiguy *
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 
32 
33 #include <iosfwd> // Forward declaration of iostream
34 
35 namespace SimTK {
36 
37 //-----------------------------------------------------------------------------
38 // Forward declarations. These are templatized by precision P and and stride S
39 // but always have length 3. TODO: this should be generalized to other lengths.
40 template <class P, int S> class UnitVec;
41 template <class P, int S> class UnitRow;
42 
43 // UnitVec3 is more intelligible name for UnitVec<Real,1>.
47 
48 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 template <class P, int S>
56 class UnitVec : public Vec<3,P,S> {
57  typedef P RealP;
58 public:
61 
64  UnitVec() : BaseVec(NTraits<P>::getNaN()) {}
65 
68  UnitVec(const UnitVec& u)
69  : BaseVec( static_cast<const BaseVec&>(u) ) {}
70 
73  template <int S2> UnitVec(const UnitVec<P,S2>& u)
74  : BaseVec( static_cast<const typename UnitVec<P,S2>::BaseVec&>(u) ) {}
75 
77  explicit UnitVec(const BaseVec& v) : BaseVec(v/v.norm()) {}
80  template <int S2>
81  explicit UnitVec(const Vec<3,P,S2>& v) : BaseVec(v/v.norm()) {}
82 
86  UnitVec(const RealP& x, const RealP& y, const RealP& z) : BaseVec(x,y,z)
87  { static_cast<BaseVec&>(*this) /= BaseVec::norm(); }
88 
91  UnitVec(const CoordinateAxis& axis) : BaseVec(0)
92  { BaseVec::operator[](axis) = 1; }
93 
98  { BaseVec::operator[](dir.getAxis()) = RealP(dir.getDirection()); }
99 
102  explicit UnitVec(int axis) : BaseVec(0)
103  { assert(0 <= axis && axis <= 2);
104  BaseVec::operator[](axis) = 1; }
105 
108  { BaseVec::operator=(static_cast<const BaseVec&>(u));
109  return *this; }
110 
113  template <int S2> UnitVec& operator=(const UnitVec<P,S2>& u)
114  { BaseVec::operator=(static_cast<const typename UnitVec<P,S2>::BaseVec&>(u));
115  return *this; }
116 
118  const BaseVec& asVec3() const {return static_cast<const BaseVec&>(*this);}
119 
120  // Override Vec3 methods which preserve length. These return a
121  // packed UnitVec regardless of our stride.
122 
125  UnitVec<P,1> negate() const {return UnitVec<P,1>(-asVec3(),true);}
128  UnitVec<P,1> operator-() const {return negate();}
129 
132  const TransposeType& operator~() const {return *reinterpret_cast<const TransposeType*>(this);}
135  TransposeType& operator~() {return *reinterpret_cast<TransposeType*>(this);}
136 
137  // We have to define these here so that the non-const ones won't be
138  // inherited. We don't trust anyone to write on one element of a UnitVec!
139 
143  const RealP& operator[](int i) const { return BaseVec::operator[](i); }
147  const RealP& operator()(int i) const { return BaseVec::operator()(i); }
148 
154  UnitVec<P,1> abs() const {return UnitVec<P,1>( asVec3().abs(), true );}
155 
159  inline UnitVec<P,1> perp() const;
160 
164  UnitVec(const BaseVec& v, bool) : BaseVec(v) {}
169  template <int S2> UnitVec(const Vec<3,RealP,S2>& v, bool) : BaseVec(v) { }
170 
176  static const UnitVec& getAs(const RealP* p)
177  { return *reinterpret_cast<const UnitVec*>(p); }
178 };
179 
180 
181 template <class P, int S> inline UnitVec<P,1>
183  // Choose the coordinate axis which makes the largest angle
184  // with this vector, that is, has the "least u" along it.
185  const UnitVec<P,1> u(abs()); // reflect to first octant
186  const int minAxis = u[0] <= u[1] ? (u[0] <= u[2] ? 0 : 2)
187  : (u[1] <= u[2] ? 1 : 2);
188  // Cross returns a Vec3 result which is then normalized.
189  return UnitVec<P,1>( *this % UnitVec<P,1>(minAxis) );
190 }
191 
194 template <class P, int S1, int S2> inline bool
196 { return u1.asVec3() == u2.asVec3(); }
197 
201 template <class P, int S1, int S2> inline bool
203 { return !(u1==u2); }
204 
205 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
211 template <class P, int S>
212 class UnitRow : public Row<3,P,S> {
213  typedef P RealP;
214 public:
217 
218  UnitRow() : BaseRow(NTraits<P>::getNaN()) { }
219 
221  UnitRow(const UnitRow& u)
222  : BaseRow(static_cast<const BaseRow&>(u)) {}
223 
226  template <int S2> UnitRow(const UnitRow<P,S2>& u)
227  : BaseRow(static_cast<const typename UnitRow<P,S2>::BaseRow&>(u)) { }
228 
231  { BaseRow::operator=(static_cast<const BaseRow&>(u));
232  return *this; }
233 
235  template <int S2> UnitRow& operator=(const UnitRow<P,S2>& u)
236  { BaseRow::operator=(static_cast<const typename UnitRow<P,S2>::BaseRow&>(u));
237  return *this; }
238 
240  explicit UnitRow(const BaseRow& v) : BaseRow(v/v.norm()) {}
243  template <int S2>
244  explicit UnitRow(const Row<3,P,S2>& v) : BaseRow(v/v.norm()) {}
245 
248  UnitRow(const RealP& x, const RealP& y, const RealP& z)
249  : BaseRow(x,y,z)
250  { static_cast<BaseRow&>(*this) /= BaseRow::norm(); }
251 
253  explicit UnitRow(int axis) : BaseRow(0)
254  { assert(0 <= axis && axis <= 2);
255  BaseRow::operator[](axis) = 1; }
256 
258  const BaseRow& asRow3() const {return static_cast<const BaseRow&>(*this);}
259 
260  // Override Row3 methods which preserve length. These return the
261  // packed UnitRow regardless of our stride.
262 
265  UnitRow<P,1> negate() const { return UnitRow<P,1>(-asRow3(),true); }
268  UnitRow<P,1> operator-() const { return negate();}
269 
272  const TransposeType& operator~() const {return *reinterpret_cast<const TransposeType*>(this);}
275  TransposeType& operator~() {return *reinterpret_cast<TransposeType*>(this);}
276 
277  // We have to define these here so that the non-const ones won't be
278  // inherited. We don't trust anyone to write on one element of a UnitRow!
279 
283  const RealP& operator[](int i) const { return BaseRow::operator[](i); }
287  const RealP& operator()(int i) const { return BaseRow::operator()(i); }
288 
294  UnitRow<P,1> abs() const {return UnitRow<P,1>(asRow3().abs(),true);}
295 
299  inline UnitRow<P,1> perp() const;
300 
304  UnitRow( const BaseRow& v, bool ) : BaseRow(v) { }
309  template <int S2> UnitRow( const Row<3,P,S2>& v, bool ) : BaseRow(v) { }
310 
316  static const UnitRow& getAs(const RealP* p)
317  { return *reinterpret_cast<const UnitRow*>(p); }
318 };
319 
320 template <class P, int S>
322  // Choose the coordinate axis which makes the largest angle
323  // with this vector, that is, has the "least u" along it.
324  const UnitRow<P,1> u(abs()); // reflect to first octant
325  const int minAxis = u[0] <= u[1] ? (u[0] <= u[2] ? 0 : 2)
326  : (u[1] <= u[2] ? 1 : 2);
327  // Cross returns a Row3 result which is then normalized.
328  return UnitRow<P,1>(*this % UnitRow<P,1>(minAxis));
329 }
330 
331 
334 template <class P, int S1, int S2> inline bool
336 { return u1.asRow3() == u2.asRow3(); }
337 
341 template <class P, int S1, int S2> inline bool
343 { return !(u1==u2); }
344 
345 //------------------------------------------------------------------------------
346 } // End of namespace SimTK
347 
348 //--------------------------------------------------------------------------
349 #endif // SimTK_UNITVEC_H_
350 //--------------------------------------------------------------------------
351 
352