Salome HOME
global misprinting in the word "splitted" is replaced by "split"
[modules/hydro.git] / src / HYDROData / HYDROData_TopoCurve.h
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #ifndef HYDRODATA_TOPOCURVE_H
20 #define HYDRODATA_TOPOCURVE_H
21
22 #include <deque>
23 #include <Handle_Geom_BSplineCurve.hxx>
24 #include <HYDROData.h>
25 #include <list>
26 #include <TopExp.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Vertex.hxx>
29
30 class TopoDS_Wire;
31
32 //! The type represents a 1 monifold connected topo curve
33 //! with forward orientation.
34 class HYDROData_TopoCurve
35 {
36 public:
37   //! The default computer.
38   HYDROData_TopoCurve() {}
39
40   //! Constructs the curve from the edge.
41   HYDROData_TopoCurve(const TopoDS_Edge& theEdge) {myEdges.push_back(theEdge);}
42
43   //! Initializes the curve by the wire.
44   //! Returns 'false' if the wire is not 1 monifold or
45   //! is disconnected or is empty.
46   HYDRODATA_EXPORT bool Initialize(const TopoDS_Wire& theWire);
47
48   //! Converts the curve to the wire.
49   HYDRODATA_EXPORT TopoDS_Wire Wire() const;
50
51   //! Returns 'true' if the curve has no edges.
52   bool IsEmpty() const {return myEdges.empty();}
53
54   //! Returns 'true' whether the curve is closed.
55   bool IsClosed() const
56   {
57     return TopExp::LastVertex(myEdges.back(), Standard_True).
58         IsSame(TopExp::FirstVertex(myEdges.front(), Standard_True)) ==
59       Standard_True;
60   }
61
62   //! Returns the curve edges.
63   const std::list<TopoDS_Edge>& Edges() const {return myEdges;}
64
65   //! Returns the curve edges.
66   std::list<TopoDS_Edge>& Edges() {return myEdges;}
67
68   //! Cuts the curve in the given parameter of the given edge and
69   //! fills the cut part.
70   //! Returns 'true' if:
71   //! -  the curve is open and was split into two parts or
72   //! -  the curve is closed and was cut into an open curve.
73   HYDRODATA_EXPORT bool Cut(
74     const std::list<TopoDS_Edge>::iterator& theEdgePosition,
75     const double theParameter,
76     HYDROData_TopoCurve& theCurve);
77
78   //! Cuts the curve in the given parameter of the given edge and
79   //! fills the cut parts.
80   HYDRODATA_EXPORT void Cut(
81       const std::list<TopoDS_Edge>::const_iterator& theEdgePosition,
82       const double theParameter,
83       HYDROData_TopoCurve& theCurve1,
84       HYDROData_TopoCurve& theCurve2) const;
85
86   //! Cuts the curve at the parameters.
87   //! Each parameter vector list corresponds to the curve edge and
88   //! is ordered in the ascending order.
89   HYDRODATA_EXPORT bool Cut(
90     const std::deque<std::list<double> >& theParameters,
91     std::deque<HYDROData_TopoCurve>& theCurves) const;
92
93   //! Projects the point to the nearest point of the curve.
94   //! Fills the iterator of the edge and
95   //! the parameter on the edge for the projection.
96   //! Returns the square distance from the point to the projection.
97   HYDRODATA_EXPORT double Project(
98     const gp_XYZ& thePoint,
99     std::list<TopoDS_Edge>::const_iterator& theEdgeIterator,
100     double& theParameter) const;
101
102   //! Adds the intersection parameters of the curve with the wire to the vector.
103   HYDRODATA_EXPORT int Intersect(
104     const TopoDS_Wire& theWire,
105     std::deque<std::list<double> >& theParameters) const;
106
107   //! Replaces the first vertex by the last one.
108   HYDRODATA_EXPORT void CloseCurve();
109
110   //! Topologically extends the curve by the parameter at the position:
111   //! 0 - start, 1 - end.
112   HYDRODATA_EXPORT void Merge(
113     const int thePosition, HYDROData_TopoCurve& theCurve);
114
115   //! Topologically merges the curve to the curves.
116   HYDRODATA_EXPORT void Merge(
117     const double theTolerance, std::deque<HYDROData_TopoCurve>& theCurves);
118
119   //! Topologically merges the wire curve to the curves.
120   //! Returns 'false' if the wire is not a curve.
121   static bool Merge(
122     const double theTolerance,
123     const TopoDS_Wire& theWire,
124     std::deque<HYDROData_TopoCurve>& theCurves)
125   {
126     HYDROData_TopoCurve aCurve;
127     const bool aResult = aCurve.Initialize(theWire);
128     if (aResult)
129     {
130       aCurve.Merge(theTolerance, theCurves);
131     }
132     return aResult;
133   }
134
135   //! Topologically connects the curve to the first open one of the curves.
136   //! If the gap between the curve and the first open curve is more than the
137   //! tolerance then the gap is smoothly filled by a curve.
138   //! It is assumed that only one of the parameter curves may be open.
139   //! If the curve is closed then it is added to the curves.
140   HYDRODATA_EXPORT bool Connect(
141     const Standard_Real theTolerance,
142     std::deque<HYDROData_TopoCurve>& theCurves);
143
144   //! Works by the same way as the above method.
145   static bool Connect(
146     const Standard_Real theTolerance,
147     const TopoDS_Wire& theWire,
148     std::deque<HYDROData_TopoCurve>& theCurves)
149   {
150     HYDROData_TopoCurve aCurve;
151     if (aCurve.Initialize(theWire))
152     {
153       return aCurve.Connect(theTolerance, theCurves);
154     }
155
156     return false;
157   }
158
159   //! Creates a B-spline piecewise curve corresponding to the curve
160   //! and using the deflection.
161   //! Returns the piece count.
162   //! Returns 0 in the case of any error.
163   HYDRODATA_EXPORT int BSplinePiecewiseCurve(
164     const double theDeflection, HYDROData_TopoCurve& theCurve) const;
165
166   //! Calculates the values of the curve in its knots.
167   //! Returns 'false' if a curve edge has a nonidentity location or a nonforward
168   //! orientation or has no a B-spline representation.
169   HYDRODATA_EXPORT bool ValuesInKnots(std::list<gp_XYZ>& theValues) const;
170
171 private:
172   //! Transfers the edges of the parameter to this curve end.
173   void append(HYDROData_TopoCurve& theCurve)
174     {myEdges.splice(myEdges.end(), theCurve.myEdges);}
175
176   //! Transfers the edges of the parameter to this curve start.
177   void prepend(HYDROData_TopoCurve& theCurve)
178     {myEdges.splice(myEdges.begin(), theCurve.myEdges);}
179
180   std::list<TopoDS_Edge> myEdges; //!< The ordered curve edges.
181 };
182
183 #endif