Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_TETGEN_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_TETGEN_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <iostream>
00013 #include <fstream>
00014 #include <map>
00015
00016 namespace OpenTissue
00017 {
00018 namespace t4mesh
00019 {
00029 template<typename point_container,typename t4mesh_type>
00030 bool tetgen_read(const std::string & filename,t4mesh_type & mesh,point_container & points)
00031 {
00032 typedef typename t4mesh_type::node_type node_type;
00033 typedef typename node_type::real_type real_type;
00034 typedef typename t4mesh_type::node_iterator node_iterator;
00035 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00036
00037 mesh.clear();
00038
00039 std::string node_filename = filename + ".node";
00040 std::string element_filename = filename + ".ele";
00041
00042 std::ifstream file(node_filename.c_str());
00043
00044 if(!file.is_open())
00045 {
00046 std::cerr << "Error unable to open file '" << node_filename << "'" << std::endl;
00047 return false;
00048 }
00049
00050
00051
00052
00053
00054 int cnt_nodes, dimension, cnt_attributes;
00055 bool has_boundary;
00056
00057 file >> cnt_nodes >> dimension >> cnt_attributes >> has_boundary;
00058
00059 points.resize(cnt_nodes);
00060
00061 std::map<int,node_iterator> internal_node_iterator;
00062
00063 int external_index;
00064 real_type x, y, z;
00065 real_type attribute;
00066
00067 int boundary_marker;
00068 for(int i=0; i<cnt_nodes; ++i)
00069 {
00070 file >> external_index >> x >> y >> z;
00071
00072 for (int a = 0; a < cnt_attributes; ++a)
00073 file >> attribute;
00074 if (has_boundary)
00075 file >> boundary_marker;
00076
00077 node_iterator node = mesh.insert();
00078 points[node->idx()](0) = x;
00079 points[node->idx()](1) = y;
00080 points[node->idx()](2) = z;
00081
00082 internal_node_iterator[external_index] = node;
00083 }
00084 file.close();
00085
00086
00087
00088
00089 file.open(element_filename.c_str());
00090 if(!file.is_open())
00091 {
00092 std::cerr << "Error unable to open file '" << element_filename << "'" << std::endl;
00093 return false;
00094 }
00095 int cnt_tetrahedra, cnt_points;
00096 file >> cnt_tetrahedra >> cnt_points >> cnt_attributes;
00097 if (cnt_points != 4)
00098 {
00099 std::cerr << "We only support tetrahedra with 4 points!" << std::endl;
00100 file.close();
00101 return false;
00102 }
00103
00104
00105
00106
00107 if(cnt_attributes>1)
00108 {
00109 std::cerr << "We only support tetrahedra with 1 region!" << std::endl;
00110 file.close();
00111 return false;
00112 }
00113 int external_node_idx0;
00114 int external_node_idx1;
00115 int external_node_idx2;
00116 int external_node_idx3;
00117 for(int i=0;i<cnt_tetrahedra;++i)
00118 {
00119 file >> external_index >> external_node_idx0 >> external_node_idx1 >> external_node_idx2 >> external_node_idx3;
00120
00121 for (int a = 0; a < cnt_attributes; ++a)
00122 file >> attribute;
00123
00124 node_iterator n0 = internal_node_iterator[external_node_idx0];
00125 node_iterator n1 = internal_node_iterator[external_node_idx1];
00126 node_iterator n2 = internal_node_iterator[external_node_idx2];
00127 node_iterator n3 = internal_node_iterator[external_node_idx3];
00128 mesh.insert(n0,n1,n2,n3);
00129 }
00130 file.close();
00131 std::cout << "Read "
00132 << cnt_nodes
00133 << " nodes and "
00134 << cnt_tetrahedra
00135 << " tetrahedra"
00136 << std::endl;
00137 return true;
00138 }
00139
00140
00150 template<typename t4mesh_type>
00151 bool tetgen_read(const std::string & filename,t4mesh_type & mesh)
00152 {
00153 default_point_container<t4mesh_type> points(&mesh);
00154 return tetgen_read(filename,mesh,points);
00155 }
00156
00157 }
00158 }
00159
00160
00161 #endif