Salome HOME
Issue #2560: Add Interpolation feature to Build plugin for creation a curve by the...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_CurveBuilder.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_CurveBuilder.h"
22
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_ShapeExplorer.h>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TColgp_HArray1OfPnt.hxx>
33 #include <GeomAPI_Interpolate.hxx>
34 #include <gp_Pnt.hxx>
35 #include <Precision.hxx>
36
37
38 static void reorder(Handle(TColgp_HArray1OfPnt) thePoints);
39
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)
46 {
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>());
53   }
54
55   // Reorder points if required
56   if (theIsToReorder) {
57     reorder(aPoints);
58   }
59
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);
64   }
65
66   // Initialize interpolator
67   GeomAPI_Interpolate anInterp(aPoints, theIsClosed, gp::Resolution());
68
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());
75
76     anInterp.Load(anInitialTangent, aFinalTangent);
77   }
78
79   // Compute
80   if (aPoints->Length() > 1) {
81     anInterp.Perform();
82   }
83
84   // Set result in form of edge
85   TopoDS_Edge anEdge;
86   if (anInterp.IsDone()) {
87     anEdge = BRepBuilderAPI_MakeEdge(anInterp.Curve()).Edge();
88   }
89
90   GeomEdgePtr aResultShape(new GeomAPI_Edge);
91   aResultShape->setImpl(new TopoDS_Shape(anEdge));
92
93   return aResultShape;
94 }
95
96 //================   Auxiliary functions   ========================================================
97 void reorder(Handle(TColgp_HArray1OfPnt) thePoints)
98 {
99   if (thePoints->Length() < 3) {
100     return;
101   }
102
103   int aNbPoints = thePoints->Length();
104   int aNbDup = 0;
105   gp_Pnt aPrevPnt = thePoints->Value(1);
106   for (int i = 1; i < aNbPoints - 1; i++) {
107     gp_Pnt aPnt = thePoints->Value(i);
108     int aNearest = 0;
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()) {
113         aNearest = j;
114         aMinDist = aDist;
115       }
116     }
117     if (aNearest > 0 && aNearest != i + 1) {
118       // Keep given order of points to use it in case of equidistant candidates
119       //               .-<---<-.
120       //              /         \
121                       // o  o  o  c  o->o->o->o->n  o  o
122       //          |  |           |
123       //          i i+1       nearest
124       gp_Pnt aNearestPnt = thePoints->Value(aNearest);
125       for (int j = aNearest; j > i + 1; j--) {
126         thePoints->SetValue(j, thePoints->Value(j - 1));
127       }
128       thePoints->SetValue(i + 1, aNearestPnt);
129     }
130     if (aPrevPnt.Distance(thePoints->Value(i + 1)) <= Precision::Confusion())
131       aNbDup++;
132     else
133       aPrevPnt = thePoints->Value(i + 1);
134   }
135
136   if (aNbDup > 0) {
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);
142       }
143     }
144     thePoints = aTmpPoints;
145   }
146 }