ContinuousTriangleIntersection.h

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 #ifndef SOFA_COMPONENT_COLLISION_CONTINUOUSTRIANGLEINTERSECTION_H
00026 #define SOFA_COMPONENT_COLLISION_CONTINUOUSTRIANGLEINTERSECTION_H
00027 #include <sofa/component/collision/Triangle.h>
00028 #include <sofa/core/componentmodel/collision/DetectionOutput.h>
00029 #include <math.h>
00030 
00031 #define cbrt(x)     ((x) > (SReal)0.0 ? pow((SReal)(x), (SReal)1.0/(SReal)3.0) : \
00032                     ((x) < (SReal)0.0 ? -pow((SReal)-(x), (SReal)1.0/(SReal)3.0) : (SReal)0.0))
00033 #define EPSILON 0.000001
00034 
00035 #define EQN_EPS     1e-9
00036 #define IsZero(x)   ((x) > -EQN_EPS && (x) < EQN_EPS)
00037 
00038 namespace sofa
00039 {
00040 
00041 namespace component
00042 {
00043 
00044 namespace collision
00045 {
00046 
00047 using namespace collision;
00048 
00049 class ContinuousTriangleIntersection
00050 {
00051 private:
00052     Triangle &tr1, &tr2;
00053     SReal m_tolerance, m_tolmin, m_tolmax;
00054 
00055     int intersectPointTriangle (SReal& t, SReal& u, SReal& v,
00056                                 const Vector3& p0, const Vector3& v0,
00057                                 const Vector3& p1, const Vector3& v1,
00058                                 const Vector3& p2, const Vector3& v2,
00059                                 const Vector3& p3, const Vector3& v3,
00060                                 double dt);
00061 
00062     int intersectEdgeEdge(SReal& t, SReal& u, SReal& v,
00063                            const Vector3& p1, const Vector3& p2,
00064                            const Vector3& v1, const Vector3& v2,
00065                            const Vector3& p3, const Vector3& p4,
00066                            const Vector3& v3, const Vector3& v4,
00067                            double dt);
00068 
00069     int solveCubic(Vector3& s, SReal c[4]);
00070     
00071     int solveQuadratic (SReal& t1, SReal& t2, 
00072             const SReal& a, 
00073             const SReal& b, 
00074             const SReal& c);
00075     
00076     bool checkValidRoots (SReal& validRoot,
00077                           const int& numRoots,
00078                           const Vector3& roots,
00079                           const SReal& rootMin,
00080                           const SReal& rootMax);
00081 
00084     bool checkRange (const SReal& val,
00085                      const SReal& min,
00086                      const SReal& max)
00087     {
00088         return ((val >= min) && (val <= max));
00089     };
00090 
00093     inline SReal getMinVal (const SReal& val0, const SReal& val1)
00094     {
00095         return (val0 < val1) ? val0 : val1;
00096     };
00097 
00100    SReal getMinVal(const SReal &val0,
00101                             const SReal &val1,
00102                             const SReal &val2)
00103     {
00104         if (val0 < val1)
00105         {
00106             return (val0 < val2) ? val0 : val2;
00107         }
00108         else 
00109         {
00110             return (val1 < val2) ? val1 : val2;
00111         }
00112     };
00113 
00116     void setTolerance(const SReal &tol)
00117     {
00118         m_tolerance = tol;
00119         m_tolmin = -m_tolerance;
00120         m_tolmax = (SReal)1.0 + m_tolerance;
00121     };
00122 
00123 public:
00124     ContinuousTriangleIntersection (Triangle& t1, Triangle& t2);
00125     ~ContinuousTriangleIntersection(void);
00126     core::componentmodel::collision::DetectionOutput* computeDetectionOutput (void);
00127     bool isCollision(void);
00128 };
00129 
00130 } // namespace collision
00131 
00132 } // namespace component
00133 
00134 } // namespace sofa
00135 
00136 
00137 #endif /* _CONTINUOUSTRIANGLEINTERSECTION_H_ */

Generated on Sun Nov 23 02:33:11 2008 for Sofa by  doxygen 1.5.7.1