Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_NExplode.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_NExplode.h"
21
22 #include <TopoDS_Shape.hxx>
23 #include <BRep_Tool.hxx>
24 #include <TopoDS.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>
33
34 #include <vector>
35 #include <algorithm>
36
37 static std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S)
38 {
39   // Computing of CentreOfMass
40   gp_Pnt GPoint;
41   double Len;
42
43   if (S.ShapeType() == TopAbs_VERTEX) {
44     GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
45     Len = (double)S.Orientation();
46   }
47   else {
48     GProp_GProps GPr;
49     if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
50       BRepGProp::LinearProperties(S, GPr);
51     }
52     else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
53       BRepGProp::SurfaceProperties(S, GPr);
54     }
55     else {
56       BRepGProp::VolumeProperties(S, GPr);
57     }
58     GPoint = GPr.CentreOfMass();
59     Len = GPr.Mass();
60   }
61
62   double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
63   return std::make_pair(dMidXYZ, Len);
64 }
65
66 /*!
67 * \brief Sort shapes in the list by their coordinates.
68 */
69 struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
70 {
71   typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > DataMapOfShapeDouble;
72
73   CompareShapes(DataMapOfShapeDouble* theCashMap) : myMap(theCashMap) {}
74
75   bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
76
77   DataMapOfShapeDouble* myMap;
78 };
79
80 bool CompareShapes::operator() (const TopoDS_Shape& theShape1,
81   const TopoDS_Shape& theShape2)
82 {
83   if (!myMap->IsBound(theShape1)) {
84     myMap->Bind(theShape1, ShapeToDouble(theShape1));
85   }
86
87   if (!myMap->IsBound(theShape2)) {
88     myMap->Bind(theShape2, ShapeToDouble(theShape2));
89   }
90
91   std::pair<double, double> val1 = myMap->Find(theShape1);
92   std::pair<double, double> val2 = myMap->Find(theShape2);
93
94   double tol = Precision::Confusion();
95   bool exchange = Standard_False;
96
97   double dMidXYZ = val1.first - val2.first;
98   if (dMidXYZ >= tol) {
99     exchange = Standard_True;
100   }
101   else if (Abs(dMidXYZ) < tol) {
102     double dLength = val1.second - val2.second;
103     if (dLength >= tol) {
104       exchange = Standard_True;
105     }
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
109       Bnd_Box box1,box2;
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;
116         }
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;
125           }
126           else // compare adresses if shapes are geometrically equal
127             exchange = theShape1.TShape().get() > theShape2.TShape().get();
128         }
129       }
130     } else // compare adresses if shapes are geometrically equal
131       exchange = theShape1.TShape().get() > theShape2.TShape().get();
132   }
133
134   //return val1 < val2;
135   return !exchange;
136 }
137
138 GeomAlgoAPI_NExplode::GeomAlgoAPI_NExplode(
139   const GeomShapePtr theContext, const GeomAPI_Shape::ShapeType theShapeType)
140 {
141   std::vector<TopoDS_Shape> aShapesVec;
142
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());
149   }
150
151   CompareShapes::DataMapOfShapeDouble aCash;
152   CompareShapes shComp(&aCash);
153   std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
154
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);
160   }
161 }
162
163 int GeomAlgoAPI_NExplode::index(const GeomShapePtr theSubShape)
164 {
165   ListOfShape::iterator anIter = mySorted.begin();
166   for(int anIndex = 1; anIter != mySorted.end(); anIter++, anIndex++) {
167     if ((*anIter)->isSame(theSubShape))
168       return anIndex;
169   }
170   return 0; // not found
171 }
172
173 GeomShapePtr GeomAlgoAPI_NExplode::shape(const int theIndex)
174 {
175   ListOfShape::iterator anIter = mySorted.begin();
176   for(int anIndex = 1; anIter != mySorted.end(); anIter++, anIndex++) {
177     if (anIndex == theIndex)
178       return *anIter;
179   }
180   return GeomShapePtr(); // not found
181 }