summaryrefslogtreecommitdiff
path: root/src/IntPolyh/IntPolyh_Intersection_1.cxx
blob: 8356c3491ecadddc3d229c33fbf0989cc075f25f (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
// File:	IntPolyh_Intersection_1.cxx
// Created:	Mon Sep 26 15:40:16 2005
// Author:	Igor FEOKTISTOV
//		<ifv@ygrex.nnov.opencascade.com>


#include <IntPolyh_Intersection.ixx>
#include <IntPolyh_PMaillageAffinage.hxx>
#include <IntPolyh_MaillageAffinage.hxx>
#include <IntPolyh_ArrayOfCouples.hxx>
#include <IntPolyh_Couple.hxx>

Standard_Integer MYPRINT1   = 0;


//=======================================================================
//function : IntPolyh_Intersection
//purpose  : 
//=======================================================================

IntPolyh_Intersection::IntPolyh_Intersection(const Handle(Adaptor3d_HSurface)& S1,
					     const TColStd_Array1OfReal& Upars1,
					     const TColStd_Array1OfReal& Vpars1,
					     const Handle(Adaptor3d_HSurface)& S2,
					     const TColStd_Array1OfReal& Upars2,
					     const TColStd_Array1OfReal& Vpars2)
{
  myNbSU1 = Upars1.Length();
  myNbSV1 = Vpars1.Length();
  myNbSU2 = Upars2.Length(); 
  myNbSV2 = Vpars2.Length(); 
  mySurf1 = S1;
  mySurf2 = S2;
  done = Standard_False;
  TSectionLines.Init(1000);
  TTangentZones.Init(10000);
  Perform(Upars1, Vpars1, Upars2, Vpars2);
}

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

void IntPolyh_Intersection::Perform(const TColStd_Array1OfReal& Upars1,
				    const TColStd_Array1OfReal& Vpars1,
				    const TColStd_Array1OfReal& Upars2,
				    const TColStd_Array1OfReal& Vpars2) { 

  done = Standard_True;

  Standard_Boolean startFromAdvanced = Standard_False;
  Standard_Boolean isStdDone = Standard_False;
  Standard_Boolean isAdvDone = Standard_False;
  Standard_Integer nbCouplesStd = 0;
  Standard_Integer nbCouplesAdv = 0;
  
  
  IntPolyh_PMaillageAffinage aPMaillageStd = 0;
  IntPolyh_PMaillageAffinage aPMaillageFF = 0;
  IntPolyh_PMaillageAffinage aPMaillageFR = 0;
  IntPolyh_PMaillageAffinage aPMaillageRF = 0;
  IntPolyh_PMaillageAffinage aPMaillageRR = 0;


  if(!startFromAdvanced) {

    isStdDone = PerformStd(Upars1, Vpars1, Upars2, Vpars2, 
			   aPMaillageStd,nbCouplesStd);

    // default interference done well, use it
    if(isStdDone && nbCouplesStd > 10) {
      aPMaillageStd->StartPointsChain(TSectionLines, TTangentZones);
    }
    // default interference done, but too few interferences foud;
    // use advanced interference
    else if(isStdDone && nbCouplesStd <= 10) {
      isAdvDone = PerformAdv(Upars1, Vpars1, Upars2, Vpars2,
			     aPMaillageFF,aPMaillageFR,aPMaillageRF,aPMaillageRR,nbCouplesAdv);
      
      // advanced interference found
      if(isAdvDone && nbCouplesAdv > 10) {
	aPMaillageFF->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageFR->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageRF->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageRR->StartPointsChain(TSectionLines,TTangentZones);
      }
      else {
	// use result of default
	if(nbCouplesStd > 0)
	  aPMaillageStd->StartPointsChain(TSectionLines, TTangentZones);
      }
    }
    // default interference faild, use advanced
    else {
//       isAdvDone = PerformAdv(aPMaillageFF,aPMaillageFR,aPMaillageRF,aPMaillageRR,nbCouplesAdv);
      
//       if(isAdvDone && nbCouplesAdv > 0) {cout << "4adv done, nbc: " << nbCouplesAdv << endl;
// 	aPMaillageFF->StartPointsChain(TSectionLines,TTangentZones);
// 	aPMaillageFR->StartPointsChain(TSectionLines,TTangentZones);
// 	aPMaillageRF->StartPointsChain(TSectionLines,TTangentZones);
// 	aPMaillageRR->StartPointsChain(TSectionLines,TTangentZones);
//       }
    }
  }// start from default
  else {
    
    isAdvDone = PerformAdv(Upars1, Vpars1, Upars2, Vpars2,
			   aPMaillageFF,aPMaillageFR,aPMaillageRF,aPMaillageRR,nbCouplesAdv);

    // advanced done, interference found; use it
    if(isAdvDone) {

      if(nbCouplesAdv > 0) {
	aPMaillageFF->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageFR->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageRF->StartPointsChain(TSectionLines,TTangentZones);
	aPMaillageRR->StartPointsChain(TSectionLines,TTangentZones);
      }
      else {
	isStdDone = PerformStd(aPMaillageStd,nbCouplesStd);
	if(isStdDone && nbCouplesStd > 0)
	  aPMaillageStd->StartPointsChain(TSectionLines, TTangentZones);
      }
    }
    else {
      isStdDone = PerformStd(Upars1, Vpars1, Upars2, Vpars2,
			     aPMaillageStd,nbCouplesStd);
      if(isStdDone && nbCouplesStd > 0)
	aPMaillageStd->StartPointsChain(TSectionLines, TTangentZones);
    }
  } // start from advanced

  // accept result
  nbsectionlines = TSectionLines.NbSectionLines();
  nbtangentzones = TTangentZones.NbTangentZones();

  // clean up
  if(aPMaillageStd) delete aPMaillageStd;
  if(aPMaillageFF) delete aPMaillageFF;
  if(aPMaillageFR) delete aPMaillageFR;
  if(aPMaillageRF) delete aPMaillageRF;
  if(aPMaillageRR) delete aPMaillageRR;

  // verify
  if(!isStdDone && !isAdvDone)
    done = Standard_False;
}

//=======================================================================
//function : PerformMaillage
//purpose  : Computes MaillageAffinage
//=======================================================================
Standard_Boolean IntPolyh_Intersection::PerformMaillage(const Standard_Boolean isFirstFwd,
							const Standard_Boolean isSecondFwd,
							const TColStd_Array1OfReal& Upars1,
							const TColStd_Array1OfReal& Vpars1,
							const TColStd_Array1OfReal& Upars2,
							const TColStd_Array1OfReal& Vpars2,
							IntPolyh_PMaillageAffinage &theMaillageS)
{
  theMaillageS = new IntPolyh_MaillageAffinage(mySurf1, Upars1.Length(), Vpars1.Length(),
					       mySurf2, Upars2.Length(), Vpars2.Length(),
					       MYPRINT1);
  
  
  theMaillageS->FillArrayOfPnt(1, isFirstFwd, Upars1, Vpars1);
  theMaillageS->FillArrayOfPnt(2, isSecondFwd, Upars2, Vpars2);
  
  
  
  Standard_Real xx0,yy0,zz0,xx1,yy1,zz1;
  theMaillageS->CommonBox(theMaillageS->GetBox(1), theMaillageS->GetBox(2),
			  xx0, yy0, zz0, xx1, yy1, zz1);
  
  theMaillageS->FillArrayOfEdges(1);
  theMaillageS->FillArrayOfEdges(2);

  theMaillageS->FillArrayOfTriangles(1);
  theMaillageS->FillArrayOfTriangles(2);
  
  theMaillageS->LinkEdges2Triangles();
  
  theMaillageS->TrianglesDeflectionsRefinementBSB();

  Standard_Integer FinTTC = theMaillageS->TriangleCompare();

  // if too many intersections, consider surfaces parallel (eap)
/*
  if(FinTTC > 200 &&
     (FinTTC >= theMaillageS->GetArrayOfTriangles(1).NbTriangles() ||
      FinTTC >= theMaillageS->GetArrayOfTriangles(2).NbTriangles()) ) {
    return Standard_False;
  }
*/
//IFV test for parallel surf
  if(FinTTC > 200) {
    const Standard_Real eps = .996; //~ cos of 5deg.
    IntPolyh_ArrayOfCouples& Couples = theMaillageS->GetArrayOfCouples();
    
    Standard_Integer i, npara = 0;
    for(i = 0; i < FinTTC; ++i) {
      Standard_Real cosa = Abs(Couples[i].AngleValue());
      if(cosa > eps) ++npara;
    }
    if(npara >= theMaillageS->GetArrayOfTriangles(1).NbTriangles() ||
       npara >= theMaillageS->GetArrayOfTriangles(2).NbTriangles() ) {
      return Standard_False;
    }
  }

  return Standard_True;
}

//=======================================================================
//function : PerformMaillage
//purpose  : Computes MaillageAffinage
//=======================================================================
Standard_Boolean IntPolyh_Intersection::PerformMaillage(const TColStd_Array1OfReal& Upars1,
							const TColStd_Array1OfReal& Vpars1,
							const TColStd_Array1OfReal& Upars2,
							const TColStd_Array1OfReal& Vpars2,
							IntPolyh_PMaillageAffinage &theMaillageS)
{
    
  theMaillageS = new IntPolyh_MaillageAffinage(mySurf1, Upars1.Length(), Vpars1.Length(),
					       mySurf2, Upars2.Length(), Vpars2.Length(),
					       MYPRINT1);
  
  theMaillageS->FillArrayOfPnt(1, Upars1, Vpars1);
  theMaillageS->FillArrayOfPnt(2, Upars2, Vpars2);
  
  
  Standard_Real xx0,yy0,zz0,xx1,yy1,zz1;
  theMaillageS->CommonBox(theMaillageS->GetBox(1), theMaillageS->GetBox(2),
			  xx0, yy0, zz0, xx1, yy1, zz1);
  theMaillageS->FillArrayOfEdges(1);
  theMaillageS->FillArrayOfEdges(2);

  theMaillageS->FillArrayOfTriangles(1);
  theMaillageS->FillArrayOfTriangles(2);
  
  theMaillageS->LinkEdges2Triangles();
  
  theMaillageS->TrianglesDeflectionsRefinementBSB();

  Standard_Integer FinTTC = theMaillageS->TriangleCompare();

  if( FinTTC == 0 ) {
    Standard_Boolean myZone = Standard_True;
    theMaillageS->SetEnlargeZone( myZone );
    theMaillageS->FillArrayOfPnt(1);
    theMaillageS->FillArrayOfPnt(2);
    theMaillageS->CommonBox(theMaillageS->GetBox(1), theMaillageS->GetBox(2),
			    xx0, yy0, zz0, xx1, yy1, zz1);
    theMaillageS->FillArrayOfEdges(1);
    theMaillageS->FillArrayOfEdges(2);
    theMaillageS->FillArrayOfTriangles(1);
    theMaillageS->FillArrayOfTriangles(2);
    theMaillageS->LinkEdges2Triangles();
    theMaillageS->TrianglesDeflectionsRefinementBSB();
    FinTTC = theMaillageS->TriangleCompare();
    myZone = Standard_False;
    theMaillageS->SetEnlargeZone( myZone );
  }

  // if too many intersections, consider surfaces parallel (eap)
/*
  if(FinTTC > 200 &&
     (FinTTC >= theMaillageS->GetArrayOfTriangles(1).NbTriangles() ||
      FinTTC >= theMaillageS->GetArrayOfTriangles(2).NbTriangles()) ) {
    return Standard_False;
  }
*/
//IFV test for parallel surf
  if(FinTTC > 200) {
    const Standard_Real eps = .996; //~ cos of 5deg.
    IntPolyh_ArrayOfCouples& Couples = theMaillageS->GetArrayOfCouples();
    
    Standard_Integer i, npara = 0;
    for(i = 0; i < FinTTC; ++i) {
      Standard_Real cosa = Abs(Couples[i].AngleValue());
      if(cosa > eps) ++npara;
    }
    if(npara >= theMaillageS->GetArrayOfTriangles(1).NbTriangles() ||
       npara >= theMaillageS->GetArrayOfTriangles(2).NbTriangles() ) {
      return Standard_False;
    }
  }
  
  return Standard_True;
}

//=======================================================================
//function : PerformAdv
//purpose  : 
//=======================================================================

Standard_Boolean IntPolyh_Intersection::PerformAdv(const TColStd_Array1OfReal& Upars1,
						   const TColStd_Array1OfReal& Vpars1,
						   const TColStd_Array1OfReal& Upars2,
						   const TColStd_Array1OfReal& Vpars2,
						   IntPolyh_PMaillageAffinage& MaillageFF,
						   IntPolyh_PMaillageAffinage& MaillageFR,
						   IntPolyh_PMaillageAffinage& MaillageRF,
						   IntPolyh_PMaillageAffinage& MaillageRR,
						   Standard_Integer&           NbCouples)
{
  Standard_Boolean isdone = Standard_True;
  NbCouples = 0;

  if(!PerformMaillage(Standard_True,Standard_False,
		      Upars1, Vpars1, Upars2, Vpars2, 
		      MaillageFR) ||
     !PerformMaillage(Standard_False,Standard_True,
		      Upars1, Vpars1, Upars2, Vpars2, 
		      MaillageRF) ||
     !PerformMaillage(Standard_True,Standard_True,
		      Upars1, Vpars1, Upars2, Vpars2, 
		      MaillageFF)  ||
     !PerformMaillage(Standard_False,Standard_False,
		      Upars1, Vpars1, Upars2, Vpars2, 
		      MaillageRR) )
    isdone = Standard_False; 

  if(isdone) {
    NbCouples = MaillageFF->GetArrayOfCouples().NbCouples() +
      MaillageFR->GetArrayOfCouples().NbCouples() +
	MaillageRF->GetArrayOfCouples().NbCouples() +
	  MaillageRR->GetArrayOfCouples().NbCouples();

    if(NbCouples > 0)
      MergeCouples(MaillageFF->GetArrayOfCouples(),MaillageFR->GetArrayOfCouples(),
		   MaillageRF->GetArrayOfCouples(),MaillageRR->GetArrayOfCouples());
  }
  return isdone;
}
//=======================================================================
//function : PerformStd
//purpose  : 
//=======================================================================

Standard_Boolean IntPolyh_Intersection::PerformStd(const TColStd_Array1OfReal& Upars1,
						   const TColStd_Array1OfReal& Vpars1,
						   const TColStd_Array1OfReal& Upars2,
						   const TColStd_Array1OfReal& Vpars2,
						   IntPolyh_PMaillageAffinage& MaillageS,
						   Standard_Integer&           NbCouples)
{
  Standard_Boolean isdone = PerformMaillage(Upars1, Vpars1, Upars2, Vpars2, 
					    MaillageS);
  NbCouples = (isdone) ? (MaillageS->GetArrayOfCouples().NbCouples()) : 0;
  return isdone;
}