QGIS API Documentation 3.99.0-Master (7d2ca374f2d)
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#include <QString>
30
31using namespace Qt::StringLiterals;
32
44class CORE_EXPORT QgsNurbsCurve : public QgsCurve
45{
46 public:
51
59 QgsNurbsCurve( const QVector<QgsPoint> &controlPoints, int degree, const QVector<double> &knots, const QVector<double> &weights );
60
61 QgsNurbsCurve *clone() const override SIP_FACTORY;
62
63#ifndef SIP_RUN
64
71 [[nodiscard]] QgsPoint evaluate( double t ) const;
72#else
73
81 SIP_PYOBJECT evaluate( double t ) const SIP_TYPEHINT( QgsPoint );
82 % MethodCode
83 if ( a0 < 0.0 || a0 > 1.0 )
84 {
85 PyErr_SetString( PyExc_ValueError, "Parameter t must be in range [0, 1]" );
86 sipIsErr = 1;
87 }
88 else
89 {
90 sipRes = sipConvertFromType( new QgsPoint( sipCpp->evaluate( a0 ) ), sipType_QgsPoint, Py_None );
91 }
92 % End
93#endif
94
99 bool isBezier() const SIP_HOLDGIL;
100
104 bool isBSpline() const SIP_HOLDGIL;
105
109 bool isRational() const SIP_HOLDGIL;
110
111
125 bool isPolyBezier() const SIP_HOLDGIL;
126
127 bool isClosed() const override SIP_HOLDGIL;
128 bool isClosed2D() const override SIP_HOLDGIL;
129
130 // QgsCurve interface
131 QgsLineString *curveToLine( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
132 void draw( QPainter &p ) const override;
133 void drawAsPolygon( QPainter &p ) const override;
134 QgsPoint endPoint() const override SIP_HOLDGIL;
135 bool equals( const QgsCurve &other ) const override;
136 int indexOf( const QgsPoint &point ) const override;
137 QgsPoint *interpolatePoint( double distance ) const override SIP_FACTORY;
138 int numPoints() const override SIP_HOLDGIL;
139 bool pointAt( int node, QgsPoint &point SIP_OUT, Qgis::VertexType &type SIP_OUT ) const override;
140 void points( QgsPointSequence &pts SIP_OUT ) const override;
141 QgsCurve *reversed() const override SIP_FACTORY;
142 void scroll( int firstVertexIndex ) override;
143 std::tuple<std::unique_ptr<QgsCurve>, std::unique_ptr<QgsCurve>> splitCurveAtVertex( int index ) const override SIP_SKIP;
144 QgsPoint startPoint() const override SIP_HOLDGIL;
145 void sumUpArea( double &sum SIP_OUT ) const override;
146 void sumUpArea3D( double &sum SIP_OUT ) const override;
147 double xAt( int index ) const override;
148 double yAt( int index ) const override;
149 double zAt( int index ) const override;
150 double mAt( int index ) const override;
151
152 QPolygonF asQPolygonF() const override;
153
154 void addToPainterPath( QPainterPath &path ) const override;
155 QgsCurve *curveSubstring( double startDistance, double endDistance ) const override SIP_FACTORY;
156 double length() const override SIP_HOLDGIL;
157 double segmentLength( QgsVertexId startVertex ) const override;
158 double distanceBetweenVertices( QgsVertexId fromVertex, QgsVertexId toVertex ) const override;
159 QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, bool removeRedundantPoints = false ) const override SIP_FACTORY;
160 QgsAbstractGeometry *simplifyByDistance( double tolerance ) const override SIP_FACTORY;
161 bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
162 double vertexAngle( QgsVertexId vertex ) const override;
163 void swapXy() override;
164 bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) override;
166 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;
168 void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
169 QgsRectangle boundingBox() const override;
170 QgsBox3D boundingBox3D() const override;
171 bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
172 bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
174 QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
175 QString asWkt( int precision = 17 ) const override;
176 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
177 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
178 json asJsonObject( int precision = 17 ) const override SIP_SKIP;
179 QString asKml( int precision = 17 ) const override;
180 int dimension() const override SIP_HOLDGIL;
181 bool isEmpty() const override SIP_HOLDGIL;
182 void clear() override;
183 bool boundingBoxIntersects( const QgsRectangle &rectangle ) const override SIP_HOLDGIL;
184 bool boundingBoxIntersects( const QgsBox3D &box3d ) const override SIP_HOLDGIL;
185 QgsPoint centroid() const override;
186
187 // QgsAbstractGeometry interface
188 bool addZValue( double zValue = 0 ) override;
189 bool addMValue( double mValue = 0 ) override;
190 bool dropZValue() override;
191 bool dropMValue() override;
192 bool deleteVertex( QgsVertexId position ) override;
193#ifndef SIP_RUN
194 void filterVertices( const std::function<bool( const QgsPoint & )> &filter ) override;
195#endif
196 bool fromWkb( QgsConstWkbPtr &wkb ) override;
197 bool fromWkt( const QString &wkt ) override;
198 bool fuzzyEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
199 bool fuzzyDistanceEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
200 QString geometryType() const override SIP_HOLDGIL;
201 bool hasCurvedSegments() const override SIP_HOLDGIL;
202 int partCount() const override SIP_HOLDGIL;
203 QgsCurve *toCurveType() const override;
204 QgsPoint vertexAt( QgsVertexId id ) const override;
205 int vertexCount( int part = 0, int ring = 0 ) const override SIP_HOLDGIL;
206 int vertexNumberFromVertexId( QgsVertexId id ) const override;
207 bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
208
212 int degree() const SIP_HOLDGIL { return mDegree; }
213
218 void setDegree( int degree )
219 {
220 mDegree = degree;
221 clearCache();
222 }
223
227 QVector<QgsPoint> controlPoints() const SIP_HOLDGIL { return mControlPoints; }
228
233 void setControlPoints( const QVector<QgsPoint> &points )
234 {
235 mControlPoints = points;
236 clearCache();
237 }
238
242 QVector<double> knots() const SIP_HOLDGIL { return mKnots; }
243
248 void setKnots( const QVector<double> &knots )
249 {
250 mKnots = knots;
251 clearCache();
252 }
253
257 QVector<double> weights() const SIP_HOLDGIL { return mWeights; }
258
263 void setWeights( const QVector<double> &weights )
264 {
265 mWeights = weights;
266 clearCache();
267 }
268
269#ifndef SIP_RUN
270
276 double weight( int index ) const SIP_HOLDGIL;
277
284 bool setWeight( int index, double weight );
285#else
286
293 double weight( int index ) const SIP_HOLDGIL;
294 % MethodCode
295 const int count = sipCpp->controlPoints().size();
296 if ( a0 < 0 || a0 >= count )
297 {
298 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
299 sipIsErr = 1;
300 }
301 else
302 {
303 sipRes = sipCpp->weight( a0 );
304 }
305 % End
306
315 void setWeight( int index, double weight );
316 % MethodCode
317 const int count = sipCpp->controlPoints().size();
318 if ( a0 < 0 || a0 >= count )
319 {
320 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
321 sipIsErr = 1;
322 }
323 else if ( a1 <= 0 )
324 {
325 PyErr_SetString( PyExc_ValueError, "Weight must be positive (> 0)" );
326 sipIsErr = 1;
327 }
328 else
329 {
330 sipCpp->setWeight( a0, a1 );
331 }
332 % End
333#endif
334
349 static QVector<double> generateUniformKnots( int numControlPoints, int degree );
350
363 static QVector<double> generateKnotsForBezierConversion( int nAnchors, int degree = 3 );
364
373 bool isAnchorVertex( int localIndex ) const SIP_HOLDGIL;
374
380 inline static const QgsNurbsCurve *cast( const QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
381 {
382 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
383 return static_cast<const QgsNurbsCurve *>( geom );
384 return nullptr;
385 }
386
392 inline static QgsNurbsCurve *cast( QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
393 {
394 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
395 return static_cast<QgsNurbsCurve *>( geom );
396 return nullptr;
397 }
398
399 protected:
400 void clearCache() const override;
401 int compareToSameClass( const QgsAbstractGeometry *other ) const final;
402 QgsBox3D calculateBoundingBox3D() const override;
403
404 private:
413 void generateUniformKnots();
414
415 QVector<QgsPoint> mControlPoints;
416 QVector<double> mKnots;
417 QVector<double> mWeights;
418 int mDegree = 0;
419 mutable bool mValidityComputed = false;
420 mutable bool mIsValid = false;
421};
422
423#endif // QGSNURBSCURVE_H
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:2148
VertexType
Types of vertex.
Definition qgis.h:3151
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2744
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2745
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:45
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 structure.
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:53
A rectangle specified with double values.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:239
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_HOLDGIL
Definition qgis_sip.h:178
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34