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