00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef INVENTORVORONOIWRITER_HPP_
00031 #define INVENTORVORONOIWRITER_HPP_
00032
00033 #include "VoronoiTessellation.hpp"
00034 #include "OutputFileHandler.hpp"
00035
00036
00037 const std::string INVENTOR_HEADER="#Inventor V2.0 ascii \n\
00038 \n\
00039 Separator { \n\
00040 Info { \n\
00041 string \"tetrahedron.iv generated by IVREAD.\" \n\
00042 string \"Original data in file tetrahedron.wrl.\" \n\
00043 } \n\
00044 Separator { \n\
00045 LightModel { \n\
00046 model BASE_COLOR \n\
00047 } \n\
00048 \n\
00049 Material { \n\
00050 ambientColor 0.2 0.2 0.2 \n\
00051 \n\
00052 emissiveColor 0.0 0.0 0.0 \n\
00053 specularColor 1.0 1.0 1.0 \n\
00054 shininess 0.2 \n\
00055 \n\
00056 } \n\
00057 ShapeHints { \n\
00058 vertexOrdering COUNTERCLOCKWISE \n\
00059 shapeType UNKNOWN_SHAPE_TYPE \n\
00060 faceType CONVEX \n\
00061 creaseAngle 6.28319 \n\
00062 } \n\
00063 \n\
00064 DrawStyle { \n\
00065 style LINES \n\
00066 lineWidth 3 \n\
00067 linePattern 255 \n\
00068 } \n\
00069 \n\
00070 Coordinate3 { \n\
00071 point [ \n\
00072 ";
00073
00074
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00102
00103
00104
00105
00106 const std::string INVENTOR_MID=" ] \n\
00107 } \n\
00108 IndexedFaceSet { \n\
00109 coordIndex [ \n\
00110 ";
00111
00112 const std::string INVENTOR_FOOTER=" ] \n\
00113 } \n\
00114 } \n\
00115 } \n\
00116 ";
00117
00118
00119
00120 class InventorVoronoiWriter
00121 {
00122 protected:
00123 OutputFileHandler *mpOutputFileHandler;
00124 std::string mBaseName;
00126 public:
00128 InventorVoronoiWriter(const std::string &rDirectory,
00129 const std::string &rBaseName,
00130 const bool clearOutputDir=true)
00131 : mBaseName(rBaseName)
00132 {
00133 mpOutputFileHandler = new OutputFileHandler(rDirectory, clearOutputDir);
00134 }
00135
00137 ~InventorVoronoiWriter()
00138 {
00139 delete mpOutputFileHandler;
00140 }
00141
00145 void Write(const VoronoiTessellation<3>& rTessellation)
00146 {
00147
00148 std::string file_name = this->mBaseName+".iv";
00149 out_stream p_file = this->mpOutputFileHandler->OpenOutputFile(file_name);
00150
00151
00152
00153 *p_file << INVENTOR_HEADER;
00154
00155
00156
00157
00158 std::map< c_vector<double, 3>*, unsigned> vertex_number_map;
00159 for ( unsigned vertex_number=0;
00160 vertex_number<rTessellation.mVertices.size();
00161 vertex_number++ )
00162 {
00163 c_vector<double ,3>& vertex=*(rTessellation.mVertices[vertex_number]);
00164 *p_file << " " << vertex(0) << " " << vertex(1) << " " << vertex(2) << ",\n";
00165
00166 vertex_number_map[rTessellation.mVertices[vertex_number]]=vertex_number;
00167 }
00168
00169 *p_file << INVENTOR_MID;
00170
00171
00172
00173 for (unsigned face_number=0;
00174 face_number < rTessellation.mFaces.size();
00175 face_number++)
00176 {
00177 *p_file << " ";
00178 Face<3>& face=*(rTessellation.mFaces[face_number]);
00179 for (unsigned vertex_local_number=0;
00180 vertex_local_number < face.mVertices.size();
00181 vertex_local_number++)
00182 {
00183
00184 unsigned vertex_number=vertex_number_map[face.mVertices[vertex_local_number]];
00185 *p_file << vertex_number << ", ";
00186 }
00187 *p_file << "\n";
00188 }
00189 *p_file << INVENTOR_FOOTER;
00190 }
00191
00196 void ScaleAndWrite(VoronoiTessellation<3>& rTessellation, double scaleFactor)
00197 {
00198 if ((scaleFactor <= 0.0) || (scaleFactor > 1.0))
00199 {
00200 EXCEPTION("scaleFactor should be between 0 and 1");
00201 }
00202
00203
00204 std::string file_name = this->mBaseName+".iv";
00205 out_stream p_file = this->mpOutputFileHandler->OpenOutputFile(file_name);
00206
00207
00208 *p_file << INVENTOR_HEADER;
00209
00210 unsigned global_vertex_number = 0;
00211
00212
00213 std::vector<std::vector<unsigned> > new_faces_data;
00214 std::vector<unsigned> number_faces_per_cell;
00215
00216
00217 for (unsigned cell_index = 0; cell_index<rTessellation.mVoronoiCells.size(); cell_index++)
00218 {
00219 c_vector<double, 3>& r_cell_centre = rTessellation.mVoronoiCells[cell_index].rGetVoronoiCellCentre();
00220
00221
00222 std::map< c_vector<double, 3>*, unsigned> vertex_number_map;
00223
00224 const VoronoiCell& r_cell = rTessellation.mVoronoiCells[cell_index];
00225
00226 for (unsigned face_number=0; face_number<r_cell.mFaces.size(); face_number++)
00227 {
00228 std::vector<unsigned> face_vertex_data;
00229
00230 Face<3>& r_face = *(r_cell.mFaces[face_number]);
00231 for (unsigned face_vertex_number=0; face_vertex_number<r_face.mVertices.size(); face_vertex_number++)
00232 {
00233 unsigned global_number_for_this_vertex;
00234
00235
00236 std::map< c_vector<double,3>*,unsigned>::iterator iter = vertex_number_map.find(r_face.mVertices[face_vertex_number]);
00237 if(iter!=vertex_number_map.end())
00238 {
00239 global_number_for_this_vertex = iter->second;
00240 }
00241 else
00242 {
00243 global_number_for_this_vertex = global_vertex_number;
00244
00245
00246 vertex_number_map[r_face.mVertices[face_vertex_number]] = global_number_for_this_vertex;
00247 global_vertex_number++;
00248
00249
00250 c_vector<double,3> new_vertex = *(r_face.mVertices[face_vertex_number]);
00251 new_vertex = scaleFactor*(new_vertex - r_cell_centre) + r_cell_centre;
00252
00253 *p_file << " " << new_vertex(0) << " " << new_vertex(1) << " " << new_vertex(2) << ",\n";
00254 }
00255
00256
00257 face_vertex_data.push_back(global_number_for_this_vertex);
00258 }
00259
00260
00261 new_faces_data.push_back( face_vertex_data );
00262 }
00263
00264
00265 number_faces_per_cell.push_back(r_cell.mFaces.size());
00266 }
00267
00268 unsigned index=0;
00269
00270
00271
00272 for(unsigned i=0; i<number_faces_per_cell.size(); i++)
00273 {
00274 if(number_faces_per_cell[i]>0)
00275 {
00276 *p_file << INVENTOR_MID;
00277
00278 for(unsigned j=0; j<number_faces_per_cell[i]; j++)
00279 {
00280 *p_file << " ";
00281 assert(index<new_faces_data.size());
00282 if ((rTessellation.rGetCell(i).mOrientations)[j])
00283 {
00284 for(unsigned k=0; k<new_faces_data[index].size(); k++)
00285 {
00286 *p_file << new_faces_data[index][k] << ", ";
00287 }
00288 }
00289 else
00290 {
00291 for(unsigned k=0; k<new_faces_data[index].size(); k++)
00292 {
00293 unsigned l = new_faces_data[index].size() - k - 1;
00294 *p_file << new_faces_data[index][l] << ", ";
00295 }
00296 }
00297 *p_file << "\n";
00298 index++;
00299 }
00300 }
00301 }
00302
00303 *p_file << INVENTOR_FOOTER;
00304 }
00305 };
00306
00307 #endif