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_Vector.hxx>
39 #include <StdFail_NotDone.hxx>
45 static void Decomposition(const TopoDS_Shape& S,
46 TopTools_IndexedMapOfShape& MapV,
47 TopTools_IndexedMapOfShape& MapE,
48 TopTools_IndexedMapOfShape& MapF)
53 TopExp::MapShapes(S,TopAbs_VERTEX,MapV);
54 TopExp::MapShapes(S,TopAbs_EDGE,MapE);
55 TopExp::MapShapes(S,TopAbs_FACE,MapF);
58 static void BoxCalculation(const TopTools_IndexedMapOfShape& Map,
61 for (Standard_Integer i = 1; i <= Map.Extent(); i++)
64 BRepBndLib::Add(Map(i), box);
69 inline Standard_Real DistanceInitiale(const TopoDS_Vertex V1,
70 const TopoDS_Vertex V2)
72 return (BRep_Tool::Pnt(V1).Distance(BRep_Tool::Pnt(V2)));
75 //! Pair of objects to check extrema.
76 struct BRepExtrema_CheckPair
78 Standard_Integer Index1; //!< Index of the 1st sub-shape
79 Standard_Integer Index2; //!< Index of the 2nd sub-shape
80 Standard_Real Distance; //!< Distance between sub-shapes
82 //! Uninitialized constructor for collection.
83 BRepExtrema_CheckPair()
90 //! Creates new pair of sub-shapes.
91 BRepExtrema_CheckPair (Standard_Integer theIndex1,
92 Standard_Integer theIndex2,
93 Standard_Real theDistance)
96 Distance (theDistance) {}
99 // Used by std::sort function
100 static Standard_Boolean BRepExtrema_CheckPair_Comparator (const BRepExtrema_CheckPair& theLeft,
101 const BRepExtrema_CheckPair& theRight)
103 return (theLeft.Distance < theRight.Distance);
107 //=======================================================================
108 //function : DistanceMapMap
110 //=======================================================================
112 void BRepExtrema_DistShapeShape::DistanceMapMap (const TopTools_IndexedMapOfShape& theMap1,
113 const TopTools_IndexedMapOfShape& theMap2,
114 const Bnd_SeqOfBox& theLBox1,
115 const Bnd_SeqOfBox& theLBox2)
117 NCollection_Vector<BRepExtrema_CheckPair> aPairList;
118 const Standard_Integer aCount1 = theMap1.Extent();
119 const Standard_Integer aCount2 = theMap2.Extent();
120 for (Standard_Integer anIdx1 = 1; anIdx1 <= aCount1; ++anIdx1)
122 for (Standard_Integer anIdx2 = 1; anIdx2 <= aCount2; ++anIdx2)
124 const Bnd_Box& aBox1 = theLBox1.Value (anIdx1);
125 const Bnd_Box& aBox2 = theLBox2.Value (anIdx2);
132 const Standard_Real aDist = aBox1.Distance (aBox2);
133 if (aDist < myDistRef - myEps || fabs (aDist - myDistRef) < myEps)
135 aPairList.Append (BRepExtrema_CheckPair (anIdx1, anIdx2, aDist));
140 std::stable_sort(aPairList.begin(), aPairList.end(), BRepExtrema_CheckPair_Comparator);
142 for (NCollection_Vector<BRepExtrema_CheckPair>::Iterator aPairIter (aPairList);
143 aPairIter.More(); aPairIter.Next())
145 const BRepExtrema_CheckPair& aPair = aPairIter.Value();
146 if (aPair.Distance > myDistRef + myEps)
148 break; // early search termination
151 const Bnd_Box& aBox1 = theLBox1.Value (aPair.Index1);
152 const Bnd_Box& aBox2 = theLBox2.Value (aPair.Index2);
154 const TopoDS_Shape& aShape1 = theMap1 (aPair.Index1);
155 const TopoDS_Shape& aShape2 = theMap2 (aPair.Index2);
157 BRepExtrema_DistanceSS aDistTool (aShape1, aShape2, aBox1, aBox2, myDistRef, myEps);
158 if (aDistTool.IsDone())
160 if (aDistTool.DistValue() < myDistRef - myEps)
162 mySolutionsShape1.Clear();
163 mySolutionsShape2.Clear();
165 BRepExtrema_SeqOfSolution aSeq1 = aDistTool.Seq1Value();
166 BRepExtrema_SeqOfSolution aSeq2 = aDistTool.Seq2Value();
168 mySolutionsShape1.Append (aSeq1);
169 mySolutionsShape2.Append (aSeq2);
171 myDistRef = aDistTool.DistValue();
173 else if (fabs (aDistTool.DistValue() - myDistRef) < myEps)
175 BRepExtrema_SeqOfSolution aSeq1 = aDistTool.Seq1Value();
176 BRepExtrema_SeqOfSolution aSeq2 = aDistTool.Seq2Value();
178 mySolutionsShape1.Append (aSeq1);
179 mySolutionsShape2.Append (aSeq2);
181 if (myDistRef > aDistTool.DistValue())
183 myDistRef = aDistTool.DistValue();
190 //=======================================================================
191 //function : BRepExtrema_DistShapeShape
193 //=======================================================================
195 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape()
197 myIsDone (Standard_False),
198 myInnerSol (Standard_False),
199 myEps (Precision::Confusion()),
200 myIsInitS1 (Standard_False),
201 myIsInitS2 (Standard_False),
202 myFlag (Extrema_ExtFlag_MINMAX),
203 myAlgo (Extrema_ExtAlgo_Grad)
208 //=======================================================================
209 //function : BRepExtrema_DistShapeShape
211 //=======================================================================
212 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape(const TopoDS_Shape& Shape1,
213 const TopoDS_Shape& Shape2,
214 const Extrema_ExtFlag F,
215 const Extrema_ExtAlgo A)
217 myIsDone (Standard_False),
218 myInnerSol (Standard_False),
219 myEps (Precision::Confusion()),
220 myIsInitS1 (Standard_False),
221 myIsInitS2 (Standard_False),
230 //=======================================================================
231 //function : BRepExtrema_DistShapeShape
233 //=======================================================================
235 BRepExtrema_DistShapeShape::BRepExtrema_DistShapeShape(const TopoDS_Shape& Shape1,
236 const TopoDS_Shape& Shape2,
237 const Standard_Real theDeflection,
238 const Extrema_ExtFlag F,
239 const Extrema_ExtAlgo A)
241 myIsDone (Standard_False),
242 myInnerSol (Standard_False),
243 myEps (theDeflection),
244 myIsInitS1 (Standard_False),
245 myIsInitS2 (Standard_False),
254 //=======================================================================
257 //=======================================================================
259 void BRepExtrema_DistShapeShape::LoadS1 (const TopoDS_Shape& Shape1)
262 myIsInitS1 = Standard_False;
263 Decomposition (Shape1, myMapV1, myMapE1, myMapF1);
266 //=======================================================================
269 //=======================================================================
271 void BRepExtrema_DistShapeShape::LoadS2 (const TopoDS_Shape& Shape2)
274 myIsInitS2 = Standard_False;
275 Decomposition (Shape2, myMapV2, myMapE2, myMapF2);
278 //=======================================================================
281 //=======================================================================
283 Standard_Boolean BRepExtrema_DistShapeShape::Perform()
285 myIsDone=Standard_False;
286 myInnerSol=Standard_False;
287 mySolutionsShape1.Clear();
288 mySolutionsShape2.Clear();
290 if ( myShape1.IsNull() || myShape2.IsNull() )
291 return Standard_False;
294 const Standard_Real tol = 0.001;
296 // Treatment of solids
297 const TopAbs_ShapeEnum Type1 = myShape1.ShapeType();
298 if ((Type1==TopAbs_SOLID) || (Type1 == TopAbs_COMPSOLID))
300 BRepClass3d_SolidClassifier Classi(myShape1);
301 const Standard_Integer nbv2 = myMapV2.Extent();
302 Standard_Integer nbv1 = 0;
303 while ( (nbv1<nbv2) && (!myInnerSol) )
306 V = TopoDS::Vertex(myMapV2(nbv1));
307 const gp_Pnt &P = BRep_Tool::Pnt(V);
308 Classi.Perform(P,tol);
309 if (Classi.State()==TopAbs_IN)
311 myInnerSol = Standard_True;
313 myIsDone = Standard_True;
314 BRepExtrema_SolutionElem Sol(0,P,BRepExtrema_IsVertex,V);
315 mySolutionsShape1.Append(Sol);
316 mySolutionsShape2.Append(Sol);
321 const TopAbs_ShapeEnum Type2 = myShape2.ShapeType();
322 if (((Type2==TopAbs_SOLID) || (Type2==TopAbs_COMPSOLID)) && (!myInnerSol))
324 BRepClass3d_SolidClassifier Classi(myShape2);
325 const Standard_Integer nbv1 = myMapV1.Extent();
326 Standard_Integer nbv2 = 0;
327 while ( (nbv2<nbv1) && (!myInnerSol) )
330 V = TopoDS::Vertex(myMapV1(nbv2));
331 const gp_Pnt &P = BRep_Tool::Pnt(V);
332 Classi.Perform(P,tol);
333 if (Classi.State()==TopAbs_IN) {
334 myInnerSol = Standard_True;
336 myIsDone = Standard_True;
337 BRepExtrema_SolutionElem Sol (0,P,BRepExtrema_IsVertex,V);
338 mySolutionsShape1.Append(Sol);
339 mySolutionsShape2.Append(Sol);
346 if (!myIsInitS1) // rebuild cached data for 1st shape
352 BoxCalculation (myMapV1, myBV1);
353 BoxCalculation (myMapE1, myBE1);
354 BoxCalculation (myMapF1, myBF1);
356 myIsInitS1 = Standard_True;
359 if (!myIsInitS2) // rebuild cached data for 2nd shape
365 BoxCalculation (myMapV2, myBV2);
366 BoxCalculation (myMapE2, myBE2);
367 BoxCalculation (myMapF2, myBF2);
369 myIsInitS2 = Standard_True;
372 if (myMapV1.Extent() && myMapV2.Extent())
374 TopoDS_Vertex V1 = TopoDS::Vertex(myMapV1(1));
375 TopoDS_Vertex V2 = TopoDS::Vertex(myMapV2(1));
376 myDistRef = DistanceInitiale(V1, V2);
379 myDistRef= 1.e30; //szv:!!!
381 DistanceMapMap (myMapV1, myMapV2, myBV1, myBV2);
382 DistanceMapMap (myMapV1, myMapE2, myBV1, myBE2);
383 DistanceMapMap (myMapE1, myMapV2, myBE1, myBV2);
384 DistanceMapMap (myMapV1, myMapF2, myBV1, myBF2);
385 DistanceMapMap (myMapF1, myMapV2, myBF1, myBV2);
386 DistanceMapMap (myMapE1, myMapE2, myBE1, myBE2);
387 DistanceMapMap (myMapE1, myMapF2, myBE1, myBF2);
388 DistanceMapMap (myMapF1, myMapE2, myBF1, myBE2);
390 if (fabs (myDistRef) > myEps)
392 DistanceMapMap (myMapF1, myMapF2, myBF1, myBF2);
395 // Modified by Sergey KHROMOV - Tue Mar 6 11:55:03 2001 Begin
396 Standard_Integer i = 1;
397 for (; i <= mySolutionsShape1.Length(); i++)
398 if (mySolutionsShape1.Value(i).Dist() > myDistRef + myEps)
400 mySolutionsShape1.Remove(i);
401 mySolutionsShape2.Remove(i);
403 // Modified by Sergey KHROMOV - Tue Mar 6 11:55:04 2001 End
404 myIsDone = ( mySolutionsShape1.Length() > 0 );
409 //=======================================================================
412 //=======================================================================
414 Standard_Real BRepExtrema_DistShapeShape::Value() const
417 throw StdFail_NotDone("BRepExtrema_DistShapeShape::Value: There's no solution ");
422 //=======================================================================
423 //function : SupportOnShape1
425 //=======================================================================
427 TopoDS_Shape BRepExtrema_DistShapeShape::SupportOnShape1(const Standard_Integer N) const
430 throw StdFail_NotDone("BRepExtrema_DistShapeShape::SupportOnShape1: There's no solution ");
432 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
433 switch (sol.SupportKind())
435 case BRepExtrema_IsVertex : return sol.Vertex();
436 case BRepExtrema_IsOnEdge : return sol.Edge();
437 case BRepExtrema_IsInFace : return sol.Face();
439 return TopoDS_Shape();
442 //=======================================================================
443 //function : SupportOnShape2
445 //=======================================================================
447 TopoDS_Shape BRepExtrema_DistShapeShape::SupportOnShape2(const Standard_Integer N) const
450 throw StdFail_NotDone("BRepExtrema_DistShapeShape::SupportOnShape2: There's no solution ");
452 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
453 switch (sol.SupportKind())
455 case BRepExtrema_IsVertex : return sol.Vertex();
456 case BRepExtrema_IsOnEdge : return sol.Edge();
457 case BRepExtrema_IsInFace : return sol.Face();
459 return TopoDS_Shape();
462 //=======================================================================
463 //function : ParOnEdgeS1
465 //=======================================================================
467 void BRepExtrema_DistShapeShape::ParOnEdgeS1(const Standard_Integer N, Standard_Real& t) const
470 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnEdgeS1: There's no solution");
472 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
473 if (sol.SupportKind() != BRepExtrema_IsOnEdge)
474 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnEdgeS1: ParOnEdgeS1 is impossible without EDGE");
476 sol.EdgeParameter(t);
479 //=======================================================================
480 //function : ParOnEdgeS2
482 //=======================================================================
484 void BRepExtrema_DistShapeShape::ParOnEdgeS2(const Standard_Integer N, Standard_Real& t) const
487 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnEdgeS2: There's no solution");
489 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
490 if (sol.SupportKind() != BRepExtrema_IsOnEdge)
491 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnEdgeS2: ParOnEdgeS2 is impossible without EDGE");
493 sol.EdgeParameter(t);
496 //=======================================================================
497 //function : ParOnFaceS1
499 //=======================================================================
501 void BRepExtrema_DistShapeShape::ParOnFaceS1(const Standard_Integer N, Standard_Real& u, Standard_Real& v) const
504 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnFaceS1: There's no solution");
506 const BRepExtrema_SolutionElem &sol = mySolutionsShape1.Value(N);
507 if (sol.SupportKind() != BRepExtrema_IsInFace)
508 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnFaceS1: ParOnFaceS1 is impossible without FACE");
510 sol.FaceParameter(u, v);
513 //=======================================================================
514 //function : ParOnFaceS2
516 //=======================================================================
518 void BRepExtrema_DistShapeShape::ParOnFaceS2(const Standard_Integer N, Standard_Real& u, Standard_Real& v) const
521 throw StdFail_NotDone("BRepExtrema_DistShapeShape::ParOnFaceS2: There's no solution");
523 const BRepExtrema_SolutionElem &sol = mySolutionsShape2.Value(N);
524 if (sol.SupportKind() != BRepExtrema_IsInFace)
525 throw BRepExtrema_UnCompatibleShape("BRepExtrema_DistShapeShape::ParOnFaceS2:ParOnFaceS2 is impossible without FACE ");
527 sol.FaceParameter(u, v);
530 //=======================================================================
533 //=======================================================================
535 void BRepExtrema_DistShapeShape::Dump(Standard_OStream& o) const
540 o<< "the distance value is : " << Value()<<std::endl;
541 o<< "the number of solutions is :"<<NbSolution()<<std::endl;
543 for (i=1;i<=NbSolution();i++)
545 o<<"solution number "<<i<<": "<< std::endl;
546 o<<"the type of the solution on the first shape is " <<Standard_Integer( SupportTypeShape1(i)) <<std::endl;
547 o<<"the type of the solution on the second shape is "<<Standard_Integer( SupportTypeShape2(i))<< std::endl;
548 o<< "the coordinates of the point on the first shape are: "<<std::endl;
549 o<<"X=" <<PointOnShape1(i).X()<<" Y=" <<PointOnShape1(i).Y()<<" Z="<<PointOnShape1(i).Z()<<std::endl;
550 o<< "the coordinates of the point on the second shape are: "<<std::endl;
551 o<<"X="<< PointOnShape2(i).X()<< " Y="<<PointOnShape2(i).Y()<<" Z="<< PointOnShape2(i).Z()<<std::endl;
553 switch (SupportTypeShape1(i))
555 case BRepExtrema_IsVertex:
557 case BRepExtrema_IsOnEdge:
559 o << "parameter on the first edge : t= " << r1 << std::endl;
561 case BRepExtrema_IsInFace:
562 ParOnFaceS1(i,r1,r2);
563 o << "parameters on the first face : u= " << r1 << " v=" << r2 << std::endl;
566 switch (SupportTypeShape2(i))
568 case BRepExtrema_IsVertex:
570 case BRepExtrema_IsOnEdge:
572 o << "parameter on the second edge : t=" << r1 << std::endl;
574 case BRepExtrema_IsInFace:
575 ParOnFaceS2(i,r1,r2);
576 o << "parameters on the second face : u= " << r1 << " v=" << r2 << std::endl;