Volumes.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _VOLUMES_H
00034 #define _VOLUMES_H
00035 
00036 #include "Factorial.h"
00037 #include "Array.h"
00038 #include <vector>
00039 
00040 
00041 namespace Go {
00044 
00045 
00046 
00050 template< typename T >
00051 inline T determinantOf(const Array<T, 2>* a) 
00052 {
00053     return a[0][0] * a[1][1] - a[1][0] * a[0][1];
00054 };
00055 
00056 
00060 template<typename T>
00061 inline T determinantOf(const Array<T, 3>* a) 
00062 {
00063     return 
00064         a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2]) -
00065         a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2]) +
00066         a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
00067 };
00068 
00069 
00072 template<typename T, int Dim>
00073 inline T simplex_volume(const Array<T, Dim>* a)
00074 {
00075     Array<T, Dim> tmp[Dim];
00076     for (int i = 0; i < Dim; ++i) {
00077         tmp[i] = a[i] - a[i+1];
00078     }
00079     return determinantOf(tmp) * InverseFactorial<double, Dim>::val();
00080     // determinant / factorial
00081 }
00082 
00083 
00086 template <typename T>
00087 inline T area(const Array<T, 2>* c)
00088 { return simplex_volume(c); }
00089 
00090 
00093 template < typename T >
00094 inline T area(const Array<T, 3>* c)
00095 {
00096     // Using the one-half cross product rule
00097     Array<T, 3> d0 = c[1] - c[0];
00098     Array<T, 3> d1 = c[2] - c[0];
00099     Array<T, 3> crossprod = d0.cross(d1);
00100     return 0.5 * crossprod.length();
00101 }
00102 
00103 
00105 template <typename T>
00106 inline T volume(const Array<T, 3>* c)
00107 { return simplex_volume(c); }
00108 
00109 
00112 template <typename T>
00113 T signed_area(const Array<T, 3>* c, const Array<T, 3>& normal)
00114 {
00115     // Using the one-half cross product rule
00116     Array<T, 3> d0 = c[1] - c[0];
00117     Array<T, 3> d1 = c[2] - c[0];
00118     Array<T, 3> crossprod = d0.cross(d1);
00119     if (crossprod*normal > 0) {
00120         return 0.5 * crossprod.length();
00121     } else {
00122         return -0.5 * crossprod.length();
00123     }
00124 }
00125 
00126 
00128 } // namespace Go
00129 
00130 
00131 #endif // _VOLUMES_H
00132 
00133 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1