1 // Copyright (C) 2013-2020 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef CURVECREATOR_UTILS_H
21 #define CURVECREATOR_UTILS_H
23 #include "CurveCreator_Macro.hxx"
24 #include "CurveCreator_ICurve.hxx"
26 #include <AIS_InteractiveContext.hxx>
27 #include <AIS_InteractiveObject.hxx> // TODO: remove
28 #include <V3d_View.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>
38 #include <vector> // TODO: remove
39 #include <NCollection_IndexedDataMap.hxx>
41 class CurveCreator_Curve;
44 class CurveCreator_Utils
49 * \brief Returns the point clicked in 3D view.
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
56 CURVECREATOR_EXPORT static void ConvertPointToClick( const gp_Pnt& thePoint,
57 Handle(V3d_View) theView,
61 * Generates a random color
63 CURVECREATOR_EXPORT static Quantity_Color getRandColor();
66 * Convert QColor to Quantity_Color
68 CURVECREATOR_EXPORT static Quantity_Color colorConv(QColor color);
71 * Convert Quantity_Color to QColor
73 CURVECREATOR_EXPORT static QColor colorConv(Quantity_Color color);
76 * \brief Returns the point clicked in 3D view.
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
83 CURVECREATOR_EXPORT static gp_Pnt ConvertClickToPoint( int x, int y,
84 Handle(V3d_View) theView );
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)
92 CURVECREATOR_EXPORT static void constructShape( const CurveCreator_ICurve* theCurve,
93 TopoDS_Shape& theShape,
94 NCollection_IndexedDataMap<int, TopoDS_Shape>* Sect2Shape = NULL);
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.
104 CURVECREATOR_EXPORT static bool constructCurve
105 (const TopoDS_Shape theShape,
106 CurveCreator_Curve *theCurve,
110 * Find selected points in the context
111 * \param theContext the viewer context
112 * \param theCurve a curve object, that contains data
114 CURVECREATOR_EXPORT static void getSelectedPoints( Handle(AIS_InteractiveContext) theContext,
115 const CurveCreator_ICurve* theCurve,
116 CurveCreator_ICurve::SectionToPointList& thePoints );
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
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() );
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.
136 CURVECREATOR_EXPORT static void setLocalPointContext(
137 const CurveCreator_ICurve* theCurve,
138 Handle(AIS_InteractiveContext) theContext,
139 const bool theOpen );
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
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,
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.
166 CURVECREATOR_EXPORT static bool constructBSpline( const Handle(TColgp_HArray1OfPnt)& thePoints,
167 const Standard_Boolean theIsClosed,
168 Handle(Geom_BSplineCurve)& theBSpline );
171 * Constructs the wire corresponding to the section.
173 CURVECREATOR_EXPORT static TopoDS_Wire ConstructWire(
174 Handle(TColgp_HArray1OfPnt) thePoints,
175 const bool theIsPolyline,
176 const bool theIsClosed);
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
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,
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
203 static bool isEqualPixels( const int theX, const int theY,
204 const int theOtherX, const int theOtherY,
205 const double theTolerance, int& theDelta );
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
214 static bool isEqualPoints( const gp_Pnt& thePoint,
215 const gp_Pnt& theOtherPoint );
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
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.
227 static Handle(TColgp_HArray1OfPnt) getPoints
228 (const TopoDS_Shape &theShape,
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
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>
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
248 static void FindPlane(const Handle(TColgp_HArray1OfPnt) &thePoints,
250 Standard_Integer &thePlnStatus);
254 #endif // CURVECREATOR_UTILS_H