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