1 // Copyright (C) 2007-2016 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 //=======================================================================
94 //function : StickedShapes
96 //=======================================================================
97 const TopTools_IndexedDataMapOfShapeListOfShape&
98 GEOMAlgo_GlueDetector::StickedShapes()
100 return myStickedShapes;
102 //=======================================================================
105 //=======================================================================
106 void GEOMAlgo_GlueDetector::Perform()
110 myStickedShapes.Clear();
117 // Initialize the context
118 GEOMAlgo_GluerAlgo::Perform();
140 //=======================================================================
141 //function : DetectVertices
143 //=======================================================================
144 void GEOMAlgo_GlueDetector::DetectVertices()
146 Standard_Integer j, i, aNbV, aNbVSD;
149 TColStd_ListIteratorOfListOfInteger aIt;
151 TopTools_IndexedMapOfShape aMV;
152 TopTools_MapOfShape aMVProcessed;
153 TopTools_ListIteratorOfListOfShape aItS;
154 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
155 TopTools_DataMapOfShapeListOfShape aMVV;
156 GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
157 GEOMAlgo_IndexedDataMapOfShapeBndSphere aMSB;
158 GEOMAlgo_BndSphereTreeSelector aSelector;
159 GEOMAlgo_BndSphereTree aBBTree;
160 NCollection_UBTreeFiller <Standard_Integer, GEOMAlgo_BndSphere> aTreeFiller(aBBTree);
164 TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMV);
167 myErrorStatus=2; // no vertices in source shape
171 for (i=1; i<=aNbV; ++i) {
172 GEOMAlgo_BndSphere aBox;
174 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
175 aPV=BRep_Tool::Pnt(aV);
176 aTolV=BRep_Tool::Tolerance(aV);
178 aBox.SetGap(myTolerance);
180 aBox.SetRadius(aTolV);
182 aTreeFiller.Add(i, aBox);
190 //---------------------------------------------------
192 for (i=1; i<=aNbV; ++i) {
193 const TopoDS_Shape& aV=aMV(i);
195 if (aMVProcessed.Contains(aV)) {
199 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
200 TopTools_ListOfShape aLVSD;
201 TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
202 TColStd_MapIteratorOfMapOfInteger aIt1;
207 aIt1.Initialize(aMIP);
208 for(; aIt1.More(); aIt1.Next()) {
210 if (aMIPC.Contains(aIP)) {
214 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
215 const GEOMAlgo_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
218 aSelector.SetBox(aBoxVP);
220 aNbVSD=aBBTree.Select(aSelector);
222 continue; // it shoild not be so [at least IP itself]
225 const TColStd_ListOfInteger& aLI=aSelector.Indices();
227 for (; aIt.More(); aIt.Next()) {
229 if (aMIP.Contains(aIP1)) {
233 } //for (; aIt.More(); aIt.Next()) {
234 }//for(; aIt1.More(); aIt1.Next()) {
236 aNbIP1=aMIP1.Extent();
241 aIt1.Initialize(aMIP);
242 for(; aIt1.More(); aIt1.Next()) {
248 aIt1.Initialize(aMIP1);
249 for(; aIt1.More(); aIt1.Next()) {
257 aNbIP=aMIPC.Extent();
259 if (!aNbIP) {// no SD vertices is found
260 aMVProcessed.Add(aV);
263 //else { // SD vertices founded [ aMIPC ]
264 aIt1.Initialize(aMIPC);
265 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
267 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
272 aMVProcessed.Add(aVP);
275 myImages.Bind(aVF, aLVSD);
276 }// for (i=1; i<=aNbV; ++i) {
277 //------------------------------
279 aItIm.Initialize(myImages);
280 for (; aItIm.More(); aItIm.Next()) {
281 const TopoDS_Shape& aV=aItIm.Key();
282 const TopTools_ListOfShape& aLVSD=aItIm.Value();
283 aItS.Initialize(aLVSD);
284 for (; aItS.More(); aItS.Next()) {
285 const TopoDS_Shape& aVSD=aItS.Value();
286 if (!myOrigins.IsBound(aVSD)) {
287 myOrigins.Bind(aVSD, aV);
292 //=======================================================================
293 //function : DetectFaces
295 //=======================================================================
296 void GEOMAlgo_GlueDetector::DetectFaces()
298 DetectShapes(TopAbs_FACE);
300 //=======================================================================
301 //function : DetectEdges
303 //=======================================================================
304 void GEOMAlgo_GlueDetector::DetectEdges()
306 DetectShapes(TopAbs_EDGE);
308 //=======================================================================
309 //function : DetectShapes
311 //=======================================================================
312 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
314 Standard_Boolean bDegenerated;
315 Standard_Integer i, aNbF, aNbSDF, iErr;
316 TopTools_IndexedMapOfShape aMF;
317 TopTools_ListIteratorOfListOfShape aItLS;
318 GEOMAlgo_PassKeyShape aPKF;
319 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
323 TopExp::MapShapes(myArgument, aType, aMF);
326 for (i=1; i<=aNbF; ++i) {
327 const TopoDS_Shape& aS=aMF(i);
329 if (aType==TopAbs_FACE) {
330 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
331 FacePassKey(aF, aPKF);
333 else if (aType==TopAbs_EDGE) {
334 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
335 EdgePassKey(aE, aPKF);
342 if (aMPKLF.Contains(aPKF)) {
343 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
347 TopTools_ListOfShape aLSDF;
350 aMPKLF.Add(aPKF, aLSDF);
353 // check geometric coincidence
354 if (myCheckGeometry) {
355 iErr=GEOMAlgo_AlgoTools::RefineSDShapes(aMPKLF, myTolerance, myContext);
363 aNbF=aMPKLF.Extent();
364 for (i=1; i<=aNbF; ++i) {
365 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
366 aNbSDF=aLSDF.Extent();
368 myErrorStatus=4; // it must not be
375 const TopoDS_Shape& aS1=aLSDF.First();
377 if (aType==TopAbs_EDGE) {
378 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
379 bDegenerated=BRep_Tool::Degenerated(aE1);
385 myImages.Bind(aS1, aLSDF);
388 aItLS.Initialize(aLSDF);
389 for (; aItLS.More(); aItLS.Next()) {
390 const TopoDS_Shape& aFSD=aItLS.Value();
391 if (!myOrigins.IsBound(aFSD)) {
392 myOrigins.Bind(aFSD, aS1);
395 }// for (i=1; i<=aNbF; ++i)
397 //=======================================================================
398 //function : FacePassKey
400 //=======================================================================
401 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
402 GEOMAlgo_PassKeyShape& aPK)
404 Standard_Integer i, aNbE;
406 TopTools_ListOfShape aLE;
407 TopTools_IndexedMapOfShape aME;
409 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
412 for (i=1; i<=aNbE; ++i) {
413 const TopoDS_Shape& aE=aME(i);
415 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
416 if (BRep_Tool::Degenerated(aEE)) {
420 if (myOrigins.IsBound(aE)) {
421 aER=myOrigins.Find(aE);
430 //=======================================================================
431 //function : EdgePassKey
433 //=======================================================================
434 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
435 GEOMAlgo_PassKeyShape& aPK)
437 TopAbs_Orientation aOr;
440 TopTools_ListOfShape aLV;
443 for (; aIt.More(); aIt.Next()) {
444 const TopoDS_Shape& aV=aIt.Value();
445 aOr=aV.Orientation();
446 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
447 if (myOrigins.IsBound(aV)) {
448 aVR=myOrigins.Find(aV);
459 //=======================================================================
460 //function : CheckDetected
462 //=======================================================================
463 void GEOMAlgo_GlueDetector::CheckDetected()
465 TopoDS_Iterator aItA;
466 TopExp_Explorer aExp;
467 TopTools_ListOfShape aLV;
468 TopTools_MapOfShape aMFence;
469 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
470 TopTools_IndexedDataMapOfShapeListOfShape aMVE, aMEV;
473 TopExp::MapShapesAndAncestors(myArgument, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
475 aExp.Init(myArgument, TopAbs_EDGE);
476 for (; aExp.More(); aExp.Next()) {
477 const TopoDS_Shape& aE=aExp.Current();
482 for (; aItA.More(); aItA.Next()) {
483 const TopoDS_Shape& aV=aItA.Value();
484 if (aMFence.Add(aV)) {
492 aItIm.Initialize(myImages);
493 for (; aItIm.More(); aItIm.Next()) {
494 //const TopoDS_Shape& aV=aItIm.Key();
495 const TopTools_ListOfShape& aLVSD=aItIm.Value();
496 CheckDetected(aLVSD, aMVE, aMEV);
499 //=======================================================================
500 //function : CheckDetected
502 //=======================================================================
503 void GEOMAlgo_GlueDetector::CheckDetected
504 (const TopTools_ListOfShape& aLVSD,
505 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
506 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV)
508 Standard_Integer aNbVSD, iRet;
509 TopExp_Explorer aExp, aExpA;
510 TopTools_MapOfShape aMFence, aMVSD;
511 TopTools_ListOfShape aLV;
512 TopTools_ListIteratorOfListOfShape aItLS;
516 aNbVSD=aLVSD.Extent();
521 aItLS.Initialize(aLVSD);
522 for (; aItLS.More(); aItLS.Next()) {
523 const TopoDS_Shape& aVSD=aItLS.Value();
527 aItLS.Initialize(aLVSD);
528 for (; aItLS.More(); aItLS.Next()) {
529 const TopoDS_Shape& aVSD=aItLS.Value();
531 iRet=CheckAncesstors(aVSD, aMVSD, aMVE, aMEV, myStickedShapes);
533 // Sticked shapes detected
538 //=======================================================================
539 //function : CheckAncesstors
541 //=======================================================================
542 Standard_Integer CheckAncesstors
543 (const TopoDS_Shape& aVSD,
544 const TopTools_MapOfShape& aMVSD,
545 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
546 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
547 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ)
549 TopTools_ListOfShape *pLE, *pLV, *pLVZ;
550 Standard_Integer iRet, aNbVX;
551 TopTools_ListIteratorOfListOfShape aItLE, aItLV;
552 TopTools_MapOfShape aMFence;
553 TopTools_ListOfShape aLVX;
557 #if OCC_VERSION_MAJOR < 7
558 pLE=const_cast<TopTools_ListOfShape*>(&aMVE.FindFromKey(aVSD));
560 pLE=const_cast<TopTools_IndexedDataMapOfShapeListOfShape&>(aMVE).ChangeSeek(aVSD);
565 aItLE.Initialize(*pLE);
566 for (; aItLE.More(); aItLE.Next()) {
567 const TopoDS_Shape& aE=aItLE.Value();
569 #if OCC_VERSION_MAJOR < 7
570 pLV=const_cast<TopTools_ListOfShape*>(&aMEV.FindFromKey(aE));
572 pLV=const_cast<TopTools_IndexedDataMapOfShapeListOfShape&>(aMEV).ChangeSeek(aE);
575 continue; // it should be not so
578 aItLV.Initialize(*pLV);
579 for (; aItLV.More(); aItLV.Next()) {
580 const TopoDS_Shape& aV=aItLV.Value();
581 if (!aV.IsSame(aVSD)) {
582 if (aMVSD.Contains(aV)) {
583 if (aMFence.Add(aV)) {
597 #if OCC_VERSION_MAJOR < 7
598 pLVZ=const_cast<TopTools_ListOfShape*>(&aMEVZ.FindFromKey(aE));
600 pLVZ=aMEVZ.ChangeSeek(aE);
606 TopTools_ListOfShape& aLVZ=*pLVZ;