summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepTool/TopOpeBRepTool_PurgeInternalEdges.cxx
blob: a437be703e3990df69d2a42715fa12cf46d8093c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// File:	TopOpeBRepTool_PurgeInternalEdges.cxx
// Created:	Thu Nov 19 15:23:38 1998
// Author:	Jean-Michel BOULCOURT
//		<jmb@coulox.paris1.matra-dtv.fr>


#include <TopOpeBRepTool_PurgeInternalEdges.ixx>

#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>

#include <TopTools_MapOfShape.hxx>

#include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>

#include <TopoDS.hxx>

#include <BRepTools_Substitution.hxx>

//=======================================================================
//function : TopOpeBRepTool_PurgeInternalEdges
//purpose  : Initializes some variables for the algorithm and perform
// the computation of the internal edges of the shape
//=======================================================================

TopOpeBRepTool_PurgeInternalEdges::TopOpeBRepTool_PurgeInternalEdges(const TopoDS_Shape& theShape,const Standard_Boolean PerformNow):myShape(theShape),myIsDone(Standard_False)
{
//  if (theShape.ShapeType() != TopAbs_SHELL && theShape.ShapeType() != TopAbs_SOLID)
//    Standard_ConstructionError::Raise("PurgeInternalEdges");
  Standard_NullObject_Raise_if(theShape.IsNull(),"PurgeInternalEdges");
  
  if (PerformNow) {
    Perform();
  }
}

//=======================================================================
//function : Faces
//purpose  : 
//=======================================================================

 void TopOpeBRepTool_PurgeInternalEdges::Faces(TopTools_DataMapOfShapeListOfShape& theMapFacLstEdg) 
{

  if (!myIsDone) {
    BuildList();
  }

  theMapFacLstEdg = myMapFacLstEdg;
}


//=======================================================================
//function : NbEdges
//purpose  : 
//=======================================================================

const Standard_Integer TopOpeBRepTool_PurgeInternalEdges::NbEdges() const
{

  Standard_NullObject_Raise_if(myShape.IsNull(),"PurgeInternalEdges : No Shape");
  Standard_Integer nbedges = 0;

  // if we have at least on internal (or external) edge to remove
  if (myMapFacLstEdg.Extent() > 0) {

    TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itFacEdg;
//    TopTools_ListIteratorOfListOfShape itEdg;

    for (itFacEdg.Initialize(myMapFacLstEdg); itFacEdg.More(); itFacEdg.Next()) {
      const TopoDS_Shape& facecur = itFacEdg.Key();
      const TopTools_ListOfShape& LmapEdg = myMapFacLstEdg.Find(facecur);
     
      nbedges += LmapEdg.Extent();      
    }
    
  }

  return nbedges;

}




//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================

TopoDS_Shape& TopOpeBRepTool_PurgeInternalEdges::Shape() 
{

  Standard_NullObject_Raise_if(myShape.IsNull(),"PurgeInternalEdges : No Shape");

  return myShape;

}

//=======================================================================
//function : BuildList
//purpose  : Build the map of faces with the list of inernal edges.
//=======================================================================

