1 // Copyright (C) 2007-2012 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
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 <NMTDS_BndSphereTree.hxx>
56 #include <NMTDS_BndSphere.hxx>
57 #include <NMTDS_IndexedDataMapOfShapeBndSphere.hxx>
59 #include <GEOMAlgo_IndexedDataMapOfIntegerShape.hxx>
60 #include <GEOMAlgo_PassKeyShape.hxx>
61 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
62 #include <GEOMAlgo_Tools.hxx>
64 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopTools_MapOfShape.hxx>
69 //modified by NIZNHY-PKV Tue Mar 13 10:25:47 2012f
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);
77 //modified by NIZNHY-PKV Tue Mar 13 10:25:50 2012t
79 //=======================================================================
82 //=======================================================================
83 GEOMAlgo_GlueDetector::GEOMAlgo_GlueDetector()
88 //=======================================================================
91 //=======================================================================
92 GEOMAlgo_GlueDetector::~GEOMAlgo_GlueDetector()
94 //modified by NIZNHY-PKV Tue Mar 13 12:26:50 2012f
95 //=======================================================================
96 //function : StickedShapes
98 //=======================================================================
99 const TopTools_IndexedDataMapOfShapeListOfShape&
100 GEOMAlgo_GlueDetector::StickedShapes()
102 return myStickedShapes;
104 //modified by NIZNHY-PKV Tue Mar 13 12:26:54 2012t
105 //=======================================================================
108 //=======================================================================
109 void GEOMAlgo_GlueDetector::Perform()
113 myStickedShapes.Clear();
120 // Initialize the context
121 GEOMAlgo_GluerAlgo::Perform();
128 //modified by NIZNHY-PKV Wed Mar 14 08:00:09 2012f
133 //modified by NIZNHY-PKV Wed Mar 14 08:00:12 2012t
145 //=======================================================================
146 //function : DetectVertices
148 //=======================================================================
149 void GEOMAlgo_GlueDetector::DetectVertices()
151 Standard_Integer j, i, aNbV, aNbVSD;
154 TColStd_ListIteratorOfListOfInteger aIt;
156 TopTools_IndexedMapOfShape aMV;
157 TopTools_MapOfShape aMVProcessed;
158 TopTools_ListIteratorOfListOfShape aItS;
159 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
160 TopTools_DataMapOfShapeListOfShape aMVV;
161 GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
162 NMTDS_IndexedDataMapOfShapeBndSphere aMSB;
163 NMTDS_BndSphereTreeSelector aSelector;
164 NMTDS_BndSphereTree aBBTree;
165 NCollection_UBTreeFiller <Standard_Integer, NMTDS_BndSphere> aTreeFiller(aBBTree);
169 TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMV);
172 myErrorStatus=2; // no vertices in source shape
176 for (i=1; i<=aNbV; ++i) {
177 NMTDS_BndSphere aBox;
179 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
180 aPV=BRep_Tool::Pnt(aV);
181 aTolV=BRep_Tool::Tolerance(aV);
183 aBox.SetGap(myTolerance);
185 aBox.SetRadius(aTolV);
187 aTreeFiller.Add(i, aBox);
195 //---------------------------------------------------
197 for (i=1; i<=aNbV; ++i) {
198 const TopoDS_Shape& aV=aMV(i);
200 if (aMVProcessed.Contains(aV)) {
204 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
205 TopTools_ListOfShape aLVSD;
206 TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
207 TColStd_MapIteratorOfMapOfInteger aIt1;
212 aIt1.Initialize(aMIP);
213 for(; aIt1.More(); aIt1.Next()) {
215 if (aMIPC.Contains(aIP)) {
219 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
220 const NMTDS_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
223 aSelector.SetBox(aBoxVP);
225 aNbVSD=aBBTree.Select(aSelector);
227 continue; // it shoild not be so [at least IP itself]
230 const TColStd_ListOfInteger& aLI=aSelector.Indices();
232 for (; aIt.More(); aIt.Next()) {
234 if (aMIP.Contains(aIP1)) {
238 } //for (; aIt.More(); aIt.Next()) {
239 }//for(; aIt1.More(); aIt1.Next()) {
241 aNbIP1=aMIP1.Extent();
246 aIt1.Initialize(aMIP);
247 for(; aIt1.More(); aIt1.Next()) {
253 aIt1.Initialize(aMIP1);
254 for(; aIt1.More(); aIt1.Next()) {
262 aNbIP=aMIPC.Extent();
264 if (!aNbIP) {// no SD vertices is found
265 aMVProcessed.Add(aV);
268 //else { // SD vertices founded [ aMIPC ]
269 aIt1.Initialize(aMIPC);
270 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
272 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
277 aMVProcessed.Add(aVP);
280 myImages.Bind(aVF, aLVSD);
281 }// for (i=1; i<=aNbV; ++i) {
282 //------------------------------
284 aItIm.Initialize(myImages);
285 for (; aItIm.More(); aItIm.Next()) {
286 const TopoDS_Shape& aV=aItIm.Key();
287 const TopTools_ListOfShape& aLVSD=aItIm.Value();
288 aItS.Initialize(aLVSD);
289 for (; aItS.More(); aItS.Next()) {
290 const TopoDS_Shape& aVSD=aItS.Value();
291 if (!myOrigins.IsBound(aVSD)) {
292 myOrigins.Bind(aVSD, aV);
297 //=======================================================================
298 //function : DetectFaces
300 //=======================================================================
301 void GEOMAlgo_GlueDetector::DetectFaces()
303 DetectShapes(TopAbs_FACE);
305 //=======================================================================
306 //function : DetectEdges
308 //=======================================================================
309 void GEOMAlgo_GlueDetector::DetectEdges()
311 DetectShapes(TopAbs_EDGE);
313 //=======================================================================
314 //function : DetectShapes
316 //=======================================================================
317 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
319 Standard_Boolean bDegenerated;
320 Standard_Integer i, aNbF, aNbSDF, iErr;
321 TopTools_IndexedMapOfShape aMF;
322 TopTools_ListIteratorOfListOfShape aItLS;
323 GEOMAlgo_PassKeyShape aPKF;
324 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
328 TopExp::MapShapes(myArgument, aType, aMF);
331 for (i=1; i<=aNbF; ++i) {
332 const TopoDS_Shape& aS=aMF(i);
334 if (aType==TopAbs_FACE) {
335 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
336 FacePassKey(aF, aPKF);
338 else if (aType==TopAbs_EDGE) {
339 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
340 EdgePassKey(aE, aPKF);
347 if (aMPKLF.Contains(aPKF)) {
348 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
352 TopTools_ListOfShape aLSDF;
355 aMPKLF.Add(aPKF, aLSDF);
358 // check geometric coincidence
359 if (myCheckGeometry) {
360 iErr=GEOMAlgo_Tools::RefineSDShapes(aMPKLF, myTolerance, myContext);
368 aNbF=aMPKLF.Extent();
369 for (i=1; i<=aNbF; ++i) {
370 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
371 aNbSDF=aLSDF.Extent();
373 myErrorStatus=4; // it must not be
380 const TopoDS_Shape& aS1=aLSDF.First();
382 if (aType==TopAbs_EDGE) {
383 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
384 bDegenerated=BRep_Tool::Degenerated(aE1);
390 myImages.Bind(aS1, aLSDF);
393 aItLS.Initialize(aLSDF);
394 for (; aItLS.More(); aItLS.Next()) {
395 const TopoDS_Shape& aFSD=aItLS.Value();
396 if (!myOrigins.IsBound(aFSD)) {
397 myOrigins.Bind(aFSD, aS1);
400 }// for (i=1; i<=aNbF; ++i)
402 //=======================================================================
403 //function : FacePassKey
405 //=======================================================================
406 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
407 GEOMAlgo_PassKeyShape& aPK)
409 Standard_Integer i, aNbE;
411 TopTools_ListOfShape aLE;
412 TopTools_IndexedMapOfShape aME;
414 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
417 for (i=1; i<=aNbE; ++i) {
418 const TopoDS_Shape& aE=aME(i);
420 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
421 if (BRep_Tool::Degenerated(aEE)) {
425 if (myOrigins.IsBound(aE)) {
426 aER=myOrigins.Find(aE);
435 //=======================================================================
436 //function : EdgePassKey
438 //=======================================================================
439 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
440 GEOMAlgo_PassKeyShape& aPK)
442 TopAbs_Orientation aOr;
445 TopTools_ListOfShape aLV;
448 for (; aIt.More(); aIt.Next()) {
449 const TopoDS_Shape& aV=aIt.Value();
450 aOr=aV.Orientation();
451 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
452 if (myOrigins.IsBound(aV)) {
453 aVR=myOrigins.Find(aV);
464 //modified by NIZNHY-PKV Tue Mar 13 09:54:18 2012f
465 //=======================================================================
466 //function : CheckDetected
468 //=======================================================================
469 void GEOMAlgo_GlueDetector::CheckDetected()
471 TopoDS_Iterator aItA;
472 TopExp_Explorer aExp;
473 TopTools_ListOfShape aLV;
474 TopTools_MapOfShape aMFence;
475 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
476 TopTools_IndexedDataMapOfShapeListOfShape aMVE, aMEV;
479 TopExp::MapShapesAndAncestors(myArgument, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
481 aExp.Init(myArgument, TopAbs_EDGE);
482 for (; aExp.More(); aExp.Next()) {
483 const TopoDS_Shape& aE=aExp.Current();
488 for (; aItA.More(); aItA.Next()) {
489 const TopoDS_Shape& aV=aItA.Value();
490 if (aMFence.Add(aV)) {
498 aItIm.Initialize(myImages);
499 for (; aItIm.More(); aItIm.Next()) {
500 //const TopoDS_Shape& aV=aItIm.Key();
501 const TopTools_ListOfShape& aLVSD=aItIm.Value();
502 CheckDetected(aLVSD, aMVE, aMEV);
505 //=======================================================================
506 //function : CheckDetected
508 //=======================================================================
509 void GEOMAlgo_GlueDetector::CheckDetected
510 (const TopTools_ListOfShape& aLVSD,
511 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
512 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV)
514 Standard_Integer i, aNbVSD, aNbA, iRet;
515 TopAbs_ShapeEnum aTypeS, aTypeA[2];
516 TopExp_Explorer aExp, aExpA;
517 TopTools_MapOfShape aMFence, aMVSD;
518 TopTools_ListOfShape aLV;
519 TopTools_ListIteratorOfListOfShape aItLS;
523 aNbVSD=aLVSD.Extent();
528 aItLS.Initialize(aLVSD);
529 for (; aItLS.More(); aItLS.Next()) {
530 const TopoDS_Shape& aVSD=aItLS.Value();
534 aItLS.Initialize(aLVSD);
535 for (; aItLS.More(); aItLS.Next()) {
536 const TopoDS_Shape& aVSD=aItLS.Value();
538 iRet=CheckAncesstors(aVSD, aMVSD, aMVE, aMEV, myStickedShapes);
540 // Sticked shapes detected
545 //=======================================================================
546 //function : CheckAncesstors
548 //=======================================================================
549 Standard_Integer CheckAncesstors
550 (const TopoDS_Shape& aVSD,
551 const TopTools_MapOfShape& aMVSD,
552 const TopTools_IndexedDataMapOfShapeListOfShape& aMVE,
553 const TopTools_IndexedDataMapOfShapeListOfShape& aMEV,
554 TopTools_IndexedDataMapOfShapeListOfShape& aMEVZ)
556 Standard_Address pLE, pLV, pLVZ;
557 Standard_Integer iRet, aNbVX;
558 TopTools_ListIteratorOfListOfShape aItLE, aItLV;
559 TopTools_MapOfShape aMFence;
560 TopTools_ListOfShape aLVX;
564 pLE=aMVE.FindFromKey1(aVSD);
569 const TopTools_ListOfShape& aLE=*((TopTools_ListOfShape*)pLE);
570 aItLE.Initialize(aLE);
571 for (; aItLE.More(); aItLE.Next()) {
572 const TopoDS_Shape& aE=aItLE.Value();
574 pLV=aMEV.FindFromKey1(aE);
576 continue; // it should be not so
580 const TopTools_ListOfShape& aLV=*((TopTools_ListOfShape*)pLV);
581 aItLV.Initialize(aLV);
582 for (; aItLV.More(); aItLV.Next()) {
583 const TopoDS_Shape& aV=aItLV.Value();
584 if (!aV.IsSame(aVSD)) {
585 if (aMVSD.Contains(aV)) {
586 if (aMFence.Add(aV)) {
600 pLVZ=aMEVZ.FindFromKey1(aE);
605 TopTools_ListOfShape& aLVZ=*((TopTools_ListOfShape*)pLVZ);
612 //modified by NIZNHY-PKV Tue Mar 13 09:54:59 2012t