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)
19 #include <CSLib_Offset.hxx>
20 #include <Geom2d_BezierCurve.hxx>
21 #include <Geom2d_BSplineCurve.hxx>
22 #include <Geom2d_Circle.hxx>
23 #include <Geom2d_Curve.hxx>
24 #include <Geom2d_Ellipse.hxx>
25 #include <Geom2d_Geometry.hxx>
26 #include <Geom2d_Hyperbola.hxx>
27 #include <Geom2d_Line.hxx>
28 #include <Geom2d_OffsetCurve.hxx>
29 #include <Geom2d_Parabola.hxx>
30 #include <Geom2d_TrimmedCurve.hxx>
31 #include <Geom2d_UndefinedDerivative.hxx>
32 #include <Geom2d_UndefinedValue.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <gp_Trsf2d.hxx>
36 #include <gp_Vec2d.hxx>
38 #include <Precision.hxx>
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>
45 typedef Geom2d_OffsetCurve OffsetCurve;
46 typedef Geom2d_Curve Curve;
47 typedef gp_Dir2d Dir2d;
48 typedef gp_Pnt2d Pnt2d;
49 typedef gp_Vec2d Vec2d;
50 typedef gp_Trsf2d Trsf2d;
54 //ordre de derivation maximum pour la recherche de la premiere
56 static const int maxDerivOrder = 3;
57 static const Standard_Real MinStep = 1e-7;
58 static const Standard_Real MyAngularToleranceForG1 = Precision::Angular();
60 static gp_Vec2d dummyDerivative; // used as empty value for unused derivatives in AdjustDerivative
62 // Recalculate derivatives in the singular point
63 // Returns true if the direction of derivatives is changed
64 static 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);
68 //=======================================================================
71 //=======================================================================
73 Handle(Geom2d_Geometry) Geom2d_OffsetCurve::Copy () const
75 Handle(Geom2d_OffsetCurve) C;
76 C = new OffsetCurve (basisCurve, offsetValue);
81 //=======================================================================
82 //function : Geom2d_OffsetCurve
83 //purpose : Basis curve cannot be an Offset curve or trimmed from
85 //=======================================================================
87 Geom2d_OffsetCurve::Geom2d_OffsetCurve (const Handle(Geom2d_Curve)& theCurve,
88 const Standard_Real theOffset,
89 const Standard_Boolean isTheNotCheckC0)
90 : offsetValue (theOffset)
92 SetBasisCurve (theCurve, isTheNotCheckC0);
95 //=======================================================================
98 //=======================================================================
100 void Geom2d_OffsetCurve::Reverse ()
102 basisCurve->Reverse();
103 offsetValue = -offsetValue;
106 //=======================================================================
107 //function : ReversedParameter
109 //=======================================================================
111 Standard_Real Geom2d_OffsetCurve::ReversedParameter( const Standard_Real U) const
113 return basisCurve->ReversedParameter( U);
116 //=======================================================================
117 //function : SetBasisCurve
119 //=======================================================================
121 void Geom2d_OffsetCurve::SetBasisCurve (const Handle(Geom2d_Curve)& C,
122 const Standard_Boolean isNotCheckC0)
124 const Standard_Real aUf = C->FirstParameter(),
125 aUl = C->LastParameter();
126 Handle(Geom2d_Curve) aCheckingCurve = C;
127 Standard_Boolean isTrimmed = Standard_False;
129 while(aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ||
130 aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
132 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
134 Handle(Geom2d_TrimmedCurve) aTrimC =
135 Handle(Geom2d_TrimmedCurve)::DownCast(aCheckingCurve);
136 aCheckingCurve = aTrimC->BasisCurve();
137 isTrimmed = Standard_True;
140 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
142 Handle(Geom2d_OffsetCurve) aOC =
143 Handle(Geom2d_OffsetCurve)::DownCast(aCheckingCurve);
144 aCheckingCurve = aOC->BasisCurve();
145 offsetValue += aOC->Offset();
149 myBasisCurveContinuity = aCheckingCurve->Continuity();
151 Standard_Boolean isC0 = !isNotCheckC0 &&
152 (myBasisCurveContinuity == GeomAbs_C0);
154 // Basis curve must be at least C1
155 if (isC0 && aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
157 Handle(Geom2d_BSplineCurve) aBC = Handle(Geom2d_BSplineCurve)::DownCast(aCheckingCurve);
158 if(aBC->IsG1(aUf, aUl, MyAngularToleranceForG1))
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;
166 // Raise exception if still C0
168 Standard_ConstructionError::Raise("Offset on C0 curve");
173 basisCurve = new Geom2d_TrimmedCurve(aCheckingCurve, aUf, aUl);
177 basisCurve = aCheckingCurve;
182 //=======================================================================
183 //function : SetOffsetValue
185 //=======================================================================
187 void Geom2d_OffsetCurve::SetOffsetValue (const Standard_Real D) { offsetValue = D; }
189 //=======================================================================
190 //function : BasisCurve
192 //=======================================================================
194 Handle(Geom2d_Curve) Geom2d_OffsetCurve::BasisCurve () const
199 //=======================================================================
200 //function : Continuity
202 //=======================================================================
204 GeomAbs_Shape Geom2d_OffsetCurve::Continuity () const
206 GeomAbs_Shape OffsetShape=GeomAbs_C0;
207 switch (myBasisCurveContinuity) {
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;
220 //=======================================================================
223 //=======================================================================
225 void Geom2d_OffsetCurve::D0 (const Standard_Real theU,
229 basisCurve->D1 (theU, theP, vD1);
231 Standard_Boolean IsDirectionChange = Standard_False;
232 if(vD1.SquareMagnitude() <= gp::Resolution())
233 IsDirectionChange = AdjustDerivative(basisCurve, 1, theU, vD1);
235 CSLib_Offset::D0(theP, vD1, offsetValue, IsDirectionChange, theP);
238 //=======================================================================
241 //=======================================================================
242 void Geom2d_OffsetCurve::D1 (const Standard_Real theU, Pnt2d& theP, Vec2d& theV1) const
244 // P(u) = p(u) + Offset * Ndir / R
245 // with R = || p' ^ Z|| and Ndir = P' ^ Z
247 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
250 basisCurve->D2 (theU, theP, theV1, V2);
252 Standard_Boolean IsDirectionChange = Standard_False;
253 if(theV1.SquareMagnitude() <= gp::Resolution())
254 IsDirectionChange = AdjustDerivative(basisCurve, 2, theU, theV1, V2);
256 CSLib_Offset::D1(theP, theV1, V2, offsetValue, IsDirectionChange, theP, theV1);
259 //=======================================================================
262 //=======================================================================
264 void Geom2d_OffsetCurve::D2 (const Standard_Real theU,
266 Vec2d& theV1, Vec2d& theV2) const
268 // P(u) = p(u) + Offset * Ndir / R
269 // with R = || p' ^ Z|| and Ndir = P' ^ Z
271 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
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)))
277 basisCurve->D3 (theU, theP, theV1, theV2, V3);
279 Standard_Boolean IsDirectionChange = Standard_False;
280 if(theV1.SquareMagnitude() <= gp::Resolution())
281 IsDirectionChange = AdjustDerivative(basisCurve, 3, theU, theV1, theV2, V3);
283 CSLib_Offset::D2(theP, theV1, theV2, V3, offsetValue, IsDirectionChange, theP, theV1, theV2);
287 //=======================================================================
290 //=======================================================================
292 void Geom2d_OffsetCurve::D3 (const Standard_Real theU,
294 Vec2d& theV1, Vec2d& theV2, Vec2d& theV3) const
297 // P(u) = p(u) + Offset * Ndir / R
298 // with R = || p' ^ Z|| and Ndir = P' ^ Z
300 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
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)))
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
310 basisCurve->D3 (theU, theP, theV1, theV2, theV3);
311 Vec2d V4 = basisCurve->DN (theU, 4);
313 Standard_Boolean IsDirectionChange = Standard_False;
314 if(theV1.SquareMagnitude() <= gp::Resolution())
315 IsDirectionChange = AdjustDerivative(basisCurve, 4, theU, theV1, theV2, theV3, V4);
317 CSLib_Offset::D3(theP, theV1, theV2, theV3, V4, offsetValue, IsDirectionChange,
318 theP, theV1, theV2, theV3);
321 //=======================================================================
324 //=======================================================================
326 Vec2d Geom2d_OffsetCurve::DN (const Standard_Real U,
327 const Standard_Integer N) const
329 Standard_RangeError_Raise_if (N < 1, "Exception: Geom2d_OffsetCurve::DN(). N<1.");
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;
338 Standard_NotImplemented::Raise("Exception: Derivative order is greater than 3. "
339 "Cannot compute of derivative.");
346 //=======================================================================
349 //=======================================================================
351 void Geom2d_OffsetCurve::Value (const Standard_Real theU,
352 Pnt2d& theP, Pnt2d& thePbasis,
353 Vec2d& theV1basis ) const
355 basisCurve->D1(theU, thePbasis, theV1basis);
360 //=======================================================================
363 //=======================================================================
365 void Geom2d_OffsetCurve::D1 (const Standard_Real U,
366 Pnt2d& P, Pnt2d& Pbasis,
367 Vec2d& V1, Vec2d& V1basis,
368 Vec2d& V2basis ) const
370 // P(u) = p(u) + Offset * Ndir / R
371 // with R = || p' ^ Z|| and Ndir = P' ^ Z
373 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
375 basisCurve->D2 (U, Pbasis, V1basis, V2basis);
378 Standard_Integer Index = 2;
379 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
380 V1 = basisCurve->DN (U, Index);
384 V2 = basisCurve->DN (U, Index);
387 CSLib_Offset::D1(P, V1, V2, offsetValue, Standard_False, P, V1);
391 //=======================================================================
394 //=======================================================================
396 void 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
402 // P(u) = p(u) + Offset * Ndir / R
403 // with R = || p' ^ Z|| and Ndir = P' ^ Z
405 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
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)))
410 basisCurve->D3 (U, Pbasis, V1basis, V2basis, V3basis);
411 Standard_Integer Index = 2;
415 while (V1.Magnitude() <= gp::Resolution() && Index <= maxDerivOrder) {
416 V1 = basisCurve->DN (U, Index);
420 V2 = basisCurve->DN (U, Index);
421 V3 = basisCurve->DN (U, Index + 1);
424 CSLib_Offset::D2(P, V1, V2, V3, offsetValue, Standard_False, P, V1, V2);
427 //=======================================================================
428 //function : FirstParameter
430 //=======================================================================
432 Standard_Real Geom2d_OffsetCurve::FirstParameter () const
434 return basisCurve->FirstParameter();
437 //=======================================================================
438 //function : LastParameter
440 //=======================================================================
442 Standard_Real Geom2d_OffsetCurve::LastParameter () const
444 return basisCurve->LastParameter();
448 //=======================================================================
451 //=======================================================================
453 Standard_Real Geom2d_OffsetCurve::Offset () const { return offsetValue; }
455 //=======================================================================
456 //function : IsClosed
458 //=======================================================================
460 Standard_Boolean Geom2d_OffsetCurve::IsClosed () const
463 D0(FirstParameter(),PF);
464 D0(LastParameter(),PL);
465 return ( PF.Distance(PL) <= gp::Resolution());
468 //=======================================================================
471 //=======================================================================
473 Standard_Boolean Geom2d_OffsetCurve::IsCN (const Standard_Integer N) const
475 Standard_RangeError_Raise_if (N < 0, " " );
476 return basisCurve->IsCN (N + 1);
479 //=======================================================================
480 //function : IsPeriodic
482 //=======================================================================
484 Standard_Boolean Geom2d_OffsetCurve::IsPeriodic () const
486 return basisCurve->IsPeriodic();
489 //=======================================================================
492 //=======================================================================
494 Standard_Real Geom2d_OffsetCurve::Period() const
496 return basisCurve->Period();
499 //=======================================================================
500 //function : Transform
502 //=======================================================================
504 void Geom2d_OffsetCurve::Transform (const Trsf2d& T)
506 basisCurve->Transform (T);
507 offsetValue *= Abs(T.ScaleFactor());
511 //=======================================================================
512 //function : TransformedParameter
514 //=======================================================================
516 Standard_Real Geom2d_OffsetCurve::TransformedParameter(const Standard_Real U,
517 const gp_Trsf2d& T) const
519 return basisCurve->TransformedParameter(U,T);
522 //=======================================================================
523 //function : ParametricTransformation
525 //=======================================================================
527 Standard_Real Geom2d_OffsetCurve::ParametricTransformation(const gp_Trsf2d& T) const
529 return basisCurve->ParametricTransformation(T);
532 //=======================================================================
533 //function : GetBasisCurveContinuity
535 //=======================================================================
536 GeomAbs_Shape Geom2d_OffsetCurve::GetBasisCurveContinuity() const
538 return myBasisCurveContinuity;
542 // ============= Auxiliary functions ===================
543 Standard_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)
547 static const Standard_Real aTol = gp::Resolution();
549 Standard_Boolean IsDirectionChange = Standard_False;
550 const Standard_Real anUinfium = theCurve->FirstParameter();
551 const Standard_Real anUsupremum = theCurve->LastParameter();
553 const Standard_Real DivisionFactor = 1.e-3;
555 if((anUsupremum >= RealLast()) || (anUinfium <= RealFirst()))
558 du = anUsupremum - anUinfium;
560 const Standard_Real aDelta = Max(du * DivisionFactor, MinStep);
562 //Derivative is approximated by Taylor-series
563 Standard_Integer anIndex = 1; //Derivative order
568 V = theCurve->DN(theU, ++anIndex);
570 while((V.SquareMagnitude() <= aTol) && anIndex < maxDerivOrder);
574 if(theU-anUinfium < aDelta)
580 theCurve->D0(Min(theU, u),P1);
581 theCurve->D0(Max(theU, u),P2);
584 IsDirectionChange = V.Dot(V1) < 0.0;
585 Standard_Real aSign = IsDirectionChange ? -1.0 : 1.0;
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;
592 return IsDirectionChange;