0030366: Coding Rules - GeometryTest_API2dCommands.cxx seems to be excess
[occt.git] / src / GeometryTest / GeometryTest_TestProjCommands.cxx
1 // Created on: 2011-06-30
2 // Created by: jgv@ROLEX
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <GeometryTest.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom2d_Curve.hxx>
19 #include <Geom_Surface.hxx>
20 #include <GeomAPI_ProjectPointOnSurf.hxx>
21
22 #include <DBRep.hxx>
23 #include <DBRep_DrawableShape.hxx>
24 #include <Draw_Interpretor.hxx>
25 #include <Draw_Appli.hxx>
26 #include <DrawTrSurf.hxx>
27 #include <Draw_Marker3D.hxx>
28
29 #include <stdio.h>
30 #ifdef _WIN32
31 Standard_IMPORT Draw_Viewer dout;
32 #endif
33
34 //=======================================================================
35 //function : xdistcc
36 //purpose  : 
37 //=======================================================================
38 static Standard_Integer xdistcc(Draw_Interpretor& , Standard_Integer n, const char** a)
39 {
40   if (n < 5)
41   {
42     cout<<" Use xdistcc c1 c2 t1 t2 nbp"<<endl;
43     return 0;
44   }
45
46   Standard_Integer i, aNbP, iSize;
47   Standard_Real aD, aT, aT1, aT2, dT;
48   gp_Pnt aP1, aP2;
49   Handle(Geom_Curve) aC1, aC2;
50   Handle(Draw_Marker3D) aMr;
51   Draw_Color aColor(Draw_rouge);
52
53   aC1=DrawTrSurf::GetCurve(a[1]);
54   if (aC1.IsNull())
55   {
56     cout<<a[1]<<" is null curve"<<endl;
57     return 0;
58   }
59
60   aC2=DrawTrSurf::GetCurve(a[2]);
61   if (aC2.IsNull())
62   {
63     cout<<a[2]<<" is null curve"<<endl;
64     return 0;
65   }
66
67   aT1=Draw::Atof(a[3]);
68   aT2=Draw::Atof(a[4]);
69
70   aNbP=10;
71   if (n > 4)
72   {
73     aNbP=Draw::Atoi(a[5]);
74   }
75
76   iSize=3;
77
78   Standard_Real aMaxParam = 0.0;
79   Standard_Real aMaxDist  = 0.0;
80
81   dT=(aT2 - aT1) / (aNbP - 1);
82   for(i = 0; i < aNbP; ++i)
83   {
84     aT=aT1 + i * dT;
85     if (i == aNbP-1) 
86       aT=aT2;
87
88     aC1->D0(aT, aP1);
89     aC2->D0(aT, aP2);
90
91     aD = aP1.Distance(aP2);
92
93     if (aD > aMaxDist)
94     {
95       aMaxParam = aT;
96       aMaxDist = aD;
97     }
98
99     printf(" T=%lg\tD=%lg\n", aT, aD);
100
101     aMr=new Draw_Marker3D(aP1, Draw_Plus, aColor, iSize);
102     dout << aMr;
103   }
104
105   cout << "Max distance = "  << aMaxDist << endl;
106   cout << "Param = " << aMaxParam << endl;
107
108   return 0;
109 }
110
111 //=======================================================================
112 //function : xdistc2dc2dss
113 //purpose  : 
114 //=======================================================================
115 static Standard_Integer xdistc2dc2dss(Draw_Interpretor& , Standard_Integer n, const char** a)
116 {
117   if (n < 7)
118   {
119     cout<<" Use xdistc2dc2dss c2d_1 c2d_2 s1 s2 t1 t2 nbp"<<endl;
120     return 0;
121   }
122
123   Standard_Integer i, aNbP, iSize;
124   Standard_Real aD, aT, aT1, aT2, dT;
125   gp_Pnt aP1, aP2;
126   gp_Pnt2d aP2d1, aP2d2;
127   Handle(Geom2d_Curve) aC2d1, aC2d2;
128   Handle(Geom_Surface) aS1, aS2;
129   Handle(Draw_Marker3D) aMr;
130   Draw_Color aColor(Draw_rouge);
131
132   aC2d1=DrawTrSurf::GetCurve2d(a[1]);
133   if (aC2d1.IsNull())
134   {
135     cout<<a[1]<<" is null 2dcurve"<<endl;
136     return 0;
137   }
138
139   aC2d2=DrawTrSurf::GetCurve2d(a[2]);
140   if (aC2d2.IsNull())
141   {
142     cout<<a[2]<<" is null 2dcurve"<<endl;
143     return 0;
144   }
145
146   aS1=DrawTrSurf::GetSurface(a[3]);
147   if (aS1.IsNull())
148   {
149     cout<<a[3]<<" is null surface"<<endl;
150     return 0;
151   }
152
153   aS2=DrawTrSurf::GetSurface(a[4]);
154   if (aS2.IsNull())
155   {
156     cout<<a[4]<<" is null surface"<<endl;
157     return 0;
158   }
159
160   aT1=Draw::Atof(a[5]);
161   aT2=Draw::Atof(a[6]);
162
163   aNbP=10;
164   if (n > 6)
165   {
166     aNbP=Draw::Atoi(a[7]);
167   }
168
169   iSize=3;
170
171   Standard_Real aMaxParam = 0.0;
172   Standard_Real aMaxDist  = 0.0;
173
174   dT=(aT2 - aT1) / (aNbP - 1);
175   for(i = 0; i < aNbP; ++i)
176   {
177     aT=aT1 + i * dT;
178     if (i == aNbP-1) 
179       aT=aT2;
180
181     aC2d1->D0(aT, aP2d1);
182     aS1->D0(aP2d1.X(), aP2d1.Y(), aP1);
183
184     aC2d2->D0(aT, aP2d2);
185     aS2->D0(aP2d2.X(), aP2d2.Y(), aP2);
186
187     aD = aP1.Distance(aP2);
188
189     if (aD > aMaxDist)
190     {
191       aMaxParam = aT;
192       aMaxDist = aD;
193     }
194
195     printf(" T=%lg\tD=%lg\n", aT, aD);
196
197     aMr=new Draw_Marker3D(aP1, Draw_Plus, aColor, iSize);
198     dout << aMr;
199   }
200
201   cout << "Max distance = "  << aMaxDist << endl;
202   cout << "Param = " << aMaxParam << endl;
203
204   return 0;
205 }
206
207 //=======================================================================
208 //function : xdistcc2ds
209 //purpose  : 
210 //=======================================================================
211 static Standard_Integer xdistcc2ds(Draw_Interpretor& , Standard_Integer n, const char** a)
212 {
213   if (n < 6)
214   {
215     cout<<" Use xdistcc2ds c c2d s t1 t2 nbp"<<endl;
216     return 0;
217   }
218
219   Standard_Integer i, aNbP, iSize;
220   Standard_Real aD, aT, aT1, aT2, dT;
221   gp_Pnt aP, aPOnS;
222   gp_Pnt2d aP2d;
223   Handle(Geom_Curve) aC;
224   Handle(Geom2d_Curve) aC2d;
225   Handle(Geom_Surface) aS;
226   Handle(Draw_Marker3D) aMr;
227   Draw_Color aColor(Draw_rouge);
228
229   aC=DrawTrSurf::GetCurve(a[1]);
230   if (aC.IsNull())
231   {
232     cout<<a[1]<<" is null curve"<<endl;
233     return 0;
234   }
235
236   aC2d=DrawTrSurf::GetCurve2d(a[2]);
237   if (aC2d.IsNull())
238   {
239     cout<<a[2]<<" is null 2dcurve"<<endl;
240     return 0;
241   }
242
243   aS=DrawTrSurf::GetSurface(a[3]);
244   if (aS.IsNull())
245   {
246     cout<<a[3]<<" is null surface"<<endl;
247     return 0;
248   }
249
250   aT1=Draw::Atof(a[4]);
251   aT2=Draw::Atof(a[5]);
252
253   aNbP=10;
254   if (n>5)
255   {
256     aNbP=Draw::Atoi(a[6]);
257   }
258
259   iSize=3;
260
261   Standard_Real aMaxParam = 0.0;
262   Standard_Real aMaxDist  = 0.0;
263
264   dT=(aT2 - aT1) / (aNbP - 1);
265   for(i = 0; i < aNbP; ++i)
266   {
267     aT=aT1 + i * dT;
268     if (i == aNbP-1) 
269       aT=aT2;
270
271     aC->D0(aT, aP);
272
273     aC2d->D0(aT, aP2d);
274     aS->D0(aP2d.X(), aP2d.Y(), aPOnS);
275
276     aD = aP.Distance(aPOnS);
277
278     if (aD > aMaxDist)
279     {
280       aMaxParam = aT;
281       aMaxDist = aD;
282     }
283
284     printf(" T=%lg\tD=%lg\n", aT, aD);
285
286     aMr=new Draw_Marker3D(aP, Draw_Plus, aColor, iSize);
287     dout << aMr;
288   }
289
290   cout << "Max distance = "  << aMaxDist << endl;
291   cout << "Param = " << aMaxParam << endl;
292
293   return 0;
294 }
295
296 //=======================================================================
297 //function : xdistcs
298 //purpose  : 
299 //=======================================================================
300 static Standard_Integer xdistcs(Draw_Interpretor& di, Standard_Integer n, const char** a)
301 {
302   if (n < 6) {
303     cout << "Use: xdistcs curve surface t1 t2 nbpoints [tol [warn_tol]]" << endl;
304     cout << "Measures distances from curve to surface by nbpoints probing points on a curve" << endl;
305     cout << "Error will be reported for points where distance is greater than tol" << endl;
306     cout << "Warning will be reported for points where distance is greater than warn_tol" << endl;
307     return 0;
308   }
309   //
310   Standard_Boolean bRet;
311   Standard_Integer i, aNbP, iSize;
312   Standard_Real aTol, aD, aT, aT1, aT2, dT;
313   gp_Pnt aP;
314   Handle(Geom_Curve) aC;
315   Handle(Geom_Surface) aS;
316   GeomAPI_ProjectPointOnSurf aPPS;
317   Handle(Draw_Marker3D) aMr;
318   Draw_Color aColor(Draw_rouge);
319   //
320   aTol=1.e-7;
321   //
322   aC=DrawTrSurf::GetCurve(a[1]);
323   if (aC.IsNull()) {
324     di << "Error: " << a[1] << " is not a curve!\n";
325     return 0;
326   }
327   //
328   aS=DrawTrSurf::GetSurface(a[2]);
329   if (aS.IsNull()) {
330     di << "Error: " << a[2] << " is not a surface!\n";
331     return 0;
332   }
333   //
334   aT1=Draw::Atof(a[3]);
335   aT2=Draw::Atof(a[4]);
336   //
337   aNbP=10;
338   if (n>5) {
339     aNbP=Draw::Atoi(a[5]);
340   }
341   Standard_Real anErrTol = (n > 6 ? Draw::Atof(a[6]) : RealLast());
342   Standard_Real aWarnTol = (n > 7 ? Draw::Atof(a[7]) : RealLast());
343   //
344   iSize=3;
345   //
346   dT=(aT2-aT1)/(aNbP-1);
347
348   Standard_Real aMaxParam = 0.0;
349   Standard_Real aMaxDist  = 0.0;
350   for (i=0; i<aNbP; ++i) {
351     aT=aT1+i*dT;
352     if (i==aNbP-1) {
353       aT=aT2;
354     }
355     //
356     aC->D0(aT, aP);
357     aPPS.Init(aP, aS, aTol);
358     bRet=aPPS.IsDone();
359     if (!bRet) {
360       di << "Error: GeomAPI_ProjectPointOnSurf failed\n";
361       return 0;
362     }
363     //
364     aD=aPPS.LowerDistance();
365     // report error or warning if distance is greater than tolerance
366     if (aD > anErrTol)
367     {
368       di << "Error in " << a[1] << ":";
369     }
370     else if (aD > aWarnTol)
371     {
372       di << "Attention (critical value of tolerance) :";
373     }
374     char aMsg[256];
375     sprintf(aMsg," T=%lg\tD=%lg\n", aT, aD);
376     di << aMsg;
377     //
378     aMr=new Draw_Marker3D(aP, Draw_Plus, aColor, iSize);
379     dout << aMr;
380
381     if (aD > aMaxDist)
382     {
383       aMaxParam = aT;
384       aMaxDist = aD;
385     }
386   }
387
388   di << "Max distance = "  << aMaxDist << "\n" ;
389   di << "Param = " << aMaxParam << "\n" ;
390   //
391   return 0;
392 }
393
394 //=======================================================================
395 //function : TestProjCommands
396 //purpose  : 
397 //=======================================================================
398
399 void  GeometryTest::TestProjCommands(Draw_Interpretor& theCommands)
400 {
401   
402   static Standard_Boolean loaded = Standard_False;
403   if (loaded) return;
404   loaded = Standard_True;
405   
406   DrawTrSurf::BasicCommands(theCommands);
407   
408   const char* g;
409   
410   g = "Testing of projection (geometric objects)";
411   
412   theCommands.Add("xdistcs", "xdistcs curve surface t1 t2 nbpoints [tol [warn_tol]]", __FILE__, xdistcs, g);
413   theCommands.Add("xdistcc2ds", "xdistcc2ds c c2d s t1 t2 nbp", __FILE__, xdistcc2ds, g);
414   theCommands.Add("xdistc2dc2dss", "xdistc2dc2dss c2d_1 c2d_2 s1 s2 t1 t2 nbp", __FILE__, xdistc2dc2dss, g);
415   theCommands.Add("xdistcc", "xdistcc c1 c2 t1 t2 nbp", __FILE__, xdistcc, g);
416 }