0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / BRepTest / BRepTest_GPropCommands.cxx
1 // Created on: 1994-02-18
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-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 <Standard_Stream.hxx>
18 #include <BRepTest.hxx>
19 #include <Draw_Interpretor.hxx>
20 #include <Draw_Appli.hxx>
21 #include <DBRep.hxx>
22 #include <BRepGProp.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <GProp_PrincipalProps.hxx>
25 #include <gp_Ax2.hxx>
26 #include <gp_Ax3.hxx>
27
28 #include <Draw_Axis3D.hxx>
29 #include <Precision.hxx>
30 #include <OSD_Chronometer.hxx>
31 #include <Geom_Surface.hxx>
32 #include <DrawTrSurf.hxx>
33 #include <Geom_Plane.hxx>
34 #include <gp_Pln.hxx>
35
36 #ifdef _WIN32
37 Standard_IMPORT Draw_Viewer dout;
38 #endif
39
40
41 Standard_Integer props(Draw_Interpretor& di, Standard_Integer n, const char** a)
42 {
43   if (n < 2) {
44     di << "Use: " << a[0] << " shape [epsilon] [c[losed]] [x y z] [-skip] [-full] [-tri]\n";
45     di << "Compute properties of the shape, exact geometry (curves, surfaces) or\n";
46     di << "some discrete data (polygons, triangulations) can be used for calculations\n";
47     di << "The epsilon, if given, defines relative precision of computation\n";
48     di << "The \"closed\" flag, if present, do computation only closed shells of the shape\n";
49     di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n";
50     di << "Shared entities will be take in account only one time in the skip mode\n";
51     di << "All values are outputted with the full precision in the full mode.\n";
52     di << "Preferable source of geometry data are triangulations in case if it exists, if the -tri key is used.\n";
53     di << "If epsilon is given, exact geometry (curves, surfaces) are used for calculations independently of using key -tri\n\n";
54     return 1;
55   }
56
57   Standard_Boolean UseTriangulation = Standard_False;
58   if (n >= 2 && strcmp(a[n - 1], "-tri") == 0)
59   {
60     UseTriangulation = Standard_True;
61     --n;
62   }
63   Standard_Boolean isFullMode = Standard_False;
64   if (n >= 2 && strcmp(a[n-1], "-full") == 0)
65   {
66     isFullMode = Standard_True;
67     --n;
68   }
69   Standard_Boolean SkipShared = Standard_False;
70   if (n >= 2 && strcmp(a[n-1], "-skip") == 0)
71   {
72     SkipShared = Standard_True;
73     --n;
74   }
75
76   TopoDS_Shape S = DBRep::Get(a[1]);
77   if (S.IsNull()) return 0;
78
79   GProp_GProps G;
80
81   Standard_Boolean onlyClosed = Standard_False;
82   Standard_Real eps = 1.0;
83   Standard_Boolean witheps = Standard_False;
84   if((n > 2 && *a[2]=='c') || (n > 3 && *a[3]=='c')) onlyClosed = Standard_True;
85   if(n > 2 && *a[2]!='c' && n != 5) {eps = Draw::Atof (a[2]); witheps = Standard_True;}
86
87   if (witheps){
88     if (Abs(eps) < Precision::Angular()) return 2;
89     if (*a[0] == 'l')
90       BRepGProp::LinearProperties(S,G,SkipShared);
91     else if (*a[0] == 's')
92       eps = BRepGProp::SurfaceProperties(S,G,eps,SkipShared);
93     else 
94       eps = BRepGProp::VolumeProperties(S,G,eps,onlyClosed,SkipShared);
95   }
96   else {
97     if (*a[0] == 'l')
98       BRepGProp::LinearProperties(S, G, SkipShared, UseTriangulation);
99     else if (*a[0] == 's')
100       BRepGProp::SurfaceProperties(S, G, SkipShared, UseTriangulation);
101     else 
102       BRepGProp::VolumeProperties(S,G,onlyClosed,SkipShared, UseTriangulation);
103   }
104   
105   gp_Pnt P = G.CentreOfMass();
106   gp_Mat I = G.MatrixOfInertia();
107
108   if (n >= 5) {
109     Standard_Integer shift =  n - 5;
110     Draw::Set(a[shift+2],P.X());
111     Draw::Set(a[shift+3],P.Y());
112     Draw::Set(a[shift+4],P.Z());
113   }
114
115   GProp_PrincipalProps Pr = G.PrincipalProperties();
116   Standard_Real Ix,Iy,Iz;
117   Pr.Moments(Ix,Iy,Iz);
118
119   if (!isFullMode)
120   {
121     Standard_SStream aSStream1;
122     aSStream1 << "\n\n";
123     aSStream1 << "Mass : " << std::setw(15) << G.Mass() << "\n\n";
124     if(witheps && *a[0] != 'l') aSStream1 << "Relative error of mass computation : " <<  std::setw(15) << eps <<  "\n\n";
125   
126     aSStream1 << "Center of gravity : \n";
127     aSStream1 << "X = " << std::setw(15) << P.X() << "\n";
128     aSStream1 << "Y = " << std::setw(15) << P.Y() << "\n";
129     aSStream1 << "Z = " << std::setw(15) << P.Z() << "\n";
130     aSStream1 << "\n";
131   
132     aSStream1 << "Matrix of Inertia : \n";
133     aSStream1 << std::setw(15) << I(1,1);
134     aSStream1 << " " << std::setw(15) << I(1,2);
135     aSStream1 << " " << std::setw(15) << I(1,3) << "\n";
136     aSStream1 << std::setw(15) << I(2,1);
137     aSStream1 << " " << std::setw(15) << I(2,2);
138     aSStream1 << " " << std::setw(15) << I(2,3) << "\n";
139     aSStream1 << std::setw(15) << I(3,1);
140     aSStream1 << " " << std::setw(15) << I(3,2);
141     aSStream1 << " " << std::setw(15) << I(3,3) << "\n";
142     aSStream1 << "\n";
143     aSStream1 << std::ends;
144     di << aSStream1;
145
146     Standard_SStream aSStream2;
147     aSStream2 << "Moments : \n";
148     aSStream2 << "IX = " << std::setw(15) << Ix << "\n";
149     aSStream2 << "IY = " << std::setw(15) << Iy << "\n";
150     aSStream2 << "IZ = " << std::setw(15) << Iz << "\n";
151     aSStream2 << "\n";
152     aSStream2 << std::ends;
153     di << aSStream2;
154   }
155   else
156   {
157     di << "\n\nMass : " << G.Mass() << "\n\n";
158     if (witheps && *a[0] != 'l')
159     {
160       di << "Relative error of mass computation : " << eps <<  "\n\n";
161     }
162
163     di << "Center of gravity : \n";
164     di << "X = " << P.X() << "\n";
165     di << "Y = " << P.Y() << "\n";
166     di << "Z = " << P.Z() << "\n\n";
167
168     di << "Matrix of Inertia :\n";
169     di << I(1,1) << "    " << I(1,2) << "    " << I(1,3) << "\n";
170     di << I(2,1) << "    " << I(2,2) << "    " << I(2,3) << "\n";
171     di << I(3,1) << "    " << I(3,2) << "    " << I(3,3) << "\n\n";
172
173     di << "Moments :\n";
174     di << "IX = " << Ix << "\n";
175     di << "IY = " << Iy << "\n";
176     di << "IZ = " << Iz << "\n\n";
177   }
178
179   //if (n == 2) {  
180     gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
181     
182     Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
183     dout << Dax;
184   //}
185
186   return 0;
187 }
188
189
190 Standard_Integer vpropsgk(Draw_Interpretor& di, Standard_Integer n, const char** a)
191 {
192   if (n < 2) {
193     di << "Use: " << a[0] << " shape epsilon closed span mode [x y z] [-skip]\n";
194     di << "Compute properties of the shape\n";
195     di << "The epsilon defines relative precision of computation\n";
196     di << "The \"closed\" flag, if equal 1, causes computation only closed shells of the shape\n";
197     di << "The \"span\" flag, if equal 1, says that computation is performed on spans\n";
198     di << "      This option makes effect only for BSpline surfaces.\n";
199     di << "mode can be 0 - only volume calculations\n";
200     di << "            1 - volume and gravity center\n";
201     di << "            2 - volume, gravity center and matrix of inertia\n";
202     di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n\n";
203     return 1;
204   }
205
206   if ( n > 2 && n < 6) {
207     di << "Wrong arguments\n";
208     return 1;
209   }
210
211   TopoDS_Shape S = DBRep::Get(a[1]);
212   if (S.IsNull()) return 0;
213
214   GProp_GProps G;
215   Standard_Boolean SkipShared = Standard_False;
216   if (n >= 2 && strcmp(a[n-1], "-skip") == 0)
217   {
218     SkipShared = Standard_True;
219     --n;
220   }
221
222   Standard_Boolean onlyClosed  = Standard_False;
223   Standard_Boolean isUseSpan   = Standard_False;
224   Standard_Boolean CGFlag = Standard_False;
225   Standard_Boolean IFlag = Standard_False;
226   Standard_Real    eps         = 1.e-3;
227 //Standard_Real    aDefaultTol = 1.e-3;
228   Standard_Integer mode = 0;
229
230   eps = Draw::Atof(a[2]);
231   mode = Draw::Atoi(a[3]);
232   if(mode > 0) onlyClosed = Standard_True;
233   mode = Draw::Atoi(a[4]);
234   if(mode > 0) isUseSpan = Standard_True;
235
236   mode = Draw::Atoi(a[5]);
237   if(mode == 1 || mode == 3) CGFlag = Standard_True;
238   if(mode == 2 || mode == 3) IFlag = Standard_True;
239
240   //OSD_Chronometer aChrono;
241
242   //aChrono.Reset();
243   //aChrono.Start();
244   eps = BRepGProp::VolumePropertiesGK(S, G, eps, onlyClosed, isUseSpan, CGFlag, IFlag, SkipShared);
245   //aChrono.Stop();
246
247   Standard_SStream aSStream0;
248   Standard_Integer anOutWidth = 24;
249
250   aSStream0.precision(15);
251   aSStream0 << "\n\n";
252   aSStream0 << "Mass : " << std::setw(anOutWidth) << G.Mass() << "\n\n";
253   aSStream0 << "Relative error of mass computation : " <<  std::setw(anOutWidth) << eps <<  "\n\n";
254   aSStream0 << std::ends;
255   di << aSStream0;
256
257   if(CGFlag || IFlag) {
258     Standard_SStream aSStream1;
259     gp_Pnt P = G.CentreOfMass();
260     if (n > 6) {
261       Draw::Set(a[6],P.X());
262     }
263     if (n > 7) {
264       Draw::Set(a[7],P.Y());
265     }
266     if (n > 8) {
267       Draw::Set(a[8],P.Z());
268     }
269   
270     aSStream1.precision(15);
271     aSStream1 << "Center of gravity : \n";
272     aSStream1 << "X = " << std::setw(anOutWidth) << P.X() << "\n";
273     aSStream1 << "Y = " << std::setw(anOutWidth) << P.Y() << "\n";
274     aSStream1 << "Z = " << std::setw(anOutWidth) << P.Z() << "\n";
275     aSStream1 << "\n";
276  
277     if(IFlag) {
278       gp_Mat I = G.MatrixOfInertia();
279
280       aSStream1 << "Matrix of Inertia : \n";
281       aSStream1 << std::setw(anOutWidth) << I(1,1);
282       aSStream1 << " " << std::setw(anOutWidth) << I(1,2);
283       aSStream1 << " " << std::setw(anOutWidth) << I(1,3) << "\n";
284       aSStream1 << std::setw(anOutWidth) << I(2,1);
285       aSStream1 << " " << std::setw(anOutWidth) << I(2,2);
286       aSStream1 << " " << std::setw(anOutWidth) << I(2,3) << "\n";
287       aSStream1 << std::setw(anOutWidth) << I(3,1);
288       aSStream1 << " " << std::setw(anOutWidth) << I(3,2);
289       aSStream1 << " " << std::setw(anOutWidth) << I(3,3) << "\n";
290       aSStream1 << "\n";
291     }
292     aSStream1 << std::ends;
293     di << aSStream1;
294   }
295
296   if(IFlag) {
297
298     GProp_PrincipalProps Pr = G.PrincipalProperties();
299
300     Standard_Real Ix,Iy,Iz;
301     Pr.Moments(Ix,Iy,Iz);
302     gp_Pnt P = G.CentreOfMass();
303   
304     Standard_SStream aSStream2;
305
306     aSStream2.precision(15);
307     aSStream2 << "Moments : \n";
308     aSStream2 << "IX = " << std::setw(anOutWidth) << Ix << "\n";
309     aSStream2 << "IY = " << std::setw(anOutWidth) << Iy << "\n";
310     aSStream2 << "IZ = " << std::setw(anOutWidth) << Iz << "\n";
311     aSStream2 << "\n";
312     aSStream2 << "\n";
313     aSStream2 << std::ends;
314     di << aSStream2;
315
316     gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
317     
318     Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
319     dout << Dax;
320   }
321   return 0;
322 }
323
324
325
326 //=======================================================================
327 //function : GPropCommands
328 //purpose  : 
329 //=======================================================================
330
331 void  BRepTest::GPropCommands(Draw_Interpretor& theCommands)
332 {
333   static Standard_Boolean done = Standard_False;
334   if (done) return;
335   done = Standard_True;
336
337   DBRep::BasicCommands(theCommands);
338
339   const char* g = "Global properties";
340   theCommands.Add("lprops",
341     "lprops name [x y z] [-skip] [-full] [-tri]: compute linear properties",
342     __FILE__, props, g);
343   theCommands.Add("sprops", "sprops name [epsilon] [x y z] [-skip] [-full] [-tri]:\n"
344 "  compute surfacic properties", __FILE__, props, g);
345   theCommands.Add("vprops", "vprops name [epsilon] [c[losed]] [x y z] [-skip] [-full] [-tri]:\n"
346 "  compute volumic properties", __FILE__, props, g);
347
348   theCommands.Add("vpropsgk",
349                   "vpropsgk name epsilon closed span mode [x y z] [-skip] : compute volumic properties",
350                   __FILE__,
351                   vpropsgk,
352                   g);
353 }