core/vnl/vnl_cross.h
Go to the documentation of this file.
00001 #ifndef vnl_cross_h_
00002 #define vnl_cross_h_
00003 //:
00004 // \file
00005 // Implements cross product for vectors.
00006 // \author Amitha Perera
00007 // \verbatim
00008 //  Modifications
00009 //   Oct.2002 - Amitha Perera - moved from vnl_vector.h
00010 // \endverbatim
00011 
00012 #include <vnl/vnl_vector.h>
00013 #include <vnl/vnl_vector_fixed.h>
00014 #include <vcl_cassert.h>
00015 
00016 //: Compute the 2-D cross product
00017 // \relatesalso vnl_vector
00018 template<class T>
00019 inline T
00020 vnl_cross_2d( const vnl_vector<T>& v1, const vnl_vector<T>& v2 )
00021 {
00022   assert( v1.size() >= 2 && v2.size() >= 2 );
00023   return v1[0] * v2[1] - v1[1] * v2[0];
00024 }
00025 
00026 //: Compute the 2-D cross product
00027 // \relatesalso vnl_vector_fixed
00028 template<class T>
00029 inline T
00030 vnl_cross_2d( const vnl_vector_fixed<T,2>& v1, const vnl_vector_fixed<T,2>& v2 )
00031 {
00032   return v1[0] * v2[1] - v1[1] * v2[0];
00033 }
00034 
00035 //: Compute the 2-D cross product
00036 // \relatesalso vnl_vector
00037 // \relatesalso vnl_vector_fixed
00038 template<class T>
00039 inline T
00040 vnl_cross_2d(vnl_vector_fixed<T,2> const& v1, vnl_vector<T> const& v2)
00041 {
00042   assert( v2.size() == 2 );
00043   return v1[0] * v2[1] - v1[1] * v2[0];
00044 }
00045 
00046 //: Compute the 2-D cross product
00047 // \relatesalso vnl_vector
00048 // \relatesalso vnl_vector_fixed
00049 template<class T>
00050 inline T
00051 vnl_cross_2d(vnl_vector<T> const& v1, vnl_vector_fixed<T,2> const& v2)
00052 {
00053   assert( v1.size() == 2 );
00054   return v1[0] * v2[1] - v1[1] * v2[0];
00055 }
00056 
00057 //: Compute the 3-D cross product
00058 // \relatesalso vnl_vector
00059 template<class T>
00060 inline vnl_vector<T>
00061 vnl_cross_3d( const vnl_vector<T>& v1, const vnl_vector<T>& v2 )
00062 {
00063   assert( v1.size() == 3 && v2.size() == 3 );
00064   vnl_vector<T> result(3);
00065   result[0] = v1[1] * v2[2] - v1[2] * v2[1]; // work for both col/row
00066   result[1] = v1[2] * v2[0] - v1[0] * v2[2]; // representation
00067   result[2] = v1[0] * v2[1] - v1[1] * v2[0];
00068   return result;
00069 }
00070 
00071 //: Compute the 3-D cross product
00072 // \relatesalso vnl_vector_fixed
00073 template<class T>
00074 inline vnl_vector_fixed<T,3>
00075 vnl_cross_3d( const vnl_vector_fixed<T,3>& v1, const vnl_vector_fixed<T,3>& v2 )
00076 {
00077   vnl_vector_fixed<T,3> result;
00078   result[0] = v1[1] * v2[2] - v1[2] * v2[1]; // work for both col/row
00079   result[1] = v1[2] * v2[0] - v1[0] * v2[2]; // representation
00080   result[2] = v1[0] * v2[1] - v1[1] * v2[0];
00081   return result;
00082 }
00083 
00084 //: Compute the 3-D cross product
00085 // \relatesalso vnl_vector
00086 // \relatesalso vnl_vector_fixed
00087 template<class T,unsigned int n>
00088 inline vnl_vector_fixed<T,n>
00089 vnl_cross_3d( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
00090 {
00091   return vnl_cross_3d(a.as_ref(), b);
00092 }
00093 
00094 //: Compute the 3-D cross product
00095 // \relatesalso vnl_vector
00096 // \relatesalso vnl_vector_fixed
00097 template<class T,unsigned int n>
00098 inline vnl_vector_fixed<T,n>
00099 vnl_cross_3d( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
00100 {
00101   return vnl_cross_3d(a, b.as_ref());
00102 }
00103 
00104 #endif // vnl_cross_h_