Salome HOME
Merge remote-tracking branch 'remotes/origin/occ/eliminateWarnings'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_CurveBuilder.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_CurveBuilder.h"
21
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_ShapeExplorer.h>
26
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TColgp_HArray1OfPnt.hxx>
32 #include <GeomAPI_Interpolate.hxx>
33 #include <gp_Pnt.hxx>
34 #include <Precision.hxx>
35
36
37 static void reorder(Handle(TColgp_HArray1OfPnt)& thePoints);
38
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)
45 {
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>());
52   }
53
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);
58   }
59
60   // Reorder points if required
61   if (theIsToReorder) {
62     reorder(aPoints);
63   }
64
65   // Initialize interpolator
66   GeomAPI_Interpolate anInterp(aPoints, theIsClosed, gp::Resolution());
67
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());
74
75     anInterp.Load(anInitialTangent, aFinalTangent);
76   }
77
78   // Compute
79   if (aPoints->Length() > 1) {
80     anInterp.Perform();
81   }
82
83   // Set result in form of edge
84   TopoDS_Edge anEdge;
85   if (anInterp.IsDone()) {
86     anEdge = BRepBuilderAPI_MakeEdge(anInterp.Curve()).Edge();
87   }
88
89   GeomEdgePtr aResultShape(new GeomAPI_Edge);
90   aResultShape->setImpl(new TopoDS_Shape(anEdge));
91
92   return aResultShape;
93 }
94
95 //================   Auxiliary functions   ========================================================
96 void reorder(Handle(TColgp_HArray1OfPnt)& thePoints)
97 {
98   if (thePoints->Length() < 3) {
99     return;
100   }
101
102   int aNbPoints = thePoints->Length();
103   int aNbDup = 0;
104   gp_Pnt aPrevPnt = thePoints->Value(1);
105   for (int i = 1; i < aNbPoints; i++) {
106     gp_Pnt aPnt = thePoints->Value(i);
107     int aNearest = 0;
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()) {
112         aNearest = j;
113         aMinDist = aDist;
114       }
115     }
116     if (aNearest > 0 && aNearest != i + 1) {
117       // Keep given order of points to use it in case of equidistant candidates
118       //              .--<---<--.
119       //             |           |
120       // o  o  o  c  o->o->o->o->n  o  o
121       //          |  |           |
122       //          i i+1       nearest
123       gp_Pnt aNearestPnt = thePoints->Value(aNearest);
124       for (int j = aNearest; j > i + 1; j--) {
125         thePoints->SetValue(j, thePoints->Value(j - 1));
126       }
127       thePoints->SetValue(i + 1, aNearestPnt);
128     }
129     if (aPrevPnt.Distance(thePoints->Value(i + 1)) <= Precision::Confusion())
130       aNbDup++;
131     else
132       aPrevPnt = thePoints->Value(i + 1);
133   }
134
135   if (aNbDup > 0) {
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);
141       }
142     }
143     thePoints = aTmpPoints;
144   }
145 }