root/juggler/tags/1.0.7/Math/vjPlane.h
| Revision 8789, 4.6 kB (checked in by patrickh, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /*************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | * |
| 3 | * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002 |
| 4 | * by Iowa State University |
| 5 | * |
| 6 | * Original Authors: |
| 7 | * Allen Bierbaum, Christopher Just, |
| 8 | * Patrick Hartling, Kevin Meinert, |
| 9 | * Carolina Cruz-Neira, Albert Baker |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Library General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Library General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Library General Public |
| 22 | * License along with this library; if not, write to the |
| 23 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 | * Boston, MA 02111-1307, USA. |
| 25 | * |
| 26 | * ----------------------------------------------------------------- |
| 27 | * File: $RCSfile$ |
| 28 | * Date modified: $Date$ |
| 29 | * Version: $Revision$ |
| 30 | * ----------------------------------------------------------------- |
| 31 | * |
| 32 | *************** <auto-copyright.pl END do not edit this line> ***************/ |
| 33 | |
| 34 | |
| 35 | #ifndef _VJ_PLANE_H_ |
| 36 | #define _VJ_PLANE_H_ |
| 37 | //#pragma once |
| 38 | |
| 39 | #include <vjConfig.h> |
| 40 | #include <Kernel/vjDebug.h> |
| 41 | #include <Math/vjVec3.h> |
| 42 | #include <Math/vjSeg.h> |
| 43 | |
| 44 | |
| 45 | //: sgPlane: Defines a geometrical plane. |
| 46 | // |
| 47 | // All points on the plane satify the equation dot(Pt,Normal) = offset |
| 48 | // normal is assumed to be normalized |
| 49 | // |
| 50 | // pg. 309 Computer Graphics 2nd Edition Hearn Baker |
| 51 | // N dot P = -D |
| 52 | // |
| 53 | class vjPlane |
| 54 | { |
| 55 | public: |
| 56 | vjPlane() |
| 57 | {} |
| 58 | |
| 59 | //: Create a plane containing the given points. |
| 60 | void makePts(const vjVec3& pt1, const vjVec3& pt2, const vjVec3& pt3); |
| 61 | |
| 62 | //: Create a plane given a normal and a point |
| 63 | void makeNormPt(const vjVec3& _pt, const vjVec3& norm); |
| 64 | |
| 65 | |
| 66 | //: Intersects the plane with a given segment. |
| 67 | //! RETURNS: TRUE if there is a hit (within the seg). |
| 68 | //+ *t = distance "down" the segment to the hit. |
| 69 | // |
| 70 | //! PRE: seg.dir must be normalized |
| 71 | bool isect(vjSeg& seg, float* t); |
| 72 | |
| 73 | |
| 74 | //: Intersects the plane with the line defined |
| 75 | // by the given segment |
| 76 | // |
| 77 | // seg - seg that represents the line to isect |
| 78 | // t - the t value of the isect |
| 79 | bool isectLine(const vjSeg& seg, float* t); |
| 80 | |
| 81 | //: Find nearest pt on the plane |
| 82 | //! RETURN: distance to the point (d) |
| 83 | //+ If d is positive, pt lies on same side as normal |
| 84 | //+ If d is negative, pt lies on opposite side from normal |
| 85 | //+ if d is "near" zero, pt is on the plane |
| 86 | float findNearestPt(const vjVec3& pt, vjVec3& nearPt); |
| 87 | |
| 88 | public: |
| 89 | vjVec3 normal; // N or Just Normal |
| 90 | float offset; // D or offset |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | /****************************************************** |
| 95 | INLINES |
| 96 | ******************************************************/ |
| 97 | |
| 98 | /// Create a plane containing the given points. |
| 99 | inline |
| 100 | void vjPlane::makePts(const vjVec3& pt1, const vjVec3& pt2, const vjVec3& pt3) |
| 101 | { |
| 102 | vjVec3 vec12(pt2-pt1); |
| 103 | vjVec3 vec13(pt3-pt1); |
| 104 | |
| 105 | normal = vec12.cross(vec13); |
| 106 | |
| 107 | normal.normalize(); |
| 108 | offset = -1.0f * (pt1.dot(normal)); // Graphics Gems I: Page 390 |
| 109 | } |
| 110 | |
| 111 | inline |
| 112 | void vjPlane::makeNormPt(const vjVec3& norm, const vjVec3& _pt) |
| 113 | { |
| 114 | normal = norm; |
| 115 | offset = -(_pt.dot(normal)); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | // Finds the point on the plane nearest to pt. |
| 121 | // Returns the nearest point in nearPt |
| 122 | inline |
| 123 | float vjPlane::findNearestPt(const vjVec3& pt, vjVec3& nearPt) |
| 124 | { |
| 125 | // GGI: P 297 |
| 126 | // GGII: Pg 223 |
| 127 | vjASSERT(normal.isNormalized()); // Assert: Normalized |
| 128 | float dist_to_plane(0.0); |
| 129 | dist_to_plane = offset+normal.dot(pt); // Distance to plane |
| 130 | nearPt = pt - (normal*dist_to_plane); |
| 131 | return dist_to_plane; |
| 132 | } |
| 133 | |
| 134 | // Intersects the plane with the line defined |
| 135 | // by the given segment |
| 136 | // |
| 137 | // seg - seg that represents the line to isect |
| 138 | // t - the t value of the isect |
| 139 | inline |
| 140 | bool vjPlane::isectLine(const vjSeg& seg, float* t) |
| 141 | { |
| 142 | // GGI: Pg 299 |
| 143 | // Lu = seg.pos; |
| 144 | // Lv = seg.dir; |
| 145 | // Jn = normal; |
| 146 | // Jd = offset; |
| 147 | |
| 148 | float denom = normal.dot(seg.dir); |
| 149 | if(VJ_IS_ZERO(denom)) |
| 150 | return false; |
| 151 | else |
| 152 | { |
| 153 | *t = - (offset+ normal.dot(seg.pos))/(denom); |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Intersects the plane with a given segment. |
| 159 | // Returns TRUE if there is a hit (within the seg). |
| 160 | // Also returns the distance "down" the segment of the hit in t. |
| 161 | // |
| 162 | // PRE: seg.dir must be normalized |
| 163 | inline |
| 164 | bool vjPlane::isect(vjSeg& seg, float* t) |
| 165 | { |
| 166 | if(!isectLine(seg, t)) |
| 167 | return false; // Plane and seg are parallel |
| 168 | |
| 169 | if(seg.tValueOnSeg(*t)) |
| 170 | return true; |
| 171 | else |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | #endif |
Note: See TracBrowser for help on using the browser.
