summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_RemoveInternalWires.cxx
blob: dd38392859794690b21ac30111cdb9974777a1d2 (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
265
266
#include <ShapeUpgrade_RemoveInternalWires.ixx>
#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>  
#include <TopoDS_Iterator.hxx>
#include <TopoDS.hxx>
#include <ShapeAnalysis.hxx>
#include <ShapeExtend.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <ShapeExtend_WireData.hxx>
#include <ShapeBuild_ReShape.hxx>
#include <Precision.hxx>
#include <TopoDS_Edge.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
  
//=======================================================================
//function : ShapeUpgrade_RemoveInternalWires
//purpose  : 
//=======================================================================

ShapeUpgrade_RemoveInternalWires::ShapeUpgrade_RemoveInternalWires()
{
  myMinArea =0.;
  myRemoveFacesMode = Standard_True;
  myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
  Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
  SetContext(aContext);
  
}

//=======================================================================
//function : ShapeUpgrade_RemoveInternalWires
//purpose  : 
//=======================================================================

ShapeUpgrade_RemoveInternalWires::ShapeUpgrade_RemoveInternalWires(const TopoDS_Shape& theShape)
{
  Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
  SetContext(aContext);
  Init(theShape);
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

 void ShapeUpgrade_RemoveInternalWires::Init(const TopoDS_Shape& theShape) 
{
  myShape = theShape;
  Context()->Apply(theShape);
  TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,myEdgeFaces);
  myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
  myMinArea =0.;
  myRemoveFacesMode = Standard_True;
}

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

 Standard_Boolean ShapeUpgrade_RemoveInternalWires::Perform() 
{
  Clear();
  if(myShape.IsNull()) {
    myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
    return Standard_False;
  }
  TopExp_Explorer aExpF(myShape,TopAbs_FACE);
  for( ; aExpF.More(); aExpF.Next()) {
    TopoDS_Face aF = TopoDS::Face(aExpF.Current());
    removeSmallWire(aF,TopoDS_Wire());
  }
  if(myRemoveFacesMode)
    removeSmallFaces();
  
  myResult = Context()->Apply(myShape);
  return Status( ShapeExtend_DONE );
}
 
//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

 Standard_Boolean ShapeUpgrade_RemoveInternalWires::Perform(const TopTools_SequenceOfShape& theSeqShapes) 
{
  if(myShape.IsNull()) {
    myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
    return Standard_False;
  }
  Clear();
  TopTools_IndexedDataMapOfShapeListOfShape aWireFaces;
  Standard_Integer i =1, nb = theSeqShapes.Length();
  for( ; i <= nb; i++) {
    TopoDS_Shape aS = theSeqShapes.Value(i);
    if(aS.ShapeType() == TopAbs_FACE)
      removeSmallWire(aS,TopoDS_Wire());
    else if(aS.ShapeType() == TopAbs_WIRE) {
      if(!aWireFaces.Extent())
        TopExp::MapShapesAndAncestors(myShape,TopAbs_WIRE,TopAbs_FACE,aWireFaces);
      if(aWireFaces.Contains(aS)) {
        const TopTools_ListOfShape& alfaces = aWireFaces.FindFromKey(aS);
        TopTools_ListIteratorOfListOfShape liter(alfaces);
        for( ; liter.More(); liter.Next())
          removeSmallWire(liter.Value(),aS);
      }
        
    }
    
  }
  if(myRemoveFacesMode)
      removeSmallFaces();
  myResult = Context()->Apply(myShape);
  return Status( ShapeExtend_DONE );
}

//=======================================================================
//function : removeSmallWire
//purpose  : 
//=======================================================================

void ShapeUpgrade_RemoveInternalWires::removeSmallWire (const TopoDS_Shape& theFace,
                                                        const TopoDS_Shape& theWire) 
{
  TopoDS_Face aF = TopoDS::Face(theFace);
  TopoDS_Wire anOutW = ShapeAnalysis::OuterWire(aF);
  TopoDS_Iterator aIt(aF);
  for( ; aIt.More(); aIt.Next()) {
    if(aIt.Value().ShapeType() != TopAbs_WIRE || aIt.Value().IsSame(anOutW))
      continue;
    //Handle(ShapeExtend_WireData) asewd = new  ShapeExtend_WireData();
    TopoDS_Wire aW = TopoDS::Wire(aIt.Value());
    if(!theWire.IsNull() && !theWire.IsSame(aW))
      continue;
    Standard_Real anArea = ShapeAnalysis::ContourArea(aW);
    if(anArea < myMinArea -Precision::Confusion()) {
      Context()->Remove(aW);
      myRemoveWires.Append(aW);
      myStatus |= ShapeExtend::EncodeStatus(ShapeExtend_DONE1);
      if(!myRemoveFacesMode )
        continue;
      
      TopoDS_Iterator aIte(aW,Standard_False);
      for( ; aIte.More(); aIte.Next()) {
        TopoDS_Shape aE = aIte.Value();
        if(myRemoveEdges.IsBound(aE))
          myRemoveEdges.ChangeFind(aE).Append(aF);
        else {
          TopTools_ListOfShape alfaces;
          alfaces.Append(aF);
          myRemoveEdges.Bind(aE,alfaces);
        }
      }
    }
    
  }
}

//=======================================================================
//function : removeSmallFaces
//purpose  : 
//=======================================================================

void ShapeUpgrade_RemoveInternalWires::removeSmallFaces ()
{
  
  Standard_Integer i =1;
  for( ; i <= myRemoveWires.Length() ; i++) {
    TopoDS_Shape aWire = myRemoveWires.Value(i);
    TopoDS_Iterator aIte(aWire,Standard_False);
    TopTools_IndexedMapOfShape aFaceCandidates;
    //collecting all faces containing edges from removed wire
    for( ; aIte.More(); aIte.Next()) {
      
      TopoDS_Shape aEdge =  aIte.Value();
      if(!myEdgeFaces.Contains(aEdge)) {
        myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
        continue;
      }
      const TopTools_ListOfShape& aLface1 = myEdgeFaces.FindFromKey(aEdge);
      const TopTools_ListOfShape& aLface2 = myRemoveEdges.Find(aEdge);
      TopTools_ListIteratorOfListOfShape aliter(aLface1);
      TopTools_ListIteratorOfListOfShape aliter2(aLface2);
      for( ; aliter.More(); aliter.Next()) {
        TopoDS_Shape aF = Context()->Apply(aliter.Value());
        if(aF.IsNull())
          continue;
        Standard_Boolean isFind = Standard_False;
        for( ; aliter2.More() && !isFind; aliter2.Next()) {
          TopoDS_Shape aF2 = Context()->Apply(aliter2.Value());
          isFind = aF.IsSame(aF2);
        }
        
        if(!isFind) {
          TopoDS_Wire aWout = ShapeAnalysis::OuterWire(TopoDS::Face(aF));
          Standard_Boolean isOuter = Standard_False;
          TopoDS_Iterator aIter(aWout,Standard_False);
          for( ; aIter.More() && !isOuter; aIter.Next())
            isOuter = aEdge.IsSame(aIter.Value());
          if(isOuter)
            aFaceCandidates.Add(aF);
        }
      }
      
    }
    
    //remove faces which have outer wire consist of only
    //edges from removed wires and
    //seam edges for faces based on conic surface or 
    //in the case of a few faces based on the same conic surface
    //the edges belogining these faces.
    Standard_Integer k =1;
    for( ; k <= aFaceCandidates.Extent(); k++) {
      TopoDS_Shape aF = aFaceCandidates.FindKey(k);
      TopoDS_Wire anOutW = ShapeAnalysis::OuterWire(TopoDS::Face(aF));
      Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(anOutW);
      Standard_Integer n =1, nbE = asewd->NbEdges();
      Standard_Integer nbNotRemoved =0;
      for( ; n <= nbE; n++) {
        if(asewd->IsSeam(n))
          continue;
        TopoDS_Edge aE = asewd->Edge(n);
        if(!myRemoveEdges.IsBound(aE) ) {
          const TopTools_ListOfShape& aLface3 = myEdgeFaces.FindFromKey(aE);
          TopTools_ListIteratorOfListOfShape aliter3(aLface3);
          for( ; aliter3.More();aliter3.Next()) {
            TopoDS_Shape aF2 = Context()->Apply(aliter3.Value());
            if(aF2.IsNull())
              continue;
            if(!aF.IsSame(aF2) && !aFaceCandidates.Contains(aF2))
              nbNotRemoved++;
          }
        }
        
      }
    
      if(!nbNotRemoved) {
        Context()->Remove(aF);
        myRemovedFaces.Append(aF);
      }
    }
  }
  
  if(myRemovedFaces.Length())
    myStatus |= ShapeExtend::EncodeStatus(ShapeExtend_DONE2);
}

//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void ShapeUpgrade_RemoveInternalWires::Clear()
{
  myRemoveEdges.Clear();
  myRemovedFaces.Clear();
  myRemoveWires.Clear();
  myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
}