root/juggler/branches/1.0/Math/vjSeg.cpp

Revision 8789, 3.8 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 #include <vjConfig.h>
36 #include <Math/vjSeg.h>
37 #include <Math/vjPlane.h>
38
39
40 //: Finds the point on the seg nearest to pt.
41 // Returns the nearest point in nearPt
42 //
43 // Makes assumptions that all pt dir is normalized
44 void vjSeg::findNearestPt(const vjVec3& pt, vjVec3& nearPt)
45 {
46    // GGI Pg. 300
47    // Find plane through point pt normal to line
48    // Isect the line with this plane
49    vjASSERT(dir.isNormalized());
50    vjPlane J;
51    J.normal = dir;         // Plane normal
52    J.offset = -(pt.dot(dir));
53    float tDist;         // tDist along seg
54    J.isectLine(*this, &tDist);
55
56    // Find pt on the segment
57    if (tDist < 0)               // Behind pos
58       nearPt = pos;
59    else if (tDist > length)     // Past end. NOTE: Assumes that dir is normalized
60       nearPt = pos + (dir*length);
61    else                        // It is on the line
62       nearPt = pos + (dir*tDist);
63 }
64
65 bool vjSeg::isectTriangle(const vjVec3 _v1, const vjVec3 _v2, const vjVec3 _v3, float* t)
66 {
67    // Graphic Gems I: Page 392
68    int i0, i1, i2;      // Axis indices
69    float u0, u1, u2, v0, v1, v2, alpha, beta;
70    i0 = 0;
71    vjPlane triPlane;   // The plane the triangle is in
72    float   tDist;      // Dist hit is along the seg
73    vjVec3  hitPt;      // Point hit on the plane
74
75    triPlane.makePts(_v1, _v2, _v3);
76    if (triPlane.isect(*this, &tDist))    // Does the seg hit the plane?
77    {
78       hitPt = pos + (dir * tDist);    // Find point hit
79
80       // Find max index
81       // Note: I added the fabs because of bug in code.  NOT IN GGI
82       for (int index=0;index<3;index++)
83          if (fabs(triPlane.normal.vec[index]) > fabs(triPlane.normal.vec[i0]))
84             i0 = index;
85
86       if (i0 == 0)
87       {
88          i1 = 1; i2 = 2;
89       }
90       else if (i0 == 1)
91       {
92          i1 = 2; i2 = 0;
93       }
94       else
95       {
96          i1 =0; i2 = 1;
97       }
98
99       u0 = hitPt[i1] - _v1[i1];
100       v0 = hitPt[i2] - _v1[i2];
101       u1 = _v2[i1] - _v1[i1];
102       u2 = _v3[i1] - _v1[i1];
103       v1 = _v2[i2] - _v1[i2];
104       v2 = _v3[i2] - _v1[i2];
105       if (u1 == 0)
106       {
107          beta = (u0/u2);
108          if ((beta >= 0) && (beta<= 1))
109             alpha = (v0 - (beta*v2))/v1;
110       }
111       else
112       {
113          beta = ((v0*u1) - (u0*v1))/((v2*u1) - (u2*v1));
114          if ((beta >= 0) && (beta<= 1))
115             alpha = (u0 - (beta*u2))/u1;
116       }
117
118       if ((alpha >= 0) && (beta >= 0) && ((alpha+beta) <= 1))      // If it is in the tri
119       {
120          *t = tDist;
121          return true;
122       }
123       else
124       {
125          return false;
126       }
127
128    }
129    else
130       return false;
131 }
Note: See TracBrowser for help on using the browser.