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>
47 #include <BRepBuilderAPI_Sewing.hxx>
49 #include <Bnd_Box.hxx>
54 #include <TopoDS_Edge.hxx>
55 #include <TopoDS_Face.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <TopoDS_Compound.hxx>
59 #include <TopoDS_Iterator.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopTools_MapOfShape.hxx>
62 #include <TopTools_ListOfShape.hxx>
63 #include <TopTools_ListIteratorOfListOfShape.hxx>
64 #include <TopTools_Array1OfShape.hxx>
66 #include <Geom_Circle.hxx>
67 #include <Geom_Surface.hxx>
68 #include <Geom_Plane.hxx>
69 #include <Geom_SphericalSurface.hxx>
70 #include <Geom_ToroidalSurface.hxx>
71 #include <Geom_RectangularTrimmedSurface.hxx>
73 #include <GeomLProp_CLProps.hxx>
74 #include <GeomLProp_SLProps.hxx>
76 #include <GProp_GProps.hxx>
77 #include <GProp_PrincipalProps.hxx>
79 #include <TColStd_Array1OfReal.hxx>
84 #include <ShapeAnalysis.hxx>
85 #include <ShapeFix_Shape.hxx>
87 #include <ProjLib.hxx>
93 #include <Standard_Failure.hxx>
94 #include <Standard_NullObject.hxx>
95 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
97 #define STD_SORT_ALGO 1
102 * This function constructs and returns modified shape from the original one
103 * for singular cases. It is used for the method GetMinDistanceSingular.
105 * \param theShape the original shape
106 * \param theModifiedShape output parameter. The modified shape.
107 * \param theAddDist output parameter. The added distance for modified shape.
108 * \retval true if the shape is modified; false otherwise.
112 Standard_Boolean ModifyShape(const TopoDS_Shape &theShape,
113 TopoDS_Shape &theModifiedShape,
114 Standard_Real &theAddDist)
116 TopExp_Explorer anExp;
120 theModifiedShape.Nullify();
122 for ( anExp.Init( theShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
124 theModifiedShape = anExp.Current();
127 TopoDS_Shape sh = theShape;
128 while(sh.ShapeType()==TopAbs_COMPOUND) {
129 TopoDS_Iterator it(sh);
132 Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(theModifiedShape));
133 if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
134 S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ||
136 const Standard_Boolean isShell =
137 (sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE);
139 if ( !isShell && S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
140 Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
141 gp_Pnt PC = SS->Location();
144 B.MakeVertex(V,PC,1.e-7);
145 theModifiedShape = V;
146 theAddDist = SS->Radius();
147 return Standard_True;
149 if ( !isShell && S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
150 Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
151 gp_Ax3 ax3 = TS->Position();
152 Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
155 B.MakeEdge(E,C,1.e-7);
156 theModifiedShape = E;
157 theAddDist = TS->MinorRadius();
158 return Standard_True;
161 // non solid case or any periodic surface (Mantis 22454).
163 // changes for 0020677: EDF 1219 GEOM: MinDistance gives 0 instead of 20.88
164 //S->Bounds(U1,U2,V1,V2); changed by
165 ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(theModifiedShape),U1,U2,V1,V2);
166 // end of changes for 020677 (dmv)
167 Handle(Geom_RectangularTrimmedSurface) TrS1 =
168 new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
169 Handle(Geom_RectangularTrimmedSurface) TrS2 =
170 new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
171 TopoDS_Shape aMShape;
173 TopoDS_Face F1 = BRepBuilderAPI_MakeFace(TrS1, Precision::Confusion());
174 TopoDS_Face F2 = BRepBuilderAPI_MakeFace(TrS2, Precision::Confusion());
178 B.MakeCompound(TopoDS::Compound(aMShape));
182 // The original shape is a solid.
183 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
187 aMShape = aSewing.SewedShape();
191 B.Add(aSolid, aMShape);
195 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
197 sfs->SetPrecision(1.e-6);
198 sfs->SetMaxTolerance(1.0);
200 theModifiedShape = sfs->Shape();
201 return Standard_True;
205 theModifiedShape = theShape;
206 return Standard_False;
209 //=======================================================================
210 //function : ShapeToDouble
211 //purpose : used by CompareShapes::operator()
212 //=======================================================================
213 std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S, bool isOldSorting)
215 // Computing of CentreOfMass
219 if (S.ShapeType() == TopAbs_VERTEX) {
220 GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
221 Len = (double)S.Orientation();
225 // BEGIN: fix for Mantis issue 0020842
227 BRepGProp::LinearProperties(S, GPr);
230 if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
231 BRepGProp::LinearProperties(S, GPr);
233 else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
234 BRepGProp::SurfaceProperties(S, GPr);
237 BRepGProp::VolumeProperties(S, GPr);
240 // END: fix for Mantis issue 0020842
241 GPoint = GPr.CentreOfMass();
245 double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
246 return std::make_pair(dMidXYZ, Len);
249 void parseWard( const GEOMUtils::LevelsList &theLevelList, std::string &treeStr )
251 treeStr.append( "{" );
252 for( GEOMUtils::LevelsList::const_iterator j = theLevelList.begin();
253 j != theLevelList.end(); ++j ) {
254 if ( j != theLevelList.begin() ) {
255 treeStr.append( ";" );
257 GEOMUtils::LevelInfo level = (*j);
258 GEOMUtils::LevelInfo::iterator upIter;
259 for ( upIter = level.begin(); upIter != level.end(); ++upIter ) {
260 if ( upIter != level.begin() ) {
261 treeStr.append( "," );
263 treeStr.append( upIter->first );
264 for ( std::vector<std::string>::iterator k = upIter->second.begin(); k != upIter->second.end(); ++k ) {
265 treeStr.append( "_" );
266 treeStr.append( *k );
270 treeStr.append( "}" );
273 GEOMUtils::LevelsList parseWard( const std::string& theData, std::size_t& theCursor )
275 std::size_t indexStart = theData.find( "{", theCursor ) + 1;
276 std::size_t indexEnd = theData.find( "}", indexStart );
278 std::string ward = theData.substr( indexStart, indexEnd - indexStart );
279 std::stringstream ss(ward);
281 std::vector<std::string> levelsListStr;
282 while ( std::getline( ss, substr, ';' ) ) {
283 if ( !substr.empty() )
284 levelsListStr.push_back( substr );
286 GEOMUtils::LevelsList levelsListData;
287 for( int level = 0; level < levelsListStr.size(); level++ ) {
288 std::vector<std::string> namesListStr;
289 std::stringstream ss1( levelsListStr[level] );
290 while ( std::getline( ss1, substr, ',' ) ) {
291 if ( !substr.empty() )
292 namesListStr.push_back( substr );
294 GEOMUtils::LevelInfo levelInfoData;
295 for( int node = 0; node < namesListStr.size(); node++ ) {
296 std::vector<std::string> linksListStr;
297 std::stringstream ss2( namesListStr[node] );
298 while ( std::getline( ss2, substr, '_' ) ) {
299 if ( !substr.empty() )
300 linksListStr.push_back( substr );
302 std::string nodeItem = linksListStr[0];
303 if( !nodeItem.empty() ) {
304 GEOMUtils::NodeLinks linksListData;
305 for( int link = 1; link < linksListStr.size(); link++ ) {
306 std::string linkItem = linksListStr[link];
307 linksListData.push_back( linkItem );
309 levelInfoData[nodeItem] = linksListData;
312 levelsListData.push_back(levelInfoData);
315 theCursor = indexEnd + 1;
316 return levelsListData;
321 //=======================================================================
322 //function : GetPosition
324 //=======================================================================
325 gp_Ax3 GEOMUtils::GetPosition (const TopoDS_Shape& theShape)
329 if (theShape.IsNull())
333 aResult.Transform(theShape.Location().Transformation());
334 if (theShape.ShapeType() == TopAbs_FACE) {
335 Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(theShape));
336 if (!aGS.IsNull() && aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
337 Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast(aGS);
338 gp_Pln aPln = aGPlane->Pln();
339 aResult = aPln.Position();
340 // In case of reverse orinetation of the face invert the plane normal
341 // (the face's normal does not mathc the plane's normal in this case)
342 if(theShape.Orientation() == TopAbs_REVERSED)
344 gp_Dir Vx = aResult.XDirection();
345 gp_Dir N = aResult.Direction().Mirrored(Vx);
346 gp_Pnt P = aResult.Location();
347 aResult = gp_Ax3(P, N, Vx);
355 TopAbs_ShapeEnum aShType = theShape.ShapeType();
357 if (aShType == TopAbs_VERTEX) {
358 aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
361 if (aShType == TopAbs_COMPOUND) {
362 aShType = GetTypeOfSimplePart(theShape);
365 GProp_GProps aSystem;
366 if (aShType == TopAbs_EDGE || aShType == TopAbs_WIRE)
367 BRepGProp::LinearProperties(theShape, aSystem);
368 else if (aShType == TopAbs_FACE || aShType == TopAbs_SHELL)
369 BRepGProp::SurfaceProperties(theShape, aSystem);
371 BRepGProp::VolumeProperties(theShape, aSystem);
373 aPnt = aSystem.CentreOfMass();
376 aResult.SetLocation(aPnt);
381 //=======================================================================
382 //function : GetVector
384 //=======================================================================
385 gp_Vec GEOMUtils::GetVector (const TopoDS_Shape& theShape,
386 Standard_Boolean doConsiderOrientation)
388 if (theShape.IsNull())
389 Standard_NullObject::Raise("Null shape is given for a vector");
391 if (theShape.ShapeType() != TopAbs_EDGE)
392 Standard_TypeMismatch::Raise("Invalid shape is given, must be a vector or an edge");
394 TopoDS_Edge anE = TopoDS::Edge(theShape);
396 TopoDS_Vertex V1, V2;
397 TopExp::Vertices(anE, V1, V2, doConsiderOrientation);
399 if (V1.IsNull() || V2.IsNull())
400 Standard_NullObject::Raise("Invalid edge is given, it must have two points");
402 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
403 if (aV.Magnitude() < gp::Resolution()) {
404 Standard_ConstructionError::Raise("Vector of zero length is given");
410 //=======================================================================
411 //function : CompareShapes::operator()
412 //purpose : used by std::sort(), called from SortShapes()
413 //=======================================================================
414 bool GEOMUtils::CompareShapes::operator() (const TopoDS_Shape& theShape1,
415 const TopoDS_Shape& theShape2)
417 if (!myMap.IsBound(theShape1)) {
418 myMap.Bind(theShape1, ShapeToDouble(theShape1, myIsOldSorting));
421 if (!myMap.IsBound(theShape2)) {
422 myMap.Bind(theShape2, ShapeToDouble(theShape2, myIsOldSorting));
425 std::pair<double, double> val1 = myMap.Find(theShape1);
426 std::pair<double, double> val2 = myMap.Find(theShape2);
428 double tol = Precision::Confusion();
429 bool exchange = Standard_False;
431 double dMidXYZ = val1.first - val2.first;
432 if (dMidXYZ >= tol) {
433 exchange = Standard_True;
435 else if (Abs(dMidXYZ) < tol) {
436 double dLength = val1.second - val2.second;
437 if (dLength >= tol) {
438 exchange = Standard_True;
440 else if (Abs(dLength) < tol && theShape1.ShapeType() <= TopAbs_FACE) {
442 // equal values possible on shapes such as two halves of a sphere and
443 // a membrane inside the sphere
445 BRepBndLib::Add(theShape1, box1);
446 if (!box1.IsVoid()) {
447 BRepBndLib::Add(theShape2, box2);
448 Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
449 if (dSquareExtent >= tol) {
450 exchange = Standard_True;
452 else if (Abs(dSquareExtent) < tol) {
453 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
454 box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
455 val1 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
456 box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
457 val2 = (aXmin+aXmax)*999.0 + (aYmin+aYmax)*99.0 + (aZmin+aZmax)*0.9;
458 if ((val1 - val2) >= tol) {
459 exchange = Standard_True;
466 //return val1 < val2;
470 //=======================================================================
471 //function : SortShapes
473 //=======================================================================
474 void GEOMUtils::SortShapes (TopTools_ListOfShape& SL,
475 const Standard_Boolean isOldSorting)
478 std::vector<TopoDS_Shape> aShapesVec;
479 aShapesVec.reserve(SL.Extent());
481 TopTools_ListIteratorOfListOfShape it (SL);
482 for (; it.More(); it.Next()) {
483 aShapesVec.push_back(it.Value());
487 CompareShapes shComp (isOldSorting);
488 std::stable_sort(aShapesVec.begin(), aShapesVec.end(), shComp);
489 //std::sort(aShapesVec.begin(), aShapesVec.end(), shComp);
491 std::vector<TopoDS_Shape>::const_iterator anIter = aShapesVec.begin();
492 for (; anIter != aShapesVec.end(); ++anIter) {
496 // old implementation
497 Standard_Integer MaxShapes = SL.Extent();
498 TopTools_Array1OfShape aShapes (1,MaxShapes);
499 TColStd_Array1OfInteger OrderInd(1,MaxShapes);
500 TColStd_Array1OfReal MidXYZ (1,MaxShapes); //X,Y,Z;
501 TColStd_Array1OfReal Length (1,MaxShapes); //X,Y,Z;
503 // Computing of CentreOfMass
504 Standard_Integer Index;
507 TopTools_ListIteratorOfListOfShape it(SL);
508 for (Index=1; it.More(); Index++)
510 TopoDS_Shape S = it.Value();
511 SL.Remove( it ); // == it.Next()
513 OrderInd.SetValue (Index, Index);
514 if (S.ShapeType() == TopAbs_VERTEX) {
515 GPoint = BRep_Tool::Pnt( TopoDS::Vertex( S ));
516 Length.SetValue( Index, (Standard_Real) S.Orientation());
519 // BEGIN: fix for Mantis issue 0020842
521 BRepGProp::LinearProperties (S, GPr);
524 if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
525 BRepGProp::LinearProperties (S, GPr);
527 else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
528 BRepGProp::SurfaceProperties(S, GPr);
531 BRepGProp::VolumeProperties(S, GPr);
534 // END: fix for Mantis issue 0020842
535 GPoint = GPr.CentreOfMass();
536 Length.SetValue(Index, GPr.Mass());
538 MidXYZ.SetValue(Index, GPoint.X()*999.0 + GPoint.Y()*99.0 + GPoint.Z()*0.9);
539 //cout << Index << " L: " << Length(Index) << "CG: " << MidXYZ(Index) << endl;
543 Standard_Integer aTemp;
544 Standard_Boolean exchange, Sort = Standard_True;
545 Standard_Real tol = Precision::Confusion();
548 Sort = Standard_False;
549 for (Index=1; Index < MaxShapes; Index++)
551 exchange = Standard_False;
552 Standard_Real dMidXYZ = MidXYZ(OrderInd(Index)) - MidXYZ(OrderInd(Index+1));
553 Standard_Real dLength = Length(OrderInd(Index)) - Length(OrderInd(Index+1));
554 if ( dMidXYZ >= tol ) {
555 // cout << "MidXYZ: " << MidXYZ(OrderInd(Index))<< " > " <<MidXYZ(OrderInd(Index+1))
556 // << " d: " << dMidXYZ << endl;
557 exchange = Standard_True;
559 else if ( Abs(dMidXYZ) < tol && dLength >= tol ) {
560 // cout << "Length: " << Length(OrderInd(Index))<< " > " <<Length(OrderInd(Index+1))
561 // << " d: " << dLength << endl;
562 exchange = Standard_True;
564 else if ( Abs(dMidXYZ) < tol && Abs(dLength) < tol &&
565 aShapes(OrderInd(Index)).ShapeType() <= TopAbs_FACE) {
567 // equal values possible on shapes such as two halves of a sphere and
568 // a membrane inside the sphere
570 BRepBndLib::Add( aShapes( OrderInd(Index) ), box1 );
571 if ( box1.IsVoid() ) continue;
572 BRepBndLib::Add( aShapes( OrderInd(Index+1) ), box2 );
573 Standard_Real dSquareExtent = box1.SquareExtent() - box2.SquareExtent();
574 if ( dSquareExtent >= tol ) {
575 // cout << "SquareExtent: " << box1.SquareExtent()<<" > "<<box2.SquareExtent() << endl;
576 exchange = Standard_True;
578 else if ( Abs(dSquareExtent) < tol ) {
579 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, val1, val2;
580 box1.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
581 val1 = (aXmin+aXmax)*999 + (aYmin+aYmax)*99 + (aZmin+aZmax)*0.9;
582 box2.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
583 val2 = (aXmin+aXmax)*999 + (aYmin+aYmax)*99 + (aZmin+aZmax)*0.9;
584 //exchange = val1 > val2;
585 if ((val1 - val2) >= tol) {
586 exchange = Standard_True;
588 //cout << "box: " << val1<<" > "<<val2 << endl;
594 // cout << "exchange " << Index << " & " << Index+1 << endl;
595 aTemp = OrderInd(Index);
596 OrderInd(Index) = OrderInd(Index+1);
597 OrderInd(Index+1) = aTemp;
598 Sort = Standard_True;
603 for (Index=1; Index <= MaxShapes; Index++)
604 SL.Append( aShapes( OrderInd(Index) ));
608 //=======================================================================
609 //function : CompsolidToCompound
611 //=======================================================================
612 TopoDS_Shape GEOMUtils::CompsolidToCompound (const TopoDS_Shape& theCompsolid)
614 if (theCompsolid.ShapeType() != TopAbs_COMPSOLID) {
618 TopoDS_Compound aCompound;
620 B.MakeCompound(aCompound);
622 TopTools_MapOfShape mapShape;
623 TopoDS_Iterator It (theCompsolid, Standard_True, Standard_True);
625 for (; It.More(); It.Next()) {
626 TopoDS_Shape aShape_i = It.Value();
627 if (mapShape.Add(aShape_i)) {
628 B.Add(aCompound, aShape_i);
635 //=======================================================================
636 //function : AddSimpleShapes
638 //=======================================================================
639 void GEOMUtils::AddSimpleShapes (const TopoDS_Shape& theShape, TopTools_ListOfShape& theList)
641 if (theShape.ShapeType() != TopAbs_COMPOUND &&
642 theShape.ShapeType() != TopAbs_COMPSOLID) {
643 theList.Append(theShape);
647 TopTools_MapOfShape mapShape;
648 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
650 for (; It.More(); It.Next()) {
651 TopoDS_Shape aShape_i = It.Value();
652 if (mapShape.Add(aShape_i)) {
653 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
654 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
655 AddSimpleShapes(aShape_i, theList);
657 theList.Append(aShape_i);
663 //=======================================================================
664 //function : CheckTriangulation
666 //=======================================================================
667 bool GEOMUtils::CheckTriangulation (const TopoDS_Shape& aShape)
669 bool isTriangulation = true;
671 TopExp_Explorer exp (aShape, TopAbs_FACE);
674 TopLoc_Location aTopLoc;
675 Handle(Poly_Triangulation) aTRF;
676 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
678 isTriangulation = false;
681 else // no faces, try edges
683 TopExp_Explorer expe (aShape, TopAbs_EDGE);
687 TopLoc_Location aLoc;
688 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
690 isTriangulation = false;
694 if (!isTriangulation) {
695 // calculate deflection
696 Standard_Real aDeviationCoefficient = 0.001;
699 BRepBndLib::Add(aShape, B);
700 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
701 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
703 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
704 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
705 Standard_Real aHLRAngle = 0.349066;
707 BRepMesh_IncrementalMesh Inc (aShape, aDeflection, Standard_False, aHLRAngle);
713 //=======================================================================
714 //function : GetTypeOfSimplePart
716 //=======================================================================
717 TopAbs_ShapeEnum GEOMUtils::GetTypeOfSimplePart (const TopoDS_Shape& theShape)
719 TopAbs_ShapeEnum aType = theShape.ShapeType();
720 if (aType == TopAbs_VERTEX) return TopAbs_VERTEX;
721 else if (aType == TopAbs_EDGE || aType == TopAbs_WIRE) return TopAbs_EDGE;
722 else if (aType == TopAbs_FACE || aType == TopAbs_SHELL) return TopAbs_FACE;
723 else if (aType == TopAbs_SOLID || aType == TopAbs_COMPSOLID) return TopAbs_SOLID;
724 else if (aType == TopAbs_COMPOUND) {
725 // Only the iType of the first shape in the compound is taken into account
726 TopoDS_Iterator It (theShape, Standard_False, Standard_False);
728 return GetTypeOfSimplePart(It.Value());
734 //=======================================================================
735 //function : GetEdgeNearPoint
737 //=======================================================================
738 TopoDS_Shape GEOMUtils::GetEdgeNearPoint (const TopoDS_Shape& theShape,
739 const TopoDS_Vertex& thePoint)
741 TopoDS_Shape aResult;
743 // 1. Explode the shape on edges
744 TopTools_MapOfShape mapShape;
745 Standard_Integer nbEdges = 0;
746 TopExp_Explorer exp (theShape, TopAbs_EDGE);
747 for (; exp.More(); exp.Next()) {
748 if (mapShape.Add(exp.Current())) {
754 Standard_NullObject::Raise("Given shape contains no edges");
757 Standard_Integer ind = 1;
758 TopTools_Array1OfShape anEdges (1, nbEdges);
759 TColStd_Array1OfReal aDistances (1, nbEdges);
760 for (exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next()) {
761 if (mapShape.Add(exp.Current())) {
762 TopoDS_Shape anEdge = exp.Current();
763 anEdges(ind) = anEdge;
765 // 2. Classify the point relatively each edge
766 BRepExtrema_DistShapeShape aDistTool (thePoint, anEdges(ind));
767 if (!aDistTool.IsDone())
768 Standard_ConstructionError::Raise("Cannot find a distance from the given point to one of edges");
770 aDistances(ind) = aDistTool.Value();
775 // 3. Define edge, having minimum distance to the point
776 Standard_Real nearest = RealLast(), nbFound = 0;
777 Standard_Real prec = Precision::Confusion();
778 for (ind = 1; ind <= nbEdges; ind++) {
779 if (Abs(aDistances(ind) - nearest) < prec) {
782 else if (aDistances(ind) < nearest) {
783 nearest = aDistances(ind);
784 aResult = anEdges(ind);
791 Standard_ConstructionError::Raise("Multiple edges near the given point are found");
793 else if (nbFound == 0) {
794 Standard_ConstructionError::Raise("There are no edges near the given point");
802 //=======================================================================
803 //function : PreciseBoundingBox
805 //=======================================================================
806 Standard_Boolean GEOMUtils::PreciseBoundingBox
807 (const TopoDS_Shape &theShape, Bnd_Box &theBox)
809 if ( theBox.IsVoid() ) BRepBndLib::Add( theShape, theBox );
810 if ( theBox.IsVoid() ) return Standard_False;
812 Standard_Real aBound[6];
813 theBox.Get(aBound[0], aBound[2], aBound[4], aBound[1], aBound[3], aBound[5]);
816 const gp_Pnt aMid(0.5*(aBound[1] + aBound[0]), // XMid
817 0.5*(aBound[3] + aBound[2]), // YMid
818 0.5*(aBound[5] + aBound[4])); // ZMid
819 const gp_XYZ aSize(aBound[1] - aBound[0], // DX
820 aBound[3] - aBound[2], // DY
821 aBound[5] - aBound[4]); // DZ
822 const gp_Pnt aPnt[6] =
824 gp_Pnt(aBound[0] - (aBound[1] - aBound[0]), aMid.Y(), aMid.Z()), // XMin
825 gp_Pnt(aBound[1] + (aBound[1] - aBound[0]), aMid.Y(), aMid.Z()), // XMax
826 gp_Pnt(aMid.X(), aBound[2] - (aBound[3] - aBound[2]), aMid.Z()), // YMin
827 gp_Pnt(aMid.X(), aBound[3] + (aBound[3] - aBound[2]), aMid.Z()), // YMax
828 gp_Pnt(aMid.X(), aMid.Y(), aBound[4] - (aBound[5] - aBound[4])), // ZMin
829 gp_Pnt(aMid.X(), aMid.Y(), aBound[5] + (aBound[5] - aBound[4])) // ZMax
831 const gp_Dir aDir[3] = { gp::DX(), gp::DY(), gp::DZ() };
832 const Standard_Real aPlnSize[3] =
834 0.5*Max(aSize.Y(), aSize.Z()), // XMin, XMax planes
835 0.5*Max(aSize.X(), aSize.Z()), // YMin, YMax planes
836 0.5*Max(aSize.X(), aSize.Y()) // ZMin, ZMax planes
840 for (i = 0; i < 6; i++) {
841 const Standard_Integer iHalf = i/2;
842 const gp_Pln aPln(aPnt[i], aDir[iHalf]);
843 BRepBuilderAPI_MakeFace aMkFace(aPln, -aPlnSize[iHalf], aPlnSize[iHalf],
844 -aPlnSize[iHalf], aPlnSize[iHalf]);
846 if (!aMkFace.IsDone()) {
847 return Standard_False;
850 TopoDS_Shape aFace = aMkFace.Shape();
852 // Get minimal distance between planar face and shape.
853 Standard_Real aMinDist =
854 GEOMUtils::GetMinDistance(aFace, theShape, aPMin[0], aPMin[1]);
857 return Standard_False;
860 aBound[i] = aPMin[1].Coord(iHalf + 1);
863 // Update Bounding box with the new values.
865 theBox.Update(aBound[0], aBound[2], aBound[4], aBound[1], aBound[3], aBound[5]);
867 return Standard_True;
870 //=======================================================================
871 //function : GetMinDistanceSingular
873 //=======================================================================
874 double GEOMUtils::GetMinDistanceSingular(const TopoDS_Shape& aSh1,
875 const TopoDS_Shape& aSh2,
876 gp_Pnt& Ptmp1, gp_Pnt& Ptmp2)
880 Standard_Real AddDist1 = 0.;
881 Standard_Real AddDist2 = 0.;
882 Standard_Boolean IsChange1 = ModifyShape(aSh1, tmpSh1, AddDist1);
883 Standard_Boolean IsChange2 = ModifyShape(aSh2, tmpSh2, AddDist2);
885 if( !IsChange1 && !IsChange2 )
888 BRepExtrema_DistShapeShape dst(tmpSh1,tmpSh2);
890 double MinDist = 1.e9;
891 gp_Pnt PMin1, PMin2, P1, P2;
892 for (int i = 1; i <= dst.NbSolution(); i++) {
893 P1 = dst.PointOnShape1(i);
894 P2 = dst.PointOnShape2(i);
895 Standard_Real Dist = P1.Distance(P2);
896 if (MinDist > Dist) {
907 gp_Dir aDir(gp_Vec(PMin1,PMin2));
908 if( MinDist > (AddDist1+AddDist2) ) {
909 Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
910 PMin1.Y() + aDir.Y()*AddDist1,
911 PMin1.Z() + aDir.Z()*AddDist1 );
912 Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
913 PMin2.Y() - aDir.Y()*AddDist2,
914 PMin2.Z() - aDir.Z()*AddDist2 );
915 return (MinDist - AddDist1 - AddDist2);
919 Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
920 PMin1.Y() + aDir.Y()*AddDist1,
921 PMin1.Z() + aDir.Z()*AddDist1 );
925 Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
926 PMin2.Y() - aDir.Y()*AddDist2,
927 PMin2.Z() - aDir.Z()*AddDist2 );
932 double res = MinDist - AddDist1 - AddDist2;
933 if(res<0.) res = 0.0;
939 //=======================================================================
940 //function : GetMinDistance
942 //=======================================================================
943 Standard_Real GEOMUtils::GetMinDistance
944 (const TopoDS_Shape& theShape1,
945 const TopoDS_Shape& theShape2,
946 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
948 Standard_Real aResult = 1.e9;
950 // Issue 0020231: A min distance bug with torus and vertex.
951 // Make GetMinDistance() return zero if a sole VERTEX is inside any of SOLIDs
953 // which of shapes consists of only one vertex?
954 TopExp_Explorer exp1(theShape1,TopAbs_VERTEX), exp2(theShape2,TopAbs_VERTEX);
955 TopoDS_Shape V1 = exp1.More() ? exp1.Current() : TopoDS_Shape();
956 TopoDS_Shape V2 = exp2.More() ? exp2.Current() : TopoDS_Shape();
957 exp1.Next(); exp2.Next();
958 if ( exp1.More() ) V1.Nullify();
959 if ( exp2.More() ) V2.Nullify();
960 // vertex and container of solids
961 TopoDS_Shape V = V1.IsNull() ? V2 : V1;
962 TopoDS_Shape S = V1.IsNull() ? theShape1 : theShape2;
964 // classify vertex against solids
965 gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( V ) );
966 for ( exp1.Init( S, TopAbs_SOLID ); exp1.More(); exp1.Next() ) {
967 BRepClass3d_SolidClassifier classifier( exp1.Current(), p, 1e-6);
968 if ( classifier.State() == TopAbs_IN ) {
978 // additional workaround for bugs 19899, 19908 and 19910 from Mantis
979 double dist = GEOMUtils::GetMinDistanceSingular
980 (theShape1, theShape2, thePnt1, thePnt2);
986 BRepExtrema_DistShapeShape dst (theShape1, theShape2);
990 for (int i = 1; i <= dst.NbSolution(); i++) {
991 P1 = dst.PointOnShape1(i);
992 P2 = dst.PointOnShape2(i);
994 Standard_Real Dist = P1.Distance(P2);
995 if (aResult > Dist) {
1006 //=======================================================================
1007 // function : ConvertClickToPoint()
1008 // purpose : Returns the point clicked in 3D view
1009 //=======================================================================
1010 gp_Pnt GEOMUtils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
1012 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
1013 aView->Eye( XEye, YEye, ZEye );
1015 aView->At( XAt, YAt, ZAt );
1016 gp_Pnt EyePoint( XEye, YEye, ZEye );
1017 gp_Pnt AtPoint( XAt, YAt, ZAt );
1019 gp_Vec EyeVector( EyePoint, AtPoint );
1020 gp_Dir EyeDir( EyeVector );
1022 gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
1023 Standard_Real X, Y, Z;
1024 aView->Convert( x, y, X, Y, Z );
1025 gp_Pnt ConvertedPoint( X, Y, Z );
1027 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
1028 gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
1032 //=======================================================================
1033 // function : ConvertTreeToString()
1034 // purpose : Returns the string representation of dependency tree
1035 //=======================================================================
1036 void GEOMUtils::ConvertTreeToString( const TreeModel &tree,
1037 std::string &treeStr )
1039 TreeModel::const_iterator i;
1040 for ( i = tree.begin(); i != tree.end(); ++i ) {
1041 treeStr.append( i->first );
1042 treeStr.append( "-" );
1043 std::vector<LevelInfo> upLevelList = i->second.first;
1044 treeStr.append( "upward" );
1045 parseWard( upLevelList, treeStr );
1046 std::vector<LevelInfo> downLevelList = i->second.second;
1047 treeStr.append( "downward" );
1048 parseWard( downLevelList, treeStr );
1052 //=======================================================================
1053 // function : ConvertStringToTree()
1054 // purpose : Returns the dependency tree
1055 //=======================================================================
1056 void GEOMUtils::ConvertStringToTree( const std::string &theData,
1059 std::size_t cursor = 0;
1061 while( theData.find('-',cursor) != std::string::npos ) //find next selected object
1063 std::size_t objectIndex = theData.find( '-', cursor );
1064 std::string objectEntry = theData.substr( cursor, objectIndex - cursor );
1065 cursor = objectIndex;
1067 std::size_t upwardIndexBegin = theData.find("{",cursor) + 1;
1068 std::size_t upwardIndexFinish = theData.find("}",upwardIndexBegin);
1069 LevelsList upwardList = parseWard( theData, cursor );
1071 LevelsList downwardList = parseWard( theData, cursor );
1073 tree[objectEntry] = std::pair<LevelsList,LevelsList>( upwardList, downwardList );