Salome HOME
c707e2a5e8c0e61075e2be074ab3c41223742b64
[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 #include <gp_XYZ.hxx>
30 #include <Adaptor3d_Curve.hxx>
31
32 class TopoDS_Wire;
33
34 double ProjectPointToCurve(const gp_XYZ& thePoint,
35                            const Adaptor3d_Curve& theCurve,
36                            double& theParameter);
37
38 //! The type represents a 1 manifold connected topo curve
39 //! with forward orientation.
40 class HYDROData_TopoCurve
41 {
42 public:
43   //! The default computer.
44   HYDROData_TopoCurve() {}
45
46   //! Constructs the curve from the edge.
47   HYDROData_TopoCurve(const TopoDS_Edge& theEdge) {myEdges.push_back(theEdge);}
48
49   //! Initializes the curve by the wire.
50   //! Returns 'false' if the wire is not 1 manifold or
51   //! is disconnected or is empty.
52   HYDRODATA_EXPORT bool Initialize(const TopoDS_Wire& theWire);
53
54   //! Converts the curve to the wire.
55   HYDRODATA_EXPORT TopoDS_Wire Wire() const;
56
57   //! Returns 'true' if the curve has no edges.
58   bool IsEmpty() const {return myEdges.empty();}
59
60   //! Returns 'true' whether the curve is closed.
61   bool IsClosed() const
62   {
63     return TopExp::LastVertex(myEdges.back(), Standard_True).
64         IsSame(TopExp::FirstVertex(myEdges.front(), Standard_True)) ==
65       Standard_True;
66   }
67
68   //! Returns the curve edges.
69   const std::list<TopoDS_Edge>& Edges() const {return myEdges;}
70
71   //! Returns the curve edges.
72   std::list<TopoDS_Edge>& Edges() {return myEdges;}
73
74   //! Cuts the curve in the given parameter of the given edge and
75   //! fills the cut part.
76   //! Returns 'true' if:
77   //! -  the curve is open and was split into two parts or
78   //! -  the curve is closed and was cut into an open curve.
79   HYDRODATA_EXPORT bool Cut(
80     const std::list<TopoDS_Edge>::iterator& theEdgePosition,
81     const double theParameter,
82     HYDROData_TopoCurve& theCurve);
83
84   //! Cuts the curve in the given parameter of the given edge and
85   //! fills the cut parts.
86   HYDRODATA_EXPORT void Cut(
87       const std::list<TopoDS_Edge>::const_iterator& theEdgePosition,
88       const double theParameter,
89       HYDROData_TopoCurve& theCurve1,
90       HYDROData_TopoCurve& theCurve2) const;
91
92   //! Cuts the curve at the parameters.
93   //! Each parameter vector list corresponds to the curve edge and
94   //! is ordered in the ascending order.
95   HYDRODATA_EXPORT bool Cut(
96     const std::deque<std::list<double> >& theParameters,
97     std::deque<HYDROData_TopoCurve>& theCurves) const;
98
99   //! Projects the point to the nearest point of the curve.
100   //! Fills the iterator of the edge and
101   //! the parameter on the edge for the projection.
102   //! Returns the square distance from the point to the projection.
103   HYDRODATA_EXPORT double Project(
104     const gp_XYZ& thePoint,
105     std::list<TopoDS_Edge>::const_iterator& theEdgeIterator,
106     double& theParameter) const;
107
108   //! Adds the intersection parameters of the curve with the wire to the vector.
109   HYDRODATA_EXPORT int Intersect(
110     const TopoDS_Wire& theWire,
111     std::deque<std::list<double> >& theParameters) const;
112
113   //! Replaces the first vertex by the last one.
114   HYDRODATA_EXPORT void CloseCurve();
115
116   //! Topologically extends the curve by the parameter at the position:
117   //! 0 - start, 1 - end.
118   HYDRODATA_EXPORT void Merge(
119     const int thePosition, HYDROData_TopoCurve& theCurve);
120
121   //! Topologically merges the curve to the curves.
122   HYDRODATA_EXPORT void Merge(
123     const double theTolerance, std::deque<HYDROData_TopoCurve>& theCurves);
124
125   //! Topologically merges the wire curve to the curves.
126   //! Returns 'false' if the wire is not a curve.
127   static bool Merge(
128     const double theTolerance,
129     const TopoDS_Wire& theWire,
130     std::deque<HYDROData_TopoCurve>& theCurves)
131   {
132     HYDROData_TopoCurve aCurve;
133     const bool aResult = aCurve.Initialize(theWire);
134     if (aResult)
135     {
136       aCurve.Merge(theTolerance, theCurves);
137     }
138     return aResult;
139   }
140
141   //! Topologically connects the curve to the first open one of the curves.
142   //! If the gap between the curve and the first open curve is more than the
143   //! tolerance then the gap is smoothly filled by a curve.
144   //! It is assumed that only one of the parameter curves may be open.
145   //! If the curve is closed then it is added to the curves.
146   HYDRODATA_EXPORT bool Connect(
147     const Standard_Real theTolerance,
148     std::deque<HYDROData_TopoCurve>& theCurves);
149
150   //! Works by the same way as the above method.
151   static bool Connect(
152     const Standard_Real theTolerance,
153     const TopoDS_Wire& theWire,
154     std::deque<HYDROData_TopoCurve>& theCurves)
155   {
156     HYDROData_TopoCurve aCurve;
157     if (aCurve.Initialize(theWire))
158     {
159       return aCurve.Connect(theTolerance, theCurves);
160     }
161
162     return false;
163   }
164
165   //! Creates a B-spline piecewise curve corresponding to the curve
166   //! and using the deflection.
167   //! Returns the piece count.
168   //! Returns 0 in the case of any error.
169   HYDRODATA_EXPORT int BSplinePiecewiseCurve(
170     const double theDeflection, HYDROData_TopoCurve& theCurve) const;
171
172   //! Calculates the values of the curve in its knots.
173   //! Returns 'false' if a curve edge has a nonidentity location or a nonforward
174   //! orientation or has no a B-spline representation.
175   HYDRODATA_EXPORT bool ValuesInKnots(std::list<gp_XYZ>& theValues) const;
176
177 private:
178   //! Transfers the edges of the parameter to this curve end.
179   void append(HYDROData_TopoCurve& theCurve)
180     {myEdges.splice(myEdges.end(), theCurve.myEdges);}
181
182   //! Transfers the edges of the parameter to this curve start.
183   void prepend(HYDROData_TopoCurve& theCurve)
184     {myEdges.splice(myEdges.begin(), theCurve.myEdges);}
185
186   std::list<TopoDS_Edge> myEdges; //!< The ordered curve edges.
187 };
188
189 #endif