Integration of OCCT 6.5.0 from SVN
[occt.git] / src / GeometryTest / GeometryTest_APICommands.cxx
1 // File:        GeometryTest_APICommands.cxx
2 // Created:     Tue Jan 17 18:06:26 1995
3 // Author:      Remi LEQUETTE
4 //              <rle@bravox>
5
6
7 #include <Geom_Curve.hxx>
8 #include <Geom_Surface.hxx>
9 #include <Draw.hxx>
10 #include <Draw_Interpretor.hxx>
11 #include <DrawTrSurf.hxx>
12 #include <Draw_Appli.hxx>
13 #include <GeometryTest.hxx>
14 #include <GeomAPI_ProjectPointOnCurve.hxx>
15 #include <GeomAPI_ProjectPointOnSurf.hxx>
16 #include <GeomAPI_ExtremaCurveCurve.hxx>
17 #include <GeomAPI_ExtremaCurveSurface.hxx>
18 #include <GeomAPI_ExtremaSurfaceSurface.hxx>
19 #include <GeomAPI_PointsToBSpline.hxx>
20 #include <GeomAPI_PointsToBSplineSurface.hxx>
21 #include <Geom_Line.hxx>
22 #include <Geom_TrimmedCurve.hxx>
23 #include <Draw_Segment3D.hxx>
24 #include <Draw_Marker3D.hxx>
25 #include <Draw_Color.hxx>
26 #include <Draw_MarkerShape.hxx>
27 #include <TColgp_Array1OfPnt.hxx>
28 #include <TColgp_Array2OfPnt.hxx>
29 #include <TColStd_Array2OfReal.hxx>
30 #include <Precision.hxx>
31 #include <stdio.h>
32 #ifdef WNT
33 Standard_IMPORT Draw_Viewer dout;
34 #endif
35
36 //=======================================================================
37 //function : proj
38 //purpose  : 
39 //=======================================================================
40
41 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
42 {
43   if ( n < 5) return 1;
44
45   gp_Pnt P(atof(a[2]),atof(a[3]),atof(a[4]));
46
47   char name[100];
48
49   Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(a[1]);
50   Handle(Geom_Surface) GS;
51
52   if (GC.IsNull())  {
53     GS = DrawTrSurf::GetSurface(a[1]);
54     if (GS.IsNull())
55       return 1;
56     Standard_Real U1, U2, V1, V2;
57     GS->Bounds(U1,U2,V1,V2);
58
59     GeomAPI_ProjectPointOnSurf proj(P,GS,U1,U2,V1,V2);
60     Standard_Real UU,VV;
61     for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
62       gp_Pnt P1 = proj.Point(i);
63       if ( P.Distance(P1) > Precision::Confusion()) {
64         Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
65         Handle(Geom_TrimmedCurve) CT = 
66           new Geom_TrimmedCurve(L, 0., P.Distance(P1));
67         sprintf(name,"%s%d","ext_",i);
68         char* temp = name; // portage WNT
69         DrawTrSurf::Set(temp, CT);
70         di << name << " ";
71       }
72       else {
73         sprintf(name,"%s%d","ext_",i);
74         di << name << " ";
75         char* temp = name; // portage WNT
76         DrawTrSurf::Set(temp, P1);
77         proj.Parameters(i,UU,VV);
78         di << " Le point est sur la surface." << "\n";
79         di << " Ses parametres sont:  UU = " << UU << "\n";
80         di << "                       VV = " << VV << "\n";
81       }
82     }
83
84   }
85   else {
86     GeomAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
87                                           GC->LastParameter());
88 //    Standard_Real UU;
89     for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
90       gp_Pnt P1 = proj.Point(i);
91       Standard_Real UU = proj.Parameter(i);
92       di << " parameter " << i << " = " << UU << "\n";
93       if ( P.Distance(P1) > Precision::Confusion()) {
94         Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
95         Handle(Geom_TrimmedCurve) CT = 
96           new Geom_TrimmedCurve(L, 0., P.Distance(P1));
97         sprintf(name,"%s%d","ext_",i);
98         char* temp = name; // portage WNT
99         DrawTrSurf::Set(temp, CT);
100         di << name << " ";
101       }
102       else {
103         sprintf(name,"%s%d","ext_",i);
104         char* temp = name; // portage WNT
105         DrawTrSurf::Set(temp, P1);
106         di << name << " ";
107         UU = proj.Parameter(i);
108         di << " Le point est sur la courbe." << "\n";
109         di << " Son parametre est U = " << UU << "\n";
110       }
111     }
112   }
113   return 0;
114 }
115
116 //=======================================================================
117 //function : appro
118 //purpose  : 
119 //=======================================================================
120
121 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
122 {
123   if ( n<3) return 1;
124
125   Handle(Geom_Curve) GC;
126   Standard_Integer Nb = atoi(a[2]);
127
128   TColgp_Array1OfPnt Points(1, Nb);
129
130   Handle(Draw_Marker3D) mark;
131
132   if ( n == 4) {
133     GC = DrawTrSurf::GetCurve(a[3]);
134     if ( GC.IsNull()) 
135       return 1;
136
137     Standard_Real U, U1, U2;
138     U1 = GC->FirstParameter();
139     U2 = GC->LastParameter();
140     Standard_Real Delta = ( U2 - U1) / (Nb-1);
141     for ( Standard_Integer i = 1 ; i <= Nb; i++) {
142       U = U1 + (i-1) * Delta;
143       Points(i) = GC->Value(U);
144       mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
145       dout << mark;
146     }
147   }
148   else {
149     Standard_Integer id,XX,YY,b;
150     dout.Select(id,XX,YY,b);
151     Standard_Real zoom = dout.Zoom(id);
152
153     Points(1) = gp_Pnt( ((Standard_Real)XX)/zoom, 
154                         ((Standard_Real)YY)/zoom, 
155                         0.);
156     
157     mark = new Draw_Marker3D( Points(1), Draw_X, Draw_vert); 
158     
159     dout << mark;
160     
161     for (Standard_Integer i = 2; i<=Nb; i++) {
162       dout.Select(id,XX,YY,b);
163       Points(i) = gp_Pnt( ((Standard_Real)XX)/zoom, 
164                          ((Standard_Real)YY)/zoom, 
165                          0.);
166       mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
167       dout << mark;
168     }
169   }    
170   dout.Flush();
171   Standard_Integer Dmin = 3;
172   Standard_Integer Dmax = 8;
173   Standard_Real Tol3d = 1.e-3;
174   
175   Handle(Geom_BSplineCurve) TheCurve;
176   GeomAPI_PointsToBSpline aPointToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol3d);
177   TheCurve = aPointToBSpline.Curve();
178
179   
180   DrawTrSurf::Set(a[1], TheCurve);
181   di << a[1];
182
183   return 0;
184
185 }
186
187
188 //=======================================================================
189 //function : grilapp
190 //purpose  : 
191 //=======================================================================
192
193 static Standard_Integer grilapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
194 {
195   if ( n < 12) return 1;
196
197   Standard_Integer i,j;
198   Standard_Integer Nu = atoi(a[2]);
199   Standard_Integer Nv = atoi(a[3]);
200   TColStd_Array2OfReal ZPoints (1, Nu, 1, Nv);
201
202   Standard_Real X0 = atof(a[4]);
203   Standard_Real dX = atof(a[5]);
204   Standard_Real Y0 = atof(a[6]);
205   Standard_Real dY = atof(a[7]);
206
207   Standard_Integer Count = 8;
208   for ( j = 1; j <= Nv; j++) {
209     for ( i = 1; i <= Nu; i++) {
210       if ( Count > n) return 1;
211       ZPoints(i,j) = atof(a[Count]);
212       Count++;
213     }
214   }
215   
216   Handle(Geom_BSplineSurface) S 
217     = GeomAPI_PointsToBSplineSurface(ZPoints,X0,dX,Y0,dY);
218   DrawTrSurf::Set(a[1],S);
219
220   di << a[1];
221   
222   return 0;
223 }
224
225 //=======================================================================
226 //function : surfapp
227 //purpose  : 
228 //=======================================================================
229
230 static Standard_Integer surfapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
231 {
232   if ( n < 5 ) return 1;
233
234   Standard_Integer i,j;
235   Standard_Integer Nu = atoi(a[2]);
236   Standard_Integer Nv = atoi(a[3]);
237   TColgp_Array2OfPnt Points (1, Nu, 1, Nv);
238
239   if ( n == 5) {
240     Handle(Geom_Surface) Surf = DrawTrSurf::GetSurface(a[4]);
241     if ( Surf.IsNull()) return 1;
242
243     Standard_Real U, V, U1, V1, U2, V2;
244     Surf->Bounds( U1, U2, V1, V2);
245     for ( j = 1; j <= Nv; j++) {
246       V = V1 + (j-1) * (V2-V1) / (Nv-1);
247       for ( i = 1; i <= Nu; i++) {
248         U = U1 + (i-1) * (U2-U1) / (Nu-1);
249         Points(i,j) = Surf->Value(U,V);
250       }
251     } 
252   }
253   else if ( n >= 16) {
254     Standard_Integer Count = 4;
255     for ( j = 1; j <= Nv; j++) {
256       for ( i = 1; i <= Nu; i++) {
257         if ( Count > n) return 1;
258         Points(i,j) = gp_Pnt(atof(a[Count]),atof(a[Count+1]),atof(a[Count+2]));
259         Count += 3;
260       }
261     }
262   }
263   char name[100];
264   Standard_Integer Count = 1;
265   for ( j = 1; j <= Nv; j++) {
266     for ( i = 1; i <= Nu; i++) {
267       sprintf(name,"point_%d",Count++);
268       char* temp = name; // portage WNT
269       DrawTrSurf::Set(temp,Points(i,j));
270     }
271   } 
272
273   Handle(Geom_BSplineSurface) S = GeomAPI_PointsToBSplineSurface(Points);
274   DrawTrSurf::Set(a[1],S);
275   di << a[1];
276
277   return 0;
278 }
279
280
281 //=======================================================================
282 //function : extrema
283 //purpose  : 
284 //=======================================================================
285
286 static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
287 {
288   if ( n<3) return 1;
289
290   Handle(Geom_Curve)   GC1, GC2;
291   Handle(Geom_Surface) GS1, GS2;
292
293   Standard_Boolean C1 = Standard_False;
294   Standard_Boolean C2 = Standard_False;
295   Standard_Boolean S1 = Standard_False;
296   Standard_Boolean S2 = Standard_False;
297
298   Standard_Real U1f,U1l,U2f,U2l,V1f,V1l,V2f,V2l;
299
300   GC1 = DrawTrSurf::GetCurve(a[1]);
301   if ( GC1.IsNull()) {
302     GS1 = DrawTrSurf::GetSurface(a[1]);
303     if ( GS1.IsNull())
304       return 1;
305     S1 = Standard_True;
306     GS1->Bounds(U1f,U1l,V1f,V1l);
307   }
308   else {
309     C1 = Standard_True;
310     U1f = GC1->FirstParameter();
311     U1l = GC1->LastParameter();
312   }
313
314   GC2 = DrawTrSurf::GetCurve(a[2]);
315   if ( GC2.IsNull()) {
316     GS2 = DrawTrSurf::GetSurface(a[2]);
317     if ( GS2.IsNull())
318       return 1;
319     S2 = Standard_True;
320     GS2->Bounds(U2f,U2l,V2f,V2l);
321   }
322   else {
323     C2 = Standard_True;
324     U2f = GC2->FirstParameter();
325     U2l = GC2->LastParameter();
326   }
327
328   char name[100];
329   if ( C1 && C2) {
330     GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
331     if(!Ex.Extrema().IsParallel()) {
332       for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
333         gp_Pnt P1,P2;
334         Ex.Points(i,P1,P2);
335         if (P1.Distance(P2) < 1.e-16) {
336           di << "Extrema " << i << " is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
337           continue;
338         }
339         Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
340         Handle(Geom_TrimmedCurve) CT = 
341           new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
342         sprintf(name,"%s%d","ext_",i);
343         char* temp = name; // portage WNT
344         DrawTrSurf::Set(temp, CT);
345         di << name << " ";
346       }
347     }
348     else {
349       di << "Infinite number of extremas, distance = " << Ex.LowerDistance() << "\n";
350     }
351   }
352   else if ( C1 & S2) {
353     GeomAPI_ExtremaCurveSurface Ex(GC1,GS2,U1f,U1l,U2f,U2l,V2f,V2l);
354     for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
355       gp_Pnt P1,P2;
356       Ex.Points(i,P1,P2);
357       if (P1.Distance(P2) < 1.e-16) continue;
358       Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
359       Handle(Geom_TrimmedCurve) CT = 
360         new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
361       sprintf(name,"%s%d","ext_",i);
362       char* temp = name; // portage WNT
363       DrawTrSurf::Set(temp, CT);
364       di << name << " ";
365     }
366   }
367   else if ( S1 & C2) {
368     GeomAPI_ExtremaCurveSurface Ex(GC2,GS1,U2f,U2l,U1f,U1l,V1f,V1l);
369     for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
370       gp_Pnt P1,P2;
371       Ex.Points(i,P1,P2);
372       if (P1.Distance(P2) < 1.e-16) continue;
373       Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
374       Handle(Geom_TrimmedCurve) CT = 
375         new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
376       sprintf(name,"%s%d","ext_",i);
377       char* temp = name; // portage WNT
378       DrawTrSurf::Set(temp, CT);
379       di << name << " ";
380     }
381   }
382   else if ( S1 & S2) {
383     GeomAPI_ExtremaSurfaceSurface Ex(GS1,GS2,U1f,U1l,V1f,V1l,U2f,U2l,V2f,V2l);
384     for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
385       gp_Pnt P1,P2;
386       Ex.Points(i,P1,P2);
387       if (P1.Distance(P2) < 1.e-16) continue;
388       Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
389       Handle(Geom_TrimmedCurve) CT = 
390         new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
391       sprintf(name,"%s%d","ext_",i);
392       char* temp = name; // portage WNT
393       DrawTrSurf::Set(temp, CT);
394       di << name << " ";
395     }
396   }
397
398   return 0;
399
400 }
401
402 //=======================================================================
403 //function : totalextcc
404 //purpose  : 
405 //=======================================================================
406
407 static Standard_Integer totalextcc(Draw_Interpretor& di, Standard_Integer n, const char** a)
408 {
409   if ( n<3) return 1;
410
411   Handle(Geom_Curve)   GC1, GC2;
412
413
414   Standard_Real U1f,U1l,U2f,U2l;
415
416   GC1 = DrawTrSurf::GetCurve(a[1]);
417   if ( GC1.IsNull()) {
418       return 1;
419   }
420   else {
421     U1f = GC1->FirstParameter();
422     U1l = GC1->LastParameter();
423   }
424
425   GC2 = DrawTrSurf::GetCurve(a[2]);
426   if ( GC2.IsNull()) {
427       return 1;
428   }
429   else {
430     U2f = GC2->FirstParameter();
431     U2l = GC2->LastParameter();
432   }
433
434   char name[100];
435     GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
436   gp_Pnt P1,P2;
437   if(Ex.TotalNearestPoints(P1,P2)) {
438     if (P1.Distance(P2) < 1.e-16) {
439       di << "Extrema is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
440     }
441     else {
442       di << "Extrema is segment of line" << "\n"; 
443       Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
444       Handle(Geom_TrimmedCurve) CT = 
445         new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
446       sprintf(name,"%s%d","ext_",1);
447       char* temp = name; // portage WNT
448       DrawTrSurf::Set(temp, CT);
449       di << name << " ";
450     }
451
452     Standard_Real u1, u2;
453     Ex.TotalLowerDistanceParameters(u1, u2);
454
455     di << "Parameters on curves : " << u1 << " " << u2 << "\n";
456
457   }
458   else {
459     di << "Curves are infinite and parallel" << "\n";
460   }
461   
462   di << "Minimal distance : " << Ex.TotalLowerDistance() << "\n";
463
464   return 0;
465
466 }
467
468
469 void GeometryTest::APICommands(Draw_Interpretor& theCommands)
470 {
471   static Standard_Boolean done = Standard_False;
472   if (done) return;
473
474   done = Standard_True;
475   const char* g;
476
477   g = "GEOMETRY curves and surfaces analysis";
478
479   theCommands.Add("proj", "proj curve/surf x y z",__FILE__, proj);
480
481   g = "GEOMETRY approximations";
482
483   theCommands.Add("appro", "appro result nbpoint [curve]",__FILE__, appro);
484   theCommands.Add("surfapp","surfapp result nbupoint nbvpoint x y z ....",
485                   __FILE__,
486                   surfapp);
487   theCommands.Add("grilapp",
488        "grilapp result nbupoint nbvpoint X0 dX Y0 dY z11 z12 .. z1nu ....  ",
489         __FILE__,grilapp);
490
491   g = "GEOMETRY curves and surfaces analysis";
492
493   theCommands.Add("extrema", "extrema curve/surface curve/surface",__FILE__,extrema);
494   theCommands.Add("totalextcc", "totalextcc curve curve",__FILE__,totalextcc);
495 }