1 // Created on: 1996-04-22
2 // Created by: Herve LOUESSARD
3 // Copyright (c) 1996-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.
17 // Modified: Mps(10-04-97) portage WNT
19 #include <BRepExtrema_DistShapeShape.hxx>
21 #include <Standard_OStream.hxx>
22 #include <TopTools_IndexedMapOfShape.hxx>
23 #include <BRepBndLib.hxx>
24 #include <Bnd_Box.hxx>
26 #include <BRepExtrema_DistanceSS.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopAbs_ShapeEnum.hxx>
33 #include <Precision.hxx>
34 #include <Bnd_SeqOfBox.hxx>
35 #include <BRepExtrema_UnCompatibleShape.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepClass3d_SolidClassifier.hxx>
38 #include <NCollection_Comparator.hxx>
39 #include <NCollection_Vector.hxx>
40 #include <StdFail_NotDone.hxx>
46 static void Decomposition(const TopoDS_Shape& S,
47 TopTools_IndexedMapOfShape& MapV,
48 TopTools_IndexedMapOfShape& MapE,
49 TopTools_IndexedMapOfShape& MapF)
54 TopExp::MapShapes(S,TopAbs_VERTEX,MapV);
55 TopExp::MapShapes(S,TopAbs_EDGE,MapE);
56 TopExp::MapShapes(S,TopAbs_FACE,MapF);
59 static void BoxCalculation(const TopTools_IndexedMapOfShape& Map,
62 for (Standard_Integer i = 1; i <= Map.Extent(); i++)
65 BRepBndLib::Add(Map(i), box);
70 inline Standard_Real DistanceInitiale(const TopoDS_Vertex V1,
71 const TopoDS_Vertex V2)
73 return (BRep_Tool::Pnt(V1).Distance(BRep_Tool::Pnt(V2)));
76 //! Pair of objects to check extrema.
77 struct BRepExtrema_CheckPair
79 Standard_Integer Index1; //!< Index of the 1st sub-shape
80 Standard_Integer Index2; //!< Index of the 2nd sub-shape
81 Standard_Real Distance; //!< Distance between sub-shapes
83 //! Uninitialized constructor for collection.
84 BRepExtrema_CheckPair() {}
86 //! Creates new pair of sub-shapes.
87 BRepExtrema_CheckPair (Standard_Integer theIndex1,
88 Standard_Integer theIndex2,
89 Standard_Real theDistance)
92 Distance (theDistance) {}
95 // Used by std::sort function
96 static Standard_Boolean BRepExtrema_CheckPair_Comparator (const BRepExtrema_CheckPair& theLeft,
97 const BRepExtrema_CheckPair& theRight)
99 return (theLeft.Distance < theRight.Distance);
103 //=======================================================================
104 //function : DistanceMapMap
106 //=======================================================================
108 void BRepExtrema_DistShapeShape::DistanceMapMap (const TopTools_IndexedMapOfShape& theMap1,
109 const TopTools_IndexedMapOfShape& theMap2,
110 const Bnd_SeqOfBox& theLBox1,
111 const Bnd_SeqOfBox& theLBox2)
113 NCollection_Vector<BRepExtrema_CheckPair> aPairList;
114 const Standard_Integer aCount1 = theMap1.Extent();
115 const Standard_Integer aCount2 = theMap2.Extent();
116 for (Standard_Integer anIdx1 = 1; anIdx1 <= aCount1; ++anIdx1)
118 for (Standard_Integer anIdx2 = 1; anIdx2 <= aCount2; ++anIdx2)
120 const Bnd_Box& aBox1 = theLBox1.Value (anIdx1);
121 const Bnd_Box& aBox2 = theLBox2.Value (anIdx2);
128 const Standard_Real aDist = aBox1.Distance (aBox2);
129 if (aDist < myDistRef - myEps || fabs (aDist - myDistRef) < myEps)
131 aPairList.Append (BRepExtrema_CheckPair (anIdx1, anIdx2, aDist));
136 std::stable_sort(aPairList.begin(), aPairList.end(), BRepExtrema_CheckPair_Comparator);
138 for (NCollection_Vector<BRepExtrema_CheckPair>::Iterator aPairIter (aPairList);
139 aPairIter.More(); aPairIter.Next())
141 const BRepExtrema_CheckPair& aPair = aPairIter.Value();
142 if (aPair.Distance > myDistRef + myEps)
144 break; // early search termination
147 const Bnd_Box& aBox1 = theLBox1.Value (aPair.Index1);
148 const Bnd_Box& aBox2 = theLBox2.Value (aPair.Index2);
150 const TopoDS_Shape& aShape1 = theMap1 (aPair.Index1);
151 const TopoDS_Shape& aShape2 = theMap2 (aPair.Index2);
153 BRepExtrema_DistanceSS aDistTool (aShape1, aShape2, aBox1, aBox2, myDistRef, myEps);
154 if (aDistTool.IsDone())
156 if (aDistTool.DistValue() < myDistRef - myEps)
158 mySolutionsShape1.Clear();
159 mySolutionsShape2.Clear();
161 BRepExtrema_SeqOfSolution aSeq1 = aDistTool.Seq1Value();
162 BRepExtrema_SeqOfSolution aSeq2 = aDistTool.Seq2Value();
164 mySolutionsShape1.Append (aSeq1);
165 mySolutionsShape2.Append (aSeq2);
167 myDistRef = aDistTool.DistValue();
169 else if (fabs (aDistTool.DistValue() - myDistRef) < myEps)
171 BRepExtrema_SeqOfSolution aSeq1 = aDistTool.Seq1Value();
172 BRepExtrema_SeqOfSolution aSeq2 = aDistTool.Seq2Value();
174 mySolutionsShape1.Append (aSeq1);
175 mySolutionsShape2.Append (aSeq2);
177 if (myDistRef > aDistTool.DistValue())
179 myDistRef = aDistTool.DistValue();
186 //=======================================================================
187 //function : BRepExtrema_DistShapeShape
189 //=======================================================================
191 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape()
193 myIsDone (Standard_False),
194 myInnerSol (Standard_False),
195 myEps (Precision::Confusion()),
196 myIsInitS1 (Standard_False),
197 myIsInitS2 (Standard_False),
198 myFlag (Extrema_ExtFlag_MINMAX),
199 myAlgo (Extrema_ExtAlgo_Grad)
204 //=======================================================================
205 //function : BRepExtrema_DistShapeShape
207 //=======================================================================
208 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape(const TopoDS_Shape& Shape1,
209 const TopoDS_Shape& Shape2,
210 const Extrema_ExtFlag F,
211 const Extrema_ExtAlgo A)
213 myIsDone (Standard_False),
214 myInnerSol (Standard_False),
215 myEps (Precision::Confusion()),
216 myIsInitS1 (Standard_False),
217 myIsInitS2 (Standard_False),
226 //=======================================================================
227 //function : BRepExtrema_DistShapeShape
229 //=======================================================================
231 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape(const TopoDS_Shape& Shape1,
232 const TopoDS_Shape& Shape2,
233 const Standard_Real theDeflection,
234 const Extrema_ExtFlag F,
235 const Extrema_ExtAlgo A)
237 myIsDone (Standard_False),
238 myInnerSol (Standard_False),
239 myEps (theDeflection),
240 myIsInitS1 (Standard_False),
241 myIsInitS2 (Standard_False),
250 //=======================================================================
253 //=======================================================================
255 void BRepExtrema_DistShapeShape::LoadS1 (const TopoDS_Shape& Shape1)
258 myIsInitS1 = Standard_False;
259 Decomposition (Shape1, myMapV1, myMapE1, myMapF1);
262 //=======================================================================
265 //=======================================================================
267 void BRepExtrema_DistShapeShape::LoadS2 (const TopoDS_Shape& Shape2)
270 myIsInitS2 = Standard_False;
271 Decomposition (Shape2, myMapV2, myMapE2, myMapF2);
274 //=======================================================================
277 //=======================================================================
279 Standard_Boolean BRepExtrema_DistShapeShape::Perform()
281 myIsDone=Standard_False;
282 myInnerSol=Standard_False;
283 mySolutionsShape1.Clear();
284 mySolutionsShape2.Clear();
286 if ( myShape1.IsNull() || myShape2.IsNull() )
287 return Standard_False;
290 const Standard_Real tol = 0.001;
292 // Treatment of solids
293 const TopAbs_ShapeEnum Type1 = myShape1.ShapeType();
294 if ((Type1==TopAbs_SOLID) || (Type1 == TopAbs_COMPSOLID))
296 BRepClass3d_SolidClassifier Classi(myShape1);
297 const Standard_Integer nbv2 = myMapV2.Extent();
298 Standard_Integer nbv1 = 0;
299 while ( (nbv1<nbv2) && (!myInnerSol) )
302 V = TopoDS::Vertex(myMapV2(nbv1));
303 const gp_Pnt &P = BRep_Tool::Pnt(V);
304 Classi.Perform(P,tol);
305 if (Classi.State()==TopAbs_IN)
307 myInnerSol = Standard_True;
309 myIsDone = Standard_True;
310 BRepExtrema_SolutionElem Sol(0,P,BRepExtrema_IsVertex,V);
311 mySolutionsShape1.Append(Sol);
312 mySolutionsShape2.Append(Sol);
317 const TopAbs_ShapeEnum Type2 = myShape2.ShapeType();
318 if (((Type2==TopAbs_SOLID) || (Type2==TopAbs_COMPSOLID)) && (!myInnerSol))
320 BRepClass3d_SolidClassifier Classi(myShape2);
321 const Standard_Integer nbv1 = myMapV1.Extent();
322 Standard_Integer nbv2 = 0;
323 while ( (nbv2<nbv1) && (!myInnerSol) )
326 V = TopoDS::Vertex(myMapV1(nbv2));
327 const gp_Pnt &P = BRep_Tool::Pnt(V);
328 Classi.Perform(P,tol);
329 if (Classi.State()==TopAbs_IN) {
330 myInnerSol = Standard_True;
332 myIsDone = Standard_True;
333 BRepExtrema_SolutionElem Sol (0,P,BRepExtrema_IsVertex,V);
334 mySolutionsShape1.Append(Sol);
335 mySolutionsShape2.Append(Sol);
342 if (!myIsInitS1) // rebuild cached data for 1st shape
348 BoxCalculation (myMapV1, myBV1);
349 BoxCalculation (myMapE1, myBE1);
350 BoxCalculation (myMapF1, myBF1);
352 myIsInitS1 = Standard_True;
355 if (!myIsInitS2) // rebuild cached data for 2nd shape
361 BoxCalculation (myMapV2, myBV2);
362 BoxCalculation (myMapE2, myBE2);
363 BoxCalculation (myMapF2, myBF2);
365 myIsInitS2 = Standard_True;
368 if (myMapV1.Extent() && myMapV2.Extent())
370 TopoDS_Vertex V1 = TopoDS::Vertex(myMapV1(1));
371 TopoDS_Vertex V2 = TopoDS::Vertex(myMapV2(1));
372 myDistRef = DistanceInitiale(V1, V2);
375 myDistRef= 1.e30; //szv:!!!
377 DistanceMapMap (myMapV1, myMapV2, myBV1, myBV2);
378 DistanceMapMap (myMapV1, myMapE2, myBV1, myBE2);
379 DistanceMapMap (myMapE1, myMapV2, myBE1, myBV2);
380 DistanceMapMap (myMapV1, myMapF2, myBV1, myBF2);
381 DistanceMapMap (myMapF1, myMapV2, myBF1, myBV2);
382 DistanceMapMap (myMapE1, myMapE2, myBE1, myBE2);
383 DistanceMapMap (myMapE1, myMapF2, myBE1, myBF2);
384 DistanceMapMap (myMapF1, myMapE2, myBF1, myBE2);
386 if (fabs (myDistRef) > myEps)
388 DistanceMapMap (myMapF1, myMapF2, myBF1, myBF2);
391 // Modified by Sergey KHROMOV - Tue Mar 6 11:55:03 2001 Begin
392 Standard_Integer i = 1;
393 for (; i <= mySolutionsShape1.Length(); i++)
394 if (mySolutionsShape1.Value(i).Dist() > myDistRef + myEps)
396 mySolutionsShape1.Remove(i);
397 mySolutionsShape2.Remove(i);
399 // Modified by Sergey KHROMOV - Tue Mar 6 11:55:04 2001 End
400 myIsDone = ( mySolutionsShape1.Length() > 0 );
405 //=======================================================================
408 //=======================================================================
410 Standard_Real BRepExtrema_DistShapeShape::Value() const
413 throw StdFail_NotDone("BRepExtrema_DistShapeShape::Value: There's no solution ");
418 //=======================================================================
419 //function : SupportOnShape1
421 //=======================================================================
423 TopoDS_Shape BRepExtrema_DistShapeShape::SupportOnShape1(const Standard_Integer N) const
426 throw StdFail_NotDone("BRepExtrema_DistShapeShape::SupportOnShape1: There's no solution ");
428 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
429 switch (sol.SupportKind())
431 case BRepExtrema_IsVertex : return sol.Vertex();
432 case BRepExtrema_IsOnEdge : return sol.Edge();
433 case BRepExtrema_IsInFace : return sol.Face();
435 return TopoDS_Shape();
438 //=======================================================================
439 //function : SupportOnShape2
441 //=======================================================================
443 TopoDS_Shape BRepExtrema_DistShapeShape::SupportOnShape2(const Standard_Integer N) const
446 throw StdFail_NotDone("BRepExtrema_DistShapeShape::SupportOnShape2: There's no solution ");
448 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
449 switch (sol.SupportKind())
451 case BRepExtrema_IsVertex : return sol.Vertex();
452 case BRepExtrema_IsOnEdge : return sol.Edge();
453 case BRepExtrema_IsInFace : return sol.Face();
455 return TopoDS_Shape();
458 //=======================================================================
459 //function : ParOnEdgeS1
461 //=======================================================================
463 void BRepExtrema_DistShapeShape::ParOnEdgeS1(const Standard_Integer N, Standard_Real& t) const
466 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnEdgeS1: There's no solution");
468 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
469 if (sol.SupportKind() != BRepExtrema_IsOnEdge)
470 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnEdgeS1: ParOnEdgeS1 is impossible without EDGE");
472 sol.EdgeParameter(t);
475 //=======================================================================
476 //function : ParOnEdgeS2
478 //=======================================================================
480 void BRepExtrema_DistShapeShape::ParOnEdgeS2(const Standard_Integer N, Standard_Real& t) const
483 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnEdgeS2: There's no solution");
485 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
486 if (sol.SupportKind() != BRepExtrema_IsOnEdge)
487 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnEdgeS2: ParOnEdgeS2 is impossible without EDGE");
489 sol.EdgeParameter(t);
492 //=======================================================================
493 //function : ParOnFaceS1
495 //=======================================================================
497 void BRepExtrema_DistShapeShape::ParOnFaceS1(const Standard_Integer N, Standard_Real& u, Standard_Real& v) const
500 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnFaceS1: There's no solution");
502 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
503 if (sol.SupportKind() != BRepExtrema_IsInFace)
504 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnFaceS1: ParOnFaceS1 is impossible without FACE");
506 sol.FaceParameter(u, v);
509 //=======================================================================
510 //function : ParOnFaceS2
512 //=======================================================================
514 void BRepExtrema_DistShapeShape::ParOnFaceS2(const Standard_Integer N, Standard_Real& u, Standard_Real& v) const
517 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnFaceS2: There's no solution");
519 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
520 if (sol.SupportKind() != BRepExtrema_IsInFace)
521 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnFaceS2:ParOnFaceS2 is impossible without FACE ");
523 sol.FaceParameter(u, v);
526 //=======================================================================
529 //=======================================================================
531 void BRepExtrema_DistShapeShape::Dump(Standard_OStream& o) const
536 o<< "the distance value is : " << Value()<<endl;
537 o<< "the number of solutions is :"<<NbSolution()<<endl;
539 for (i=1;i<=NbSolution();i++)
541 o<<"solution number "<<i<<": "<< endl;
542 o<<"the type of the solution on the first shape is " <<Standard_Integer( SupportTypeShape1(i)) <<endl;
543 o<<"the type of the solution on the second shape is "<<Standard_Integer( SupportTypeShape2(i))<< endl;
544 o<< "the coordinates of the point on the first shape are: "<<endl;
545 o<<"X=" <<PointOnShape1(i).X()<<" Y=" <<PointOnShape1(i).Y()<<" Z="<<PointOnShape1(i).Z()<<endl;
546 o<< "the coordinates of the point on the second shape are: "<<endl;
547 o<<"X="<< PointOnShape2(i).X()<< " Y="<<PointOnShape2(i).Y()<<" Z="<< PointOnShape2(i).Z()<<endl;
549 switch (SupportTypeShape1(i))
551 case BRepExtrema_IsVertex:
553 case BRepExtrema_IsOnEdge:
555 o << "parameter on the first edge : t= " << r1 << endl;
557 case BRepExtrema_IsInFace:
558 ParOnFaceS1(i,r1,r2);
559 o << "parameters on the first face : u= " << r1 << " v=" << r2 << endl;
562 switch (SupportTypeShape2(i))
564 case BRepExtrema_IsVertex:
566 case BRepExtrema_IsOnEdge:
568 o << "parameter on the second edge : t=" << r1 << endl;
570 case BRepExtrema_IsInFace:
571 ParOnFaceS2(i,r1,r2);
572 o << "parameters on the second face : u= " << r1 << " v=" << r2 << endl;