Salome HOME
29393fa3f2c942de66168b7ed775dac9e01b08d0
[modules/geom.git] / src / CurveCreator / CurveCreator_Utils.hxx
1 // Copyright (C) 2013-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef CURVECREATOR_UTILS_H
21 #define CURVECREATOR_UTILS_H
22
23 #include "CurveCreator_Macro.hxx"
24 #include "CurveCreator_ICurve.hxx"
25
26 #include <AIS_InteractiveContext.hxx>
27 #include <AIS_InteractiveObject.hxx> // TODO: remove
28 #include <V3d_View.hxx>
29 #include <gp_Pnt.hxx>
30 #include <Geom_Curve.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Wire.hxx>
33 #include <TColgp_HArray1OfPnt.hxx>
34 #include <Geom_BSplineCurve.hxx>
35
36 #include <list>
37 #include <vector> // TODO: remove
38
39 class CurveCreator_Curve;
40
41
42 class CurveCreator_Utils
43 {
44 public:
45
46     /*!
47    * \brief Returns the point clicked in 3D view.
48    *
49    * \param x The X coordinate in the view.
50    * \param y The Y coordinate in the view.
51    * \param theView View where the given point takes place.
52    * \retval gp_Pnt Returns the point clicked in 3D view
53    */
54   CURVECREATOR_EXPORT static void ConvertPointToClick( const gp_Pnt& thePoint,
55                                                        Handle(V3d_View) theView,
56                                                        int& x, int& y );
57
58   /*!
59    * \brief Returns the point clicked in 3D view.
60    *
61    * \param x The X coordinate in the view.
62    * \param y The Y coordinate in the view.
63    * \param theView View where the given point takes place.
64    * \retval gp_Pnt Returns the point clicked in 3D view
65    */
66   CURVECREATOR_EXPORT static gp_Pnt ConvertClickToPoint( int x, int y,
67                                                          Handle(V3d_View) theView );
68
69   /**
70    * Generates shape on the curve
71    * \param theCurve a curve object, that contains data
72    * \param theShape a generated shape
73    */
74   CURVECREATOR_EXPORT static void constructShape( const CurveCreator_ICurve* theCurve,
75                                                   TopoDS_Shape& theShape );
76
77   /**
78    * Generates a curve from a shape.
79    * \param theShape a shape to be converted to curve.
80    * \param theCurve a curve object to be initialized.
81    * \param theLocalCS the local coordinate system of the curve.
82    * \return true in case of success; false otherwise. Warning: the curve can
83    *         be modified even if the shape is not valid for curve construction.
84    */
85   CURVECREATOR_EXPORT static bool constructCurve
86                       (const TopoDS_Shape        theShape,
87                              CurveCreator_Curve *theCurve,
88                              gp_Ax3             &theLocalCS);
89
90   /**
91    * Find selected points in the context
92    * \param theContext the viewer context
93    * \param theCurve a curve object, that contains data
94    */
95   CURVECREATOR_EXPORT static void getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
96                                          const CurveCreator_ICurve* theCurve,
97                                          CurveCreator_ICurve::SectionToPointList& thePoints );
98
99   /**
100    * Set selected points to the context
101    * \param theContext the viewer context
102    * \param theCurve a curve object, that contains data
103    * \param thePoints the curve point indices to be selected in the context
104    */
105   CURVECREATOR_EXPORT static void setSelectedPoints(
106                                          Handle(AIS_InteractiveContext) theContext,
107                                          const CurveCreator_ICurve* theCurve,
108                                          const CurveCreator_ICurve::SectionToPointList& thePoints =
109                                                CurveCreator_ICurve::SectionToPointList() );
110
111   /*!
112    * \brief Sets the local point context for the 3D viewer.
113    * \param theCurve a curve object, that contains data
114    * \param theContext the viewer context
115    * \param theOpen The flag to open or close the local context.
116    */
117   CURVECREATOR_EXPORT static void setLocalPointContext(
118                                         const CurveCreator_ICurve* theCurve,
119                                         Handle(AIS_InteractiveContext) theContext,
120                                         const bool theOpen );
121
122   /**
123    * Checks whether the point belongs to the OCC object
124    * \param theObject a line or shape with a bspline inside
125    * \param theX the X coordinate in the view.
126    * \param theY the Y coordinate in the view.
127    * \param thePoint the output point to be append to the model curve
128    * \param thePoint1 the output point to bound the line where a new point should be inserted
129    * \param thePoint2 the output point to bound the line where a new point should be inserted
130    */
131   CURVECREATOR_EXPORT static bool pointOnObject( Handle(V3d_View) theView,
132                                                  Handle(AIS_InteractiveObject) theObject,
133                                                  const int theX, const int theY,
134                                                  gp_Pnt& thePoint, gp_Pnt& thePoint1,
135                                                  gp_Pnt& thePoint2 );
136
137   /**
138    * The algorithm builds the cubic B-spline passing through the points that the
139    * tangent vector in each given point P is calculated by the following way:
140    * if point P is preceded by a point A and is followed by a point B then
141    * the tangent vector is equal to (P - A) / |P - A| + (B - P) / |B - P|;
142    * if point P is preceded by a point A but is not followed by any point then
143    * the tangent vector is equal to P - A;
144    * if point P is followed by a point B but is not preceded by any point then
145    * the tangent vector is equal to B - P.
146    */
147   CURVECREATOR_EXPORT static bool constructBSpline( const Handle(TColgp_HArray1OfPnt)& thePoints,
148                                                     const Standard_Boolean theIsClosed,
149                                                     Handle(Geom_BSplineCurve)& theBSpline );
150
151   /**
152    * Constructs the wire corresponding to the section.
153    */
154   CURVECREATOR_EXPORT static TopoDS_Wire ConstructWire(
155     Handle(TColgp_HArray1OfPnt) thePoints,
156     const bool theIsPolyline,
157     const bool theIsClosed);
158
159 protected:
160   /*
161    * Returns whether the clicked point belong to the curve or has a very near projection
162    * \param theX the X coordinate of a point clicked in the OCC viewer
163    * \param theY the Y coordinate of a point clicked in the OCC viewer
164    * \param theCurve a geometry curve
165    * \param theOutPoint a found projected point on the curve
166   */
167   static bool hasProjectPointOnCurve(
168                              Handle(V3d_View) theView,
169                              const int theX, const int theY,
170                              const Handle(Geom_Curve)& theCurve,
171                              Standard_Real& theParameter,
172                              int& theDelta );
173
174   /*
175    * Returns whether the X and Y coordinates is in the pixel tolerance
176    * \param theX the X coordinate of the first point
177    * \param theY the Y coordinate of the first point
178    * \param theOtherX the X coordinate of the second point
179    * \param theOtherY the Y coordinate of the second point
180    * \param theTolerance the tolerance to compare
181    * \param theDelta the sum of the a square of X and a square of Y
182    * \returns whether the points are provide to the pixel tolerance
183   */
184   static bool isEqualPixels( const int theX, const int theY,
185                              const int theOtherX, const int theOtherY,
186                              const double theTolerance, int& theDelta );
187
188
189   /*
190    * Returns whether the points are the same
191    * \param thePoint the first point
192    * \param theOtherPoint the second point
193    * \returns whether the points are provide to the pixel tolerance
194   */
195   static bool isEqualPoints( const gp_Pnt& thePoint,
196                              const gp_Pnt& theOtherPoint );
197
198   /**
199    * Returns the array of points of a shape to construct a curve section. The
200    * shape can be either a wire or a vertex. For vertex a single point in the
201    * array is returned.
202    *
203    * \param theShape the shape. Can be either a wire or a vertex.
204    * \param IsClosed closed flag. Output parameter.
205    * \param IsBSpline BSpline flag. Output parameter.
206    * \return the array of points. Null handle in case of failure.
207    */
208   static Handle_TColgp_HArray1OfPnt getPoints
209                            (const TopoDS_Shape &theShape,
210                                   bool         &IsClosed,
211                                   bool         &IsBSpline);
212
213   /**
214    * This method computes a plane using the input points. The plane is defined
215    * by gp_Pln object and the status. The status can have one of the following
216    * values:
217    *   - 0 plane is not set.<BR>
218    *   - 1 origin of the plane is fixed. The plane is defined by 1 or several
219    *       coincident points.<BR>
220    *   - 2 origin + OX axis of the plane is fixed. The plane is defined by 2
221    *       or more points that lie on a particular line.<BR>
222    *   - 3 plane is fixed. Plane is defined by 3 not coincident points.<BR>
223    *
224    * \param thePoints the points.
225    * \param thePlane the current plane on input. It can be modified on output.
226    * \param thePlnStatus the current status on input. It can be modified on
227    *        output.
228    */
229   static void FindPlane(const Handle_TColgp_HArray1OfPnt &thePoints,
230                               gp_Pln                     &thePlane,
231                               Standard_Integer           &thePlnStatus);
232
233 };
234
235 #endif // CURVECREATOR_UTILS_H