Line data Source code
1 : /*
2 : * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include <fstream>
34 : #include "file_io_dump.h"
35 : #include "common/util/loader/loader_util.h"
36 : #include "lib_grid/lg_base.h"
37 :
38 : using namespace std;
39 :
40 : namespace ug
41 : {
42 :
43 0 : static bool ReadTriangles(Grid& grid, ifstream& in,
44 : Grid::VertexAttachmentAccessor<APosition>& aaPos,
45 : int& lineCount)
46 : {
47 0 : char* BUFFER = new char[512];
48 : vector<string> paramVec;
49 : bool bSuccess = true;
50 :
51 0 : while(!in.eof())
52 : {
53 : // read the line
54 0 : in.getline(BUFFER, 512);
55 0 : BUFFER[511] = 0;
56 :
57 : // split the parameters
58 0 : split_parameters(paramVec, BUFFER, " \r");
59 :
60 : // check if there are some at all
61 0 : if(!paramVec.empty())
62 : {
63 : // check if the line is a commentary
64 : string& str = paramVec[0];
65 0 : if(str.find('#') == 0){
66 : // theres nothing to do in here. Proceed by reading the next line.
67 : }
68 : // if the first parameter contains "end", we're done in here.
69 0 : else if(str.find("end") == 0){
70 : break;
71 : }
72 : // read the triangle
73 0 : else if(paramVec.size() == 9){
74 : // this line contains the coordinates of a triangle.
75 : // create vertices and assign the coordinates.
76 : RegularVertex* v[3];
77 0 : for(int i = 0; i < 3; ++i)
78 : {
79 0 : v[i] = *grid.create<RegularVertex>();
80 0 : aaPos[v[i]].x() = atof(paramVec[i*3].c_str());
81 0 : aaPos[v[i]].y() = atof(paramVec[i*3 + 1].c_str());
82 0 : aaPos[v[i]].z() = atof(paramVec[i*3 + 2].c_str());
83 : }
84 : // create a triangle
85 0 : grid.create<Triangle>(TriangleDescriptor(v[0], v[1], v[2]));
86 : }
87 : else
88 : {
89 : // this line can't be interpreted correctly:
90 : bSuccess = false;
91 : break;
92 : }
93 : }
94 0 : lineCount++;
95 : }
96 :
97 0 : delete[] BUFFER;
98 :
99 0 : return bSuccess;
100 0 : }
101 :
102 0 : static bool ReadTetrahedrons(Grid& grid, ifstream& in,
103 : Grid::VertexAttachmentAccessor<APosition>& aaPos,
104 : int& lineCount)
105 : {
106 0 : char* BUFFER = new char[512];
107 : vector<string> paramVec;
108 : bool bSuccess = true;
109 :
110 0 : while(!in.eof())
111 : {
112 : // read the line
113 0 : in.getline(BUFFER, 512);
114 0 : BUFFER[511] = 0;
115 :
116 : // split the parameters
117 0 : split_parameters(paramVec, BUFFER, " \r");
118 :
119 : // check if there are some at all
120 0 : if(!paramVec.empty())
121 : {
122 : // check if the line is a commentary
123 : string& str = paramVec[0];
124 0 : if(str.find('#') == 0){
125 : // theres nothing to do in here. Proceed by reading the next line.
126 : }
127 : // if the first parameter contains "end", we're done in here.
128 0 : else if(str.find("end") == 0){
129 : break;
130 : }
131 : // read the triangle
132 0 : else if(paramVec.size() == 12){
133 : // this line contains the coordinates of a triangle.
134 : // create vertices and assign the coordinates.
135 : RegularVertex* v[4];
136 0 : for(int i = 0; i < 4; ++i)
137 : {
138 0 : v[i] = *grid.create<RegularVertex>();
139 0 : aaPos[v[i]].x() = atof(paramVec[i*3].c_str());
140 0 : aaPos[v[i]].y() = atof(paramVec[i*3 + 1].c_str());
141 0 : aaPos[v[i]].z() = atof(paramVec[i*3 + 2].c_str());
142 : }
143 : // create a triangle
144 0 : grid.create<Tetrahedron>(TetrahedronDescriptor(v[0], v[1], v[2], v[3]));
145 : }
146 : else
147 : {
148 : // this line can't be interpreted correctly:
149 : bSuccess = false;
150 : break;
151 : }
152 : }
153 0 : lineCount++;
154 : }
155 :
156 0 : delete[] BUFFER;
157 :
158 0 : return bSuccess;
159 0 : }
160 :
161 0 : bool LoadGridFromDUMP(Grid& grid, const char* filename,
162 : ISubsetHandler* pSH, AVector3& aPos)
163 : {
164 : // open the file
165 0 : ifstream in(filename);
166 :
167 : if(!in)
168 : {
169 0 : LOG(" file not found: " << filename << "\n");
170 : return false;
171 : }
172 :
173 0 : if(!grid.has_vertex_attachment(aPos))
174 0 : grid.attach_to_vertices(aPos);
175 : Grid::VertexAttachmentAccessor<AVector3> aaPos(grid, aPos);
176 :
177 0 : char* BUFFER = new char[512];
178 : vector<string> paramVec;
179 : bool bSuccess = true;
180 0 : int lineCount = 0;
181 :
182 : int oldDefSI = -1;
183 :
184 0 : if(pSH){
185 : oldDefSI = pSH->get_default_subset_index();
186 0 : pSH->set_default_subset_index(0);
187 : }
188 :
189 0 : while(!in.eof())
190 : {
191 : // read the line
192 0 : in.getline(BUFFER, 512);
193 0 : BUFFER[511] = 0;
194 :
195 : // split the parameters
196 0 : split_parameters(paramVec, BUFFER, " \r");
197 :
198 : // check if there are some at all
199 0 : if(!paramVec.empty())
200 : {
201 : // check if the line is a commentary
202 : string& str = paramVec[0];
203 0 : if(str.find('#') == 0)
204 : {
205 : // theres nothing to do in here. Proceed by reading the next line.
206 : }
207 : // check whether a new block starts
208 0 : else if(str.find("begin") == 0)
209 : {
210 0 : if(paramVec.size() < 2){
211 0 : LOG(" PROBLEM while reading from " << filename << ":\n");
212 0 : LOG(" missing block type specifier in line " << lineCount << endl);
213 0 : continue;
214 : }
215 :
216 0 : if(paramVec.size() > 2){
217 0 : if(pSH)
218 0 : pSH->set_default_subset_index(atoi(paramVec[2].c_str()));
219 : }
220 :
221 : // check second paramenter
222 : string& type = paramVec[1];
223 :
224 0 : if(type.find("triangles") == 0){
225 0 : if(!ReadTriangles(grid, in, aaPos, lineCount)){
226 0 : LOG(" PROBLEM while reading from " << filename << ":\n");
227 0 : LOG(" ReadTriangles failed in line " << lineCount << endl);
228 : bSuccess = false;
229 : }
230 : }
231 0 : else if(type.find("tetrahedrons") == 0){
232 0 : if(!ReadTetrahedrons(grid, in ,aaPos, lineCount)){
233 0 : LOG(" PROBLEM while reading from " << filename << ":\n");
234 0 : LOG(" ReadTetrahedrons failed in line " << lineCount << endl);
235 : bSuccess = false;
236 : }
237 : }
238 : else{
239 0 : LOG(" PROBLEM while reading from " << filename << ":\n");
240 0 : LOG(" unknown block type specifier in line " << lineCount << endl);
241 : bSuccess = false;
242 : }
243 : }
244 : }
245 0 : lineCount++;
246 : }
247 :
248 0 : delete[] BUFFER;
249 :
250 0 : if(pSH){
251 0 : pSH->set_default_subset_index(oldDefSI);
252 : }
253 :
254 : return bSuccess;
255 0 : }
256 :
257 : }
|