Salome HOME
547907856fb18e4a90c3778a9829c5d6af816e47
[modules/geom.git] / src / CurveCreator / CurveCreator_Utils.hxx
1 // Copyright (C) 2013-2023  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 #include <QColor>
36
37 #include <list>
38 #include <vector> // TODO: remove
39 #include <NCollection_IndexedDataMap.hxx>
40
41 class CurveCreator_Curve;
42
43
44 class CurveCreator_Utils
45 {
46 public:
47
48     /*!
49    * \brief Returns the point clicked in 3D view.
50    *
51    * \param x The X coordinate in the view.
52    * \param y The Y coordinate in the view.
53    * \param theView View where the given point takes place.
54    * \retval gp_Pnt Returns the point clicked in 3D view
55    */
56   CURVECREATOR_EXPORT static void ConvertPointToClick( const gp_Pnt& thePoint,
57                                                        Handle(V3d_View) theView,
58                                                        int& x, int& y );
59
60   /**
61   * Generates a random color
62   */
63   CURVECREATOR_EXPORT static Quantity_Color getRandColor();
64
65   /**
66   * Convert QColor to Quantity_Color
67   */
68   CURVECREATOR_EXPORT static Quantity_Color colorConv(QColor color);
69
70   /**
71   * Convert Quantity_Color to QColor
72   */
73   CURVECREATOR_EXPORT static QColor colorConv(Quantity_Color color);
74
75   /*!
76    * \brief Returns the point clicked in 3D view.
77    *
78    * \param x The X coordinate in the view.
79    * \param y The Y coordinate in the view.
80    * \param theView View where the given point takes place.
81    * \retval gp_Pnt Returns the point clicked in 3D view
82    */
83   CURVECREATOR_EXPORT static gp_Pnt ConvertClickToPoint( int x, int y,
84                                                          Handle(V3d_View) theView );
85
86   /**
87    * Generates shape on the curve
88    * \param theCurve a curve object, that contains data
89    * \param theShape a generated shape
90    * \param Sect2Shape optional out map: section to constructed shape (wire+vertices)
91    */
92   CURVECREATOR_EXPORT static void constructShape( const CurveCreator_ICurve* theCurve,
93                                                   TopoDS_Shape& theShape,
94                                                   NCollection_IndexedDataMap<int, TopoDS_Shape>* Sect2Shape = NULL);
95
96   /**
97    * Generates a curve from a shape.
98    * \param theShape a shape to be converted to curve.
99    * \param theCurve a curve object to be initialized.
100    * \param theLocalCS the local coordinate system of the curve.
101    * \return true in case of success; false otherwise. Warning: the curve can
102    *         be modified even if the shape is not valid for curve construction.
103    */
104   CURVECREATOR_EXPORT static bool constructCurve
105                       (const TopoDS_Shape        theShape,
106                              CurveCreator_Curve *theCurve,
107                              gp_Ax3             &theLocalCS);
108
109   /**
110    * Find selected points in the context
111    * \param theContext the viewer context
112    * \param theCurve a curve object, that contains data
113    */
114   CURVECREATOR_EXPORT static void getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
115                                          const CurveCreator_ICurve* theCurve,
116                                          CurveCreator_ICurve::SectionToPointList& thePoints );
117
118   /**
119    * Set selected points to the context
120    * \param theContext the viewer context
121    * \param theCurve a curve object, that contains data
122    * \param thePoints the curve point indices to be selected in the context
123    */
124   CURVECREATOR_EXPORT static void setSelectedPoints(
125                                          Handle(AIS_InteractiveContext) theContext,
126                                          const CurveCreator_ICurve* theCurve,
127                                          const CurveCreator_ICurve::SectionToPointList& thePoints =
128                                                CurveCreator_ICurve::SectionToPointList() );
129
130   /*!
131    * \brief Sets the local point context for the 3D viewer.
132    * \param theCurve a curve object, that contains data
133    * \param theContext the viewer context
134    * \param theOpen The flag to open or close the local context.
135    */
136   CURVECREATOR_EXPORT static void setLocalPointContext(
137                                         const CurveCreator_ICurve* theCurve,
138                                         Handle(AIS_InteractiveContext) theContext,
139                                         const bool theOpen );
140
141   /**
142    * Checks whether the point belongs to the OCC object
143    * \param theObject a line or shape with a bspline inside
144    * \param theX the X coordinate in the view.
145    * \param theY the Y coordinate in the view.
146    * \param thePoint the output point to be append to the model curve
147    * \param thePoint1 the output point to bound the line where a new point should be inserted
148    * \param thePoint2 the output point to bound the line where a new point should be inserted
149    */
150   CURVECREATOR_EXPORT static bool pointOnObject( Handle(V3d_View) theView,
151                                                  Handle(AIS_InteractiveObject) theObject,
152                                                  const int theX, const int theY,
153                                                  gp_Pnt& thePoint, gp_Pnt& thePoint1,
154                                                  gp_Pnt& thePoint2 );
155
156   /**
157    * The algorithm builds the cubic B-spline passing through the points that the
158    * tangent vector in each given point P is calculated by the following way:
159    * if point P is preceded by a point A and is followed by a point B then
160    * the tangent vector is equal to (P - A) / |P - A| + (B - P) / |B - P|;
161    * if point P is preceded by a point A but is not followed by any point then
162    * the tangent vector is equal to P - A;
163    * if point P is followed by a point B but is not preceded by any point then
164    * the tangent vector is equal to B - P.
165    */
166   CURVECREATOR_EXPORT static bool constructBSpline( const Handle(TColgp_HArray1OfPnt)& thePoints,
167                                                     const Standard_Boolean theIsClosed,
168                                                     Handle(Geom_BSplineCurve)& theBSpline );
169
170   /**
171    * Constructs the wire corresponding to the section.
172    */
173   CURVECREATOR_EXPORT static TopoDS_Wire ConstructWire(
174     Handle(TColgp_HArray1OfPnt) thePoints,
175     const bool theIsPolyline,
176     const bool theIsClosed);
177
178 protected:
179   /*
180    * Returns whether the clicked point belong to the curve or has a very near projection
181    * \param theX the X coordinate of a point clicked in the OCC viewer
182    * \param theY the Y coordinate of a point clicked in the OCC viewer
183    * \param theCurve a geometry curve
184    * \param theOutPoint a found projected point on the curve
185   */
186   static bool hasProjectPointOnCurve(
187                              Handle(V3d_View) theView,
188                              const int theX, const int theY,
189                              const Handle(Geom_Curve)& theCurve,
190                              Standard_Real& theParameter,
191                              int& theDelta );
192
193   /*
194    * Returns whether the X and Y coordinates is in the pixel tolerance
195    * \param theX the X coordinate of the first point
196    * \param theY the Y coordinate of the first point
197    * \param theOtherX the X coordinate of the second point
198    * \param theOtherY the Y coordinate of the second point
199    * \param theTolerance the tolerance to compare
200    * \param theDelta the sum of the a square of X and a square of Y
201    * \returns whether the points are provide to the pixel tolerance
202   */
203   static bool isEqualPixels( const int theX, const int theY,
204                              const int theOtherX, const int theOtherY,
205                              const double theTolerance, int& theDelta );
206
207
208   /*
209    * Returns whether the points are the same
210    * \param thePoint the first point
211    * \param theOtherPoint the second point
212    * \returns whether the points are provide to the pixel tolerance
213   */
214   static bool isEqualPoints( const gp_Pnt& thePoint,
215                              const gp_Pnt& theOtherPoint );
216
217   /**
218    * Returns the array of points of a shape to construct a curve section. The
219    * shape can be either a wire or a vertex. For vertex a single point in the
220    * array is returned.
221    *
222    * \param theShape the shape. Can be either a wire or a vertex.
223    * \param IsClosed closed flag. Output parameter.
224    * \param IsBSpline BSpline flag. Output parameter.
225    * \return the array of points. Null handle in case of failure.
226    */
227   static Handle(TColgp_HArray1OfPnt) getPoints
228                            (const TopoDS_Shape &theShape,
229                                   bool         &IsClosed,
230                                   bool         &IsBSpline);
231
232   /**
233    * This method computes a plane using the input points. The plane is defined
234    * by gp_Pln object and the status. The status can have one of the following
235    * values:
236    *   - 0 plane is not set.<BR>
237    *   - 1 origin of the plane is fixed. The plane is defined by 1 or several
238    *       coincident points.<BR>
239    *   - 2 origin + OX axis of the plane is fixed. The plane is defined by 2
240    *       or more points that lie on a particular line.<BR>
241    *   - 3 plane is fixed. Plane is defined by 3 not coincident points.<BR>
242    *
243    * \param thePoints the points.
244    * \param thePlane the current plane on input. It can be modified on output.
245    * \param thePlnStatus the current status on input. It can be modified on
246    *        output.
247    */
248   static void FindPlane(const Handle(TColgp_HArray1OfPnt) &thePoints,
249                               gp_Pln                     &thePlane,
250                               Standard_Integer           &thePlnStatus);
251
252 };
253
254 #endif // CURVECREATOR_UTILS_H