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