0024023: Revamp the OCCT Handle -- general
[occt.git] / src / Geom / Geom_TrimmedCurve.cxx
1 // Created on: 1993-03-10
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 <Geom_TrimmedCurve.ixx>
18
19 #include <gp.hxx>
20 #include <Geom_Geometry.hxx>
21 #include <Geom_BSplineCurve.hxx>
22 #include <Geom_BezierCurve.hxx>
23 #include <Geom_OffsetCurve.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Circle.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <Geom_Hyperbola.hxx>
28 #include <Geom_Parabola.hxx>
29 #include <Standard_ConstructionError.hxx>
30 #include <Standard_RangeError.hxx>
31 #include <Precision.hxx>
32 #include <ElCLib.hxx>
33
34 typedef Geom_TrimmedCurve         TrimmedCurve;
35 typedef gp_Ax1  Ax1;
36 typedef gp_Ax2  Ax2;
37 typedef gp_Pnt  Pnt;
38 typedef gp_Trsf Trsf;
39 typedef gp_Vec  Vec;
40
41 //=======================================================================
42 //function : Copy
43 //purpose  : 
44 //=======================================================================
45
46 Handle(Geom_Geometry) Geom_TrimmedCurve::Copy () const {
47  
48   Handle(Geom_TrimmedCurve) Tc = new TrimmedCurve (basisCurve, uTrim1, uTrim2);
49   return Tc;
50 }
51
52
53 //=======================================================================
54 //function : Geom_TrimmedCurve
55 //purpose  : 
56 //=======================================================================
57
58 Geom_TrimmedCurve::Geom_TrimmedCurve (const Handle(Geom_Curve)& C, 
59                                       const Standard_Real U1,
60                                       const Standard_Real U2,
61                                       const Standard_Boolean Sense,
62                                       const Standard_Boolean theAdjustPeriodic) :
63        uTrim1 (U1),
64        uTrim2 (U2) 
65 {
66   // kill trimmed basis curves
67   Handle(Geom_TrimmedCurve) T = Handle(Geom_TrimmedCurve)::DownCast(C);
68   if (!T.IsNull())
69     basisCurve = Handle(Geom_Curve)::DownCast(T->BasisCurve()->Copy());
70   else
71     basisCurve = Handle(Geom_Curve)::DownCast(C->Copy());
72
73   SetTrim(U1, U2, Sense, theAdjustPeriodic);
74 }
75
76 //=======================================================================
77 //function : Reverse
78 //purpose  : 
79 //=======================================================================
80
81 void Geom_TrimmedCurve::Reverse () 
82 {
83   Standard_Real U1 = basisCurve->ReversedParameter(uTrim2);
84   Standard_Real U2 = basisCurve->ReversedParameter(uTrim1);
85   basisCurve->Reverse();
86   SetTrim(U1, U2, Standard_True, Standard_False);
87 }
88
89
90 //=======================================================================
91 //function : ReversedParameter
92 //purpose  : 
93 //=======================================================================
94
95 Standard_Real Geom_TrimmedCurve::ReversedParameter
96   (const Standard_Real U) const 
97 {
98   return basisCurve->ReversedParameter(U);
99 }
100
101
102 //=======================================================================
103 //function : SetTrim
104 //purpose  : 
105 //=======================================================================
106
107 void Geom_TrimmedCurve::SetTrim (const Standard_Real U1, 
108                                  const Standard_Real U2, 
109                                  const Standard_Boolean Sense,
110                                  const Standard_Boolean theAdjustPeriodic) 
111 {
112    Standard_Boolean sameSense = Standard_True;
113    if (U1 == U2) 
114      Standard_ConstructionError::Raise("Geom_TrimmedCurve::U1 == U2");
115
116    Standard_Real Udeb = basisCurve->FirstParameter();
117    Standard_Real Ufin = basisCurve->LastParameter();
118
119    if (basisCurve->IsPeriodic())  {
120      sameSense = Sense;
121       
122      // set uTrim1 in the range Udeb , Ufin
123      // set uTrim2 in the range uTrim1 , uTrim1 + Period()
124      uTrim1 = U1;
125      uTrim2 = U2;
126      if (theAdjustPeriodic)
127        ElCLib::AdjustPeriodic(Udeb, Ufin,
128                               Min(Abs(uTrim2-uTrim1)/2,Precision::PConfusion()),
129                               uTrim1, uTrim2);
130    }
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
157 //=======================================================================
158 //function : IsClosed
159 //purpose  : 
160 //=======================================================================
161
162 Standard_Boolean Geom_TrimmedCurve::IsClosed () const
163 {
164   return ( StartPoint().Distance(EndPoint()) <= gp::Resolution());
165 }
166
167 //=======================================================================
168 //function : IsPeriodic
169 //purpose  : 
170 //=======================================================================
171
172 Standard_Boolean Geom_TrimmedCurve::IsPeriodic () const 
173 {
174   //return basisCurve->IsPeriodic();
175   return Standard_False;
176 }
177
178
179 //=======================================================================
180 //function : Period
181 //purpose  : 
182 //=======================================================================
183
184 Standard_Real Geom_TrimmedCurve::Period() const
185 {
186   return basisCurve->Period();
187 }
188
189
190 //=======================================================================
191 //function : Continuity
192 //purpose  : 
193 //=======================================================================
194
195 GeomAbs_Shape Geom_TrimmedCurve::Continuity () const { 
196
197   return basisCurve->Continuity ();
198 }
199
200
201 //=======================================================================
202 //function : BasisCurve
203 //purpose  : 
204 //=======================================================================
205
206 Handle(Geom_Curve) Geom_TrimmedCurve::BasisCurve () const { 
207
208   return basisCurve;
209 }
210
211
212 //=======================================================================
213 //function : D0
214 //purpose  : 
215 //=======================================================================
216
217 void Geom_TrimmedCurve::D0 (const Standard_Real U, Pnt& P) const {
218
219     basisCurve->D0( U, P);
220 }
221
222
223 //=======================================================================
224 //function : D1
225 //purpose  : 
226 //=======================================================================
227
228 void Geom_TrimmedCurve::D1 (const Standard_Real U, Pnt& P, Vec& V1) const {
229
230     basisCurve->D1 (U, P, V1);
231 }
232
233
234 //=======================================================================
235 //function : D2
236 //purpose  : 
237 //=======================================================================
238
239 void Geom_TrimmedCurve::D2 ( const Standard_Real U, 
240                              Pnt& P, 
241                              Vec& V1, Vec& V2) const {
242
243   basisCurve->D2 (U, P, V1, V2);
244 }
245
246
247 //=======================================================================
248 //function : D3
249 //purpose  : 
250 //=======================================================================
251
252 void Geom_TrimmedCurve::D3 (const Standard_Real U, 
253                             Pnt& P, 
254                             Vec& V1, Vec& V2, Vec& V3) const {
255  
256   basisCurve->D3 (U, P, V1, V2, V3);
257 }
258
259
260 //=======================================================================
261 //function : DN
262 //purpose  : 
263 //=======================================================================
264
265 Vec Geom_TrimmedCurve::DN (const Standard_Real U, 
266                            const Standard_Integer N) const 
267 {
268    return basisCurve->DN (U, N);
269 }
270
271
272 //=======================================================================
273 //function : EndPoint
274 //purpose  : 
275 //=======================================================================
276
277 Pnt Geom_TrimmedCurve::EndPoint () const { 
278  
279   return basisCurve->Value (uTrim2);
280 }
281
282
283 //=======================================================================
284 //function : FirstParameter
285 //purpose  : 
286 //=======================================================================
287
288 Standard_Real Geom_TrimmedCurve::FirstParameter () const { 
289
290   return uTrim1; 
291 }
292
293 //=======================================================================
294 //function : LastParameter
295 //purpose  : 
296 //=======================================================================
297
298 Standard_Real Geom_TrimmedCurve::LastParameter () const {
299
300   return uTrim2; 
301 }
302
303 //=======================================================================
304 //function : StartPoint
305 //purpose  : 
306 //=======================================================================
307
308 Pnt Geom_TrimmedCurve::StartPoint () const {
309
310   return basisCurve->Value (uTrim1);
311 }
312
313
314 //=======================================================================
315 //function : IsCN
316 //purpose  : 
317 //=======================================================================
318
319 Standard_Boolean Geom_TrimmedCurve::IsCN (const Standard_Integer N) const {
320   
321   Standard_RangeError_Raise_if (N < 0, " ");
322   return basisCurve->IsCN (N);
323 }
324
325  
326 //=======================================================================
327 //function : Transform
328 //purpose  : 
329 //=======================================================================
330
331 void Geom_TrimmedCurve::Transform (const Trsf& T) 
332 {
333   basisCurve->Transform (T);
334   Standard_Real U1 = basisCurve->TransformedParameter(uTrim1,T);
335   Standard_Real U2 = basisCurve->TransformedParameter(uTrim2,T);
336   SetTrim(U1, U2, Standard_True, Standard_False);
337 }
338
339
340 //=======================================================================
341 //function : TransformedParameter
342 //purpose  : 
343 //=======================================================================
344
345 Standard_Real Geom_TrimmedCurve::TransformedParameter(const Standard_Real U,
346                                                       const gp_Trsf& T) const
347 {
348   return basisCurve->TransformedParameter(U,T);
349 }
350
351 //=======================================================================
352 //function : ParametricTransformation
353 //purpose  : 
354 //=======================================================================
355
356 Standard_Real Geom_TrimmedCurve::ParametricTransformation(const gp_Trsf& T) 
357 const
358 {
359   return basisCurve->ParametricTransformation(T);
360 }
361