summaryrefslogtreecommitdiff
path: root/src/BOPTools/BOPTools_RoughShapeIntersector.cxx
blob: fd2d36da2e835f7051d367539fda48d2eb82ef57 (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
#include <BOPTools_RoughShapeIntersector.ixx>

#include <TopExp_Explorer.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>
#include <TColStd_Array1OfListOfInteger.hxx>
#include <BooleanOperations_ShapesDataStructure.hxx>
#include <Bnd_Box.hxx>

static Standard_Integer TypeToIndex(const TopAbs_ShapeEnum& theType);

static Bnd_Box GetBoxEnlargedBySubShape(const Standard_Integer                        theIndex,
					const BooleanOperations_PShapesDataStructure& PDS);


// =============================================================================================
// function: Constructor
// =============================================================================================
  BOPTools_RoughShapeIntersector::BOPTools_RoughShapeIntersector(const BooleanOperations_PShapesDataStructure& PDS)
:
  myIsDone(Standard_False)
{
  myPDS = PDS;
}

// =============================================================================================
// function: Perform
// =============================================================================================
  void BOPTools_RoughShapeIntersector::Perform() 
{
  myIsDone = Standard_True;

  Prepare();

  // check flag myIsDone after Prepare() function
  if(!myIsDone)
    return;
  TColStd_ListOfInteger thelist;
  TColStd_Array1OfListOfInteger aSortedByTypeShapes1(TypeToIndex(TopAbs_COMPOUND), TypeToIndex(TopAbs_VERTEX));
  aSortedByTypeShapes1.Init(thelist);

  TColStd_ListOfInteger thelist1;
  TColStd_Array1OfListOfInteger aSortedByTypeShapes2(TypeToIndex(TopAbs_COMPOUND), TypeToIndex(TopAbs_VERTEX));
  aSortedByTypeShapes2.Init(thelist1);

  Standard_Integer k = 0;

  for(k = myTableOfStatus->LowerRow(); k <= myTableOfStatus->UpperRow(); k++) {
    aSortedByTypeShapes1(TypeToIndex(myPDS->GetShapeType(k))).Append(k);
  }

  for(k = myTableOfStatus->LowerCol(); k <= myTableOfStatus->UpperCol(); k++) {
    aSortedByTypeShapes2(TypeToIndex(myPDS->GetShapeType(k))).Append(k);
  }

  // 
  Standard_Integer aStartIndex = TypeToIndex(TopAbs_COMPOUND);
  Standard_Integer anEndIndex = TypeToIndex(TopAbs_VERTEX);

  for(Standard_Integer index1 = aStartIndex; index1 <= anEndIndex; index1++) {
  // 
    if(aSortedByTypeShapes1(index1).IsEmpty())
	continue;

    for(Standard_Integer index2 = aStartIndex; index2 <= anEndIndex; index2++) {
      if(aSortedByTypeShapes2(index2).IsEmpty())
	continue;

      TColStd_ListIteratorOfListOfInteger anIt1;
      TColStd_ListIteratorOfListOfInteger anIt2;
	
      for(anIt1.Initialize(aSortedByTypeShapes1.Value(index1)); anIt1.More(); anIt1.Next()) {
	for(anIt2.Initialize(aSortedByTypeShapes2.Value(index2)); anIt2.More(); anIt2.Next()) {
    
	  // 
	  BOPTools_IntersectionStatus aStatus = myTableOfStatus->Value(anIt1.Value(), anIt2.Value());

	  if(aStatus != BOPTools_UNKNOWN && aStatus != BOPTools_BOUNDINGBOXOFSUBSHAPESINTERSECTED)
	    continue;

	  const Bnd_Box& B1 = myBoundingBoxes->Value(anIt1.Value());
	  const Bnd_Box& B2 = myBoundingBoxes->Value(anIt2.Value());
	  // 
	  BOPTools_IntersectionStatus aNewValue = BOPTools_BOUNDINGBOXINTERSECTED;

	  if(B1.IsOut(B2)) {
	    aNewValue = BOPTools_NONINTERSECTED;
	    // 
	    PropagateForSuccessors1(anIt1.Value(), anIt2.Value(), aNewValue);
	    //
	  }
	  else {
	    aNewValue = BOPTools_BOUNDINGBOXINTERSECTED;
	    const Bnd_Box& BB1 = myPDS->GetBoundingBox(anIt1.Value());
	    const Bnd_Box& BB2 = myPDS->GetBoundingBox(anIt2.Value());

	    if(BB1.IsOut(BB2)) {
	      aNewValue = BOPTools_BOUNDINGBOXOFSUBSHAPESINTERSECTED;
	    }
	  }
	  // It is important to place this line after Propagation
	  myTableOfStatus->ChangeValue(anIt1.Value(), anIt2.Value()) = aNewValue;
	}
      }
    }
  }
}

// =============================================================================================
// function: TableOfStatus
// =============================================================================================
const Handle(BOPTools_HArray2OfIntersectionStatus)& BOPTools_RoughShapeIntersector::TableOfStatus() const
{
  return myTableOfStatus;
}

// =============================================================================================
// function: Prepare
// =============================================================================================
void BOPTools_RoughShapeIntersector::Prepare() 
{
  myIsDone = Standard_False;

  if(!myTableOfStatus.IsNull() &&
     (myPDS != NULL) &&
     (myTableOfStatus->LowerRow() == 1) &&
     (myTableOfStatus->UpperRow() == myPDS->NumberOfShapesOfTheObject()) &&
     (myTableOfStatus->LowerCol() == myPDS->NumberOfShapesOfTheObject() + 1) &&
     (myTableOfStatus->UpperCol() == myPDS->NumberOfShapesOfTheObject() + myPDS->NumberOfShapesOfTheTool())) {
    myIsDone = Standard_True;
  }
  else { 
    if(myPDS != NULL) {
      Standard_Integer firstlowerindex = 1;
      Standard_Integer firstupperindex = myPDS->NumberOfShapesOfTheObject();
      Standard_Integer secondlowerindex = myPDS->NumberOfShapesOfTheObject() + 1;
      Standard_Integer secondupperindex = myPDS->NumberOfShapesOfTheTool() + myPDS->NumberOfShapesOfTheObject();
      myTableOfStatus = new BOPTools_HArray2OfIntersectionStatus(firstlowerindex, firstupperindex, 
								 secondlowerindex, secondupperindex);
      myIsDone = Standard_True;
    }
  }
  if(!myIsDone)
    return;

  myTableOfStatus->Init(BOPTools_UNKNOWN);  
  // 
  myBoundingBoxes = new Bnd_HArray1OfBox(1, myPDS->NumberOfSourceShapes());

  for(Standard_Integer i = 1; i <= myPDS->NumberOfSourceShapes(); i++) {    
    const Bnd_Box& aBox = GetBoxEnlargedBySubShape(i, myPDS);
    myBoundingBoxes->SetValue(i, aBox);
  }
}

// =============================================================================================
// function: IsDone
// =============================================================================================
  Standard_Boolean BOPTools_RoughShapeIntersector::IsDone() const
{
  return myIsDone;
}

// =============================================================================================
// function: PropagateForSuccessors1
// =============================================================================================
void BOPTools_RoughShapeIntersector::PropagateForSuccessors1(const Standard_Integer            AncestorsIndex1,
							     const Standard_Integer            AncestorsIndex2,
							     const BOPTools_IntersectionStatus theStatus) 
{
  if(myTableOfStatus->Value(AncestorsIndex1, AncestorsIndex2) != BOPTools_UNKNOWN)
    return;

  myTableOfStatus->ChangeValue(AncestorsIndex1, AncestorsIndex2) = theStatus;
  
  PropagateForSuccessors2(AncestorsIndex1, AncestorsIndex2, theStatus);
  Standard_Integer i = 1;

  for(i = 1; i <= myPDS->NumberOfSuccessors(AncestorsIndex1); i++) {
    Standard_Integer asuccessor1 = myPDS->GetSuccessor(AncestorsIndex1, i);
    PropagateForSuccessors1(asuccessor1, AncestorsIndex2, theStatus);
  }
}

// =============================================================================================
// function: PropagateForSuccessors2
// =============================================================================================
void BOPTools_RoughShapeIntersector::PropagateForSuccessors2(const Standard_Integer            AncestorsIndex1,
							     const Standard_Integer            AncestorsIndex2,
							     const BOPTools_IntersectionStatus theStatus) 
{
  for(Standard_Integer j = 1; j <= myPDS->NumberOfSuccessors(AncestorsIndex2); j++) {
    Standard_Integer asuccessor2 = myPDS->GetSuccessor(AncestorsIndex2, j);

    if(myTableOfStatus->Value(AncestorsIndex1, asuccessor2) == BOPTools_UNKNOWN) {
      myTableOfStatus->ChangeValue(AncestorsIndex1, asuccessor2) = theStatus;

      PropagateForSuccessors2(AncestorsIndex1, asuccessor2, theStatus);
    }
  }
}

// =============================================================================================
// function: TypeToIndex
// =============================================================================================
 Standard_Integer TypeToIndex(const TopAbs_ShapeEnum& theType) {
  return (Standard_Integer) theType;
}

// =============================================================================================
// function: GetBoxEnlargedBySubShape
// =============================================================================================
Bnd_Box GetBoxEnlargedBySubShape(const Standard_Integer                        theIndex,
				 const BooleanOperations_PShapesDataStructure& PDS) {
  Bnd_Box aBox = PDS->GetBoundingBox(theIndex);
  
  for(Standard_Integer i=1; i<= PDS->NumberOfSuccessors(theIndex); i++) {
    Standard_Integer asuccessorindex = PDS->GetSuccessor(theIndex, i);
    aBox.Add(GetBoxEnlargedBySubShape(asuccessorindex, PDS));
  }
  return aBox;
}