00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_MAKE_SPHERE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_POLYMESH_UTIL_MAKE_SPHERE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/common/util/mesh_compute_angle_weighted_vertex_normals.h>
00013 #include <OpenTissue/core/containers/mesh/polymesh/util/polymesh_subdivide.h>
00014
00015 namespace OpenTissue
00016 {
00017 namespace polymesh
00018 {
00019
00030 template<typename mesh_type>
00031 bool make_sphere(
00032 typename mesh_type::math_types::real_type const & radius
00033 , unsigned int n
00034 , mesh_type & mesh
00035 , bool use_tetrahedron = false
00036 )
00037 {
00038 typedef typename mesh_type::math_types math_types;
00039 typedef typename math_types::value_traits value_traits;
00040 typedef typename math_types::vector3_type vector3_type;
00041 typedef typename math_types::real_type real_type;
00042 typedef typename mesh_type::vertex_iterator vertex_iterator;
00043 typedef typename mesh_type::vertex_handle vertex_handle;
00044
00045 mesh.clear();
00046
00047 if(use_tetrahedron)
00048 {
00049
00050
00051 vector3_type coord0( -1, -1, -1 );
00052 vector3_type coord1( 1, 1, -1 );
00053 vector3_type coord2( 1, -1, 1 );
00054 vector3_type coord3( -1, 1, 1 );
00055
00056
00057
00058
00059
00060
00061 vertex_handle p0 = mesh.add_vertex(coord0);
00062 vertex_handle p1 = mesh.add_vertex(coord1);
00063 vertex_handle p2 = mesh.add_vertex(coord2);
00064 vertex_handle p3 = mesh.add_vertex(coord3);
00065 mesh.add_face(p0,p1,p2);
00066 mesh.add_face(p1,p0,p3);
00067 mesh.add_face(p2,p1,p3);
00068 mesh.add_face(p0,p2,p3);
00069
00070 }
00071 else
00072 {
00073
00074 real_type const X = 0.525731112119133606;
00075 real_type const Z = 0.850650808352039932;
00076
00077 vector3_type coord0(-X, 0.0, Z);
00078 vector3_type coord1(X, 0.0, Z);
00079 vector3_type coord2(-X, 0.0, -Z);
00080 vector3_type coord3(X, 0.0, -Z);
00081 vector3_type coord4(0.0, Z, X);
00082 vector3_type coord5(0.0, Z, -X);
00083 vector3_type coord6(0.0, -Z, X);
00084 vector3_type coord7(0.0, -Z, -X);
00085 vector3_type coord8(Z, X, 0.0);
00086 vector3_type coord9(-Z, X, 0.0);
00087 vector3_type coord10(Z, -X, 0.0);
00088 vector3_type coord11(-Z, -X, 0.0);
00089
00090 vertex_handle p0 = mesh.add_vertex( coord0 );
00091 vertex_handle p1 = mesh.add_vertex( coord1 );
00092 vertex_handle p2 = mesh.add_vertex( coord2 );
00093 vertex_handle p3 = mesh.add_vertex( coord3 );
00094 vertex_handle p4 = mesh.add_vertex( coord4 );
00095 vertex_handle p5 = mesh.add_vertex( coord5 );
00096 vertex_handle p6 = mesh.add_vertex( coord6 );
00097 vertex_handle p7 = mesh.add_vertex( coord7 );
00098 vertex_handle p8 = mesh.add_vertex( coord8 );
00099 vertex_handle p9 = mesh.add_vertex( coord9 );
00100 vertex_handle p10 = mesh.add_vertex( coord10 );
00101 vertex_handle p11 = mesh.add_vertex( coord11 );
00102
00103 mesh.add_face( p4, p0, p1 );
00104 mesh.add_face( p9, p0, p4 );
00105 mesh.add_face( p5, p9, p4 );
00106 mesh.add_face( p5, p4, p8 );
00107 mesh.add_face( p8, p4, p1 );
00108 mesh.add_face( p10, p8, p1 );
00109 mesh.add_face( p3, p8, p10 );
00110 mesh.add_face( p3, p5, p8 );
00111 mesh.add_face( p2, p5, p3 );
00112 mesh.add_face( p7, p2, p3 );
00113 mesh.add_face( p10, p7, p3 );
00114 mesh.add_face( p6, p7, p10 );
00115 mesh.add_face( p11, p7, p6 );
00116 mesh.add_face( p0, p11, p6 );
00117 mesh.add_face( p1, p0, p6 );
00118 mesh.add_face( p1, p6, p10 );
00119 mesh.add_face( p0, p9, p11 );
00120 mesh.add_face( p11, p9, p2 );
00121 mesh.add_face( p2, p9, p5 );
00122 mesh.add_face( p2, p7, p11 );
00123 }
00124
00125
00126 for(vertex_iterator v = mesh.vertex_begin();v!=mesh.vertex_end();++v)
00127 v->m_coord = radius*unit(v->m_coord);
00128
00129 for(unsigned int i=0;i<n;++i)
00130 {
00131 subdivide( mesh, 0.0);
00132 for(vertex_iterator v = mesh.vertex_begin();v!=mesh.vertex_end();++v)
00133 v->m_coord = radius*unit(v->m_coord);
00134 }
00135
00136 mesh::compute_angle_weighted_vertex_normals(mesh);
00137
00138 return true;
00139 }
00140
00141 }
00142 }
00143
00144
00145 #endif