00001 #ifndef OPENTISSUE_CORE_GEOMETRY_GEOMETRY_TETRAHEDRON_SLICER_H 00002 #define OPENTISSUE_CORE_GEOMETRY_GEOMETRY_TETRAHEDRON_SLICER_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 00013 namespace OpenTissue 00014 { 00015 namespace geometry 00016 { 00017 00018 00019 template<typename vector3_type_> 00020 class TetrahedronSlicer 00021 { 00022 public: 00023 00024 typedef vector3_type_ vector3_type; 00025 typedef typename vector3_type::value_type real_type; 00026 00027 protected: 00028 00029 vector3_type m_nodes[4]; 00030 int m_edges[6][2]; 00031 00032 00033 00034 vector3_type m_intersections[4]; 00035 vector3_type m_triangle1[3]; 00036 vector3_type m_triangle2[3]; 00037 00038 public: 00039 00040 TetrahedronSlicer( 00041 vector3_type const & p0 00042 , vector3_type const & p1 00043 , vector3_type const & p2 00044 , vector3_type const & p3 00045 ) 00046 { 00047 m_nodes[0] = p0; 00048 m_nodes[1] = p1; 00049 m_nodes[2] = p2; 00050 m_nodes[3] = p3; 00051 00052 // 00053 // Node indices and edge labels: 00054 // 00055 // 3 00056 // + 00057 // / \ 00058 // / \ 00059 // / . \ 00060 // / \ 00061 // / \ 00062 // / . \ 00063 // / \ 00064 // / \ 00065 // / . \ 00066 // C F E 00067 // / \ 00068 // / . \ 00069 // / \ 00070 // / . \ 00071 // / 2+ \ 00072 // / . . \ 00073 // / . B D. \ 00074 // / . . \ 00075 // +------------------A------------------+ 00076 //0 1 00077 // 00078 // 00079 m_edges[ 0 ][ 0 ] = 0; // A 00080 m_edges[ 0 ][ 1 ] = 1; 00081 m_edges[ 1 ][ 0 ] = 0; // B 00082 m_edges[ 1 ][ 1 ] = 2; 00083 m_edges[ 2 ][ 0 ] = 0; // C 00084 m_edges[ 2 ][ 1 ] = 3; 00085 m_edges[ 3 ][ 0 ] = 1; // D 00086 m_edges[ 3 ][ 1 ] = 2; 00087 m_edges[ 4 ][ 0 ] = 1; // E 00088 m_edges[ 4 ][ 1 ] = 3; 00089 m_edges[ 5 ][ 0 ] = 2; // F 00090 m_edges[ 5 ][ 1 ] = 3; 00091 00092 }; 00093 00094 protected: 00095 00096 template<typename plane_type> 00097 void compute_slice(plane_type const & plane) 00098 { 00099 //--- Loop over all edges and find those that cross the plane 00100 int count = 0; 00101 for(int i=0;i<6;++i) 00102 { 00103 vector3_type & a = m_nodes[m_edges[i][0]]; 00104 vector3_type & b = m_nodes[m_edges[i][1]]; 00105 real_type dst_a = plane.signed_distance(a); 00106 real_type dst_b = plane.signed_distance(b); 00107 if ( (dst_a*dst_b<0) || (dst_a==0 && dst_b>0) || (dst_b==0 && dst_a>0)) 00108 { 00109 //--- Compute coordinates of the plane intersections 00110 vector3_type & a = m_nodes[m_edges[ i ][0]]; 00111 vector3_type & b = m_nodes[m_edges[ i ][1]]; 00112 m_intersections[count++] = plane.get_intersection(a,b); 00113 } 00114 } 00115 assert(count==3 || count==4); //--no other possibilities? 00116 00117 //--- Order plane intersections to a a CCW polygon (as seen from positive side of plane) 00118 vector3_type & n = plane.n(); 00119 vector3_type e10 = m_intersections[1] - m_intersections[0]; 00120 vector3_type e21 = m_intersections[2] - m_intersections[1]; 00121 if(n*(e10%e20)<0) 00122 { 00123 m_triangle1[0] = m_intersections[0]; 00124 m_triangle1[1] = m_intersections[2]; 00125 m_triangle1[2] = m_intersections[1]; 00126 } 00127 else 00128 { 00129 m_triangle1[0] = m_intersections[0]; 00130 m_triangle1[1] = m_intersections[1]; 00131 m_triangle1[2] = m_intersections[2]; 00132 } 00133 if ( count == 4 ) 00134 { 00135 for(int i=0;i<3;++i) 00136 { 00137 int next = (i+1)%3; 00138 int prev = i; 00139 vector3_type e = m_triangle1[next] - m_triangle1[prev]; 00140 vector3_type h = m_intersections[4] - m_triangle1[prev]; 00141 if(n*(h%e)>0) 00142 { 00143 m_triangle2[0] = m_triangle1[prev]; 00144 m_triangle2[1] = m_intersections[4]; 00145 m_triangle2[2] = m_triangle1[next]; 00146 break; 00147 } 00148 } 00149 } 00150 }; 00151 00152 }; 00153 00154 } // namespace geometry 00155 } // namespace OpenTissue 00156 00157 //OPENTISSUE_CORE_GEOMETRY_GEOMETRY_TETRAHEDRON_SLICER_H 00158 #endif