00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_MESH_MAKE_BOX_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_UTIL_MESH_MAKE_BOX_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012
00013 namespace OpenTissue
00014 {
00015 namespace mesh
00016 {
00017
00029 template<typename real_type,typename mesh_type>
00030 bool make_box(real_type const & length,real_type const & height,real_type const & depth,mesh_type & mesh)
00031 {
00032 typedef typename mesh_type::math_types math_types;
00033 typedef typename math_types::value_traits value_traits;
00034 typedef typename math_types::vector3_type vector3_type;
00035
00036
00037 assert(length>=0 || !"Box did not have valid dimensions");
00038 assert(height>=0 || !"Box did not have valid dimensions");
00039 assert(depth >=0 || !"Box did not have valid dimensions");
00040
00041 mesh.clear();
00042
00043 real_type x = length/static_cast<real_type>(2.0);
00044 real_type y = height/static_cast<real_type>(2.0);
00045 real_type z = depth/static_cast<real_type>(2.0);
00046
00047 vector3_type cl0( x, y,-z);
00048 vector3_type cl1(-x, y,-z);
00049 vector3_type cl2(-x,-y,-z);
00050 vector3_type cl3( x,-y,-z);
00051 vector3_type cu0( x, y, z);
00052 vector3_type cu1(-x, y, z);
00053 vector3_type cu2(-x,-y, z);
00054 vector3_type cu3( x,-y, z);
00055
00056 typename mesh_type::vertex_handle l0 = mesh.add_vertex(cl0);
00057 typename mesh_type::vertex_handle l1 = mesh.add_vertex(cl1);
00058 typename mesh_type::vertex_handle l2 = mesh.add_vertex(cl2);
00059 typename mesh_type::vertex_handle l3 = mesh.add_vertex(cl3);
00060
00061 typename mesh_type::vertex_handle u0 = mesh.add_vertex(cu0);
00062 typename mesh_type::vertex_handle u1 = mesh.add_vertex(cu1);
00063 typename mesh_type::vertex_handle u2 = mesh.add_vertex(cu2);
00064 typename mesh_type::vertex_handle u3 = mesh.add_vertex(cu3);
00065
00066 mesh.add_face(l1,l0,l2);
00067 mesh.add_face(l2,l0,l3);
00068 mesh.add_face(u3,u0,u2);
00069 mesh.add_face(u2,u0,u1);
00070 mesh.add_face(l3,l0,u3);
00071 mesh.add_face(u3,l0,u0);
00072 mesh.add_face(u1,l1,u2);
00073 mesh.add_face(u2,l1,l2);
00074 mesh.add_face(l0,l1,u0);
00075 mesh.add_face(u0,l1,u1);
00076 mesh.add_face(l2,l3,u2);
00077 mesh.add_face(u2,l3,u3);
00078 {
00079 typename mesh_type::vertex_iterator vl0 = mesh.get_vertex_iterator(l0);
00080 vl0->m_normal = normalize(vl0->m_coord);
00081 vl0->m_u = 0.0;
00082 vl0->m_v = 0.0;
00083 vl0->m_color = vector3_type(0.0,0.0,0.0);
00084
00085 typename mesh_type::vertex_iterator vl1 = mesh.get_vertex_iterator(l1);
00086 vl1->m_normal = normalize(vl1->m_coord);
00087 vl1->m_u = 1.0;
00088 vl1->m_v = 0.0;
00089 vl1->m_color = vector3_type(1.0,0.0,0.0);
00090
00091 typename mesh_type::vertex_iterator vl2 = mesh.get_vertex_iterator(l2);
00092 vl2->m_normal = normalize(vl2->m_coord);
00093 vl2->m_u = 1.0;
00094 vl2->m_v = 1.0;
00095 vl2->m_color = vector3_type(1.0,1.0,0.0);
00096
00097 typename mesh_type::vertex_iterator vl3 = mesh.get_vertex_iterator(l3);
00098 vl3->m_normal = normalize(vl3->m_coord);
00099 vl3->m_u = 0.0;
00100 vl3->m_v = 1.0;
00101 vl3->m_color = vector3_type(0.0,1.0,0.0);
00102
00103 typename mesh_type::vertex_iterator vu0 = mesh.get_vertex_iterator(u0);
00104 vu0->m_normal = normalize(vu0->m_coord);
00105 vu0->m_u = 0.0;
00106 vu0->m_v = 0.0;
00107 vu0->m_color = vector3_type(0.0,0.0,1.0);
00108
00109 typename mesh_type::vertex_iterator vu1 = mesh.get_vertex_iterator(u1);
00110 vu1->m_normal = normalize(vu1->m_coord);
00111 vu1->m_u = 1.0;
00112 vu1->m_v = 0.0;
00113 vu1->m_color = vector3_type(1.0,0.0,1.0);
00114
00115 typename mesh_type::vertex_iterator vu2 = mesh.get_vertex_iterator(u2);
00116 vu2->m_normal = normalize(vu2->m_coord);
00117 vu2->m_u = 1.0;
00118 vu2->m_v = 1.0;
00119 vu2->m_color = vector3_type(1.0,1.0,1.0);
00120
00121 typename mesh_type::vertex_iterator vu3 = mesh.get_vertex_iterator(u3);
00122 vu3->m_normal = normalize(vu3->m_coord);
00123 vu3->m_u = 0.0;
00124 vu3->m_v = 1.0;
00125 vu3->m_color = vector3_type(0.0,1.0,1.0);
00126 }
00127 return true;
00128 }
00129
00130 }
00131 }
00132
00133
00134 #endif