1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_CurveBuilder.h"
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_ShapeExplorer.h>
28 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TColgp_HArray1OfPnt.hxx>
33 #include <GeomAPI_Interpolate.hxx>
35 #include <Precision.hxx>
38 static void reorder(Handle(TColgp_HArray1OfPnt) thePoints);
40 //=================================================================================================
41 GeomEdgePtr GeomAlgoAPI_CurveBuilder::edge(const std::list<GeomPointPtr>& thePoints,
42 const bool theIsClosed,
43 const bool theIsToReorder,
44 const GeomDirPtr& theStartTangent,
45 const GeomDirPtr& theEndTangent)
47 // Prepare points array
48 Handle(TColgp_HArray1OfPnt) aPoints = new TColgp_HArray1OfPnt(1, (int)thePoints.size());
49 std::list<GeomPointPtr>::const_iterator anIt = thePoints.begin();
50 for (int i = 1; anIt != thePoints.end(); anIt++, i++) {
51 GeomPointPtr aPoint = *anIt;
52 aPoints->SetValue(i, aPoint->impl<gp_Pnt>());
55 // Reorder points if required
60 // If the curve to be closed - remove last point if it is too close to the first one
61 bool isClose = aPoints->First().Distance(aPoints->Last()) <= gp::Resolution();
62 if (isClose && theIsClosed) {
63 aPoints->Resize(aPoints->Lower(), aPoints->Upper() - 1, Standard_True);
66 // Initialize interpolator
67 GeomAPI_Interpolate anInterp(aPoints, theIsClosed, gp::Resolution());
69 // Assign tangents if defined
70 if (theStartTangent && theEndTangent) {
71 gp_Dir aDir = theStartTangent->impl<gp_Dir>();
72 gp_Vec anInitialTangent(aDir.XYZ());
73 aDir = theEndTangent->impl<gp_Dir>();
74 gp_Vec aFinalTangent(aDir.XYZ());
76 anInterp.Load(anInitialTangent, aFinalTangent);
80 if (aPoints->Length() > 1) {
84 // Set result in form of edge
86 if (anInterp.IsDone()) {
87 anEdge = BRepBuilderAPI_MakeEdge(anInterp.Curve()).Edge();
90 GeomEdgePtr aResultShape(new GeomAPI_Edge);
91 aResultShape->setImpl(new TopoDS_Shape(anEdge));
96 //================ Auxiliary functions ========================================================
97 void reorder(Handle(TColgp_HArray1OfPnt) thePoints)
99 if (thePoints->Length() < 3) {
103 int aNbPoints = thePoints->Length();
105 gp_Pnt aPrevPnt = thePoints->Value(1);
106 for (int i = 1; i < aNbPoints - 1; i++) {
107 gp_Pnt aPnt = thePoints->Value(i);
109 double aMinDist = RealLast();
110 for (int j = i + 1; j <= aNbPoints; j++) {
111 double aDist = aPnt.SquareDistance(thePoints->Value(j));
112 if (aDist < aMinDist && (aMinDist - aDist) > Precision::Confusion()) {
117 if (aNearest > 0 && aNearest != i + 1) {
118 // Keep given order of points to use it in case of equidistant candidates
121 // o o o c o->o->o->o->n o o
124 gp_Pnt aNearestPnt = thePoints->Value(aNearest);
125 for (int j = aNearest; j > i + 1; j--) {
126 thePoints->SetValue(j, thePoints->Value(j - 1));
128 thePoints->SetValue(i + 1, aNearestPnt);
130 if (aPrevPnt.Distance(thePoints->Value(i + 1)) <= Precision::Confusion())
133 aPrevPnt = thePoints->Value(i + 1);
137 Handle(TColgp_HArray1OfPnt) aTmpPoints = new TColgp_HArray1OfPnt(1, aNbPoints - aNbDup);
138 for (int j = 1, i = 1; i <= aNbPoints; i++) {
139 if (i == 1 || aPrevPnt.Distance(thePoints->Value(i)) > Precision::Confusion()) {
140 aTmpPoints->SetValue(j++, thePoints->Value(i));
141 aPrevPnt = thePoints->Value(i);
144 thePoints = aTmpPoints;