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