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()
94 // Initialize the context
95 GEOMAlgo_GluerAlgo::Perform();
112 //=======================================================================
113 //function : DetectVertices
115 //=======================================================================
116 void GEOMAlgo_GlueDetector::DetectVertices()
118 Standard_Integer j, i, aNbV, aNbVSD;
121 TColStd_ListIteratorOfListOfInteger aIt;
123 TopTools_IndexedMapOfShape aMV;
124 TopTools_MapOfShape aMVProcessed;
125 TopTools_ListIteratorOfListOfShape aItS;
126 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
127 TopTools_DataMapOfShapeListOfShape aMVV;
128 GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
129 NMTDS_IndexedDataMapOfShapeBndSphere aMSB;
131 NMTDS_BndSphereTreeSelector aSelector;
132 NMTDS_BndSphereTree aBBTree;
133 NCollection_UBTreeFiller <Standard_Integer, NMTDS_BndSphere> aTreeFiller(aBBTree);
137 TopExp::MapShapes(myArgument, TopAbs_VERTEX, aMV);
140 myErrorStatus=2; // no vertices in source shape
144 for (i=1; i<=aNbV; ++i) {
145 NMTDS_BndSphere aBox;
147 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
148 aPV=BRep_Tool::Pnt(aV);
149 aTolV=BRep_Tool::Tolerance(aV);
151 aBox.SetGap(myTolerance);
153 aBox.SetRadius(aTolV);
155 aTreeFiller.Add(i, aBox);
163 //---------------------------------------------------
165 for (i=1; i<=aNbV; ++i) {
166 const TopoDS_Shape& aV=aMV(i);
168 if (aMVProcessed.Contains(aV)) {
172 Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
173 TopTools_ListOfShape aLVSD;
174 TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
175 TColStd_MapIteratorOfMapOfInteger aIt1;
180 aIt1.Initialize(aMIP);
181 for(; aIt1.More(); aIt1.Next()) {
183 if (aMIPC.Contains(aIP)) {
187 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
188 const NMTDS_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
191 aSelector.SetBox(aBoxVP);
193 aNbVSD=aBBTree.Select(aSelector);
195 continue; // it shoild not be so [at least IP itself]
198 const TColStd_ListOfInteger& aLI=aSelector.Indices();
200 for (; aIt.More(); aIt.Next()) {
202 if (aMIP.Contains(aIP1)) {
206 } //for (; aIt.More(); aIt.Next()) {
207 }//for(; aIt1.More(); aIt1.Next()) {
209 aNbIP1=aMIP1.Extent();
214 aIt1.Initialize(aMIP);
215 for(; aIt1.More(); aIt1.Next()) {
221 aIt1.Initialize(aMIP1);
222 for(; aIt1.More(); aIt1.Next()) {
230 aNbIP=aMIPC.Extent();
232 if (!aNbIP) {// no SD vertices is found
233 aMVProcessed.Add(aV);
236 //else { // SD vertices founded [ aMIPC ]
237 aIt1.Initialize(aMIPC);
238 for(j=0; aIt1.More(); aIt1.Next(), ++j) {
240 const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
245 aMVProcessed.Add(aVP);
248 myImages.Bind(aVF, aLVSD);
249 }// for (i=1; i<=aNbV; ++i) {
250 //------------------------------
252 aItIm.Initialize(myImages);
253 for (; aItIm.More(); aItIm.Next()) {
254 const TopoDS_Shape& aV=aItIm.Key();
255 const TopTools_ListOfShape& aLVSD=aItIm.Value();
256 aItS.Initialize(aLVSD);
257 for (; aItS.More(); aItS.Next()) {
258 const TopoDS_Shape& aVSD=aItS.Value();
259 if (!myOrigins.IsBound(aVSD)) {
260 myOrigins.Bind(aVSD, aV);
265 //=======================================================================
266 //function : DetectFaces
268 //=======================================================================
269 void GEOMAlgo_GlueDetector::DetectFaces()
271 DetectShapes(TopAbs_FACE);
273 //=======================================================================
274 //function : DetectEdges
276 //=======================================================================
277 void GEOMAlgo_GlueDetector::DetectEdges()
279 DetectShapes(TopAbs_EDGE);
281 //=======================================================================
282 //function : DetectShapes
284 //=======================================================================
285 void GEOMAlgo_GlueDetector::DetectShapes(const TopAbs_ShapeEnum aType)
287 Standard_Boolean bDegenerated;
288 Standard_Integer i, aNbF, aNbSDF, iErr;
289 TopTools_IndexedMapOfShape aMF;
290 TopTools_ListIteratorOfListOfShape aItLS;
291 GEOMAlgo_PassKeyShape aPKF;
292 GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
296 TopExp::MapShapes(myArgument, aType, aMF);
299 for (i=1; i<=aNbF; ++i) {
300 const TopoDS_Shape& aS=aMF(i);
302 if (aType==TopAbs_FACE) {
303 const TopoDS_Face& aF=*((TopoDS_Face*)&aS);
304 FacePassKey(aF, aPKF);
306 else if (aType==TopAbs_EDGE) {
307 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aS);
308 EdgePassKey(aE, aPKF);
315 if (aMPKLF.Contains(aPKF)) {
316 TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
320 TopTools_ListOfShape aLSDF;
323 aMPKLF.Add(aPKF, aLSDF);
326 // check geometric coincidence
327 if (myCheckGeometry) {
328 iErr=GEOMAlgo_Tools::RefineSDShapes(aMPKLF, myTolerance, myContext);
336 aNbF=aMPKLF.Extent();
337 for (i=1; i<=aNbF; ++i) {
338 const TopTools_ListOfShape& aLSDF=aMPKLF(i);
339 aNbSDF=aLSDF.Extent();
341 myErrorStatus=4; // it must not be
348 const TopoDS_Shape& aS1=aLSDF.First();
350 if (aType==TopAbs_EDGE) {
351 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
352 bDegenerated=BRep_Tool::Degenerated(aE1);
358 myImages.Bind(aS1, aLSDF);
360 aItLS.Initialize(aLSDF);
361 for (; aItLS.More(); aItLS.Next()) {
362 const TopoDS_Shape& aFSD=aItLS.Value();
363 if (!myOrigins.IsBound(aFSD)) {
364 myOrigins.Bind(aFSD, aS1);
367 }// for (i=1; i<=aNbF; ++i)
369 //=======================================================================
370 //function : FacePassKey
372 //=======================================================================
373 void GEOMAlgo_GlueDetector::FacePassKey(const TopoDS_Face& aF,
374 GEOMAlgo_PassKeyShape& aPK)
376 Standard_Integer i, aNbE;
378 TopTools_ListOfShape aLE;
379 TopTools_IndexedMapOfShape aME;
381 TopExp::MapShapes(aF, TopAbs_EDGE, aME);
384 for (i=1; i<=aNbE; ++i) {
385 const TopoDS_Shape& aE=aME(i);
387 const TopoDS_Edge& aEE=*((TopoDS_Edge*)&aE);
388 if (BRep_Tool::Degenerated(aEE)) {
392 if (myOrigins.IsBound(aE)) {
393 aER=myOrigins.Find(aE);
402 //=======================================================================
403 //function : EdgePassKey
405 //=======================================================================
406 void GEOMAlgo_GlueDetector::EdgePassKey(const TopoDS_Edge& aE,
407 GEOMAlgo_PassKeyShape& aPK)
409 TopAbs_Orientation aOr;
412 TopTools_ListOfShape aLV;
415 for (; aIt.More(); aIt.Next()) {
416 const TopoDS_Shape& aV=aIt.Value();
417 aOr=aV.Orientation();
418 if (aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED) {
419 if (myOrigins.IsBound(aV)) {
420 aVR=myOrigins.Find(aV);