1 // Created on: 1991-06-25
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // modified by Edward AGAPOV (eap) Jan 28 2002 --- DN(), occ143(BUC60654)
21 #include <Geom2d_OffsetCurve.ixx>
23 #include <Standard_ConstructionError.hxx>
24 #include <Standard_RangeError.hxx>
25 #include <Standard_NotImplemented.hxx>
26 #include <CSLib_Offset.hxx>
27 #include <Geom2d_UndefinedDerivative.hxx>
28 #include <Geom2d_UndefinedValue.hxx>
29 #include <Geom2d_Line.hxx>
30 #include <Geom2d_Circle.hxx>
31 #include <Geom2d_Ellipse.hxx>
32 #include <Geom2d_Hyperbola.hxx>
33 #include <Geom2d_Parabola.hxx>
34 #include <Geom2d_BezierCurve.hxx>
35 #include <Geom2d_BSplineCurve.hxx>
36 #include <Geom2d_TrimmedCurve.hxx>
38 #include <Precision.hxx>
40 typedef Handle(Geom2d_OffsetCurve) Handle(OffsetCurve);
41 typedef Geom2d_OffsetCurve OffsetCurve;
42 typedef Handle(Geom2d_Geometry) Handle(Geometry);
43 typedef Handle(Geom2d_Curve) Handle(Curve);
44 typedef Geom2d_Curve Curve;
45 typedef gp_Dir2d Dir2d;
46 typedef gp_Pnt2d Pnt2d;
47 typedef gp_Vec2d Vec2d;
48 typedef gp_Trsf2d Trsf2d;
52 //ordre de derivation maximum pour la recherche de la premiere
54 static const int maxDerivOrder = 3;
55 static const Standard_Real MinStep = 1e-7;
56 static const Standard_Real MyAngularToleranceForG1 = Precision::Angular();
58 static gp_Vec2d dummyDerivative; // used as empty value for unused derivatives in AdjustDerivative
60 // Recalculate derivatives in the singular point
61 // Returns true if the direction of derivatives is changed
62 static Standard_Boolean AdjustDerivative(const Handle(Geom2d_Curve)& theCurve, Standard_Integer theMaxDerivative,
63 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2 = dummyDerivative,
64 gp_Vec2d& theD3 = dummyDerivative, gp_Vec2d& theD4 = dummyDerivative);
66 //=======================================================================
69 //=======================================================================
71 Handle(Geom2d_Geometry) Geom2d_OffsetCurve::Copy () const
73 Handle(OffsetCurve) C;
74 C = new OffsetCurve (basisCurve, offsetValue);
79 //=======================================================================
80 //function : Geom2d_OffsetCurve
81 //purpose : Basis curve cannot be an Offset curve or trimmed from
83 //=======================================================================
85 Geom2d_OffsetCurve::Geom2d_OffsetCurve (const Handle(Geom2d_Curve)& theCurve,
86 const Standard_Real theOffset,
87 const Standard_Boolean isTheNotCheckC0)
88 : offsetValue (theOffset)
90 SetBasisCurve (theCurve, isTheNotCheckC0);
93 //=======================================================================
96 //=======================================================================
98 void Geom2d_OffsetCurve::Reverse ()
100 basisCurve->Reverse();
101 offsetValue = -offsetValue;
104 //=======================================================================
105 //function : ReversedParameter
107 //=======================================================================
109 Standard_Real Geom2d_OffsetCurve::ReversedParameter( const Standard_Real U) const
111 return basisCurve->ReversedParameter( U);
114 //=======================================================================
115 //function : SetBasisCurve
117 //=======================================================================
119 void Geom2d_OffsetCurve::SetBasisCurve (const Handle(Curve)& C,
120 const Standard_Boolean isNotCheckC0)
122 const Standard_Real aUf = C->FirstParameter(),
123 aUl = C->LastParameter();
124 Handle(Geom2d_Curve) aCheckingCurve = C;
125 Standard_Boolean isTrimmed = Standard_False;
127 while(aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ||
128 aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
130 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
132 Handle(Geom2d_TrimmedCurve) aTrimC =
133 Handle(Geom2d_TrimmedCurve)::DownCast(aCheckingCurve);
134 aCheckingCurve = aTrimC->BasisCurve();
135 isTrimmed = Standard_True;
138 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
140 Handle(Geom2d_OffsetCurve) aOC =
141 Handle(Geom2d_OffsetCurve)::DownCast(aCheckingCurve);
142 aCheckingCurve = aOC->BasisCurve();
143 offsetValue += aOC->Offset();
147 myBasisCurveContinuity = aCheckingCurve->Continuity();
149 Standard_Boolean isC0 = !isNotCheckC0 &&
150 (myBasisCurveContinuity == GeomAbs_C0);
152 // Basis curve must be at least C1
153 if (isC0 && aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
155 Handle(Geom2d_BSplineCurve) aBC = Handle(Geom2d_BSplineCurve)::DownCast(aCheckingCurve);
156 if(aBC->IsG1(aUf, aUl, MyAngularToleranceForG1))
158 //Checking if basis curve has more smooth (C1, G2 and above) is not done.
159 //It can be done in case of need.
160 myBasisCurveContinuity = GeomAbs_G1;
161 isC0 = Standard_False;
164 // Raise exception if still C0
166 Standard_ConstructionError::Raise("Offset on C0 curve");
171 basisCurve = new Geom2d_TrimmedCurve(aCheckingCurve, aUf, aUl);
175 basisCurve = aCheckingCurve;
180 //=======================================================================
181 //function : SetOffsetValue
183 //=======================================================================
185 void Geom2d_OffsetCurve::SetOffsetValue (const Standard_Real D) { offsetValue = D; }
187 //=======================================================================
188 //function : BasisCurve
190 //=======================================================================
192 Handle(Curve) Geom2d_OffsetCurve::BasisCurve () const
197 //=======================================================================
198 //function : Continuity
200 //=======================================================================
202 GeomAbs_Shape Geom2d_OffsetCurve::Continuity () const
204 GeomAbs_Shape OffsetShape=GeomAbs_C0;
205 switch (myBasisCurveContinuity) {
206 case GeomAbs_C0 : OffsetShape = GeomAbs_C0; break;
207 case GeomAbs_C1 : OffsetShape = GeomAbs_C0; break;
208 case GeomAbs_C2 : OffsetShape = GeomAbs_C1; break;
209 case GeomAbs_C3 : OffsetShape = GeomAbs_C2; break;
210 case GeomAbs_CN : OffsetShape = GeomAbs_CN; break;
211 case GeomAbs_G1 : OffsetShape = GeomAbs_G1; break;
212 case GeomAbs_G2 : OffsetShape = GeomAbs_G2; break;
218 //=======================================================================
221 //=======================================================================
223 void Geom2d_OffsetCurve::D0 (const Standard_Real theU,
227 basisCurve->D1 (theU, theP, vD1);
229 Standard_Boolean IsDirectionChange = Standard_False;
230 if(vD1.SquareMagnitude() <= gp::Resolution())
231 IsDirectionChange = AdjustDerivative(basisCurve, 1, theU, vD1);
233 CSLib_Offset::D0(theP, vD1, offsetValue, IsDirectionChange, theP);
236 //=======================================================================
239 //=======================================================================
240 void Geom2d_OffsetCurve::D1 (const Standard_Real theU, Pnt2d& theP, Vec2d& theV1) const
242 // P(u) = p(u) + Offset * Ndir / R
243 // with R = || p' ^ Z|| and Ndir = P' ^ Z
245 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
248 basisCurve->D2 (theU, theP, theV1, V2);
250 Standard_Boolean IsDirectionChange = Standard_False;
251 if(theV1.SquareMagnitude() <= gp::Resolution())
252 IsDirectionChange = AdjustDerivative(basisCurve, 2, theU, theV1, V2);
254 CSLib_Offset::D1(theP, theV1, V2, offsetValue, IsDirectionChange, theP, theV1);
257 //=======================================================================
260 //=======================================================================
262 void Geom2d_OffsetCurve::D2 (const Standard_Real theU,
264 Vec2d& theV1, Vec2d& theV2) const
266 // P(u) = p(u) + Offset * Ndir / R
267 // with R = || p' ^ Z|| and Ndir = P' ^ Z
269 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
271 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
272 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
275 basisCurve->D3 (theU, theP, theV1, theV2, V3);
277 Standard_Boolean IsDirectionChange = Standard_False;
278 if(theV1.SquareMagnitude() <= gp::Resolution())
279 IsDirectionChange = AdjustDerivative(basisCurve, 3, theU, theV1, theV2, V3);
281 CSLib_Offset::D2(theP, theV1, theV2, V3, offsetValue, IsDirectionChange, theP, theV1, theV2);
285 //=======================================================================
288 //=======================================================================
290 void Geom2d_OffsetCurve::D3 (const Standard_Real theU,
292 Vec2d& theV1, Vec2d& theV2, Vec2d& theV3) const
295 // P(u) = p(u) + Offset * Ndir / R
296 // with R = || p' ^ Z|| and Ndir = P' ^ Z
298 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
300 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
301 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
303 //P"'(u) = p"'(u) + (Offset / R) * (D3Ndir - (3.0 * Dr/R**2 ) * D2Ndir -
304 // (3.0 * D2r / R2) * DNdir) + (3.0 * Dr * Dr / R4) * DNdir -
305 // (D3r/R2) * Ndir + (6.0 * Dr * Dr / R4) * Ndir +
306 // (6.0 * Dr * D2r / R4) * Ndir - (15.0 * Dr* Dr* Dr /R6) * Ndir
308 basisCurve->D3 (theU, theP, theV1, theV2, theV3);
309 Vec2d V4 = basisCurve->DN (theU, 4);
311 Standard_Boolean IsDirectionChange = Standard_False;
312 if(theV1.SquareMagnitude() <= gp::Resolution())
313 IsDirectionChange = AdjustDerivative(basisCurve, 4, theU, theV1, theV2, theV3, V4);
315 CSLib_Offset::D3(theP, theV1, theV2, theV3, V4, offsetValue, IsDirectionChange,
316 theP, theV1, theV2, theV3);
319 //=======================================================================
322 //=======================================================================
324 Vec2d Geom2d_OffsetCurve::DN (const Standard_Real U,
325 const Standard_Integer N) const
327 Standard_RangeError_Raise_if (N < 1, "Exception: Geom2d_OffsetCurve::DN(). N<1.");
332 case 1: D1( U, PBidon, VN); break;
333 case 2: D2( U, PBidon, VBidon, VN); break;
334 case 3: D3( U, PBidon, VBidon, VBidon, VN); break;
336 Standard_NotImplemented::Raise("Exception: Derivative order is greater than 3. "
337 "Cannot compute of derivative.");
344 //=======================================================================
347 //=======================================================================
349 void Geom2d_OffsetCurve::Value (const Standard_Real theU,
350 Pnt2d& theP, Pnt2d& thePbasis,
351 Vec2d& theV1basis ) const
353 basisCurve->D1(theU, thePbasis, theV1basis);
358 //=======================================================================
361 //=======================================================================
363 void Geom2d_OffsetCurve::D1 (const Standard_Real U,
364 Pnt2d& P, Pnt2d& Pbasis,
365 Vec2d& V1, Vec2d& V1basis,
366 Vec2d& V2basis ) const
368 // P(u) = p(u) + Offset * Ndir / R
369 // with R = || p' ^ Z|| and Ndir = P' ^ Z
371 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
373 basisCurve->D2 (U, Pbasis, V1basis, V2basis);
376 Standard_Integer Index = 2;
377 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
378 V1 = basisCurve->DN (U, Index);
382 V2 = basisCurve->DN (U, Index);
385 CSLib_Offset::D1(P, V1, V2, offsetValue, Standard_False, P, V1);
389 //=======================================================================
392 //=======================================================================
394 void Geom2d_OffsetCurve::D2 (const Standard_Real U,
395 Pnt2d& P, Pnt2d& Pbasis,
396 Vec2d& V1, Vec2d& V2,
397 Vec2d& V1basis, Vec2d& V2basis,
398 Vec2d& V3basis ) const
400 // P(u) = p(u) + Offset * Ndir / R
401 // with R = || p' ^ Z|| and Ndir = P' ^ Z
403 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
405 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
406 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
408 basisCurve->D3 (U, Pbasis, V1basis, V2basis, V3basis);
409 Standard_Integer Index = 2;
413 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
414 V1 = basisCurve->DN (U, Index);
418 V2 = basisCurve->DN (U, Index);
419 V3 = basisCurve->DN (U, Index + 1);
422 CSLib_Offset::D2(P, V1, V2, V3, offsetValue, Standard_False, P, V1, V2);
425 //=======================================================================
426 //function : FirstParameter
428 //=======================================================================
430 Standard_Real Geom2d_OffsetCurve::FirstParameter () const
432 return basisCurve->FirstParameter();
435 //=======================================================================
436 //function : LastParameter
438 //=======================================================================
440 Standard_Real Geom2d_OffsetCurve::LastParameter () const
442 return basisCurve->LastParameter();
446 //=======================================================================
449 //=======================================================================
451 Standard_Real Geom2d_OffsetCurve::Offset () const { return offsetValue; }
453 //=======================================================================
454 //function : IsClosed
456 //=======================================================================
458 Standard_Boolean Geom2d_OffsetCurve::IsClosed () const
461 D0(FirstParameter(),PF);
462 D0(LastParameter(),PL);
463 return ( PF.Distance(PL) <= gp::Resolution());
466 //=======================================================================
469 //=======================================================================
471 Standard_Boolean Geom2d_OffsetCurve::IsCN (const Standard_Integer N) const
473 Standard_RangeError_Raise_if (N < 0, " " );
474 return basisCurve->IsCN (N + 1);
477 //=======================================================================
478 //function : IsPeriodic
480 //=======================================================================
482 Standard_Boolean Geom2d_OffsetCurve::IsPeriodic () const
484 return basisCurve->IsPeriodic();
487 //=======================================================================
490 //=======================================================================
492 Standard_Real Geom2d_OffsetCurve::Period() const
494 return basisCurve->Period();
497 //=======================================================================
498 //function : Transform
500 //=======================================================================
502 void Geom2d_OffsetCurve::Transform (const Trsf2d& T)
504 basisCurve->Transform (T);
505 offsetValue *= Abs(T.ScaleFactor());
509 //=======================================================================
510 //function : TransformedParameter
512 //=======================================================================
514 Standard_Real Geom2d_OffsetCurve::TransformedParameter(const Standard_Real U,
515 const gp_Trsf2d& T) const
517 return basisCurve->TransformedParameter(U,T);
520 //=======================================================================
521 //function : ParametricTransformation
523 //=======================================================================
525 Standard_Real Geom2d_OffsetCurve::ParametricTransformation(const gp_Trsf2d& T) const
527 return basisCurve->ParametricTransformation(T);
530 //=======================================================================
531 //function : GetBasisCurveContinuity
533 //=======================================================================
534 GeomAbs_Shape Geom2d_OffsetCurve::GetBasisCurveContinuity() const
536 return myBasisCurveContinuity;
540 // ============= Auxiliary functions ===================
541 Standard_Boolean AdjustDerivative(const Handle(Geom2d_Curve)& theCurve, Standard_Integer theMaxDerivative,
542 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2,
543 gp_Vec2d& theD3, gp_Vec2d& theD4)
545 static const Standard_Real aTol = gp::Resolution();
547 Standard_Boolean IsDirectionChange = Standard_False;
548 const Standard_Real anUinfium = theCurve->FirstParameter();
549 const Standard_Real anUsupremum = theCurve->LastParameter();
551 const Standard_Real DivisionFactor = 1.e-3;
553 if((anUsupremum >= RealLast()) || (anUinfium <= RealFirst()))
556 du = anUsupremum - anUinfium;
558 const Standard_Real aDelta = Max(du * DivisionFactor, MinStep);
560 //Derivative is approximated by Taylor-series
561 Standard_Integer anIndex = 1; //Derivative order
566 V = theCurve->DN(theU, ++anIndex);
568 while((V.SquareMagnitude() <= aTol) && anIndex < maxDerivOrder);
572 if(theU-anUinfium < aDelta)
578 theCurve->D0(Min(theU, u),P1);
579 theCurve->D0(Max(theU, u),P2);
582 IsDirectionChange = V.Dot(V1) < 0.0;
583 Standard_Real aSign = IsDirectionChange ? -1.0 : 1.0;
586 gp_Vec2d* aDeriv[3] = {&theD2, &theD3, &theD4};
587 for (Standard_Integer i = 1; i < theMaxDerivative; i++)
588 *(aDeriv[i-1]) = theCurve->DN(theU, anIndex + i) * aSign;
590 return IsDirectionChange;