source: projectionDesigner/trunk/projdesigner/include/gmtl/Plane.h @ 137

Last change on this file since 137 was 4, checked in by Torben Dannhauer, 15 years ago
File size: 6.1 KB
Line 
1/************************************************************** ggt-head beg
2 *
3 * GGT: Generic Graphics Toolkit
4 *
5 * Original Authors:
6 *   Allen Bierbaum
7 *
8 * -----------------------------------------------------------------
9 * File:          Plane.h,v
10 * Date modified: 2003/04/18 22:55:21
11 * Version:       1.14
12 * -----------------------------------------------------------------
13 *
14 *********************************************************** ggt-head end */
15/*************************************************************** ggt-cpr beg
16*
17* GGT: The Generic Graphics Toolkit
18* Copyright (C) 2001,2002 Allen Bierbaum
19*
20* This library is free software; you can redistribute it and/or
21* modify it under the terms of the GNU Lesser General Public
22* License as published by the Free Software Foundation; either
23* version 2.1 of the License, or (at your option) any later version.
24*
25* This library is distributed in the hope that it will be useful,
26* but WITHOUT ANY WARRANTY; without even the implied warranty of
27* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28* Lesser General Public License for more details.
29*
30* You should have received a copy of the GNU Lesser General Public
31* License along with this library; if not, write to the Free Software
32* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33*
34 ************************************************************ ggt-cpr end */
35#ifndef _GMTL_PLANE_H
36#define _GMTL_PLANE_H
37
38#include <gmtl/Vec.h>
39#include <gmtl/Point.h>
40#include <gmtl/VecOps.h>
41
42namespace gmtl
43{
44
45/**
46 *  Plane: Defines a geometrical plane.
47 *
48 *  All points on the plane satify the equation dot(Pt,Normal) = offset
49 *  normal is assumed to be normalized
50 *
51 *  NOTE: Some plane implementation store D instead of offset.  Thus
52 *       those implementation have opposite sign from what we have
53 *
54 * pg. 309 Computer Graphics 2nd Edition Hearn Baker
55 *<pre>
56 *  N dot P = -D
57 *   |
58 *   |-d-|
59 * __|___|-->N
60 *   |   |
61 *</pre>
62 * @ingroup Types
63 */
64template< class DATA_TYPE>
65class Plane
66{
67public:
68   /**
69    * Creates an uninitialized Plane. In other words, the normal is (0,0,0) and
70    * the offset is 0.
71    */
72   Plane()
73      : mOffset( 0 )
74   {}
75
76   /**
77    * Creates a plane that the given points lie on.
78    *
79    * @param pt1     a point on the plane
80    * @param pt2     a point on the plane
81    * @param pt3     a point on the plane
82    */
83   Plane( const Point<DATA_TYPE, 3>& pt1, const Point<DATA_TYPE, 3>& pt2,
84          const Point<DATA_TYPE, 3>& pt3)
85   {
86      Vec<DATA_TYPE, 3> vec12( pt2-pt1 );
87      Vec<DATA_TYPE, 3> vec13( pt3-pt1 );
88
89      cross( mNorm, vec12, vec13 );
90      normalize( mNorm );
91
92      mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt1), mNorm );  // Graphics Gems I: Page 390
93   }
94
95   /**
96    * Creates a plane with the given normal on which pt resides.
97    *
98    * @param norm    the normal of the plane
99    * @param pt      a point that lies on the plane
100    */
101   Plane( const Vec<DATA_TYPE, 3>& norm, const Point<DATA_TYPE, 3>& pt )
102      : mNorm( norm )
103   {
104      mOffset = dot( static_cast< Vec<DATA_TYPE, 3> >(pt), norm );
105   }
106
107   /**
108    * Creates a plane with the given normal and offset.
109    *
110    * @param norm          the normal of the plane
111    * @param dPlaneConst   the plane offset constant
112    */
113   Plane( const Vec<DATA_TYPE, 3>& norm, const DATA_TYPE& dPlaneConst )
114      : mNorm( norm ), mOffset( dPlaneConst )
115   {}
116
117   /**
118    * Creates an exact duplicate of the given plane.
119    *
120    * @param plane   the plane to copy
121    */
122   Plane( const Plane<DATA_TYPE>& plane )
123      : mNorm( plane.mNorm ), mOffset( plane.mOffset )
124   {}
125
126   /**
127    * Gets the normal for this plane.
128    *
129    * @return  this plane's normal vector
130    */
131   const Vec<DATA_TYPE, 3>& getNormal() const
132   {
133      return mNorm;
134   }
135
136   /**
137    * Sets the normal for this plane to the given vector.
138    *
139    * @param norm    the new normalized vector
140    *
141    * @pre |norm| = 1
142    */
143   void setNormal( const Vec<DATA_TYPE, 3>& norm )
144   {
145      mNorm = norm;
146   }
147
148   /**
149    * Gets the offset of this plane from the origin such that the offset is the
150    * negative distance from the origin.
151    *
152    * @return  this plane's offset
153    */
154   const DATA_TYPE& getOffset() const
155   {
156      return mOffset;
157   }
158
159   /**
160    * Sets the offset of this plane from the origin.
161    *
162    * @param offset     the new offset
163    */
164   void setOffset( const DATA_TYPE& offset )
165   {
166      mOffset = offset;
167   }
168
169public:
170   // dot(Pt,mNorm) = mOffset
171   /**
172    * The normal for this vector. For any point on the plane,
173    * dot( pt, mNorm) = mOffset.
174    */
175   Vec<DATA_TYPE, 3> mNorm;
176
177   /**
178    * This plane's offset from the origin such that for any point pt,
179    * dot( pt, mNorm ) = mOffset. Note that mOffset = -D (neg dist from the
180    * origin).
181    */
182   DATA_TYPE mOffset;
183};
184
185typedef Plane<float> Planef;
186typedef Plane<double> Planed;
187
188/*
189#include <geomdist.h>
190
191
192// Intersects the plane with a given segment.
193// Returns TRUE if there is a hit (within the seg).
194// Also returns the distance "down" the segment of the hit in t.
195//
196// PRE: seg.dir must be normalized
197//
198int sgPlane::isect(sgSeg& seg, float* t)
199{
200        // Graphic Gems I: Page 391
201        float denom = normal.dot(seg.dir);
202        if (SG_IS_ZERO(denom))          // No Hit
203        {
204                //: So now, it is just dist to plane tested against length
205                sgVec3  hit_pt;
206                float           hit_dist;               // Dist to hit
207               
208                hit_dist = findNearestPt(seg.pos, hit_pt);
209                *t = hit_dist;                  // Since dir is normalized
210
211                if(seg.tValueOnSeg(*t))
212                        return 1;
213                else
214                        return 0;
215        }
216        else
217        {
218                float numer = offset + normal.dot(seg.pos);
219                (*t) = -1.0f * (numer/denom);
220               
221                if(seg.tValueOnSeg(*t))
222                        return 1;
223                else
224                        return 0;
225        }
226}
227
228///
229 // Intersects the plane with the line defined
230 // by the given segment
231 //
232 // seg - seg that represents the line to isect
233 // t   - the t value of the isect
234 //
235int sgPlane::isectLine(const sgSeg& seg, float* t)
236{
237        // GGI: Pg 299
238        // Lu = seg.pos;
239        // Lv = seg.dir;
240        // Jn = normal;
241        // Jd = offset;
242       
243        float denom = normal.dot(seg.dir);
244        if(denom == 0.0f)
245      return 0;
246        else
247        {
248      *t = - (offset+ normal.dot(seg.pos))/(denom);
249      return 1;
250        }
251}
252*/
253
254} // namespace gmtl
255#endif
Note: See TracBrowser for help on using the repository browser.