0024023: Revamp the OCCT Handle - gcc and clang
[occt.git] / src / Geom2d / Geom2d_OffsetCurve.cxx
CommitLineData
b311480e 1// Created on: 1991-06-25
2// Created by: JCV
3// Copyright (c) 1991-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17// modified by Edward AGAPOV (eap) Jan 28 2002 --- DN(), occ143(BUC60654)
18
94f71cad 19#include <CSLib_Offset.hxx>
42cf5bc1 20#include <Geom2d_BezierCurve.hxx>
21#include <Geom2d_BSplineCurve.hxx>
7fd59977 22#include <Geom2d_Circle.hxx>
42cf5bc1 23#include <Geom2d_Curve.hxx>
7fd59977 24#include <Geom2d_Ellipse.hxx>
42cf5bc1 25#include <Geom2d_Geometry.hxx>
7fd59977 26#include <Geom2d_Hyperbola.hxx>
42cf5bc1 27#include <Geom2d_Line.hxx>
28#include <Geom2d_OffsetCurve.hxx>
7fd59977 29#include <Geom2d_Parabola.hxx>
7fd59977 30#include <Geom2d_TrimmedCurve.hxx>
42cf5bc1 31#include <Geom2d_UndefinedDerivative.hxx>
32#include <Geom2d_UndefinedValue.hxx>
33#include <gp.hxx>
34#include <gp_Pnt2d.hxx>
35#include <gp_Trsf2d.hxx>
36#include <gp_Vec2d.hxx>
7fd59977 37#include <gp_XY.hxx>
3d58dc49 38#include <Precision.hxx>
42cf5bc1 39#include <Standard_ConstructionError.hxx>
40#include <Standard_NoSuchObject.hxx>
41#include <Standard_NotImplemented.hxx>
42#include <Standard_RangeError.hxx>
43#include <Standard_Type.hxx>
7fd59977 44
7fd59977 45typedef Geom2d_OffsetCurve OffsetCurve;
7fd59977 46typedef Geom2d_Curve Curve;
47typedef gp_Dir2d Dir2d;
48typedef gp_Pnt2d Pnt2d;
49typedef gp_Vec2d Vec2d;
50typedef gp_Trsf2d Trsf2d;
51typedef gp_XY XY;
52
53
7fd59977 54//ordre de derivation maximum pour la recherche de la premiere
55//derivee non nulle
32ca7a51 56static const int maxDerivOrder = 3;
57static const Standard_Real MinStep = 1e-7;
3d58dc49 58static const Standard_Real MyAngularToleranceForG1 = Precision::Angular();
7fd59977 59
94f71cad 60static gp_Vec2d dummyDerivative; // used as empty value for unused derivatives in AdjustDerivative
61
62// Recalculate derivatives in the singular point
63// Returns true if the direction of derivatives is changed
64static Standard_Boolean AdjustDerivative(const Handle(Geom2d_Curve)& theCurve, Standard_Integer theMaxDerivative,
65 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2 = dummyDerivative,
66 gp_Vec2d& theD3 = dummyDerivative, gp_Vec2d& theD4 = dummyDerivative);
67
7fd59977 68//=======================================================================
69//function : Copy
70//purpose :
71//=======================================================================
72
73Handle(Geom2d_Geometry) Geom2d_OffsetCurve::Copy () const
74{
c04c30b3 75 Handle(Geom2d_OffsetCurve) C;
7fd59977 76 C = new OffsetCurve (basisCurve, offsetValue);
77 return C;
78}
79
80
81//=======================================================================
82//function : Geom2d_OffsetCurve
3d58dc49 83//purpose : Basis curve cannot be an Offset curve or trimmed from
84// offset curve.
7fd59977 85//=======================================================================
86
3d58dc49 87Geom2d_OffsetCurve::Geom2d_OffsetCurve (const Handle(Geom2d_Curve)& theCurve,
88 const Standard_Real theOffset,
89 const Standard_Boolean isTheNotCheckC0)
90: offsetValue (theOffset)
7fd59977 91{
3d58dc49 92 SetBasisCurve (theCurve, isTheNotCheckC0);
7fd59977 93}
94
95//=======================================================================
96//function : Reverse
97//purpose :
98//=======================================================================
99
100void Geom2d_OffsetCurve::Reverse ()
101{
102 basisCurve->Reverse();
103 offsetValue = -offsetValue;
104}
105
106//=======================================================================
107//function : ReversedParameter
108//purpose :
109//=======================================================================
110
111Standard_Real Geom2d_OffsetCurve::ReversedParameter( const Standard_Real U) const
112{
113 return basisCurve->ReversedParameter( U);
114}
115
116//=======================================================================
117//function : SetBasisCurve
118//purpose :
119//=======================================================================
120
c04c30b3 121void Geom2d_OffsetCurve::SetBasisCurve (const Handle(Geom2d_Curve)& C,
3d58dc49 122 const Standard_Boolean isNotCheckC0)
7fd59977 123{
3d58dc49 124 const Standard_Real aUf = C->FirstParameter(),
125 aUl = C->LastParameter();
126 Handle(Geom2d_Curve) aCheckingCurve = C;
127 Standard_Boolean isTrimmed = Standard_False;
128
129 while(aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ||
130 aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
131 {
132 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
133 {
134 Handle(Geom2d_TrimmedCurve) aTrimC =
135 Handle(Geom2d_TrimmedCurve)::DownCast(aCheckingCurve);
136 aCheckingCurve = aTrimC->BasisCurve();
137 isTrimmed = Standard_True;
138 }
139
140 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
141 {
142 Handle(Geom2d_OffsetCurve) aOC =
143 Handle(Geom2d_OffsetCurve)::DownCast(aCheckingCurve);
144 aCheckingCurve = aOC->BasisCurve();
145 offsetValue += aOC->Offset();
146 }
147 }
148
149 myBasisCurveContinuity = aCheckingCurve->Continuity();
150
151 Standard_Boolean isC0 = !isNotCheckC0 &&
152 (myBasisCurveContinuity == GeomAbs_C0);
06be28a4
RL
153
154 // Basis curve must be at least C1
3d58dc49 155 if (isC0 && aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
06be28a4 156 {
3d58dc49 157 Handle(Geom2d_BSplineCurve) aBC = Handle(Geom2d_BSplineCurve)::DownCast(aCheckingCurve);
158 if(aBC->IsG1(aUf, aUl, MyAngularToleranceForG1))
06be28a4 159 {
3d58dc49 160 //Checking if basis curve has more smooth (C1, G2 and above) is not done.
161 //It can be done in case of need.
162 myBasisCurveContinuity = GeomAbs_G1;
163 isC0 = Standard_False;
06be28a4
RL
164 }
165
166 // Raise exception if still C0
3d58dc49 167 if (isC0)
06be28a4
RL
168 Standard_ConstructionError::Raise("Offset on C0 curve");
169 }
3d58dc49 170 //
171 if(isTrimmed)
172 {
173 basisCurve = new Geom2d_TrimmedCurve(aCheckingCurve, aUf, aUl);
174 }
175 else
176 {
177 basisCurve = aCheckingCurve;
178 }
06be28a4 179
7fd59977 180}
181
182//=======================================================================
183//function : SetOffsetValue
184//purpose :
185//=======================================================================
186
187void Geom2d_OffsetCurve::SetOffsetValue (const Standard_Real D) { offsetValue = D; }
188
189//=======================================================================
190//function : BasisCurve
191//purpose :
192//=======================================================================
193
c04c30b3 194Handle(Geom2d_Curve) Geom2d_OffsetCurve::BasisCurve () const
7fd59977 195{
196 return basisCurve;
197}
198
199//=======================================================================
200//function : Continuity
201//purpose :
202//=======================================================================
203
204GeomAbs_Shape Geom2d_OffsetCurve::Continuity () const
205{
206 GeomAbs_Shape OffsetShape=GeomAbs_C0;
3d58dc49 207 switch (myBasisCurveContinuity) {
7fd59977 208 case GeomAbs_C0 : OffsetShape = GeomAbs_C0; break;
209 case GeomAbs_C1 : OffsetShape = GeomAbs_C0; break;
210 case GeomAbs_C2 : OffsetShape = GeomAbs_C1; break;
211 case GeomAbs_C3 : OffsetShape = GeomAbs_C2; break;
212 case GeomAbs_CN : OffsetShape = GeomAbs_CN; break;
213 case GeomAbs_G1 : OffsetShape = GeomAbs_G1; break;
214 case GeomAbs_G2 : OffsetShape = GeomAbs_G2; break;
215 }
3d58dc49 216
7fd59977 217 return OffsetShape;
218}
219
220//=======================================================================
221//function : D0
222//purpose :
223//=======================================================================
224
94f71cad 225void Geom2d_OffsetCurve::D0 (const Standard_Real theU,
226 Pnt2d& theP) const
32ca7a51 227 {
32ca7a51 228 Vec2d vD1;
32ca7a51 229 basisCurve->D1 (theU, theP, vD1);
32ca7a51 230
94f71cad 231 Standard_Boolean IsDirectionChange = Standard_False;
232 if(vD1.SquareMagnitude() <= gp::Resolution())
233 IsDirectionChange = AdjustDerivative(basisCurve, 1, theU, vD1);
234
235 CSLib_Offset::D0(theP, vD1, offsetValue, IsDirectionChange, theP);
236}
7fd59977 237
238//=======================================================================
239//function : D1
240//purpose :
241//=======================================================================
94f71cad 242void Geom2d_OffsetCurve::D1 (const Standard_Real theU, Pnt2d& theP, Vec2d& theV1) const
243{
7fd59977 244 // P(u) = p(u) + Offset * Ndir / R
245 // with R = || p' ^ Z|| and Ndir = P' ^ Z
246
247 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
248
7fd59977 249 Vec2d V2;
94f71cad 250 basisCurve->D2 (theU, theP, theV1, V2);
32ca7a51 251
94f71cad 252 Standard_Boolean IsDirectionChange = Standard_False;
253 if(theV1.SquareMagnitude() <= gp::Resolution())
254 IsDirectionChange = AdjustDerivative(basisCurve, 2, theU, theV1, V2);
255
256 CSLib_Offset::D1(theP, theV1, V2, offsetValue, IsDirectionChange, theP, theV1);
7fd59977 257}
258
259//=======================================================================
260//function : D2
261//purpose :
262//=======================================================================
263
32ca7a51 264void Geom2d_OffsetCurve::D2 (const Standard_Real theU,
94f71cad 265 Pnt2d& theP,
266 Vec2d& theV1, Vec2d& theV2) const
7fd59977 267{
268 // P(u) = p(u) + Offset * Ndir / R
269 // with R = || p' ^ Z|| and Ndir = P' ^ Z
270
271 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
272
273 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
274 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
275
7fd59977 276 Vec2d V3;
94f71cad 277 basisCurve->D3 (theU, theP, theV1, theV2, V3);
32ca7a51 278
279 Standard_Boolean IsDirectionChange = Standard_False;
94f71cad 280 if(theV1.SquareMagnitude() <= gp::Resolution())
281 IsDirectionChange = AdjustDerivative(basisCurve, 3, theU, theV1, theV2, V3);
32ca7a51 282
94f71cad 283 CSLib_Offset::D2(theP, theV1, theV2, V3, offsetValue, IsDirectionChange, theP, theV1, theV2);
7fd59977 284}
285
286
287//=======================================================================
288//function : D3
289//purpose :
290//=======================================================================
291
32ca7a51 292void Geom2d_OffsetCurve::D3 (const Standard_Real theU,
94f71cad 293 Pnt2d& theP,
294 Vec2d& theV1, Vec2d& theV2, Vec2d& theV3) const
295{
7fd59977 296
297 // P(u) = p(u) + Offset * Ndir / R
298 // with R = || p' ^ Z|| and Ndir = P' ^ Z
299
300 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
301
302 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
303 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
304
305 //P"'(u) = p"'(u) + (Offset / R) * (D3Ndir - (3.0 * Dr/R**2 ) * D2Ndir -
306 // (3.0 * D2r / R2) * DNdir) + (3.0 * Dr * Dr / R4) * DNdir -
307 // (D3r/R2) * Ndir + (6.0 * Dr * Dr / R4) * Ndir +
308 // (6.0 * Dr * D2r / R4) * Ndir - (15.0 * Dr* Dr* Dr /R6) * Ndir
309
94f71cad 310 basisCurve->D3 (theU, theP, theV1, theV2, theV3);
311 Vec2d V4 = basisCurve->DN (theU, 4);
7fd59977 312
32ca7a51 313 Standard_Boolean IsDirectionChange = Standard_False;
94f71cad 314 if(theV1.SquareMagnitude() <= gp::Resolution())
315 IsDirectionChange = AdjustDerivative(basisCurve, 4, theU, theV1, theV2, theV3, V4);
7fd59977 316
94f71cad 317 CSLib_Offset::D3(theP, theV1, theV2, theV3, V4, offsetValue, IsDirectionChange,
318 theP, theV1, theV2, theV3);
319}
7fd59977 320
321//=======================================================================
322//function : DN
323//purpose :
324//=======================================================================
325
326Vec2d Geom2d_OffsetCurve::DN (const Standard_Real U,
327 const Standard_Integer N) const
328{
32ca7a51 329Standard_RangeError_Raise_if (N < 1, "Exception: Geom2d_OffsetCurve::DN(). N<1.");
7fd59977 330
331 gp_Vec2d VN, VBidon;
332 gp_Pnt2d PBidon;
333 switch (N) {
334 case 1: D1( U, PBidon, VN); break;
335 case 2: D2( U, PBidon, VBidon, VN); break;
336 case 3: D3( U, PBidon, VBidon, VBidon, VN); break;
337 default:
32ca7a51 338 Standard_NotImplemented::Raise("Exception: Derivative order is greater than 3. "
339 "Cannot compute of derivative.");
7fd59977 340 }
341
342 return VN;
343}
344
345
346//=======================================================================
347//function : Value
348//purpose :
349//=======================================================================
350
32ca7a51 351void Geom2d_OffsetCurve::Value (const Standard_Real theU,
352 Pnt2d& theP, Pnt2d& thePbasis,
353 Vec2d& theV1basis ) const
94f71cad 354{
32ca7a51 355 basisCurve->D1(theU, thePbasis, theV1basis);
356 D0(theU,theP);
94f71cad 357}
7fd59977 358
359
360//=======================================================================
361//function : D1
362//purpose :
363//=======================================================================
364
365void Geom2d_OffsetCurve::D1 (const Standard_Real U,
366 Pnt2d& P, Pnt2d& Pbasis,
367 Vec2d& V1, Vec2d& V1basis,
368 Vec2d& V2basis ) const
369{
370 // P(u) = p(u) + Offset * Ndir / R
371 // with R = || p' ^ Z|| and Ndir = P' ^ Z
372
373 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
374
7fd59977 375 basisCurve->D2 (U, Pbasis, V1basis, V2basis);
376 V1 = V1basis;
377 Vec2d V2 = V2basis;
378 Standard_Integer Index = 2;
32ca7a51 379 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
7fd59977 380 V1 = basisCurve->DN (U, Index);
381 Index++;
382 }
383 if (Index != 2) {
384 V2 = basisCurve->DN (U, Index);
385 }
94f71cad 386
387 CSLib_Offset::D1(P, V1, V2, offsetValue, Standard_False, P, V1);
7fd59977 388}
389
390
391//=======================================================================
392//function : D2
393//purpose :
394//=======================================================================
395
396void Geom2d_OffsetCurve::D2 (const Standard_Real U,
397 Pnt2d& P, Pnt2d& Pbasis,
398 Vec2d& V1, Vec2d& V2,
399 Vec2d& V1basis, Vec2d& V2basis,
400 Vec2d& V3basis ) const
401{
402 // P(u) = p(u) + Offset * Ndir / R
403 // with R = || p' ^ Z|| and Ndir = P' ^ Z
404
405 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
406
407 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
408 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
409
7fd59977 410 basisCurve->D3 (U, Pbasis, V1basis, V2basis, V3basis);
411 Standard_Integer Index = 2;
412 V1 = V1basis;
413 V2 = V2basis;
414 Vec2d V3 = V3basis;
32ca7a51 415 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
7fd59977 416 V1 = basisCurve->DN (U, Index);
417 Index++;
418 }
419 if (Index != 2) {
420 V2 = basisCurve->DN (U, Index);
421 V3 = basisCurve->DN (U, Index + 1);
422 }
94f71cad 423
424 CSLib_Offset::D2(P, V1, V2, V3, offsetValue, Standard_False, P, V1, V2);
7fd59977 425}
426
427//=======================================================================
428//function : FirstParameter
429//purpose :
430//=======================================================================
431
432Standard_Real Geom2d_OffsetCurve::FirstParameter () const
433{
434 return basisCurve->FirstParameter();
435}
436
437//=======================================================================
438//function : LastParameter
439//purpose :
440//=======================================================================
441
442Standard_Real Geom2d_OffsetCurve::LastParameter () const
443{
444 return basisCurve->LastParameter();
445}
446
447
448//=======================================================================
449//function : Offset
450//purpose :
451//=======================================================================
452
453Standard_Real Geom2d_OffsetCurve::Offset () const { return offsetValue; }
454
455//=======================================================================
456//function : IsClosed
457//purpose :
458//=======================================================================
459
460Standard_Boolean Geom2d_OffsetCurve::IsClosed () const
461{
462 gp_Pnt2d PF, PL;
463 D0(FirstParameter(),PF);
464 D0(LastParameter(),PL);
465 return ( PF.Distance(PL) <= gp::Resolution());
466}
467
468//=======================================================================
469//function : IsCN
470//purpose :
471//=======================================================================
472
473Standard_Boolean Geom2d_OffsetCurve::IsCN (const Standard_Integer N) const
474{
475 Standard_RangeError_Raise_if (N < 0, " " );
476 return basisCurve->IsCN (N + 1);
477}
478
479//=======================================================================
480//function : IsPeriodic
481//purpose :
482//=======================================================================
483
484Standard_Boolean Geom2d_OffsetCurve::IsPeriodic () const
485{
486 return basisCurve->IsPeriodic();
487}
488
489//=======================================================================
490//function : Period
491//purpose :
492//=======================================================================
493
494Standard_Real Geom2d_OffsetCurve::Period() const
495{
496 return basisCurve->Period();
497}
498
499//=======================================================================
500//function : Transform
501//purpose :
502//=======================================================================
503
504void Geom2d_OffsetCurve::Transform (const Trsf2d& T)
505{
506 basisCurve->Transform (T);
507 offsetValue *= Abs(T.ScaleFactor());
508}
509
510
511//=======================================================================
512//function : TransformedParameter
513//purpose :
514//=======================================================================
515
516Standard_Real Geom2d_OffsetCurve::TransformedParameter(const Standard_Real U,
517 const gp_Trsf2d& T) const
518{
519 return basisCurve->TransformedParameter(U,T);
520}
521
522//=======================================================================
523//function : ParametricTransformation
524//purpose :
525//=======================================================================
526
527Standard_Real Geom2d_OffsetCurve::ParametricTransformation(const gp_Trsf2d& T) const
528{
529 return basisCurve->ParametricTransformation(T);
530}
3d58dc49 531
532//=======================================================================
533//function : GetBasisCurveContinuity
534//purpose :
535//=======================================================================
536GeomAbs_Shape Geom2d_OffsetCurve::GetBasisCurveContinuity() const
537{
538 return myBasisCurveContinuity;
539}
94f71cad 540
541
542// ============= Auxiliary functions ===================
543Standard_Boolean AdjustDerivative(const Handle(Geom2d_Curve)& theCurve, Standard_Integer theMaxDerivative,
544 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2,
545 gp_Vec2d& theD3, gp_Vec2d& theD4)
546{
547 static const Standard_Real aTol = gp::Resolution();
548
549 Standard_Boolean IsDirectionChange = Standard_False;
550 const Standard_Real anUinfium = theCurve->FirstParameter();
551 const Standard_Real anUsupremum = theCurve->LastParameter();
552
553 const Standard_Real DivisionFactor = 1.e-3;
554 Standard_Real du;
555 if((anUsupremum >= RealLast()) || (anUinfium <= RealFirst()))
556 du = 0.0;
557 else
558 du = anUsupremum - anUinfium;
559
560 const Standard_Real aDelta = Max(du * DivisionFactor, MinStep);
561
562 //Derivative is approximated by Taylor-series
563 Standard_Integer anIndex = 1; //Derivative order
564 Vec2d V;
565
566 do
567 {
568 V = theCurve->DN(theU, ++anIndex);
569 }
570 while((V.SquareMagnitude() <= aTol) && anIndex < maxDerivOrder);
571
572 Standard_Real u;
573
574 if(theU-anUinfium < aDelta)
575 u = theU+aDelta;
576 else
577 u = theU-aDelta;
578
579 Pnt2d P1, P2;
580 theCurve->D0(Min(theU, u),P1);
581 theCurve->D0(Max(theU, u),P2);
582
583 Vec2d V1(P1,P2);
584 IsDirectionChange = V.Dot(V1) < 0.0;
585 Standard_Real aSign = IsDirectionChange ? -1.0 : 1.0;
586
587 theD1 = V * aSign;
588 gp_Vec2d* aDeriv[3] = {&theD2, &theD3, &theD4};
589 for (Standard_Integer i = 1; i < theMaxDerivative; i++)
590 *(aDeriv[i-1]) = theCurve->DN(theU, anIndex + i) * aSign;
591
592 return IsDirectionChange;
593}