//=========================================================================== // 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. //=========================================================================== 00013 #ifndef _BARYCOORDSYSTEMTRIANGLE3D_H 00014 #define _BARYCOORDSYSTEMTRIANGLE3D_H 00015 00016 00017 #include "GoTools/utils/Array.h" 00018 #include "GoTools/utils/Volumes.h" 00019 #include <iostream> 00020 #include <algorithm> 00021 00022 00023 namespace Go { 00024 00031 class BaryCoordSystemTriangle3D { 00032 public: 00034 BaryCoordSystemTriangle3D() { } 00035 00038 BaryCoordSystemTriangle3D(const Array<double, 3>* corners) 00039 { 00040 std::copy(corners, corners+3, corners_); 00041 total_area_ = area(corners); 00042 normal_ = (corners[1]-corners[0]) % (corners[2]-corners[0]); 00043 normal_.normalize(); 00044 } 00045 00048 template <typename T> 00049 Array<T, 3> baryToCart(const Array<T, 3>& bary_pt) const 00050 { 00051 Array<T, 3> cart_pt(T(0.0), T(0.0), T(0.0)); 00052 for (int i = 0; i < 3; ++i) { 00053 cart_pt[0] += corners_[i][0] * bary_pt[i]; 00054 cart_pt[1] += corners_[i][1] * bary_pt[i]; 00055 cart_pt[2] += corners_[i][2] * bary_pt[i]; 00056 } 00057 return cart_pt; 00058 } 00059 00062 template <typename T> 00063 Array<T, 3> cartToBary(const Array<T, 3>& cart_pt) const 00064 { 00065 static Array<T, 3> subtriangle[3]; 00066 int i; 00067 for (i = 1; i < 3; ++i) { 00068 subtriangle[i] = corners_[i]; 00069 } 00070 00071 Array<T, 3> bary_pt; 00072 for (i = 0; i < 3; ++i) { 00073 subtriangle[i] = cart_pt; 00074 bary_pt[i] = signed_area(subtriangle, normal_); 00075 subtriangle[i] = corners_[i]; 00076 } 00077 return bary_pt / total_area_; 00078 } 00079 00080 private: 00081 Array<double, 3> corners_[3]; 00082 Array<double, 3> normal_; 00083 double total_area_; 00084 }; 00085 00086 00087 } // namespace Go 00088 00089 #endif // _BARYCOORDSYSTEMTRIANGLE3D_H 00090