Nektar++
Loading...
Searching...
No Matches
CADSystem.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: CADSystem.h
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#ifndef NEKTAR_SPATIALDOMAINS_CADSYSTEM_CADSYSTEM
36#define NEKTAR_SPATIALDOMAINS_CADSYSTEM_CADSYSTEM
37
39
41
43
45
49
51
52#include "CADObject.h"
53
55{
56
57// forward declorators
58class CADVert;
59typedef std::shared_ptr<CADVert> CADVertSharedPtr;
60class CADCurve;
61typedef std::shared_ptr<CADCurve> CADCurveSharedPtr;
62class CADSurf;
63typedef std::shared_ptr<CADSurf> CADSurfSharedPtr;
64
65/**
66 * @brief Base class for CAD interface system.
67 *
68 * A class which can load and interact with CAD for Nektar++.
69 * This class contains maps to subclasses surface and curves.
70 */
72{
73public:
74 friend class MemoryManager<CADSystem>;
75
76 /**
77 * @brief Default constructor.
78 */
79 CADSystem(std::string name, std::string engine)
80 : m_name(name), m_engine(engine)
81 {
82 }
83
84 virtual ~CADSystem()
85 {
86 }
87
88 /**
89 * @brief Return the name of the CAD file.
90 */
91 std::string GetName()
92 {
93 return m_name;
94 }
95
96 /**
97 * @brief Set the CAD engine up to handle 2D geometries explicitly.
98 */
99 void Set2D()
100 {
101 m_2d = true;
102 }
103
104 /**
105 * @brief Returns whether the CAD engine is set in 2D mode.
106 */
107 bool Is2D()
108 {
109 return m_2d;
110 }
111
112 /**
113 * @brief Gets a configuration option.
114 *
115 * @param key Configuration key.
116 *
117 * @return The configuration value.
118 */
119 const std::string &GetConfig(const std::string &key) const
120 {
121 auto it = m_config.find(key);
122 return it->second;
123 }
124
125 /**
126 * @brief Sets a configuration option @p key to @p value.
127 *
128 * @param key Configuration key.
129 * @param value The configuration value.
130 */
131 void SetConfig(const std::string &key, const std::string &value)
132 {
133 m_config[key] = value;
134 }
135
136 /**
137 * @brief Initialises CAD and makes surface, curve and vertex maps.
138 *
139 * @return true if completed successfully
140 */
141 SPATIAL_DOMAINS_EXPORT virtual bool LoadCAD() = 0;
142
143 /**
144 * @brief Returns bounding box of the domain.
145 *
146 * Gets the bounding box of the domain by considering the start and end
147 * points of each curve in the geometry.
148 *
149 * @return Array with 6 entries: xmin, xmax, ymin, ymax, zmin and zmax.
150 */
151 SPATIAL_DOMAINS_EXPORT virtual std::array<NekDouble, 6> GetBoundingBox() = 0;
152
153 /**
154 * @brief Get the number of surfaces.
155 */
157 {
158 return m_surfs.size();
159 }
160
161 /**
162 * @brief Get the number of curves.
163 */
165 {
166 return m_curves.size();
167 }
168
169 /**
170 * @brief Gets a curve from the map.
171 */
173 {
174 auto search = m_curves.find(i);
175 ASSERTL1(search != m_curves.end(), "curve does not exist");
176
177 return search->second;
178 }
179
180 /**
181 * @brief Gets a surface from the map.
182 */
184 {
185 auto search = m_surfs.find(i);
186 ASSERTL1(search != m_surfs.end(), "surface does not exist");
187
188 return search->second;
189 }
190
191 /**
192 * @brief Gets a vert from the map.
193 */
195 {
196 auto search = m_verts.find(i);
197 ASSERTL1(search != m_verts.end(), "vert does not exist");
198
199 return search->second;
200 }
201
202 /**
203 * @brief Gets map of all vertices.
204 */
205 std::map<int, CADVertSharedPtr> GetVerts()
206 {
207 return m_verts;
208 }
209
210 /**
211 * @brief Gets number of vertices
212 */
213 int GetNumVerts() const
214 {
215 return m_verts.size();
216 }
217
218 /**
219 * @brief Gets void points for tetrahedral meshing.
220 */
221 std::vector<std::array<NekDouble, 3>> GetVoidPoints()
222 {
223 return m_voidPoints;
224 }
225
226 /**
227 * @brief Sets void points for tetrahedral meshing.
228 */
229 void SetVoidPoints(const std::vector<std::array<NekDouble, 3>> &voidPts)
230 {
231 m_voidPoints = voidPts;
232 }
233
234 /**
235 * @brief Return the vector of translation from one curve to another to
236 * allow for periodic mesh generation in 2D.
237 */
239 int first, int second);
240
241 /**
242 * @brief Return the name of the CAD system engine (e.g. OCE).
243 */
244 std::string GetEngine() const
245 {
246 return m_engine;
247 }
248
250 {
251 m_log = log;
252 m_log.SetPrefix("CADSystem");
253 }
254
255 /**
256 * @brief Fail unless every curve is bounded by exactly two surfaces.
257 *
258 * Anything else is not a closed manifold solid and the surface mesher has
259 * no closed boundary to work from. This is the condition the sewing in
260 * the OpenCASCADE engine exists to establish, so when it fails the sewing
261 * tolerance is the first thing to look at: a curve bounded by one surface
262 * is a seam that did not close, and one bounded by three or more is a
263 * genuine non-manifold join in the model.
264 */
266 {
267 std::stringstream ss;
268 int nBad = 0;
269
270 const int maxReported = 10;
271
272 for (auto &[id, curve] : m_curves)
273 {
274 size_t nAdj = curve->GetAdjSurf().size();
275 if (nAdj == 2)
276 {
277 continue;
278 }
279
280 // Report where, because a curve ID on its own cannot be looked up
281 // in a CAD package, and which surfaces, because that is what
282 // names the feature.
283 if (++nBad <= maxReported)
284 {
285 auto mm = curve->GetMinMax();
286 ss << std::endl
287 << " curve " << id << ", (" << mm[0] << ", " << mm[1]
288 << ", " << mm[2] << ") -> (" << mm[3] << ", " << mm[4]
289 << ", " << mm[5] << "), bounded by " << nAdj << " surface"
290 << (nAdj == 1 ? "" : "s");
291
292 for (auto &adj : curve->GetAdjSurf())
293 {
294 ss << " " << adj.first.lock()->GetId();
295 }
296 }
297 }
298
299 if (nBad > maxReported)
300 {
301 ss << std::endl << " ... and " << nBad - maxReported << " more.";
302 }
303
304 if (nBad > 0)
305 {
306 m_log(FATAL) << "the CAD model is not a closed solid: " << nBad
307 << " of its " << m_curves.size()
308 << " curves are not bounded by exactly two surfaces, "
309 "so the surface mesher has no closed boundary to "
310 "work from."
311 << ss.str() << std::endl;
312 }
313 }
314
315protected:
316 /// Name of CAD file
317 std::string m_name;
318 /// Name of CAD engine.
319 std::string m_engine;
320 /// Map of curves
321 std::map<int, CADCurveSharedPtr> m_curves;
322 /// Map of surfaces
323 std::map<int, CADSurfSharedPtr> m_surfs;
324 /// Map of vertices
325 std::map<int, CADVertSharedPtr> m_verts;
326 /// 2D cad flag
327 bool m_2d = false;
328 /// Configuration options which might be used per-engine, serialised in
329 /// strings.
330 std::map<std::string, std::string> m_config;
331 /// Points contained within volume voids for tetrahedralisation
332 std::vector<std::array<NekDouble, 3>> m_voidPoints;
333 /// Logger object.
335
336 /**
337 * @brief Reports basic properties to screen.
338 */
339 void Report()
340 {
341 m_log(VERBOSE) << "Using CAD engine: '" << m_engine << "'" << std::endl;
342 m_log(VERBOSE) << " - CAD has: " << m_verts.size() << " verts."
343 << std::endl;
344 m_log(VERBOSE) << " - CAD has: " << m_curves.size() << " curves."
345 << std::endl;
346 m_log(VERBOSE) << " - CAD has: " << m_surfs.size() << " surfaces."
347 << std::endl;
348
349 // The extent is in mesh units, and is the quickest way to see that a
350 // model has come in at the wrong scale: it is the same number the
351 // user sizes MinDelta and MaxDelta against, so if it does not look
352 // like the part they drew, nothing downstream will either.
353 auto bb = GetBoundingBox();
354 m_log(VERBOSE) << " - CAD extent: " << bb[1] - bb[0] << " x "
355 << bb[3] - bb[2] << " x " << bb[5] - bb[4]
356 << " (mesh units)" << std::endl;
357 }
358};
359
360typedef std::shared_ptr<CADSystem> CADSystemSharedPtr;
363
365} // namespace Nektar::SpatialDomains
366
367#endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
#define SPATIAL_DOMAINS_EXPORT
Basic logging class for the NekMesh library.
Definition Log.hpp:218
void SetPrefix(const std::string &prefix)
Sets the prefix for this Logger that is automatically preprended to all messages.
Definition Log.hpp:425
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
Base class for CAD interface system.
Definition CADSystem.h:72
CADCurveSharedPtr GetCurve(int i)
Gets a curve from the map.
Definition CADSystem.h:172
const std::string & GetConfig(const std::string &key) const
Gets a configuration option.
Definition CADSystem.h:119
std::string GetEngine() const
Return the name of the CAD system engine (e.g. OCE).
Definition CADSystem.h:244
std::vector< std::array< NekDouble, 3 > > m_voidPoints
Points contained within volume voids for tetrahedralisation.
Definition CADSystem.h:332
std::map< int, CADSurfSharedPtr > m_surfs
Map of surfaces.
Definition CADSystem.h:323
std::map< std::string, std::string > m_config
Configuration options which might be used per-engine, serialised in strings.
Definition CADSystem.h:330
void SetVoidPoints(const std::vector< std::array< NekDouble, 3 > > &voidPts)
Sets void points for tetrahedral meshing.
Definition CADSystem.h:229
std::string GetName()
Return the name of the CAD file.
Definition CADSystem.h:91
int GetNumVerts() const
Gets number of vertices.
Definition CADSystem.h:213
void Set2D()
Set the CAD engine up to handle 2D geometries explicitly.
Definition CADSystem.h:99
std::vector< std::array< NekDouble, 3 > > GetVoidPoints()
Gets void points for tetrahedral meshing.
Definition CADSystem.h:221
Logger m_log
Logger object.
Definition CADSystem.h:334
std::map< int, CADVertSharedPtr > GetVerts()
Gets map of all vertices.
Definition CADSystem.h:205
std::map< int, CADVertSharedPtr > m_verts
Map of vertices.
Definition CADSystem.h:325
CADVertSharedPtr GetVert(int i)
Gets a vert from the map.
Definition CADSystem.h:194
CADSurfSharedPtr GetSurf(int i)
Gets a surface from the map.
Definition CADSystem.h:183
virtual bool LoadCAD()=0
Initialises CAD and makes surface, curve and vertex maps.
int GetNumCurve()
Get the number of curves.
Definition CADSystem.h:164
bool Is2D()
Returns whether the CAD engine is set in 2D mode.
Definition CADSystem.h:107
std::map< int, CADCurveSharedPtr > m_curves
Map of curves.
Definition CADSystem.h:321
void Report()
Reports basic properties to screen.
Definition CADSystem.h:339
std::string m_engine
Name of CAD engine.
Definition CADSystem.h:319
void SetConfig(const std::string &key, const std::string &value)
Sets a configuration option key to value.
Definition CADSystem.h:131
CADSystem(std::string name, std::string engine)
Default constructor.
Definition CADSystem.h:79
void CheckClosed()
Fail unless every curve is bounded by exactly two surfaces.
Definition CADSystem.h:265
std::string m_name
Name of CAD file.
Definition CADSystem.h:317
int GetNumSurf()
Get the number of surfaces.
Definition CADSystem.h:156
virtual std::array< NekDouble, 6 > GetBoundingBox()=0
Returns bounding box of the domain.
std::array< NekDouble, 3 > GetPeriodicTranslationVector(int first, int second)
Return the vector of translation from one curve to another to allow for periodic mesh generation in 2...
Definition CADSystem.cpp:65
std::shared_ptr< CADCurve > CADCurveSharedPtr
Definition CADCurve.h:247
std::shared_ptr< CADVert > CADVertSharedPtr
Definition CADCurve.h:49
EngineFactory & GetEngineFactory()
Definition CADSystem.cpp:42
std::shared_ptr< CADSystem > CADSystemSharedPtr
Definition CADSystem.h:360
std::shared_ptr< CADSurf > CADSurfSharedPtr
Definition CADCurve.h:51
LibUtilities::NekFactory< std::string, CADSystem, std::string > EngineFactory
Definition CADSystem.h:362
@ FATAL
Fatal errors that trigger exceptions.
Definition Log.hpp:65
@ VERBOSE
Verbose output.
Definition Log.hpp:68
scalarT< T > log(scalarT< T > in)
Definition scalar.hpp:309