summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_ShellSolidHistoryCollector.cxx
blob: ffe1aa64067d0aa2fc0f273317edae6587df3e27 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <BOP_ShellSolidHistoryCollector.ixx>

#include <TopAbs.hxx>
#include <TopExp_Explorer.hxx>
#include <BooleanOperations_ShapesDataStructure.hxx>
#include <BooleanOperations_IndexedDataMapOfShapeInteger.hxx>
#include <BOPTools_SSInterference.hxx>
#include <BOPTools_InterferencePool.hxx>
#include <BOPTools_CArray1OfSSInterference.hxx>
#include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
#include <BOPTools_Curve.hxx>
#include <BOPTools_PaveBlock.hxx>
#include <BOPTools_DSFiller.hxx>
#include <BOPTools_PaveFiller.hxx>
#include <BOPTools_CommonBlock.hxx>
#include <BOPTools_ListOfCommonBlock.hxx>
#include <BOPTools_ListIteratorOfListOfCommonBlock.hxx>
#include <BOPTools_ListIteratorOfListOfPave.hxx>
#include <BOP_BuilderTools.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopExp.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>

// ====================================================================================================
// function: Constructor
// purpose:
// ====================================================================================================
BOP_ShellSolidHistoryCollector::BOP_ShellSolidHistoryCollector(const TopoDS_Shape& theShape1,
							       const TopoDS_Shape& theShape2,
							       const BOP_Operation theOperation) :
BOP_HistoryCollector(theShape1, theShape2, theOperation)
{
}

// ====================================================================================================
// function: AddNewFace
// purpose:
// ====================================================================================================
void BOP_ShellSolidHistoryCollector::AddNewFace(const TopoDS_Shape&       theOldShape,
						const TopoDS_Shape&       theNewShape,
						const BOPTools_PDSFiller& theDSFiller) 
{
  Standard_Boolean bAddAsGenerated = Standard_True;

  if(theOldShape.ShapeType() != theNewShape.ShapeType())
    return;

  const BooleanOperations_IndexedDataMapOfShapeInteger& aMap = theDSFiller->DS().ShapeIndexMap(1);
  Standard_Boolean bIsObject = (myS1.ShapeType() == TopAbs_SHELL) ? aMap.Contains(theOldShape) : !aMap.Contains(theOldShape);

  if(bIsObject) {
    bAddAsGenerated = Standard_False;
  }

  TopTools_DataMapOfShapeListOfShape& aHistoryMap = (bAddAsGenerated) ? myGenMap : myModifMap;

  if(aHistoryMap.IsBound(theOldShape)) {
    aHistoryMap.ChangeFind(theOldShape).Append(theNewShape);
  }
  else {
    TopTools_ListOfShape aShapeList;
    aShapeList.Append(theNewShape);
    aHistoryMap.Bind(theOldShape, aShapeList);
  }
}

