0029712: Extrema algorithm raises exception
[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-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_BSplineCurve.hxx>
18 #include <Geom_BSplineSurface.hxx>
19 #include <Draw.hxx>
20 #include <Draw_Interpretor.hxx>
21 #include <DrawTrSurf.hxx>
22 #include <Draw_Appli.hxx>
23 #include <GeometryTest.hxx>
24 #include <GeomAPI_ProjectPointOnCurve.hxx>
25 #include <GeomAPI_ProjectPointOnSurf.hxx>
26 #include <Extrema_GenLocateExtPS.hxx>
27 #include <GeomAPI_ExtremaCurveCurve.hxx>
28 #include <GeomAPI_ExtremaCurveSurface.hxx>
29 #include <GeomAPI_ExtremaSurfaceSurface.hxx>
30 #include <GeomAPI_PointsToBSpline.hxx>
31 #include <GeomAPI_PointsToBSplineSurface.hxx>
32 #include <Geom_Line.hxx>
33 #include <Geom_TrimmedCurve.hxx>
34 #include <Draw_Segment3D.hxx>
35 #include <Draw_Marker3D.hxx>
36 #include <Draw_Color.hxx>
37 #include <Draw_MarkerShape.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
39 #include <TColgp_Array2OfPnt.hxx>
40 #include <TColStd_Array2OfReal.hxx>
41 #include <Precision.hxx>
42 #include <stdio.h>
43
44 #ifdef _WIN32
45 Standard_IMPORT Draw_Viewer dout;
46 #endif
47
48 //=======================================================================
49 //function : proj
50 //purpose  : 
51 //=======================================================================
52
53 static void showProjSolution(Draw_Interpretor& di,
54                              const Standard_Integer i,
55                              const gp_Pnt& P, const gp_Pnt& P1,
56                              const Standard_Real U, const Standard_Real V,
57                              const Standard_Boolean isSurface)
58 {
59   char name[100];
60   Sprintf(name, "%s%d", "ext_", i);
61   di << name << " ";
62   char* temp = name; // portage WNT
63   if (P.Distance(P1) > Precision::Confusion())
64   {
65     Handle(Geom_Line) L = new Geom_Line(P, gp_Vec(P, P1));
66     Handle(Geom_TrimmedCurve) CT =
67       new Geom_TrimmedCurve(L, 0., P.Distance(P1));
68     DrawTrSurf::Set(temp, CT);
69   }
70   else
71   {
72     DrawTrSurf::Set(temp, P1);
73     if (isSurface)
74       di << " Point on surface ";
75     else
76       di << " Point on curve ";
77   }
78   if (isSurface)
79     di << " Parameters: " << U << " " << V << "\n";
80   else
81     di << " parameter " << i << " = " << U << "\n";
82 }
83
84 //=======================================================================
85 //function : proj
86 //purpose  : 
87 //=======================================================================
88
89 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
90 {
91   if ( n < 5)
92   {
93     cout << " Use proj curve/surf x y z [{extrema algo: g(grad)/t(tree)}|{u v}]" << endl;
94     return 1;
95   }
96
97   gp_Pnt P(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
98
99   Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(a[1]);
100   Handle(Geom_Surface) GS;
101   Extrema_ExtAlgo aProjAlgo = Extrema_ExtAlgo_Grad;
102
103   if (n == 6 && a[5][0] == 't')
104     aProjAlgo = Extrema_ExtAlgo_Tree;
105
106   if (GC.IsNull())
107   {
108     GS = DrawTrSurf::GetSurface(a[1]);
109
110     if (GS.IsNull())
111       return 1;
112
113     if (n <= 6)
114     {
115       Standard_Real U1, U2, V1, V2;
116       GS->Bounds(U1,U2,V1,V2);
117
118       GeomAPI_ProjectPointOnSurf proj(P,GS,U1,U2,V1,V2,aProjAlgo);
119       if (!proj.IsDone())
120       {
121         di << "projection failed.";
122         return 0;
123       }
124
125       Standard_Real UU,VV;
126       for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++)
127       {
128         gp_Pnt P1 = proj.Point(i);
129         proj.Parameters(i, UU, VV);
130         showProjSolution(di, i, P, P1, UU, VV, Standard_True);
131       }
132     }
133     else if (n == 7)
134     {
135       const gp_XY aP2d(Draw::Atof(a[5]), Draw::Atof(a[6]));
136       GeomAdaptor_Surface aGAS(GS);
137       Extrema_GenLocateExtPS aProjector(aGAS, Precision::PConfusion(), Precision::PConfusion());
138       aProjector.Perform(P, aP2d.X(), aP2d.Y());
139       if (!aProjector.IsDone())
140       {
141         di << "projection failed.";
142         return 0;
143       }
144
145       const Extrema_POnSurf& aP = aProjector.Point();
146       Standard_Real UU, VV;
147       aP.Parameter(UU, VV);
148       showProjSolution(di, 1, P, aP.Value(), UU, VV, Standard_True);
149     }
150   }
151   else
152   {
153     GeomAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
154       GC->LastParameter());
155
156     if(proj.NbPoints() == 0)
157     {
158       cout << "No project point was found." << endl;
159       return 0;
160     }
161
162     for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++)
163     {
164       gp_Pnt P1 = proj.Point(i);
165       Standard_Real UU = proj.Parameter(i);
166       showProjSolution(di, i, P, P1, UU, UU, Standard_False);
167     }
168   }
169
170   return 0;
171 }
172
173 //=======================================================================
174 //function : appro
175 //purpose  : 
176 //=======================================================================
177
178 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
179 {
180   if ( n<3) return 1;
181
182   Handle(Geom_Curve) GC;
183   Standard_Integer Nb = Draw::Atoi(a[2]);
184
185   TColgp_Array1OfPnt Points(1, Nb);
186
187   Handle(Draw_Marker3D) mark;
188
189   if ( n == 4) {
190     GC = DrawTrSurf::GetCurve(a[3]);
191     if ( GC.IsNull()) 
192       return 1;
193
194     Standard_Real U, U1, U2;
195     U1 = GC->FirstParameter();
196     U2 = GC->LastParameter();
197     Standard_Real Delta = ( U2 - U1) / (Nb-1);
198     for ( Standard_Integer i = 1 ; i <= Nb; i++) {
199       U = U1 + (i-1) * Delta;
200       Points(i) = GC->Value(U);
201       mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
202       dout << mark;
203     }
204   }
205   else {
206     Standard_Integer id,XX,YY,b;
207     dout.Select(id,XX,YY,b);
208     Standard_Real zoom = dout.Zoom(id);
209
210     Points(1) = gp_Pnt( ((Standard_Real)XX)/zoom, 
211                         ((Standard_Real)YY)/zoom, 
212                         0.);
213     
214     mark = new Draw_Marker3D( Points(1), Draw_X, Draw_vert); 
215     
216     dout << mark;
217     
218     for (Standard_Integer i = 2; i<=Nb; i++) {
219       dout.Select(id,XX,YY,b);
220       Points(i) = gp_Pnt( ((Standard_Real)XX)/zoom, 
221                          ((Standard_Real)YY)/zoom, 
222                          0.);
223       mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
224       dout << mark;
225     }
226   }    
227   dout.Flush();
228   Standard_Integer Dmin = 3;
229   Standard_Integer Dmax = 8;
230   Standard_Real Tol3d = 1.e-3;
231   
232   Handle(Geom_BSplineCurve) TheCurve;
233   GeomAPI_PointsToBSpline aPointToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol3d);
234   TheCurve = aPointToBSpline.Curve();
235
236   
237   DrawTrSurf::Set(a[1], TheCurve);
238   di << a[1];
239
240   return 0;
241
242 }
243
244
245 //=======================================================================
246 //function : grilapp
247 //purpose  : 
248 //=======================================================================
249
250 static Standard_Integer grilapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
251 {
252   if ( n < 12) return 1;
253
254   Standard_Integer i,j;
255   Standard_Integer Nu = Draw::Atoi(a[2]);
256   Standard_Integer Nv = Draw::Atoi(a[3]);
257   TColStd_Array2OfReal ZPoints (1, Nu, 1, Nv);
258
259   Standard_Real X0 = Draw::Atof(a[4]);
260   Standard_Real dX = Draw::Atof(a[5]);
261   Standard_Real Y0 = Draw::Atof(a[6]);
262   Standard_Real dY = Draw::Atof(a[7]);
263
264   Standard_Integer Count = 8;
265   for ( j = 1; j <= Nv; j++) {
266     for ( i = 1; i <= Nu; i++) {
267       if ( Count > n) return 1;
268       ZPoints(i,j) = Draw::Atof(a[Count]);
269       Count++;
270     }
271   }
272   
273   Handle(Geom_BSplineSurface) S 
274     = GeomAPI_PointsToBSplineSurface(ZPoints,X0,dX,Y0,dY);
275   DrawTrSurf::Set(a[1],S);
276
277   di << a[1];
278   
279   return 0;
280 }
281
282 //=======================================================================
283 //function : surfapp
284 //purpose  : 
285 //=======================================================================
286
287 static Standard_Integer surfapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
288 {
289   if ( n < 5 ) return 1;
290
291   Standard_Integer i,j;
292   Standard_Integer Nu = Draw::Atoi(a[2]);
293   Standard_Integer Nv = Draw::Atoi(a[3]);
294   TColgp_Array2OfPnt Points (1, Nu, 1, Nv);
295
296   if ( n == 5) {
297     Handle(Geom_Surface) Surf = DrawTrSurf::GetSurface(a[4]);
298     if ( Surf.IsNull()) return 1;
299
300     Standard_Real U, V, U1, V1, U2, V2;
301     Surf->Bounds( U1, U2, V1, V2);
302     for ( j = 1; j <= Nv; j++) {
303       V = V1 + (j-1) * (V2-V1) / (Nv-1);
304       for ( i = 1; i <= Nu; i++) {
305         U = U1 + (i-1) * (U2-U1) / (Nu-1);
306         Points(i,j) = Surf->Value(U,V);
307       }
308     } 
309   }
310   else if ( n >= 16) {
311     Standard_Integer Count = 4;
312     for ( j = 1; j <= Nv; j++) {
313       for ( i = 1; i <= Nu; i++) {
314         if ( Count > n) return 1;
315         Points(i,j) = gp_Pnt(Draw::Atof(a[Count]),Draw::Atof(a[Count+1]),Draw::Atof(a[Count+2]));
316         Count += 3;
317       }
318     }
319   }
320   char name[100];
321   Standard_Integer Count = 1;
322   for ( j = 1; j <= Nv; j++) {
323     for ( i = 1; i <= Nu; i++) {
324       Sprintf(name,"point_%d",Count++);
325       char* temp = name; // portage WNT
326       DrawTrSurf::Set(temp,Points(i,j));
327     }
328   } 
329
330   Handle(Geom_BSplineSurface) S = GeomAPI_PointsToBSplineSurface(Points);
331   DrawTrSurf::Set(a[1],S);
332   di << a[1];
333
334   return 0;
335 }
336
337
338 //=======================================================================
339 //function : extrema
340 //purpose  : 
341 //=======================================================================
342
343 static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
344 {
345   if (n < 3)
346   {
347     return 1;
348   }
349
350   Handle(Geom_Curve) GC1, GC2;
351   Handle(Geom_Surface) GS1, GS2;
352
353   Standard_Boolean isInfinitySolutions = Standard_False;
354   Standard_Real aMinDist = RealLast();
355
356   Standard_Real U1f, U1l, U2f, U2l, V1f = 0., V1l = 0., V2f = 0., V2l = 0.;
357
358   GC1 = DrawTrSurf::GetCurve(a[1]);
359   if ( GC1.IsNull()) {
360     GS1 = DrawTrSurf::GetSurface(a[1]);
361     if ( GS1.IsNull())
362       return 1;
363
364     GS1->Bounds(U1f,U1l,V1f,V1l);
365   }
366   else {
367     U1f = GC1->FirstParameter();
368     U1l = GC1->LastParameter();
369   }
370
371   GC2 = DrawTrSurf::GetCurve(a[2]);
372   if ( GC2.IsNull()) {
373     GS2 = DrawTrSurf::GetSurface(a[2]);
374     if ( GS2.IsNull())
375       return 1;
376     GS2->Bounds(U2f,U2l,V2f,V2l);
377   }
378   else {
379     U2f = GC2->FirstParameter();
380     U2l = GC2->LastParameter();
381   }
382
383   NCollection_Vector<gp_Pnt> aPnts1, aPnts2;
384   NCollection_Vector<Standard_Real> aPrms[4];
385   if (!GC1.IsNull() && !GC2.IsNull())
386   {
387     GeomAPI_ExtremaCurveCurve Ex(GC1, GC2, U1f, U1l, U2f, U2l);
388     
389     // Since GeomAPI cannot provide access to flag directly.
390     isInfinitySolutions = Ex.Extrema().IsParallel();
391     if (isInfinitySolutions)
392     {
393       aMinDist = Ex.LowerDistance();
394     }
395     else
396     {
397       for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
398       {
399         gp_Pnt aP1, aP2;
400         Ex.Points(aJ, aP1, aP2);
401         aPnts1.Append(aP1);
402         aPnts2.Append(aP2);
403
404         Standard_Real aU1, aU2;
405         Ex.Parameters(aJ, aU1, aU2);
406         aPrms[0].Append(aU1);
407         aPrms[2].Append(aU2);
408       }
409     }
410   }
411   else if (!GC1.IsNull() && !GS2.IsNull())
412   {
413     GeomAPI_ExtremaCurveSurface Ex(GC1, GS2, U1f, U1l, U2f, U2l, V2f, V2l);
414
415     isInfinitySolutions = Ex.Extrema().IsParallel();
416     if (isInfinitySolutions)
417     {
418       aMinDist = Ex.LowerDistance();
419     }
420     else
421     {
422       for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
423       {
424         gp_Pnt aP1, aP2;
425         Ex.Points(aJ, aP1, aP2);
426         aPnts1.Append(aP1);
427         aPnts2.Append(aP2);
428
429         Standard_Real aU1, aU2, aV2;
430         Ex.Parameters(aJ, aU1, aU2, aV2);
431         aPrms[0].Append(aU1);
432         aPrms[2].Append(aU2);
433         aPrms[3].Append(aV2);
434       }
435     }
436   }
437   else if (!GS1.IsNull() && !GC2.IsNull())
438   {
439     GeomAPI_ExtremaCurveSurface Ex(GC2, GS1, U2f, U2l, U1f, U1l, V1f, V1l);
440
441     isInfinitySolutions = Ex.Extrema().IsParallel();
442     if (isInfinitySolutions)
443     {
444       aMinDist = Ex.LowerDistance();
445     }
446     else
447     {
448       for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
449       {
450         gp_Pnt aP2, aP1;
451         Ex.Points(aJ, aP2, aP1);
452         aPnts1.Append(aP1);
453         aPnts2.Append(aP2);
454
455         Standard_Real aU1, aV1, aU2;
456         Ex.Parameters(aJ, aU2, aU1, aV1);
457         aPrms[0].Append(aU1);
458         aPrms[1].Append(aV1);
459         aPrms[2].Append(aU2);
460       }
461     }
462   }
463   else if (!GS1.IsNull() && !GS2.IsNull())
464   {
465     GeomAPI_ExtremaSurfaceSurface Ex(GS1, GS2, U1f, U1l, V1f, V1l, U2f, U2l, V2f, V2l);
466     // Since GeomAPI cannot provide access to flag directly.
467     isInfinitySolutions = Ex.Extrema().IsParallel();
468     if (isInfinitySolutions)
469     {
470       aMinDist = Ex.LowerDistance();
471     }
472     else
473     {
474       for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
475       {
476         gp_Pnt aP1, aP2;
477         Ex.Points(aJ, aP1, aP2);
478         aPnts1.Append(aP1);
479         aPnts2.Append(aP2);
480
481         Standard_Real aU1, aV1, aU2, aV2;
482         Ex.Parameters(aJ, aU1, aV1, aU2, aV2);
483         aPrms[0].Append(aU1);
484         aPrms[1].Append(aV1);
485         aPrms[2].Append(aU2);
486         aPrms[3].Append(aV2);
487       }
488     }
489   }
490
491   char aName[100];
492   char* aName2 = aName; // portage WNT
493
494   // Output points.
495   const Standard_Integer aPntCount = aPnts1.Size();
496   if (aPntCount == 0 || isInfinitySolutions)
497   {
498     // Infinity solutions flag may be set with 0 number of 
499     // solutions in analytic extrema Curve/Curve.
500     if (isInfinitySolutions) 
501       di << "Infinite number of extremas, distance = " << aMinDist << "\n";
502     else
503       di << "No solutions!\n";
504   }
505   for (Standard_Integer aJ = 1; aJ <= aPntCount; aJ++)
506   {
507     gp_Pnt aP1 = aPnts1(aJ - 1), aP2 = aPnts2(aJ - 1);
508
509     if (aP1.Distance(aP2) < 1.e-16)
510     {
511       di << "Extrema " << aJ << " is point : " <<
512         aP1.X() << " " << aP1.Y() << " " << aP1.Z() << "\n";
513       continue;
514     }
515
516     Handle(Geom_Line) aL = new Geom_Line(aP1, gp_Vec(aP1, aP2));
517     Handle(Geom_TrimmedCurve) aCT = 
518       new Geom_TrimmedCurve(aL, 0., aP1.Distance(aP2));
519     Sprintf(aName, "%s%d", "ext_", aJ);
520     DrawTrSurf::Set(aName2, aCT);
521     di << aName << " ";
522   }
523
524   if (n >= 4)
525   {
526     // Output points.
527     for (Standard_Integer aJ = 1; aJ <= aPntCount; aJ++)
528     {
529       gp_Pnt aP1 = aPnts1(aJ - 1), aP2 = aPnts2(aJ - 1);
530       Sprintf(aName, "%s%d%s", "ext_", aJ, "_2");
531       DrawTrSurf::Set(aName2, aP1);
532       di << aName << " ";
533       Sprintf(aName, "%s%d%s", "ext_", aJ, "_3");
534       DrawTrSurf::Set(aName2, aP2);
535       di << aName << " ";
536     }
537
538     // Output parameters.
539     for (Standard_Integer aJ = 0; aJ < 4; ++aJ)
540     {
541       for (Standard_Integer aPrmCount = aPrms[aJ].Size(), aK = 0;
542         aK < aPrmCount; ++aK)
543       {
544         Standard_Real aP = aPrms[aJ](aK);
545         Sprintf(aName, "%s%d%s%d", "prm_", aJ + 1, "_", aK + 1);
546         Draw::Set(aName2, aP);
547         di << aName << " ";
548       }
549     }
550   }
551
552   return 0;
553 }
554
555 //=======================================================================
556 //function : totalextcc
557 //purpose  : 
558 //=======================================================================
559
560 static Standard_Integer totalextcc(Draw_Interpretor& di, Standard_Integer n, const char** a)
561 {
562   if ( n<3) return 1;
563
564   Handle(Geom_Curve)   GC1, GC2;
565
566
567   Standard_Real U1f,U1l,U2f,U2l;
568
569   GC1 = DrawTrSurf::GetCurve(a[1]);
570   if ( GC1.IsNull()) {
571       return 1;
572   }
573   else {
574     U1f = GC1->FirstParameter();
575     U1l = GC1->LastParameter();
576   }
577
578   GC2 = DrawTrSurf::GetCurve(a[2]);
579   if ( GC2.IsNull()) {
580       return 1;
581   }
582   else {
583     U2f = GC2->FirstParameter();
584     U2l = GC2->LastParameter();
585   }
586
587   char name[100];
588     GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
589   gp_Pnt P1,P2;
590   if(Ex.TotalNearestPoints(P1,P2)) {
591     if (P1.Distance(P2) < 1.e-16) {
592       di << "Extrema is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
593     }
594     else {
595       di << "Extrema is segment of line\n"; 
596       Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
597       Handle(Geom_TrimmedCurve) CT = 
598         new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
599       Sprintf(name,"%s%d","ext_",1);
600       char* temp = name; // portage WNT
601       DrawTrSurf::Set(temp, CT);
602       di << name << " ";
603     }
604
605     Standard_Real u1, u2;
606     Ex.TotalLowerDistanceParameters(u1, u2);
607
608     di << "Parameters on curves : " << u1 << " " << u2 << "\n";
609
610   }
611   else {
612     di << "Curves are infinite and parallel\n";
613   }
614   
615   di << "Minimal distance : " << Ex.TotalLowerDistance() << "\n";
616
617   return 0;
618
619 }
620
621
622 void GeometryTest::APICommands(Draw_Interpretor& theCommands)
623 {
624   static Standard_Boolean done = Standard_False;
625   if (done) return;
626
627   done = Standard_True;
628
629   theCommands.Add("proj", "proj curve/surf x y z [{extrema algo: g(grad)/t(tree)}|{u v}]\n"
630                   "\t\tOptional parameters are relevant to surf only.\n"
631                   "\t\tIf initial {u v} are given then local extrema is called",__FILE__, proj);
632
633   theCommands.Add("appro", "appro result nbpoint [curve]",__FILE__, appro);
634   theCommands.Add("surfapp","surfapp result nbupoint nbvpoint x y z ....",
635                   __FILE__,
636                   surfapp);
637   theCommands.Add("grilapp",
638        "grilapp result nbupoint nbvpoint X0 dX Y0 dY z11 z12 .. z1nu ....  ",
639         __FILE__,grilapp);
640
641   theCommands.Add("extrema", "extrema curve/surface curve/surface [extended_output = 0|1]",__FILE__,extrema);
642   theCommands.Add("totalextcc", "totalextcc curve curve",__FILE__,totalextcc);
643 }