1 // Created on: 2015-09-21
2 // Copyright (c) 2015 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <GeomEvaluator_OffsetSurface.hxx>
17 #include <GeomAdaptor_HSurface.hxx>
19 #include <CSLib_NormalStatus.hxx>
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_BSplineSurface.hxx>
22 #include <Geom_UndefinedValue.hxx>
23 #include <GeomAdaptor_HSurface.hxx>
25 #include <Standard_RangeError.hxx>
27 IMPLEMENT_STANDARD_RTTIEXT(GeomEvaluator_OffsetSurface,GeomEvaluator_Surface)
31 // tolerance for considering derivative to be null
32 const Standard_Real the_D1MagTol = 1.e-9;
34 // If calculation of normal fails, try shifting the point towards the center
35 // of the parametric space of the surface, in the hope that derivatives
36 // are better defined there.
38 // This shift is iterative, starting with Precision::PConfusion()
39 // and increasing by multiple of 2 on each step.
41 // NB: temporarily this is made as static function and not class method,
42 // hence code duplications
43 static Standard_Boolean shiftPoint (const Standard_Real theUStart, const Standard_Real theVStart,
44 Standard_Real& theU, Standard_Real& theV,
45 const Handle(Geom_Surface)& theSurf,
46 const Handle(GeomAdaptor_HSurface)& theAdaptor,
47 const gp_Vec& theD1U, const gp_Vec& theD1V)
49 // Get parametric bounds and closure status
50 Standard_Real aUMin, aUMax, aVMin, aVMax;
51 Standard_Boolean isUPeriodic, isVPeriodic;
52 if (! theSurf.IsNull())
54 theSurf->Bounds (aUMin, aUMax, aVMin, aVMax);
55 isUPeriodic = theSurf->IsUPeriodic();
56 isVPeriodic = theSurf->IsVPeriodic();
60 aUMin = theAdaptor->FirstUParameter();
61 aUMax = theAdaptor->LastUParameter();
62 aVMin = theAdaptor->FirstVParameter();
63 aVMax = theAdaptor->LastVParameter();
64 isUPeriodic = theAdaptor->IsUPeriodic();
65 isVPeriodic = theAdaptor->IsVPeriodic();
68 // check if either U or V is singular (normally one of them is)
69 Standard_Boolean isUSingular = (theD1U.SquareMagnitude() < the_D1MagTol * the_D1MagTol);
70 Standard_Boolean isVSingular = (theD1V.SquareMagnitude() < the_D1MagTol * the_D1MagTol);
72 // compute vector to shift from start point to center of the surface;
73 // if surface is periodic or singular in some direction, take shift in that direction zero
74 Standard_Real aDirU = (isUPeriodic || (isUSingular && ! isVSingular) ? 0. : 0.5 * (aUMin + aUMax) - theUStart);
75 Standard_Real aDirV = (isVPeriodic || (isVSingular && ! isUSingular) ? 0. : 0.5 * (aVMin + aVMax) - theVStart);
76 Standard_Real aDist = Sqrt (aDirU * aDirU + aDirV * aDirV);
78 // shift current point from its current position towards center, by value of twice
79 // current distance from it to start (but not less than Precision::PConfusion());
80 // fail if center is overpassed.
81 Standard_Real aDU = theU - theUStart;
82 Standard_Real aDV = theV - theVStart;
83 Standard_Real aStep = Max (2. * Sqrt (aDU * aDU + aDV * aDV), Precision::PConfusion());
86 return Standard_False;
90 theU += aDirU * aStep;
91 theV += aDirV * aStep;
93 // std::cout << "Normal calculation failed at (" << theUStart << ", " << theVStart << "), shifting to (" << theU << ", " << theV << ")" << std::endl;
99 template<class SurfOrAdapt>
100 static void derivatives(Standard_Integer theMaxOrder,
101 Standard_Integer theMinOrder,
102 const Standard_Real theU,
103 const Standard_Real theV,
104 const SurfOrAdapt& theBasisSurf,
105 const Standard_Integer theNU,
106 const Standard_Integer theNV,
107 const Standard_Boolean theAlongU,
108 const Standard_Boolean theAlongV,
109 const Handle(Geom_BSplineSurface)& theL,
110 TColgp_Array2OfVec& theDerNUV,
111 TColgp_Array2OfVec& theDerSurf)
113 Standard_Integer i, j;
115 gp_Vec DL1U, DL1V, DL2U, DL2V, DL2UV, DL3U, DL3UUV, DL3UVV, DL3V;
117 if (theAlongU || theAlongV)
120 TColgp_Array2OfVec DerSurfL(0, theMaxOrder + theNU + 1, 0, theMaxOrder + theNV + 1);
124 theL->D1(theU, theV, P, DL1U, DL1V);
125 DerSurfL.SetValue(1, 0, DL1U);
126 DerSurfL.SetValue(0, 1, DL1V);
129 theL->D2(theU, theV, P, DL1U, DL1V, DL2U, DL2V, DL2UV);
130 DerSurfL.SetValue(1, 0, DL1U);
131 DerSurfL.SetValue(0, 1, DL1V);
132 DerSurfL.SetValue(1, 1, DL2UV);
133 DerSurfL.SetValue(2, 0, DL2U);
134 DerSurfL.SetValue(0, 2, DL2V);
137 theL->D3(theU, theV, P, DL1U, DL1V, DL2U, DL2V, DL2UV, DL3U, DL3V, DL3UUV, DL3UVV);
138 DerSurfL.SetValue(1, 0, DL1U);
139 DerSurfL.SetValue(0, 1, DL1V);
140 DerSurfL.SetValue(1, 1, DL2UV);
141 DerSurfL.SetValue(2, 0, DL2U);
142 DerSurfL.SetValue(0, 2, DL2V);
143 DerSurfL.SetValue(3, 0, DL3U);
144 DerSurfL.SetValue(2, 1, DL3UUV);
145 DerSurfL.SetValue(1, 2, DL3UVV);
146 DerSurfL.SetValue(0, 3, DL3V);
154 for (i = 0; i <= theMaxOrder + 1 + theNU; i++)
155 for (j = i; j <= theMaxOrder + theNV + 1; j++)
156 if (i + j > theMinOrder)
158 DerSurfL.SetValue(i, j, theL->DN(theU, theV, i, j));
159 theDerSurf.SetValue(i, j, theBasisSurf->DN(theU, theV, i, j));
160 if (i != j && j <= theNU + 1)
162 theDerSurf.SetValue(j, i, theBasisSurf->DN(theU, theV, j, i));
163 DerSurfL.SetValue(j, i, theL->DN(theU, theV, j, i));
169 for (j = 0; j <= theMaxOrder + 1 + theNV; j++)
170 for (i = j; i <= theMaxOrder + theNU + 1; i++)
171 if (i + j > theMinOrder)
173 DerSurfL.SetValue(i, j, theL->DN(theU, theV, i, j));
174 theDerSurf.SetValue(i, j, theBasisSurf->DN(theU, theV, i, j));
175 if (i != j && i <= theNV + 1)
177 theDerSurf.SetValue(j, i, theBasisSurf->DN(theU, theV, j, i));
178 DerSurfL.SetValue(j, i, theL->DN(theU, theV, j, i));
182 for (i = 0; i <= theMaxOrder + theNU; i++)
183 for (j = 0; j <= theMaxOrder + theNV; j++)
186 theDerNUV.SetValue(i, j, CSLib::DNNUV(i, j, DerSurfL, theDerSurf));
188 theDerNUV.SetValue(i, j, CSLib::DNNUV(i, j, theDerSurf, DerSurfL));
193 for (i = 0; i <= theMaxOrder + theNU+ 1; i++)
194 for (j = i; j <= theMaxOrder + theNV + 1; j++)
195 if (i + j > theMinOrder)
197 theDerSurf.SetValue(i, j, theBasisSurf->DN(theU, theV, i, j));
199 theDerSurf.SetValue(j, i, theBasisSurf->DN(theU, theV, j, i));
201 for (i = 0; i <= theMaxOrder + theNU; i++)
202 for (j = 0; j <= theMaxOrder + theNV; j++)
203 theDerNUV.SetValue(i, j, CSLib::DNNUV(i, j, theDerSurf));
207 } // end of anonymous namespace
209 GeomEvaluator_OffsetSurface::GeomEvaluator_OffsetSurface(
210 const Handle(Geom_Surface)& theBase,
211 const Standard_Real theOffset,
212 const Handle(Geom_OsculatingSurface)& theOscSurf)
213 : GeomEvaluator_Surface(),
216 myOscSurf(theOscSurf)
218 if (!myOscSurf.IsNull())
219 return; // osculating surface already exists
221 // Create osculating surface for B-spline and Besier surfaces only
222 if (myBaseSurf->IsKind(STANDARD_TYPE(Geom_BSplineSurface)) ||
223 myBaseSurf->IsKind(STANDARD_TYPE(Geom_BezierSurface)))
224 myOscSurf = new Geom_OsculatingSurface(myBaseSurf, Precision::Confusion());
227 GeomEvaluator_OffsetSurface::GeomEvaluator_OffsetSurface(
228 const Handle(GeomAdaptor_HSurface)& theBase,
229 const Standard_Real theOffset,
230 const Handle(Geom_OsculatingSurface)& theOscSurf)
231 : GeomEvaluator_Surface(),
232 myBaseAdaptor(theBase),
234 myOscSurf(theOscSurf)
238 void GeomEvaluator_OffsetSurface::D0(
239 const Standard_Real theU, const Standard_Real theV, gp_Pnt& theValue) const
241 Standard_Real aU = theU, aV = theV;
245 BaseD1 (aU, aV, theValue, aD1U, aD1V);
248 CalculateD0(aU, aV, theValue, aD1U, aD1V);
251 catch (Geom_UndefinedValue&)
253 // if failed at parametric boundary, try taking derivative at shifted point
254 if (! shiftPoint (theU, theV, aU, aV, myBaseSurf, myBaseAdaptor, aD1U, aD1V))
262 void GeomEvaluator_OffsetSurface::D1(
263 const Standard_Real theU, const Standard_Real theV,
264 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V) const
266 Standard_Real aU = theU, aV = theV;
269 gp_Vec aD2U, aD2V, aD2UV;
270 BaseD2 (aU, aV, theValue, theD1U, theD1V, aD2U, aD2V, aD2UV);
273 CalculateD1(aU, aV, theValue, theD1U, theD1V, aD2U, aD2V, aD2UV);
276 catch (Geom_UndefinedValue&)
278 // if failed at parametric boundary, try taking derivative at shifted point
279 if (! shiftPoint (theU, theV, aU, aV, myBaseSurf, myBaseAdaptor, theD1U, theD1V))
287 void GeomEvaluator_OffsetSurface::D2(
288 const Standard_Real theU, const Standard_Real theV,
289 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
290 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV) const
292 Standard_Real aU = theU, aV = theV;
295 gp_Vec aD3U, aD3V, aD3UUV, aD3UVV;
296 BaseD3 (aU, aV, theValue, theD1U, theD1V,
297 theD2U, theD2V, theD2UV, aD3U, aD3V, aD3UUV, aD3UVV);
300 CalculateD2 (aU, aV, theValue, theD1U, theD1V,
301 theD2U, theD2V, theD2UV, aD3U, aD3V, aD3UUV, aD3UVV);
304 catch (Geom_UndefinedValue&)
306 // if failed at parametric boundary, try taking derivative at shifted point
307 if (! shiftPoint (theU, theV, aU, aV, myBaseSurf, myBaseAdaptor, theD1U, theD1V))
315 void GeomEvaluator_OffsetSurface::D3(
316 const Standard_Real theU, const Standard_Real theV,
317 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
318 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV,
319 gp_Vec& theD3U, gp_Vec& theD3V, gp_Vec& theD3UUV, gp_Vec& theD3UVV) const
321 Standard_Real aU = theU, aV = theV;
324 BaseD3 (aU, aV, theValue, theD1U, theD1V,
325 theD2U, theD2V, theD2UV, theD3U, theD3V, theD3UUV, theD3UVV);
328 CalculateD3 (aU, aV, theValue, theD1U, theD1V,
329 theD2U, theD2V, theD2UV, theD3U, theD3V, theD3UUV, theD3UVV);
332 catch (Geom_UndefinedValue&)
334 // if failed at parametric boundary, try taking derivative at shifted point
335 if (! shiftPoint (theU, theV, aU, aV, myBaseSurf, myBaseAdaptor, theD1U, theD1V))
343 gp_Vec GeomEvaluator_OffsetSurface::DN(
344 const Standard_Real theU, const Standard_Real theV,
345 const Standard_Integer theDerU, const Standard_Integer theDerV) const
347 Standard_RangeError_Raise_if(theDerU < 0, "GeomEvaluator_OffsetSurface::DN(): theDerU < 0");
348 Standard_RangeError_Raise_if(theDerV < 0, "GeomEvaluator_OffsetSurface::DN(): theDerV < 0");
349 Standard_RangeError_Raise_if(theDerU + theDerV < 1,
350 "GeomEvaluator_OffsetSurface::DN(): theDerU + theDerV < 1");
352 Standard_Real aU = theU, aV = theV;
357 BaseD1 (aU, aV, aP, aD1U, aD1V);
360 return CalculateDN (aU, aV, theDerU, theDerV, aD1U, aD1V);
362 catch (Geom_UndefinedValue&)
364 // if failed at parametric boundary, try taking derivative at shifted point
365 if (! shiftPoint (theU, theV, aU, aV, myBaseSurf, myBaseAdaptor, aD1U, aD1V))
374 void GeomEvaluator_OffsetSurface::BaseD0(const Standard_Real theU, const Standard_Real theV,
375 gp_Pnt& theValue) const
377 if (!myBaseAdaptor.IsNull())
378 myBaseAdaptor->D0(theU, theV, theValue);
380 myBaseSurf->D0(theU, theV, theValue);
383 void GeomEvaluator_OffsetSurface::BaseD1(const Standard_Real theU, const Standard_Real theV,
384 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V) const
386 if (!myBaseAdaptor.IsNull())
387 myBaseAdaptor->D1(theU, theV, theValue, theD1U, theD1V);
389 myBaseSurf->D1(theU, theV, theValue, theD1U, theD1V);
392 void GeomEvaluator_OffsetSurface::BaseD2(const Standard_Real theU, const Standard_Real theV,
393 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
394 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV) const
396 if (!myBaseAdaptor.IsNull())
397 myBaseAdaptor->D2(theU, theV, theValue, theD1U, theD1V, theD2U, theD2V, theD2UV);
399 myBaseSurf->D2(theU, theV, theValue, theD1U, theD1V, theD2U, theD2V, theD2UV);
402 void GeomEvaluator_OffsetSurface::BaseD3(
403 const Standard_Real theU, const Standard_Real theV,
404 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
405 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV,
406 gp_Vec& theD3U, gp_Vec& theD3V, gp_Vec& theD3UUV, gp_Vec& theD3UVV) const
408 if (!myBaseAdaptor.IsNull())
409 myBaseAdaptor->D3(theU, theV, theValue, theD1U, theD1V,
410 theD2U, theD2V, theD2UV, theD3U, theD3V, theD3UUV, theD3UVV);
412 myBaseSurf->D3(theU, theV, theValue, theD1U, theD1V,
413 theD2U, theD2V, theD2UV, theD3U, theD3V, theD3UUV, theD3UVV);
417 void GeomEvaluator_OffsetSurface::CalculateD0(
418 const Standard_Real theU, const Standard_Real theV,
420 const gp_Vec& theD1U, const gp_Vec& theD1V) const
422 // Normalize derivatives before normal calculation because it gives more stable result.
423 // There will be normalized only derivatives greater than 1.0 to avoid differences in last significant digit
426 Standard_Real aD1UNorm2 = aD1U.SquareMagnitude();
427 Standard_Real aD1VNorm2 = aD1V.SquareMagnitude();
429 aD1U /= Sqrt(aD1UNorm2);
431 aD1V /= Sqrt(aD1VNorm2);
433 gp_Vec aNorm = aD1U.Crossed(aD1V);
434 if (aNorm.SquareMagnitude() > the_D1MagTol * the_D1MagTol)
436 // Non singular case. Simple computations.
438 theValue.SetXYZ(theValue.XYZ() + myOffset * aNorm.XYZ());
442 const Standard_Integer MaxOrder = 3;
444 Handle(Geom_BSplineSurface) L;
445 Standard_Boolean isOpposite = Standard_False;
446 Standard_Boolean AlongU = Standard_False;
447 Standard_Boolean AlongV = Standard_False;
448 if (!myOscSurf.IsNull())
450 AlongU = myOscSurf->UOscSurf(theU, theV, isOpposite, L);
451 AlongV = myOscSurf->VOscSurf(theU, theV, isOpposite, L);
453 const Standard_Real aSign = ((AlongV || AlongU) && isOpposite) ? -1. : 1.;
455 TColgp_Array2OfVec DerNUV(0, MaxOrder, 0, MaxOrder);
456 TColgp_Array2OfVec DerSurf(0, MaxOrder + 1, 0, MaxOrder + 1);
457 Standard_Integer OrderU, OrderV;
458 Standard_Real Umin = 0, Umax = 0, Vmin = 0, Vmax = 0;
459 Bounds(Umin, Umax, Vmin, Vmax);
461 DerSurf.SetValue(1, 0, theD1U);
462 DerSurf.SetValue(0, 1, theD1V);
463 if (!myBaseSurf.IsNull())
464 derivatives(MaxOrder, 1, theU, theV, myBaseSurf, 0, 0, AlongU, AlongV, L, DerNUV, DerSurf);
466 derivatives(MaxOrder, 1, theU, theV, myBaseAdaptor, 0, 0, AlongU, AlongV, L, DerNUV, DerSurf);
469 CSLib_NormalStatus NStatus = CSLib_Singular;
470 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax,
471 NStatus, Normal, OrderU, OrderV);
472 if (NStatus == CSLib_InfinityOfSolutions)
474 // Replace zero derivative and try to calculate normal
475 gp_Vec aNewDU = theD1U;
476 gp_Vec aNewDV = theD1V;
477 if (ReplaceDerivative(theU, theV, aNewDU, aNewDV, the_D1MagTol * the_D1MagTol))
478 CSLib::Normal(aNewDU, aNewDV, the_D1MagTol, NStatus, Normal);
481 if (NStatus != CSLib_Defined)
482 throw Geom_UndefinedValue(
483 "GeomEvaluator_OffsetSurface::CalculateD0(): Unable to calculate normal");
485 theValue.SetXYZ(theValue.XYZ() + myOffset * aSign * Normal.XYZ());
489 void GeomEvaluator_OffsetSurface::CalculateD1(
490 const Standard_Real theU, const Standard_Real theV,
491 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
492 const gp_Vec& theD2U, const gp_Vec& theD2V, const gp_Vec& theD2UV) const
494 // Check offset side.
495 Handle(Geom_BSplineSurface) L;
496 Standard_Boolean isOpposite = Standard_False;
497 Standard_Boolean AlongU = Standard_False;
498 Standard_Boolean AlongV = Standard_False;
500 // Normalize derivatives before normal calculation because it gives more stable result.
501 // There will be normalized only derivatives greater than 1.0 to avoid differences in last significant digit
504 Standard_Real aD1UNorm2 = aD1U.SquareMagnitude();
505 Standard_Real aD1VNorm2 = aD1V.SquareMagnitude();
507 aD1U /= Sqrt(aD1UNorm2);
509 aD1V /= Sqrt(aD1VNorm2);
511 Standard_Boolean isSingular = Standard_False;
512 Standard_Integer MaxOrder = 0;
513 gp_Vec aNorm = aD1U.Crossed(aD1V);
514 if (aNorm.SquareMagnitude() <= the_D1MagTol * the_D1MagTol)
516 if (!myOscSurf.IsNull())
518 AlongU = myOscSurf->UOscSurf(theU, theV, isOpposite, L);
519 AlongV = myOscSurf->VOscSurf(theU, theV, isOpposite, L);
523 isSingular = Standard_True;
526 const Standard_Real aSign = ((AlongV || AlongU) && isOpposite) ? -1. : 1.;
530 // AlongU or AlongV leads to more complex D1 computation
531 // Try to compute D0 and D1 much simpler
533 theValue.SetXYZ(theValue.XYZ() + myOffset * aSign * aNorm.XYZ());
535 gp_Vec aN0(aNorm.XYZ()), aN1U, aN1V;
536 Standard_Real aScale = (theD1U^theD1V).Dot(aN0);
537 aN1U.SetX(theD2U.Y() * theD1V.Z() + theD1U.Y() * theD2UV.Z()
538 - theD2U.Z() * theD1V.Y() - theD1U.Z() * theD2UV.Y());
539 aN1U.SetY((theD2U.X() * theD1V.Z() + theD1U.X() * theD2UV.Z()
540 - theD2U.Z() * theD1V.X() - theD1U.Z() * theD2UV.X()) * -1.0);
541 aN1U.SetZ(theD2U.X() * theD1V.Y() + theD1U.X() * theD2UV.Y()
542 - theD2U.Y() * theD1V.X() - theD1U.Y() * theD2UV.X());
543 Standard_Real aScaleU = aN1U.Dot(aN0);
544 aN1U.Subtract(aScaleU * aN0);
547 aN1V.SetX(theD2UV.Y() * theD1V.Z() + theD2V.Z() * theD1U.Y()
548 - theD2UV.Z() * theD1V.Y() - theD2V.Y() * theD1U.Z());
549 aN1V.SetY((theD2UV.X() * theD1V.Z() + theD2V.Z() * theD1U.X()
550 - theD2UV.Z() * theD1V.X() - theD2V.X() * theD1U.Z()) * -1.0);
551 aN1V.SetZ(theD2UV.X() * theD1V.Y() + theD2V.Y() * theD1U.X()
552 - theD2UV.Y() * theD1V.X() - theD2V.X() * theD1U.Y());
553 Standard_Real aScaleV = aN1V.Dot(aN0);
554 aN1V.Subtract(aScaleV * aN0);
557 theD1U += myOffset * aSign * aN1U;
558 theD1V += myOffset * aSign * aN1V;
563 Standard_Integer OrderU, OrderV;
564 TColgp_Array2OfVec DerNUV(0, MaxOrder + 1, 0, MaxOrder + 1);
565 TColgp_Array2OfVec DerSurf(0, MaxOrder + 2, 0, MaxOrder + 2);
566 Standard_Real Umin = 0, Umax = 0, Vmin = 0, Vmax = 0;
567 Bounds(Umin, Umax, Vmin, Vmax);
569 DerSurf.SetValue(1, 0, theD1U);
570 DerSurf.SetValue(0, 1, theD1V);
571 DerSurf.SetValue(1, 1, theD2UV);
572 DerSurf.SetValue(2, 0, theD2U);
573 DerSurf.SetValue(0, 2, theD2V);
574 if (!myBaseSurf.IsNull())
575 derivatives(MaxOrder, 2, theU, theV, myBaseSurf, 1, 1, AlongU, AlongV, L, DerNUV, DerSurf);
577 derivatives(MaxOrder, 2, theU, theV, myBaseAdaptor, 1, 1, AlongU, AlongV, L, DerNUV, DerSurf);
580 CSLib_NormalStatus NStatus;
581 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax, NStatus, Normal, OrderU, OrderV);
582 if (NStatus == CSLib_InfinityOfSolutions)
584 gp_Vec aNewDU = theD1U;
585 gp_Vec aNewDV = theD1V;
586 // Replace zero derivative and try to calculate normal
587 if (ReplaceDerivative(theU, theV, aNewDU, aNewDV, the_D1MagTol * the_D1MagTol))
589 DerSurf.SetValue(1, 0, aNewDU);
590 DerSurf.SetValue(0, 1, aNewDV);
591 if (!myBaseSurf.IsNull())
592 derivatives(MaxOrder, 2, theU, theV, myBaseSurf, 1, 1, AlongU, AlongV, L, DerNUV, DerSurf);
594 derivatives(MaxOrder, 2, theU, theV, myBaseAdaptor, 1, 1, AlongU, AlongV, L, DerNUV, DerSurf);
595 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax,
596 NStatus, Normal, OrderU, OrderV);
600 if (NStatus != CSLib_Defined)
601 throw Geom_UndefinedValue(
602 "GeomEvaluator_OffsetSurface::CalculateD1(): Unable to calculate normal");
604 theValue.SetXYZ(theValue.XYZ() + myOffset * aSign * Normal.XYZ());
606 theD1U = DerSurf(1, 0) + myOffset * aSign * CSLib::DNNormal(1, 0, DerNUV, OrderU, OrderV);
607 theD1V = DerSurf(0, 1) + myOffset * aSign * CSLib::DNNormal(0, 1, DerNUV, OrderU, OrderV);
610 void GeomEvaluator_OffsetSurface::CalculateD2(
611 const Standard_Real theU, const Standard_Real theV,
612 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
613 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV,
614 const gp_Vec& theD3U, const gp_Vec& theD3V, const gp_Vec& theD3UUV, const gp_Vec& theD3UVV) const
617 CSLib_NormalStatus NStatus;
618 CSLib::Normal(theD1U, theD1V, the_D1MagTol, NStatus, Normal);
620 const Standard_Integer MaxOrder = (NStatus == CSLib_Defined) ? 0 : 3;
621 Standard_Integer OrderU, OrderV;
622 TColgp_Array2OfVec DerNUV(0, MaxOrder + 2, 0, MaxOrder + 2);
623 TColgp_Array2OfVec DerSurf(0, MaxOrder + 3, 0, MaxOrder + 3);
625 Standard_Real Umin = 0, Umax = 0, Vmin = 0, Vmax = 0;
626 Bounds(Umin, Umax, Vmin, Vmax);
628 DerSurf.SetValue(1, 0, theD1U);
629 DerSurf.SetValue(0, 1, theD1V);
630 DerSurf.SetValue(1, 1, theD2UV);
631 DerSurf.SetValue(2, 0, theD2U);
632 DerSurf.SetValue(0, 2, theD2V);
633 DerSurf.SetValue(3, 0, theD3U);
634 DerSurf.SetValue(2, 1, theD3UUV);
635 DerSurf.SetValue(1, 2, theD3UVV);
636 DerSurf.SetValue(0, 3, theD3V);
637 //*********************
639 Handle(Geom_BSplineSurface) L;
640 Standard_Boolean isOpposite = Standard_False;
641 Standard_Boolean AlongU = Standard_False;
642 Standard_Boolean AlongV = Standard_False;
643 if ((NStatus != CSLib_Defined) && !myOscSurf.IsNull())
645 AlongU = myOscSurf->UOscSurf(theU, theV, isOpposite, L);
646 AlongV = myOscSurf->VOscSurf(theU, theV, isOpposite, L);
648 const Standard_Real aSign = ((AlongV || AlongU) && isOpposite) ? -1. : 1.;
650 if (!myBaseSurf.IsNull())
651 derivatives(MaxOrder, 3, theU, theV, myBaseSurf, 2, 2, AlongU, AlongV, L, DerNUV, DerSurf);
653 derivatives(MaxOrder, 3, theU, theV, myBaseAdaptor, 2, 2, AlongU, AlongV, L, DerNUV, DerSurf);
655 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax,
656 NStatus, Normal, OrderU, OrderV);
657 if (NStatus != CSLib_Defined)
658 throw Geom_UndefinedValue(
659 "GeomEvaluator_OffsetSurface::CalculateD2(): Unable to calculate normal");
661 theValue.SetXYZ(theValue.XYZ() + myOffset * aSign * Normal.XYZ());
663 theD1U = DerSurf(1, 0) + myOffset * aSign * CSLib::DNNormal(1, 0, DerNUV, OrderU, OrderV);
664 theD1V = DerSurf(0, 1) + myOffset * aSign * CSLib::DNNormal(0, 1, DerNUV, OrderU, OrderV);
666 if (!myBaseSurf.IsNull())
668 theD2U = myBaseSurf->DN(theU, theV, 2, 0);
669 theD2V = myBaseSurf->DN(theU, theV, 0, 2);
670 theD2UV = myBaseSurf->DN(theU, theV, 1, 1);
674 theD2U = myBaseAdaptor->DN(theU, theV, 2, 0);
675 theD2V = myBaseAdaptor->DN(theU, theV, 0, 2);
676 theD2UV = myBaseAdaptor->DN(theU, theV, 1, 1);
679 theD2U += aSign * myOffset * CSLib::DNNormal(2, 0, DerNUV, OrderU, OrderV);
680 theD2V += aSign * myOffset * CSLib::DNNormal(0, 2, DerNUV, OrderU, OrderV);
681 theD2UV += aSign * myOffset * CSLib::DNNormal(1, 1, DerNUV, OrderU, OrderV);
684 void GeomEvaluator_OffsetSurface::CalculateD3(
685 const Standard_Real theU, const Standard_Real theV,
686 gp_Pnt& theValue, gp_Vec& theD1U, gp_Vec& theD1V,
687 gp_Vec& theD2U, gp_Vec& theD2V, gp_Vec& theD2UV,
688 gp_Vec& theD3U, gp_Vec& theD3V, gp_Vec& theD3UUV, gp_Vec& theD3UVV) const
691 CSLib_NormalStatus NStatus;
692 CSLib::Normal(theD1U, theD1V, the_D1MagTol, NStatus, Normal);
693 const Standard_Integer MaxOrder = (NStatus == CSLib_Defined) ? 0 : 3;
694 Standard_Integer OrderU, OrderV;
695 TColgp_Array2OfVec DerNUV(0, MaxOrder + 3, 0, MaxOrder + 3);
696 TColgp_Array2OfVec DerSurf(0, MaxOrder + 4, 0, MaxOrder + 4);
697 Standard_Real Umin = 0, Umax = 0, Vmin = 0, Vmax = 0;
698 Bounds(Umin, Umax, Vmin, Vmax);
700 DerSurf.SetValue(1, 0, theD1U);
701 DerSurf.SetValue(0, 1, theD1V);
702 DerSurf.SetValue(1, 1, theD2UV);
703 DerSurf.SetValue(2, 0, theD2U);
704 DerSurf.SetValue(0, 2, theD2V);
705 DerSurf.SetValue(3, 0, theD3U);
706 DerSurf.SetValue(2, 1, theD3UUV);
707 DerSurf.SetValue(1, 2, theD3UVV);
708 DerSurf.SetValue(0, 3, theD3V);
711 //*********************
712 Handle(Geom_BSplineSurface) L;
713 Standard_Boolean isOpposite = Standard_False;
714 Standard_Boolean AlongU = Standard_False;
715 Standard_Boolean AlongV = Standard_False;
716 if ((NStatus != CSLib_Defined) && !myOscSurf.IsNull())
718 AlongU = myOscSurf->UOscSurf(theU, theV, isOpposite, L);
719 AlongV = myOscSurf->VOscSurf(theU, theV, isOpposite, L);
721 const Standard_Real aSign = ((AlongV || AlongU) && isOpposite) ? -1. : 1.;
723 if (!myBaseSurf.IsNull())
724 derivatives(MaxOrder, 3, theU, theV, myBaseSurf, 3, 3, AlongU, AlongV, L, DerNUV, DerSurf);
726 derivatives(MaxOrder, 3, theU, theV, myBaseAdaptor, 3, 3, AlongU, AlongV, L, DerNUV, DerSurf);
728 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax,
729 NStatus, Normal, OrderU, OrderV);
730 if (NStatus != CSLib_Defined)
731 throw Geom_UndefinedValue(
732 "GeomEvaluator_OffsetSurface::CalculateD3(): Unable to calculate normal");
734 theValue.SetXYZ(theValue.XYZ() + myOffset * aSign * Normal.XYZ());
736 theD1U = DerSurf(1, 0) + myOffset * aSign * CSLib::DNNormal(1, 0, DerNUV, OrderU, OrderV);
737 theD1V = DerSurf(0, 1) + myOffset * aSign * CSLib::DNNormal(0, 1, DerNUV, OrderU, OrderV);
739 if (!myBaseSurf.IsNull())
741 theD2U = myBaseSurf->DN(theU, theV, 2, 0);
742 theD2V = myBaseSurf->DN(theU, theV, 0, 2);
743 theD2UV = myBaseSurf->DN(theU, theV, 1, 1);
744 theD3U = myBaseSurf->DN(theU, theV, 3, 0);
745 theD3V = myBaseSurf->DN(theU, theV, 0, 3);
746 theD3UUV = myBaseSurf->DN(theU, theV, 2, 1);
747 theD3UVV = myBaseSurf->DN(theU, theV, 1, 2);
751 theD2U = myBaseAdaptor->DN(theU, theV, 2, 0);
752 theD2V = myBaseAdaptor->DN(theU, theV, 0, 2);
753 theD2UV = myBaseAdaptor->DN(theU, theV, 1, 1);
754 theD3U = myBaseAdaptor->DN(theU, theV, 3, 0);
755 theD3V = myBaseAdaptor->DN(theU, theV, 0, 3);
756 theD3UUV = myBaseAdaptor->DN(theU, theV, 2, 1);
757 theD3UVV = myBaseAdaptor->DN(theU, theV, 1, 2);
760 theD2U += aSign * myOffset * CSLib::DNNormal(2, 0, DerNUV, OrderU, OrderV);
761 theD2V += aSign * myOffset * CSLib::DNNormal(0, 2, DerNUV, OrderU, OrderV);
762 theD2UV += aSign * myOffset * CSLib::DNNormal(1, 1, DerNUV, OrderU, OrderV);
763 theD3U += aSign * myOffset * CSLib::DNNormal(3, 0, DerNUV, OrderU, OrderV);
764 theD3V += aSign * myOffset * CSLib::DNNormal(0, 3, DerNUV, OrderU, OrderV);
765 theD3UUV += aSign * myOffset * CSLib::DNNormal(2, 1, DerNUV, OrderU, OrderV);
766 theD3UVV += aSign * myOffset * CSLib::DNNormal(1, 2, DerNUV, OrderU, OrderV);
769 gp_Vec GeomEvaluator_OffsetSurface::CalculateDN(
770 const Standard_Real theU, const Standard_Real theV,
771 const Standard_Integer theNu, const Standard_Integer theNv,
772 const gp_Vec& theD1U, const gp_Vec& theD1V) const
775 CSLib_NormalStatus NStatus;
776 CSLib::Normal(theD1U, theD1V, the_D1MagTol, NStatus, Normal);
777 const Standard_Integer MaxOrder = (NStatus == CSLib_Defined) ? 0 : 3;
778 Standard_Integer OrderU, OrderV;
779 TColgp_Array2OfVec DerNUV(0, MaxOrder + theNu, 0, MaxOrder + theNu);
780 TColgp_Array2OfVec DerSurf(0, MaxOrder + theNu + 1, 0, MaxOrder + theNv + 1);
782 Standard_Real Umin = 0, Umax = 0, Vmin = 0, Vmax = 0;
783 Bounds(Umin, Umax, Vmin, Vmax);
785 DerSurf.SetValue(1, 0, theD1U);
786 DerSurf.SetValue(0, 1, theD1V);
788 //*********************
789 Handle(Geom_BSplineSurface) L;
790 // Is there any osculatingsurface along U or V;
791 Standard_Boolean isOpposite = Standard_False;
792 Standard_Boolean AlongU = Standard_False;
793 Standard_Boolean AlongV = Standard_False;
794 if ((NStatus != CSLib_Defined) && !myOscSurf.IsNull())
796 AlongU = myOscSurf->UOscSurf(theU, theV, isOpposite, L);
797 AlongV = myOscSurf->VOscSurf(theU, theV, isOpposite, L);
799 const Standard_Real aSign = ((AlongV || AlongU) && isOpposite) ? -1. : 1.;
801 if (!myBaseSurf.IsNull())
802 derivatives(MaxOrder, 1, theU, theV, myBaseSurf, theNu, theNv, AlongU, AlongV, L, DerNUV, DerSurf);
804 derivatives(MaxOrder, 1, theU, theV, myBaseAdaptor, theNu, theNv, AlongU, AlongV, L, DerNUV, DerSurf);
806 CSLib::Normal(MaxOrder, DerNUV, the_D1MagTol, theU, theV, Umin, Umax, Vmin, Vmax,
807 NStatus, Normal, OrderU, OrderV);
808 if (NStatus != CSLib_Defined)
809 throw Geom_UndefinedValue(
810 "GeomEvaluator_OffsetSurface::CalculateDN(): Unable to calculate normal");
813 if (!myBaseSurf.IsNull())
814 D = myBaseSurf->DN(theU, theV, theNu, theNv);
816 D = myBaseAdaptor->DN(theU, theV, theNu, theNv);
818 D += aSign * myOffset * CSLib::DNNormal(theNu, theNv, DerNUV, OrderU, OrderV);
823 void GeomEvaluator_OffsetSurface::Bounds(Standard_Real& theUMin, Standard_Real& theUMax,
824 Standard_Real& theVMin, Standard_Real& theVMax) const
826 if (!myBaseSurf.IsNull())
827 myBaseSurf->Bounds(theUMin, theUMax, theVMin, theVMax);
830 theUMin = myBaseAdaptor->FirstUParameter();
831 theUMax = myBaseAdaptor->LastUParameter();
832 theVMin = myBaseAdaptor->FirstVParameter();
833 theVMax = myBaseAdaptor->LastVParameter();
838 Standard_Boolean GeomEvaluator_OffsetSurface::ReplaceDerivative(
839 const Standard_Real theU, const Standard_Real theV,
840 gp_Vec& theDU, gp_Vec& theDV,
841 const Standard_Real theSquareTol) const
843 Standard_Boolean isReplaceDU = theDU.SquareMagnitude() < theSquareTol;
844 Standard_Boolean isReplaceDV = theDV.SquareMagnitude() < theSquareTol;
845 Standard_Boolean isReplaced = Standard_False;
846 if (isReplaceDU ^ isReplaceDV)
848 Standard_Real aU = theU;
849 Standard_Real aV = theV;
850 Standard_Real aUMin = 0, aUMax = 0, aVMin = 0, aVMax = 0;
851 Bounds(aUMin, aUMax, aVMin, aVMax);
853 // Calculate step along non-zero derivative
855 Handle(Adaptor3d_HSurface) aSurfAdapt;
856 if (!myBaseAdaptor.IsNull())
857 aSurfAdapt = myBaseAdaptor;
859 aSurfAdapt = new GeomAdaptor_HSurface(myBaseSurf);
862 aStep = Precision::Confusion() * theDU.Magnitude();
863 if (aStep > aUMax - aUMin)
864 aStep = (aUMax - aUMin) / 100.;
868 aStep = Precision::Confusion() * theDV.Magnitude();
869 if (aStep > aVMax - aVMin)
870 aStep = (aVMax - aVMin) / 100.;
875 // Step away from currect parametric coordinates and calculate derivatives once again.
876 // Replace zero derivative by the obtained.
877 for (Standard_Real aStepSign = -1.0; aStepSign <= 1.0 && !isReplaced; aStepSign += 2.0)
881 aU = theU + aStepSign * aStep;
882 if (aU < aUMin || aU > aUMax)
887 aV = theV + aStepSign * aStep;
888 if (aV < aVMin || aV > aVMax)
892 BaseD1(aU, aV, aP, aDU, aDV);
894 if (isReplaceDU && aDU.SquareMagnitude() > theSquareTol)
897 isReplaced = Standard_True;
899 if (isReplaceDV && aDV.SquareMagnitude() > theSquareTol)
902 isReplaced = Standard_True;