Chi-Tech
chi_meshcontinuum_exportobj.cc
Go to the documentation of this file.
1#include "chi_meshcontinuum.h"
2#include <fstream>
3
4#include "chi_runtime.h"
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8
9#include "chi_mpi.h"
10
11
12//###################################################################
13/**Export cells to python.
14 *
15 * \todo Export Cells to OBJ needs polygon support. */
17 ExportCellsToObj(const char* fileName, bool per_material, int options) const
18{
19 if (!per_material)
20 {
21 FILE* of = fopen(fileName,"w");
22
23 if (of == nullptr)
24 {
25 Chi::log.LogAllError() << "Could not open file: "
26 << std::string(fileName);
27 Chi::Exit(EXIT_FAILURE);
28 }
29
30 //====================================== Develop list of faces and nodes
31 std::set<int> nodes_set;
32 std::vector<chi_mesh::CellFace> faces_to_export;
33 for (auto& cell : local_cells)
34 {
35 if (cell.Type() == chi_mesh::CellType::POLYHEDRON)
36 {
37 for (auto& face : cell.faces_)
38 {
39 if (not face.has_neighbor_)
40 {
41 faces_to_export.push_back(face);
42
43 for (int vid : face.vertex_ids_)
44 nodes_set.insert(vid);
45 }//if boundary
46 }//for face
47 }//if polyhedron
48 }//for local cell
49
50 //====================================== Write header
51 fprintf(of,"# Exported mesh file from Extrusion script\n");
52 std::string str_file_name(fileName);
53 std::string file_base_name =
54 str_file_name.substr(0,str_file_name.find('.'));
55 fprintf(of,"o %s\n",file_base_name.c_str());
56
57 //====================================== Develop node mapping and write them
58 std::vector<int> node_mapping(GetGlobalVertexCount(), -1);
59
60 int node_counter=0;
61 for (auto node : nodes_set)
62 {
63 node_counter++;
64 int node_g_index = node;
65 node_mapping[node_g_index] = node_counter;
66
67 chi_mesh::Vertex cur_v = vertices[node_g_index];
68
69 fprintf(of,"v %9.6f %9.6f %9.6f\n",cur_v.x,cur_v.y,cur_v.z);
70 }
71
72 //====================================== Write face normals
73 for (const auto& face : faces_to_export)
74 {
75 fprintf(of,"vn %.4f %.4f %.4f\n",
76 face.normal_.x,
77 face.normal_.y,
78 face.normal_.z);
79 }
80
81 //====================================== Write faces
82 int normal_counter=0;
83 for (const auto& face : faces_to_export)
84 {
85 normal_counter++;
86 fprintf(of,"f");
87
88 for (auto v_g_index : face.vertex_ids_)
89 fprintf(of," %d//%d",node_mapping[v_g_index],normal_counter);
90
91 fprintf(of,"\n");
92 }
93
94
95 fclose(of);
96
98 << "Exported Volume mesh to "
99 << str_file_name;
100 }//Whole mesh
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PER MATERIAL
102 else
103 {
104 //========================================= Get base name
105 std::string str_file_name(fileName);
106 std::string file_base_name =
107 str_file_name.substr(0,str_file_name.find('.'));
108
109 if (Chi::material_stack.empty())
110 {
112 << "ExportCellsToObj: No mesh will be exported because there "
113 << "are no physics materials present";
114 }
115
116
117 for (int mat=0; mat< Chi::material_stack.size(); mat++)
118 {
119 std::string mat_base_name = file_base_name +
120 std::string("_m") +
121 std::to_string(mat);
122 std::string mat_file_name = mat_base_name +
123 std::string(".obj");
124 FILE* of = fopen(mat_file_name.c_str(),"w");
125
126 if (of == nullptr)
127 {
128 Chi::log.LogAllError() << "Could not open file: "
129 << mat_file_name;
130 Chi::Exit(EXIT_FAILURE);
131 }
132
133 //====================================== Develop list of faces and nodes
134 std::set<int> nodes_set;
135 std::vector<chi_mesh::CellFace> faces_to_export;
136 for (const auto& cell : local_cells)
137 {
138 if (cell.Type() == chi_mesh::CellType::POLYHEDRON)
139 {
140 if (cell.material_id_ != mat) continue;
141
142 for (const auto& face : cell.faces_)
143 {
144 int adjcell_glob_index = face.neighbor_id_;
145
146 if (adjcell_glob_index<0)
147 {
148 faces_to_export.push_back(face);
149
150 for (auto vid : face.vertex_ids_)
151 nodes_set.insert(vid);
152 }//if boundary
153 else
154 {
155 auto& adj_cell = cells[adjcell_glob_index];
156
157 if (adj_cell.material_id_ != mat)
158 {
159 faces_to_export.push_back(face);
160
161 for (auto vid : face.vertex_ids_)
162 nodes_set.insert(vid);
163 }//if material missmatch
164 }//if neigbor cell
165 }//for face
166 }//if polyhedron
167 }//for local cell
168
169 //====================================== Write header
170 fprintf(of,"# Exported mesh file from Extrusion script\n");
171 fprintf(of,"o %s\n",mat_base_name.c_str());
172
173 //====================================== Develop node mapping and write them
174 std::vector<int> node_mapping(GetGlobalVertexCount(), -1);
175
176 int node_counter=0;
177 for (auto node : nodes_set)
178 {
179 node_counter++;
180 int node_g_index = node;
181 node_mapping[node_g_index] = node_counter;
182
183 chi_mesh::Vertex cur_v = vertices[node_g_index];
184
185 fprintf(of,"v %9.6f %9.6f %9.6f\n",cur_v.x,cur_v.y,cur_v.z);
186 }
187
188 //====================================== Write face normals
189 for (const auto& face : faces_to_export)
190 {
191 fprintf(of,"vn %.4f %.4f %.4f\n",
192 face.normal_.x,
193 face.normal_.y,
194 face.normal_.z);
195 }
196
197 //====================================== Write faces
198 int normal_counter=0;
199 for (const auto& face : faces_to_export)
200 {
201 normal_counter++;
202 fprintf(of,"f");
203
204 for (auto v_g_index : face.vertex_ids_)
205 fprintf(of," %d//%d",node_mapping[v_g_index],normal_counter);
206
207 fprintf(of,"\n");
208 }
209
210
211 fclose(of);
212
213 Chi::log.Log()
214 << "Exported Material Volume mesh to "
215 << mat_file_name;
216 }//for mat
217 }//if per material
218
219}
static std::vector< chi_physics::MaterialPtr > material_stack
Definition: chi_runtime.h:90
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream LogAllError()
Definition: chi_log.h:239
LogStream Log0Warning()
Definition: chi_log.h:231
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
void push_back(std::unique_ptr< chi_mesh::Cell > new_cell)
LocalCellHandler local_cells
uint64_t GetGlobalVertexCount() const
GlobalCellHandler cells
void ExportCellsToObj(const char *fileName, bool per_material=false, int options=0) const
double x
Element-0.
double y
Element-1.
double z
Element-2.