//=========================================================================== // GoTools Core - SINTEF Geometry Tools Core library, version 2.0.1 // // Copyright (C) 2000-2007, 2010 SINTEF ICT, Applied Mathematics, Norway. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // Contact information: E-mail: tor.dokken@sintef.no // SINTEF ICT, Department of Applied Mathematics, // P.O. Box 124 Blindern, // 0314 Oslo, Norway. // // Other licenses are also available for this software, notably licenses // for: // - Building commercial software. // - Building software whose source code you wish to keep private. //=========================================================================== 00015 #ifndef _VOLUMES_H 00016 #define _VOLUMES_H 00017 00018 #include "GoTools/utils/Factorial.h" 00019 #include "GoTools/utils/Array.h" 00020 #include <vector> 00021 00022 00023 namespace Go { 00024 00025 00029 template< typename T > 00030 inline T determinantOf(const Array<T, 2>* a) 00031 { 00032 return a[0][0] * a[1][1] - a[1][0] * a[0][1]; 00033 }; 00034 00035 00039 template<typename T> 00040 inline T determinantOf(const Array<T, 3>* a) 00041 { 00042 return 00043 a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2]) - 00044 a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2]) + 00045 a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]); 00046 }; 00047 00048 00051 template<typename T, int Dim> 00052 inline T simplex_volume(const Array<T, Dim>* a) 00053 { 00054 Array<T, Dim> tmp[Dim]; 00055 for (int i = 0; i < Dim; ++i) { 00056 tmp[i] = a[i] - a[i+1]; 00057 } 00058 return determinantOf(tmp) * InverseFactorial<double, Dim>::val(); 00059 // determinant / factorial 00060 } 00061 00062 00065 template <typename T> 00066 inline T area(const Array<T, 2>* c) 00067 { return simplex_volume(c); } 00068 00069 00072 template < typename T > 00073 inline T area(const Array<T, 3>* c) 00074 { 00075 // Using the one-half cross product rule 00076 Array<T, 3> d0 = c[1] - c[0]; 00077 Array<T, 3> d1 = c[2] - c[0]; 00078 Array<T, 3> crossprod = d0.cross(d1); 00079 return 0.5 * crossprod.length(); 00080 } 00081 00082 00084 template <typename T> 00085 inline T volume(const Array<T, 3>* c) 00086 { return simplex_volume(c); } 00087 00088 00091 template <typename T> 00092 T signed_area(const Array<T, 3>* c, const Array<T, 3>& normal) 00093 { 00094 // Using the one-half cross product rule 00095 Array<T, 3> d0 = c[1] - c[0]; 00096 Array<T, 3> d1 = c[2] - c[0]; 00097 Array<T, 3> crossprod = d0.cross(d1); 00098 if (crossprod*normal > 0) { 00099 return 0.5 * crossprod.length(); 00100 } else { 00101 return -0.5 * crossprod.length(); 00102 } 00103 } 00104 00105 00106 } // namespace Go 00107 00108 00109 #endif // _VOLUMES_H 00110