// ====================================================================================================
// function: SetResult
// purpose:
// ====================================================================================================
void BOP_ShellSolidHistoryCollector::SetResult(const TopoDS_Shape&       theResult,
					       const BOPTools_PDSFiller& theDSFiller) 
{
  myResult = theResult;
  FillSection(theDSFiller);
  FillEdgeHistory(theDSFiller);

  myHasDeleted = Standard_False;
  Standard_Integer i = 0;

  TopTools_IndexedMapOfShape aFreeBoundaryMap;
  TopTools_IndexedDataMapOfShapeListOfShape aEFMap;

  if(myS1.ShapeType() == TopAbs_SHELL) {
    TopExp::MapShapesAndAncestors(myS1, TopAbs_EDGE, TopAbs_FACE, aEFMap);
  }
  else {
    TopExp::MapShapesAndAncestors(myS2, TopAbs_EDGE, TopAbs_FACE, aEFMap);
  }

  for(i = 1; i <= aEFMap.Extent(); i++) {
    if(aEFMap.FindFromIndex(i).Extent() < 2)
      aFreeBoundaryMap.Add(aEFMap.FindKey(i));
  }
  aEFMap.Clear();
  
  for(i = (Standard_Integer)TopAbs_FACE; !myHasDeleted && (i < (Standard_Integer)TopAbs_EDGE); i++) {
    if(i == (Standard_Integer)TopAbs_WIRE)
      continue;

    TopAbs_ShapeEnum aType = (TopAbs_ShapeEnum) i;
    TopTools_IndexedMapOfShape aMap;
    TopExp::MapShapes(myResult, aType, aMap);

    TopExp_Explorer anExpObj(myS1, aType);

    for(; anExpObj.More(); anExpObj.Next()) {
      const TopoDS_Shape& aS = anExpObj.Current();

      if((i == (Standard_Integer)TopAbs_EDGE) && !aFreeBoundaryMap.Contains(aS))
	continue;

      if(!aMap.Contains(aS)) {

	if((!myModifMap.IsBound(aS) || myModifMap(aS).IsEmpty()) &&
	   (!myGenMap.IsBound(aS) || myGenMap(aS).IsEmpty())) {
	  myHasDeleted = Standard_True;
	  break;
	}
      }
    }
    TopExp_Explorer anExpTool(myS2, aType);

    for(; anExpTool.More(); anExpTool.Next()) {
      const TopoDS_Shape& aS = anExpTool.Current();

      if(!aMap.Contains(aS)) {
	if((!myModifMap.IsBound(aS) || myModifMap(aS).IsEmpty()) &&
	   (!myGenMap.IsBound(aS) || myGenMap(aS).IsEmpty())) {
	  myHasDeleted = Standard_True;
	  break;
	}
      }
    }
  }
}

