Nektar++
Loading...
Searching...
No Matches
CADSystemCFI.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: CADSystemCFI.cpp
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: cad object methods.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include "CADSystemCFI.h"
36#include "CADCurveCFI.h"
37#include "CADSurfCFI.h"
38#include "CADVertCFI.h"
39
40#include <boost/lexical_cast.hpp>
41
42using namespace std;
43
44namespace Nektar
45{
46namespace NekMesh
47{
48
50 "cfi", CADSystemCFI::create, "Uses CFI as cad engine");
51
53{
54 // it is possible to get CFI to lock on to a open gui session
55 // not sure it ever will with this code
56 m_cfiHandle.startServer();
57
58 m_log(VERBOSE) << " - CFI engine loaded in mode: ";
59 if (m_cfiHandle.info.mode == cfi::MODE_STANDALONE)
60 {
61 m_log(VERBOSE) << "standalone" << endl;
62 }
63 else if (m_cfiHandle.info.mode == cfi::MODE_CLIENT)
64 {
65 m_log(VERBOSE) << "client" << endl;
66 }
67 else if (m_cfiHandle.info.mode == cfi::MODE_SERVER)
68 {
69 m_log(VERBOSE) << "server" << endl;
70 }
71 else if (m_cfiHandle.info.mode == cfi::MODE_BOTH)
72 {
73 m_log(VERBOSE) << "both" << endl;
74 }
75 else if (m_cfiHandle.info.mode == cfi::MODE_PLUGIN)
76 {
77 m_log(VERBOSE) << "plugin" << endl;
78 }
79 else
80 {
81 m_log(VERBOSE) << "unknown" << endl;
82 }
83
84 m_log(VERBOSE) << " - CFI Version: " << m_cfiHandle.info.version << endl;
85 m_log(VERBOSE) << " - Fix no : " << m_cfiHandle.info.fixno << endl;
86 m_log(VERBOSE) << " - ubid : " << m_cfiHandle.info.ubid << endl;
87
88 if (m_config.count("UseCFIMesh"))
89 {
90 m_useCFIMesh = boost::lexical_cast<bool>(m_config["UseCFIMesh"]);
91 }
92
93 m_model = m_cfiHandle.openModelFile(m_name.c_str());
94
95 if (m_model->getEntityTotal(cfi::TYPE_BODY, cfi::SUBTYPE_ALL) != 1)
96 {
97 if (m_useCFIMesh)
98 {
99 m_log(VERBOSE) << "Will extract mesh and have multibodies" << endl;
100 }
101 else
102 {
103 m_log(FATAL) << "Has multibodies and instructions to mesh, "
104 << "this is not possible" << endl;
105 }
106 }
107
108 vector<cfi::Entity *> *bds =
109 m_model->getEntityList(cfi::TYPE_BODY, cfi::SUBTYPE_ALL);
110
111 for (auto &i : *bds)
112 {
113 cfi::Body *b = static_cast<cfi::Body *>(i);
114 m_bodies.push_back(b);
115 }
116
117 // cfi doesnt mind stupid units so this scales everything back to meters
118 // which is what nekmesh assumes its in
119 // the m_scal object is passed to all cad entities and scales any operation
120 // before running it.
121 m_scal = 1.0;
122 if (m_model->getUnits() == cfi::UNIT_INCHES)
123 {
124 m_log(VERBOSE) << " - Model is in inches, scaling accordingly" << endl;
125 m_scal = 0.0254;
126 }
127 else if (m_model->getUnits() == cfi::UNIT_MILLIMETERS ||
128 m_model->getUnits() == cfi::UNIT_MILLIMETRES)
129 {
130 m_log(VERBOSE) << " - Model is in mm, scaling accordingly" << endl;
131 m_scal = 1e-3;
132 }
133
134 // CFI does everything by string identifers
135 // currently nekmesh cad system uses integer ids.
136 // it really should use strings but doesnt currently
137 map<string, cfi::Point *> mapOfVerts;
138 map<string, cfi::Line *> mapOfEdges;
139 map<string, cfi::Face *> mapOfFaces;
140
141 // nothing is unique in cfi, there can list of verts that arnt used
142 // or are listed twice. This block gets all the real unique vertices in the
143 // cad by cascading down from the faces
144 // also builds a list on unique edges in the process
145
146 for (int i = 0; i < m_bodies.size(); i++)
147 {
148 // check that it is not a group of bodies
149 if (m_bodies[i]->getTopoSubtype() == cfi::SUBTYPE_COMBINED)
150 {
151 continue;
152 }
153
154 vector<cfi::Oriented<cfi::TopoEntity *>> *faceList =
155 m_bodies[i]->getChildList();
156
157 vector<cfi::Oriented<cfi::TopoEntity *>>::iterator it, it2, it3;
158 for (it = faceList->begin(); it != faceList->end(); it++)
159 {
160 cfi::Oriented<cfi::TopoEntity *> orientatedFace = *it;
161 cfi::Face *face = static_cast<cfi::Face *>(orientatedFace.entity);
162
163 try
164 {
165 boost::optional<cfi::Oriented<cfi::Surface *>> surf =
166 face->getTopoEmbedding();
167 }
168 catch (std::exception &e)
169 {
170 // dont want this face
171 continue;
172 }
173
174 auto it = mapOfFaces.find(face->getName());
175 if (it == mapOfFaces.end())
176 {
177 vector<cfi::Oriented<cfi::TopoEntity *>> *edgeList =
178 face->getChildList();
179
180 // unrolling combined edges
181 vector<cfi::Oriented<cfi::TopoEntity *>> fullEdgeList;
182 for (it2 = edgeList->begin(); it2 != edgeList->end(); it2++)
183 {
184 cfi::Line *edge = static_cast<cfi::Line *>(it2->entity);
185
186 if (edge->getTopoSubtype() == cfi::SUBTYPE_COMBINED)
187 {
188 vector<cfi::Oriented<cfi::TopoEntity *>> *subEdgeList =
189 edge->getChildList();
190
191 for (it3 = subEdgeList->begin();
192 it3 != subEdgeList->end(); it3++)
193 {
194 fullEdgeList.push_back(*it3);
195 }
196 }
197 else
198 {
199 fullEdgeList.push_back(*it2);
200 }
201 }
202
203 delete edgeList;
204
205 for (it2 = fullEdgeList.begin(); it2 != fullEdgeList.end();
206 it2++)
207 {
208 cfi::Oriented<cfi::TopoEntity *> orientatedEdge = *it2;
209 cfi::Line *edge =
210 static_cast<cfi::Line *>(orientatedEdge.entity);
211 mapOfEdges[edge->getName()] = edge;
212
213 vector<cfi::Oriented<cfi::TopoEntity *>> *vertList =
214 edge->getChildList();
215 for (it3 = vertList->begin(); it3 != vertList->end(); it3++)
216 {
217 cfi::Oriented<cfi::TopoEntity *> orientatedVert = *it3;
218 cfi::Point *vert =
219 static_cast<cfi::Point *>(orientatedVert.entity);
220 mapOfVerts[vert->getName()] = vert;
221 m_mapVertToListEdge[vert->getName()].push_back(
222 edge->getName());
223 }
224 delete vertList;
225 }
226
227 mapOfFaces[face->getName()] = face;
228 }
229 }
230 delete faceList;
231 }
232
233 // make the vertices and build a map of name to id
234 map<string, cfi::Point *>::iterator vit;
235 int i = 1; // from one to be consistent with oce
236 for (vit = mapOfVerts.begin(); vit != mapOfVerts.end(); vit++, i++)
237 {
238 AddVert(i, vit->second);
239 m_nameToVertId[vit->second->getName()] = i;
240 }
241
242 // build curves
243 map<string, cfi::Line *>::iterator eit;
244 i = 1;
245 for (eit = mapOfEdges.begin(); eit != mapOfEdges.end(); eit++, i++)
246 {
247 AddCurve(i, eit->second);
248 m_nameToCurveId[eit->second->getName()] = i;
249 }
250
251 // build surfaces
252 map<string, cfi::Face *>::iterator fit;
253 i = 1;
254 for (fit = mapOfFaces.begin(); fit != mapOfFaces.end(); fit++, i++)
255 {
256 m_nameToFaceId[fit->second->getName()] = i;
257
258 AddSurf(i, fit->second);
259 }
260
261 // TODO identify Degenerated faces and setdegen on vertices accordinaly
262
263 if (!m_2d && !m_useCFIMesh)
264 {
265 CheckClosed();
266 }
267
268 Report();
269
270 // Tidy up
271 delete bds;
272
273 return true;
274}
275
276void CADSystemCFI::AddVert(int i, cfi::Point *in)
277{
278 CADVertSharedPtr newVert = GetCADVertFactory().CreateInstance(key);
279 newVert->SetLogger(m_log);
280 static_pointer_cast<CADVertCFI>(newVert)->Initialise(i, in, m_scal);
281
282 m_verts[i] = newVert;
283 m_verts[i]->SetName(in->getName());
284}
285
286void CADSystemCFI::AddCurve(int i, cfi::Line *in)
287{
288 CADCurveSharedPtr newCurve = GetCADCurveFactory().CreateInstance(key);
289 newCurve->SetLogger(m_log);
290 static_pointer_cast<CADCurveCFI>(newCurve)->Initialise(i, in, m_scal);
291
292 vector<cfi::Oriented<cfi::TopoEntity *>> *vertList = in->getChildList();
293
294 ASSERTL0(vertList->size() == 2, "should be two ends");
295
296 vector<cfi::Oriented<cfi::TopoEntity *>>::iterator it;
297 vector<NekDouble> t;
298 for (it = vertList->begin(); it != vertList->end(); it++)
299 {
300 cfi::Oriented<cfi::TopoEntity *> orientatedVert = *it;
301 cfi::Point *vert = static_cast<cfi::Point *>(orientatedVert.entity);
302 boost::optional<cfi::Projected<double>> pj =
303 in->calcTFromXYZ(vert->getGeometry(), -1);
304 t.push_back(pj.value().parameters);
305 }
306 ASSERTL0(t[0] < t[1], "weirdness");
307
308 vector<CADVertSharedPtr> vs;
309 vs.push_back(m_verts[m_nameToVertId[vertList->at(0).entity->getName()]]);
310 vs.push_back(m_verts[m_nameToVertId[vertList->at(1).entity->getName()]]);
311 m_curves[i] = newCurve;
312 m_curves[i]->SetVert(vs);
313 m_curves[i]->SetName(in->getName());
314 m_verts[m_nameToVertId[vertList->at(0).entity->getName()]]->AddAdjCurve(
315 m_curves[i]);
316 m_verts[m_nameToVertId[vertList->at(1).entity->getName()]]->AddAdjCurve(
317 m_curves[i]);
318 delete vertList;
319}
320
321void CADSystemCFI::AddSurf(int i, cfi::Face *in)
322{
323 CADSurfSharedPtr newSurf = GetCADSurfFactory().CreateInstance(key);
324 newSurf->SetLogger(m_log);
325 static_pointer_cast<CADSurfCFI>(newSurf)->Initialise(i, in, m_scal);
326
327 vector<cfi::Oriented<cfi::TopoEntity *>> *edgeList = in->getChildList();
328
329 // unrolling combined edges
330 vector<cfi::Oriented<cfi::TopoEntity *>> fullEdgeList;
331 vector<cfi::Oriented<cfi::TopoEntity *>>::iterator it2, it3;
332 for (it2 = edgeList->begin(); it2 != edgeList->end(); it2++)
333 {
334 cfi::Line *edge = static_cast<cfi::Line *>(it2->entity);
335
336 if (edge->getTopoSubtype() == cfi::SUBTYPE_COMBINED)
337 {
338 vector<cfi::Oriented<cfi::TopoEntity *>> *subEdgeList =
339 edge->getChildList();
340
341 for (it3 = subEdgeList->begin(); it3 != subEdgeList->end(); it3++)
342 {
343 fullEdgeList.push_back(*it3);
344 }
345
346 if (it2->orientation == cfi::ORIENT_NEGATIVE)
347 {
348 reverse(fullEdgeList.begin() + fullEdgeList.size() -
349 subEdgeList->size(),
350 fullEdgeList.end());
351 }
352 }
353 else
354 {
355 fullEdgeList.push_back(*it2);
356 }
357 }
358 delete edgeList;
359
360 vector<EdgeLoopSharedPtr> edgeloops;
361 int done = 0;
362 while (done != fullEdgeList.size())
363 {
364 EdgeLoopSharedPtr edgeloop = EdgeLoopSharedPtr(new EdgeLoop);
365 string firstVert;
366 vector<cfi::Oriented<cfi::TopoEntity *>> *vertList =
367 fullEdgeList.at(done).entity->getChildList();
368 if (fullEdgeList.at(done).orientation == cfi::ORIENT_POSITIVE)
369 {
370 firstVert = vertList->at(0).entity->getName();
371 edgeloop->edgeo.push_back(CADOrientation::eForwards);
372 }
373 else
374 {
375 firstVert = vertList->at(1).entity->getName();
376 edgeloop->edgeo.push_back(CADOrientation::eBackwards);
377 }
378
379 delete vertList;
380
381 edgeloop->edges.push_back(
382 m_curves[m_nameToCurveId[fullEdgeList.at(done).entity->getName()]]);
383
384 for (done++; done < fullEdgeList.size(); done++)
385 {
386 bool end = false;
387 vertList = fullEdgeList.at(done).entity->getChildList();
388 if (fullEdgeList.at(done).orientation == cfi::ORIENT_POSITIVE)
389 {
390 if (vertList->at(1).entity->getName() == firstVert)
391 {
392 end = true;
393 }
394 edgeloop->edgeo.push_back(CADOrientation::eForwards);
395 }
396 else
397 {
398 if (vertList->at(0).entity->getName() == firstVert)
399 {
400 end = true;
401 }
402 edgeloop->edgeo.push_back(CADOrientation::eBackwards);
403 }
404
405 delete vertList;
406
407 edgeloop->edges.push_back(
408 m_curves
409 [m_nameToCurveId[fullEdgeList.at(done).entity->getName()]]);
410
411 if (end)
412 {
413 done++;
414 break;
415 }
416 }
417 edgeloops.push_back(edgeloop);
418 }
419
420 // TODO find if surface has reversed normal or not
421
422 int tote = 0;
423 for (int k = 0; k < edgeloops.size(); k++)
424 {
425 tote += edgeloops[k]->edges.size();
426 }
427
428 ASSERTL0(tote != 1, "cannot handle periodic curves");
429
430 CADSurf::OrientateEdges(newSurf, edgeloops);
431 newSurf->SetEdges(edgeloops);
432
433 // now the loops are orientated, tell the curves how they are
434 for (int k = 0; k < edgeloops.size(); k++)
435 {
436 for (int j = 0; j < edgeloops[k]->edges.size(); j++)
437 {
438 edgeloops[k]->edges[j]->SetAdjSurf(
439 make_pair(newSurf, edgeloops[k]->edgeo[j]));
440 }
441 }
442
443 m_surfs[i] = newSurf;
444 m_surfs[i]->SetName(in->getName());
445}
446
447std::array<NekDouble, 6> CADSystemCFI::GetBoundingBox()
448{
449 cfi::BoundingBox box = m_model->calcBoundingBox();
450
451 return {box.xLower, box.xUpper, box.yLower,
452 box.yUpper, box.zLower, box.zUpper};
453}
454} // namespace NekMesh
455} // namespace Nektar
#define ASSERTL0(condition, msg)
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
std::map< std::string, std::vector< std::string > > m_mapVertToListEdge
void AddVert(int i, cfi::Point *in)
std::map< std::string, int > m_nameToVertId
static CADSystemSharedPtr create(std::string name)
void AddCurve(int i, cfi::Line *in)
void AddSurf(int i, cfi::Face *in)
std::array< NekDouble, 6 > GetBoundingBox()
std::map< std::string, int > m_nameToCurveId
std::map< std::string, int > m_nameToFaceId
std::vector< cfi::Body * > m_bodies
EngineFactory & GetEngineFactory()
Definition CADSystem.cpp:42
@ FATAL
Fatal errors that trigger exceptions.
Definition Log.hpp:65
@ VERBOSE
Verbose output.
Definition Log.hpp:68
STL namespace.