Salome HOME
5657a171f3a4e304307eb937e9493bb0a12b883d
[modules/shaper.git] / src / Selector / Selector_NExplode.cpp
1 // Copyright (C) 2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Selector_NExplode.h"
22
23 #include <TopoDS_Shape.hxx>
24 #include <BRep_Tool.hxx>
25 #include <TopoDS.hxx>
26 #include <GProp_GProps.hxx>
27 #include <BRepGProp.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <Precision.hxx>
30 #include <Bnd_Box.hxx>
31 #include <BRepBndLib.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopTools_MapOfShape.hxx>
34
35 #include <vector>
36 #include <algorithm>
37
38 static std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S)
39 {
40   // Computing of CentreOfMass
41   gp_Pnt GPoint;
42   double Len;
43
44   if (S.ShapeType() == TopAbs_VERTEX) {
45     GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
46     Len = (double)S.Orientation();
47   }
48   else {
49     GProp_GProps GPr;
50     if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
51       BRepGProp::LinearProperties(S, GPr);
52     }
53     else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
54       BRepGProp::SurfaceProperties(S, GPr);
55     }
56     else {
57       BRepGProp::VolumeProperties(S, GPr);
58     }
59     GPoint = GPr.CentreOfMass();
60     Len = GPr.Mass();
61   }
62
63   double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
64   return std::make_pair(dMidXYZ, Len);
65 }
66
67 /*!
68 * \brief Sort shapes in the list by their coordinates.
69 */
70 struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
71 {
72   typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > DataMapOfShapeDouble;
73
74   CompareShapes(DataMapOfShapeDouble* theCashMap) : myMap(theCashMap) {}
75
76   bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
77
78   DataMapOfShapeDouble* myMap;
79 };
80
81 bool CompareShapes::operator() (const TopoDS_Shape& theShape1,
82   const TopoDS_Shape& theShape2)
83 {
84   if (!myMap->IsBound(theShape1)) {
85     myMap->Bind(theShape1, ShapeToDouble(theShape1));
86   }
87
88   if (!myMap->IsBound(theShape2)) {
89     myMap->Bind(theShape2, ShapeToDouble(theShape2));
90   }
91
92   std::pair<double, double> val1 = myMap->Find(theShape1);
93   std::pair<double, double> val2 = myMap->Find(theShape2);
94
95   double tol = Precision::Confusion();
96   bool exchange = Standard_False;
97
98   double dMidXYZ = val1.first - val2.first;
99   if (dMidXYZ >= tol) {
100     exchange = Standard_True;
101   }
102   else if (Abs(dMidXYZ) < tol) {
103     double dLength = val1.second - val2.second;
104     if (dLength >= tol) {
105       exchange = Standard_True;
106     }
107     else if (Abs(dLength) < tol && theShape1.ShapeType() <= TopAbs_FACE) {
108       // equal values possible on shapes such as two halves of a sphere and
109       // a membrane inside the sphere
110 // LCOV_EXCL_START
111       // this part of code is taken from GEOM module, but can not reproduce in SHAPER
112       Bnd_Box box1,box2;
113       BRepBndLib::Add(theShape1, box1);
114       if (!box1.IsVoid()) {
115         BRepBndLib::Add(theShape2, box2);
116         Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
117         if (dSquareExtent >= tol) {
118           exchange = Standard_True;
119         }
120         else if (Abs(dSquareExtent) < tol) {
121           Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
122           box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
123           val1 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
124           box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
125           val2 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
126           if ((val1 - val2) >= tol) {
127             exchange = Standard_True;
128           }
129         }
130       }
131 // LCOV_EXCL_STOP
132     } else // compare addresses if shapes are geometrically equal
133       return theShape1.TShape().get() > theShape2.TShape().get();
134   }
135
136   //return val1 < val2;
137   return !exchange;
138 }
139
140 Selector_NExplode::Selector_NExplode(const TopoDS_ListOfShape& theShapes)
141 {
142   std::vector<TopoDS_Shape> aShapesVec;
143
144   for(TopoDS_ListOfShape::Iterator anIter(theShapes); anIter.More(); anIter.Next()) {
145       aShapesVec.push_back(anIter.Value());
146   }
147
148   CompareShapes::DataMapOfShapeDouble aCash;
149   CompareShapes shComp(&aCash);
150   std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
151
152   std::vector<TopoDS_Shape>::const_iterator anIter = aShapesVec.begin();
153   for (; anIter != aShapesVec.end(); ++anIter) {
154     mySorted.Append(*anIter);
155   }
156 }
157
158 Selector_NExplode::Selector_NExplode(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
159 {
160   std::vector<TopoDS_Shape> aShapesVec;
161   TopTools_MapOfShape anAdded; // to avoid same shapes duplication
162   for(TopExp_Explorer anExp(theShape, theType); anExp.More(); anExp.Next()) {
163     if (anAdded.Add(anExp.Current()))
164      aShapesVec.push_back(anExp.Current());
165   }
166
167   CompareShapes::DataMapOfShapeDouble aCash;
168   CompareShapes shComp(&aCash);
169   std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
170
171   std::vector<TopoDS_Shape>::const_iterator anIter = aShapesVec.begin();
172   for (; anIter != aShapesVec.end(); ++anIter) {
173     mySorted.Append(*anIter);
174   }
175 }
176
177
178 int Selector_NExplode::index(const TopoDS_Shape& theSubShape)
179 {
180   TopoDS_ListOfShape::Iterator anIter(mySorted);
181   for(int anIndex = 1; anIter.More(); anIter.Next(), anIndex++) {
182     if (anIter.Value().IsSame(theSubShape))
183       return anIndex;
184   }
185   return -1; // not found
186 }
187
188 TopoDS_Shape Selector_NExplode::shape(const int theIndex)
189 {
190   TopoDS_ListOfShape::Iterator anIter(mySorted);
191   for(int anIndex = 1; anIter.More(); anIter.Next(), anIndex++) {
192     if (anIndex == theIndex)
193       return anIter.Value();
194   }
195   return TopoDS_Shape(); // not found
196 }