// ====================================================================================================
// function: FillSection
// purpose:
// ====================================================================================================
void BOP_ShellSolidHistoryCollector::FillSection(const BOPTools_PDSFiller& theDSFiller) 
{
  BOPTools_InterferencePool* pIntrPool = (BOPTools_InterferencePool*)&theDSFiller->InterfPool();
  BOPTools_CArray1OfSSInterference& aFFs = pIntrPool->SSInterferences();
  Standard_Integer i = 0;
  TopTools_IndexedDataMapOfShapeListOfShape aResultMap;

  if(!myResult.IsNull()) {
    TopExp::MapShapesAndAncestors(myResult, TopAbs_EDGE, TopAbs_FACE, aResultMap);
  }

  for(i = 1; i <= aFFs.Extent(); i++) {
    BOPTools_SSInterference& aFF = aFFs(i);
    Standard_Integer nF1 = aFF.Index1();
    Standard_Integer nF2 = aFF.Index2();

    if(aFF.IsTangentFaces())
      continue;
    TopoDS_Shape aF1 = theDSFiller->DS().Shape(nF1);
    TopoDS_Shape aF2 = theDSFiller->DS().Shape(nF2);


    BOPTools_SequenceOfCurves& aSeqOfCurves = aFF.Curves();
    Standard_Integer j = 0;

    for(j = 1; j <= aSeqOfCurves.Length(); j++) {
      const BOPTools_Curve& aCurve = aSeqOfCurves.Value(j);

      const BOPTools_ListOfPaveBlock& aPBList = aCurve.NewPaveBlocks();
      BOPTools_ListIteratorOfListOfPaveBlock anIt(aPBList);

      for (; anIt.More();anIt.Next()) {
	const BOPTools_PaveBlock& aPB = anIt.Value();
	Standard_Integer anindex = aPB.Edge();
	const TopoDS_Shape& aS = theDSFiller->DS().GetShape(anindex);

	if(aResultMap.Contains(aS)) {
          TopTools_ListOfShape thelist;
	  if(!myGenMap.IsBound(aF1))
	    myGenMap.Bind(aF1, thelist);
	  myGenMap.ChangeFind(aF1).Append(aS);

	  if(!myGenMap.IsBound(aF2))
	    myGenMap.Bind(aF2, thelist);
	  myGenMap.ChangeFind(aF2).Append(aS);
	}
      }
    }

    // Old Section Edges
    const BOPTools_ListOfPaveBlock& aSectList = aFF.PaveBlocks();
    BOPTools_ListIteratorOfListOfPaveBlock anIt(aSectList);

    for (; anIt.More();anIt.Next()) {
      const BOPTools_PaveBlock& aPB=anIt.Value();
      Standard_Integer nSect = aPB.Edge();
      const TopoDS_Shape& aS = theDSFiller->DS().GetShape(nSect);

      if(aResultMap.Contains(aS)) {
	if(aResultMap.FindFromKey(aS).Extent() >= 2)
	  continue;

	Standard_Boolean add1 = Standard_True;	
        TopTools_ListOfShape thelist1;
	if(!myGenMap.IsBound(aF1))
	  myGenMap.Bind(aF1, thelist1);
	else {
	  TopTools_ListOfShape& aList = myGenMap.ChangeFind(aF1);
	  TopTools_ListIteratorOfListOfShape anItF1(aList);

	  for(; anItF1.More(); anItF1.Next()) {
	    if(aS.IsSame(anItF1.Value())) {
	      add1 = Standard_False;
	      break;
	    }
	  }
	}

	if(add1) {
	  myGenMap.ChangeFind(aF1).Append(aS);
	}
	Standard_Boolean add2 = Standard_True;	
        TopTools_ListOfShape thelist2;
	if(!myGenMap.IsBound(aF2))
	  myGenMap.Bind(aF2, thelist2);
	else {
	  TopTools_ListOfShape& aList = myGenMap.ChangeFind(aF2);
	  TopTools_ListIteratorOfListOfShape anItF2(aList);

	  for(; anItF2.More(); anItF2.Next()) {
	    if(aS.IsSame(anItF2.Value())) {
	      add2 = Standard_False;
	      break;
	    }
	  }
	}

	if(add2) {
	  myGenMap.ChangeFind(aF2).Append(aS);
	}
      }
    }
  }
  aResultMap.Clear();
  TopTools_IndexedMapOfShape aResultMapV;

  if(!myResult.IsNull()) {
    TopExp::MapShapes(myResult, TopAbs_VERTEX, aResultMapV);
  }

  for(i = 1; i <= aFFs.Extent(); i++) {
    BOPTools_SSInterference& aFF = aFFs(i);
    Standard_Integer nF1 = aFF.Index1();
    Standard_Integer nF2 = aFF.Index2();

    if(aFF.IsTangentFaces())
      continue;
    TopoDS_Shape aF1 = theDSFiller->DS().Shape(nF1);
    TopoDS_Shape aF2 = theDSFiller->DS().Shape(nF2);

    TColStd_ListIteratorOfListOfInteger anIt(aFF.AloneVertices());

    for(; anIt.More(); anIt.Next()) {
      TopoDS_Shape aNewVertex = theDSFiller->DS().Shape(anIt.Value());

      if(aResultMapV.Contains(aNewVertex)) {
        TopTools_ListOfShape thelist3;
	if(!myGenMap.IsBound(aF1))
	  myGenMap.Bind(aF1, thelist3);
	myGenMap.ChangeFind(aF1).Append(aNewVertex);

	if(!myGenMap.IsBound(aF2))
	  myGenMap.Bind(aF2, thelist3);
	myGenMap.ChangeFind(aF2).Append(aNewVertex);
      }
    }
  }
}

