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