Integration of OCCT 6.5.0 from SVN
[occt.git] / src / GeomliteTest / GeomliteTest_API2dCommands.cxx
1 // File:        GeometryTest_API2dCommands.cxx
2 // Created:     Wed Jan 11 10:33:13 1995
3 // Author:      Remi LEQUETTE
4 //              <rle@bravox>
5 // modified : pmn 11/04/97 : mis dans GeomliteTest
6
7
8 #include <GeomliteTest.hxx>
9 #include <Geom2d_Curve.hxx>
10 #include <Draw.hxx>
11 #include <Draw_Interpretor.hxx>
12 #include <DrawTrSurf.hxx>
13 #include <Draw_Appli.hxx>
14 #include <DrawTrSurf_Curve2d.hxx>
15 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
16 #include <Geom2dAPI_ExtremaCurveCurve.hxx>
17 #include <Geom2dAPI_PointsToBSpline.hxx>
18 #include <Geom2dAPI_InterCurveCurve.hxx>
19 #include <Geom2d_Line.hxx>
20 #include <Geom2d_TrimmedCurve.hxx>
21 #include <TColgp_Array1OfPnt2d.hxx>
22 #include <gp_Pnt.hxx>
23 #include <Draw_Marker2D.hxx>
24 #include <Draw_Color.hxx>
25 #include <Draw_MarkerShape.hxx>
26 #include <TColStd_Array1OfReal.hxx>
27 #include <GeomAbs_Shape.hxx>
28 #include <Precision.hxx>
29
30 #include <stdio.h>
31 #ifdef WNT
32 Standard_IMPORT Draw_Viewer dout;
33 #endif
34
35 //=======================================================================
36 //function : proj
37 //purpose  : 
38 //=======================================================================
39
40 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
41 {
42   if ( n < 4) return 1;
43
44   gp_Pnt2d P(atof(a[2]),atof(a[3]));
45
46   char name[100];
47
48   Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]);
49
50   if (GC.IsNull())
51     return 1;
52
53   Geom2dAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
54                                           GC->LastParameter());
55   
56   for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
57     gp_Pnt2d P1 = proj.Point(i);
58     Handle(Geom2d_Line) L = new Geom2d_Line(P,gp_Vec2d(P,P1));
59     Handle(Geom2d_TrimmedCurve) CT = 
60       new Geom2d_TrimmedCurve(L, 0., P.Distance(P1));
61     sprintf(name,"%s%d","ext_",i);
62     char* temp = name; // portage WNT
63     DrawTrSurf::Set(temp, CT);
64     di << name << " ";
65   }
66
67   return 0;
68 }
69
70 //=======================================================================
71 //function : appro
72 //purpose  : 
73 //=======================================================================
74
75 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
76 {
77   // Approximation et interpolation 2d
78
79   // 2dappro
80   //     - affiche la tolerance
81   // 2dappro tol
82   //     - change la tolerance
83   // 2dappro result nbpoint 
84   //     - saisie interactive
85   // 2dappro result nbpoint curve 
86   //     - calcule des points sur la courbe
87   // 2dappro result nbpoint x1 y1 x2 y2 .. 
88   //     - tableau de points
89   // 2dappro result nbpoint x1 dx y1 y2 ..
90   //     - tableau de points (x1,y1) (x1+dx,y2) ... avec x = t
91   
92
93   static Standard_Real Tol2d = 1.e-6;
94
95   if (n < 3) {
96     if (n == 2) 
97       Tol2d = atof(a[1]);
98     
99     di << "Tolerance for 2d approx : "<< Tol2d << "\n";
100     return 0;
101   }
102
103
104   Standard_Integer i, Nb = atoi(a[2]);
105   
106   Standard_Boolean hasPoints = Standard_True;
107   TColgp_Array1OfPnt2d Points(1, Nb);
108   TColStd_Array1OfReal YValues(1,Nb);
109   Standard_Real X0=0,DX=0;
110   
111   Handle(Draw_Marker2D) mark;
112   
113   if (n == 3)  {
114     // saisie interactive
115     Standard_Integer id,XX,YY,b;
116     dout.Select(id,XX,YY,b);
117     Standard_Real zoom = dout.Zoom(id);
118
119     Points(1) = gp_Pnt2d( ((Standard_Real)XX)/zoom, 
120                           ((Standard_Real)YY)/zoom );
121     
122     mark = new Draw_Marker2D( Points(1), Draw_X, Draw_vert); 
123     
124     dout << mark;
125     
126     for (i = 2; i<=Nb; i++) {
127       dout.Select(id,XX,YY,b);
128       Points(i) = gp_Pnt2d( ((Standard_Real)XX)/zoom, 
129                             ((Standard_Real)YY)/zoom );
130       mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert); 
131       dout << mark;
132     }
133   }    
134   else {
135     if ( n == 4) {
136     // points sur courbe
137       Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[3]);
138       if ( GC.IsNull()) 
139         return 1;
140
141       Standard_Real U, U1, U2;
142       U1 = GC->FirstParameter();
143       U2 = GC->LastParameter();
144       Standard_Real Delta = ( U2 - U1) / (Nb-1);
145       for ( i = 1 ; i <= Nb; i++) {
146         U = U1 + (i-1) * Delta;
147         Points(i) = GC->Value(U);
148       }
149     }
150
151     else {
152       // test points ou ordonnees
153       hasPoints = Standard_False;
154       Standard_Integer nc = n - 3;
155       if (nc == 2 * Nb) {
156         // points
157         nc = 3;
158         for (i = 1; i <= Nb; i++) {
159           Points(i).SetCoord(atof(a[nc]),atof(a[nc+1]));
160           nc += 2;
161         }
162       }
163       else if (nc - 2 == Nb) {
164         // YValues
165         nc = 5;
166         X0 = atof(a[3]);
167         DX = atof(a[4]);
168         for (i = 1; i <= Nb; i++) {
169           YValues(i) = atof(a[nc]);
170           Points(i).SetCoord(X0+(i-1)*DX,YValues(i));
171           nc++;
172         }
173       }
174       else
175         return 1;
176     }
177     // display the points
178     for ( i = 1 ; i <= Nb; i++) {
179       mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert); 
180       dout << mark;
181     }
182   }
183   dout.Flush();
184   Standard_Integer Dmin = 3;
185   Standard_Integer Dmax = 8;
186   
187   Handle(Geom2d_BSplineCurve) TheCurve;
188   if (hasPoints)
189     TheCurve = Geom2dAPI_PointsToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol2d);
190   else
191     TheCurve = Geom2dAPI_PointsToBSpline(YValues,X0,DX,Dmin,Dmax,GeomAbs_C2,Tol2d);
192   
193   DrawTrSurf::Set(a[1], TheCurve);
194   di << a[1];
195
196   return 0;
197
198 }
199
200 //=======================================================================
201 //function : extrema
202 //purpose  : 
203 //=======================================================================
204
205 static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
206 {
207   if ( n<3) return 1;
208
209   Handle(Geom2d_Curve)   GC1, GC2;
210
211   Standard_Real U1f,U1l,U2f,U2l;
212
213   GC1 = DrawTrSurf::GetCurve2d(a[1]);
214   if ( GC1.IsNull())
215     return 1;
216   U1f = GC1->FirstParameter();
217   U1l = GC1->LastParameter();
218
219   GC2 = DrawTrSurf::GetCurve2d(a[2]);
220   if ( GC2.IsNull())
221     return 1;
222   U2f = GC2->FirstParameter();
223   U2l = GC2->LastParameter();
224
225   char name[100];
226
227   Geom2dAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
228
229 // modified by APV (compilation error - LINUX)
230 //  for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
231   Standard_Integer i;
232   for ( i = 1; i <= Ex.NbExtrema(); i++) {
233 // modified by APV (compilation error - LINUX)
234
235     gp_Pnt2d P1,P2;
236     Ex.Points(i,P1,P2);
237     di << "dist " << i << ": " << Ex.Distance(i) << " \n";
238     if (Ex.Distance(i) <= Precision::PConfusion()) {
239       Handle(Draw_Marker2D) mark = new Draw_Marker2D( P1, Draw_X, Draw_vert); 
240       dout << mark;
241       dout.Flush();
242     }
243     else {
244       Handle(Geom2d_Line) L = new Geom2d_Line(P1,gp_Vec2d(P1,P2));
245       Handle(Geom2d_TrimmedCurve) CT = 
246         new Geom2d_TrimmedCurve(L, 0., P1.Distance(P2));
247       sprintf(name,"%s%d","ext_",i);
248       char* temp = name; // portage WNT
249       DrawTrSurf::Set(temp, CT);
250       di << name << " ";
251     }
252   }
253   if (i==1)
254     di << "No decisions ";
255
256   return 0;
257 }
258
259
260 //=======================================================================
261 //function : intersect
262 //purpose  : 
263 //=======================================================================
264
265 static Standard_Integer intersect(Draw_Interpretor& /*di*/, Standard_Integer n, const char** a)
266 {
267   if( n < 2) 
268     return 1;
269
270   Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]);
271   if ( C1.IsNull()) 
272     return 1;
273
274   Standard_Real Tol = 0.001;
275   Geom2dAPI_InterCurveCurve Intersector;
276
277   Handle(Geom2d_Curve) C2;
278   if ( n == 3) {
279     C2 = DrawTrSurf::GetCurve2d(a[2]);
280     if ( C2.IsNull())
281       return 1;
282     Intersector.Init(C1,C2,Tol);
283   }
284   else {
285     Intersector.Init(C1, Tol);
286   }
287
288   Standard_Integer i;
289
290   for ( i = 1; i <= Intersector.NbPoints(); i++) {
291     gp_Pnt2d P = Intersector.Point(i);
292     Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert); 
293     dout << mark;
294   }
295   dout.Flush();
296
297   Handle(Geom2d_Curve) S1,S2;
298   Handle(DrawTrSurf_Curve2d) CD;
299   if ( n == 3) {
300     for ( i = 1; i <= Intersector.NbSegments(); i++) {
301       Intersector.Segment(i,S1,S2);
302       CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30);
303       dout << CD;
304       CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30);
305       dout << CD;
306     }
307   }
308   dout.Flush();
309
310   return 0;
311 }
312
313
314 void GeomliteTest::API2dCommands(Draw_Interpretor& theCommands)
315 {
316   static Standard_Boolean done = Standard_False;
317   if (done) return;
318
319   const char *g;
320
321   done = Standard_True;
322   g = "GEOMETRY curves and surfaces analysis";
323
324   theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g);
325
326   g = "GEOMETRY approximations";
327
328   theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__, 
329                   appro,g);
330   theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__, 
331                   appro,g);
332
333   g = "GEOMETRY curves and surfaces analysis";
334
335   theCommands.Add("2dextrema", "extrema curve curve",__FILE__,
336                   extrema,g);
337
338   g = "GEOMETRY intersections";
339
340   theCommands.Add("2dintersect", "intersect curve curve",__FILE__,
341                   intersect,g);
342 }