mt
 All Classes Files Functions Enumerations Groups Pages
unit3.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 // HEADER GUARD
24 #ifndef MT_UNIT3_H
25 #define MT_UNIT3_H
26 
27 
28 // MT LIBRARY HEADERS
29 #include <mt/exception.h>
30 #include <mt/scalar.h>
31 #include <mt/vector3.h>
32 
33 
35 
36 namespace mt
37 {
38 
39 
41 
44 
91 
92 class Unit3 : public Vector3
93 {
94 public:
95 
96 // LIFECYCLE
97 
99  Unit3();
100 
105  Unit3(const Scalar& x,
106  const Scalar& y,
107  const Scalar& z);
108 
110  explicit Unit3(const Scalar* u);
111 
113  Unit3(const Vector3& v);
114 
115  // Compiler generated copy constructor for Unit3 input is being used
116 
117  // Compiler generated destructor is being used
118 
119 
120 // OPERATORS
121 
122  // Compiler generated assignment operator for Unit3 input is being used
123 
127  Unit3& operator=(const Vector3& v);
128 
131  bool operator==(const Unit3& u) const;
132 
133  bool operator!=(const Unit3& u) const;
134 
135 // OPERATIONS
136 
138  Scalar angleCos(const Unit3& u) const;
139 
141  Scalar angle(const Unit3& u) const;
142 
143 // ACCESS
144 
146  void setValue(const Scalar& x,
147  const Scalar& y,
148  const Scalar& z);
149 
151  void setValue(const Scalar* u);
152 
153 };
154 
155 
157 
158 // FUNCTIONS
159 
161 Scalar angleCos(const Unit3& u1,
162  const Unit3& u2);
163 
164 
166 Scalar angle(const Unit3& u1,
167  const Unit3& u2);
168 
180 void orthonormalBasis(const Unit3& u1,
181  const Unit3& u2,
182  Unit3& v2,
183  Unit3& v3);
184 
185 
187 
188 // LIFECYCLE
189 
190 inline Unit3::Unit3() : Vector3(0.0, 0.0, 1.0) {}
191 
192 
193 inline Unit3::Unit3(const Scalar& x,
194  const Scalar& y,
195  const Scalar& z)
196 {
197  setValue(x, y, z);
198 }
199 
200 
201 inline Unit3::Unit3(const Scalar* u)
202 {
203  setValue(u);
204 }
205 
206 
207 inline Unit3::Unit3(const Vector3& v) : Vector3(v)
208 {
209  normalize();
210 }
211 
212 
213 // OPERATORS
214 
215 inline Unit3& Unit3::operator=(const Vector3& v)
216 {
217  if (this != &v)
218  {
219  // Assignation
221  normalize();
222  }
223  return *this;
224 }
225 
226 
227 inline bool Unit3::operator==(const Unit3& u) const
228 {
229  return (angle(u) == 0.0);
230 }
231 
232 
233 inline bool Unit3::operator!=(const Unit3& u) const
234 {
235  return !(*this == u);
236 }
237 
238 
239 // OPERATIONS
240 
241 inline Scalar Unit3::angleCos(const Unit3& u) const
242 {
243  Scalar ang_cos(dot(u));
244  return ang_cos;
245 }
246 
247 
248 inline Scalar Unit3::angle(const Unit3& u) const
249 {
250  const Scalar ang_cos(angleCos(u));
251  return acos(ang_cos);
252 }
253 
254 
255 // ACCESS
256 
257 inline void Unit3::setValue(const Scalar& x,
258  const Scalar& y,
259  const Scalar& z)
260 {
261  Vector3::setValue(x, y, z);
262  normalize();
263 }
264 
265 
266 inline void Unit3::setValue(const Scalar* u)
267 {
269  normalize();
270 }
271 
272 
274 
275 inline Scalar angleCos(const Unit3& u1,
276  const Unit3& u2)
277 {
278  return u1.angleCos(u2);
279 }
280 
281 
282 inline Scalar angle(const Unit3& u1,
283  const Unit3& u2)
284 {
285  return u1.angle(u2);
286 }
287 
288 
289 inline void orthonormalBasis(const Unit3& u1,
290  const Unit3& u2,
291  Unit3& v2,
292  Unit3& v3)
293 {
294  // If vectors are parallel or antiparallel an exception is thrown
295  #ifdef MT_USE_BASIC_SCALAR
296  util::Assert(abs(angleCos(u1, u2)) != 1.0,
297  Exception("Cannot define orthonormal basis from (anti)parallel \
298 input vectors"));
299  #endif
300 
301  // Values of unit vectors perpendicular to "u1"
302  v2 = u2 - dot(u1, u2) * u1;
303  v3 = cross(u1, v2);
304 }
305 
306 } // mt
307 
308 #endif // MT_UNIT3_H