1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <BRepGProp.hxx>
16 #include <BRepGProp_Cinert.hxx>
17 #include <BRepGProp_Sinert.hxx>
18 #include <BRepGProp_Vinert.hxx>
19 #include <BRepGProp_VinertGK.hxx>
20 #include <BRepGProp_Face.hxx>
21 #include <BRepGProp_Domain.hxx>
23 #include <BRepAdaptor_Curve.hxx>
25 #include <TopTools.hxx>
26 #include <BRep_Tool.hxx>
27 #include <TopTools_ListOfShape.hxx>
28 #include <BRepCheck_Shell.hxx>
29 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 static Standard_Integer AffichEps = 0;
34 static gp_Pnt roughBaryCenter(const TopoDS_Shape& S){
35 Standard_Integer i; TopExp_Explorer ex;
37 for (ex.Init(S,TopAbs_VERTEX), i = 0; ex.More(); ex.Next(), i++)
38 xyz += BRep_Tool::Pnt(TopoDS::Vertex(ex.Current())).XYZ();
39 if ( i > 0 ) xyz /= i;
43 void BRepGProp::LinearProperties(const TopoDS_Shape& S, GProp_GProps& SProps){
46 P.Transform(S.Location());
47 SProps = GProp_GProps(P);
49 BRepAdaptor_Curve BAC;
50 // Standard_Integer n,i;
52 for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
53 BAC.Initialize(TopoDS::Edge(ex.Current()));
54 BRepGProp_Cinert CG(BAC,P);
59 static Standard_Real surfaceProperties(const TopoDS_Shape& S, GProp_GProps& Props, const Standard_Real Eps){
62 Standard_Integer iErrorMax = 0;
64 Standard_Real ErrorMax = 0.0, Error;
66 gp_Pnt P(roughBaryCenter(S));
67 BRepGProp_Sinert G; G.SetLocation(P);
72 for (ex.Init(S,TopAbs_FACE), i = 1; ex.More(); ex.Next(), i++) {
73 const TopoDS_Face& F = TopoDS::Face(ex.Current());
75 if(!BF.NaturalRestriction()) BD.Init(F);
77 G.Perform(BF, BD, Eps);
78 Error = G.GetEpsilon();
79 if(ErrorMax < Error) {
86 if(BF.NaturalRestriction()) G.Perform(BF);
87 else G.Perform(BF, BD);
91 if(AffichEps) cout<<"\n"<<i<<":\tEpsArea = "<< G.GetEpsilon();
95 if(AffichEps) cout<<"\n-----------------\n"<<iErrorMax<<":\tMaxError = "<<ErrorMax<<"\n";
99 void BRepGProp::SurfaceProperties(const TopoDS_Shape& S, GProp_GProps& Props){
102 P.Transform(S.Location());
103 Props = GProp_GProps(P);
104 surfaceProperties(S,Props,1.0);
106 Standard_Real BRepGProp::SurfaceProperties(const TopoDS_Shape& S, GProp_GProps& Props, const Standard_Real Eps){
108 gp_Pnt P(0,0,0); P.Transform(S.Location());
109 Props = GProp_GProps(P);
110 Standard_Real ErrorMax = surfaceProperties(S,Props,Eps);
114 //=======================================================================
115 //function : volumeProperties
117 //=======================================================================
119 static Standard_Real volumeProperties(const TopoDS_Shape& S, GProp_GProps& Props, const Standard_Real Eps){
122 Standard_Integer iErrorMax = 0;
124 Standard_Real ErrorMax = 0.0, Error = 0.0;
126 gp_Pnt P(roughBaryCenter(S));
127 BRepGProp_Vinert G; G.SetLocation(P);
132 for (ex.Init(S,TopAbs_FACE), i = 1; ex.More(); ex.Next(), i++) {
133 const TopoDS_Face& F = TopoDS::Face(ex.Current());
134 if ((F.Orientation() == TopAbs_FORWARD) || (F.Orientation() == TopAbs_REVERSED)){
136 if(!BF.NaturalRestriction()) BD.Init(F);
138 G.Perform(BF, BD, Eps);
139 Error = G.GetEpsilon();
140 if(ErrorMax < Error) {
148 if(BF.NaturalRestriction()) G.Perform(BF);
149 else G.Perform(BF, BD);
153 if(AffichEps) cout<<"\n"<<i<<":\tEpsVolume = "<< G.GetEpsilon();
158 if(AffichEps) cout<<"\n-----------------\n"<<iErrorMax<<":\tMaxError = "<<ErrorMax<<"\n";
162 void BRepGProp::VolumeProperties(const TopoDS_Shape& S, GProp_GProps& Props, const Standard_Boolean OnlyClosed){
164 gp_Pnt P(0,0,0); P.Transform(S.Location());
165 Props = GProp_GProps(P);
167 TopExp_Explorer ex(S,TopAbs_SHELL);
168 for (; ex.More(); ex.Next()) {
169 const TopoDS_Shape& Sh = ex.Current();
170 if(BRep_Tool::IsClosed(Sh)) volumeProperties(Sh,Props,1.0);
172 } else volumeProperties(S,Props,1.0);
175 //=======================================================================
176 //function : VolumeProperties
178 //=======================================================================
180 Standard_Real BRepGProp::VolumeProperties(const TopoDS_Shape& S, GProp_GProps& Props,
181 const Standard_Real Eps, const Standard_Boolean OnlyClosed)
184 gp_Pnt P(0,0,0); P.Transform(S.Location());
185 Props = GProp_GProps(P);
188 Standard_Integer iErrorMax = 0;
190 Standard_Real ErrorMax = 0.0, Error = 0.0;
192 TopExp_Explorer ex(S,TopAbs_SHELL);
193 for (i = 1; ex.More(); ex.Next(), i++) {
194 const TopoDS_Shape& Sh = ex.Current();
195 if(BRep_Tool::IsClosed(Sh)) {
196 Error = volumeProperties(Sh,Props,Eps);
197 if(ErrorMax < Error) {
205 } else ErrorMax = volumeProperties(S,Props,Eps);
207 if(AffichEps) cout<<"\n\n==================="<<iErrorMax<<":\tMaxEpsVolume = "<<ErrorMax<<"\n";
212 //===========================================================================================//
213 // Volume properties by Gauss-Kronrod integration
214 //===========================================================================================//
215 //=======================================================================
216 //function : VolumePropertiesGK
218 //=======================================================================
220 static Standard_Real volumePropertiesGK(const TopoDS_Shape &theShape,
221 GProp_GProps &theProps,
222 const Standard_Real theTol,
223 const Standard_Boolean IsUseSpan,
224 const Standard_Boolean CGFlag,
225 const Standard_Boolean IFlag)
227 TopExp_Explorer anExp;
228 anExp.Init(theShape, TopAbs_FACE);
230 Standard_Real aTol = theTol;
232 // Compute properties.
233 gp_Pnt aLoc(roughBaryCenter(theShape));
234 BRepGProp_VinertGK aVProps;
235 BRepGProp_Face aPropFace(IsUseSpan);
236 BRepGProp_Domain aPropDomain;
237 Standard_Real aLocalError;
238 Standard_Real anError = 0.;
240 aVProps.SetLocation(aLoc);
242 for (; anExp.More(); anExp.Next()) {
243 TopoDS_Face aFace = TopoDS::Face(anExp.Current());
245 if (aFace.Orientation() == TopAbs_FORWARD ||
246 aFace.Orientation() == TopAbs_REVERSED) {
247 aPropFace.Load(aFace);
249 if(aPropFace.NaturalRestriction())
250 aLocalError = aVProps.Perform(aPropFace, aTol, CGFlag, IFlag);
252 aPropDomain.Init(aFace);
253 aLocalError = aVProps.Perform(aPropFace, aPropDomain, aTol, CGFlag, IFlag);
256 if (aLocalError < 0.)
259 anError += aLocalError;
260 theProps.Add(aVProps);
267 //=======================================================================
268 //function : VolumePropertiesGK
270 //=======================================================================
272 Standard_Real BRepGProp::VolumePropertiesGK(const TopoDS_Shape &S,
274 const Standard_Real Eps,
275 const Standard_Boolean OnlyClosed,
276 const Standard_Boolean IsUseSpan,
277 const Standard_Boolean CGFlag,
278 const Standard_Boolean IFlag)
281 Standard_Real anError = 0.;
283 P.Transform(S.Location());
284 Props = GProp_GProps(P);
287 // To select closed shells.
288 TopExp_Explorer anExp;
289 TopTools_ListOfShape aClosedShells;
291 anExp.Init(S, TopAbs_SHELL);
293 for (; anExp.More(); anExp.Next()) {
294 const TopoDS_Shape &aShell = anExp.Current();
296 BRepCheck_Shell aChecker(TopoDS::Shell(aShell));
297 BRepCheck_Status aStatus = aChecker.Closed(Standard_False);
299 if(aStatus == BRepCheck_NoError)
300 aClosedShells.Append(aShell);
304 if (aClosedShells.IsEmpty())
307 // Compute the properties for each closed shell.
308 Standard_Real aTol = Eps;
309 Standard_Real aLocalError;
310 TopTools_ListIteratorOfListOfShape anIter(aClosedShells);
312 for (; anIter.More(); anIter.Next()) {
313 const TopoDS_Shape &aShell = anIter.Value();
315 aLocalError = volumePropertiesGK(aShell, Props, aTol, IsUseSpan, CGFlag, IFlag);
320 anError += aLocalError;
324 anError = volumePropertiesGK(S, Props, Eps, IsUseSpan, CGFlag, IFlag);
326 Standard_Real vol = Props.Mass();
327 if(vol > Epsilon(1.)) anError /= vol;
331 //=======================================================================
332 //function : VolumeProperties
334 //=======================================================================
336 static Standard_Real volumePropertiesGK(const TopoDS_Shape &theShape,
337 GProp_GProps &theProps,
338 const gp_Pln &thePln,
339 const Standard_Real theTol,
340 const Standard_Boolean IsUseSpan,
341 const Standard_Boolean CGFlag,
342 const Standard_Boolean IFlag)
344 TopExp_Explorer anExp;
345 anExp.Init(theShape, TopAbs_FACE);
347 Standard_Real aTol = theTol;
349 // Compute properties.
350 gp_Pnt aLoc(roughBaryCenter(theShape));
351 BRepGProp_VinertGK aVProps;
352 BRepGProp_Face aPropFace(IsUseSpan);
353 BRepGProp_Domain aPropDomain;
354 Standard_Real aLocalError;
355 Standard_Real anError = 0.;
357 aVProps.SetLocation(aLoc);
359 for (; anExp.More(); anExp.Next()) {
360 TopoDS_Face aFace = TopoDS::Face(anExp.Current());
362 if (aFace.Orientation() == TopAbs_FORWARD ||
363 aFace.Orientation() == TopAbs_REVERSED) {
364 aPropFace.Load(aFace);
366 if(aPropFace.NaturalRestriction())
367 aLocalError = aVProps.Perform(aPropFace, thePln, aTol, CGFlag, IFlag);
369 aPropDomain.Init(aFace);
370 aLocalError = aVProps.Perform(aPropFace, aPropDomain, thePln, aTol, CGFlag, IFlag);
373 if (aLocalError < 0.)
376 anError += aLocalError;
377 theProps.Add(aVProps);
384 //=======================================================================
385 //function : VolumeProperties
387 //=======================================================================
389 Standard_Real BRepGProp::VolumePropertiesGK(const TopoDS_Shape &S,
391 const gp_Pln &thePln,
392 const Standard_Real Eps,
393 const Standard_Boolean OnlyClosed,
394 const Standard_Boolean IsUseSpan,
395 const Standard_Boolean CGFlag,
396 const Standard_Boolean IFlag)
399 Standard_Real anError = 0.;
401 P.Transform(S.Location());
402 Props = GProp_GProps(P);
405 // To select closed shells.
406 TopExp_Explorer anExp;
407 TopTools_ListOfShape aClosedShells;
409 anExp.Init(S, TopAbs_SHELL);
411 for (; anExp.More(); anExp.Next()) {
412 const TopoDS_Shape &aShell = anExp.Current();
414 BRepCheck_Shell aChecker(TopoDS::Shell(aShell));
415 BRepCheck_Status aStatus = aChecker.Closed(Standard_False);
417 if(aStatus == BRepCheck_NoError)
418 aClosedShells.Append(aShell);
422 if (aClosedShells.IsEmpty())
425 // Compute the properties for each closed shell.
426 Standard_Real aTol = Eps;
427 Standard_Real aLocalError;
428 TopTools_ListIteratorOfListOfShape anIter(aClosedShells);
430 for (; anIter.More(); anIter.Next()) {
431 const TopoDS_Shape &aShell = anIter.Value();
433 aLocalError = volumePropertiesGK(aShell, Props, thePln, aTol, IsUseSpan, CGFlag, IFlag);
438 anError += aLocalError;
441 anError = volumePropertiesGK(S, Props, thePln, Eps, IsUseSpan, CGFlag, IFlag);
443 Standard_Real vol = Props.Mass();
444 if(vol > Epsilon(1.)) anError /= vol;