Salome HOME
Merge branch 'BR_v14_rc' into BR_quadtree
[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   HYDRODATA_EXPORT void Cut(
75     const std::list<TopoDS_Edge>::iterator& theEdgePosition,
76     const double theParameter,
77     HYDROData_TopoCurve& theCurve);
78
79   //! Cuts the curve in the given parameter of the given edge and
80   //! fills the cut parts.
81   HYDRODATA_EXPORT void Cut(
82       const std::list<TopoDS_Edge>::const_iterator& theEdgePosition,
83       const double theParameter,
84       HYDROData_TopoCurve& theCurve1,
85       HYDROData_TopoCurve& theCurve2) const;
86
87   //! Cuts the curve at the parameters.
88   //! Each parameter vector list corresponds to the curve edge and
89   //! is ordered in the ascending order.
90   HYDRODATA_EXPORT void Cut(
91     const std::deque<std::list<double> >& theParameters,
92     std::deque<HYDROData_TopoCurve>& theCurves) const;
93
94   //! Projects the point to the nearest point of the curve.
95   //! Fills the iterator of the edge and
96   //! the parameter on the edge for the projection.
97   //! Returns the square distance from the point to the projection.
98   HYDRODATA_EXPORT double Project(
99     const gp_XYZ& thePoint,
100     std::list<TopoDS_Edge>::const_iterator& theEdgeIterator,
101     double& theParameter) const;
102
103   //! Adds the intersection parameters of the curve with the wire to the vector.
104   HYDRODATA_EXPORT int Intersect(
105     const TopoDS_Wire& theWire,
106     std::deque<std::list<double> >& theParameters) const;
107
108   //! Replaces the first vertex by the last one.
109   HYDRODATA_EXPORT void CloseCurve();
110
111   //! Topologically extends the curve by the parameter at the position:
112   //! 0 - start, 1 - end.
113   HYDRODATA_EXPORT void Merge(
114     const int thePosition, HYDROData_TopoCurve& theCurve);
115
116   //! Topologically merges the curve to the curves.
117   HYDRODATA_EXPORT void Merge(
118     const double theTolerance, std::deque<HYDROData_TopoCurve>& theCurves);
119
120   //! Topologically merges the wire curve to the curves.
121   //! Returns 'false' if the wire is not a curve.
122   static bool Merge(
123     const double theTolerance,
124     const TopoDS_Wire& theWire,
125     std::deque<HYDROData_TopoCurve>& theCurves)
126   {
127     HYDROData_TopoCurve aCurve;
128     const bool aResult = aCurve.Initialize(theWire);
129     if (aResult)
130     {
131       aCurve.Merge(theTolerance, theCurves);
132     }
133     return aResult;
134   }
135
136   //! Topologically connects the curve to the first open one of the curves.
137   //! If the gap between the curve and the first open curve is more than the
138   //! tolerance then the gap is smoothly filled by a curve.
139   //! It is assumed that only one of the parameter curves may be open.
140   //! If the curve is closed then it is added to the curves.
141   HYDRODATA_EXPORT bool Connect(
142     const Standard_Real theTolerance,
143     std::deque<HYDROData_TopoCurve>& theCurves);
144
145   //! Works by the same way as the above method.
146   static bool Connect(
147     const Standard_Real theTolerance,
148     const TopoDS_Wire& theWire,
149     std::deque<HYDROData_TopoCurve>& theCurves)
150   {
151     HYDROData_TopoCurve aCurve;
152     if (!aCurve.Initialize(theWire))
153     {
154       return aCurve.Connect(theTolerance, theCurves);
155     }
156
157     return false;
158   }
159
160   //! Creates a B-spline piecewise curve corresponding to the curve
161   //! and using the deflection.
162   //! Returns the piece count.
163   //! Returns 0 in the case of any error.
164   HYDRODATA_EXPORT int BSplinePiecewiseCurve(
165     const double theDeflection, HYDROData_TopoCurve& theCurve) const;
166
167   //! Calculates the values of the curve in its knots.
168   //! Returns 'false' if a curve edge has a nonidentity location or a nonforward
169   //! orientation or has no a B-spline representation.
170   HYDRODATA_EXPORT bool ValuesInKnots(std::list<gp_XYZ>& theValues) const;
171
172 private:
173   //! Transfers the edges of the parameter to this curve end.
174   void append(HYDROData_TopoCurve& theCurve)
175     {myEdges.splice(myEdges.end(), theCurve.myEdges);}
176
177   //! Transfers the edges of the parameter to this curve start.
178   void prepend(HYDROData_TopoCurve& theCurve)
179     {myEdges.splice(myEdges.begin(), theCurve.myEdges);}
180
181   std::list<TopoDS_Edge> myEdges; //!< The ordered curve edges.
182 };
183
184 #endif