TCMathCoordUtil.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCMathCoordUtil.h 801 2008-01-23 19:46:25Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TC_COORD_UTIL_H_
00037 #define _TC_COORD_UTIL_H_
00038 
00039 #include "TCMathCoord3D.h"
00040 #include "TCMathCoord2D.h"
00041 
00042 namespace TC
00043 {
00044    namespace Math
00045    {
00063       template <class COORD_TYPE>
00064       inline double Distance(const COORD_TYPE& a, const COORD_TYPE& b)
00065       {
00066          COORD_TYPE c = a-b;
00067          return c.Length();
00068       }
00069 
00076       template <class COORD_TYPE>
00077       inline double Distance2(const COORD_TYPE &a, const COORD_TYPE &b)
00078       {
00079          COORD_TYPE c = a-b;
00080          return c.Length2();
00081       }
00082 
00089       template <class COORD_TYPE>
00090       inline typename COORD_TYPE::DataType DotProduct(const COORD_TYPE& a, const COORD_TYPE& b)
00091       {
00092          return a * b;
00093       }
00094 
00101       template <class COORD_TYPE>
00102       inline typename COORD_TYPE::DataType DotProduct2(const COORD_TYPE& a, const COORD_TYPE& b)
00103       {
00104          COORD_TYPE v = a * b;
00105          return v * v;
00106       }
00107 
00114       template <class COORD_TYPE>
00115       inline COORD_TYPE Normalize(const COORD_TYPE& coord)
00116       {
00117          COORD_TYPE new_coord(coord);
00118          new_coord.Normalize();
00119          return new_coord;
00120       }
00121 
00129       template <class COORD_TYPE>
00130       inline COORD_TYPE CrossProduct(const COORD_TYPE& a, const COORD_TYPE& b)
00131       {
00132          return a ^ b;
00133       }
00134 
00135       template <class T>
00136       inline Coord3D<T> Max(const Coord3D<T>& a, const Coord3D<T>& b)
00137       {
00138          Coord3D<T> coord;
00139          coord[0] = TC::Util::Max(a[0], b[0]);
00140          coord[1] = TC::Util::Max(a[1], b[1]);
00141          coord[2] = TC::Util::Max(a[2], b[2]);
00142 
00143          return coord;
00144       }
00145 
00146       template <class T>
00147       inline Coord2D<T> Max(const Coord2D<T>& a, const Coord2D<T>& b)
00148       {
00149          Coord2D<T> coord;
00150          coord[0] = TC::Util::Max(a[0], b[0]);
00151          coord[1] = TC::Util::Max(a[1], b[1]);
00152 
00153          return coord;
00154       }
00155 
00156       template <class T>
00157       inline Coord3D<T> Min(const Coord3D<T>& a, const Coord3D<T>& b)
00158       {
00159          Coord3D<T> coord;
00160          coord[0] = TC::Util::Min(a[0], b[0]);
00161          coord[1] = TC::Util::Min(a[1], b[1]);
00162          coord[2] = TC::Util::Min(a[2], b[2]);
00163 
00164          return coord;
00165       }
00166 
00167       template <class T>
00168       inline Coord2D<T> Min(const Coord2D<T>& a, const Coord2D<T>& b)
00169       {
00170          Coord2D<T> coord;
00171          coord[0] = TC::Util::Min(a[0], b[0]);
00172          coord[1] = TC::Util::Min(a[1], b[1]);
00173 
00174          return coord;
00175       }
00176 
00183       template <class T>
00184       inline Coord3D<T> Normalvector(sint32 numPoints, const Coord3D<T>* coords)
00185       {
00186          Coord3D<T> out;
00187 
00188          for (sint32 pos=0; pos<numPoints; pos++)
00189          {
00190             sint32 index = (pos+1) % numPoints;
00191             out[0] += (coords[pos][1] - coords[index][1]) * (coords[pos][2] + coords[index][2]);
00192             out[1] += (coords[pos][2] - coords[index][2]) * (coords[pos][0] + coords[index][0]);
00193             out[2] += (coords[pos][0] - coords[index][0]) * (coords[pos][1] + coords[index][1]);
00194          }
00195          return out;
00196       }
00197 
00204       template <class COORD_TYPE>
00205       inline double Angle(const COORD_TYPE& a, const COORD_TYPE& b)
00206       {
00207          if (DotProduct(a, b) < 0.0)
00208          {
00209             double sinValue = (-b-a).Length()/2;
00210             if (sinValue > 1.0) sinValue = 1.0;
00211             if (sinValue < -1.0) sinValue = -1.0;
00212             return PI - 2*asin(sinValue);
00213          }
00214          else
00215          {
00216             double sinValue = (b-a).Length()/2;
00217             if (sinValue > 1.0) sinValue = 1.0;
00218             if (sinValue < -1.0) sinValue = -1.0;
00219             return 2*asin(sinValue);
00220          }
00221       }
00222 
00223       template <class T>
00224       inline bool IsPointInCircle(const Coord2D<T> &Point,
00225          const Coord2D<T> &center,
00226          double radius)
00227       {   
00228          double distance = sqrt((double)((Point[0]-center[0])*(Point[0]-center[0]) 
00229             + (Point[1]-center[1])*(Point[1]-center[1])));
00230 
00231          return distance <= radius;
00232       }
00233 
00234       template <class T>
00235       inline bool CheckInsidePolygon(const Coord2D<T> &point,
00236          sint32 num_points, const Coord2D<T> *polygon)
00237       {
00238          uint32 crossings = 0;
00239          //loop over all edges of polygon
00240          for(sint32 num1=0; num1<num_points; num1++)
00241          {
00242             sint32 num2 = (num1 - 1 + num_points) % num_points;
00243             Coord2D<T> point1 = polygon[num1] - point;
00244             Coord2D<T> point2 = polygon[num2] - point;
00245 
00246             //check if edge i-num2 straddles x axis
00247             if (((point1[1] > 0.0) && (polygon[num2][1] <= 0.0)) ||
00248                ((point2[1] > 0.0) && (point1[1] <= 0.0))  )
00249             {
00250                //edge crosses ray if strictly positive intersection
00251                double det = point2[1] - point1[1];
00252                double d   = point1[0] * point2[1] -
00253                   point2[0] * point1[1]  ;
00254 
00255                if((d / det) > 0.0) crossings++;
00256             }
00257          }
00258 
00259          //if number of ray crossings odd, point is within polygon
00260          return (crossings % 2) != 0;
00261       }
00262 
00267    } // namespace Math
00268 } // namespace TC
00269 
00270 
00271 #endif // _TC_COORD_UTIL_H_
00272 

Copyright (c) Thomas Goessler 2003 - 2008