Nektar++
Loading...
Searching...
No Matches
CADSurfOCE.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: CADSurfOCE.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 surface methods.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <cmath>
36
37#include <BRep_Tool.hxx>
38#include <Geom2d_Curve.hxx>
39#include <GeomAPI_ProjectPointOnSurf.hxx>
44#include <TopoDS.hxx>
45
46using namespace std;
47
49{
50
52 "oce", CADSurfOCE::create, "CADSurfOCE");
53
54void CADSurfOCE::Initialise(int i, TopoDS_Shape in)
55{
56 m_s = BRep_Tool::Surface(TopoDS::Face(in));
57
58 // Test to see if this surface is a transfinite surface, since some OCC
59 // functions will not work on our custom type.
61 Handle(Geom_TransfiniteSurface)::DownCast(m_s);
62 m_isTransfiniteSurf = !tf.IsNull();
63
64 if (in.Orientation() == TopAbs_REVERSED)
65 {
67 }
68
69 m_id = i;
70
71 BRepTools::UVBounds(TopoDS::Face(in), m_bounds[0], m_bounds[1], m_bounds[2],
72 m_bounds[3]);
73 m_sas = new ShapeAnalysis_Surface(m_s);
74 m_sas->SetDomain(m_bounds[0], m_bounds[1], m_bounds[2], m_bounds[3]);
75
76 m_shape = in;
77
78 m_2Dclass = new BRepTopAdaptor_FClass2d(TopoDS::Face(m_shape), 1e-4);
79}
80
81std::array<NekDouble, 4> CADSurfOCE::GetBounds()
82{
83 return m_bounds;
84}
85
87 NekDouble &vmax)
88{
89 umin = m_bounds[0];
90 umax = m_bounds[1];
91 vmin = m_bounds[2];
92 vmax = m_bounds[3];
93}
94
96{
97 if (m_sas->Adaptor3d()->GetType() == GeomAbs_Plane)
98 {
99 return true;
100 }
101
102 return false;
103}
104
105std::array<NekDouble, 6> CADSurfOCE::BoundingBox()
106{
107 BRepMesh_IncrementalMesh brmsh(m_shape, 0.005);
108
109 Bnd_Box B;
110 BRepBndLib::Add(m_shape, B);
111 NekDouble e = sqrt(B.SquareExtent()) * 0.01;
112 e = min(e, 5e-3);
113 B.Enlarge(e);
114
115 std::array<NekDouble, 6> ret;
116 B.Get(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
117 for (auto &r : ret)
118 {
119 r = CADUnits::FromCAD(r);
120 }
121
122 return ret;
123}
124
125std::array<NekDouble, 6> CADSurfOCE::BoundingBox(NekDouble scale)
126{
127 BRepMesh_IncrementalMesh brmsh(m_shape, 0.005 * scale);
128
129 Bnd_Box B;
130 BRepBndLib::Add(m_shape, B);
131 NekDouble e = sqrt(B.SquareExtent()) * 0.01 * scale;
132 e = min(e, 5e-3 * scale);
133 B.Enlarge(e);
134
135 std::array<NekDouble, 6> ret;
136 B.Get(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
137 for (auto &r : ret)
138 {
139 r = CADUnits::FromCAD(r);
140 }
141
142 return ret;
143}
144
145std::array<NekDouble, 2> CADSurfOCE::locuv(std::array<NekDouble, 3> p,
146 NekDouble &dist)
147{
148 gp_Pnt loc = CADUnits::PntToCAD(p);
149 std::array<NekDouble, 2> uv;
150
152 {
153 gp_Pnt2d p2 = m_sas->ValueOfUV(loc, Precision::Confusion());
154
155 TopAbs_State s = m_2Dclass->Perform(p2);
156
157 if (s == TopAbs_OUT)
158 {
159 BRepBuilderAPI_MakeVertex v(loc);
160 BRepExtrema_DistShapeShape dss(
161 BRepTools::OuterWire(TopoDS::Face(m_shape)), v.Shape());
162 dss.Perform();
163 gp_Pnt np = dss.PointOnShape1(1);
164 p2 = m_sas->ValueOfUV(np, Precision::Confusion());
165 }
166
167 uv[0] = p2.X();
168 uv[1] = p2.Y();
169
170 gp_Pnt p3 = m_sas->Value(p2);
171
172 dist = CADUnits::FromCAD(p3.Distance(loc));
173 }
174 else
175 {
176 GeomAPI_ProjectPointOnSurf proj(loc, m_s, Precision::Confusion());
177 proj.Perform(loc);
178 ASSERTL1(proj.NbPoints() > 0, "Unable to find a projection!");
179 proj.LowerDistanceParameters(uv[0], uv[1]);
180 dist = CADUnits::FromCAD(proj.LowerDistance());
181 }
182
183 return uv;
184}
185
186std::array<NekDouble, 2> CADSurfOCE::locuv(std::array<NekDouble, 3> p,
187 NekDouble &dist, NekDouble Umin,
188 NekDouble Usup, NekDouble Vmin,
189 NekDouble Vsup)
190{
191 gp_Pnt loc = CADUnits::PntToCAD(p);
192 std::array<NekDouble, 2> uv;
193
195 {
196 gp_Pnt2d p2 = m_sas->ValueOfUV(loc, Precision::Confusion());
197
198 TopAbs_State s = m_2Dclass->Perform(p2);
199
200 if (s == TopAbs_OUT)
201 {
202 BRepBuilderAPI_MakeVertex v(loc);
203 BRepExtrema_DistShapeShape dss(
204 BRepTools::OuterWire(TopoDS::Face(m_shape)), v.Shape());
205 dss.Perform();
206 gp_Pnt np = dss.PointOnShape1(1);
207 p2 = m_sas->ValueOfUV(np, Precision::Confusion());
208 }
209
210 uv[0] = p2.X();
211 uv[1] = p2.Y();
212
213 gp_Pnt p3 = m_sas->Value(p2);
214
215 dist = CADUnits::FromCAD(p3.Distance(loc));
216 }
217 else
218 {
219 GeomAPI_ProjectPointOnSurf proj(loc, m_s, Umin, Usup, Vmin, Vsup,
220 Precision::Confusion(),
221 Extrema_ExtAlgo_Grad);
222 proj.Perform(loc);
223 ASSERTL1(proj.NbPoints() > 0, "Unable to find a projection!");
224 proj.LowerDistanceParameters(uv[0], uv[1]);
225 dist = CADUnits::FromCAD(proj.LowerDistance());
226 }
227
228 return uv;
229}
230
231NekDouble CADSurfOCE::Curvature(std::array<NekDouble, 2> uv)
232{
233#if defined(NEKTAR_DEBUG)
234 Test(uv);
235#endif
236
237 GeomLProp_SLProps d(m_s, 2, Precision::Confusion());
238 d.SetParameters(uv[0], uv[1]);
239
240 if (!d.IsCurvatureDefined())
241 {
242 return -1.0;
243 }
244
245 NekDouble d1 = std::abs(d.MaxCurvature());
246 NekDouble d2 = std::abs(d.MinCurvature());
247
248 return CADUnits::CurvatureFromCAD(d1 > d2 ? d1 : d2);
249}
250
251NekDouble CADSurfOCE::CurvatureAndPosition(std::array<NekDouble, 2> uv,
252 std::array<NekDouble, 3> &loc)
253{
254#if defined(NEKTAR_DEBUG)
255 Test(uv);
256#endif
257
258 // One evaluation for both: SLProps computes the surface's position along
259 // with the derivatives the curvature comes from, so asking it for the
260 // point costs nothing beyond the call already made.
261 GeomLProp_SLProps d(m_s, 2, Precision::Confusion());
262 d.SetParameters(uv[0], uv[1]);
263
264 loc = CADUnits::PntFromCAD(d.Value());
265
266 if (!d.IsCurvatureDefined())
267 {
268 return -1.0;
269 }
270
271 NekDouble d1 = std::abs(d.MaxCurvature());
272 NekDouble d2 = std::abs(d.MinCurvature());
273
274 return CADUnits::CurvatureFromCAD(d1 > d2 ? d1 : d2);
275}
276
277std::array<NekDouble, 2> CADSurfOCE::locuvFrom(std::array<NekDouble, 3> p,
278 std::array<NekDouble, 2> guess,
279 NekDouble &dist)
280{
281 // Only the plain projection takes a starting point; a transfinite
282 // surface goes the long way round as before.
284 {
285 return locuv(p, dist);
286 }
287
288 gp_Pnt loc = CADUnits::PntToCAD(p);
289 gp_Pnt2d p2 = m_sas->NextValueOfUV(gp_Pnt2d(guess[0], guess[1]), loc,
290 Precision::Confusion());
291
292 TopAbs_State state = m_2Dclass->Perform(p2);
293
294 if (state == TopAbs_OUT)
295 {
296 // Outside the face's own boundary, which means the guess has led the
297 // search somewhere it should not be. Fall back to the cold search,
298 // which handles this case by pulling the point onto the outer wire.
299 return locuv(p, dist);
300 }
301
302 gp_Pnt p3 = m_sas->Value(p2);
303 dist = CADUnits::FromCAD(p3.Distance(loc));
304
305 return {p2.X(), p2.Y()};
306}
307
308std::array<NekDouble, 2> CADSurfOCE::locuvOnCurve(
309 const CADCurveSharedPtr &curve, NekDouble t)
310{
311 // A bounding curve is stored twice by OpenCASCADE: once in space, and
312 // once as a curve in this surface's parameter plane. Reading the second
313 // gives the (u,v) exactly and immediately, where projecting the first
314 // back onto the surface is an iterative minimisation costing some
315 // hundreds of microseconds. On brakedisc that projection was 22 of the
316 // 34 seconds the whole mesh took.
317 auto oceCurve = std::dynamic_pointer_cast<CADCurveOCE>(curve);
318
319 if (oceCurve)
320 {
321 const TopoDS_Edge &edge = oceCurve->GetOccEdge();
322 TopoDS_Face face = TopoDS::Face(m_shape);
323
324 // A seam edge has two parameter-plane curves on this face, one for
325 // each side of the seam, and which of them comes back depends on the
326 // orientation the edge is asked for rather than on anything known
327 // here. Project in that case rather than pick the wrong one.
328 if (!BRep_Tool::IsClosed(edge, face))
329 {
330 Standard_Real first, last;
331 Handle(Geom2d_Curve) pcurve =
332 BRep_Tool::CurveOnSurface(edge, face, first, last);
333
334 if (!pcurve.IsNull())
335 {
336 gp_Pnt2d p = pcurve->Value(t);
337 return {p.X(), p.Y()};
338 }
339 }
340 }
341
342 return CADSurf::locuv(curve->P(t));
343}
344
345std::array<NekDouble, 3> CADSurfOCE::P(std::array<NekDouble, 2> uv)
346{
347#if defined(NEKTAR_DEBUG)
348 Test(uv);
349#endif
350
351 return CADUnits::PntFromCAD(m_s->Value(uv[0], uv[1]));
352}
353
354void CADSurfOCE::P(std::array<NekDouble, 2> uv, NekDouble &x, NekDouble &y,
355 NekDouble &z)
356{
357#if defined(NEKTAR_DEBUG)
358 Test(uv);
359#endif
360
361 auto loc = CADUnits::PntFromCAD(m_s->Value(uv[0], uv[1]));
362 x = loc[0];
363 y = loc[1];
364 z = loc[2];
365}
366
367std::array<NekDouble, 3> CADSurfOCE::N(std::array<NekDouble, 2> uv)
368{
369#if defined(NEKTAR_DEBUG)
370 Test(uv);
371#endif
372
373 GeomLProp_SLProps d(m_s, 2, Precision::Confusion());
374 d.SetParameters(uv[0], uv[1]);
375
376 std::array<NekDouble, 3> normal = {0.0, 0.0, 0.0};
377
378 if (!d.IsNormalDefined())
379 {
380 return normal;
381 }
382
383 gp_Dir n = d.Normal();
384
386 {
387 n.Reverse();
388 }
389
390 normal[0] = n.X();
391 normal[1] = n.Y();
392 normal[2] = n.Z();
393
394 return normal;
395}
396
397std::array<NekDouble, 9> CADSurfOCE::D1(std::array<NekDouble, 2> uv)
398{
399#if defined(NEKTAR_DEBUG)
400 Test(uv);
401#endif
402
403 std::array<NekDouble, 9> r;
404 gp_Pnt Loc;
405 gp_Vec D1U, D1V;
406 m_s->D1(uv[0], uv[1], Loc, D1U, D1V);
407
408 auto l = CADUnits::PntFromCAD(Loc);
409 auto du = CADUnits::VecFromCAD(D1U);
410 auto dv = CADUnits::VecFromCAD(D1V);
411
412 r = {l[0], l[1], l[2], // x, y, z
413 du[0], du[1], du[2], // dx/du, dy/du, dz/du
414 dv[0], dv[1], dv[2]};
415
416 return r;
417}
418
419std::array<NekDouble, 18> CADSurfOCE::D2(std::array<NekDouble, 2> uv)
420{
421#if defined(NEKTAR_DEBUG)
422 Test(uv);
423#endif
424
425 std::array<NekDouble, 18> r;
426 gp_Pnt Loc;
427 gp_Vec D1U, D1V, D2U, D2V, D2UV;
428 m_s->D2(uv[0], uv[1], Loc, D1U, D1V, D2U, D2V, D2UV);
429
430 auto l = CADUnits::PntFromCAD(Loc);
431 auto du = CADUnits::VecFromCAD(D1U);
432 auto dv = CADUnits::VecFromCAD(D1V);
433 auto duu = CADUnits::VecFromCAD(D2U);
434 auto dvv = CADUnits::VecFromCAD(D2V);
435 auto duv = CADUnits::VecFromCAD(D2UV);
436
437 r = {l[0], l[1], l[2], // x, y, z
438 du[0], du[1], du[2], // dx/du, dy/du, dz/du
439 dv[0], dv[1], dv[2], // dx/dv, dy/dv, dz/dv
440 duu[0], duu[1], duu[2], // d2x/du2, d2y/du2, d2z/du2
441 dvv[0], dvv[1], dvv[2], // d2x/dv2, d2y/dv2, d2z/dv2
442 duv[0], duv[1], duv[2]};
443
444 return r;
445}
446
447void CADSurfOCE::Test(std::array<NekDouble, 2> uv)
448{
449 stringstream error;
450
451 error << "Point not within parameter plane: " << endl;
452
453 bool passed = true;
454
455 if (uv[0] < m_bounds[0])
456 {
457 if (fabs(uv[0] - m_bounds[0]) > 1E-6)
458 {
459 error << "U(" << uv[0] << ") is less than Umin(" << m_bounds[0]
460 << ")";
461 passed = false;
462 }
463 }
464 else if (uv[0] > m_bounds[1])
465 {
466 if (fabs(uv[0] - m_bounds[1]) > 1E-6)
467 {
468 error << "U(" << uv[0] << ") is greater than Umax(" << m_bounds[1]
469 << ")";
470 passed = false;
471 }
472 }
473 else if (uv[1] < m_bounds[2])
474 {
475 if (fabs(uv[1] - m_bounds[2]) > 1E-6)
476 {
477 error << "V(" << uv[1] << ") is less than Vmin(" << m_bounds[2]
478 << ")";
479 passed = false;
480 }
481 }
482 else if (uv[1] > m_bounds[3])
483 {
484 if (fabs(uv[1] - m_bounds[3]) > 1E-6)
485 {
486 error << "V(" << uv[1] << ") is greater than Vmax(" << m_bounds[3]
487 << ")";
488 passed = false;
489 }
490 }
491
492 error << " On Surface: " << GetId();
493 WARNINGL1(passed, "Warning: " + error.str());
494 (void)passed; // suppress warning
495}
496
497} // namespace Nektar::SpatialDomains
#define WARNINGL1(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
A class that defines a transfinite surface defined by four curves.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
CADOrientation::Orientation m_orientation
orientation of the CADObject
Definition CADObject.h:150
int GetId()
Return ID of the CAD object.
Definition CADObject.h:85
virtual std::array< NekDouble, 2 > locuv(std::array< NekDouble, 3 > p, NekDouble &dist)=0
Performs a reverse look up to find u,v and x,y,z. if xyz is off the surface it will return the closes...
bool IsPlanar() override
Is the surface defined by a planar surface (i.e not nurbs and is flat)
void Test(std::array< NekDouble, 2 > uv) override
Function which tests the the value of uv used is within the surface.
Handle(Geom_Surface) m_s
OpenCascade object for surface.
std::array< NekDouble, 2 > locuv(std::array< NekDouble, 3 > p, NekDouble &dist) override
Performs a reverse look up to find u,v and x,y,z. if xyz is off the surface it will return the closes...
NekDouble Curvature(std::array< NekDouble, 2 > uv) override
returns curvature at point uv
TopoDS_Shape m_shape
original shape
Definition CADSurfOCE.h:103
std::array< NekDouble, 6 > BoundingBox() override
Returns the bounding box of the surface.
std::array< NekDouble, 4 > m_bounds
parametric bounds
Definition CADSurfOCE.h:99
void Initialise(int i, TopoDS_Shape in)
NekDouble CurvatureAndPosition(std::array< NekDouble, 2 > uv, std::array< NekDouble, 3 > &loc) override
std::array< NekDouble, 3 > P(std::array< NekDouble, 2 > uv) override
Get the x,y,z at parametric point u,v.
std::array< NekDouble, 2 > locuvOnCurve(const CADCurveSharedPtr &curve, NekDouble t) override
std::array< NekDouble, 3 > N(std::array< NekDouble, 2 > uv) override
Get the normal vector at parametric point u,v.
std::array< NekDouble, 18 > D2(std::array< NekDouble, 2 > uv) override
Get the set of second derivatives at parametric point u,v.
ShapeAnalysis_Surface * m_sas
locuv object (stored because it gets faster with stored information)
Definition CADSurfOCE.h:101
std::array< NekDouble, 9 > D1(std::array< NekDouble, 2 > uv) override
Get the set of first derivatives at parametric point u,v.
static CADSurfSharedPtr create()
Definition CADSurfOCE.h:47
bool m_isTransfiniteSurf
True if we're a transfinite surface (used for Geo)
Definition CADSurfOCE.h:107
std::array< NekDouble, 2 > locuvFrom(std::array< NekDouble, 3 > p, std::array< NekDouble, 2 > guess, NekDouble &dist) override
Curvature and position at uv together.
std::array< NekDouble, 4 > GetBounds() override
Get the limits of the parametric space for the surface.
BRepTopAdaptor_FClass2d * m_2Dclass
Definition CADSurfOCE.h:105
gp_Pnt PntToCAD(const std::array< NekDouble, 3 > &p)
A point on its way into OpenCASCADE.
Definition CADUnits.hpp:95
std::array< NekDouble, 3 > PntFromCAD(const gp_Pnt &p)
A point on its way out of OpenCASCADE.
Definition CADUnits.hpp:101
std::array< NekDouble, 3 > VecFromCAD(const gp_Vec &v)
A vector on its way out of OpenCASCADE.
Definition CADUnits.hpp:107
NekDouble CurvatureFromCAD(NekDouble curvature)
A curvature, one over a length, on its way out of OpenCASCADE.
Definition CADUnits.hpp:89
NekDouble FromCAD(NekDouble length)
A length on its way out of OpenCASCADE.
Definition CADUnits.hpp:83
std::shared_ptr< CADCurve > CADCurveSharedPtr
Definition CADCurve.h:247
CADSurfFactory & GetCADSurfFactory()
Definition CADSystem.cpp:60
STL namespace.
scalarT< T > min(scalarT< T > lhs, scalarT< T > rhs)
Definition scalar.hpp:299
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:289