0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- manual
[occt.git] / src / Geom2d / Geom2d_TrimmedCurve.cxx
1 // Created on: 1993-03-24
2 // Created by: JCV
3 // Copyright (c) 1993-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
18 #include <ElCLib.hxx>
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 <Precision.hxx>
37 #include <Standard_ConstructionError.hxx>
38 #include <Standard_NoSuchObject.hxx>
39 #include <Standard_RangeError.hxx>
40 #include <Standard_Type.hxx>
41
42 typedef Geom2d_TrimmedCurve         TrimmedCurve;
43 typedef gp_Ax2d   Ax2d;
44 typedef gp_Pnt2d  Pnt2d;
45 typedef gp_Trsf2d Trsf2d;
46 typedef gp_Vec2d  Vec2d;
47
48 //=======================================================================
49 //function : Copy
50 //purpose  : 
51 //=======================================================================
52
53 Handle(Geom2d_Geometry) Geom2d_TrimmedCurve::Copy () const 
54 {
55   Handle(Geom2d_TrimmedCurve) Tc;
56   Tc = new TrimmedCurve (basisCurve, uTrim1, uTrim2);
57   return Tc;
58 }
59
60 //=======================================================================
61 //function : Geom2d_TrimmedCurve
62 //purpose  : 
63 //=======================================================================
64
65 Geom2d_TrimmedCurve::Geom2d_TrimmedCurve (const Handle(Geom2d_Curve)& C, 
66                                           const Standard_Real U1, 
67                                           const Standard_Real U2,
68                                           const Standard_Boolean Sense,
69                                           const Standard_Boolean theAdjustPeriodic) :
70      uTrim1 (U1),
71      uTrim2 (U2)
72 {
73   if(C.IsNull()) Standard_ConstructionError::Raise("Geom2d_TrimmedCurve:: C is null");
74   // kill trimmed basis curves
75   Handle(Geom2d_TrimmedCurve) T = Handle(Geom2d_TrimmedCurve)::DownCast(C);
76   if (!T.IsNull())
77     basisCurve = Handle(Geom2d_Curve)::DownCast(T->BasisCurve()->Copy());
78   else
79     basisCurve = Handle(Geom2d_Curve)::DownCast(C->Copy());
80
81   SetTrim(U1, U2, Sense, theAdjustPeriodic);
82 }
83
84 //=======================================================================
85 //function : Reverse
86 //purpose  : 
87 //=======================================================================
88
89 void Geom2d_TrimmedCurve::Reverse () 
90 {
91   Standard_Real U1 = basisCurve->ReversedParameter(uTrim2);
92   Standard_Real U2 = basisCurve->ReversedParameter(uTrim1);
93   basisCurve->Reverse();
94   SetTrim(U1, U2, Standard_True, Standard_False);
95 }
96
97 //=======================================================================
98 //function : ReversedParamater
99 //purpose  : 
100 //=======================================================================
101
102 Standard_Real Geom2d_TrimmedCurve::ReversedParameter( const Standard_Real U) const
103 {
104   return basisCurve->ReversedParameter(U);
105 }
106
107 //=======================================================================
108 //function : SetTrim
109 //purpose  : 
110 //=======================================================================
111
112 void Geom2d_TrimmedCurve::SetTrim (const Standard_Real U1,
113                                    const Standard_Real U2,
114                                    const Standard_Boolean Sense,
115                                    const Standard_Boolean theAdjustPeriodic) 
116 {
117   Standard_Boolean sameSense = Standard_True;
118   if (U1 == U2)
119     Standard_ConstructionError::Raise("Geom2d_TrimmedCurve::U1 == U2");
120
121   Standard_Real Udeb = basisCurve->FirstParameter();
122   Standard_Real Ufin = basisCurve->LastParameter();
123   
124   if (basisCurve->IsPeriodic())  {
125      sameSense = Sense;
126       
127      // set uTrim1 in the range Udeb , Ufin
128      // set uTrim2 in the range uTrim1 , uTrim1 + Period()
129      uTrim1 = U1;
130      uTrim2 = U2;
131      if (theAdjustPeriodic)
132        ElCLib::AdjustPeriodic(Udeb, Ufin,
133                               Min(Abs(uTrim2-uTrim1)/2,Precision::PConfusion()),
134                               uTrim1, uTrim2);
135   }
136   else { 
137     if (U1 < U2) {
138       sameSense = Sense;
139       uTrim1 = U1;
140       uTrim2 = U2;
141     }
142     else {
143       sameSense = !Sense;
144       uTrim1 = U2;
145       uTrim2 = U1;
146     }
147
148     if ((Udeb - uTrim1 > Precision::PConfusion()) ||
149         (uTrim2 - Ufin > Precision::PConfusion()))   {
150       Standard_ConstructionError::Raise
151         ("Geom_TrimmedCurve::parameters out of range");
152     }
153   }    
154
155   if (!sameSense)
156     Reverse();
157 }
158
159 //=======================================================================
160 //function : BasisCurve
161 //purpose  : 
162 //=======================================================================
163
164 Handle(Geom2d_Curve) Geom2d_TrimmedCurve::BasisCurve () const 
165
166   return basisCurve;
167 }
168
169 //=======================================================================
170 //function : Continuity
171 //purpose  : 
172 //=======================================================================
173
174 GeomAbs_Shape Geom2d_TrimmedCurve::Continuity () const
175
176   return basisCurve->Continuity(); 
177 }
178
179 //=======================================================================
180 //function : IsCN
181 //purpose  : 
182 //=======================================================================
183
184 Standard_Boolean Geom2d_TrimmedCurve::IsCN (const Standard_Integer N) const 
185 {
186   Standard_RangeError_Raise_if (N < 0, " ");
187   return basisCurve->IsCN (N);
188 }
189
190 //=======================================================================
191 //function : EndPoint
192 //purpose  : 
193 //=======================================================================
194
195 Pnt2d Geom2d_TrimmedCurve::EndPoint () const 
196
197   return  basisCurve->Value(uTrim2);
198 }
199
200 //=======================================================================
201 //function : FirstParameter
202 //purpose  : 
203 //=======================================================================
204
205 Standard_Real Geom2d_TrimmedCurve::FirstParameter () const       { return uTrim1; }
206
207 //=======================================================================
208 //function : IsClosed
209 //purpose  : 
210 //=======================================================================
211
212 Standard_Boolean Geom2d_TrimmedCurve::IsClosed () const          
213 {
214   Standard_Real Dist = 
215     Value(FirstParameter()).Distance(Value(LastParameter()));
216   return ( Dist <= gp::Resolution());
217 }
218
219 //=======================================================================
220 //function : IsPeriodic
221 //purpose  : 
222 //=======================================================================
223
224 Standard_Boolean Geom2d_TrimmedCurve::IsPeriodic () const        
225 {
226   //return basisCurve->IsPeriodic();
227   return Standard_False;
228 }
229
230 //=======================================================================
231 //function : Period
232 //purpose  : 
233 //=======================================================================
234
235 Standard_Real Geom2d_TrimmedCurve::Period () const
236 {
237   return basisCurve->Period();
238 }
239
240 //=======================================================================
241 //function : LastParameter
242 //purpose  : 
243 //=======================================================================
244
245 Standard_Real Geom2d_TrimmedCurve::LastParameter () const        { return uTrim2; }
246
247 //=======================================================================
248 //function : StartPoint
249 //purpose  : 
250 //=======================================================================
251
252 Pnt2d Geom2d_TrimmedCurve::StartPoint () const 
253 {
254   gp_Pnt2d P;
255     P = basisCurve->Value(uTrim1);
256   return P;
257 }
258
259 //=======================================================================
260 //function : D0
261 //purpose  : 
262 //=======================================================================
263
264 void Geom2d_TrimmedCurve::D0 (const Standard_Real   U,
265                                     Pnt2d& P ) const {
266   basisCurve->D0(U, P);
267 }
268
269 //=======================================================================
270 //function : D1
271 //purpose  : 
272 //=======================================================================
273
274 void Geom2d_TrimmedCurve::D1 (const Standard_Real U, Pnt2d& P, Vec2d& V1) const 
275 {
276    basisCurve->D1 (U, P, V1);
277 }
278
279 //=======================================================================
280 //function : D2
281 //purpose  : 
282 //=======================================================================
283
284 void Geom2d_TrimmedCurve::D2 (const Standard_Real U, 
285                                     Pnt2d& P, 
286                                     Vec2d& V1, Vec2d& V2) const 
287 {
288    basisCurve->D2 (U, P, V1, V2);
289 }
290
291 //=======================================================================
292 //function : D3
293 //purpose  : 
294 //=======================================================================
295
296 void Geom2d_TrimmedCurve::D3 (const Standard_Real U, 
297                                     Pnt2d& P, 
298                                     Vec2d& V1, Vec2d& V2, Vec2d& V3) const 
299 {
300    basisCurve->D3 (U, P, V1, V2, V3);
301 }
302
303 //=======================================================================
304 //function : DN
305 //purpose  : 
306 //=======================================================================
307
308 Vec2d Geom2d_TrimmedCurve::DN (const Standard_Real U, const Standard_Integer N) const 
309 {
310   return  basisCurve->DN(U, N);
311 }
312
313 //=======================================================================
314 //function : Transform
315 //purpose  : 
316 //=======================================================================
317
318 void Geom2d_TrimmedCurve::Transform (const Trsf2d& T) 
319 {
320   basisCurve->Transform (T);
321   Standard_Real U1 = basisCurve->TransformedParameter(uTrim1,T);
322   Standard_Real U2 = basisCurve->TransformedParameter(uTrim2,T);
323   SetTrim(U1, U2, Standard_True, Standard_False);
324 }
325
326 //=======================================================================
327 //function : TransformedParameter
328 //purpose  : 
329 //=======================================================================
330
331 Standard_Real Geom2d_TrimmedCurve::TransformedParameter(const Standard_Real U,
332                                                         const gp_Trsf2d& T) const
333 {
334   return basisCurve->TransformedParameter(U,T);
335 }
336
337 //=======================================================================
338 //function : ParametricTransformation
339 //purpose  : 
340 //=======================================================================
341
342 Standard_Real Geom2d_TrimmedCurve::ParametricTransformation(const gp_Trsf2d& T) const
343 {
344   return basisCurve->ParametricTransformation(T);
345 }
346