BaryCoordSystemTriangle3D.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 _BARYCOORDSYSTEMTRIANGLE3D_H
00034 #define _BARYCOORDSYSTEMTRIANGLE3D_H
00035 
00036 
00037 #include "Array.h"
00038 #include "Volumes.h"
00039 #include <iostream>
00040 #include <algorithm>
00041 
00042 
00043 namespace Go {
00046 
00047 
00054 class BaryCoordSystemTriangle3D {
00055 public:
00057     BaryCoordSystemTriangle3D() { }
00058 
00061     BaryCoordSystemTriangle3D(const Array<double, 3>* corners)
00062     {
00063         std::copy(corners, corners+3, corners_);
00064         total_area_ = area(corners);
00065         normal_ = (corners[1]-corners[0]) % (corners[2]-corners[0]);
00066         normal_.normalize();
00067     }
00068 
00071     template <typename T>
00072     Array<T, 3> baryToCart(const Array<T, 3>& bary_pt) const
00073     {
00074         Array<T, 3> cart_pt(T(0.0), T(0.0), T(0.0));
00075         for (int i = 0; i < 3; ++i) {
00076             cart_pt[0] += corners_[i][0] * bary_pt[i];
00077             cart_pt[1] += corners_[i][1] * bary_pt[i];
00078             cart_pt[2] += corners_[i][2] * bary_pt[i];
00079         }
00080         return cart_pt;
00081     }
00082 
00085     template <typename T>
00086     Array<T, 3> cartToBary(const Array<T, 3>& cart_pt) const
00087     {
00088         static Array<T, 3> subtriangle[3];
00089         int i;
00090         for (i = 1; i < 3; ++i) {
00091             subtriangle[i] = corners_[i];
00092         }
00093 
00094         Array<T, 3> bary_pt;
00095         for (i = 0; i < 3; ++i) {
00096             subtriangle[i] = cart_pt;
00097             bary_pt[i] = signed_area(subtriangle, normal_);
00098             subtriangle[i] = corners_[i];
00099         }
00100         return bary_pt / total_area_;
00101     }
00102 
00103 private:
00104     Array<double, 3> corners_[3];
00105     Array<double, 3> normal_;
00106     double total_area_;
00107 };
00108 
00109 
00111 } // namespace Go
00112 
00113 #endif // _BARYCOORDSYSTEMTRIANGLE3D_H
00114 
00115 

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