00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_UTIL_T4MESH_BLOCK_GENERATOR_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_UTIL_T4MESH_BLOCK_GENERATOR_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/grid/grid.h>
00013 #include <OpenTissue/core/containers/grid/util/grid_idx2coord.h>
00014
00015 namespace OpenTissue
00016 {
00017 namespace t4mesh
00018 {
00019
00020
00032 template < typename real_type, typename t4mesh_type >
00033 void generate_blocks(
00034 unsigned int const & I
00035 , unsigned int const & J
00036 , unsigned int const & K
00037 , real_type const & block_width
00038 , real_type const & block_height
00039 , real_type const & block_depth
00040 , t4mesh_type & mesh
00041 )
00042 {
00043 typedef typename t4mesh_type::node_iterator node_iterator;
00044 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00045
00046 mesh.clear();
00047
00048 unsigned int numVertices = (I + 1) * (J + 1) * (K + 1);
00049 for (unsigned int i=0; i<numVertices; ++i)
00050 mesh.insert();
00051
00052 node_iterator node = mesh.node_begin();
00053 for (unsigned int x = 0; x <= I; ++x)
00054 {
00055 for (unsigned int y = 0; y <= J; ++y)
00056 {
00057 for (unsigned int z = 0; z <= K; ++z)
00058 {
00059 node->m_coord(0) = block_width*x;
00060 node->m_coord(2) = block_depth*y;
00061 node->m_coord(1) = block_height*z;
00062 ++node;
00063 }
00064 }
00065 }
00066 for (unsigned int i = 0; i < I; ++i)
00067 {
00068 for (unsigned int j = 0; j < J; ++j)
00069 {
00070 for (unsigned int k = 0; k < K; ++k)
00071 {
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 int p0 = (i * (J + 1) + j) * (K + 1) + k;
00082 int p1 = p0 + 1;
00083 int p3 = ((i + 1) * (J + 1) + j) * (K + 1) + k;
00084 int p2 = p3 + 1;
00085 int p7 = ((i + 1) * (J + 1) + (j + 1)) * (K + 1) + k;
00086 int p6 = p7 + 1;
00087 int p4 = (i * (J + 1) + (j + 1)) * (K + 1) + k;
00088 int p5 = p4 + 1;
00089 tetrahedron_iterator t;
00090
00091 if ((i + j + k) % 2 == 1)
00092 {
00093 t = mesh.insert(p1,p2,p6,p3);
00094 t = mesh.insert(p3,p6,p4,p7);
00095 t = mesh.insert(p1,p4,p6,p5);
00096 t = mesh.insert(p1,p3,p4,p0);
00097 t = mesh.insert(p1,p6,p4,p3);
00098 }
00099 else
00100 {
00101 t = mesh.insert(p2,p0,p5,p1);
00102 t = mesh.insert(p2,p7,p0,p3);
00103 t = mesh.insert(p2,p5,p7,p6);
00104 t = mesh.insert(p0,p7,p5,p4);
00105 t = mesh.insert(p2,p0,p7,p5);
00106 }
00107 }
00108 }
00109 }
00110 };
00111
00112
00119 template <typename t4mesh_type, typename grid_type >
00120 void generate_blocks(
00121 grid_type & voxels
00122 , t4mesh_type & mesh
00123 )
00124 {
00125 typedef typename grid_type::math_types math_types;
00126 typedef typename t4mesh_type::node_iterator node_iterator;
00127 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00128 typedef typename math_types::real_type real_type;
00129 typedef typename math_types::vector3_type vector3_type;
00130 typedef OpenTissue::grid::Grid<int,math_types> idx_grid_type;
00131 typedef typename grid_type::index_iterator voxel_iterator;
00132
00133 mesh.clear();
00134
00135 idx_grid_type grid;
00136 grid.create(voxels.min_coord(),voxels.max_coord(),voxels.I(),voxels.J(),voxels.K());
00137
00138
00139 {
00140 voxel_iterator v = voxels.begin();
00141 voxel_iterator vend = voxels.end();
00142 for(;v!=vend;++v)
00143 {
00144 if(*v)
00145 {
00146 vector3_type coord;
00147 OpenTissue::grid::idx2coord(v,coord);
00148 node_iterator n = mesh.insert( coord );
00149 grid(v.i(),v.j(),v.k()) = n->idx();
00150 }
00151 else
00152 grid(v.i(),v.j(),v.k()) = -1;
00153 }
00154 }
00155
00156
00157 unsigned int I = voxels.I();
00158 unsigned int J = voxels.J();
00159 unsigned int K = voxels.K();
00160 for (unsigned int i = 0; i < I-1; ++i)
00161 {
00162 for (unsigned int j = 0; j < J-1; ++j)
00163 {
00164 for (unsigned int k = 0; k < K-1; ++k)
00165 {
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 int p0 = (k*J + j)*I + i;
00182 int p1 = (k*J + j)*I + (i+1);
00183 int p2 = (k*J + (j+1))*I + (i+1);
00184 int p3 = (k*J + (j+1))*I + i;
00185 int p4 = ((k+1)*J + j)*I + i;
00186 int p5 = ((k+1)*J + j)*I + (i+1);
00187 int p6 = ((k+1)*J + (j+1))*I + (i+1);
00188 int p7 = ((k+1)*J + (j+1))*I + i;
00189
00190 tetrahedron_iterator t;
00191
00192
00193 if ((i + j + k) % 2 == 1)
00194 {
00195 if ( voxels(p1) && voxels(p2) && voxels(p3) && voxels(p6) )
00196 t = mesh.insert(grid(p1),grid(p2),grid(p3),grid(p6));
00197
00198 if ( voxels(p3) && voxels(p6) && voxels(p7) && voxels(p4) )
00199 t = mesh.insert(grid(p3),grid(p6),grid(p7),grid(p4));
00200
00201 if ( voxels(p1) && voxels(p4) && voxels(p5) && voxels(p6) )
00202 t = mesh.insert(grid(p1),grid(p4),grid(p5),grid(p6));
00203
00204 if ( voxels(p1) && voxels(p3) && voxels(p0) && voxels(p4) )
00205 t = mesh.insert(grid(p1),grid(p3),grid(p0),grid(p4));
00206
00207 if ( voxels(p1) && voxels(p6) && voxels(p3) && voxels(p4) )
00208 t = mesh.insert(grid(p1),grid(p6),grid(p3),grid(p4));
00209 }
00210 else
00211 {
00212 if ( voxels(p2) && voxels(p0) && voxels(p1) && voxels(p5) )
00213 t = mesh.insert(grid(p2),grid(p0),grid(p1),grid(p5));
00214
00215 if ( voxels(p2) && voxels(p7) && voxels(p3) && voxels(p0) )
00216 t = mesh.insert(grid(p2),grid(p7),grid(p3),grid(p0));
00217
00218 if ( voxels(p2) && voxels(p5) && voxels(p6) && voxels(p7) )
00219 t = mesh.insert(grid(p2),grid(p5),grid(p6),grid(p7));
00220
00221 if ( voxels(p0) && voxels(p7) && voxels(p4) && voxels(p5) )
00222 t = mesh.insert(grid(p0),grid(p7),grid(p4),grid(p5));
00223
00224 if ( voxels(p2) && voxels(p0) && voxels(p5) && voxels(p7) )
00225 t = mesh.insert(grid(p2),grid(p0),grid(p5),grid(p7));
00226 }
00227 }
00228 }
00229 }
00230
00231
00232 node_iterator n = mesh.node_begin();
00233 node_iterator nend = mesh.node_end();
00234 for(;n!=nend;++n)
00235 {
00236 if(n->isolated())
00237 {
00238 std::cout << "t4mesh::generate_blocks(): Isolated node encountered, could not erase it from t4mesh" << std::endl;
00239
00240 }
00241 }
00242 std::cout << "t4mesh::generate_blocks(): |N| = " << mesh.size_nodes() << " |T| = " << mesh.size_tetrahedra() << std::endl;
00243 }
00244
00245 }
00246 }
00247
00248
00249 #endif