1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: GEOMAlgo_GlueDetector.cxx
24 // Author: Peter KURNEV
27 #include <GEOMAlgo_GlueDetector.hxx>
29 #include <Bnd_Box.hxx>
30 #include <NCollection_UBTreeFiller.hxx>
32 #include <TColStd_ListOfInteger.hxx>
33 #include <TColStd_ListIteratorOfListOfInteger.hxx>
34 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Compound.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_MapIteratorOfMapOfShape.hxx>
52 #include <BRep_Tool.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRepBndLib.hxx>
56 #include <NMTDS_BndSphereTree.hxx>
57 #include <NMTDS_BndSphere.hxx>
58 #include <NMTDS_IndexedDataMapOfShapeBndSphere.hxx>
60 #include <GEOMAlgo_IndexedDataMapOfIntegerShape.hxx>
61 #include <GEOMAlgo_PassKeyShape.hxx>
62 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
63 #include <GEOMAlgo_Tools.hxx>
65 //=======================================================================
68 //=======================================================================
69 GEOMAlgo_GlueDetector::GEOMAlgo_GlueDetector()
74 //=======================================================================
77 //=======================================================================
78 GEOMAlgo_GlueDetector::~GEOMAlgo_GlueDetector()
80 //=======================================================================
83 //=======================================================================
84 void GEOMAlgo_GlueDetector::Perform()
109 //=======================================================================
110 //function : DetectVertices
112 //=======================================================================
113 void GEOMAlgo_GlueDetector::DetectVertices()
115 Standard_Integer j, i, aNbV, aNbVSD;
118 TColStd_ListIteratorOfListOfInteger aIt;
120 TopTools_IndexedMapOfShape aMV;
121 TopTools_MapOfShape aMVProcessed;
122 TopTools_ListIteratorOfListOfShape aItS;
123 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
124 TopTools_DataMapOfShapeListOfShape aMVV;
125 GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
126 NMTDS_IndexedDataMapOfShapeBndSphere aMSB;
128 NMTDS_BndSphereTreeSelector aSelector;
129 NMTDS_BndSphereTree aBBTree;
130 NCollection_UBTreeFiller <Standard_Integer, NMTDS_BndSphere> aTreeFiller(aBBTree);
134 TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMV);
137 myErrorStatus=2; // no vertices in source shape
141 for (i=1; i<=aNbV; ++i) {
142 NMTDS_BndSphere aBox;
144 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
145 aPV=BRep_Tool::Pnt(aV);
146 aTolV=BRep_Tool::Tolerance(aV);
148 aBox.SetGap(myTolerance);
150 aBox.SetRadius(aTolV);
152 aTreeFiller.Add(i, aBox);
160 //---------------------------------------------------
162 for (i=1; i<=aNbV; ++i) {
163 const TopoDS_Shape& aV=aMV(i);
165 if (aMVProcessed.Contains(aV)) {
169 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
170 TopTools_ListOfShape aLVSD;
171 TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
172 TColStd_MapIteratorOfMapOfInteger aIt1;
177 aIt1.Initialize(aMIP);
178 for(; aIt1.More(); aIt1.Next()) {
180 if (aMIPC.Contains(aIP)) {
184 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
185 const NMTDS_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
188 aSelector.SetBox(aBoxVP);
190 aNbVSD=aBBTree.Select(aSelector);
192 continue; // it shoild not be so [at least IP itself]
195 const TColStd_ListOfInteger& aLI=aSelector.Indices();
197 for (; aIt.More(); aIt.Next()) {
199 if (aMIP.Contains(aIP1)) {
203 } //for (; aIt.More(); aIt.Next()) {
204 }//for(; aIt1.More(); aIt1.Next()) {
206 aNbIP1=aMIP1.Extent();
211 aIt1.Initialize(aMIP);
212 for(; aIt1.More(); aIt1.Next()) {
218 aIt1.Initialize(aMIP1);
219 for(; aIt1.More(); aIt1.Next()) {
227 aNbIP=aMIPC.Extent();
229 if (!aNbIP) {// no SD vertices is found
230 aMVProcessed.Add(aV);
233 //else { // SD vertices founded [ aMIPC ]
234 aIt1.Initialize(aMIPC);
235 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
237 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
242 aMVProcessed.Add(aVP);
245 myImages.Bind(aVF, aLVSD);
246 }// for (i=1; i<=aNbV; ++i) {
247 //------------------------------
249 aItIm.Initialize(myImages);
250 for (; aItIm.More(); aItIm.Next()) {
251 const TopoDS_Shape& aV=aItIm.Key();
252 const TopTools_ListOfShape& aLVSD=aItIm.Value();
253 aItS.Initialize(aLVSD);
254 for (; aItS.More(); aItS.Next()) {
255 const TopoDS_Shape& aVSD=aItS.Value();
256 if (!myOrigins.IsBound(aVSD)) {
257 myOrigins.Bind(aVSD, aV);
262 //=======================================================================
263 //function : DetectFaces
265 //=======================================================================
266 void GEOMAlgo_GlueDetector::DetectFaces()
268 DetectShapes(TopAbs_FACE);
270 //=======================================================================
271 //function : DetectEdges
273 //=======================================================================
274 void GEOMAlgo_GlueDetector::DetectEdges()
276 DetectShapes(TopAbs_EDGE);
278 //=======================================================================
279 //function : DetectShapes
281 //=======================================================================
282 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
284 Standard_Boolean bDegenerated;
285 Standard_Integer i, aNbF, aNbSDF, iErr;
286 TopTools_IndexedMapOfShape aMF;
287 TopTools_ListIteratorOfListOfShape aItLS;
288 GEOMAlgo_PassKeyShape aPKF;
289 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
293 TopExp::MapShapes(myArgument, aType, aMF);
296 for (i=1; i<=aNbF; ++i) {
297 const TopoDS_Shape& aS=aMF(i);
299 if (aType==TopAbs_FACE) {
300 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
301 FacePassKey(aF, aPKF);
303 else if (aType==TopAbs_EDGE) {
304 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
305 EdgePassKey(aE, aPKF);
312 if (aMPKLF.Contains(aPKF)) {
313 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
317 TopTools_ListOfShape aLSDF;
320 aMPKLF.Add(aPKF, aLSDF);
323 // check geometric coincidence
324 if (myCheckGeometry) {
325 iErr=GEOMAlgo_Tools::RefineSDShapes(aMPKLF, myTolerance, myContext);
333 aNbF=aMPKLF.Extent();
334 for (i=1; i<=aNbF; ++i) {
335 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
336 aNbSDF=aLSDF.Extent();
338 myErrorStatus=4; // it must not be
345 const TopoDS_Shape& aS1=aLSDF.First();
347 if (aType==TopAbs_EDGE) {
348 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
349 bDegenerated=BRep_Tool::Degenerated(aE1);
355 myImages.Bind(aS1, aLSDF);
357 aItLS.Initialize(aLSDF);
358 for (; aItLS.More(); aItLS.Next()) {
359 const TopoDS_Shape& aFSD=aItLS.Value();
360 if (!myOrigins.IsBound(aFSD)) {
361 myOrigins.Bind(aFSD, aS1);
364 }// for (i=1; i<=aNbF; ++i)
366 //=======================================================================
367 //function : FacePassKey
369 //=======================================================================
370 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
371 GEOMAlgo_PassKeyShape& aPK)
373 Standard_Integer i, aNbE;
375 TopTools_ListOfShape aLE;
376 TopTools_IndexedMapOfShape aME;
378 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
381 for (i=1; i<=aNbE; ++i) {
382 const TopoDS_Shape& aE=aME(i);
384 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
385 if (BRep_Tool::Degenerated(aEE)) {
389 if (myOrigins.IsBound(aE)) {
390 aER=myOrigins.Find(aE);
399 //=======================================================================
400 //function : EdgePassKey
402 //=======================================================================
403 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
404 GEOMAlgo_PassKeyShape& aPK)
406 TopAbs_Orientation aOr;
409 TopTools_ListOfShape aLV;
412 for (; aIt.More(); aIt.Next()) {
413 const TopoDS_Shape& aV=aIt.Value();
414 aOr=aV.Orientation();
415 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
416 if (myOrigins.IsBound(aV)) {
417 aVR=myOrigins.Find(aV);