Simbody
3.4 (development)
|
00001 #ifndef SimTK_SIMMATH_GEO_TRIANGLE_H_ 00002 #define SimTK_SIMMATH_GEO_TRIANGLE_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * Simbody(tm): SimTKmath * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. * 00011 * * 00012 * Portions copyright (c) 2011-12 Stanford University and the Authors. * 00013 * Authors: Michael Sherman * 00014 * Contributors: * 00015 * * 00016 * Licensed under the Apache License, Version 2.0 (the "License"); you may * 00017 * not use this file except in compliance with the License. You may obtain a * 00018 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * 00019 * * 00020 * Unless required by applicable law or agreed to in writing, software * 00021 * distributed under the License is distributed on an "AS IS" BASIS, * 00022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00023 * See the License for the specific language governing permissions and * 00024 * limitations under the License. * 00025 * -------------------------------------------------------------------------- */ 00026 00030 #include "SimTKcommon.h" 00031 #include "simmath/internal/common.h" 00032 #include "simmath/internal/Geo.h" 00033 #include "simmath/internal/Geo_Point.h" 00034 #include "simmath/internal/Geo_LineSeg.h" 00035 00036 #include <cassert> 00037 #include <cmath> 00038 #include <algorithm> 00039 00040 namespace SimTK { 00041 00042 //============================================================================== 00043 // GEO TRIANGLE 00044 //============================================================================== 00048 template <class P> 00049 class Geo::Triangle_ { 00050 typedef P RealP; 00051 typedef Vec<2,P> Vec2P; 00052 typedef Vec<3,P> Vec3P; 00053 typedef UnitVec<P,1> UnitVec3P; 00054 public: 00057 Triangle_() {} 00059 Triangle_(const Vec3P& v0, const Vec3P& v1, const Vec3P& v2) 00060 { setVertices(v0,v1,v2); } 00063 explicit Triangle_(const Vec3P* vertices) 00064 { setVertices(vertices); } 00067 explicit Triangle_(const Vec3P** vertexPointers) 00068 { setVertices(*vertexPointers[0], *vertexPointers[1], *vertexPointers[2]); } 00069 00071 Triangle_& setVertex(int i, const Vec3P& p) 00072 { assert(0<=i && i<3); v[i] = p; return *this; } 00074 Triangle_& setVertices(const Vec3P& v0, const Vec3P& v1, const Vec3P& v2) 00075 { v[0]=v0; v[1]=v1; v[2]=v2; return *this; } 00078 Triangle_& setVertices(const Vec3P* vertices) 00079 { v[0]=vertices[0]; v[1]=vertices[1]; v[2]=vertices[2]; return *this; } 00080 00083 const Vec3P& getVertex(int i) const 00084 { SimTK_INDEXCHECK(i,3,"Geo::Triangle_::getVertex()"); 00085 return v[i]; } 00088 Vec3P& updVertex(int i) 00089 { SimTK_INDEXCHECK(i,3,"Geo::Triangle_::updVertex()"); 00090 return v[i]; } 00091 00093 const Vec3P& operator[](int i) const {return getVertex(i);} 00095 Vec3P& operator[](int i) {return updVertex(i);} 00096 00100 LineSeg_<P> getEdge(int i) const 00101 { SimTK_INDEXCHECK(i,3,"Geo::Triangle_::getEdge()"); 00102 return LineSeg_<P>(v[i],v[(i+1)%3]); } 00103 00106 Vec3P findPoint(const Vec2P& uv) const 00107 { return uv[0]*v[0] + uv[1]*v[1] + (1-uv[0]-uv[1])*v[2]; } 00108 00111 Vec3P findCentroid() const 00112 { return (v[0]+v[1]+v[2]) / RealP(3); } 00113 00116 UnitVec3P calcUnitNormal() const 00117 { return UnitVec3P((v[1]-v[0]) % (v[2]-v[0])); } 00118 00122 Sphere_<P> calcBoundingSphere() const 00123 { return Geo::Point_<P>::calcBoundingSphere(v[0],v[1],v[2]); } 00124 00126 RealP calcArea() const 00127 { return ((v[1]-v[0]) % (v[2]-v[0])).norm() / 2; } 00128 00130 RealP calcAreaSqr() const 00131 { return ((v[1]-v[0]) % (v[2]-v[0])).normSqr() / 4; } 00132 00136 Vec3P findNearestPoint(const Vec3P& position, Vec2P& uv) const 00137 {SimTK_ASSERT_ALWAYS(!"implemented", 00138 "Geo::Triangle_::findNearestPoint(): Not implemented yet."); 00139 return Vec3P();} 00140 00143 bool intersectsRay(const Vec3P& origin, const UnitVec3P& direction, 00144 RealP& distance, Vec2P& uv) const 00145 {SimTK_ASSERT_ALWAYS(!"implemented", 00146 "Geo::Triangle_::intersectsRay(): Not implemented yet."); return false;} 00147 00150 SimTK_SIMMATH_EXPORT bool overlapsTriangle(const Triangle_<P>& other) const; 00157 SimTK_SIMMATH_EXPORT bool intersectsTriangle(const Triangle_<P>& other, LineSeg_<P>& seg, 00158 bool& isCoplanar) const; 00159 00166 private: 00167 // Vertices in the order they were supplied in the constructor. 00168 Vec3P v[3]; 00169 }; 00170 00171 } // namespace SimTK 00172 00173 #endif // SimTK_SIMMATH_GEO_TRIANGLE_H_