1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_CurveBuilder.h"
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_ShapeExplorer.h>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TColgp_HArray1OfPnt.hxx>
32 #include <GeomAPI_Interpolate.hxx>
34 #include <Precision.hxx>
37 static void reorder(Handle(TColgp_HArray1OfPnt)& thePoints);
39 //=================================================================================================
40 GeomEdgePtr GeomAlgoAPI_CurveBuilder::edge(const std::list<GeomPointPtr>& thePoints,
41 const bool theIsClosed,
42 const bool theIsToReorder,
43 const GeomDirPtr& theStartTangent,
44 const GeomDirPtr& theEndTangent)
46 // Prepare points array
47 Handle(TColgp_HArray1OfPnt) aPoints = new TColgp_HArray1OfPnt(1, (int)thePoints.size());
48 std::list<GeomPointPtr>::const_iterator anIt = thePoints.begin();
49 for (int i = 1; anIt != thePoints.end(); anIt++, i++) {
50 GeomPointPtr aPoint = *anIt;
51 aPoints->SetValue(i, aPoint->impl<gp_Pnt>());
54 // If the curve to be closed - remove last point if it is too close to the first one
55 bool isClose = aPoints->First().Distance(aPoints->Last()) <= gp::Resolution();
56 if (isClose && theIsClosed) {
57 aPoints->Resize(aPoints->Lower(), aPoints->Upper() - 1, Standard_True);
60 // Reorder points if required
65 // Initialize interpolator
66 GeomAPI_Interpolate anInterp(aPoints, theIsClosed, gp::Resolution());
68 // Assign tangents if defined
69 if (theStartTangent && theEndTangent) {
70 gp_Dir aDir = theStartTangent->impl<gp_Dir>();
71 gp_Vec anInitialTangent(aDir.XYZ());
72 aDir = theEndTangent->impl<gp_Dir>();
73 gp_Vec aFinalTangent(aDir.XYZ());
75 anInterp.Load(anInitialTangent, aFinalTangent);
79 if (aPoints->Length() > 1) {
83 // Set result in form of edge
85 if (anInterp.IsDone()) {
86 anEdge = BRepBuilderAPI_MakeEdge(anInterp.Curve()).Edge();
89 GeomEdgePtr aResultShape(new GeomAPI_Edge);
90 aResultShape->setImpl(new TopoDS_Shape(anEdge));
95 //================ Auxiliary functions ========================================================
96 void reorder(Handle(TColgp_HArray1OfPnt)& thePoints)
98 if (thePoints->Length() < 3) {
102 int aNbPoints = thePoints->Length();
104 gp_Pnt aPrevPnt = thePoints->Value(1);
105 for (int i = 1; i < aNbPoints; i++) {
106 gp_Pnt aPnt = thePoints->Value(i);
108 double aMinDist = RealLast();
109 for (int j = i + 1; j <= aNbPoints; j++) {
110 double aDist = aPnt.SquareDistance(thePoints->Value(j));
111 if (aDist < aMinDist && (aMinDist - aDist) > Precision::Confusion()) {
116 if (aNearest > 0 && aNearest != i + 1) {
117 // Keep given order of points to use it in case of equidistant candidates
120 // o o o c o->o->o->o->n o o
123 gp_Pnt aNearestPnt = thePoints->Value(aNearest);
124 for (int j = aNearest; j > i + 1; j--) {
125 thePoints->SetValue(j, thePoints->Value(j - 1));
127 thePoints->SetValue(i + 1, aNearestPnt);
129 if (aPrevPnt.Distance(thePoints->Value(i + 1)) <= Precision::Confusion())
132 aPrevPnt = thePoints->Value(i + 1);
136 Handle(TColgp_HArray1OfPnt) aTmpPoints = new TColgp_HArray1OfPnt(1, aNbPoints - aNbDup);
137 for (int j = 1, i = 1; i <= aNbPoints; i++) {
138 if (i == 1 || aPrevPnt.Distance(thePoints->Value(i)) > Precision::Confusion()) {
139 aTmpPoints->SetValue(j++, thePoints->Value(i));
140 aPrevPnt = thePoints->Value(i);
143 thePoints = aTmpPoints;