source: projectionDesigner/trunk/projdesigner/include/gmtl/PlaneOps.h @ 415

Last change on this file since 415 was 4, checked in by Torben Dannhauer, 15 years ago
File size: 6.2 KB
RevLine 
[4]1/************************************************************** ggt-head beg
2 *
3 * GGT: Generic Graphics Toolkit
4 *
5 * Original Authors:
6 *   Allen Bierbaum
7 *
8 * -----------------------------------------------------------------
9 * File:          PlaneOps.h,v
10 * Date modified: 2005/06/04 18:40:46
11 * Version:       1.13
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_PLANEOPS_H_
36#define _GMTL_PLANEOPS_H_
37
38#include <gmtl/Defines.h>
39#include <gmtl/Plane.h>
40#include <gmtl/Math.h>
41
42namespace gmtl
43{
44
45/** @ingroup Ops Plane
46 * @name Plane Operations
47 * @{
48 */
49       
50/**
51 * Computes the distance from the plane to the point.
52 *
53 * @param plane      the plane to compare the point to it
54 * @param pt         a point in space
55 *
56 * @return  the distance from the point to the plane
57 */
58template< class DATA_TYPE >
59DATA_TYPE distance( const Plane<DATA_TYPE>& plane, const Point<DATA_TYPE, 3>& pt )
60{
61   return ( dot(plane.mNorm, static_cast< Vec<DATA_TYPE, 3> >(pt)) - plane.mOffset );
62}
63
64/**
65 * Determines which side of the plane the given point lies. This operation is
66 * done with ZERO tolerance.
67 *
68 * @param plane      the plane to compare the point to
69 * @param pt         the point to test
70 *
71 * @return  the PlaneSide enum describing on which side of the plane the point
72 *          lies
73 */
74template< class DATA_TYPE >
75PlaneSide whichSide( const Plane<DATA_TYPE>& plane,
76                     const Point<DATA_TYPE, 3>& pt )
77{
78   DATA_TYPE dist = distance( plane, pt );
79
80   if ( dist < DATA_TYPE(0) )
81      return NEG_SIDE;
82   else if ( dist > DATA_TYPE(0) )
83      return POS_SIDE;
84   else
85      return ON_PLANE;
86}
87
88/**
89 * Determines which side of the plane the given point lies with the given
90 * epsilon tolerance.
91 *
92 * @param plane      the plane to compare the point to
93 * @param pt         the point to test
94 * @param eps        the epsilon tolerance to use while testing
95 *
96 * @return  the PlaneSide enum describing on which side of the plane the point
97 *          lies
98 */
99template< class DATA_TYPE >
100PlaneSide whichSide( const Plane<DATA_TYPE>& plane,
101                     const Point<DATA_TYPE, 3>& pt,
102                     const DATA_TYPE& eps )
103{
104   DATA_TYPE dist = distance( plane, pt );
105
106   if ( dist < eps )
107      return NEG_SIDE;
108   else if ( dist > eps )
109      return POS_SIDE;
110   else
111      return ON_PLANE;
112}
113
114/**
115 * Finds the point on the plane that is nearest to the given point. As a
116 * convenience, the distance between pt and result is returned.
117 *
118 * @param plane  [in]   the plane to compare the point to
119 * @param pt     [in]   the point to test
120 * @param result [out]  the point on plane closest to pt
121 *
122 * @return  the distance between pt and result
123 */
124template< class DATA_TYPE >
125DATA_TYPE findNearestPt( const Plane<DATA_TYPE>& plane,
126                         const Point<DATA_TYPE, 3>& pt,
127                         Point<DATA_TYPE, 3>& result )
128{
129   // GGI:  p297
130   // GGII: p223
131   gmtlASSERT( isNormalized(plane.mNorm) );   // Assert: Normalized
132   DATA_TYPE dist_to_plane(0);
133   dist_to_plane = plane.mOffset + dot( plane.mNorm, static_cast< Vec<DATA_TYPE, 3> >(pt) );
134   result = pt - (plane.mNorm * dist_to_plane);
135   return dist_to_plane;
136}
137
138/**
139 * Mirror the point by the plane
140 */
141template< class DATA_TYPE, unsigned SIZE>
142void reflect( Point<DATA_TYPE, SIZE>& result,
143              const Plane<DATA_TYPE>& plane,
144              const Point<DATA_TYPE, SIZE>& point )
145{
146   gmtl::Point<DATA_TYPE, SIZE> point_on_plane;
147   findNearestPt( plane, point, point_on_plane );
148   gmtl::Vec<DATA_TYPE, SIZE> dir = point_on_plane - point;
149   result = point + (dir * DATA_TYPE(2.0f));
150}
151/** @} */
152
153/** @ingroup Compare Plane
154 * @name Plane Comparitors
155 * @{
156 */
157
158/**
159 * Compare two planes to see if they are EXACTLY the same. In other words, this
160 * comparison is done with zero tolerance.
161 *
162 * @param p1      the first plane to compare
163 * @param p2      the second plane to compare
164 *
165 * @return  true if they are equal, false otherwise
166 */
167template< class DATA_TYPE >
168inline bool operator==( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2 )
169{
170   return ( (p1.mNorm == p2.mNorm) && (p1.mOffset == p2.mOffset) );
171}
172
173/**
174 * Compare two planes to see if they are not EXACTLY the same. In other words,
175 * this comparison is done with zero tolerance.
176 *
177 * @param p1      the first plane to compare
178 * @param p2      the second plane to compare
179 *
180 * @return  true if they are not equal, false otherwise
181 */
182template< class DATA_TYPE >
183inline bool operator!=( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2 )
184{
185   return (! (p1 == p2));
186}
187
188/**
189 * Compare two planes to see if they are the same within the given tolerance.
190 *
191 * @param p1      the first plane to compare
192 * @param p2      the second plane to compare
193 * @param eps     the tolerance value to use
194 *
195 * @pre eps must be >= 0
196 *
197 * @return  true if they are equal within a tolerance, false otherwise
198 */
199template< class DATA_TYPE >
200inline bool isEqual( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2,
201                     const DATA_TYPE& eps )
202{
203   gmtlASSERT( eps >= 0 );
204   return ( (isEqual(p1.mNorm, p2.mNorm, eps)) &&
205            (Math::isEqual(p1.mOffset, p2.mOffset, eps)) );
206}
207/** @} */
208
209
210} // namespace gmtl
211
212#endif
213
Note: See TracBrowser for help on using the repository browser.