1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMUtils.hxx>
27 #include <Basics_OCCTVersion.hxx>
29 #include <utilities.h>
31 #include <Utils_ExceptHandlers.hxx>
34 #include <BRepMesh_IncrementalMesh.hxx>
36 #include <BRepExtrema_DistShapeShape.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRep_Tool.hxx>
40 #include <BRepBndLib.hxx>
41 #include <BRepGProp.hxx>
42 #include <BRepTools.hxx>
44 #include <BRepClass3d_SolidClassifier.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <Bnd_Box.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Compound.hxx>
58 #include <TopoDS_Iterator.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopTools_ListOfShape.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <TopTools_Array1OfShape.hxx>
65 #include <Geom_Circle.hxx>
66 #include <Geom_Surface.hxx>
67 #include <Geom_Plane.hxx>
68 #include <Geom_SphericalSurface.hxx>
69 #include <Geom_ToroidalSurface.hxx>
70 #include <Geom_RectangularTrimmedSurface.hxx>
72 #include <GeomLProp_CLProps.hxx>
73 #include <GeomLProp_SLProps.hxx>
75 #include <GProp_GProps.hxx>
76 #include <GProp_PrincipalProps.hxx>
78 #include <TColStd_Array1OfReal.hxx>
83 #include <ShapeAnalysis.hxx>
84 #include <ShapeFix_Shape.hxx>
86 #include <ProjLib.hxx>
91 #include <Standard_Failure.hxx>
92 #include <Standard_NullObject.hxx>
93 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
95 #define STD_SORT_ALGO 1
97 //=======================================================================
98 //function : GetPosition
100 //=======================================================================
101 gp_Ax3 GEOMUtils::GetPosition (const TopoDS_Shape& theShape)
105 if (theShape.IsNull())
109 aResult.Transform(theShape.Location().Transformation());
110 if (theShape.ShapeType() == TopAbs_FACE) {
111 Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(theShape));
112 if (!aGS.IsNull() && aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
113 Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast(aGS);
114 gp_Pln aPln = aGPlane->Pln();
115 aResult = aPln.Position();
116 // In case of reverse orinetation of the face invert the plane normal
117 // (the face's normal does not mathc the plane's normal in this case)
118 if(theShape.Orientation() == TopAbs_REVERSED)
120 gp_Dir Vx = aResult.XDirection();
121 gp_Dir N = aResult.Direction().Mirrored(Vx);
122 gp_Pnt P = aResult.Location();
123 aResult = gp_Ax3(P, N, Vx);
131 TopAbs_ShapeEnum aShType = theShape.ShapeType();
133 if (aShType == TopAbs_VERTEX) {
134 aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
137 if (aShType == TopAbs_COMPOUND) {
138 aShType = GetTypeOfSimplePart(theShape);
141 GProp_GProps aSystem;
142 if (aShType == TopAbs_EDGE || aShType == TopAbs_WIRE)
143 BRepGProp::LinearProperties(theShape, aSystem);
144 else if (aShType == TopAbs_FACE || aShType == TopAbs_SHELL)
145 BRepGProp::SurfaceProperties(theShape, aSystem);
147 BRepGProp::VolumeProperties(theShape, aSystem);
149 aPnt = aSystem.CentreOfMass();
152 aResult.SetLocation(aPnt);
157 //=======================================================================
158 //function : GetVector
160 //=======================================================================
161 gp_Vec GEOMUtils::GetVector (const TopoDS_Shape& theShape,
162 Standard_Boolean doConsiderOrientation)
164 if (theShape.IsNull())
165 Standard_NullObject::Raise("Null shape is given for a vector");
167 if (theShape.ShapeType() != TopAbs_EDGE)
168 Standard_TypeMismatch::Raise("Invalid shape is given, must be a vector or an edge");
170 TopoDS_Edge anE = TopoDS::Edge(theShape);
172 TopoDS_Vertex V1, V2;
173 TopExp::Vertices(anE, V1, V2, doConsiderOrientation);
175 if (V1.IsNull() || V2.IsNull())
176 Standard_NullObject::Raise("Invalid edge is given, it must have two points");
178 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
179 if (aV.Magnitude() < gp::Resolution()) {
180 Standard_ConstructionError::Raise("Vector of zero length is given");
186 //=======================================================================
187 //function : ShapeToDouble
188 //purpose : used by CompareShapes::operator()
189 //=======================================================================
190 std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S, bool isOldSorting)
192 // Computing of CentreOfMass
196 if (S.ShapeType() == TopAbs_VERTEX) {
197 GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
198 Len = (double)S.Orientation();
202 // BEGIN: fix for Mantis issue 0020842
204 BRepGProp::LinearProperties(S, GPr);
207 if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
208 BRepGProp::LinearProperties(S, GPr);
210 else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
211 BRepGProp::SurfaceProperties(S, GPr);
214 BRepGProp::VolumeProperties(S, GPr);
217 // END: fix for Mantis issue 0020842
218 GPoint = GPr.CentreOfMass();
222 double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
223 return std::make_pair(dMidXYZ, Len);
226 //=======================================================================
227 //function : CompareShapes::operator()
228 //purpose : used by std::sort(), called from SortShapes()
229 //=======================================================================
230 bool GEOMUtils::CompareShapes::operator() (const TopoDS_Shape& theShape1,
231 const TopoDS_Shape& theShape2)
233 if (!myMap.IsBound(theShape1)) {
234 myMap.Bind(theShape1, ShapeToDouble(theShape1, myIsOldSorting));
237 if (!myMap.IsBound(theShape2)) {
238 myMap.Bind(theShape2, ShapeToDouble(theShape2, myIsOldSorting));
241 std::pair<double, double> val1 = myMap.Find(theShape1);
242 std::pair<double, double> val2 = myMap.Find(theShape2);
244 double tol = Precision::Confusion();
245 bool exchange = Standard_False;
247 double dMidXYZ = val1.first - val2.first;
248 if (dMidXYZ >= tol) {
249 exchange = Standard_True;
251 else if (Abs(dMidXYZ) < tol) {
252 double dLength = val1.second - val2.second;
253 if (dLength >= tol) {
254 exchange = Standard_True;
256 else if (Abs(dLength) < tol && theShape1.ShapeType() <= TopAbs_FACE) {
258 // equal values possible on shapes such as two halves of a sphere and
259 // a membrane inside the sphere
261 BRepBndLib::Add(theShape1, box1);
262 if (!box1.IsVoid()) {
263 BRepBndLib::Add(theShape2, box2);
264 Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
265 if (dSquareExtent >= tol) {
266 exchange = Standard_True;
268 else if (Abs(dSquareExtent) < tol) {
269 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
270 box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
271 val1 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
272 box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
273 val2 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
274 if ((val1 - val2) >= tol) {
275 exchange = Standard_True;
282 //return val1 < val2;
286 //=======================================================================
287 //function : SortShapes
289 //=======================================================================
290 void GEOMUtils::SortShapes (TopTools_ListOfShape& SL,
291 const Standard_Boolean isOldSorting)
294 std::vector<TopoDS_Shape> aShapesVec;
295 aShapesVec.reserve(SL.Extent());
297 TopTools_ListIteratorOfListOfShape it (SL);
298 for (; it.More(); it.Next()) {
299 aShapesVec.push_back(it.Value());
303 CompareShapes shComp (isOldSorting);
304 std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
305 //std::sort(aShapesVec.begin(), aShapesVec.end(), shComp);
307 std::vector<TopoDS_Shape>::const_iterator anIter = aShapesVec.begin();
308 for (; anIter != aShapesVec.end(); ++anIter) {
312 // old implementation
313 Standard_Integer MaxShapes = SL.Extent();
314 TopTools_Array1OfShape aShapes (1,MaxShapes);
315 TColStd_Array1OfInteger OrderInd(1,MaxShapes);
316 TColStd_Array1OfReal MidXYZ (1,MaxShapes); //X,Y,Z;
317 TColStd_Array1OfReal Length (1,MaxShapes); //X,Y,Z;
319 // Computing of CentreOfMass
320 Standard_Integer Index;
323 TopTools_ListIteratorOfListOfShape it(SL);
324 for (Index=1; it.More(); Index++)
326 TopoDS_Shape S = it.Value();
327 SL.Remove( it ); // == it.Next()
329 OrderInd.SetValue (Index, Index);
330 if (S.ShapeType() == TopAbs_VERTEX) {
331 GPoint = BRep_Tool::Pnt( TopoDS::Vertex( S ));
332 Length.SetValue( Index, (Standard_Real) S.Orientation());
335 // BEGIN: fix for Mantis issue 0020842
337 BRepGProp::LinearProperties (S, GPr);
340 if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
341 BRepGProp::LinearProperties (S, GPr);
343 else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
344 BRepGProp::SurfaceProperties(S, GPr);
347 BRepGProp::VolumeProperties(S, GPr);
350 // END: fix for Mantis issue 0020842
351 GPoint = GPr.CentreOfMass();
352 Length.SetValue(Index, GPr.Mass());
354 MidXYZ.SetValue(Index, GPoint.X()*999.0 + GPoint.Y()*99.0 + GPoint.Z()*0.9);
355 //cout << Index << " L: " << Length(Index) << "CG: " << MidXYZ(Index) << endl;
359 Standard_Integer aTemp;
360 Standard_Boolean exchange, Sort = Standard_True;
361 Standard_Real tol = Precision::Confusion();
364 Sort = Standard_False;
365 for (Index=1; Index < MaxShapes; Index++)
367 exchange = Standard_False;
368 Standard_Real dMidXYZ = MidXYZ(OrderInd(Index)) - MidXYZ(OrderInd(Index+1));
369 Standard_Real dLength = Length(OrderInd(Index)) - Length(OrderInd(Index+1));
370 if ( dMidXYZ >= tol ) {
371 // cout << "MidXYZ: " << MidXYZ(OrderInd(Index))<< " > " <<MidXYZ(OrderInd(Index+1))
372 // << " d: " << dMidXYZ << endl;
373 exchange = Standard_True;
375 else if ( Abs(dMidXYZ) < tol && dLength >= tol ) {
376 // cout << "Length: " << Length(OrderInd(Index))<< " > " <<Length(OrderInd(Index+1))
377 // << " d: " << dLength << endl;
378 exchange = Standard_True;
380 else if ( Abs(dMidXYZ) < tol && Abs(dLength) < tol &&
381 aShapes(OrderInd(Index)).ShapeType() <= TopAbs_FACE) {
383 // equal values possible on shapes such as two halves of a sphere and
384 // a membrane inside the sphere
386 BRepBndLib::Add( aShapes( OrderInd(Index) ), box1 );
387 if ( box1.IsVoid() ) continue;
388 BRepBndLib::Add( aShapes( OrderInd(Index+1) ), box2 );
389 Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
390 if ( dSquareExtent >= tol ) {
391 // cout << "SquareExtent: " << box1.SquareExtent()<<" > "<<box2.SquareExtent() << endl;
392 exchange = Standard_True;
394 else if ( Abs(dSquareExtent) < tol ) {
395 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
396 box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
397 val1 = (aXmin+aXmax)*999 + (aYmin+aYmax)*99 + (aZmin+aZmax)*0.9;
398 box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
399 val2 = (aXmin+aXmax)*999 + (aYmin+aYmax)*99 + (aZmin+aZmax)*0.9;
400 //exchange = val1 > val2;
401 if ((val1 - val2) >= tol) {
402 exchange = Standard_True;
404 //cout << "box: " << val1<<" > "<<val2 << endl;
410 // cout << "exchange " << Index << " & " << Index+1 << endl;
411 aTemp = OrderInd(Index);
412 OrderInd(Index) = OrderInd(Index+1);
413 OrderInd(Index+1) = aTemp;
414 Sort = Standard_True;
419 for (Index=1; Index <= MaxShapes; Index++)
420 SL.Append( aShapes( OrderInd(Index) ));
424 //=======================================================================
425 //function : CompsolidToCompound
427 //=======================================================================
428 TopoDS_Shape GEOMUtils::CompsolidToCompound (const TopoDS_Shape& theCompsolid)
430 if (theCompsolid.ShapeType() != TopAbs_COMPSOLID) {
434 TopoDS_Compound aCompound;
436 B.MakeCompound(aCompound);
438 TopTools_MapOfShape mapShape;
439 TopoDS_Iterator It (theCompsolid, Standard_True, Standard_True);
441 for (; It.More(); It.Next()) {
442 TopoDS_Shape aShape_i = It.Value();
443 if (mapShape.Add(aShape_i)) {
444 B.Add(aCompound, aShape_i);
451 //=======================================================================
452 //function : AddSimpleShapes
454 //=======================================================================
455 void GEOMUtils::AddSimpleShapes (const TopoDS_Shape& theShape, TopTools_ListOfShape& theList)
457 if (theShape.ShapeType() != TopAbs_COMPOUND &&
458 theShape.ShapeType() != TopAbs_COMPSOLID) {
459 theList.Append(theShape);
463 TopTools_MapOfShape mapShape;
464 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
466 for (; It.More(); It.Next()) {
467 TopoDS_Shape aShape_i = It.Value();
468 if (mapShape.Add(aShape_i)) {
469 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
470 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
471 AddSimpleShapes(aShape_i, theList);
473 theList.Append(aShape_i);
479 //=======================================================================
480 //function : CheckTriangulation
482 //=======================================================================
483 bool GEOMUtils::CheckTriangulation (const TopoDS_Shape& aShape)
485 bool isTriangulation = true;
487 TopExp_Explorer exp (aShape, TopAbs_FACE);
490 TopLoc_Location aTopLoc;
491 Handle(Poly_Triangulation) aTRF;
492 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
494 isTriangulation = false;
497 else // no faces, try edges
499 TopExp_Explorer expe (aShape, TopAbs_EDGE);
503 TopLoc_Location aLoc;
504 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
506 isTriangulation = false;
510 if (!isTriangulation) {
511 // calculate deflection
512 Standard_Real aDeviationCoefficient = 0.001;
515 BRepBndLib::Add(aShape, B);
516 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
517 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
519 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
520 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
521 Standard_Real aHLRAngle = 0.349066;
523 BRepMesh_IncrementalMesh Inc (aShape, aDeflection, Standard_False, aHLRAngle);
529 //=======================================================================
530 //function : GetTypeOfSimplePart
532 //=======================================================================
533 TopAbs_ShapeEnum GEOMUtils::GetTypeOfSimplePart (const TopoDS_Shape& theShape)
535 TopAbs_ShapeEnum aType = theShape.ShapeType();
536 if (aType == TopAbs_VERTEX) return TopAbs_VERTEX;
537 else if (aType == TopAbs_EDGE || aType == TopAbs_WIRE) return TopAbs_EDGE;
538 else if (aType == TopAbs_FACE || aType == TopAbs_SHELL) return TopAbs_FACE;
539 else if (aType == TopAbs_SOLID || aType == TopAbs_COMPSOLID) return TopAbs_SOLID;
540 else if (aType == TopAbs_COMPOUND) {
541 // Only the iType of the first shape in the compound is taken into account
542 TopoDS_Iterator It (theShape, Standard_False, Standard_False);
544 return GetTypeOfSimplePart(It.Value());
550 //=======================================================================
551 //function : GetEdgeNearPoint
553 //=======================================================================
554 TopoDS_Shape GEOMUtils::GetEdgeNearPoint (const TopoDS_Shape& theShape,
555 const TopoDS_Vertex& thePoint)
557 TopoDS_Shape aResult;
559 // 1. Explode the shape on edges
560 TopTools_MapOfShape mapShape;
561 Standard_Integer nbEdges = 0;
562 TopExp_Explorer exp (theShape, TopAbs_EDGE);
563 for (; exp.More(); exp.Next()) {
564 if (mapShape.Add(exp.Current())) {
570 Standard_NullObject::Raise("Given shape contains no edges");
573 Standard_Integer ind = 1;
574 TopTools_Array1OfShape anEdges (1, nbEdges);
575 TColStd_Array1OfReal aDistances (1, nbEdges);
576 for (exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next()) {
577 if (mapShape.Add(exp.Current())) {
578 TopoDS_Shape anEdge = exp.Current();
579 anEdges(ind) = anEdge;
581 // 2. Classify the point relatively each edge
582 BRepExtrema_DistShapeShape aDistTool (thePoint, anEdges(ind));
583 if (!aDistTool.IsDone())
584 Standard_ConstructionError::Raise("Cannot find a distance from the given point to one of edges");
586 aDistances(ind) = aDistTool.Value();
591 // 3. Define edge, having minimum distance to the point
592 Standard_Real nearest = RealLast(), nbFound = 0;
593 Standard_Real prec = Precision::Confusion();
594 for (ind = 1; ind <= nbEdges; ind++) {
595 if (Abs(aDistances(ind) - nearest) < prec) {
598 else if (aDistances(ind) < nearest) {
599 nearest = aDistances(ind);
600 aResult = anEdges(ind);
607 Standard_ConstructionError::Raise("Multiple edges near the given point are found");
609 else if (nbFound == 0) {
610 Standard_ConstructionError::Raise("There are no edges near the given point");
618 //=======================================================================
619 //function : PreciseBoundingBox
621 //=======================================================================
622 Standard_Boolean GEOMUtils::PreciseBoundingBox
623 (const TopoDS_Shape &theShape, Bnd_Box &theBox)
625 Standard_Real aBound[6];
627 theBox.Get(aBound[0], aBound[2], aBound[4], aBound[1], aBound[3], aBound[5]);
630 const gp_Pnt aMid(0.5*(aBound[1] + aBound[0]), // XMid
631 0.5*(aBound[3] + aBound[2]), // YMid
632 0.5*(aBound[5] + aBound[4])); // ZMid
633 const gp_XYZ aSize(aBound[1] - aBound[0], // DX
634 aBound[3] - aBound[2], // DY
635 aBound[5] - aBound[4]); // DZ
636 const gp_Pnt aPnt[6] =
638 gp_Pnt(aBound[0] - (aBound[1] - aBound[0]), aMid.Y(), aMid.Z()), // XMin
639 gp_Pnt(aBound[1] + (aBound[1] - aBound[0]), aMid.Y(), aMid.Z()), // XMax
640 gp_Pnt(aMid.X(), aBound[2] - (aBound[3] - aBound[2]), aMid.Z()), // YMin
641 gp_Pnt(aMid.X(), aBound[3] + (aBound[3] - aBound[2]), aMid.Z()), // YMax
642 gp_Pnt(aMid.X(), aMid.Y(), aBound[4] - (aBound[5] - aBound[4])), // ZMin
643 gp_Pnt(aMid.X(), aMid.Y(), aBound[5] + (aBound[5] - aBound[4])) // ZMax
645 const gp_Dir aDir[3] = { gp::DX(), gp::DY(), gp::DZ() };
646 const Standard_Real aPlnSize[3] =
648 0.5*Max(aSize.Y(), aSize.Z()), // XMin, XMax planes
649 0.5*Max(aSize.X(), aSize.Z()), // YMin, YMax planes
650 0.5*Max(aSize.X(), aSize.Y()) // ZMin, ZMax planes
654 for (i = 0; i < 6; i++) {
655 const Standard_Integer iHalf = i/2;
656 const gp_Pln aPln(aPnt[i], aDir[iHalf]);
657 BRepBuilderAPI_MakeFace aMkFace(aPln, -aPlnSize[iHalf], aPlnSize[iHalf],
658 -aPlnSize[iHalf], aPlnSize[iHalf]);
660 if (!aMkFace.IsDone()) {
661 return Standard_False;
664 TopoDS_Shape aFace = aMkFace.Shape();
666 // Get minimal distance between planar face and shape.
667 Standard_Real aMinDist =
668 GEOMUtils::GetMinDistance(aFace, theShape, aPMin[0], aPMin[1]);
671 return Standard_False;
674 aBound[i] = aPMin[1].Coord(iHalf + 1);
677 // Update Bounding box with the new values.
679 theBox.Update(aBound[0], aBound[2], aBound[4], aBound[1], aBound[3], aBound[5]);
681 return Standard_True;
684 //=======================================================================
685 //function : GetMinDistanceSingular
687 //=======================================================================
688 double GEOMUtils::GetMinDistanceSingular(const TopoDS_Shape& aSh1,
689 const TopoDS_Shape& aSh2,
690 gp_Pnt& Ptmp1, gp_Pnt& Ptmp2)
694 Standard_Real AddDist1 = 0.;
695 Standard_Real AddDist2 = 0.;
696 Standard_Boolean IsChange1 = GEOMUtils::ModifyShape(aSh1, tmpSh1, AddDist1);
697 Standard_Boolean IsChange2 = GEOMUtils::ModifyShape(aSh2, tmpSh2, AddDist2);
699 if( !IsChange1 && !IsChange2 )
702 BRepExtrema_DistShapeShape dst(tmpSh1,tmpSh2);
704 double MinDist = 1.e9;
705 gp_Pnt PMin1, PMin2, P1, P2;
706 for (int i = 1; i <= dst.NbSolution(); i++) {
707 P1 = dst.PointOnShape1(i);
708 P2 = dst.PointOnShape2(i);
709 Standard_Real Dist = P1.Distance(P2);
710 if (MinDist > Dist) {
721 gp_Dir aDir(gp_Vec(PMin1,PMin2));
722 if( MinDist > (AddDist1+AddDist2) ) {
723 Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
724 PMin1.Y() + aDir.Y()*AddDist1,
725 PMin1.Z() + aDir.Z()*AddDist1 );
726 Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
727 PMin2.Y() - aDir.Y()*AddDist2,
728 PMin2.Z() - aDir.Z()*AddDist2 );
729 return (MinDist - AddDist1 - AddDist2);
733 Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
734 PMin1.Y() + aDir.Y()*AddDist1,
735 PMin1.Z() + aDir.Z()*AddDist1 );
739 Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
740 PMin2.Y() - aDir.Y()*AddDist2,
741 PMin2.Z() - aDir.Z()*AddDist2 );
746 double res = MinDist - AddDist1 - AddDist2;
747 if(res<0.) res = 0.0;
753 //=======================================================================
754 //function : GetMinDistance
756 //=======================================================================
757 Standard_Real GEOMUtils::GetMinDistance
758 (const TopoDS_Shape& theShape1,
759 const TopoDS_Shape& theShape2,
760 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
762 Standard_Real aResult = 1.e9;
764 // Issue 0020231: A min distance bug with torus and vertex.
765 // Make GetMinDistance() return zero if a sole VERTEX is inside any of SOLIDs
767 // which of shapes consists of only one vertex?
768 TopExp_Explorer exp1(theShape1,TopAbs_VERTEX), exp2(theShape2,TopAbs_VERTEX);
769 TopoDS_Shape V1 = exp1.More() ? exp1.Current() : TopoDS_Shape();
770 TopoDS_Shape V2 = exp2.More() ? exp2.Current() : TopoDS_Shape();
771 exp1.Next(); exp2.Next();
772 if ( exp1.More() ) V1.Nullify();
773 if ( exp2.More() ) V2.Nullify();
774 // vertex and container of solids
775 TopoDS_Shape V = V1.IsNull() ? V2 : V1;
776 TopoDS_Shape S = V1.IsNull() ? theShape1 : theShape2;
778 // classify vertex against solids
779 gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( V ) );
780 for ( exp1.Init( S, TopAbs_SOLID ); exp1.More(); exp1.Next() ) {
781 BRepClass3d_SolidClassifier classifier( exp1.Current(), p, 1e-6);
782 if ( classifier.State() == TopAbs_IN ) {
792 // additional workaround for bugs 19899, 19908 and 19910 from Mantis
793 double dist = GEOMUtils::GetMinDistanceSingular
794 (theShape1, theShape2, thePnt1, thePnt2);
800 BRepExtrema_DistShapeShape dst (theShape1, theShape2);
804 for (int i = 1; i <= dst.NbSolution(); i++) {
805 P1 = dst.PointOnShape1(i);
806 P2 = dst.PointOnShape2(i);
808 Standard_Real Dist = P1.Distance(P2);
809 if (aResult > Dist) {
820 //=======================================================================
821 // function : ConvertClickToPoint()
822 // purpose : Returns the point clicked in 3D view
823 //=======================================================================
824 gp_Pnt GEOMUtils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
826 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
827 aView->Eye( XEye, YEye, ZEye );
829 aView->At( XAt, YAt, ZAt );
830 gp_Pnt EyePoint( XEye, YEye, ZEye );
831 gp_Pnt AtPoint( XAt, YAt, ZAt );
833 gp_Vec EyeVector( EyePoint, AtPoint );
834 gp_Dir EyeDir( EyeVector );
836 gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
837 Standard_Real X, Y, Z;
838 aView->Convert( x, y, X, Y, Z );
839 gp_Pnt ConvertedPoint( X, Y, Z );
841 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
842 gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
846 //=======================================================================
847 // function : ModifyShape
849 //=======================================================================
850 Standard_Boolean GEOMUtils::ModifyShape(const TopoDS_Shape &theShape,
851 TopoDS_Shape &theModifiedShape,
852 Standard_Real &theAddDist)
854 Standard_Boolean isModified = Standard_False;
855 TopExp_Explorer anExp;
859 theModifiedShape.Nullify();
861 for ( anExp.Init( theShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
863 theModifiedShape = anExp.Current();
866 TopoDS_Shape sh = theShape;
867 while(sh.ShapeType()==TopAbs_COMPOUND) {
868 TopoDS_Iterator it(sh);
871 Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(theModifiedShape));
872 if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
873 S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ||
875 const Standard_Boolean isShell =
876 (sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE);
878 if( isShell || S->IsUPeriodic() ) {
879 // non solid case or any periodic surface (Mantis 22454).
881 // changes for 0020677: EDF 1219 GEOM: MinDistance gives 0 instead of 20.88
882 //S->Bounds(U1,U2,V1,V2); changed by
883 ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(theModifiedShape),U1,U2,V1,V2);
884 // end of changes for 020677 (dmv)
885 Handle(Geom_RectangularTrimmedSurface) TrS1 =
886 new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
887 Handle(Geom_RectangularTrimmedSurface) TrS2 =
888 new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
891 TopoDS_Shape aMShape;
894 B.MakeCompound(TopoDS::Compound(aMShape));
896 B.MakeShell(TopoDS::Shell(aMShape));
899 B.MakeFace(F1,TrS1,1.e-7);
901 B.MakeFace(F2,TrS2,1.e-7);
903 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
906 // The original shape is a solid.
910 B.Add(aSolid, aMShape);
915 sfs->SetPrecision(1.e-6);
916 sfs->SetMaxTolerance(1.0);
918 theModifiedShape = sfs->Shape();
919 isModified = Standard_True;
922 if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
923 Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
924 gp_Pnt PC = SS->Location();
927 B.MakeVertex(V,PC,1.e-7);
928 theModifiedShape = V;
929 theAddDist = SS->Radius();
930 isModified = Standard_True;
933 Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
934 gp_Ax3 ax3 = TS->Position();
935 Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
938 B.MakeEdge(E,C,1.e-7);
939 theModifiedShape = E;
940 theAddDist = TS->MinorRadius();
941 isModified = Standard_True;
945 theModifiedShape = theShape;
949 theModifiedShape = theShape;