vnl_cross.h
Go to the documentation of this file.
1 #ifndef vnl_cross_h_
2 #define vnl_cross_h_
3 //:
4 // \file
5 // Implements cross product for vectors.
6 // \author Amitha Perera
7 // \verbatim
8 // Modifications
9 // Oct.2002 - Amitha Perera - moved from vnl_vector.h
10 // \endverbatim
11 
12 #include <vnl/vnl_vector.h>
13 #include <vnl/vnl_vector_fixed.h>
14 #include <cassert>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 #include "vnl/vnl_export.h"
19 
20 //: Compute the 2-D cross product
21 // \relatesalso vnl_vector
22 template<class T>
23 inline T
24 vnl_cross_2d( const vnl_vector<T>& v1, const vnl_vector<T>& v2 )
25 {
26  assert( v1.size() >= 2 && v2.size() >= 2 );
27  return v1[0] * v2[1] - v1[1] * v2[0];
28 }
29 
30 //: Compute the 2-D cross product
31 // \relatesalso vnl_vector_fixed
32 template<class T>
33 inline T
35 {
36  return v1[0] * v2[1] - v1[1] * v2[0];
37 }
38 
39 //: Compute the 2-D cross product
40 // \relatesalso vnl_vector
41 // \relatesalso vnl_vector_fixed
42 template<class T>
43 inline T
45 {
46  assert( v2.size() == 2 );
47  return v1[0] * v2[1] - v1[1] * v2[0];
48 }
49 
50 //: Compute the 2-D cross product
51 // \relatesalso vnl_vector
52 // \relatesalso vnl_vector_fixed
53 template<class T>
54 inline T
56 {
57  assert( v1.size() == 2 );
58  return v1[0] * v2[1] - v1[1] * v2[0];
59 }
60 
61 //: Compute the 3-D cross product
62 // \relatesalso vnl_vector
63 template<class T>
64 inline vnl_vector<T>
65 vnl_cross_3d( const vnl_vector<T>& v1, const vnl_vector<T>& v2 )
66 {
67  assert( v1.size() == 3 && v2.size() == 3 );
68  vnl_vector<T> result(3);
69  result[0] = v1[1] * v2[2] - v1[2] * v2[1]; // work for both col/row
70  result[1] = v1[2] * v2[0] - v1[0] * v2[2]; // representation
71  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
72  return result;
73 }
74 
75 //: Compute the 3-D cross product
76 // \relatesalso vnl_vector_fixed
77 template<class T>
80 {
81  vnl_vector_fixed<T,3> result;
82  result[0] = v1[1] * v2[2] - v1[2] * v2[1]; // work for both col/row
83  result[1] = v1[2] * v2[0] - v1[0] * v2[2]; // representation
84  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
85  return result;
86 }
87 
88 //: Compute the 3-D cross product
89 // \relatesalso vnl_vector
90 // \relatesalso vnl_vector_fixed
91 template<class T,unsigned int n>
94 {
95  return vnl_cross_3d(a.as_ref(), b);
96 }
97 
98 //: Compute the 3-D cross product
99 // \relatesalso vnl_vector
100 // \relatesalso vnl_vector_fixed
101 template<class T,unsigned int n>
104 {
105  return vnl_cross_3d(a, b.as_ref());
106 }
107 
108 #endif // vnl_cross_h_
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
vnl_vector_ref< T > as_ref()
Explicit conversion to a vnl_vector_ref.
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
Fixed length stack-stored vector.
T vnl_cross_2d(const vnl_vector< T > &v1, const vnl_vector< T > &v2)
Compute the 2-D cross product.
Definition: vnl_cross.h:24
vnl_vector< T > vnl_cross_3d(const vnl_vector< T > &v1, const vnl_vector< T > &v2)
Compute the 3-D cross product.
Definition: vnl_cross.h:65