// ====================================================================================================
// function: FillEdgeHistory
// purpose:  
// ====================================================================================================
void BOP_ShellSolidHistoryCollector::FillEdgeHistory(const BOPTools_PDSFiller& theDSFiller) 
{
  const BooleanOperations_ShapesDataStructure& aDS = theDSFiller->DS();
  const BOPTools_PaveFiller& aPaveFiller           = theDSFiller->PaveFiller();
  const BOPTools_SplitShapesPool& aSplitShapesPool = aPaveFiller.SplitShapesPool();
  TopTools_IndexedMapOfShape aResultMap, aFreeBoundaryMap;

  if(!myResult.IsNull()) {
    TopExp::MapShapes(myResult, TopAbs_EDGE, aResultMap);

    TopTools_IndexedDataMapOfShapeListOfShape aEFMap;

    if(myS1.ShapeType() == TopAbs_SHELL) {
      TopExp::MapShapesAndAncestors(myS1, TopAbs_EDGE, TopAbs_FACE, aEFMap);
    }
    else {
      TopExp::MapShapesAndAncestors(myS2, TopAbs_EDGE, TopAbs_FACE, aEFMap);
    }
    Standard_Integer i = 0;

    for(i = 1; i <= aEFMap.Extent(); i++) {
      if(aEFMap.FindFromIndex(i).Extent() < 2)
	aFreeBoundaryMap.Add(aEFMap.FindKey(i));
    }
  }
  
  Standard_Integer iRank = 1;

  for(; iRank <= 2; iRank++) {
    BooleanOperations_StateOfShape aStateCmp, aState;
    aStateCmp = BOP_BuilderTools::StateToCompare(iRank, myOp);
    Standard_Integer i = 0;
    Standard_Integer nb = aDS.NumberOfShapesOfTheObject();
    nb = (iRank == 1) ? nb : (nb + aDS.NumberOfShapesOfTheTool());
    Standard_Integer startindex = (iRank == 1) ? 1 : (aDS.NumberOfShapesOfTheObject() + 1);

    for(i = startindex; i <= nb; i++) {
      if(aDS.GetShapeType(i) != TopAbs_EDGE)
	continue;
      const BOPTools_ListOfPaveBlock& aLPB = aSplitShapesPool(aDS.RefEdge(i));
      const TopoDS_Shape& anOldShape = aDS.Shape(i);

      if(!aFreeBoundaryMap.Contains(anOldShape)) {
	continue;
      }

      if(!aLPB.IsEmpty()) {
	BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aLPB);

	for(; aPBIt.More(); aPBIt.Next()) {
	  const BOPTools_PaveBlock& aPB = aPBIt.Value();
	  Standard_Integer nSp = aPB.Edge();

	  if(nSp == i)
	    continue;
	  aState=aDS.GetState(nSp);

	  if (aState == aStateCmp) {
	    const TopoDS_Shape& aNewShape = aDS.Shape(nSp);

	    if(aResultMap.Contains(aNewShape)) {
	      TopTools_DataMapOfShapeListOfShape& aHistoryMap = myModifMap;

	      if(aHistoryMap.IsBound(anOldShape)) {
		aHistoryMap.ChangeFind(anOldShape).Append(aNewShape);
	      }
	      else {
		TopTools_ListOfShape aShapeList;
		aShapeList.Append(aNewShape);
		aHistoryMap.Bind(anOldShape, aShapeList);
	      }
	    }
	  }
	}
      }
      //end if(!aLPB.IsEmpty...

      const BOPTools_CommonBlockPool& aCBPool = aPaveFiller.CommonBlockPool();
      const BOPTools_ListOfCommonBlock& aLCB = aCBPool(aDS.RefEdge(i));
      BOPTools_ListIteratorOfListOfCommonBlock anItCB(aLCB);

      for (; anItCB.More(); anItCB.Next()) {
	const BOPTools_CommonBlock& aCB = anItCB.Value();
	const BOPTools_PaveBlock& aPB = aCB.PaveBlock1();
	Standard_Integer nSp = aPB.Edge();
	TopoDS_Shape aNewShape = aDS.Shape(nSp);

	if(aResultMap.Contains(aNewShape)) {
	  TopTools_DataMapOfShapeListOfShape& aHistoryMap = myModifMap;

	  if(aHistoryMap.IsBound(anOldShape)) {
	    aHistoryMap.ChangeFind(anOldShape).Append(aNewShape);
	  }
	  else {
	    TopTools_ListOfShape aShapeList;
	    aShapeList.Append(aNewShape);
	    aHistoryMap.Bind(anOldShape, aShapeList);
	  }
	}
      }
      // end for (; anItCB.More...
    }
  }
}