1 // Copyright (C) 2017-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_NExplode.h"
22 #include <TopoDS_Shape.hxx>
23 #include <BRep_Tool.hxx>
25 #include <GProp_GProps.hxx>
26 #include <BRepGProp.hxx>
27 #include <NCollection_DataMap.hxx>
28 #include <Precision.hxx>
29 #include <Bnd_Box.hxx>
30 #include <BRepBndLib.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopTools_MapOfShape.hxx>
37 static std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S)
39 // Computing of CentreOfMass
43 if (S.ShapeType() == TopAbs_VERTEX) {
44 GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
45 Len = (double)S.Orientation();
49 if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
50 BRepGProp::LinearProperties(S, GPr);
52 else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
53 BRepGProp::SurfaceProperties(S, GPr);
56 BRepGProp::VolumeProperties(S, GPr);
58 GPoint = GPr.CentreOfMass();
62 double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
63 return std::make_pair(dMidXYZ, Len);
67 * \brief Sort shapes in the list by their coordinates.
69 struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
71 typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > DataMapOfShapeDouble;
73 CompareShapes(DataMapOfShapeDouble* theCashMap) : myMap(theCashMap) {}
75 bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
77 DataMapOfShapeDouble* myMap;
80 bool CompareShapes::operator() (const TopoDS_Shape& theShape1,
81 const TopoDS_Shape& theShape2)
83 if (!myMap->IsBound(theShape1)) {
84 myMap->Bind(theShape1, ShapeToDouble(theShape1));
87 if (!myMap->IsBound(theShape2)) {
88 myMap->Bind(theShape2, ShapeToDouble(theShape2));
91 std::pair<double, double> val1 = myMap->Find(theShape1);
92 std::pair<double, double> val2 = myMap->Find(theShape2);
94 double tol = Precision::Confusion();
95 bool exchange = Standard_False;
97 double dMidXYZ = val1.first - val2.first;
99 exchange = Standard_True;
101 else if (Abs(dMidXYZ) < tol) {
102 double dLength = val1.second - val2.second;
103 if (dLength >= tol) {
104 exchange = Standard_True;
106 else if (Abs(dLength) < tol && theShape1.ShapeType() <= TopAbs_FACE) {
107 // equal values possible on shapes such as two halves of a sphere and
108 // a membrane inside the sphere
110 BRepBndLib::Add(theShape1, box1);
111 if (!box1.IsVoid()) {
112 BRepBndLib::Add(theShape2, box2);
113 Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
114 if (dSquareExtent >= tol) {
115 exchange = Standard_True;
117 else if (Abs(dSquareExtent) < tol) {
118 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
119 box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
120 val1 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
121 box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
122 val2 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
123 if ((val1 - val2) >= tol) {
124 exchange = Standard_True;
126 else // compare adresses if shapes are geometrically equal
127 exchange = theShape1.TShape().get() > theShape2.TShape().get();
130 } else // compare adresses if shapes are geometrically equal
131 exchange = theShape1.TShape().get() > theShape2.TShape().get();
134 //return val1 < val2;
138 GeomAlgoAPI_NExplode::GeomAlgoAPI_NExplode(
139 const GeomShapePtr theContext, const GeomAPI_Shape::ShapeType theShapeType)
141 std::vector<TopoDS_Shape> aShapesVec;
143 const TopoDS_Shape& aContext = theContext->impl<TopoDS_Shape>();
144 TopExp_Explorer anExp(aContext, (TopAbs_ShapeEnum)theShapeType);
145 TopTools_MapOfShape aMapShape;
146 for(; anExp.More(); anExp.Next()) {
147 if (aMapShape.Add(anExp.Current()))
148 aShapesVec.push_back(anExp.Current());
151 CompareShapes::DataMapOfShapeDouble aCash;
152 CompareShapes shComp(&aCash);
153 std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
155 std::vector<TopoDS_Shape>::const_iterator anIter = aShapesVec.begin();
156 for (; anIter != aShapesVec.end(); ++anIter) {
157 GeomShapePtr aShapePtr(new GeomAPI_Shape);
158 aShapePtr->setImpl<TopoDS_Shape>(new TopoDS_Shape(*anIter));
159 mySorted.push_back(aShapePtr);
163 int GeomAlgoAPI_NExplode::index(const GeomShapePtr theSubShape)
165 ListOfShape::iterator anIter = mySorted.begin();
166 for(int anIndex = 1; anIter != mySorted.end(); anIter++, anIndex++) {
167 if ((*anIter)->isSame(theSubShape))
170 return 0; // not found
173 GeomShapePtr GeomAlgoAPI_NExplode::shape(const int theIndex)
175 ListOfShape::iterator anIter = mySorted.begin();
176 for(int anIndex = 1; anIter != mySorted.end(); anIter++, anIndex++) {
177 if (anIndex == theIndex)
180 return GeomShapePtr(); // not found