1 // Copyright (C) 2007-2022 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 static Standard_Integer CheckAncesstors(const TopoDS_Shape& aVSD,
72 const TopTools_MapOfShape& aMVSD,
73 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
74 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
75 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ);
78 //=======================================================================
81 //=======================================================================
82 GEOMAlgo_GlueDetector::GEOMAlgo_GlueDetector()
88 //=======================================================================
91 //=======================================================================
92 GEOMAlgo_GlueDetector::~GEOMAlgo_GlueDetector()
95 //=======================================================================
96 //function : StickedShapes
98 //=======================================================================
99 const TopTools_IndexedDataMapOfShapeListOfShape&
100 GEOMAlgo_GlueDetector::StickedShapes()
102 return myStickedShapes;
105 //=======================================================================
108 //=======================================================================
109 void GEOMAlgo_GlueDetector::Perform()
113 myStickedShapes.Clear();
120 // Initialize the context
121 GEOMAlgo_GluerAlgo::Perform();
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 should 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);
297 //=======================================================================
298 //function : DetectFaces
300 //=======================================================================
301 void GEOMAlgo_GlueDetector::DetectFaces()
303 DetectShapes(TopAbs_FACE);
306 //=======================================================================
307 //function : DetectEdges
309 //=======================================================================
310 void GEOMAlgo_GlueDetector::DetectEdges()
312 DetectShapes(TopAbs_EDGE);
315 //=======================================================================
316 //function : DetectShapes
318 //=======================================================================
319 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
321 Standard_Boolean bDegenerated;
322 Standard_Integer i, aNbF, aNbSDF, iErr;
323 TopTools_IndexedMapOfShape aMF;
324 TopTools_ListIteratorOfListOfShape aItLS;
325 GEOMAlgo_PassKeyShape aPKF;
326 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
330 TopExp::MapShapes(myArgument, aType, aMF);
333 for (i=1; i<=aNbF; ++i) {
334 const TopoDS_Shape& aS=aMF(i);
336 if (aType==TopAbs_FACE) {
337 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
338 FacePassKey(aF, aPKF);
340 else if (aType==TopAbs_EDGE) {
341 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
342 EdgePassKey(aE, aPKF);
349 if (aMPKLF.Contains(aPKF)) {
350 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
354 TopTools_ListOfShape aLSDF;
357 aMPKLF.Add(aPKF, aLSDF);
360 // check geometric coincidence
361 if (myCheckGeometry) {
362 iErr=GEOMAlgo_AlgoTools::RefineSDShapes(aMPKLF, myTolerance, myContext);
370 aNbF=aMPKLF.Extent();
371 for (i=1; i<=aNbF; ++i) {
372 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
373 aNbSDF=aLSDF.Extent();
375 myErrorStatus=4; // it must not be
382 const TopoDS_Shape& aS1=aLSDF.First();
384 if (aType==TopAbs_EDGE) {
385 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
386 bDegenerated=BRep_Tool::Degenerated(aE1);
392 myImages.Bind(aS1, aLSDF);
395 aItLS.Initialize(aLSDF);
396 for (; aItLS.More(); aItLS.Next()) {
397 const TopoDS_Shape& aFSD=aItLS.Value();
398 if (!myOrigins.IsBound(aFSD)) {
399 myOrigins.Bind(aFSD, aS1);
402 }// for (i=1; i<=aNbF; ++i)
405 //=======================================================================
406 //function : FacePassKey
408 //=======================================================================
409 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
410 GEOMAlgo_PassKeyShape& aPK)
412 Standard_Integer i, aNbE;
414 TopTools_ListOfShape aLE;
415 TopTools_IndexedMapOfShape aME;
417 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
420 for (i=1; i<=aNbE; ++i) {
421 const TopoDS_Shape& aE=aME(i);
423 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
424 if (BRep_Tool::Degenerated(aEE)) {
428 if (myOrigins.IsBound(aE)) {
429 aER=myOrigins.Find(aE);
439 //=======================================================================
440 //function : EdgePassKey
442 //=======================================================================
443 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
444 GEOMAlgo_PassKeyShape& aPK)
446 TopAbs_Orientation aOr;
449 TopTools_ListOfShape aLV;
452 for (; aIt.More(); aIt.Next()) {
453 const TopoDS_Shape& aV=aIt.Value();
454 aOr=aV.Orientation();
455 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
456 if (myOrigins.IsBound(aV)) {
457 aVR=myOrigins.Find(aV);
469 //=======================================================================
470 //function : CheckDetected
472 //=======================================================================
473 void GEOMAlgo_GlueDetector::CheckDetected()
475 TopoDS_Iterator aItA;
476 TopExp_Explorer aExp;
477 TopTools_ListOfShape aLV;
478 TopTools_MapOfShape aMFence;
479 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
480 TopTools_IndexedDataMapOfShapeListOfShape aMVE, aMEV;
483 TopExp::MapShapesAndAncestors(myArgument, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
485 aExp.Init(myArgument, TopAbs_EDGE);
486 for (; aExp.More(); aExp.Next()) {
487 const TopoDS_Shape& aE=aExp.Current();
492 for (; aItA.More(); aItA.Next()) {
493 const TopoDS_Shape& aV=aItA.Value();
494 if (aMFence.Add(aV)) {
502 aItIm.Initialize(myImages);
503 for (; aItIm.More(); aItIm.Next()) {
504 //const TopoDS_Shape& aV=aItIm.Key();
505 const TopTools_ListOfShape& aLVSD=aItIm.Value();
506 CheckDetected(aLVSD, aMVE, aMEV);
510 //=======================================================================
511 //function : CheckDetected
513 //=======================================================================
514 void GEOMAlgo_GlueDetector::CheckDetected(const TopTools_ListOfShape& aLVSD,
515 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
516 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV)
518 Standard_Integer aNbVSD, iRet;
519 TopExp_Explorer aExp, aExpA;
520 TopTools_MapOfShape aMFence, aMVSD;
521 TopTools_ListOfShape aLV;
522 TopTools_ListIteratorOfListOfShape aItLS;
526 aNbVSD=aLVSD.Extent();
531 aItLS.Initialize(aLVSD);
532 for (; aItLS.More(); aItLS.Next()) {
533 const TopoDS_Shape& aVSD=aItLS.Value();
537 aItLS.Initialize(aLVSD);
538 for (; aItLS.More(); aItLS.Next()) {
539 const TopoDS_Shape& aVSD=aItLS.Value();
541 iRet=CheckAncesstors(aVSD, aMVSD, aMVE, aMEV, myStickedShapes);
543 // Sticked shapes detected
549 //=======================================================================
550 //function : CheckAncesstors
552 //=======================================================================
553 Standard_Integer CheckAncesstors(const TopoDS_Shape& aVSD,
554 const TopTools_MapOfShape& aMVSD,
555 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
556 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
557 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ)
559 TopTools_ListOfShape *pLE, *pLV, *pLVZ;
560 Standard_Integer iRet, aNbVX;
561 TopTools_ListIteratorOfListOfShape aItLE, aItLV;
562 TopTools_MapOfShape aMFence;
563 TopTools_ListOfShape aLVX;
567 pLE=const_cast<TopTools_IndexedDataMapOfShapeListOfShape&>(aMVE).ChangeSeek(aVSD);
571 aItLE.Initialize(*pLE);
572 for (; aItLE.More(); aItLE.Next()) {
573 const TopoDS_Shape& aE=aItLE.Value();
575 pLV=const_cast<TopTools_IndexedDataMapOfShapeListOfShape&>(aMEV).ChangeSeek(aE);
577 continue; // it should be not so
580 aItLV.Initialize(*pLV);
581 for (; aItLV.More(); aItLV.Next()) {
582 const TopoDS_Shape& aV=aItLV.Value();
583 if (!aV.IsSame(aVSD)) {
584 if (aMVSD.Contains(aV)) {
585 if (aMFence.Add(aV)) {
599 pLVZ=aMEVZ.ChangeSeek(aE);
604 TopTools_ListOfShape& aLVZ=*pLVZ;