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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Geom_BSplineCurve.hxx>
18 #include <Geom_BSplineSurface.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 <GeomAPI_ExtremaCurveCurve.hxx>
27 #include <GeomAPI_ExtremaCurveSurface.hxx>
28 #include <GeomAPI_ExtremaSurfaceSurface.hxx>
29 #include <GeomAPI_PointsToBSpline.hxx>
30 #include <GeomAPI_PointsToBSplineSurface.hxx>
31 #include <Geom_Line.hxx>
32 #include <Geom_TrimmedCurve.hxx>
33 #include <Draw_Segment3D.hxx>
34 #include <Draw_Marker3D.hxx>
35 #include <Draw_Color.hxx>
36 #include <Draw_MarkerShape.hxx>
37 #include <TColgp_Array1OfPnt.hxx>
38 #include <TColgp_Array2OfPnt.hxx>
39 #include <TColStd_Array2OfReal.hxx>
40 #include <Precision.hxx>
43 Standard_IMPORT Draw_Viewer dout;
46 //=======================================================================
49 //=======================================================================
51 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
55 cout << " Use proj curve/surf x y z [extrema algo: g(grad)/t(tree)]" << endl;
59 gp_Pnt P(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
63 Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(a[1]);
64 Handle(Geom_Surface) GS;
65 Extrema_ExtAlgo aProjAlgo = Extrema_ExtAlgo_Grad;
67 if (n == 6 && a[5][0] == 't')
68 aProjAlgo = Extrema_ExtAlgo_Tree;
72 GS = DrawTrSurf::GetSurface(a[1]);
77 Standard_Real U1, U2, V1, V2;
78 GS->Bounds(U1,U2,V1,V2);
80 GeomAPI_ProjectPointOnSurf proj(P,GS,U1,U2,V1,V2,aProjAlgo);
83 for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++)
85 gp_Pnt P1 = proj.Point(i);
86 if ( P.Distance(P1) > Precision::Confusion())
88 Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
89 Handle(Geom_TrimmedCurve) CT =
90 new Geom_TrimmedCurve(L, 0., P.Distance(P1));
91 Sprintf(name,"%s%d","ext_",i);
92 char* temp = name; // portage WNT
93 DrawTrSurf::Set(temp, CT);
98 Sprintf(name,"%s%d","ext_",i);
100 char* temp = name; // portage WNT
101 DrawTrSurf::Set(temp, P1);
102 proj.Parameters(i,UU,VV);
103 di << " Le point est sur la surface.\n";
104 di << " Ses parametres sont: UU = " << UU << "\n";
105 di << " VV = " << VV << "\n";
111 GeomAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
112 GC->LastParameter());
114 if(proj.NbPoints() == 0)
116 cout << "No project point was found." << endl;
120 for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++)
122 gp_Pnt P1 = proj.Point(i);
123 Standard_Real UU = proj.Parameter(i);
124 di << " parameter " << i << " = " << UU << "\n";
125 if ( P.Distance(P1) > Precision::Confusion())
127 Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
128 Handle(Geom_TrimmedCurve) CT =
129 new Geom_TrimmedCurve(L, 0., P.Distance(P1));
130 Sprintf(name,"%s%d","ext_",i);
131 char* temp = name; // portage WNT
132 DrawTrSurf::Set(temp, CT);
137 Sprintf(name,"%s%d","ext_",i);
138 char* temp = name; // portage WNT
139 DrawTrSurf::Set(temp, P1);
141 UU = proj.Parameter(i);
142 di << " Le point est sur la courbe.\n";
143 di << " Son parametre est U = " << UU << "\n";
151 //=======================================================================
154 //=======================================================================
156 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
160 Handle(Geom_Curve) GC;
161 Standard_Integer Nb = Draw::Atoi(a[2]);
163 TColgp_Array1OfPnt Points(1, Nb);
165 Handle(Draw_Marker3D) mark;
168 GC = DrawTrSurf::GetCurve(a[3]);
172 Standard_Real U, U1, U2;
173 U1 = GC->FirstParameter();
174 U2 = GC->LastParameter();
175 Standard_Real Delta = ( U2 - U1) / (Nb-1);
176 for ( Standard_Integer i = 1 ; i <= Nb; i++) {
177 U = U1 + (i-1) * Delta;
178 Points(i) = GC->Value(U);
179 mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert);
184 Standard_Integer id,XX,YY,b;
185 dout.Select(id,XX,YY,b);
186 Standard_Real zoom = dout.Zoom(id);
188 Points(1) = gp_Pnt( ((Standard_Real)XX)/zoom,
189 ((Standard_Real)YY)/zoom,
192 mark = new Draw_Marker3D( Points(1), Draw_X, Draw_vert);
196 for (Standard_Integer i = 2; i<=Nb; i++) {
197 dout.Select(id,XX,YY,b);
198 Points(i) = gp_Pnt( ((Standard_Real)XX)/zoom,
199 ((Standard_Real)YY)/zoom,
201 mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert);
206 Standard_Integer Dmin = 3;
207 Standard_Integer Dmax = 8;
208 Standard_Real Tol3d = 1.e-3;
210 Handle(Geom_BSplineCurve) TheCurve;
211 GeomAPI_PointsToBSpline aPointToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol3d);
212 TheCurve = aPointToBSpline.Curve();
215 DrawTrSurf::Set(a[1], TheCurve);
223 //=======================================================================
226 //=======================================================================
228 static Standard_Integer grilapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
230 if ( n < 12) return 1;
232 Standard_Integer i,j;
233 Standard_Integer Nu = Draw::Atoi(a[2]);
234 Standard_Integer Nv = Draw::Atoi(a[3]);
235 TColStd_Array2OfReal ZPoints (1, Nu, 1, Nv);
237 Standard_Real X0 = Draw::Atof(a[4]);
238 Standard_Real dX = Draw::Atof(a[5]);
239 Standard_Real Y0 = Draw::Atof(a[6]);
240 Standard_Real dY = Draw::Atof(a[7]);
242 Standard_Integer Count = 8;
243 for ( j = 1; j <= Nv; j++) {
244 for ( i = 1; i <= Nu; i++) {
245 if ( Count > n) return 1;
246 ZPoints(i,j) = Draw::Atof(a[Count]);
251 Handle(Geom_BSplineSurface) S
252 = GeomAPI_PointsToBSplineSurface(ZPoints,X0,dX,Y0,dY);
253 DrawTrSurf::Set(a[1],S);
260 //=======================================================================
263 //=======================================================================
265 static Standard_Integer surfapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
267 if ( n < 5 ) return 1;
269 Standard_Integer i,j;
270 Standard_Integer Nu = Draw::Atoi(a[2]);
271 Standard_Integer Nv = Draw::Atoi(a[3]);
272 TColgp_Array2OfPnt Points (1, Nu, 1, Nv);
275 Handle(Geom_Surface) Surf = DrawTrSurf::GetSurface(a[4]);
276 if ( Surf.IsNull()) return 1;
278 Standard_Real U, V, U1, V1, U2, V2;
279 Surf->Bounds( U1, U2, V1, V2);
280 for ( j = 1; j <= Nv; j++) {
281 V = V1 + (j-1) * (V2-V1) / (Nv-1);
282 for ( i = 1; i <= Nu; i++) {
283 U = U1 + (i-1) * (U2-U1) / (Nu-1);
284 Points(i,j) = Surf->Value(U,V);
289 Standard_Integer Count = 4;
290 for ( j = 1; j <= Nv; j++) {
291 for ( i = 1; i <= Nu; i++) {
292 if ( Count > n) return 1;
293 Points(i,j) = gp_Pnt(Draw::Atof(a[Count]),Draw::Atof(a[Count+1]),Draw::Atof(a[Count+2]));
299 Standard_Integer Count = 1;
300 for ( j = 1; j <= Nv; j++) {
301 for ( i = 1; i <= Nu; i++) {
302 Sprintf(name,"point_%d",Count++);
303 char* temp = name; // portage WNT
304 DrawTrSurf::Set(temp,Points(i,j));
308 Handle(Geom_BSplineSurface) S = GeomAPI_PointsToBSplineSurface(Points);
309 DrawTrSurf::Set(a[1],S);
316 //=======================================================================
319 //=======================================================================
321 static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
328 Handle(Geom_Curve) GC1, GC2;
329 Handle(Geom_Surface) GS1, GS2;
331 Standard_Boolean C1 = Standard_False;
332 Standard_Boolean C2 = Standard_False;
333 Standard_Boolean S1 = Standard_False;
334 Standard_Boolean S2 = Standard_False;
335 Standard_Boolean isInfinitySolutions = Standard_False;
336 Standard_Real aMinDist = RealLast();
338 Standard_Real U1f, U1l, U2f, U2l, V1f = 0., V1l = 0., V2f = 0., V2l = 0.;
340 GC1 = DrawTrSurf::GetCurve(a[1]);
342 GS1 = DrawTrSurf::GetSurface(a[1]);
346 GS1->Bounds(U1f,U1l,V1f,V1l);
350 U1f = GC1->FirstParameter();
351 U1l = GC1->LastParameter();
354 GC2 = DrawTrSurf::GetCurve(a[2]);
356 GS2 = DrawTrSurf::GetSurface(a[2]);
360 GS2->Bounds(U2f,U2l,V2f,V2l);
364 U2f = GC2->FirstParameter();
365 U2l = GC2->LastParameter();
368 NCollection_Vector<gp_Pnt> aPnts1, aPnts2;
369 NCollection_Vector<Standard_Real> aPrms[4];
372 GeomAPI_ExtremaCurveCurve Ex(GC1, GC2, U1f, U1l, U2f, U2l);
374 for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
377 Ex.Points(aJ, aP1, aP2);
381 Standard_Real aU1, aU2;
382 Ex.Parameters(aJ, aU1, aU2);
383 aPrms[0].Append(aU1);
384 aPrms[2].Append(aU2);
386 // Since GeomAPI cannot provide access to flag directly.
387 isInfinitySolutions = Ex.Extrema().IsParallel();
388 aMinDist = Ex.LowerDistance();
392 GeomAPI_ExtremaCurveSurface Ex(GC1, GS2, U1f, U1l, U2f, U2l, V2f, V2l);
393 for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
396 Ex.Points(aJ, aP1, aP2);
400 Standard_Real aU1, aU2, aV2;
401 Ex.Parameters(aJ, aU1, aU2, aV2);
402 aPrms[0].Append(aU1);
403 aPrms[2].Append(aU2);
404 aPrms[3].Append(aV2);
409 GeomAPI_ExtremaCurveSurface Ex(GC2, GS1, U2f, U2l, U1f, U1l, V1f, V1l);
410 for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
413 Ex.Points(aJ, aP2, aP1);
417 Standard_Real aU1, aV1, aU2;
418 Ex.Parameters(aJ, aU2, aU1, aV1);
419 aPrms[0].Append(aU1);
420 aPrms[1].Append(aV1);
421 aPrms[2].Append(aU2);
426 GeomAPI_ExtremaSurfaceSurface Ex(
427 GS1, GS2, U1f, U1l, V1f, V1l, U2f, U2l, V2f, V2l);
428 for (Standard_Integer aJ = 1; aJ <= Ex.NbExtrema(); ++aJ)
431 Ex.Points(aJ, aP1, aP2);
435 Standard_Real aU1, aV1, aU2, aV2;
436 Ex.Parameters(aJ, aU1, aV1, aU2, aV2);
437 aPrms[0].Append(aU1);
438 aPrms[1].Append(aV1);
439 aPrms[2].Append(aU2);
440 aPrms[3].Append(aV2);
445 char* aName2 = aName; // portage WNT
448 const Standard_Integer aPntCount = aPnts1.Size();
449 if (aPntCount == 0 || isInfinitySolutions)
451 // Infinity solutions flag may be set with 0 number of
452 // solutions in analytic extrema Curve/Curve.
453 if (isInfinitySolutions)
454 di << "Infinite number of extremas, distance = " << aMinDist << "\n";
456 di << "No solutions!\n";
458 for (Standard_Integer aJ = 1; aJ <= aPntCount; aJ++)
460 gp_Pnt aP1 = aPnts1(aJ - 1), aP2 = aPnts2(aJ - 1);
462 if (aP1.Distance(aP2) < 1.e-16)
464 di << "Extrema " << aJ << " is point : " <<
465 aP1.X() << " " << aP1.Y() << " " << aP1.Z() << "\n";
469 Handle(Geom_Line) aL = new Geom_Line(aP1, gp_Vec(aP1, aP2));
470 Handle(Geom_TrimmedCurve) aCT =
471 new Geom_TrimmedCurve(aL, 0., aP1.Distance(aP2));
472 Sprintf(aName, "%s%d", "ext_", aJ);
473 DrawTrSurf::Set(aName2, aCT);
480 for (Standard_Integer aJ = 1; aJ <= aPntCount; aJ++)
482 gp_Pnt aP1 = aPnts1(aJ - 1), aP2 = aPnts2(aJ - 1);
483 Sprintf(aName, "%s%d%s", "ext_", aJ, "_2");
484 DrawTrSurf::Set(aName2, aP1);
486 Sprintf(aName, "%s%d%s", "ext_", aJ, "_3");
487 DrawTrSurf::Set(aName2, aP2);
491 // Output parameters.
492 for (Standard_Integer aJ = 0; aJ < 4; ++aJ)
494 for (Standard_Integer aPrmCount = aPrms[aJ].Size(), aK = 0;
495 aK < aPrmCount; ++aK)
497 Standard_Real aP = aPrms[aJ](aK);
498 Sprintf(aName, "%s%d%s%d", "prm_", aJ + 1, "_", aK + 1);
499 Draw::Set(aName2, aP);
508 //=======================================================================
509 //function : totalextcc
511 //=======================================================================
513 static Standard_Integer totalextcc(Draw_Interpretor& di, Standard_Integer n, const char** a)
517 Handle(Geom_Curve) GC1, GC2;
520 Standard_Real U1f,U1l,U2f,U2l;
522 GC1 = DrawTrSurf::GetCurve(a[1]);
527 U1f = GC1->FirstParameter();
528 U1l = GC1->LastParameter();
531 GC2 = DrawTrSurf::GetCurve(a[2]);
536 U2f = GC2->FirstParameter();
537 U2l = GC2->LastParameter();
541 GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
543 if(Ex.TotalNearestPoints(P1,P2)) {
544 if (P1.Distance(P2) < 1.e-16) {
545 di << "Extrema is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
548 di << "Extrema is segment of line\n";
549 Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
550 Handle(Geom_TrimmedCurve) CT =
551 new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
552 Sprintf(name,"%s%d","ext_",1);
553 char* temp = name; // portage WNT
554 DrawTrSurf::Set(temp, CT);
558 Standard_Real u1, u2;
559 Ex.TotalLowerDistanceParameters(u1, u2);
561 di << "Parameters on curves : " << u1 << " " << u2 << "\n";
565 di << "Curves are infinite and parallel\n";
568 di << "Minimal distance : " << Ex.TotalLowerDistance() << "\n";
575 void GeometryTest::APICommands(Draw_Interpretor& theCommands)
577 static Standard_Boolean done = Standard_False;
580 done = Standard_True;
582 theCommands.Add("proj", "proj curve/surf x y z [extrema algo: g(grad)/t(tree)]",__FILE__, proj);
584 theCommands.Add("appro", "appro result nbpoint [curve]",__FILE__, appro);
585 theCommands.Add("surfapp","surfapp result nbupoint nbvpoint x y z ....",
588 theCommands.Add("grilapp",
589 "grilapp result nbupoint nbvpoint X0 dX Y0 dY z11 z12 .. z1nu .... ",
592 theCommands.Add("extrema", "extrema curve/surface curve/surface [extended_output = 0|1]",__FILE__,extrema);
593 theCommands.Add("totalextcc", "totalextcc curve curve",__FILE__,totalextcc);