RayTriangleIntersection.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #include "RayTriangleIntersection.h"
00026 #include <sofa/helper/LCPSolver.inl>
00027 
00028 namespace sofa
00029 {
00030 
00031 namespace component
00032 {
00033 
00034 namespace collision
00035 {
00036 
00037 RayTriangleIntersection::RayTriangleIntersection(){
00038 }
00039 RayTriangleIntersection::~RayTriangleIntersection(){
00040 }
00041 bool
00042 RayTriangleIntersection::NewComputation(Triangle *triP, const Vector3 &origin, const Vector3 &direction,   double &t,  double &u, double &v)
00043 
00044         {
00045             t = 0; u = 0; v = 0;
00046 
00047             Vector3 edge1 = triP->p2() - triP->p1();
00048             Vector3 edge2 = triP->p3() - triP->p1();
00049 
00050             Vector3 tvec, pvec, qvec;
00051             double det, inv_det;
00052 
00053             pvec = direction.cross(edge2);
00054 
00055             det = dot(edge1, pvec);
00056     if(det==0.0){
00057         
00058         return false;
00059         
00060 }
00061 //      if (fabs(triP->n()*direction) < 0.0000)
00062 //                 return false;
00063 
00064             inv_det = 1.0 / det;
00065 
00066             tvec = origin - triP->p1();
00067 
00068             u = dot(tvec, pvec) * inv_det;
00069             if (u < 0.000001||u >0.999999900000)
00070                 return false;
00071 
00072             qvec = tvec.cross(edge1);
00073 
00074             v = dot(direction, qvec) * inv_det;
00075             if (v < 0.00001|| (u +v) > 0.999999900000)
00076                 return false;
00077 
00078             t = dot(edge2, qvec) * inv_det;
00079 
00080             if (t <=0.000001||t!=t||v!=v||u!=u)
00081                 return false;   
00082         
00083             return true;
00084         }
00085 
00086 }
00087 }
00088 }
00089     

Generated on Sun Nov 23 02:35:10 2008 for Sofa by  doxygen 1.5.7.1