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 #include <GeometryTest.hxx>
18 #include <Geom2d_BSplineCurve.hxx>
20 #include <Draw_Interpretor.hxx>
21 #include <DrawTrSurf.hxx>
22 #include <Draw_Appli.hxx>
23 #include <DrawTrSurf_Curve2d.hxx>
24 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
25 #include <Geom2dAPI_ExtremaCurveCurve.hxx>
26 #include <Geom2dAPI_PointsToBSpline.hxx>
27 #include <Geom2dAPI_InterCurveCurve.hxx>
28 #include <Geom2d_Line.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <TColgp_Array1OfPnt2d.hxx>
32 #include <Draw_Marker2D.hxx>
33 #include <Draw_Color.hxx>
34 #include <Draw_MarkerShape.hxx>
35 #include <TColStd_Array1OfReal.hxx>
36 #include <GeomAbs_Shape.hxx>
40 Standard_IMPORT Draw_Viewer dout;
43 //=======================================================================
46 //=======================================================================
48 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
52 gp_Pnt2d P(Draw::Atof(a[2]),Draw::Atof(a[3]));
56 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]);
61 Geom2dAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
64 for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
65 gp_Pnt2d P1 = proj.Point(i);
66 Handle(Geom2d_Line) L = new Geom2d_Line(P,gp_Vec2d(P,P1));
67 Handle(Geom2d_TrimmedCurve) CT =
68 new Geom2d_TrimmedCurve(L, 0., P.Distance(P1));
69 Sprintf(name,"%s%d","ext_",i);
70 char* temp = name; // portage WNT
71 DrawTrSurf::Set(temp, CT);
78 //=======================================================================
81 //=======================================================================
83 static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
85 // Approximation et interpolation 2d
88 // - affiche la tolerance
90 // - change la tolerance
91 // 2dappro result nbpoint
92 // - saisie interactive
93 // 2dappro result nbpoint curve
94 // - calcule des points sur la courbe
95 // 2dappro result nbpoint x1 y1 x2 y2 ..
96 // - tableau de points
97 // 2dappro result nbpoint x1 dx y1 y2 ..
98 // - tableau de points (x1,y1) (x1+dx,y2) ... avec x = t
101 static Standard_Real Tol2d = 1.e-6;
105 Tol2d = Draw::Atof(a[1]);
107 di << "Tolerance for 2d approx : "<< Tol2d << "\n";
112 Standard_Integer i, Nb = Draw::Atoi(a[2]);
114 Standard_Boolean hasPoints = Standard_True;
115 TColgp_Array1OfPnt2d Points(1, Nb);
116 TColStd_Array1OfReal YValues(1,Nb);
117 Standard_Real X0=0,DX=0;
119 Handle(Draw_Marker2D) mark;
122 // saisie interactive
123 Standard_Integer id,XX,YY,b;
124 dout.Select(id,XX,YY,b);
125 Standard_Real zoom = dout.Zoom(id);
127 Points(1) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
128 ((Standard_Real)YY)/zoom );
130 mark = new Draw_Marker2D( Points(1), Draw_X, Draw_vert);
134 for (Standard_Integer j = 2; j<=Nb; j++) {
135 dout.Select(id,XX,YY,b);
136 Points(j) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
137 ((Standard_Real)YY)/zoom );
138 mark = new Draw_Marker2D( Points(j), Draw_X, Draw_vert);
145 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[3]);
149 Standard_Real U, U1, U2;
150 U1 = GC->FirstParameter();
151 U2 = GC->LastParameter();
152 Standard_Real Delta = ( U2 - U1) / (Nb-1);
153 for ( i = 1 ; i <= Nb; i++) {
154 U = U1 + (i-1) * Delta;
155 Points(i) = GC->Value(U);
160 // test points ou ordonnees
161 hasPoints = Standard_False;
162 Standard_Integer nc = n - 3;
166 for (i = 1; i <= Nb; i++) {
167 Points(i).SetCoord(Draw::Atof(a[nc]),Draw::Atof(a[nc+1]));
171 else if (nc - 2 == Nb) {
174 X0 = Draw::Atof(a[3]);
175 DX = Draw::Atof(a[4]);
176 for (i = 1; i <= Nb; i++) {
177 YValues(i) = Draw::Atof(a[nc]);
178 Points(i).SetCoord(X0+(i-1)*DX,YValues(i));
185 // display the points
186 for ( i = 1 ; i <= Nb; i++) {
187 mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert);
192 Standard_Integer Dmin = 3;
193 Standard_Integer Dmax = 8;
195 Handle(Geom2d_BSplineCurve) TheCurve;
197 TheCurve = Geom2dAPI_PointsToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol2d);
199 TheCurve = Geom2dAPI_PointsToBSpline(YValues,X0,DX,Dmin,Dmax,GeomAbs_C2,Tol2d);
201 DrawTrSurf::Set(a[1], TheCurve);
208 //=======================================================================
209 //function : intersect
211 //=======================================================================
213 static Standard_Integer intersect(Draw_Interpretor& /*di*/, Standard_Integer n, const char** a)
218 Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]);
222 Standard_Real Tol = 0.001;
223 Geom2dAPI_InterCurveCurve Intersector;
225 Handle(Geom2d_Curve) C2;
227 C2 = DrawTrSurf::GetCurve2d(a[2]);
230 Intersector.Init(C1,C2,Tol);
233 Intersector.Init(C1, Tol);
238 for ( i = 1; i <= Intersector.NbPoints(); i++) {
239 gp_Pnt2d P = Intersector.Point(i);
240 Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert);
245 Handle(Geom2d_Curve) S1,S2;
246 Handle(DrawTrSurf_Curve2d) CD;
248 for ( i = 1; i <= Intersector.NbSegments(); i++) {
249 Intersector.Segment(i,S1,S2);
250 CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30);
252 CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30);
262 void GeometryTest::API2dCommands(Draw_Interpretor& theCommands)
264 static Standard_Boolean done = Standard_False;
269 done = Standard_True;
270 g = "GEOMETRY curves and surfaces analysis";
272 theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g);
274 g = "GEOMETRY approximations";
276 theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__,
278 theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__,
281 g = "GEOMETRY curves and surfaces analysis";
283 g = "GEOMETRY intersections";
285 theCommands.Add("2dintersect", "intersect curve curve",__FILE__,