QGIS API Documentation 3.99.0-Master (a8882ad4560)
Loading...
Searching...
No Matches
qgsnurbscurve.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsnurbscurve.h
3 -----------------
4 begin : September 2025
5 copyright : (C) 2025 by Loïc Bartoletti
6 email : loic dot bartoletti at oslandia dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSNURBSCURVE_H
19#define QGSNURBSCURVE_H
20
21#include <float.h>
22#include <memory>
23
24#include "qgis_core.h"
25#include "qgis_sip.h"
26#include "qgscurve.h"
27
28#include <QPolygonF>
29
41class CORE_EXPORT QgsNurbsCurve : public QgsCurve
42{
43 public:
48
56 QgsNurbsCurve( const QVector<QgsPoint> &controlPoints, int degree, const QVector<double> &knots, const QVector<double> &weights );
57
58 QgsNurbsCurve *clone() const override SIP_FACTORY;
59
60#ifndef SIP_RUN
61
68 [[nodiscard]] QgsPoint evaluate( double t ) const;
69#else
70
78 SIP_PYOBJECT evaluate( double t ) const SIP_TYPEHINT( QgsPoint );
79 % MethodCode
80 if ( a0 < 0.0 || a0 > 1.0 )
81 {
82 PyErr_SetString( PyExc_ValueError, "Parameter t must be in range [0, 1]" );
83 sipIsErr = 1;
84 }
85 else
86 {
87 sipRes = sipConvertFromType( new QgsPoint( sipCpp->evaluate( a0 ) ), sipType_QgsPoint, Py_None );
88 }
89 % End
90#endif
91
96 bool isBezier() const SIP_HOLDGIL;
97
101 bool isBSpline() const SIP_HOLDGIL;
102
106 bool isRational() const SIP_HOLDGIL;
107
113 bool isPolyBezier() const SIP_HOLDGIL;
114
115 bool isClosed() const override SIP_HOLDGIL;
116 bool isClosed2D() const override SIP_HOLDGIL;
117
118 // QgsCurve interface
119 QgsLineString *curveToLine( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
120 void draw( QPainter &p ) const override;
121 void drawAsPolygon( QPainter &p ) const override;
122 QgsPoint endPoint() const override SIP_HOLDGIL;
123 bool equals( const QgsCurve &other ) const override;
124 int indexOf( const QgsPoint &point ) const override;
125 QgsPoint *interpolatePoint( double distance ) const override SIP_FACTORY;
126 int numPoints() const override SIP_HOLDGIL;
127 bool pointAt( int node, QgsPoint &point SIP_OUT, Qgis::VertexType &type SIP_OUT ) const override;
128 void points( QgsPointSequence &pts SIP_OUT ) const override;
129 QgsCurve *reversed() const override SIP_FACTORY;
130 void scroll( int firstVertexIndex ) override;
131 std::tuple<std::unique_ptr<QgsCurve>, std::unique_ptr<QgsCurve>> splitCurveAtVertex( int index ) const override SIP_SKIP;
132 QgsPoint startPoint() const override SIP_HOLDGIL;
133 void sumUpArea( double &sum SIP_OUT ) const override;
134 void sumUpArea3D( double &sum SIP_OUT ) const override;
135 double xAt( int index ) const override;
136 double yAt( int index ) const override;
137 double zAt( int index ) const override;
138 double mAt( int index ) const override;
139
140 QPolygonF asQPolygonF() const override;
141
142 void addToPainterPath( QPainterPath &path ) const override;
143 QgsCurve *curveSubstring( double startDistance, double endDistance ) const override SIP_FACTORY;
144 double length() const override SIP_HOLDGIL;
145 double segmentLength( QgsVertexId startVertex ) const override;
146 double distanceBetweenVertices( QgsVertexId fromVertex, QgsVertexId toVertex ) const override;
147 QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, bool removeRedundantPoints = false ) const override SIP_FACTORY;
148 QgsAbstractGeometry *simplifyByDistance( double tolerance ) const override SIP_FACTORY;
149 bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
150 double vertexAngle( QgsVertexId vertex ) const override;
151 void swapXy() override;
152 bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) override;
154 double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT, QgsVertexId &vertexAfter SIP_OUT, int *leftOf SIP_OUT = nullptr, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const override;
156 void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
157 QgsRectangle boundingBox() const override;
158 QgsBox3D boundingBox3D() const override;
159 bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
160 bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
162 QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
163 QString asWkt( int precision = 17 ) const override;
164 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
165 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
166 json asJsonObject( int precision = 17 ) const override SIP_SKIP;
167 QString asKml( int precision = 17 ) const override;
168 int dimension() const override SIP_HOLDGIL;
169 bool isEmpty() const override SIP_HOLDGIL;
170 void clear() override;
171 bool boundingBoxIntersects( const QgsRectangle &rectangle ) const override SIP_HOLDGIL;
172 bool boundingBoxIntersects( const QgsBox3D &box3d ) const override SIP_HOLDGIL;
173 QgsPoint centroid() const override;
174
175 // QgsAbstractGeometry interface
176 bool addZValue( double zValue = 0 ) override;
177 bool addMValue( double mValue = 0 ) override;
178 bool dropZValue() override;
179 bool dropMValue() override;
180 bool deleteVertex( QgsVertexId position ) override;
181#ifndef SIP_RUN
182 void filterVertices( const std::function<bool( const QgsPoint & )> &filter ) override;
183#endif
184 bool fromWkb( QgsConstWkbPtr &wkb ) override;
185 bool fromWkt( const QString &wkt ) override;
186 bool fuzzyEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
187 bool fuzzyDistanceEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
188 QString geometryType() const override SIP_HOLDGIL;
189 bool hasCurvedSegments() const override SIP_HOLDGIL;
190 int partCount() const override SIP_HOLDGIL;
191 QgsCurve *toCurveType() const override;
192 QgsPoint vertexAt( QgsVertexId id ) const override;
193 int vertexCount( int part = 0, int ring = 0 ) const override SIP_HOLDGIL;
194 int vertexNumberFromVertexId( QgsVertexId id ) const override;
195 bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
196
200 int degree() const SIP_HOLDGIL { return mDegree; }
201
206 void setDegree( int degree )
207 {
208 mDegree = degree;
209 clearCache();
210 }
211
215 QVector<QgsPoint> controlPoints() const SIP_HOLDGIL { return mControlPoints; }
216
221 void setControlPoints( const QVector<QgsPoint> &points )
222 {
223 mControlPoints = points;
224 clearCache();
225 }
226
230 QVector<double> knots() const SIP_HOLDGIL { return mKnots; }
231
236 void setKnots( const QVector<double> &knots )
237 {
238 mKnots = knots;
239 clearCache();
240 }
241
245 QVector<double> weights() const SIP_HOLDGIL { return mWeights; }
246
251 void setWeights( const QVector<double> &weights )
252 {
253 mWeights = weights;
254 clearCache();
255 }
256
257#ifndef SIP_RUN
258
264 double weight( int index ) const SIP_HOLDGIL;
265
272 bool setWeight( int index, double weight );
273#else
274
281 double weight( int index ) const SIP_HOLDGIL;
282 % MethodCode
283 const int count = sipCpp->controlPoints().size();
284 if ( a0 < 0 || a0 >= count )
285 {
286 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
287 sipIsErr = 1;
288 }
289 else
290 {
291 sipRes = sipCpp->weight( a0 );
292 }
293 % End
294
303 void setWeight( int index, double weight );
304 % MethodCode
305 const int count = sipCpp->controlPoints().size();
306 if ( a0 < 0 || a0 >= count )
307 {
308 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
309 sipIsErr = 1;
310 }
311 else if ( a1 <= 0 )
312 {
313 PyErr_SetString( PyExc_ValueError, "Weight must be positive (> 0)" );
314 sipIsErr = 1;
315 }
316 else
317 {
318 sipCpp->setWeight( a0, a1 );
319 }
320 % End
321#endif
322
328 inline static const QgsNurbsCurve *cast( const QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
329 {
330 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
331 return static_cast<const QgsNurbsCurve *>( geom );
332 return nullptr;
333 }
334
340 inline static QgsNurbsCurve *cast( QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
341 {
342 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
343 return static_cast<QgsNurbsCurve *>( geom );
344 return nullptr;
345 }
346
347 protected:
348 void clearCache() const override;
349 int compareToSameClass( const QgsAbstractGeometry *other ) const final;
350 QgsBox3D calculateBoundingBox3D() const override;
351
352 private:
361 void generateUniformKnots();
362
363 QVector<QgsPoint> mControlPoints;
364 QVector<double> mKnots;
365 QVector<double> mWeights;
366 int mDegree = 0;
367 mutable bool mValidityComputed = false;
368 mutable bool mIsValid = false;
369};
370
371#endif // QGSNURBSCURVE_H
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:2121
VertexType
Types of vertex.
Definition qgis.h:3112
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2717
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2718
An abstract base class for classes which transform geometries by transforming input points to output ...
Abstract base class for all geometries.
virtual QgsAbstractGeometry * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0, bool removeRedundantPoints=false) const =0
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
@ MaximumAngle
Maximum angle between generating radii (lines from arc center to output vertices).
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
virtual QgsBox3D calculateBoundingBox3D() const
Calculates the minimal 3D bounding box for the geometry.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
QFlags< WkbFlag > WkbFlags
virtual QgsAbstractGeometry * simplifyByDistance(double tolerance) const =0
Simplifies the geometry by applying the Douglas Peucker simplification by distance algorithm.
virtual QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
AxisOrder
Axis order for GML generation.
@ XY
X comes before Y (or lon before lat).
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
virtual QByteArray asWkb(WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const =0
Returns a WKB representation of the geometry.
virtual QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML2 representation of the geometry.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
virtual QString asWkt(int precision=17) const =0
Returns a WKT representation of the geometry.
virtual bool fuzzyEqual(const QgsAbstractGeometry &other, double epsilon=1e-8) const =0
Performs fuzzy comparison between this geometry and other using an epsilon.
virtual bool isEmpty() const
Returns true if the geometry is empty.
virtual bool fuzzyDistanceEqual(const QgsAbstractGeometry &other, double epsilon=1e-8) const =0
Performs fuzzy distance comparison between this geometry and other using an epsilon.
virtual json asJsonObject(int precision=17) const
Returns a json object representation of the geometry.
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
virtual void transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection d=Qgis::TransformDirection::Forward, bool transformZ=false)=0
Transforms the geometry using a coordinate transform.
virtual bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
virtual bool hasCurvedSegments() const
Returns true if the geometry contains curved segments.
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
virtual void filterVertices(const std::function< bool(const QgsPoint &) > &filter)
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual QgsPoint centroid() const
Returns the centroid of the geometry.
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
virtual bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)=0
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
virtual int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const =0
Returns the length of the QByteArray returned by asWkb().
QgsAbstractGeometry()=default
virtual int compareToSameClass(const QgsAbstractGeometry *other) const =0
Compares to an other geometry of the same class, and returns a integer for sorting of the two geometr...
virtual double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const =0
Searches for the closest segment of the geometry to a given point.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:42
A const WKB pointer.
Definition qgswkbptr.h:139
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
virtual bool equals(const QgsCurve &other) const =0
Checks whether this curve exactly equals another curve.
virtual int numPoints() const =0
Returns the number of points in the curve.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition qgscurve.cpp:294
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
Definition qgscurve.cpp:141
virtual void scroll(int firstVertexIndex)=0
Scrolls the curve vertices so that they start with the vertex at the given index.
int partCount() const override
Returns count of parts contained in the geometry.
Definition qgscurve.cpp:194
virtual bool pointAt(int node, QgsPoint &point, Qgis::VertexType &type) const =0
Returns the point and vertex id of a point within the curve.
QgsBox3D boundingBox3D() const override
Returns the 3D bounding box for the geometry.
Definition qgscurve.cpp:239
virtual QgsCurve * curveSubstring(double startDistance, double endDistance) const =0
Returns a new curve representing a substring of this curve.
virtual bool isClosed() const
Returns true if the curve is closed.
Definition qgscurve.cpp:54
bool isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override
Checks validity of the geometry, and returns true if the geometry is valid.
Definition qgscurve.cpp:248
virtual bool isClosed2D() const
Returns true if the curve is closed.
Definition qgscurve.cpp:42
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition qgscurve.cpp:181
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition qgscurve.cpp:199
virtual QPolygonF asQPolygonF() const
Returns a QPolygonF representing the points.
Definition qgscurve.cpp:267
virtual void addToPainterPath(QPainterPath &path) const =0
Adds a curve to a painter path.
virtual int indexOf(const QgsPoint &point) const =0
Returns the index of the first vertex matching the given point, or -1 if a matching vertex is not fou...
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition qgscurve.cpp:207
virtual void sumUpArea(double &sum) const =0
Sums up the area of the curve by iterating over the vertices (shoelace formula).
QString asKml(int precision=17) const override
Returns a KML representation of the geometry.
Definition qgscurve.cpp:165
virtual QgsPoint * interpolatePoint(double distance) const =0
Returns an interpolated point on the curve at the specified distance.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
virtual double distanceBetweenVertices(QgsVertexId fromVertex, QgsVertexId toVertex) const =0
Returns the distance along the curve between two vertices.
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
virtual std::tuple< std::unique_ptr< QgsCurve >, std::unique_ptr< QgsCurve > > splitCurveAtVertex(int index) const =0
Splits the curve at the specified vertex index, returning two curves which represent the portion of t...
virtual double zAt(int index) const =0
Returns the z-coordinate of the specified node in the line string.
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
virtual void drawAsPolygon(QPainter &p) const =0
Draws the curve as a polygon on the specified QPainter.
virtual double mAt(int index) const =0
Returns the m-coordinate of the specified node in the line string.
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
virtual QgsLineString * curveToLine(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve.
virtual void sumUpArea3D(double &sum) const =0
Sums up the 3d area of the curve by iterating over the vertices (shoelace formula).
QgsCurve()=default
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Line string geometry type, with support for z-dimension and m-values.
bool isRational() const
Returns true if this curve is rational (has non-uniform weights).
QgsNurbsCurve()
Constructor for an empty NURBS curve geometry.
QVector< double > knots() const
Returns the knot vector of the NURBS curve.
static QgsNurbsCurve * cast(QgsAbstractGeometry *geom)
Cast the geom to a QgsNurbsCurve.
static const QgsNurbsCurve * cast(const QgsAbstractGeometry *geom)
Cast the geom to a QgsNurbsCurve.
bool isPolyBezier() const
Returns true if this curve represents a poly-Bézier curve.
bool isBSpline() const
Returns true if this curve represents a B-spline (non-rational NURBS).
void setWeights(const QVector< double > &weights)
Sets the weight vector of the NURBS curve.
void points(QgsPointSequence &pts) const override
Returns a list of points within the curve.
int degree() const
Returns the degree of the NURBS curve.
QVector< QgsPoint > controlPoints() const
Returns the control points of the NURBS curve.
void setKnots(const QVector< double > &knots)
Sets the knot vector of the NURBS curve.
bool isBezier() const
Returns true if this curve represents a Bézier curve.
void setDegree(int degree)
Sets the degree of the NURBS curve.
QgsPoint evaluate(double t) const
Evaluates the NURBS curve at parameter t ∈ [0,1].
void setControlPoints(const QVector< QgsPoint > &points)
Sets the control points of the NURBS curve.
QVector< double > weights() const
Returns the weight vector of the NURBS curve.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:240
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30