Common/Geom/PointAlg.h

Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00004 //  Copyright (c) 2004 Evan Sherbrooke and Michael Lauer
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 //
00020 //-------------------------------------------------------------------
00021 #pragma once
00022 #ifndef VOLUMILL_POINTALG_H
00023 #define VOLUMILL_POINTALG_H
00024 
00025 #include <Geom/Points.h>
00026 
00027 namespace geom
00028 {
00029 
00031 inline
00032 Vec3d crossProduct(const Vec3d &v0, const Vec3d &v1)
00033 {
00034    return Vec3d(v0[1] * v1[2] - v0[2] * v1[1],
00035                 v0[2] * v1[0] - v0[0] * v1[2],
00036                 v0[0] * v1[1] - v0[1] * v1[0]);
00037 }
00038 
00040 inline
00041 Point3d crossProduct(const Point3d &v0, const Point3d &v1)
00042 {
00043    return Point3d(v0[1] * v1[2] - v0[2] * v1[1],
00044                 v0[2] * v1[0] - v0[0] * v1[2],
00045                 v0[0] * v1[1] - v0[1] * v1[0]);
00046 }
00047 
00051 template <class V>
00052 inline
00053 double tripleProduct(const V &v0, const V &v1, const V &v2)
00054 {
00055    BOOST_STATIC_ASSERT(V::Dim == 3);
00056    return v0[0] * (v1[1] * v2[2] - v1[2] * v2[1])
00057         + v0[1] * (v1[2] * v2[0] - v1[0] * v2[2])
00058         + v0[2] * (v1[0] * v2[1] - v1[1] * v2[0])
00059         ;
00060 }
00061 
00063 inline
00064 double crossProduct(const Vec2d &v0, const Vec2d &v1)
00065 {
00066    return v0[0] * v1[1] - v0[1] * v1[0];
00067 }
00068 
00070 inline
00071 double crossProduct(const Point2d &v0, const Point2d &v1)
00072 {
00073    return v0[0] * v1[1] - v0[1] * v1[0];
00074 }
00075 
00077 template <int n>
00078 VecNd<n> normalize(const VecNd<n> &v)
00079 {
00080    double dot = v*v;
00081    if(dot <= 0.0)
00082    {
00083       // Really, we ought to throw an exception
00084       return VecNd<n>();
00085    }
00086    return v * (1.0/sqrt(dot));
00087 }
00088 
00090 template <int n>
00091 inline
00092 double length(const VecNd<n> &v)
00093 {
00094    double dot = v*v;
00095    return sqrt(dot);
00096 }
00097 
00099 inline double
00100 angle(const Vec2d &v0, const Vec2d &v1)
00101 {
00102    double cross = crossProduct(v0, v1);
00103    double dot = v0 * v1;
00104    double a = atan2(cross, dot);
00105    return a;
00106 }
00107 
00109 template <int n>
00110 inline double distance (const PointNd<n>& p0, const PointNd<n>& p1)
00111 {
00112    return length (VecNd<n> (p1-p0));
00113 }
00114 
00116 template <class Vec>
00117 inline
00118 Vec projectParallel(const Vec &v1,
00119                     const Vec &v2)
00120 {
00121    return ((v1 * v2) / (v2 * v2)) * v2;
00122 }
00123 
00125 template <class Vec>
00126 inline
00127 Vec projectPerp(const Vec &v1, const Vec &v2)
00128 {
00129    return v1 - projectParallel(v1, v2);
00130 }
00131 
00132 }
00133 #endif

Generated on Tue Jan 29 21:37:56 2008 for VoluMill Universal Client by  doxygen 1.4.6