1 // Created on: 1991-06-25
2 // Created by: JCV
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16 
17 //  modified by Edward AGAPOV (eap) Jan 28 2002 --- DN(), occ143(BUC60654)
18 
19 #include <Geom2d_BezierCurve.hxx>
20 #include <Geom2d_BSplineCurve.hxx>
21 #include <Geom2d_Circle.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2d_Ellipse.hxx>
24 #include <Geom2d_Geometry.hxx>
25 #include <Geom2d_Hyperbola.hxx>
26 #include <Geom2d_Line.hxx>
27 #include <Geom2d_OffsetCurve.hxx>
28 #include <Geom2d_Parabola.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <Geom2d_UndefinedDerivative.hxx>
31 #include <Geom2d_UndefinedValue.hxx>
32 #include <gp.hxx>
33 #include <gp_Pnt2d.hxx>
34 #include <gp_Trsf2d.hxx>
35 #include <gp_Vec2d.hxx>
36 #include <gp_XY.hxx>
37 #include <Precision.hxx>
38 #include <Standard_ConstructionError.hxx>
39 #include <Standard_NoSuchObject.hxx>
40 #include <Standard_NotImplemented.hxx>
41 #include <Standard_RangeError.hxx>
42 #include <Standard_Type.hxx>
43 
44 IMPLEMENT_STANDARD_RTTIEXT(Geom2d_OffsetCurve,Geom2d_Curve)
45 
46 static const Standard_Real MyAngularToleranceForG1 = Precision::Angular();
47 
48 
49 //=======================================================================
50 //function : Copy
51 //purpose  :
52 //=======================================================================
53 
Handle(Geom2d_Geometry)54 Handle(Geom2d_Geometry) Geom2d_OffsetCurve::Copy () const
55 {
56   Handle(Geom2d_OffsetCurve) C;
57   C = new Geom2d_OffsetCurve (basisCurve, offsetValue);
58   return C;
59 }
60 
61 
62 //=======================================================================
63 //function : Geom2d_OffsetCurve
64 //purpose  : Basis curve cannot be an Offset curve or trimmed from
65 //            offset curve.
66 //=======================================================================
67 
Geom2d_OffsetCurve(const Handle (Geom2d_Curve)& theCurve,const Standard_Real theOffset,const Standard_Boolean isTheNotCheckC0)68 Geom2d_OffsetCurve::Geom2d_OffsetCurve (const Handle(Geom2d_Curve)& theCurve,
69                                         const Standard_Real theOffset,
70                                         const Standard_Boolean isTheNotCheckC0)
71 : offsetValue (theOffset)
72 {
73   SetBasisCurve (theCurve, isTheNotCheckC0);
74 }
75 
76 //=======================================================================
77 //function : Reverse
78 //purpose  :
79 //=======================================================================
80 
Reverse()81 void Geom2d_OffsetCurve::Reverse ()
82 {
83   basisCurve->Reverse();
84   offsetValue = -offsetValue;
85 }
86 
87 //=======================================================================
88 //function : ReversedParameter
89 //purpose  :
90 //=======================================================================
91 
ReversedParameter(const Standard_Real U) const92 Standard_Real Geom2d_OffsetCurve::ReversedParameter( const Standard_Real U) const
93 {
94   return basisCurve->ReversedParameter( U);
95 }
96 
97 //=======================================================================
98 //function : SetBasisCurve
99 //purpose  :
100 //=======================================================================
101 
SetBasisCurve(const Handle (Geom2d_Curve)& C,const Standard_Boolean isNotCheckC0)102 void Geom2d_OffsetCurve::SetBasisCurve (const Handle(Geom2d_Curve)& C,
103                                         const Standard_Boolean isNotCheckC0)
104 {
105   const Standard_Real aUf = C->FirstParameter(),
106                       aUl = C->LastParameter();
107   Handle(Geom2d_Curve) aCheckingCurve = C;
108   Standard_Boolean isTrimmed = Standard_False;
109 
110   while(aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ||
111         aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
112   {
113     if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
114     {
115       Handle(Geom2d_TrimmedCurve) aTrimC =
116                 Handle(Geom2d_TrimmedCurve)::DownCast(aCheckingCurve);
117       aCheckingCurve = aTrimC->BasisCurve();
118       isTrimmed = Standard_True;
119     }
120 
121     if (aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
122     {
123       Handle(Geom2d_OffsetCurve) aOC =
124                 Handle(Geom2d_OffsetCurve)::DownCast(aCheckingCurve);
125       aCheckingCurve = aOC->BasisCurve();
126       offsetValue += aOC->Offset();
127     }
128   }
129 
130   myBasisCurveContinuity = aCheckingCurve->Continuity();
131 
132   Standard_Boolean isC0 = !isNotCheckC0 &&
133                           (myBasisCurveContinuity == GeomAbs_C0);
134 
135   // Basis curve must be at least C1
136   if (isC0 && aCheckingCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
137   {
138     Handle(Geom2d_BSplineCurve) aBC = Handle(Geom2d_BSplineCurve)::DownCast(aCheckingCurve);
139     if(aBC->IsG1(aUf, aUl, MyAngularToleranceForG1))
140     {
141       //Checking if basis curve has more smooth (C1, G2 and above) is not done.
142       //It can be done in case of need.
143       myBasisCurveContinuity = GeomAbs_G1;
144       isC0 = Standard_False;
145     }
146 
147     // Raise exception if still C0
148     if (isC0)
149       throw Standard_ConstructionError("Offset on C0 curve");
150   }
151   //
152   if(isTrimmed)
153   {
154     basisCurve = new Geom2d_TrimmedCurve(aCheckingCurve, aUf, aUl);
155   }
156   else
157   {
158     basisCurve = aCheckingCurve;
159   }
160 
161   myEvaluator = new Geom2dEvaluator_OffsetCurve(basisCurve, offsetValue);
162 }
163 
164 //=======================================================================
165 //function : SetOffsetValue
166 //purpose  :
167 //=======================================================================
168 
SetOffsetValue(const Standard_Real D)169 void Geom2d_OffsetCurve::SetOffsetValue (const Standard_Real D)
170 {
171   offsetValue = D;
172   myEvaluator->SetOffsetValue(offsetValue);
173 }
174 
175 //=======================================================================
176 //function : BasisCurve
177 //purpose  :
178 //=======================================================================
179 
Handle(Geom2d_Curve)180 Handle(Geom2d_Curve) Geom2d_OffsetCurve::BasisCurve () const
181 {
182   return basisCurve;
183 }
184 
185 //=======================================================================
186 //function : Continuity
187 //purpose  :
188 //=======================================================================
189 
Continuity() const190 GeomAbs_Shape Geom2d_OffsetCurve::Continuity () const
191 {
192   GeomAbs_Shape OffsetShape=GeomAbs_C0;
193   switch (myBasisCurveContinuity) {
194      case GeomAbs_C0 : OffsetShape = GeomAbs_C0;   break;
195      case GeomAbs_C1 : OffsetShape = GeomAbs_C0;   break;
196      case GeomAbs_C2 : OffsetShape = GeomAbs_C1;   break;
197      case GeomAbs_C3 : OffsetShape = GeomAbs_C2;   break;
198      case GeomAbs_CN : OffsetShape = GeomAbs_CN;   break;
199      case GeomAbs_G1 : OffsetShape = GeomAbs_G1;   break;
200      case GeomAbs_G2 : OffsetShape = GeomAbs_G2;   break;
201   }
202 
203   return OffsetShape;
204 }
205 
206 //=======================================================================
207 //function : D0
208 //purpose  :
209 //=======================================================================
210 
D0(const Standard_Real theU,gp_Pnt2d & theP) const211 void Geom2d_OffsetCurve::D0 (const Standard_Real theU,
212                                    gp_Pnt2d&     theP) const
213 {
214   myEvaluator->D0(theU, theP);
215 }
216 
217 //=======================================================================
218 //function : D1
219 //purpose  :
220 //=======================================================================
D1(const Standard_Real theU,gp_Pnt2d & theP,gp_Vec2d & theV1) const221 void Geom2d_OffsetCurve::D1 (const Standard_Real theU, gp_Pnt2d& theP, gp_Vec2d& theV1) const
222 {
223   myEvaluator->D1(theU, theP, theV1);
224 }
225 
226 //=======================================================================
227 //function : D2
228 //purpose  :
229 //=======================================================================
230 
D2(const Standard_Real theU,gp_Pnt2d & theP,gp_Vec2d & theV1,gp_Vec2d & theV2) const231 void Geom2d_OffsetCurve::D2 (const Standard_Real theU,
232                                    gp_Pnt2d& theP,
233                                    gp_Vec2d& theV1, gp_Vec2d& theV2) const
234 {
235   myEvaluator->D2(theU, theP, theV1, theV2);
236 }
237 
238 
239 //=======================================================================
240 //function : D3
241 //purpose  :
242 //=======================================================================
243 
D3(const Standard_Real theU,gp_Pnt2d & theP,gp_Vec2d & theV1,gp_Vec2d & theV2,gp_Vec2d & theV3) const244 void Geom2d_OffsetCurve::D3 (const Standard_Real theU,
245                                    gp_Pnt2d& theP,
246                                    gp_Vec2d& theV1, gp_Vec2d& theV2, gp_Vec2d& theV3) const
247 {
248   myEvaluator->D3(theU, theP, theV1, theV2, theV3);
249 }
250 
251 //=======================================================================
252 //function : DN
253 //purpose  :
254 //=======================================================================
255 
DN(const Standard_Real U,const Standard_Integer N) const256 gp_Vec2d Geom2d_OffsetCurve::DN (const Standard_Real U,
257                                  const Standard_Integer N) const
258 {
259   Standard_RangeError_Raise_if (N < 1, "Exception: Geom2d_OffsetCurve::DN(). N<1.");
260 
261   gp_Vec2d VN, VBidon;
262   gp_Pnt2d PBidon;
263   switch (N) {
264   case 1: D1( U, PBidon, VN); break;
265   case 2: D2( U, PBidon, VBidon, VN); break;
266   case 3: D3( U, PBidon, VBidon, VBidon, VN); break;
267   default:
268     throw Standard_NotImplemented("Exception: Derivative order is greater than 3. "
269       "Cannot compute of derivative.");
270   }
271 
272   return VN;
273 }
274 
275 //=======================================================================
276 //function : FirstParameter
277 //purpose  :
278 //=======================================================================
279 
FirstParameter() const280 Standard_Real Geom2d_OffsetCurve::FirstParameter () const
281 {
282   return basisCurve->FirstParameter();
283 }
284 
285 //=======================================================================
286 //function : LastParameter
287 //purpose  :
288 //=======================================================================
289 
LastParameter() const290 Standard_Real Geom2d_OffsetCurve::LastParameter () const
291 {
292   return basisCurve->LastParameter();
293 }
294 
295 
296 //=======================================================================
297 //function : Offset
298 //purpose  :
299 //=======================================================================
300 
Offset() const301 Standard_Real Geom2d_OffsetCurve::Offset () const
302 { return offsetValue; }
303 
304 //=======================================================================
305 //function : IsClosed
306 //purpose  :
307 //=======================================================================
308 
IsClosed() const309 Standard_Boolean Geom2d_OffsetCurve::IsClosed () const
310 {
311   gp_Pnt2d PF, PL;
312   D0(FirstParameter(),PF);
313   D0(LastParameter(),PL);
314   return ( PF.Distance(PL) <= gp::Resolution());
315 }
316 
317 //=======================================================================
318 //function : IsCN
319 //purpose  :
320 //=======================================================================
321 
IsCN(const Standard_Integer N) const322 Standard_Boolean Geom2d_OffsetCurve::IsCN (const Standard_Integer N) const
323 {
324   Standard_RangeError_Raise_if (N < 0, " " );
325   return basisCurve->IsCN (N + 1);
326 }
327 
328 //=======================================================================
329 //function : IsPeriodic
330 //purpose  :
331 //=======================================================================
332 
IsPeriodic() const333 Standard_Boolean Geom2d_OffsetCurve::IsPeriodic () const
334 {
335   return basisCurve->IsPeriodic();
336 }
337 
338 //=======================================================================
339 //function : Period
340 //purpose  :
341 //=======================================================================
342 
Period() const343 Standard_Real Geom2d_OffsetCurve::Period() const
344 {
345   return basisCurve->Period();
346 }
347 
348 //=======================================================================
349 //function : Transform
350 //purpose  :
351 //=======================================================================
352 
Transform(const gp_Trsf2d & T)353 void Geom2d_OffsetCurve::Transform (const gp_Trsf2d& T)
354 {
355   basisCurve->Transform (T);
356   offsetValue *= Abs(T.ScaleFactor());
357 
358   myEvaluator->SetOffsetValue(offsetValue);
359 }
360 
361 
362 //=======================================================================
363 //function : TransformedParameter
364 //purpose  :
365 //=======================================================================
366 
TransformedParameter(const Standard_Real U,const gp_Trsf2d & T) const367 Standard_Real Geom2d_OffsetCurve::TransformedParameter(const Standard_Real U,
368 							const gp_Trsf2d& T) const
369 {
370   return basisCurve->TransformedParameter(U,T);
371 }
372 
373 //=======================================================================
374 //function : ParametricTransformation
375 //purpose  :
376 //=======================================================================
377 
ParametricTransformation(const gp_Trsf2d & T) const378 Standard_Real Geom2d_OffsetCurve::ParametricTransformation(const gp_Trsf2d& T) const
379 {
380   return basisCurve->ParametricTransformation(T);
381 }
382 
383 //=======================================================================
384 //function : GetBasisCurveContinuity
385 //purpose  :
386 //=======================================================================
GetBasisCurveContinuity() const387 GeomAbs_Shape Geom2d_OffsetCurve::GetBasisCurveContinuity() const
388 {
389   return myBasisCurveContinuity;
390 }
391 
392 //=======================================================================
393 //function : DumpJson
394 //purpose  :
395 //=======================================================================
DumpJson(Standard_OStream & theOStream,Standard_Integer theDepth) const396 void Geom2d_OffsetCurve::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
397 {
398   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
399 
400   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Geom2d_Curve)
401 
402   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, basisCurve.get())
403 
404   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, offsetValue)
405   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myBasisCurveContinuity)
406 }
407