vm_math  1.0.7
vm_v3math.h
1 #ifndef vm_v3math_h_INCLUDED
2 #define vm_v3math_h_INCLUDED
3 
4 // File: vm_v3math.h
5 // Author: Terry Gaetz
6 
7 /* --8<--8<--8<--8<--
8  *
9  * Copyright (C) 2006 Smithsonian Astrophysical Observatory
10  *
11  * This file is part of vm_math.cvs
12  *
13  * vm_math.cvs is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * vm_math.cvs is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the
25  * Free Software Foundation, Inc.
26  * 51 Franklin Street, Fifth Floor
27  * Boston, MA 02110-1301, USA
28  *
29  * -->8-->8-->8-->8-- */
30 
31 /****************************************************************************
32  * Description: collection of methods for vm_V3Math
33  *
34  * History
35  *--------
36  * 0.0.0 1998-Jan-30 tjg original version
37  */
38 
39 #include <vm_math/vm_vmath.h> // vm_VMath<T,N>
40 #include <cstring> // memcpy strlen
41 #include <cmath> // sqrt fabs
42 #include <iostream> // ostream
43 
44 //########################################################################
45 // vm_V3Math<T_fp>
46 //########################################################################
47 
68 template <class T_fp>
69 class vm_V3Math
70  : public vm_VMath<T_fp,3>
71 {
72 private:
73 
74  enum ENelts_ { ENelts = 3 };
75 
76 public:
77 
83  typedef T_fp value_type;
84 
85  // ---------------------------------------------------------------------
90 
99  inline static void set( T_fp v[], T_fp x, T_fp y, T_fp z );
100 
107  inline static void set( T_fp v[], T_fp x );
110  // ---------------------------------------------------------------------
122  inline static T_fp unitize( T_fp v[] );
123 
132  inline static T_fp unitize( T_fp vu[], T_fp const vi[] );
135  // ---------------------------------------------------------------------
149  inline static int is_unit_vector( T_fp const v[], T_fp const tol );
150 
161  inline static int are_orthogonal( T_fp const v[], T_fp const other[],
162  T_fp const tol );
163 
174  inline static int are_orthonormal( T_fp const v[], T_fp const other[],
175  T_fp const tol );
178  // ---------------------------------------------------------------------
183 
192  inline static T_fp dot( T_fp const v1[], T_fp const v2[] );
193 
203  inline static void cross( T_fp prod[],
204  T_fp const v1[], T_fp const v2[] );
207  // ---------------------------------------------------------------------
220  inline static std::ostream&
221  print_on( std::ostream& os, T_fp const v[],
222  char const prefix[] = "", char const postfix[] = "" );
223 
232  inline static void
233  cprint_on( FILE* of, T_fp const v[],
234  char const prefix[] = "", char const postfix[] = "" );
237 };
238 
239 //########################################################################
240 //########################################################################
241 //
242 // # # # # # # # ###### ####
243 // # ## # # # ## # # #
244 // # # # # # # # # # ##### ####
245 // # # # # # # # # # # #
246 // # # ## # # # ## # # #
247 // # # # ###### # # # ###### ####
248 //
249 //########################################################################
250 //########################################################################
251 
252 //=========================================================================
253 template <class T_fp>
254 inline void vm_V3Math<T_fp>::
255 set( T_fp v[], T_fp x, T_fp y, T_fp z )
256 { v[0] = x; v[1] = y; v[2] = z; }
257 
258 template <class T_fp>
259 inline void vm_V3Math<T_fp>::
260 set( T_fp v[], T_fp x )
261 { vm_VMath<T_fp,3>::set( v, x ); }
262 
263 template <class T_fp>
264 inline T_fp vm_V3Math<T_fp>::
265 dot( T_fp const v1[], T_fp const v2[] )
266 {
267  return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
268 }
269 
270 template <class T_fp>
271 inline void vm_V3Math<T_fp>::
272 cross( T_fp result[],
273  T_fp const v1[], T_fp const v2[] )
274 {
275  result[0] = v1[1] * v2[2] - v1[2] * v2[1];
276  result[1] = v1[2] * v2[0] - v1[0] * v2[2];
277  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
278 }
279 
280 template <class T_fp>
281 inline T_fp vm_V3Math<T_fp>::
282 unitize( T_fp v[] )
283 {
284  T_fp size = sqrt( dot( v, v ) );
285  if ( size == 0.0 )
286  {
287  return 0.0;
288  }
289  div_eq( v, size );
290 
291  return size;
292 }
293 
294 template <class T_fp>
295 inline T_fp vm_V3Math<T_fp>::
296 unitize( T_fp vu[], T_fp const vi[] )
297 {
298  vm_V3Math<T_fp>::copy( vu, vi );
299  return vm_V3Math<T_fp>::unitize( vu );
300 }
301 
302 template <class T_fp>
303 inline int vm_V3Math<T_fp>::
304 is_unit_vector( T_fp const v[], T_fp const tol )
305 {
306  return !( (fabs(sqrt(dot(v, v))-1.0) > tol ) ? 1 : 0 );
307 }
308 
309 template <class T_fp>
310 inline int vm_V3Math<T_fp>::
311 are_orthogonal( T_fp const v[],
312  T_fp const other[],
313  T_fp const tol )
314 {
315  return !( (fabs(dot(v, other)) > tol ) ? 1 : 0 );
316 }
317 
318 template <class T_fp>
319 inline int vm_V3Math<T_fp>::
320 are_orthonormal( T_fp const v[],
321  T_fp const other[],
322  T_fp const tol )
323 {
324  return is_unit_vector(v,tol) &&
325  is_unit_vector(other,tol) &&
326  are_orthogonal(v,other,tol);
327 }
328 
329 template <class T_fp>
330 inline std::ostream& vm_V3Math<T_fp>::
331 print_on( std::ostream& os, T_fp const v[],
332  char const prefix[], char const postfix[] )
333 { if ( std::strlen(prefix) ) { os << prefix; }
334  os << v[0] << " " << v[1] << " " << v[2];
335  if ( std::strlen(postfix) ) { os << postfix; }
336  return os; }
337 
338 template <class T_fp>
339 inline void vm_V3Math<T_fp>::
340 cprint_on( FILE* of, T_fp const v[],
341  char const prefix[], char const postfix[] )
342 { if ( std::strlen(prefix) ) { std::fprintf(of, "%s", prefix); }
343  std::fprintf(of, "%.18e %.18e %.18e\n", v[0], v[1], v[2]);
344  if ( std::strlen(postfix) ) { std::fprintf(of, "%s", postfix); } }
345 
346 #endif /* vm_v3math.h */