Geometry.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include "dada.h"
00018 #include "Geometry.h"
00019 
00020 using namespace Fluxus;
00021 
00022 float Fluxus::PointLineDist(const dVector &p, const dVector &start, const dVector &end)
00023 {
00024     float linemag;
00025     dVector intersection;
00026  
00027     float len = end.dist(start);
00028  
00029     float t = ((p.x-start.x)*(end.x-start.x) +
00030                (p.y-start.y)*(end.y-start.y) +
00031                (p.z-start.z)*(end.z-start.z)) / (len*len);
00032  
00033     if (t<0.0f) // off the end
00034     {
00035         return p.dist(start);
00036     }
00037     if (t>1.0f) // off the end
00038     {
00039         return p.dist(end);
00040     }
00041     
00042     intersection.x = start.x+t*(end.x-start.x);
00043     intersection.y = start.y+t*(end.y-start.y);
00044     intersection.z = start.z+t*(end.z-start.z);
00045  
00046     return p.dist(intersection);
00047 }
00048 
00049 bool Fluxus::IntersectLineTriangle(const dVector &start, const dVector &end, 
00050     const dVector &ta, const dVector &tb, const dVector &tc, 
00051     dVector &bary)
00052 {
00053     dVector u = ta-tc;
00054     dVector v = tb-tc;
00055     dVector n = v.cross(u);
00056 
00057     if (n.mag()==0) return false;
00058     
00059     dVector ray = end-start;
00060     dVector w0 = start-tc;
00061     float a = -n.dot(w0);
00062     float b = n.dot(ray);
00063 
00064     // if b is small, ray is parallel
00065     if (b==0) return false;
00066     //     if a==0 then the ray is in the plane
00067 
00068     float r = a/b;
00069     if (r<0) return false;
00070     if (r>1) return false;
00071     dVector I = start+(ray*r);
00072     float uu = u.dot(u);
00073     float uv = u.dot(v);
00074     float vv = v.dot(v);
00075     dVector w = I-tc;
00076     float wu = w.dot(u);
00077     float wv = w.dot(v);
00078     float D = uv*uv - uu*vv;
00079 
00080     bary.x=(uv*wv - vv*wu)/D;
00081     if (bary.x<0 || bary.x>1.0) return false;
00082     bary.y=(uv*wu - uu*wv)/D;
00083     if (bary.y<0 || bary.y>1.0) return false;
00084     bary.z=1-(bary.x+bary.y);
00085     if (bary.z<0 || bary.z>1.0) return false;
00086     return true;
00087 }
00088 

Generated on Wed Sep 17 21:16:30 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1