QGIS API Documentation 3.99.0-Master (d270888f95f)
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
116 bool isPolyBezier() const SIP_HOLDGIL;
117
118 bool isClosed() const override SIP_HOLDGIL;
119 bool isClosed2D() const override SIP_HOLDGIL;
120
121 // QgsCurve interface
122 QgsLineString *curveToLine( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
123 void draw( QPainter &p ) const override;
124 void drawAsPolygon( QPainter &p ) const override;
125 QgsPoint endPoint() const override SIP_HOLDGIL;
126 bool equals( const QgsCurve &other ) const override;
127 int indexOf( const QgsPoint &point ) const override;
128 QgsPoint *interpolatePoint( double distance ) const override SIP_FACTORY;
129 int numPoints() const override SIP_HOLDGIL;
130 bool pointAt( int node, QgsPoint &point SIP_OUT, Qgis::VertexType &type SIP_OUT ) const override;
131 void points( QgsPointSequence &pts SIP_OUT ) const override;
132 QgsCurve *reversed() const override SIP_FACTORY;
133 void scroll( int firstVertexIndex ) override;
134 std::tuple<std::unique_ptr<QgsCurve>, std::unique_ptr<QgsCurve>> splitCurveAtVertex( int index ) const override SIP_SKIP;
135 QgsPoint startPoint() const override SIP_HOLDGIL;
136 void sumUpArea( double &sum SIP_OUT ) const override;
137 void sumUpArea3D( double &sum SIP_OUT ) const override;
138 double xAt( int index ) const override;
139 double yAt( int index ) const override;
140 double zAt( int index ) const override;
141 double mAt( int index ) const override;
142
143 QPolygonF asQPolygonF() const override;
144
145 void addToPainterPath( QPainterPath &path ) const override;
146 QgsCurve *curveSubstring( double startDistance, double endDistance ) const override SIP_FACTORY;
147 double length() const override SIP_HOLDGIL;
148 double segmentLength( QgsVertexId startVertex ) const override;
149 double distanceBetweenVertices( QgsVertexId fromVertex, QgsVertexId toVertex ) const override;
150 QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, bool removeRedundantPoints = false ) const override SIP_FACTORY;
151 QgsAbstractGeometry *simplifyByDistance( double tolerance ) const override SIP_FACTORY;
152 bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
153 double vertexAngle( QgsVertexId vertex ) const override;
154 void swapXy() override;
155 bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) override;
157 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;
159 void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
160 QgsRectangle boundingBox() const override;
161 QgsBox3D boundingBox3D() const override;
162 bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
163 bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
165 QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
166 QString asWkt( int precision = 17 ) const override;
167 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
168 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
169 json asJsonObject( int precision = 17 ) const override SIP_SKIP;
170 QString asKml( int precision = 17 ) const override;
171 int dimension() const override SIP_HOLDGIL;
172 bool isEmpty() const override SIP_HOLDGIL;
173 void clear() override;
174 bool boundingBoxIntersects( const QgsRectangle &rectangle ) const override SIP_HOLDGIL;
175 bool boundingBoxIntersects( const QgsBox3D &box3d ) const override SIP_HOLDGIL;
176 QgsPoint centroid() const override;
177
178 // QgsAbstractGeometry interface
179 bool addZValue( double zValue = 0 ) override;
180 bool addMValue( double mValue = 0 ) override;
181 bool dropZValue() override;
182 bool dropMValue() override;
183 bool deleteVertex( QgsVertexId position ) override;
184#ifndef SIP_RUN
185 void filterVertices( const std::function<bool( const QgsPoint & )> &filter ) override;
186#endif
187 bool fromWkb( QgsConstWkbPtr &wkb ) override;
188 bool fromWkt( const QString &wkt ) override;
189 bool fuzzyEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
190 bool fuzzyDistanceEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL;
191 QString geometryType() const override SIP_HOLDGIL;
192 bool hasCurvedSegments() const override SIP_HOLDGIL;
193 int partCount() const override SIP_HOLDGIL;
194 QgsCurve *toCurveType() const override;
195 QgsPoint vertexAt( QgsVertexId id ) const override;
196 int vertexCount( int part = 0, int ring = 0 ) const override SIP_HOLDGIL;
197 int vertexNumberFromVertexId( QgsVertexId id ) const override;
198 bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
199
203 int degree() const SIP_HOLDGIL { return mDegree; }
204
209 void setDegree( int degree )
210 {
211 mDegree = degree;
212 clearCache();
213 }
214
218 QVector<QgsPoint> controlPoints() const SIP_HOLDGIL { return mControlPoints; }
219
224 void setControlPoints( const QVector<QgsPoint> &points )
225 {
226 mControlPoints = points;
227 clearCache();
228 }
229
233 QVector<double> knots() const SIP_HOLDGIL { return mKnots; }
234
239 void setKnots( const QVector<double> &knots )
240 {
241 mKnots = knots;
242 clearCache();
243 }
244
248 QVector<double> weights() const SIP_HOLDGIL { return mWeights; }
249
254 void setWeights( const QVector<double> &weights )
255 {
256 mWeights = weights;
257 clearCache();
258 }
259
260#ifndef SIP_RUN
261
267 double weight( int index ) const SIP_HOLDGIL;
268
275 bool setWeight( int index, double weight );
276#else
277
284 double weight( int index ) const SIP_HOLDGIL;
285 % MethodCode
286 const int count = sipCpp->controlPoints().size();
287 if ( a0 < 0 || a0 >= count )
288 {
289 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
290 sipIsErr = 1;
291 }
292 else
293 {
294 sipRes = sipCpp->weight( a0 );
295 }
296 % End
297
306 void setWeight( int index, double weight );
307 % MethodCode
308 const int count = sipCpp->controlPoints().size();
309 if ( a0 < 0 || a0 >= count )
310 {
311 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
312 sipIsErr = 1;
313 }
314 else if ( a1 <= 0 )
315 {
316 PyErr_SetString( PyExc_ValueError, "Weight must be positive (> 0)" );
317 sipIsErr = 1;
318 }
319 else
320 {
321 sipCpp->setWeight( a0, a1 );
322 }
323 % End
324#endif
325
340 static QVector<double> generateUniformKnots( int numControlPoints, int degree );
341
347 inline static const QgsNurbsCurve *cast( const QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
348 {
349 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
350 return static_cast<const QgsNurbsCurve *>( geom );
351 return nullptr;
352 }
353
359 inline static QgsNurbsCurve *cast( QgsAbstractGeometry *geom ) SIP_SKIP // cppcheck-suppress duplInheritedMember
360 {
361 if ( geom && geom->geometryType() == "NurbsCurve"_L1 )
362 return static_cast<QgsNurbsCurve *>( geom );
363 return nullptr;
364 }
365
366 protected:
367 void clearCache() const override;
368 int compareToSameClass( const QgsAbstractGeometry *other ) const final;
369 QgsBox3D calculateBoundingBox3D() const override;
370
371 private:
380 void generateUniformKnots();
381
382 QVector<QgsPoint> mControlPoints;
383 QVector<double> mKnots;
384 QVector<double> mWeights;
385 int mDegree = 0;
386 mutable bool mValidityComputed = false;
387 mutable bool mIsValid = false;
388};
389
390#endif // QGSNURBSCURVE_H
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:2133
VertexType
Types of vertex.
Definition qgis.h:3124
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2729
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2730
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.
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: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:34