00001 /************************************************************** ggt-head beg 00002 * 00003 * GGT: Generic Graphics Toolkit 00004 * 00005 * Original Authors: 00006 * Allen Bierbaum 00007 * 00008 * ----------------------------------------------------------------- 00009 * File: $RCSfile: Plane.h,v $ 00010 * Date modified: $Date: 2003/03/03 00:54:05 $ 00011 * Version: $Revision: 1.13 $ 00012 * ----------------------------------------------------------------- 00013 * 00014 *********************************************************** ggt-head end */ 00015 /*************************************************************** ggt-cpr beg 00016 * 00017 * GGT: The Generic Graphics Toolkit 00018 * Copyright (C) 2001,2002 Allen Bierbaum 00019 * 00020 * This library is free software; you can redistribute it and/or 00021 * modify it under the terms of the GNU Lesser General Public 00022 * License as published by the Free Software Foundation; either 00023 * version 2.1 of the License, or (at your option) any later version. 00024 * 00025 * This library is distributed in the hope that it will be useful, 00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00028 * Lesser General Public License for more details. 00029 * 00030 * You should have received a copy of the GNU Lesser General Public 00031 * License along with this library; if not, write to the Free Software 00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00033 * 00034 ************************************************************ ggt-cpr end */ 00035 #ifndef _GMTL_PLANE_H 00036 #define _GMTL_PLANE_H 00037 00038 #include <gmtl/Vec.h> 00039 #include <gmtl/Point.h> 00040 #include <gmtl/VecOps.h> 00041 00042 namespace gmtl 00043 { 00044 00064 template< class DATA_TYPE> 00065 class Plane 00066 { 00067 public: 00072 Plane() 00073 : mOffset( 0 ) 00074 {} 00075 00083 Plane( const Point<DATA_TYPE, 3>& pt1, const Point<DATA_TYPE, 3>& pt2, 00084 const Point<DATA_TYPE, 3>& pt3) 00085 { 00086 Vec<DATA_TYPE, 3> vec12( pt2-pt1 ); 00087 Vec<DATA_TYPE, 3> vec13( pt3-pt1 ); 00088 00089 mNorm = cross( vec12, vec13 ); 00090 normalize( mNorm ); 00091 00092 mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt1), mNorm ); // Graphics Gems I: Page 390 00093 } 00094 00101 Plane( const Vec<DATA_TYPE, 3>& norm, const Point<DATA_TYPE, 3>& pt ) 00102 : mNorm( norm ) 00103 { 00104 mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt), norm ); 00105 } 00106 00113 Plane( const Vec<DATA_TYPE, 3>& norm, const DATA_TYPE& dPlaneConst ) 00114 : mNorm( norm ), mOffset( dPlaneConst ) 00115 {} 00116 00122 Plane( const Plane<DATA_TYPE>& plane ) 00123 : mNorm( plane.mNorm ), mOffset( plane.mOffset ) 00124 {} 00125 00131 const Vec<DATA_TYPE, 3>& getNormal() const 00132 { 00133 return mNorm; 00134 } 00135 00143 void setNormal( const Vec<DATA_TYPE, 3>& norm ) 00144 { 00145 mNorm = norm; 00146 } 00147 00154 const DATA_TYPE& getOffset() const 00155 { 00156 return mOffset; 00157 } 00158 00164 void setOffset( const DATA_TYPE& offset ) 00165 { 00166 mOffset = offset; 00167 } 00168 00169 public: 00170 // dot(Pt,mNorm) = mOffset 00175 Vec<DATA_TYPE, 3> mNorm; 00176 00182 DATA_TYPE mOffset; 00183 }; 00184 00185 typedef Plane<float> Planef; 00186 typedef Plane<double> Planed; 00187 00188 /* 00189 #include <geomdist.h> 00190 00191 00192 // Intersects the plane with a given segment. 00193 // Returns TRUE if there is a hit (within the seg). 00194 // Also returns the distance "down" the segment of the hit in t. 00195 // 00196 // PRE: seg.dir must be normalized 00197 // 00198 int sgPlane::isect(sgSeg& seg, float* t) 00199 { 00200 // Graphic Gems I: Page 391 00201 float denom = normal.dot(seg.dir); 00202 if (SG_IS_ZERO(denom)) // No Hit 00203 { 00204 //: So now, it is just dist to plane tested against length 00205 sgVec3 hit_pt; 00206 float hit_dist; // Dist to hit 00207 00208 hit_dist = findNearestPt(seg.pos, hit_pt); 00209 *t = hit_dist; // Since dir is normalized 00210 00211 if(seg.tValueOnSeg(*t)) 00212 return 1; 00213 else 00214 return 0; 00215 } 00216 else 00217 { 00218 float numer = offset + normal.dot(seg.pos); 00219 (*t) = -1.0f * (numer/denom); 00220 00221 if(seg.tValueOnSeg(*t)) 00222 return 1; 00223 else 00224 return 0; 00225 } 00226 } 00227 00229 // Intersects the plane with the line defined 00230 // by the given segment 00231 // 00232 // seg - seg that represents the line to isect 00233 // t - the t value of the isect 00234 // 00235 int sgPlane::isectLine(const sgSeg& seg, float* t) 00236 { 00237 // GGI: Pg 299 00238 // Lu = seg.pos; 00239 // Lv = seg.dir; 00240 // Jn = normal; 00241 // Jd = offset; 00242 00243 float denom = normal.dot(seg.dir); 00244 if(denom == 0.0f) 00245 return 0; 00246 else 00247 { 00248 *t = - (offset+ normal.dot(seg.pos))/(denom); 00249 return 1; 00250 } 00251 } 00252 */ 00253 00254 } // namespace gmtl 00255 #endif