QGIS API Documentation 3.99.0-Master (a8882ad4560)
Loading...
Searching...
No Matches
qgsnurbsutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnurbsutils.cpp
3 -----------------
4 begin : December 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#include "qgsnurbsutils.h"
19
20#include "qgscompoundcurve.h"
21#include "qgscurvepolygon.h"
23#include "qgsnurbscurve.h"
24#include "qgsvertexid.h"
25
27{
28 if ( !geom )
29 return false;
30
32 return true;
33
35 {
36 for ( int i = 0; i < gc->numGeometries(); ++i )
37 {
38 if ( containsNurbsCurve( gc->geometryN( i ) ) )
39 return true;
40 }
41 }
42
44 {
45 if ( containsNurbsCurve( cp->exteriorRing() ) )
46 return true;
47 for ( int i = 0; i < cp->numInteriorRings(); ++i )
48 {
49 if ( containsNurbsCurve( cp->interiorRing( i ) ) )
50 return true;
51 }
52 }
53
55 {
56 for ( int i = 0; i < cc->nCurves(); ++i )
57 {
58 if ( containsNurbsCurve( cc->curveAt( i ) ) )
59 return true;
60 }
61 }
62
63 return false;
64}
65
67{
68 if ( !geom )
69 return nullptr;
70
71 if ( const QgsNurbsCurve *nurbs = qgsgeometry_cast<const QgsNurbsCurve *>( geom ) )
72 return nurbs;
73
75 {
76 for ( int i = 0; i < gc->numGeometries(); ++i )
77 {
78 const QgsNurbsCurve *nurbs = extractNurbsCurve( gc->geometryN( i ) );
79 if ( nurbs )
80 return nurbs;
81 }
82 }
83
85 {
86 const QgsNurbsCurve *nurbs = extractNurbsCurve( cp->exteriorRing() );
87 if ( nurbs )
88 return nurbs;
89 for ( int i = 0; i < cp->numInteriorRings(); ++i )
90 {
91 nurbs = extractNurbsCurve( cp->interiorRing( i ) );
92 if ( nurbs )
93 return nurbs;
94 }
95 }
96
98 {
99 for ( int i = 0; i < cc->nCurves(); ++i )
100 {
101 const QgsNurbsCurve *nurbs = extractNurbsCurve( cc->curveAt( i ) );
102 if ( nurbs )
103 return nurbs;
104 }
105 }
106
107 return nullptr;
108}
109
111{
112 if ( !geom )
113 return nullptr;
114
115 // Direct NURBS curve
116 if ( const QgsNurbsCurve *nurbs = qgsgeometry_cast<const QgsNurbsCurve *>( geom ) )
117 {
118 localIndex = vid.vertex;
119 return nurbs;
120 }
121
122 // Compound curve - find the curve containing this vertex
123 if ( const QgsCompoundCurve *compound = qgsgeometry_cast<const QgsCompoundCurve *>( geom ) )
124 {
125 int vertexOffset = 0;
126 for ( int i = 0; i < compound->nCurves(); ++i )
127 {
128 const QgsCurve *curve = compound->curveAt( i );
129 const int curveVertexCount = curve->numPoints();
130
131 // Check if vertex is in this curve (accounting for shared endpoints)
132 const int adjustedCount = ( i == compound->nCurves() - 1 ) ? curveVertexCount : curveVertexCount - 1;
133 if ( vid.vertex < vertexOffset + adjustedCount )
134 {
135 if ( const QgsNurbsCurve *nurbs = qgsgeometry_cast<const QgsNurbsCurve *>( curve ) )
136 {
137 localIndex = vid.vertex - vertexOffset;
138 return nurbs;
139 }
140 return nullptr;
141 }
142 vertexOffset += adjustedCount;
143 }
144 }
145
146 // Curve polygon - check exterior and interior rings
147 if ( const QgsCurvePolygon *polygon = qgsgeometry_cast<const QgsCurvePolygon *>( geom ) )
148 {
149 const QgsCurve *ring = nullptr;
150 if ( vid.ring == 0 )
151 ring = polygon->exteriorRing();
152 else if ( vid.ring > 0 && vid.ring <= polygon->numInteriorRings() )
153 ring = polygon->interiorRing( vid.ring - 1 );
154
155 if ( ring )
156 return findNurbsCurveForVertex( ring, QgsVertexId( 0, 0, vid.vertex ), localIndex );
157 }
158
159 // Geometry collection
161 {
162 if ( vid.part >= 0 && vid.part < collection->numGeometries() )
163 {
164 return findNurbsCurveForVertex( collection->geometryN( vid.part ), QgsVertexId( 0, vid.ring, vid.vertex ), localIndex );
165 }
166 }
167
168 return nullptr;
169}
170
172{
173 return const_cast<QgsNurbsCurve *>( findNurbsCurveForVertex( const_cast<const QgsAbstractGeometry *>( geom ), vid, localIndex ) );
174}
Abstract base class for all geometries.
Compound curve geometry type.
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
virtual int numPoints() const =0
Returns the number of points in the curve.
Represents a NURBS (Non-Uniform Rational B-Spline) curve geometry in 2D/3D.
static bool containsNurbsCurve(const QgsAbstractGeometry *geom)
Returns true if the geom contains a NURBS curve (recursively).
static const QgsNurbsCurve * extractNurbsCurve(const QgsAbstractGeometry *geom)
Extracts the first NURBS curve found in the geom (recursively).
static const QgsNurbsCurve * findNurbsCurveForVertex(const QgsAbstractGeometry *geom, const QgsVertexId &vid, int &localIndex)
Finds the NURBS curve containing the vertex identified by vid.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30
int vertex
Vertex number.
Definition qgsvertexid.h:94
int part
Part number.
Definition qgsvertexid.h:88
int ring
Ring number.
Definition qgsvertexid.h:91