1 // Created on: 2016-07-07
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_TorusRangeSplitter.hxx>
17 #include <GCPnts_TangentialDeflection.hxx>
19 //=======================================================================
20 // Function: GenerateSurfaceNodes
22 //=======================================================================
23 Handle(IMeshData::ListOfPnt2d) BRepMesh_TorusRangeSplitter::GenerateSurfaceNodes(
24 const IMeshTools_Parameters& theParameters) const
26 const std::pair<Standard_Real, Standard_Real>& aRangeU = GetRangeU();
27 const std::pair<Standard_Real, Standard_Real>& aRangeV = GetRangeV();
29 const Standard_Real aDiffU = aRangeU.second - aRangeU.first;
30 const Standard_Real aDiffV = aRangeV.second - aRangeV.first;
32 const gp_Torus aTorus = GetDFace()->GetSurface()->Torus();
33 const Standard_Real r = aTorus.MinorRadius();
34 const Standard_Real R = aTorus.MajorRadius();
36 const Standard_Real oldDv = GCPnts_TangentialDeflection::ArcAngularStep(
37 r, GetDFace()->GetDeflection(), theParameters.Angle, theParameters.MinSize);
39 Standard_Real Dv = 0.9*oldDv; //TWOTHIRD * oldDv;
42 const Standard_Integer nbV = Max((Standard_Integer) (aDiffV / Dv), 2);
43 Dv = aDiffV / (nbV + 1);
46 const Standard_Real ru = R + r;
49 Du = GCPnts_TangentialDeflection::ArcAngularStep(ru,
50 GetDFace()->GetDeflection(), theParameters.Angle, theParameters.MinSize);
52 const Standard_Real aa = sqrt(Du*Du + oldDv*oldDv);
53 if (aa < gp::Resolution())
55 return Handle(IMeshData::ListOfPnt2d)();
58 Du *= Min(oldDv, Du) / aa;
65 Standard_Integer nbU = Max((Standard_Integer) (aDiffU / Du), 2);
66 nbU = Max(nbU, (Standard_Integer) (nbV * aDiffU * R / (aDiffV * r) / 5.));
67 Du = aDiffU / (nbU + 1);
69 const Handle(NCollection_IncAllocator) aTmpAlloc =
70 new NCollection_IncAllocator(IMeshData::MEMORY_BLOCK_SIZE_HUGE);
72 Handle(IMeshData::SequenceOfReal) aParamU, aParamV;
75 // As the points of edges are returned.
76 // in this case, the points are not representative.
78 //-- Choose DeltaX and DeltaY so that to avoid skipping points on the grid
79 aParamU = new IMeshData::SequenceOfReal(aTmpAlloc);
80 for (Standard_Integer i = 0; i <= nbU; i++)
82 aParamU->Append(aRangeU.first + i * Du);
87 aParamU = fillParams(GetParametersU(), GetRangeU(), nbU, 0.5, aTmpAlloc);
90 aParamV = fillParams(GetParametersV(), GetRangeV(), nbV, 2. / 3., aTmpAlloc);
92 const std::pair<Standard_Real, Standard_Real> aNewRangeU(aRangeU.first + Du * 0.1,
93 aRangeU.second - Du * 0.1);
95 const std::pair<Standard_Real, Standard_Real> aNewRangeV(aRangeV.first + Dv * 0.1,
96 aRangeV.second - Dv * 0.1);
98 Handle(IMeshData::ListOfPnt2d) aNodes = new IMeshData::ListOfPnt2d(aTmpAlloc);
99 for (Standard_Integer i = 1; i <= aParamU->Length(); ++i)
101 const Standard_Real aPasU = aParamU->Value(i);
102 if (aPasU >= aNewRangeU.first && aPasU < aNewRangeU.second)
104 for (Standard_Integer j = 1; j <= aParamV->Length(); ++j)
106 const Standard_Real aPasV = aParamV->Value(j);
107 if (aPasV >= aNewRangeV.first && aPasV < aNewRangeV.second)
109 aNodes->Append(gp_Pnt2d(aPasU, aPasV));
118 //=======================================================================
119 // Function: AddPoint
121 //=======================================================================
122 void BRepMesh_TorusRangeSplitter::AddPoint(const gp_Pnt2d& thePoint)
124 BRepMesh_DefaultRangeSplitter::AddPoint(thePoint);
125 GetParametersU().Add(thePoint.X());
126 GetParametersV().Add(thePoint.Y());
129 //=======================================================================
130 // Function: fillParams
132 //=======================================================================
133 Handle(IMeshData::SequenceOfReal) BRepMesh_TorusRangeSplitter::fillParams(
134 const IMeshData::IMapOfReal& theParams,
135 const std::pair<Standard_Real, Standard_Real>& theRange,
136 const Standard_Integer theStepsNb,
137 const Standard_Real theScale,
138 const Handle(NCollection_IncAllocator)& theAllocator) const
140 Handle(IMeshData::SequenceOfReal) aParams =
141 new IMeshData::SequenceOfReal(theAllocator);
143 const Standard_Integer aLength = theParams.Size();
144 TColStd_Array1OfReal aParamArray(1, aLength);
146 for (Standard_Integer j = 1; j <= aLength; ++j)
148 aParamArray(j) = theParams(j);
151 // Calculate DU, leave array of parameters
152 const Standard_Real aDiff = Abs(theRange.second - theRange.first);
153 Standard_Real aStep = FUN_CalcAverageDUV(aParamArray, aLength);
154 aStep = Max(aStep, aDiff / (Standard_Real) theStepsNb / 2.);
156 Standard_Real aStdStep = aDiff / (Standard_Real) aLength;
157 if (aStep > aStdStep)
161 aStdStep *= theScale;
164 for (Standard_Integer j = 1; j <= aLength; ++j)
166 const Standard_Real pp = aParamArray(j);
168 Standard_Boolean isToInsert = Standard_True;
169 const Standard_Integer aParamsLength = aParams->Length();
170 for (Standard_Integer i = 1; i <= aParamsLength && isToInsert; ++i)
172 isToInsert = (Abs(aParams->Value(i) - pp) > aStdStep);
184 //=======================================================================
185 // Function: FUN_CalcAverageDUV
187 //=======================================================================
188 Standard_Real BRepMesh_TorusRangeSplitter::FUN_CalcAverageDUV(
189 TColStd_Array1OfReal& P, const Standard_Integer PLen) const
191 Standard_Integer i, j, n = 0;
192 Standard_Real p, result = 0.;
194 for (i = 1; i <= PLen; i++)
197 for (j = i + 1; j <= PLen; j++)
209 p = Abs(P(i) - P(i - 1));
217 return (n ? (result / (Standard_Real) n) : -1.);