1 // Created on: 1995-01-11
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 // modified : pmn 11/04/97 : mis dans GeomliteTest
20 #include <GeomliteTest.hxx>
21 #include <Geom2d_BSplineCurve.hxx>
23 #include <Draw_Interpretor.hxx>
24 #include <DrawTrSurf.hxx>
25 #include <Draw_Appli.hxx>
26 #include <DrawTrSurf_Curve2d.hxx>
27 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
28 #include <Geom2dAPI_ExtremaCurveCurve.hxx>
29 #include <Geom2dAPI_PointsToBSpline.hxx>
30 #include <Geom2dAPI_InterCurveCurve.hxx>
31 #include <Geom2d_Line.hxx>
32 #include <Geom2d_TrimmedCurve.hxx>
33 #include <TColgp_Array1OfPnt2d.hxx>
35 #include <Draw_Marker2D.hxx>
36 #include <Draw_Color.hxx>
37 #include <Draw_MarkerShape.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <GeomAbs_Shape.hxx>
40 #include <Precision.hxx>
41 #include <Geom2d_Circle.hxx>
42 #include <IntAna2d_AnaIntersection.hxx>
43 #include <IntAna2d_IntPoint.hxx>
44 #include <IntRes2d_IntersectionPoint.hxx>
48 Standard_IMPORT Draw_Viewer dout;
51 //=======================================================================
54 //=======================================================================
56 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
60 gp_Pnt2d P(Draw::Atof(a[2]),Draw::Atof(a[3]));
64 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]);
69 Geom2dAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
72 for (Standard_Integer i = 1; i <= proj.NbPoints(); i++)
74 gp_Pnt2d aP1 = proj.Point(i);
75 const Standard_Real aDist = P.Distance(aP1);
76 Sprintf(name, "%s%d", "ext_", i);
78 if (aDist > Precision::PConfusion())
80 Handle(Geom2d_Line) L = new Geom2d_Line(P, gp_Dir2d(aP1.XY() - P.XY()));
81 Handle(Geom2d_TrimmedCurve) CT = new Geom2d_TrimmedCurve(L, 0., aDist);
82 DrawTrSurf::Set(name, CT);
86 DrawTrSurf::Set(name, aP1);
95 //=======================================================================
98 //=======================================================================
100 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
102 // Approximation et interpolation 2d
105 // - affiche la tolerance
107 // - change la tolerance
108 // 2dappro result nbpoint
109 // - saisie interactive
110 // 2dappro result nbpoint curve
111 // - calcule des points sur la courbe
112 // 2dappro result nbpoint x1 y1 x2 y2 ..
113 // - tableau de points
114 // 2dappro result nbpoint x1 dx y1 y2 ..
115 // - tableau de points (x1,y1) (x1+dx,y2) ... avec x = t
118 static Standard_Real Tol2d = 1.e-6;
122 Tol2d = Draw::Atof(a[1]);
124 di << "Tolerance for 2d approx : "<< Tol2d << "\n";
129 Standard_Integer i, Nb = Draw::Atoi(a[2]);
131 Standard_Boolean hasPoints = Standard_True;
132 TColgp_Array1OfPnt2d Points(1, Nb);
133 TColStd_Array1OfReal YValues(1,Nb);
134 Standard_Real X0=0,DX=0;
136 Handle(Draw_Marker2D) mark;
139 // saisie interactive
140 Standard_Integer id,XX,YY,b;
141 dout.Select(id,XX,YY,b);
142 Standard_Real zoom = dout.Zoom(id);
144 Points(1) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
145 ((Standard_Real)YY)/zoom );
147 mark = new Draw_Marker2D( Points(1), Draw_X, Draw_vert);
151 for (i = 2; i<=Nb; i++) {
152 dout.Select(id,XX,YY,b);
153 Points(i) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
154 ((Standard_Real)YY)/zoom );
155 mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert);
162 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[3]);
166 Standard_Real U, U1, U2;
167 U1 = GC->FirstParameter();
168 U2 = GC->LastParameter();
169 Standard_Real Delta = ( U2 - U1) / (Nb-1);
170 for ( i = 1 ; i <= Nb; i++) {
171 U = U1 + (i-1) * Delta;
172 Points(i) = GC->Value(U);
177 // test points ou ordonnees
178 hasPoints = Standard_False;
179 Standard_Integer nc = n - 3;
183 for (i = 1; i <= Nb; i++) {
184 Points(i).SetCoord(Draw::Atof(a[nc]),Draw::Atof(a[nc+1]));
188 else if (nc - 2 == Nb) {
191 X0 = Draw::Atof(a[3]);
192 DX = Draw::Atof(a[4]);
193 for (i = 1; i <= Nb; i++) {
194 YValues(i) = Draw::Atof(a[nc]);
195 Points(i).SetCoord(X0+(i-1)*DX,YValues(i));
202 // display the points
203 for ( i = 1 ; i <= Nb; i++) {
204 mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert);
209 Standard_Integer Dmin = 3;
210 Standard_Integer Dmax = 8;
212 Handle(Geom2d_BSplineCurve) TheCurve;
214 TheCurve = Geom2dAPI_PointsToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol2d);
216 TheCurve = Geom2dAPI_PointsToBSpline(YValues,X0,DX,Dmin,Dmax,GeomAbs_C2,Tol2d);
218 DrawTrSurf::Set(a[1], TheCurve);
225 //=======================================================================
228 //=======================================================================
230 static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
234 Handle(Geom2d_Curve) GC1, GC2;
236 Standard_Real U1f,U1l,U2f,U2l;
238 GC1 = DrawTrSurf::GetCurve2d(a[1]);
241 U1f = GC1->FirstParameter();
242 U1l = GC1->LastParameter();
244 GC2 = DrawTrSurf::GetCurve2d(a[2]);
247 U2f = GC2->FirstParameter();
248 U2l = GC2->LastParameter();
252 Geom2dAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
253 Standard_Boolean isInfinitySolutions = Ex.Extrema().IsParallel();
254 const Standard_Integer aNExtr = Ex.NbExtrema();
256 if (aNExtr == 0 || isInfinitySolutions)
258 // Infinity solutions flag may be set with 0 number of
259 // solutions in analytic extrema Curve/Curve.
260 if (isInfinitySolutions)
261 di << "Infinite number of extremas, distance = " << Ex.LowerDistance() << "\n";
263 di << "No solutions!\n";
266 for (Standard_Integer i = 1; i <= aNExtr; i++)
270 di << "dist " << i << ": " << Ex.Distance(i) << " ";
271 if (Ex.Distance(i) <= Precision::PConfusion())
273 Handle(Draw_Marker2D) mark = new Draw_Marker2D( P1, Draw_X, Draw_vert);
276 Sprintf(name,"%s%d","ext_",i);
278 DrawTrSurf::Set(temp, P1);
283 Handle(Geom2d_Line) L = new Geom2d_Line(P1,gp_Vec2d(P1,P2));
284 Handle(Geom2d_TrimmedCurve) CT = new Geom2d_TrimmedCurve(L, 0., P1.Distance(P2));
285 Sprintf(name,"%s%d","ext_",i);
286 char* temp = name; // portage WNT
287 DrawTrSurf::Set(temp, CT);
295 //=======================================================================
296 //function : intersect
298 //=======================================================================
299 static Standard_Integer intersect(Draw_Interpretor& di, Standard_Integer n, const char** a)
303 cout<< "2dintersect curve curve [Tol]"<<endl;
307 Standard_Integer k = 1;
308 Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[k++]);
312 Standard_Real Tol = 0.001;
313 Geom2dAPI_InterCurveCurve Intersector;
315 Handle(Geom2d_Curve) C2;
317 C2 = DrawTrSurf::GetCurve2d(a[k++]);
322 Tol = Draw::Atof(a[k]);
326 Intersector.Init(C1,C2,Tol);
329 Intersector.Init(C1, Tol);
334 for ( i = 1; i <= Intersector.NbPoints(); i++) {
335 gp_Pnt2d P = Intersector.Point(i);
337 di<<"Intersection point "<<i<<" : "<<P.X()<<" "<<P.Y()<<"\n";
338 di<<"parameter on the fist: "<<Intersector.Intersector().Point(i).ParamOnFirst();
339 di<<" parameter on the second: "<<Intersector.Intersector().Point(i).ParamOnSecond()<<"\n";
340 Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert);
345 Handle(Geom2d_Curve) S1,S2;
346 Handle(DrawTrSurf_Curve2d) CD;
347 for ( i = 1; i <= Intersector.NbSegments(); i++) {
348 di << "Segment #" << i << " found.\n";
349 Intersector.Segment(i,S1,S2);
350 CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30);
352 CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30);
361 //=======================================================================
362 //function : intersect
364 //=======================================================================
366 static Standard_Integer intersect_ana(Draw_Interpretor& di, Standard_Integer n, const char** a)
370 cout<< "2dintana circle circle "<<endl;
374 Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]);
375 if ( C1.IsNull() && !C1->IsKind(STANDARD_TYPE(Geom2d_Circle)))
378 Handle(Geom2d_Curve) C2 = DrawTrSurf::GetCurve2d(a[2]);
379 if ( C2.IsNull() && !C2->IsKind(STANDARD_TYPE(Geom2d_Circle)))
382 Handle(Geom2d_Circle) aCir1 = Handle(Geom2d_Circle)::DownCast(C1);
383 Handle(Geom2d_Circle) aCir2 = Handle(Geom2d_Circle)::DownCast(C2);
385 IntAna2d_AnaIntersection Intersector(aCir1->Circ2d(), aCir2->Circ2d());
389 for ( i = 1; i <= Intersector.NbPoints(); i++) {
390 gp_Pnt2d P = Intersector.Point(i).Value();
391 di<<"Intersection point "<<i<<" : "<<P.X()<<" "<<P.Y()<<"\n";
392 di<<"parameter on the fist: "<<Intersector.Point(i).ParamOnFirst();
393 di<<" parameter on the second: "<<Intersector.Point(i).ParamOnSecond()<<"\n";
394 Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert);
404 void GeomliteTest::API2dCommands(Draw_Interpretor& theCommands)
406 static Standard_Boolean done = Standard_False;
411 done = Standard_True;
412 g = "GEOMETRY curves and surfaces analysis";
414 theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g);
416 g = "GEOMETRY approximations";
418 theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__,
420 theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__,
423 g = "GEOMETRY curves and surfaces analysis";
425 theCommands.Add("2dextrema", "extrema curve curve",__FILE__,
428 g = "GEOMETRY intersections";
430 theCommands.Add("2dintersect", "intersect curve curve [Tol]",__FILE__,
433 theCommands.Add("2dintanalytical", "intersect curve curve using IntAna",__FILE__,