Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_GEOMETRY_GEOMETRY_OBB_Z_SLICER_H
00002 #define OPENTISSUE_CORE_GEOMETRY_GEOMETRY_OBB_Z_SLICER_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_precision.h>
00013
00014 #include <iostream>
00015
00016 namespace OpenTissue
00017 {
00018 namespace geometry
00019 {
00020
00052 template <typename real_type, typename vector3_type>
00053 unsigned int obb_z_slice(vector3_type const vertices[8], real_type z, vector3_type slice[8])
00054 {
00055 static real_type threshold = math::working_precision<real_type>();
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 static int edges[12][2] =
00083 {
00084 {0,1},
00085 {1,5},
00086 {4,5},
00087 {0,4},
00088 {3,7},
00089 {6,7},
00090 {2,6},
00091 {2,3},
00092 {0,3},
00093 {4,7},
00094 {5,6},
00095 {1,2}
00096 };
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 static int edge_flags[12] =
00107 {
00108 0x11,
00109 0x14,
00110 0x12,
00111 0x18,
00112 0x28,
00113 0x22,
00114 0x24,
00115 0x21,
00116 0x09,
00117 0x0A,
00118 0x06,
00119 0x05
00120 };
00121
00122 unsigned int slice_size = 0;
00123 int idx[ 12 ];
00124
00125
00126 real_type zp = z+threshold;
00127 real_type zm = z-threshold;
00128
00129 for ( unsigned int i = 0; i < 12; ++i )
00130 {
00131 real_type z0 = vertices[ edges[ i ][ 0 ] ][ 2 ];
00132 real_type z1 = vertices[ edges[ i ][ 1 ] ][ 2 ];
00133
00134
00135 if (
00136 ( ( z0 <= zp ) && ( zm <= z1 ) )
00137 ||
00138 ( ( z1 <= zp ) && ( zm <= z0 ) )
00139 )
00140 idx[ slice_size++ ] = i;
00141 }
00142
00143 if(slice_size==0)
00144 return 0;
00145
00146
00147
00148
00149
00150
00151
00152
00153 if ( slice_size > 3 )
00154 {
00155 for ( unsigned int i = 0; i < (slice_size - 1); ++i )
00156 {
00157 for ( unsigned int j = i + 1; j <= (slice_size - 1); ++j )
00158 {
00159
00160 if ( edge_flags[ idx[ i ] ] & edge_flags[ idx[ j ] ] )
00161 {
00162
00163 if ( j > i + 1 )
00164 {
00165
00166 unsigned int tmp = idx[ i + 1 ];
00167 idx[ i + 1 ] = idx[ j ];
00168 idx[ j ] = tmp;
00169 }
00170 break;
00171 }
00172 }
00173 }
00174 }
00175
00176
00177
00178 for ( unsigned int i = 0; i < slice_size; ++i )
00179 {
00180 vector3_type const & p0 = vertices[ edges[ idx[ i ] ][ 0 ] ];
00181 vector3_type const & p1 = vertices[ edges[ idx[ i ] ][ 1 ] ];
00182 real_type t = ( z - p0[ 2 ] ) / ( p1[ 2 ] - p0[ 2 ] );
00183 slice[ i ] = ((p1-p0)*t) + p0;
00184 }
00185 return slice_size;
00186 }
00187
00188 }
00189 }
00190
00191
00192 #endif