1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 // File: GEOMAlgo_GlueDetector.cxx
24 // Author: Peter KURNEV
26 #include <GEOMAlgo_GlueDetector.hxx>
28 #include <Bnd_Box.hxx>
29 #include <NCollection_UBTreeFiller.hxx>
31 #include <TColStd_ListOfInteger.hxx>
32 #include <TColStd_ListIteratorOfListOfInteger.hxx>
33 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Compound.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_MapOfShape.hxx>
48 #include <TopTools_MapIteratorOfMapOfShape.hxx>
51 #include <BRep_Tool.hxx>
52 #include <BRep_Builder.hxx>
53 #include <BRepBndLib.hxx>
55 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
57 #include <TopExp_Explorer.hxx>
58 #include <TopTools_MapOfShape.hxx>
60 #include <GEOMAlgo_BndSphereTree.hxx>
61 #include <GEOMAlgo_BndSphere.hxx>
62 #include <GEOMAlgo_IndexedDataMapOfShapeBndSphere.hxx>
64 #include <GEOMAlgo_IndexedDataMapOfIntegerShape.hxx>
65 #include <GEOMAlgo_PassKeyShape.hxx>
66 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
67 #include <GEOMAlgo_AlgoTools.hxx>
71 Standard_Integer CheckAncesstors
72 (const TopoDS_Shape& aVSD,
73 const TopTools_MapOfShape& aMVSD,
74 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
75 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
76 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ);
78 //=======================================================================
81 //=======================================================================
82 GEOMAlgo_GlueDetector::GEOMAlgo_GlueDetector()
87 //=======================================================================
90 //=======================================================================
91 GEOMAlgo_GlueDetector::~GEOMAlgo_GlueDetector()
93 //modified by NIZNHY-PKV Tue Mar 13 12:26:50 2012f
94 //=======================================================================
95 //function : StickedShapes
97 //=======================================================================
98 const TopTools_IndexedDataMapOfShapeListOfShape&
99 GEOMAlgo_GlueDetector::StickedShapes()
101 return myStickedShapes;
103 //modified by NIZNHY-PKV Tue Mar 13 12:26:54 2012t
104 //=======================================================================
107 //=======================================================================
108 void GEOMAlgo_GlueDetector::Perform()
112 myStickedShapes.Clear();
119 // Initialize the context
120 GEOMAlgo_GluerAlgo::Perform();
127 //modified by NIZNHY-PKV Wed Mar 14 08:00:09 2012f
132 //modified by NIZNHY-PKV Wed Mar 14 08:00:12 2012t
144 //=======================================================================
145 //function : DetectVertices
147 //=======================================================================
148 void GEOMAlgo_GlueDetector::DetectVertices()
150 Standard_Integer j, i, aNbV, aNbVSD;
153 TColStd_ListIteratorOfListOfInteger aIt;
155 TopTools_IndexedMapOfShape aMV;
156 TopTools_MapOfShape aMVProcessed;
157 TopTools_ListIteratorOfListOfShape aItS;
158 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
159 TopTools_DataMapOfShapeListOfShape aMVV;
160 GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
161 GEOMAlgo_IndexedDataMapOfShapeBndSphere aMSB;
162 GEOMAlgo_BndSphereTreeSelector aSelector;
163 GEOMAlgo_BndSphereTree aBBTree;
164 NCollection_UBTreeFiller <Standard_Integer, GEOMAlgo_BndSphere> aTreeFiller(aBBTree);
168 TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMV);
171 myErrorStatus=2; // no vertices in source shape
175 for (i=1; i<=aNbV; ++i) {
176 GEOMAlgo_BndSphere aBox;
178 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
179 aPV=BRep_Tool::Pnt(aV);
180 aTolV=BRep_Tool::Tolerance(aV);
182 aBox.SetGap(myTolerance);
184 aBox.SetRadius(aTolV);
186 aTreeFiller.Add(i, aBox);
194 //---------------------------------------------------
196 for (i=1; i<=aNbV; ++i) {
197 const TopoDS_Shape& aV=aMV(i);
199 if (aMVProcessed.Contains(aV)) {
203 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
204 TopTools_ListOfShape aLVSD;
205 TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
206 TColStd_MapIteratorOfMapOfInteger aIt1;
211 aIt1.Initialize(aMIP);
212 for(; aIt1.More(); aIt1.Next()) {
214 if (aMIPC.Contains(aIP)) {
218 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
219 const GEOMAlgo_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
222 aSelector.SetBox(aBoxVP);
224 aNbVSD=aBBTree.Select(aSelector);
226 continue; // it shoild not be so [at least IP itself]
229 const TColStd_ListOfInteger& aLI=aSelector.Indices();
231 for (; aIt.More(); aIt.Next()) {
233 if (aMIP.Contains(aIP1)) {
237 } //for (; aIt.More(); aIt.Next()) {
238 }//for(; aIt1.More(); aIt1.Next()) {
240 aNbIP1=aMIP1.Extent();
245 aIt1.Initialize(aMIP);
246 for(; aIt1.More(); aIt1.Next()) {
252 aIt1.Initialize(aMIP1);
253 for(; aIt1.More(); aIt1.Next()) {
261 aNbIP=aMIPC.Extent();
263 if (!aNbIP) {// no SD vertices is found
264 aMVProcessed.Add(aV);
267 //else { // SD vertices founded [ aMIPC ]
268 aIt1.Initialize(aMIPC);
269 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
271 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
276 aMVProcessed.Add(aVP);
279 myImages.Bind(aVF, aLVSD);
280 }// for (i=1; i<=aNbV; ++i) {
281 //------------------------------
283 aItIm.Initialize(myImages);
284 for (; aItIm.More(); aItIm.Next()) {
285 const TopoDS_Shape& aV=aItIm.Key();
286 const TopTools_ListOfShape& aLVSD=aItIm.Value();
287 aItS.Initialize(aLVSD);
288 for (; aItS.More(); aItS.Next()) {
289 const TopoDS_Shape& aVSD=aItS.Value();
290 if (!myOrigins.IsBound(aVSD)) {
291 myOrigins.Bind(aVSD, aV);
296 //=======================================================================
297 //function : DetectFaces
299 //=======================================================================
300 void GEOMAlgo_GlueDetector::DetectFaces()
302 DetectShapes(TopAbs_FACE);
304 //=======================================================================
305 //function : DetectEdges
307 //=======================================================================
308 void GEOMAlgo_GlueDetector::DetectEdges()
310 DetectShapes(TopAbs_EDGE);
312 //=======================================================================
313 //function : DetectShapes
315 //=======================================================================
316 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
318 Standard_Boolean bDegenerated;
319 Standard_Integer i, aNbF, aNbSDF, iErr;
320 TopTools_IndexedMapOfShape aMF;
321 TopTools_ListIteratorOfListOfShape aItLS;
322 GEOMAlgo_PassKeyShape aPKF;
323 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
327 TopExp::MapShapes(myArgument, aType, aMF);
330 for (i=1; i<=aNbF; ++i) {
331 const TopoDS_Shape& aS=aMF(i);
333 if (aType==TopAbs_FACE) {
334 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
335 FacePassKey(aF, aPKF);
337 else if (aType==TopAbs_EDGE) {
338 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
339 EdgePassKey(aE, aPKF);
346 if (aMPKLF.Contains(aPKF)) {
347 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
351 TopTools_ListOfShape aLSDF;
354 aMPKLF.Add(aPKF, aLSDF);
357 // check geometric coincidence
358 if (myCheckGeometry) {
359 iErr=GEOMAlgo_AlgoTools::RefineSDShapes(aMPKLF, myTolerance, myContext);
367 aNbF=aMPKLF.Extent();
368 for (i=1; i<=aNbF; ++i) {
369 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
370 aNbSDF=aLSDF.Extent();
372 myErrorStatus=4; // it must not be
379 const TopoDS_Shape& aS1=aLSDF.First();
381 if (aType==TopAbs_EDGE) {
382 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
383 bDegenerated=BRep_Tool::Degenerated(aE1);
389 myImages.Bind(aS1, aLSDF);
392 aItLS.Initialize(aLSDF);
393 for (; aItLS.More(); aItLS.Next()) {
394 const TopoDS_Shape& aFSD=aItLS.Value();
395 if (!myOrigins.IsBound(aFSD)) {
396 myOrigins.Bind(aFSD, aS1);
399 }// for (i=1; i<=aNbF; ++i)
401 //=======================================================================
402 //function : FacePassKey
404 //=======================================================================
405 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
406 GEOMAlgo_PassKeyShape& aPK)
408 Standard_Integer i, aNbE;
410 TopTools_ListOfShape aLE;
411 TopTools_IndexedMapOfShape aME;
413 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
416 for (i=1; i<=aNbE; ++i) {
417 const TopoDS_Shape& aE=aME(i);
419 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
420 if (BRep_Tool::Degenerated(aEE)) {
424 if (myOrigins.IsBound(aE)) {
425 aER=myOrigins.Find(aE);
434 //=======================================================================
435 //function : EdgePassKey
437 //=======================================================================
438 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
439 GEOMAlgo_PassKeyShape& aPK)
441 TopAbs_Orientation aOr;
444 TopTools_ListOfShape aLV;
447 for (; aIt.More(); aIt.Next()) {
448 const TopoDS_Shape& aV=aIt.Value();
449 aOr=aV.Orientation();
450 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
451 if (myOrigins.IsBound(aV)) {
452 aVR=myOrigins.Find(aV);
463 //modified by NIZNHY-PKV Tue Mar 13 09:54:18 2012f
464 //=======================================================================
465 //function : CheckDetected
467 //=======================================================================
468 void GEOMAlgo_GlueDetector::CheckDetected()
470 TopoDS_Iterator aItA;
471 TopExp_Explorer aExp;
472 TopTools_ListOfShape aLV;
473 TopTools_MapOfShape aMFence;
474 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
475 TopTools_IndexedDataMapOfShapeListOfShape aMVE, aMEV;
478 TopExp::MapShapesAndAncestors(myArgument, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
480 aExp.Init(myArgument, TopAbs_EDGE);
481 for (; aExp.More(); aExp.Next()) {
482 const TopoDS_Shape& aE=aExp.Current();
487 for (; aItA.More(); aItA.Next()) {
488 const TopoDS_Shape& aV=aItA.Value();
489 if (aMFence.Add(aV)) {
497 aItIm.Initialize(myImages);
498 for (; aItIm.More(); aItIm.Next()) {
499 //const TopoDS_Shape& aV=aItIm.Key();
500 const TopTools_ListOfShape& aLVSD=aItIm.Value();
501 CheckDetected(aLVSD, aMVE, aMEV);
504 //=======================================================================
505 //function : CheckDetected
507 //=======================================================================
508 void GEOMAlgo_GlueDetector::CheckDetected
509 (const TopTools_ListOfShape& aLVSD,
510 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
511 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV)
513 Standard_Integer aNbVSD, iRet;
514 TopExp_Explorer aExp, aExpA;
515 TopTools_MapOfShape aMFence, aMVSD;
516 TopTools_ListOfShape aLV;
517 TopTools_ListIteratorOfListOfShape aItLS;
521 aNbVSD=aLVSD.Extent();
526 aItLS.Initialize(aLVSD);
527 for (; aItLS.More(); aItLS.Next()) {
528 const TopoDS_Shape& aVSD=aItLS.Value();
532 aItLS.Initialize(aLVSD);
533 for (; aItLS.More(); aItLS.Next()) {
534 const TopoDS_Shape& aVSD=aItLS.Value();
536 iRet=CheckAncesstors(aVSD, aMVSD, aMVE, aMEV, myStickedShapes);
538 // Sticked shapes detected
543 //=======================================================================
544 //function : CheckAncesstors
546 //=======================================================================
547 Standard_Integer CheckAncesstors
548 (const TopoDS_Shape& aVSD,
549 const TopTools_MapOfShape& aMVSD,
550 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
551 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
552 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ)
554 Standard_Address pLE, pLV, pLVZ;
555 Standard_Integer iRet, aNbVX;
556 TopTools_ListIteratorOfListOfShape aItLE, aItLV;
557 TopTools_MapOfShape aMFence;
558 TopTools_ListOfShape aLVX;
562 pLE=aMVE.FindFromKey1(aVSD);
567 const TopTools_ListOfShape& aLE=*((TopTools_ListOfShape*)pLE);
568 aItLE.Initialize(aLE);
569 for (; aItLE.More(); aItLE.Next()) {
570 const TopoDS_Shape& aE=aItLE.Value();
572 pLV=aMEV.FindFromKey1(aE);
574 continue; // it should be not so
578 const TopTools_ListOfShape& aLV=*((TopTools_ListOfShape*)pLV);
579 aItLV.Initialize(aLV);
580 for (; aItLV.More(); aItLV.Next()) {
581 const TopoDS_Shape& aV=aItLV.Value();
582 if (!aV.IsSame(aVSD)) {
583 if (aMVSD.Contains(aV)) {
584 if (aMFence.Add(aV)) {
598 pLVZ=aMEVZ.FindFromKey1(aE);
603 TopTools_ListOfShape& aLVZ=*((TopTools_ListOfShape*)pLVZ);
610 //modified by NIZNHY-PKV Tue Mar 13 09:54:59 2012t