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