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 // 24-Aug-95 : xab removed C1 and C2 test : appeller D1 et D2
18 // avec discernement !
19 // 19-09-97 : JPI correction derivee seconde
22 #include <Geom_OffsetCurve.ixx>
27 #include <Geom_Line.hxx>
28 #include <Geom_Circle.hxx>
29 #include <Geom_Ellipse.hxx>
30 #include <Geom_Hyperbola.hxx>
31 #include <Geom_Parabola.hxx>
32 #include <Geom_BezierCurve.hxx>
33 #include <Geom_TrimmedCurve.hxx>
34 #include <Geom_BSplineCurve.hxx>
35 #include <Geom_Geometry.hxx>
37 #include <Geom_UndefinedDerivative.hxx>
38 #include <Geom_UndefinedValue.hxx>
39 #include <Standard_ConstructionError.hxx>
40 #include <Standard_RangeError.hxx>
41 #include <Standard_NotImplemented.hxx>
42 #include <CSLib_Offset.hxx>
44 typedef Geom_OffsetCurve OffsetCurve;
45 typedef Geom_Curve Curve;
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();
59 static gp_Vec 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(
63 const Handle(Geom_Curve)& theCurve, Standard_Integer theMaxDerivative, Standard_Real theU, gp_Vec& theD1,
64 gp_Vec& theD2 = dummyDerivative, gp_Vec& theD3 = dummyDerivative, gp_Vec& theD4 = dummyDerivative);
68 //=======================================================================
71 //=======================================================================
73 Handle(Geom_Geometry) Geom_OffsetCurve::Copy () const {
75 Handle(Geom_OffsetCurve) C;
76 C = new OffsetCurve (basisCurve, offsetValue, direction);
82 //=======================================================================
83 //function : Geom_OffsetCurve
84 //purpose : Basis curve cannot be an Offset curve or trimmed from
86 //=======================================================================
88 Geom_OffsetCurve::Geom_OffsetCurve (const Handle(Geom_Curve)& theCurve,
89 const Standard_Real theOffset,
91 const Standard_Boolean isTheNotCheckC0)
92 : direction(theDir), offsetValue(theOffset)
94 SetBasisCurve (theCurve, isTheNotCheckC0);
98 //=======================================================================
101 //=======================================================================
103 void Geom_OffsetCurve::Reverse ()
105 basisCurve->Reverse();
106 offsetValue = -offsetValue;
110 //=======================================================================
111 //function : ReversedParameter
113 //=======================================================================
115 Standard_Real Geom_OffsetCurve::ReversedParameter( const Standard_Real U) const
117 return basisCurve->ReversedParameter( U);
120 //=======================================================================
121 //function : Direction
123 //=======================================================================
125 const gp_Dir& Geom_OffsetCurve::Direction () const
126 { return direction; }
128 //=======================================================================
129 //function : SetDirection
131 //=======================================================================
133 void Geom_OffsetCurve::SetDirection (const Dir& V)
136 //=======================================================================
137 //function : SetOffsetValue
139 //=======================================================================
141 void Geom_OffsetCurve::SetOffsetValue (const Standard_Real D)
145 //=======================================================================
146 //function : IsPeriodic
148 //=======================================================================
150 Standard_Boolean Geom_OffsetCurve::IsPeriodic () const
152 return basisCurve->IsPeriodic();
155 //=======================================================================
158 //=======================================================================
160 Standard_Real Geom_OffsetCurve::Period () const
162 return basisCurve->Period();
165 //=======================================================================
166 //function : SetBasisCurve
168 //=======================================================================
170 void Geom_OffsetCurve::SetBasisCurve (const Handle(Geom_Curve)& C,
171 const Standard_Boolean isNotCheckC0)
173 const Standard_Real aUf = C->FirstParameter(),
174 aUl = C->LastParameter();
175 Handle(Geom_Curve) aCheckingCurve = Handle(Geom_Curve)::DownCast(C->Copy());
176 Standard_Boolean isTrimmed = Standard_False;
178 while(aCheckingCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)) ||
179 aCheckingCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve)))
181 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
183 Handle(Geom_TrimmedCurve) aTrimC =
184 Handle(Geom_TrimmedCurve)::DownCast(aCheckingCurve);
185 aCheckingCurve = aTrimC->BasisCurve();
186 isTrimmed = Standard_True;
189 if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve)))
191 Handle(Geom_OffsetCurve) aOC =
192 Handle(Geom_OffsetCurve)::DownCast(aCheckingCurve);
193 aCheckingCurve = aOC->BasisCurve();
194 Standard_Real PrevOff = aOC->Offset();
195 gp_Vec V1(aOC->Direction());
196 gp_Vec V2(direction);
197 gp_Vec Vdir(PrevOff*V1 + offsetValue*V2);
199 if (offsetValue >= 0.)
201 offsetValue = Vdir.Magnitude();
202 direction.SetXYZ(Vdir.XYZ());
206 offsetValue = -Vdir.Magnitude();
207 direction.SetXYZ((-Vdir).XYZ());
212 myBasisCurveContinuity = aCheckingCurve->Continuity();
214 Standard_Boolean isC0 = !isNotCheckC0 &&
215 (myBasisCurveContinuity == GeomAbs_C0);
217 // Basis curve must be at least C1
218 if (isC0 && aCheckingCurve->IsKind(STANDARD_TYPE(Geom_BSplineCurve)))
220 Handle(Geom_BSplineCurve) aBC = Handle(Geom_BSplineCurve)::DownCast(aCheckingCurve);
221 if(aBC->IsG1(aUf, aUl, MyAngularToleranceForG1))
223 //Checking if basis curve has more smooth (C1, G2 and above) is not done.
224 //It can be done in case of need.
225 myBasisCurveContinuity = GeomAbs_G1;
226 isC0 = Standard_False;
229 // Raise exception if still C0
231 Standard_ConstructionError::Raise("Offset on C0 curve");
236 basisCurve = new Geom_TrimmedCurve(aCheckingCurve, aUf, aUl);
240 basisCurve = aCheckingCurve;
246 //=======================================================================
247 //function : BasisCurve
249 //=======================================================================
251 Handle(Geom_Curve) Geom_OffsetCurve::BasisCurve () const
257 //=======================================================================
258 //function : Continuity
260 //=======================================================================
262 GeomAbs_Shape Geom_OffsetCurve::Continuity () const {
264 GeomAbs_Shape OffsetShape=GeomAbs_C0;
265 switch (myBasisCurveContinuity) {
266 case GeomAbs_C0 : OffsetShape = GeomAbs_C0; break;
267 case GeomAbs_C1 : OffsetShape = GeomAbs_C0; break;
268 case GeomAbs_C2 : OffsetShape = GeomAbs_C1; break;
269 case GeomAbs_C3 : OffsetShape = GeomAbs_C2; break;
270 case GeomAbs_CN : OffsetShape = GeomAbs_CN; break;
271 case GeomAbs_G1 : OffsetShape = GeomAbs_G1; break;
272 case GeomAbs_G2 : OffsetShape = GeomAbs_G2; break;
278 //=======================================================================
281 //=======================================================================
283 void Geom_OffsetCurve::D0 (const Standard_Real U, Pnt& P) const
287 D0(U,P,PBasis,VBasis);
291 //=======================================================================
294 //=======================================================================
296 void Geom_OffsetCurve::D1 (const Standard_Real U, Pnt& P, Vec& V1) const
299 gp_Vec V1Basis,V2Basis;
300 D1(U,P,PBasis,V1,V1Basis,V2Basis);
305 //=======================================================================
308 //=======================================================================
310 void Geom_OffsetCurve::D2 (const Standard_Real U, Pnt& P, Vec& V1, Vec& V2) const
313 gp_Vec V1Basis,V2Basis,V3Basis;
314 D2(U,P,PBasis,V1,V2,V1Basis,V2Basis,V3Basis);
318 //=======================================================================
321 //=======================================================================
323 void Geom_OffsetCurve::D3 (const Standard_Real theU, Pnt& theP, Vec& theV1, Vec& theV2, Vec& theV3) const
325 // P(u) = p(u) + Offset * Ndir / R
326 // with R = || p' ^ V|| and Ndir = P' ^ direction (local normal direction)
328 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
330 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
331 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
333 //P"'(u) = p"'(u) + (Offset / R) * (D3Ndir - (3.0 * Dr/R**2) * D2Ndir -
334 // (3.0 * D2r / R2) * DNdir + (3.0 * Dr * Dr / R4) * DNdir -
335 // (D3r/R2) * Ndir + (6.0 * Dr * Dr / R4) * Ndir +
336 // (6.0 * Dr * D2r / R4) * Ndir - (15.0 * Dr* Dr* Dr /R6) * Ndir
338 Standard_Boolean IsDirectionChange = Standard_False;
340 basisCurve->D3 (theU, theP, theV1, theV2, theV3);
341 Vec aV4 = basisCurve->DN (theU, 4);
342 if(theV1.SquareMagnitude() <= gp::Resolution())
343 IsDirectionChange = AdjustDerivative(basisCurve, 4, theU, theV1, theV2, theV3, aV4);
345 CSLib_Offset::D3(theP, theV1, theV2, theV3, aV4, direction, offsetValue,
346 IsDirectionChange, theP, theV1, theV2, theV3);
350 //=======================================================================
353 //=======================================================================
355 Vec Geom_OffsetCurve::DN (const Standard_Real U, const Standard_Integer N) const
357 Standard_RangeError_Raise_if (N < 1, "Exception: "
358 "Geom_OffsetCurve::DN(...). N<1.");
368 D2( U, Ptemp, Vtemp, VN);
371 D3( U, Ptemp, Vtemp, Vtemp, VN);
374 Standard_NotImplemented::Raise("Exception: "
375 "Derivative order is greater than 3. Cannot compute of derivative.");
381 //=======================================================================
384 //=======================================================================
386 void Geom_OffsetCurve::D0(const Standard_Real theU, gp_Pnt& theP,
387 gp_Pnt& thePbasis, gp_Vec& theV1basis)const
389 basisCurve->D1(theU, thePbasis, theV1basis);
390 Standard_Boolean IsDirectionChange = Standard_False;
391 if(theV1basis.SquareMagnitude() <= gp::Resolution())
392 IsDirectionChange = AdjustDerivative(basisCurve, 1, theU, theV1basis);
394 CSLib_Offset::D0(thePbasis, theV1basis, direction, offsetValue, IsDirectionChange, theP);
397 //=======================================================================
400 //=======================================================================
402 void Geom_OffsetCurve::D1 ( const Standard_Real theU,
403 Pnt& theP , Pnt& thePBasis ,
404 Vec& theV1, Vec& theV1basis, Vec& theV2basis) const {
406 // P(u) = p(u) + Offset * Ndir / R
407 // with R = || p' ^ V|| and Ndir = P' ^ direction (local normal direction)
409 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
411 basisCurve->D2 (theU, thePBasis, theV1basis, theV2basis);
413 Standard_Boolean IsDirectionChange = Standard_False;
414 if(theV1basis.SquareMagnitude() <= gp::Resolution())
415 IsDirectionChange = AdjustDerivative(basisCurve, 2, theU, theV1basis, theV2basis);
417 CSLib_Offset::D1(thePBasis, theV1basis, theV2basis, direction, offsetValue, IsDirectionChange, theP, theV1);
421 //=======================================================================
424 //=======================================================================
426 void Geom_OffsetCurve::D2 (const Standard_Real theU,
427 Pnt& theP, Pnt& thePBasis,
428 Vec& theV1, Vec& theV2,
429 Vec& theV1basis, Vec& theV2basis, Vec& theV3basis) const
431 // P(u) = p(u) + Offset * Ndir / R
432 // with R = || p' ^ V|| and Ndir = P' ^ direction (local normal direction)
434 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
436 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
437 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
439 Standard_Boolean IsDirectionChange = Standard_False;
441 basisCurve->D3 (theU, thePBasis, theV1basis, theV2basis, theV3basis);
443 if(theV1basis.SquareMagnitude() <= gp::Resolution())
444 IsDirectionChange = AdjustDerivative(basisCurve, 3, theU, theV1basis, theV2basis, theV3basis);
446 CSLib_Offset::D2(thePBasis, theV1basis, theV2basis, theV3basis, direction, offsetValue,
447 IsDirectionChange, theP, theV1, theV2);
451 //=======================================================================
452 //function : FirstParameter
454 //=======================================================================
456 Standard_Real Geom_OffsetCurve::FirstParameter () const {
458 return basisCurve->FirstParameter();
462 //=======================================================================
463 //function : LastParameter
465 //=======================================================================
467 Standard_Real Geom_OffsetCurve::LastParameter () const {
469 return basisCurve->LastParameter();
473 //=======================================================================
476 //=======================================================================
478 Standard_Real Geom_OffsetCurve::Offset () const { return offsetValue; }
480 //=======================================================================
483 //=======================================================================
485 void Geom_OffsetCurve::Value (const Standard_Real theU, Pnt& theP,
486 Pnt& thePbasis, Vec& theV1basis) const
488 if (myBasisCurveContinuity == GeomAbs_C0)
489 Geom_UndefinedValue::Raise("Exception: Basis curve is C0 continuity!");
491 basisCurve->D1(theU, thePbasis, theV1basis);
496 //=======================================================================
497 //function : IsClosed
499 //=======================================================================
501 Standard_Boolean Geom_OffsetCurve::IsClosed () const
504 D0(FirstParameter(),PF);
505 D0(LastParameter(),PL);
506 return ( PF.Distance(PL) <= gp::Resolution());
511 //=======================================================================
514 //=======================================================================
516 Standard_Boolean Geom_OffsetCurve::IsCN (const Standard_Integer N) const {
518 Standard_RangeError_Raise_if (N < 0, " ");
519 return basisCurve->IsCN (N + 1);
523 //=======================================================================
524 //function : Transform
526 //=======================================================================
528 void Geom_OffsetCurve::Transform (const Trsf& T) {
530 basisCurve->Transform (T);
531 direction.Transform(T);
532 offsetValue *= T.ScaleFactor();
535 //=======================================================================
536 //function : TransformedParameter
538 //=======================================================================
540 Standard_Real Geom_OffsetCurve::TransformedParameter(const Standard_Real U,
541 const gp_Trsf& T) const
543 return basisCurve->TransformedParameter(U,T);
546 //=======================================================================
547 //function : ParametricTransformation
549 //=======================================================================
551 Standard_Real Geom_OffsetCurve::ParametricTransformation(const gp_Trsf& T)
554 return basisCurve->ParametricTransformation(T);
557 //=======================================================================
558 //function : GetBasisCurveContinuity
560 //=======================================================================
561 GeomAbs_Shape Geom_OffsetCurve::GetBasisCurveContinuity() const
563 return myBasisCurveContinuity;
567 // ============= Auxiliary functions ===================
568 Standard_Boolean AdjustDerivative(const Handle(Geom_Curve)& theCurve, Standard_Integer theMaxDerivative,
569 Standard_Real theU, gp_Vec& theD1, gp_Vec& theD2,
570 gp_Vec& theD3, gp_Vec& theD4)
572 static const Standard_Real aTol = gp::Resolution();
574 Standard_Boolean IsDirectionChange = Standard_False;
575 const Standard_Real anUinfium = theCurve->FirstParameter();
576 const Standard_Real anUsupremum = theCurve->LastParameter();
578 const Standard_Real DivisionFactor = 1.e-3;
580 if((anUsupremum >= RealLast()) || (anUinfium <= RealFirst()))
583 du = anUsupremum - anUinfium;
585 const Standard_Real aDelta = Max(du * DivisionFactor, MinStep);
587 //Derivative is approximated by Taylor-series
588 Standard_Integer anIndex = 1; //Derivative order
593 V = theCurve->DN(theU, ++anIndex);
595 while((V.SquareMagnitude() <= aTol) && anIndex < maxDerivOrder);
599 if(theU-anUinfium < aDelta)
605 theCurve->D0(Min(theU, u), P1);
606 theCurve->D0(Max(theU, u), P2);
609 IsDirectionChange = V.Dot(V1) < 0.0;
610 Standard_Real aSign = IsDirectionChange ? -1.0 : 1.0;
613 gp_Vec* aDeriv[3] = {&theD2, &theD3, &theD4};
614 for (Standard_Integer i = 1; i < theMaxDerivative; i++)
615 *(aDeriv[i-1]) = theCurve->DN(theU, anIndex + i) * aSign;
617 return IsDirectionChange;