Line data Source code
1 : /*
2 : * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 : * Authors: Sebastian Reiter, Nicolas Tessore
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 "file_io_lgm.h"
34 :
35 : #include <vector>
36 :
37 : #include "lib_grid/lg_base.h"
38 : #include "common/math/ugmath.h"
39 :
40 : extern "C" {
41 : #include <lib_grid/file_io/externals/include/lgm/lgm.h>
42 : }
43 :
44 : using namespace std;
45 :
46 : namespace ug
47 : {
48 :
49 : ////////////////////////////////////////////////////////////////////////
50 : // ImportGridFromLGM
51 0 : bool ImportGridFromLGM(Grid& grid,
52 : const char* filename,
53 : AVector3& aPos,
54 : ISubsetHandler* pSurfaceHandler)
55 : {
56 : // we'll read the lgm in two steps:
57 : // first we'll try to load a 3d lgm. If that fails we'll try to
58 : // load a 2d on. If that fails too, we'll give up.
59 :
60 : // create lgm object
61 0 : lgm* l = lgm_new();
62 : // read lgm file
63 0 : lgm_info* linfo = lgm_info_new();
64 :
65 : // load 3d
66 0 : if(lgm_read(filename, l, linfo))
67 : {/*
68 : LOG("WARNING in ImportGridFromLGM: " << linfo->err_msg << endl);
69 : lgm_delete(l);
70 : lgm_info_delete(linfo);
71 : return false;*/
72 :
73 : // 3d could not be loaded. Try 2d.
74 : //TODO: add full 2d support to lgm_parser. There are problems
75 : // with line left and line right (They are not yet parsed).
76 : // This leads to an error in the current implementation.
77 0 : lgm_info_delete(linfo);
78 0 : lgm_delete(l);
79 0 : l = lgm_new();
80 0 : l->dim = 2;
81 0 : linfo = lgm_info_new();
82 :
83 0 : if(lgm_read(filename, l, linfo)){
84 0 : LOG("WARNING in ImportGridFromLGM: " << linfo->err_msg << endl);
85 0 : lgm_info_delete(linfo);
86 0 : lgm_delete(l);
87 0 : return false;
88 : }
89 : }
90 0 : lgm_info_delete(linfo);
91 :
92 : // make sure lgm has dimension of 2 or 3
93 0 : if(l->dim != 2 && l->dim != 3)
94 : {
95 : LOG("WARNING in ImportGridFromLGM: "
96 : << "LGM is does not have dimension of 2 or 3!"
97 : << endl);
98 0 : lgm_delete(l);
99 0 : return false;
100 : }
101 :
102 : // set up vertex attachment
103 0 : if(!grid.has_vertex_attachment(aPos))
104 0 : grid.attach_to_vertices(aPos);
105 :
106 : // set up vertex attachment accessor
107 : Grid::VertexAttachmentAccessor<AVector3> aaPosition(grid, aPos);
108 :
109 : // read points and store them in an array for index access
110 : vector<RegularVertex*> vVertices;
111 0 : vVertices.reserve(l->num_points);
112 :
113 : // read points
114 0 : for(int i = 0; i < l->num_points; ++i)
115 : {
116 : // get point
117 0 : double* point = l->points[i];
118 :
119 : // create and store vertex
120 0 : RegularVertex* vert = *grid.create<RegularVertex>();
121 0 : vVertices.push_back(vert);
122 :
123 : // set vertex coordinates
124 0 : aaPosition[vert] = vector3(
125 : (number)point[0],
126 : (number)point[1],
127 0 : (l->dim == 3) ? (number)point[2] : (number)0.
128 : );
129 : }
130 :
131 : // read lines and store them in an array for index access
132 : vector<RegularEdge*> vEdges;
133 0 : vEdges.reserve(l->num_lines);
134 :
135 : // read lines
136 0 : for(int i = 0; i < l->num_lines; ++i)
137 : {
138 : // get line
139 0 : lgm_line* li = &l->lines[i];
140 :
141 : // scan through line nodes
142 0 : for(int j = 1; j < li->num_points; ++j)
143 : {
144 : // vertex indices
145 0 : int v1 = li->points[j-1];
146 0 : int v2 = li->points[j];
147 :
148 : // create edge
149 0 : RegularEdge* e = *grid.create<RegularEdge>(EdgeDescriptor(
150 0 : vVertices[v1],
151 0 : vVertices[v2]
152 0 : ));
153 :
154 : // add line to line subset
155 0 : if(pSurfaceHandler)
156 0 : pSurfaceHandler->assign_subset(e, i);
157 :
158 : // store edge
159 0 : vEdges.push_back(e);
160 : }
161 : }
162 :
163 : // read surfaces
164 0 : for(int i = 0; i < l->num_surfaces; ++i)
165 : {
166 : // get surface
167 0 : lgm_surface* s = &l->surfaces[i];
168 : /*
169 : // read points
170 : for(int j = 0; j < s->num_points; ++j)
171 : {
172 : // vertex index
173 : int v = s->points[j];
174 :
175 : // add vertex to surface subset
176 : if(pSurfaceHandler)
177 : pSurfaceHandler->assign_subset(vVertices[v], i);
178 : }
179 :
180 : // read lines
181 : for(int j = 0; j < s->num_lines; ++j)
182 : {
183 : // edge index
184 : int e = s->lines[j];
185 :
186 : // add edge to surface subset
187 : if(pSurfaceHandler)
188 : pSurfaceHandler->assign_subset(vEdges[e], i);
189 : }*/
190 :
191 : // read triangles
192 0 : for(int j = 0; j < s->num_triangles; ++j)
193 : {
194 : // vertex indices
195 0 : int v1 = s->triangles[j][0];
196 0 : int v2 = s->triangles[j][1];
197 0 : int v3 = s->triangles[j][2];
198 :
199 : // create triangle
200 0 : Triangle* t = *grid.create<Triangle>(TriangleDescriptor(
201 0 : vVertices[v1],
202 0 : vVertices[v2],
203 0 : vVertices[v3]
204 : ));
205 :
206 : // add triangle to surface subset
207 0 : if(pSurfaceHandler)
208 0 : pSurfaceHandler->assign_subset(t, i);
209 : }
210 : }
211 :
212 : // done importing!
213 :
214 : // delete lgm object
215 0 : lgm_delete(l);
216 :
217 : return true;
218 0 : }
219 :
220 : }// end of namespace
|