ce1fc3810e029071e0a944b23b79b3bc932707b2
[occt.git] / src / GeomProjLib / GeomProjLib.cxx
1 // Created on: 1994-09-21
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 //  Modified by skv - Wed Aug 11 17:26:03 2004 OCC6272
23
24 #include <stdio.h>
25
26 #include <GeomProjLib.ixx>
27
28 #include <ProjLib_ProjectedCurve.hxx>
29 #include <ProjLib_CompProjectedCurve.hxx>
30 #include <ProjLib_ProjectOnPlane.hxx>
31 #include <ProjLib_ProjectOnSurface.hxx>
32
33 #include <Approx_CurveOnSurface.hxx>
34
35 #include <GeomAdaptor_Curve.hxx>
36 #include <GeomAdaptor_Surface.hxx>
37 #include <GeomAdaptor_HCurve.hxx>
38 #include <GeomAdaptor_HSurface.hxx>
39
40 #include <Geom_Line.hxx>
41 #include <Geom_Circle.hxx>
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_Parabola.hxx>
44 #include <Geom_Hyperbola.hxx>
45 #include <Geom_BezierCurve.hxx>
46 #include <Geom_BSplineCurve.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <Geom2d_Line.hxx>
49 #include <Geom2d_Circle.hxx>
50 #include <Geom2d_Ellipse.hxx>
51 #include <Geom2d_Parabola.hxx>
52 #include <Geom2d_Hyperbola.hxx>
53 #include <Geom2d_BezierCurve.hxx>
54 #include <Geom2d_BSplineCurve.hxx>
55 #include <Geom2d_TrimmedCurve.hxx>
56
57 #include <gp_Pln.hxx>
58
59 #include <Precision.hxx>
60 #include <TColStd_Array1OfReal.hxx>
61 #include <TColStd_Array1OfInteger.hxx>
62 #include <TColgp_Array1OfPnt.hxx>
63 #include <TColgp_Array1OfPnt2d.hxx>
64
65 #ifdef DRAW
66 #include <DrawTrSurf.hxx>
67 #endif
68 #ifdef DEB
69 static Standard_Boolean Affich = Standard_False;
70 static Standard_Integer NBPROJ = 1;
71 #endif
72
73
74 //=======================================================================
75 //function : Curve2d
76 //purpose  : 
77 //=======================================================================
78
79 Handle(Geom2d_Curve) GeomProjLib::Curve2d(const Handle(Geom_Curve)& C,
80                                           const Standard_Real First,
81                                           const Standard_Real Last,
82                                           const Handle(Geom_Surface)& S,
83                                           const Standard_Real UDeb,
84                                           const Standard_Real UFin,
85                                           const Standard_Real VDeb,
86                                           const Standard_Real VFin,
87                                                 Standard_Real& Tolerance)
88 {
89 #ifdef DRAW
90   if ( Affich) {
91 //POP pour NT
92 //    char name[100];
93     char* name = new char[100];
94     sprintf(name,"PROJCURV_%d",NBPROJ);
95     DrawTrSurf::Set(name,C);
96     sprintf(name,"PROJSURF_%d",NBPROJ);
97     DrawTrSurf::Set(name,S);
98     NBPROJ++;
99   }
100 #endif
101
102   Tolerance = Max(Precision::PConfusion(),Tolerance);
103
104   GeomAdaptor_Curve   AC(C,First,Last);
105   GeomAdaptor_Surface AS(S,
106                          UDeb,
107                          UFin,
108                          VDeb,
109                          VFin);
110   
111   Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface(AS);
112   Handle(GeomAdaptor_HCurve)   HC = new GeomAdaptor_HCurve(AC);
113
114   ProjLib_ProjectedCurve Proj(HS,HC,Tolerance);
115
116   Handle(Geom2d_Curve) G2dC;
117
118   switch ( Proj.GetType()) {
119
120   case GeomAbs_Line:
121     G2dC = new Geom2d_Line(Proj.Line());
122     break;
123
124   case GeomAbs_Circle:
125     G2dC = new Geom2d_Circle(Proj.Circle());
126     break;
127
128   case GeomAbs_Ellipse:
129     G2dC = new Geom2d_Ellipse(Proj.Ellipse());
130     break;
131
132   case GeomAbs_Parabola:
133     G2dC = new Geom2d_Parabola(Proj.Parabola());
134     break;
135
136   case GeomAbs_Hyperbola:
137     G2dC = new Geom2d_Hyperbola(Proj.Hyperbola());
138     break;
139
140   case GeomAbs_BezierCurve:
141     G2dC = Proj.Bezier();
142     break;
143
144   case GeomAbs_BSplineCurve:
145     G2dC = Proj.BSpline();
146     break;
147
148   default:
149     return G2dC;
150     
151   }
152
153   if(G2dC.IsNull()) {
154     Tolerance = Proj.GetTolerance();
155     return G2dC;
156   }
157
158   if ( C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)) ) {
159     Handle(Geom_TrimmedCurve) CTrim = Handle(Geom_TrimmedCurve)::DownCast(C);
160     Standard_Real U1 = CTrim->FirstParameter();
161     Standard_Real U2 = CTrim->LastParameter();
162     G2dC = new Geom2d_TrimmedCurve( G2dC, U1, U2);
163   }
164
165 #ifdef DRAW
166   if ( Affich) {
167     static Standard_CString aprojcurv = "projcurv" ;
168     DrawTrSurf::Set(aprojcurv,G2dC);
169   }
170 #endif
171   Tolerance = Proj.GetTolerance();
172   return G2dC;
173 }
174
175 //=======================================================================
176 //function : Curve2d
177 //purpose  : 
178 //=======================================================================
179
180 Handle(Geom2d_Curve) GeomProjLib::Curve2d(const Handle(Geom_Curve)& C,
181                                           const Standard_Real First,
182                                           const Standard_Real Last,
183                                           const Handle(Geom_Surface)& S,
184                                                 Standard_Real& Tolerance)
185 {
186  Standard_Real  UFirst,
187                 ULast,
188                 VFirst,
189                 VLast ;
190
191  S->Bounds(UFirst,
192            ULast,
193            VFirst,
194            VLast) ;
195  return Curve2d(C,
196                 First,
197                 Last,
198                 S,
199                 UFirst,
200                 ULast,
201                 VFirst,
202                 VLast,
203                 Tolerance) ;
204 }
205 //  Modified by skv - Wed Aug 11 17:26:03 2004 OCC6272 Begin
206 //  Add not implemented method.
207 //=======================================================================
208 //function : Curve2d
209 //purpose  : 
210 //=======================================================================
211
212 Handle(Geom2d_Curve) GeomProjLib::Curve2d( const Handle(Geom_Curve)& C,
213                                            const Handle(Geom_Surface)& S,
214                                            const Standard_Real UDeb,
215                                            const Standard_Real UFin,
216                                            const Standard_Real VDeb,
217                                            const Standard_Real VFin)
218 {
219   Standard_Real First = C->FirstParameter();
220   Standard_Real Last  = C->LastParameter();
221   Standard_Real Tol   = Precision::PConfusion();
222   return GeomProjLib::Curve2d(C,First,Last,S,UDeb,UFin,VDeb,VFin,Tol);
223 }
224 //  Modified by skv - Wed Aug 11 17:26:03 2004 OCC6272 End
225
226 //=======================================================================
227 //function : Curve2d
228 //purpose  : 
229 //=======================================================================
230
231 Handle(Geom2d_Curve) GeomProjLib::Curve2d( const Handle(Geom_Curve)& C,
232                                            const Handle(Geom_Surface)& S,
233                                            const Standard_Real UDeb,
234                                            const Standard_Real UFin,
235                                            const Standard_Real VDeb,
236                                            const Standard_Real VFin,
237                                            Standard_Real& Tolerance)
238 {
239   Standard_Real First = C->FirstParameter();
240   Standard_Real Last  = C->LastParameter();
241   return GeomProjLib::Curve2d(C,First,Last,S,UDeb,UFin,VDeb,VFin,Tolerance);
242 }
243
244
245 //=======================================================================
246 //function : Curve2d
247 //purpose  : 
248 //=======================================================================
249
250 Handle(Geom2d_Curve) GeomProjLib::Curve2d( const Handle(Geom_Curve)& C,
251                                            const Handle(Geom_Surface)& S) 
252 {
253   Standard_Real First = C->FirstParameter();
254   Standard_Real Last  = C->LastParameter();
255   Standard_Real Tol   = Precision::PConfusion();
256   return GeomProjLib::Curve2d(C,First,Last,S,Tol);
257 }
258
259
260 //=======================================================================
261 //function : Curve2d
262 //purpose  : 
263 //=======================================================================
264
265 Handle(Geom2d_Curve) GeomProjLib::Curve2d( const Handle(Geom_Curve)&   C,
266                                            const Standard_Real         First,
267                                            const Standard_Real         Last,
268                                            const Handle(Geom_Surface)& S) 
269 {
270   Standard_Real Tol = Precision::PConfusion();
271   return GeomProjLib::Curve2d(C,First,Last,S,Tol);
272 }
273
274
275 //=======================================================================
276 //function : Project
277 //purpose  : 
278 //=======================================================================
279
280 Handle(Geom_Curve) GeomProjLib::Project( const Handle(Geom_Curve)& C,
281                                          const Handle(Geom_Surface)& S) 
282 {
283   GeomAdaptor_Curve   AC(C);
284   GeomAdaptor_Surface AS(S);
285
286   Handle(Geom_Curve) GC;
287
288   if ( AS.GetType() == GeomAbs_Plane) {
289     ProjLib_ProjectOnPlane Proj( AS.Plane().Position());
290     Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve(AC);
291     Proj.Load(HC,Precision::PApproximation());
292
293     switch ( Proj.GetType()) {
294     case GeomAbs_Line:
295       GC = new Geom_Line(Proj.Line());
296       break;
297       
298     case GeomAbs_Circle:
299       GC = new Geom_Circle(Proj.Circle());
300       break;
301       
302     case GeomAbs_Ellipse:
303       GC = new Geom_Ellipse(Proj.Ellipse());
304       break;
305       
306     case GeomAbs_Parabola:
307       GC = new Geom_Parabola(Proj.Parabola());
308       break;
309       
310     case GeomAbs_Hyperbola:
311       GC = new Geom_Hyperbola(Proj.Hyperbola());
312       break;
313
314     case GeomAbs_BezierCurve:
315       GC = Proj.Bezier();
316       break;
317
318     case GeomAbs_BSplineCurve:
319       GC = Proj.BSpline();
320       break;
321
322     default:
323       return GC;
324       
325     }
326     
327     if ( C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
328       Handle(Geom_TrimmedCurve) CTrim = Handle(Geom_TrimmedCurve)::DownCast(C);
329       Standard_Real U1 = CTrim->FirstParameter();
330       Standard_Real U2 = CTrim->LastParameter();
331       GC = new Geom_TrimmedCurve( GC, U1, U2);
332     }
333     
334   }
335   else {
336     Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface(AS);
337     Handle(GeomAdaptor_HCurve)   HC = new GeomAdaptor_HCurve(AC);
338 //    Standard_Real Tol  = Precision::Approximation();
339 //    Standard_Real TolU = Precision::PApproximation();
340 //    Standard_Real TolV = Precision::PApproximation();
341     Standard_Real Tol  = 0.0001;
342     Standard_Real TolU = Pow(Tol, 2./3);
343     Standard_Real TolV = Pow(Tol, 2./3);
344     ProjLib_CompProjectedCurve Proj(HS,HC,TolU,TolV,-1.);
345     
346     Standard_Real f,l;
347     Proj.Bounds(1,f,l);
348     Handle(Adaptor2d_HCurve2d) HC2d = Proj.Trim(f,l,TolU);
349     Approx_CurveOnSurface Approx(HC2d, HS, f, l, Tol,
350                                  GeomAbs_C2,14,16,Standard_True);
351
352     // ici, on a toujours un type BSpline.
353     if (Approx.IsDone() && Approx.HasResult())
354       GC = Approx.Curve3d();
355   }
356
357   return GC;
358 }
359
360 //=======================================================================
361 //function : ProjectOnPlane
362 //purpose  : 
363 //=======================================================================
364
365 Handle(Geom_Curve) GeomProjLib::ProjectOnPlane
366 (const Handle(Geom_Curve)& Curve,
367  const Handle(Geom_Plane)& Plane,
368  const gp_Dir& Dir,
369  const Standard_Boolean KeepParametrization)
370 {
371   GeomAdaptor_Curve   AC(Curve);
372   Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve(AC);
373
374   ProjLib_ProjectOnPlane Proj(Plane->Position(), Dir);
375   Proj.Load(HC,Precision::Approximation(), KeepParametrization);
376
377   Handle(Geom_Curve) GC;
378
379   switch ( Proj.GetType()) {
380   case GeomAbs_Line:
381     GC = new Geom_Line(Proj.Line());
382     break;
383     
384   case GeomAbs_Circle:
385     GC = new Geom_Circle(Proj.Circle());
386     break;
387     
388   case GeomAbs_Ellipse:
389     GC = new Geom_Ellipse(Proj.Ellipse());
390     break;
391     
392   case GeomAbs_Parabola:
393     GC = new Geom_Parabola(Proj.Parabola());
394     break;
395     
396   case GeomAbs_Hyperbola:
397     GC = new Geom_Hyperbola(Proj.Hyperbola());
398     break;
399     
400   case GeomAbs_BezierCurve: 
401     GC = Proj.Bezier();
402     break;
403
404   case GeomAbs_BSplineCurve:
405     GC = Proj.BSpline();
406     break;
407   default:
408     return GC;
409     
410   }
411   
412   if ( Curve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)) ) {
413     Handle(Geom_TrimmedCurve) CTrim 
414       = Handle(Geom_TrimmedCurve)::DownCast(Curve);
415     GC = new Geom_TrimmedCurve( GC, Proj.FirstParameter(),
416                                     Proj.LastParameter());
417   }
418   
419   return GC;
420   
421 }
422
423