void TopOpeBRepTool_PurgeInternalEdges::BuildList()
{

  TopExp_Explorer ExpEdge;
  TopAbs_Orientation orien;
  

  //--------------------------------------------------------
  // Step One : Build the map of the faces ancestor of edges
  //--------------------------------------------------------
  
  // Clear the maps
  myMapEdgLstFac.Clear();
  
  TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgLstFac);
  
  
  //----------------------------------------------------------------
  // Step Two : Explore the map myMapFacLstEdg to keep only the internal
  // or external edges which have no connex faces
  //----------------------------------------------------------------
  
  
  Standard_Boolean ToKeep;
  Standard_Integer iEdg;
  TopTools_ListIteratorOfListOfShape itFac,itFacToTreat;
  TopTools_ListOfShape LstFacToTreat;
  
  // for each edge of myMapEdgLstFac
  for (iEdg = 1; iEdg <= myMapEdgLstFac.Extent(); iEdg++) {
    const TopoDS_Shape& edgecur = myMapEdgLstFac.FindKey(iEdg);
    const TopTools_ListOfShape& LmapFac = myMapEdgLstFac.FindFromKey(edgecur);
    
    
    // if there are more than on face connex to the edge then examine the orientation of
    // the edge in the faces to see it is only Internal or External. In that case the edge
    // can be removed

    itFac.Initialize(LmapFac);
    LstFacToTreat.Clear();

    if (LmapFac.Extent() > 1) {

      ToKeep = Standard_False;
      // for each face in the list of the edge
      while (itFac.More() && !ToKeep) {
	const TopoDS_Shape& facecur = itFac.Value();
	
	// find the edge in the face to get its relative orientation to the face
	for (ExpEdge.Init(facecur,TopAbs_EDGE); ExpEdge.More(); ExpEdge.Next()) {
	  const TopoDS_Shape& edge = ExpEdge.Current();
	  
	  orien = edge.Orientation();

	  if (edge.IsSame(edgecur)) {
	    // we found the edge. Now check if its orientation is internal or external
	    // then the face is candidate to be modified. So put it in a temporary List of shapes
	    if (orien == TopAbs_INTERNAL || orien == TopAbs_EXTERNAL) {
	      LstFacToTreat.Append(facecur);
	    }
	    else {
	      // there is at least one face that contains that edge with a forward
	      // or reversed orientation. So we must keep that edge.
	      LstFacToTreat.Clear();
	      ToKeep = Standard_True;
	    }
	    break;
	  }
	}
	
	itFac.Next();
      }
      
    }

    else {
       if (edgecur.Orientation() == TopAbs_INTERNAL || edgecur.Orientation() == TopAbs_EXTERNAL) {
	 LstFacToTreat.Append(itFac.Value());
       }
    }
    
    // at that point, we have in the list LstFacToTreat the faces in which edgecur is
    // connex and is coded only Internal or External.
    if (!LstFacToTreat.IsEmpty()) {
      TopTools_MapOfShape mapUniqEdg;
      for (itFacToTreat.Initialize(LstFacToTreat); itFacToTreat.More(); itFacToTreat.Next()) {
	const TopoDS_Shape& face = itFacToTreat.Value();
	
	//we build the resulting map with a face as a key and a list of internal (or external)
	// edges to remove from the face.
	if (!myMapFacLstEdg.IsBound(face)) {
	  TopTools_ListOfShape LmapEdg;
	  if (!mapUniqEdg.Contains(edgecur)) {
	    mapUniqEdg.Add(edgecur);
	    LmapEdg.Append(edgecur);
	    myMapFacLstEdg.Bind(face,LmapEdg);
	  }
	}
	else { // just append the edge to the list of edges of the face
	  TopTools_ListOfShape& LmapEdg = myMapFacLstEdg.ChangeFind(face);
	  if (!mapUniqEdg.Contains(edgecur)) {
	    mapUniqEdg.Add(edgecur);
	    LmapEdg.Append(edgecur);
	  }
	} 
	
      }
    }
    
  } 

  myIsDone = Standard_True;

}



//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

 void TopOpeBRepTool_PurgeInternalEdges::Perform() 
{

  // if the list has not been yet built then do it
  if (!myIsDone) {
    BuildList();
  }

  // if we have at least on internal (or external) edge to remove
  if (myMapFacLstEdg.Extent() > 0) {

    TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itFacEdg;
    TopTools_ListIteratorOfListOfShape itEdg;
    TopTools_ListOfShape EmptyList;
    BRepTools_Substitution Bsub;

    for (itFacEdg.Initialize(myMapFacLstEdg); itFacEdg.More(); itFacEdg.Next()) {
      const TopoDS_Shape& facecur = itFacEdg.Key();
      const TopTools_ListOfShape& LmapEdg = myMapFacLstEdg.Find(facecur);
      
      for (itEdg.Initialize(LmapEdg); itEdg.More(); itEdg.Next()) {
	Bsub.Substitute(itEdg.Value(),EmptyList);
      }
    }

    Bsub.Build(myShape);
    if (Bsub.IsCopied(myShape)) {
      myShape=(Bsub.Copy(myShape)).First();
    }
  }
}