1 // Created on: 1999-03-03
2 // Created by: Fabrice SERVANT
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <IntPolyh_Intersection.hxx>
20 #include <Adaptor3d_HSurface.hxx>
22 #include <IntPolyh_Couple.hxx>
23 #include <IntPolyh_CoupleMapHasher.hxx>
24 #include <IntPolyh_MaillageAffinage.hxx>
25 #include <IntPolyh_SectionLine.hxx>
26 #include <IntPolyh_StartPoint.hxx>
27 #include <IntPolyh_Tools.hxx>
28 #include <IntPolyh_Triangle.hxx>
30 #include <NCollection_Map.hxx>
32 static Standard_Boolean IsAdvRequired(IntPolyh_PMaillageAffinage& theMaillage);
34 static Standard_Integer ComputeIntersection(IntPolyh_PMaillageAffinage& theMaillage);
36 static Standard_Boolean AnalyzeIntersection(IntPolyh_PMaillageAffinage& theMaillage);
38 //=======================================================================
39 //function : IntPolyh_Intersection
41 //=======================================================================
42 IntPolyh_Intersection::IntPolyh_Intersection(const Handle(Adaptor3d_HSurface)& theS1,
43 const Handle(Adaptor3d_HSurface)& theS2)
51 myIsDone = Standard_False;
52 mySectionLines.Init(1000);
53 myTangentZones.Init(10000);
57 //=======================================================================
58 //function : IntPolyh_Intersection
60 //=======================================================================
61 IntPolyh_Intersection::IntPolyh_Intersection(const Handle(Adaptor3d_HSurface)& theS1,
62 const Standard_Integer theNbSU1,
63 const Standard_Integer theNbSV1,
64 const Handle(Adaptor3d_HSurface)& theS2,
65 const Standard_Integer theNbSU2,
66 const Standard_Integer theNbSV2)
74 myIsDone = Standard_False;
75 mySectionLines.Init(1000);
76 myTangentZones.Init(10000);
80 //=======================================================================
81 //function : IntPolyh_Intersection
83 //=======================================================================
84 IntPolyh_Intersection::IntPolyh_Intersection(const Handle(Adaptor3d_HSurface)& theS1,
85 const TColStd_Array1OfReal& theUPars1,
86 const TColStd_Array1OfReal& theVPars1,
87 const Handle(Adaptor3d_HSurface)& theS2,
88 const TColStd_Array1OfReal& theUPars2,
89 const TColStd_Array1OfReal& theVPars2)
93 myNbSU1 = theUPars1.Length();
94 myNbSV1 = theVPars1.Length();
95 myNbSU2 = theUPars2.Length();
96 myNbSV2 = theVPars2.Length();
97 myIsDone = Standard_False;
98 mySectionLines.Init(1000);
99 myTangentZones.Init(10000);
100 Perform(theUPars1, theVPars1, theUPars2, theVPars2);
103 //=======================================================================
104 //function : GetLinePoint
106 //=======================================================================
107 void IntPolyh_Intersection::GetLinePoint(const Standard_Integer Indexl,
108 const Standard_Integer Indexp,
116 Standard_Real &incidence) const
118 const IntPolyh_SectionLine &msl = mySectionLines[Indexl - 1];
119 const IntPolyh_StartPoint &sp = msl[Indexp - 1];
127 incidence = sp.GetAngle();
130 //=======================================================================
131 //function : GetTangentZonePoint
133 //=======================================================================
134 void IntPolyh_Intersection::GetTangentZonePoint(const Standard_Integer Indexz,
135 const Standard_Integer /*Indexp*/,
142 Standard_Real &v2) const
144 const IntPolyh_StartPoint &sp = myTangentZones[Indexz - 1];
154 //=======================================================================
157 //=======================================================================
158 void IntPolyh_Intersection::Perform()
160 // Prepare the sampling of the surfaces - UV parameters of the triangulation nodes
161 TColStd_Array1OfReal UPars1, VPars1, UPars2, VPars2;
162 IntPolyh_Tools::MakeSampling(mySurf1, myNbSU1, myNbSV1, Standard_False, UPars1, VPars1);
163 IntPolyh_Tools::MakeSampling(mySurf2, myNbSU2, myNbSV2, Standard_False, UPars2, VPars2);
165 // Perform intersection
166 Perform(UPars1, VPars1, UPars2, VPars2);
169 //=======================================================================
172 //=======================================================================
173 void IntPolyh_Intersection::Perform(const TColStd_Array1OfReal& theUPars1,
174 const TColStd_Array1OfReal& theVPars1,
175 const TColStd_Array1OfReal& theUPars2,
176 const TColStd_Array1OfReal& theVPars2)
178 myIsDone = Standard_True;
180 // Compute the deflection of the given sampling if it is not set
181 Standard_Real aDeflTol1 = IntPolyh_Tools::ComputeDeflection(mySurf1, theUPars1, theVPars1);
182 Standard_Real aDeflTol2 = IntPolyh_Tools::ComputeDeflection(mySurf2, theUPars2, theVPars2);
184 // Perform standard intersection
185 IntPolyh_PMaillageAffinage pMaillageStd = 0;
186 Standard_Integer nbCouplesStd = 0;
187 Standard_Boolean isStdDone = PerformStd(theUPars1, theVPars1,
188 theUPars2, theVPars2,
189 aDeflTol1, aDeflTol2,
190 pMaillageStd, nbCouplesStd);
194 // Intersection not done
195 myIsDone = Standard_False;
196 if (pMaillageStd) delete pMaillageStd;
200 if (!IsAdvRequired(pMaillageStd))
202 // Default interference done well, use it
203 pMaillageStd->StartPointsChain(mySectionLines, myTangentZones);
207 // Default intersection is done, but too few interferences found.
208 // Perform advanced intersection - perform intersection four times with different shifts.
209 IntPolyh_PMaillageAffinage pMaillageFF = 0;
210 IntPolyh_PMaillageAffinage pMaillageFR = 0;
211 IntPolyh_PMaillageAffinage pMaillageRF = 0;
212 IntPolyh_PMaillageAffinage pMaillageRR = 0;
213 Standard_Integer nbCouplesAdv = 0;
215 Standard_Boolean isAdvDone = PerformAdv(theUPars1, theVPars1,
216 theUPars2, theVPars2,
217 aDeflTol1, aDeflTol2,
224 if (isAdvDone && nbCouplesAdv > 0)
226 // Advanced interference found
227 pMaillageFF->StartPointsChain(mySectionLines, myTangentZones);
228 pMaillageFR->StartPointsChain(mySectionLines, myTangentZones);
229 pMaillageRF->StartPointsChain(mySectionLines, myTangentZones);
230 pMaillageRR->StartPointsChain(mySectionLines, myTangentZones);
234 // Advanced intersection not done or no intersection is found -> use standard intersection
235 if (nbCouplesStd > 0)
236 pMaillageStd->StartPointsChain(mySectionLines, myTangentZones);
240 if (pMaillageFF) delete pMaillageFF;
241 if (pMaillageFR) delete pMaillageFR;
242 if (pMaillageRF) delete pMaillageRF;
243 if (pMaillageRR) delete pMaillageRR;
247 if (pMaillageStd) delete pMaillageStd;
250 //=======================================================================
251 //function : PerformStd
253 //=======================================================================
254 Standard_Boolean IntPolyh_Intersection::PerformStd(const TColStd_Array1OfReal& theUPars1,
255 const TColStd_Array1OfReal& theVPars1,
256 const TColStd_Array1OfReal& theUPars2,
257 const TColStd_Array1OfReal& theVPars2,
258 const Standard_Real theDeflTol1,
259 const Standard_Real theDeflTol2,
260 IntPolyh_PMaillageAffinage& theMaillageS,
261 Standard_Integer& theNbCouples)
263 Standard_Boolean isDone = PerformMaillage(theUPars1, theVPars1,
264 theUPars2, theVPars2,
265 theDeflTol1, theDeflTol2,
267 theNbCouples = (isDone) ? (theMaillageS->GetCouples().Extent()) : 0;
271 //=======================================================================
272 //function : PerformAdv
274 //=======================================================================
275 Standard_Boolean IntPolyh_Intersection::PerformAdv(const TColStd_Array1OfReal& theUPars1,
276 const TColStd_Array1OfReal& theVPars1,
277 const TColStd_Array1OfReal& theUPars2,
278 const TColStd_Array1OfReal& theVPars2,
279 const Standard_Real theDeflTol1,
280 const Standard_Real theDeflTol2,
281 IntPolyh_PMaillageAffinage& theMaillageFF,
282 IntPolyh_PMaillageAffinage& theMaillageFR,
283 IntPolyh_PMaillageAffinage& theMaillageRF,
284 IntPolyh_PMaillageAffinage& theMaillageRR,
285 Standard_Integer& theNbCouples)
287 // Compute the points on the surface and normal directions in these points
288 IntPolyh_ArrayOfPointNormal aPoints1, aPoints2;
289 IntPolyh_Tools::FillArrayOfPointNormal(mySurf1, theUPars1, theVPars1, aPoints1);
290 IntPolyh_Tools::FillArrayOfPointNormal(mySurf2, theUPars2, theVPars2, aPoints2);
292 // Perform intersection with the different shifts of the triangles
293 Standard_Boolean isDone =
294 PerformMaillage(theUPars1, theVPars1, theUPars2, theVPars2, // sampling
295 theDeflTol1, theDeflTol2, // deflection tolerance
296 aPoints1, aPoints2, // points and normals
297 Standard_True , Standard_False, // shift
300 PerformMaillage(theUPars1, theVPars1, theUPars2, theVPars2, // sampling
301 theDeflTol1, theDeflTol2, // deflection tolerance
302 aPoints1, aPoints2, // points and normals
303 Standard_False, Standard_True, // shift
306 PerformMaillage(theUPars1, theVPars1, theUPars2, theVPars2, // sampling
307 theDeflTol1, theDeflTol2, // deflection tolerance
308 aPoints1, aPoints2, // points and normals
309 Standard_True, Standard_True, // shift
312 PerformMaillage(theUPars1, theVPars1, theUPars2, theVPars2, // sampling
313 theDeflTol1, theDeflTol2, // deflection tolerance
314 aPoints1, aPoints2, // points and normals
315 Standard_False, Standard_False, // shift
320 theNbCouples = theMaillageFF->GetCouples().Extent() +
321 theMaillageFR->GetCouples().Extent() +
322 theMaillageRF->GetCouples().Extent() +
323 theMaillageRR->GetCouples().Extent();
327 MergeCouples(theMaillageFF->GetCouples(),
328 theMaillageFR->GetCouples(),
329 theMaillageRF->GetCouples(),
330 theMaillageRR->GetCouples());
336 //=======================================================================
337 //function : PerformMaillage
338 //purpose : Computes standard MaillageAffinage (without shift)
339 //=======================================================================
340 Standard_Boolean IntPolyh_Intersection::PerformMaillage(const TColStd_Array1OfReal& theUPars1,
341 const TColStd_Array1OfReal& theVPars1,
342 const TColStd_Array1OfReal& theUPars2,
343 const TColStd_Array1OfReal& theVPars2,
344 const Standard_Real theDeflTol1,
345 const Standard_Real theDeflTol2,
346 IntPolyh_PMaillageAffinage& theMaillage)
349 new IntPolyh_MaillageAffinage(mySurf1, theUPars1.Length(), theVPars1.Length(),
350 mySurf2, theUPars2.Length(), theVPars2.Length(),
353 theMaillage->FillArrayOfPnt(1, theUPars1, theVPars1, &theDeflTol1);
354 theMaillage->FillArrayOfPnt(2, theUPars2, theVPars2, &theDeflTol2);
356 Standard_Integer FinTTC = ComputeIntersection(theMaillage);
358 // If no intersecting triangles are found, try enlarged surfaces
361 // Check if enlarge for the surfaces is possible
362 Standard_Boolean isEnlargeU1, isEnlargeV1, isEnlargeU2, isEnlargeV2;
363 IntPolyh_Tools::IsEnlargePossible(mySurf1, isEnlargeU1, isEnlargeV1);
364 IntPolyh_Tools::IsEnlargePossible(mySurf2, isEnlargeU2, isEnlargeV2);
366 if (isEnlargeU1 || isEnlargeV1 || isEnlargeU2 || isEnlargeV2)
368 theMaillage->SetEnlargeZone(Standard_True);
369 // Make new points on the enlarged surface
370 theMaillage->FillArrayOfPnt(1);
371 theMaillage->FillArrayOfPnt(2);
372 // Compute intersection
373 ComputeIntersection(theMaillage);
374 theMaillage->SetEnlargeZone(Standard_False);
378 // if too many intersections, consider surfaces parallel
379 return AnalyzeIntersection(theMaillage);
382 //=======================================================================
383 //function : PerformMaillage
384 //purpose : Computes MaillageAffinage
385 //=======================================================================
386 Standard_Boolean IntPolyh_Intersection::PerformMaillage(const TColStd_Array1OfReal& theUPars1,
387 const TColStd_Array1OfReal& theVPars1,
388 const TColStd_Array1OfReal& theUPars2,
389 const TColStd_Array1OfReal& theVPars2,
390 const Standard_Real theDeflTol1,
391 const Standard_Real theDeflTol2,
392 const IntPolyh_ArrayOfPointNormal& thePoints1,
393 const IntPolyh_ArrayOfPointNormal& thePoints2,
394 const Standard_Boolean theIsFirstFwd,
395 const Standard_Boolean theIsSecondFwd,
396 IntPolyh_PMaillageAffinage& theMaillage)
399 new IntPolyh_MaillageAffinage(mySurf1, theUPars1.Length(), theVPars1.Length(),
400 mySurf2, theUPars2.Length(), theVPars2.Length(),
403 theMaillage->FillArrayOfPnt(1, theIsFirstFwd , thePoints1, theUPars1, theVPars1, theDeflTol1);
404 theMaillage->FillArrayOfPnt(2, theIsSecondFwd, thePoints2, theUPars2, theVPars2, theDeflTol2);
406 ComputeIntersection(theMaillage);
408 return AnalyzeIntersection(theMaillage);
411 //=======================================================================
412 //function : MergeCouples
413 //purpose : This method analyzes the lists to find same couples.
414 // If some are detected it leaves the couple in only one list
415 // deleting from others.
416 //=======================================================================
417 void IntPolyh_Intersection::MergeCouples(IntPolyh_ListOfCouples &anArrayFF,
418 IntPolyh_ListOfCouples &anArrayFR,
419 IntPolyh_ListOfCouples &anArrayRF,
420 IntPolyh_ListOfCouples &anArrayRR) const
422 // Fence map to remove from the lists the duplicating elements.
423 NCollection_Map<IntPolyh_Couple, IntPolyh_CoupleMapHasher> aFenceMap;
425 IntPolyh_ListOfCouples* pLists[4] = {&anArrayFF, &anArrayFR, &anArrayRF, &anArrayRR};
426 for (Standard_Integer i = 0; i < 4; ++i) {
427 IntPolyh_ListIteratorOfListOfCouples aIt(*pLists[i]);
428 for (; aIt.More();) {
429 if (!aFenceMap.Add(aIt.Value())) {
430 pLists[i]->Remove(aIt);
438 //=======================================================================
439 //function : IsAdvRequired
440 //purpose : Analyzes the standard intersection on the angles between triangles.
441 // If the angle between some of the interfering triangles is
442 // too small (less than 5 deg), the advanced intersection is required.
443 // Otherwise, the standard intersection is considered satisfactory.
444 //=======================================================================
445 Standard_Boolean IsAdvRequired(IntPolyh_PMaillageAffinage& theMaillage)
448 return Standard_True;
450 // Interfering triangles
451 IntPolyh_ListOfCouples& Couples = theMaillage->GetCouples();
452 // Number of interfering pairs
453 Standard_Integer aNbCouples = Couples.Extent();
454 // Flag to define whether advanced intersection is required or not
455 Standard_Boolean isAdvReq = (aNbCouples == 0);
457 // No interfering triangles are found -> perform advanced intersection
461 // Enough interfering triangles are found -> no need to perform advanced intersection
464 const Standard_Real anEps = .996; //~ cos of 5 deg
465 IntPolyh_ListIteratorOfListOfCouples aIt(Couples);
466 for(; aIt.More(); aIt.Next())
468 if (Abs(aIt.Value().Angle()) > anEps)
470 // The angle between interfering triangles is small -> perform advanced
471 // intersection to make intersection more precise
472 isAdvReq = Standard_True;
480 //=======================================================================
481 //function : ComputeIntersection
482 //purpose : Computes the intersection of the triangles
483 //=======================================================================
484 Standard_Integer ComputeIntersection(IntPolyh_PMaillageAffinage& theMaillage)
489 // Compute common box and mark the points inside that box
490 theMaillage->CommonBox();
493 theMaillage->FillArrayOfTriangles(1);
494 theMaillage->FillArrayOfTriangles(2);
497 theMaillage->FillArrayOfEdges(1);
498 theMaillage->FillArrayOfEdges(2);
500 // Deflection refinement
501 theMaillage->TrianglesDeflectionsRefinementBSB();
503 return theMaillage->TriangleCompare();
506 //=======================================================================
507 //function : AnalyzeIntersection
508 //purpose : Analyzes the intersection on the number of interfering triangles
509 //=======================================================================
510 Standard_Boolean AnalyzeIntersection(IntPolyh_PMaillageAffinage& theMaillage)
513 return Standard_False;
515 IntPolyh_ListOfCouples& Couples = theMaillage->GetCouples();
516 Standard_Integer FinTTC = Couples.Extent();
519 const Standard_Real eps = .996; //~ cos of 5deg.
520 Standard_Integer npara = 0;
521 IntPolyh_ListIteratorOfListOfCouples aIt(Couples);
522 for(; aIt.More(); aIt.Next())
524 Standard_Real cosa = Abs(aIt.Value().Angle());
525 if(cosa > eps) ++npara;
528 if (npara >= theMaillage->GetArrayOfTriangles(1).NbItems() ||
529 npara >= theMaillage->GetArrayOfTriangles(2).NbItems())
531 return Standard_False;
534 return Standard_True;