1 // Created on: 2016-04-19
2 // Copyright (c) 2016 OPEN CASCADE SAS
3 // Created by: Oleg AGASHIN
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <BRepMesh_Deflection.hxx>
18 #include <Bnd_Box.hxx>
19 #include <BRepBndLib.hxx>
20 #include <BRepMesh_ShapeTool.hxx>
21 #include <IMeshData_Edge.hxx>
22 #include <IMeshData_Wire.hxx>
23 #include <IMeshTools_Parameters.hxx>
25 #include <TopoDS_Vertex.hxx>
27 //=======================================================================
28 //function : RelativeEdgeDeflection
30 //=======================================================================
31 Standard_Real BRepMesh_Deflection::ComputeAbsoluteDeflection(
32 const TopoDS_Shape& theShape,
33 const Standard_Real theRelativeDeflection,
34 const Standard_Real theMaxShapeSize)
36 if (theShape.IsNull())
38 return theRelativeDeflection;
42 BRepBndLib::Add (theShape, aBox, Standard_False);
44 Standard_Real aShapeSize = theRelativeDeflection;
45 BRepMesh_ShapeTool::BoxMaxDimension (aBox, aShapeSize);
47 // Adjust resulting value in relation to the total size
49 Standard_Real aX1, aY1, aZ1, aX2, aY2, aZ2;
50 aBox.Get(aX1, aY1, aZ1, aX2, aY2, aZ2);
51 const Standard_Real aMaxShapeSize = (theMaxShapeSize > 0.0) ? theMaxShapeSize :
52 Max(aX2 - aX1, Max(aY2 - aY1, aZ2 - aZ1));
54 Standard_Real anAdjustmentCoefficient = aMaxShapeSize / (2 * aShapeSize);
55 if (anAdjustmentCoefficient < 0.5)
57 anAdjustmentCoefficient = 0.5;
59 else if (anAdjustmentCoefficient > 2.)
61 anAdjustmentCoefficient = 2.;
64 return (anAdjustmentCoefficient * aShapeSize * theRelativeDeflection);
67 //=======================================================================
68 // Function: ComputeDeflection (edge)
70 //=======================================================================
71 void BRepMesh_Deflection::ComputeDeflection (
72 const IMeshData::IEdgeHandle& theDEdge,
73 const Standard_Real theMaxShapeSize,
74 const IMeshTools_Parameters& theParameters)
76 const Standard_Real aAngDeflection = theParameters.Angle;
77 Standard_Real aLinDeflection =
78 !theParameters.Relative ? theParameters.Deflection :
79 ComputeAbsoluteDeflection(theDEdge->GetEdge(),
80 theParameters.Deflection,
83 const TopoDS_Edge& anEdge = theDEdge->GetEdge();
85 TopoDS_Vertex aFirstVertex, aLastVertex;
86 TopExp::Vertices(anEdge, aFirstVertex, aLastVertex);
88 Handle(Geom_Curve) aCurve;
89 Standard_Real aFirstParam, aLastParam;
90 if (BRepMesh_ShapeTool::Range(anEdge, aCurve, aFirstParam, aLastParam))
92 const Standard_Real aDistF = aFirstVertex.IsNull() ? -1.0 :
93 BRep_Tool::Pnt(aFirstVertex).Distance(aCurve->Value(aFirstParam));
94 const Standard_Real aDistL = aLastVertex.IsNull() ? -1.0 :
95 BRep_Tool::Pnt(aLastVertex).Distance(aCurve->Value(aLastParam));
97 const Standard_Real aVertexAdjustDistance = Max(aDistF, aDistL);
99 aLinDeflection = Max(aVertexAdjustDistance, aLinDeflection);
102 theDEdge->SetDeflection (aLinDeflection);
103 theDEdge->SetAngularDeflection (aAngDeflection);
106 //=======================================================================
107 // Function: ComputeDeflection (wire)
109 //=======================================================================
110 void BRepMesh_Deflection::ComputeDeflection (
111 const IMeshData::IWireHandle& theDWire,
112 const IMeshTools_Parameters& theParameters)
114 Standard_Real aWireDeflection = 0.;
115 if (theDWire->EdgesNb () > 0)
117 for (Standard_Integer aEdgeIt = 0; aEdgeIt < theDWire->EdgesNb(); ++aEdgeIt)
119 aWireDeflection += theDWire->GetEdge(aEdgeIt)->GetDeflection();
122 aWireDeflection /= theDWire->EdgesNb ();
126 aWireDeflection = theParameters.Deflection;
129 theDWire->SetDeflection (aWireDeflection);
132 //=======================================================================
133 // Function: ComputeDeflection (face)
135 //=======================================================================
136 void BRepMesh_Deflection::ComputeDeflection (
137 const IMeshData::IFaceHandle& theDFace,
138 const IMeshTools_Parameters& theParameters)
140 Standard_Real aDeflection = theParameters.DeflectionInterior;
141 if (theParameters.Relative)
143 aDeflection = ComputeAbsoluteDeflection(theDFace->GetFace(),
147 Standard_Real aFaceDeflection = 0.0;
148 if (!theParameters.ForceFaceDeflection)
150 if (theDFace->WiresNb () > 0)
152 for (Standard_Integer aWireIt = 0; aWireIt < theDFace->WiresNb (); ++aWireIt)
154 aFaceDeflection += theDFace->GetWire (aWireIt)->GetDeflection ();
157 aFaceDeflection /= theDFace->WiresNb ();
160 aFaceDeflection = Max (2. * BRepMesh_ShapeTool::MaxFaceTolerance (
161 theDFace->GetFace ()), aFaceDeflection);
163 aFaceDeflection = Max (aDeflection, aFaceDeflection);
165 theDFace->SetDeflection (aFaceDeflection);
168 //=======================================================================
169 // Function: IsConsistent
171 //=======================================================================
172 Standard_Boolean BRepMesh_Deflection::IsConsistent (
173 const Standard_Real theCurrent,
174 const Standard_Real theRequired,
175 const Standard_Boolean theAllowDecrease,
176 const Standard_Real theRatio)
178 // Check if the deflection of existing polygonal representation
179 // fits the required deflection.
180 Standard_Boolean isConsistent = theCurrent < (1. + theRatio) * theRequired
181 && (!theAllowDecrease || theCurrent > (1. - theRatio) * theRequired);