QGIS API Documentation 4.1.0-Master (01362494303)
Loading...
Searching...
No Matches
qgssfcgalengine.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssfcgalengine.h
3 -------------------------------------------------------------------
4 begin : May 2025
5 copyright : (C) 2025 by Oslandia
6 email : benoit dot de dot mezzo at oslandia dot com
7 email : jean dot felder at oslandia dot com
8 email : loic dot bartoletti at oslandia dot com
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#ifdef WITH_SFCGAL
19#ifndef QGSSFCGALENGINE_H
20#define QGSSFCGALENGINE_H
21
22
23#include <SFCGAL/capi/sfcgal_c.h>
24#include <source_location>
25
26#include "qgis_core.h"
27#include "qgsexception.h"
28#include "qgsgeometry.h"
29#include "qgslogger.h"
30#include "qgsmatrix4x4.h"
31#include "qgspoint.h"
32#include "qgsvector3d.h"
33
34#define SIP_NO_FILE
35
36class QgsGeometry;
37class QgsSfcgalGeometry;
38
41#ifndef SFCGAL_MAKE_VERSION
42#define SFCGAL_MAKE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
43#endif
46#ifndef SFCGAL_VERSION_NUM
47#define SFCGAL_VERSION_NUM SFCGAL_MAKE_VERSION( SFCGAL_VERSION_MAJOR_INT, SFCGAL_VERSION_MINOR_INT, SFCGAL_VERSION_PATCH_INT )
48#endif
49
51#define CHECK_NOT_NULL( ptr, defaultObj ) \
52 if ( !( ptr ) ) \
53 { \
54 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ) ); \
55 return ( defaultObj ); \
56 }
57
59#define CHECK_SUCCESS( errorMsg, defaultObj ) \
60 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
61 { \
62 return ( defaultObj ); \
63 }
64
66#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
67 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
68 { \
69 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
70 return ( defaultObj ); \
71 }
72
74#define THROW_ON_ERROR( errorMsg ) \
75 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
76 { \
77 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
78 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
79 }
80
81
87namespace sfcgal
88{
89 // ==== SFCGAL geometry
91 using geometry = sfcgal_geometry_t;
92
94 struct GeometryDeleter
95 {
97 void CORE_EXPORT operator()( geometry *geom ) const;
98 };
99
101 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
103 using shared_geom = std::shared_ptr< geometry >; // NO DELETER ==> added with function make_shared_geom!!!!!
104
106 shared_geom make_shared_geom( geometry *geom );
107} // namespace sfcgal
108
109namespace sfcgal
110{
111 // ==== SFCGAL primitive
112#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
114 using primitive = sfcgal_primitive_t;
116 using primitiveType = sfcgal_primitive_type_t;
117#else
119 using primitive = int;
121 using primitiveType = int;
122#endif
123
125 struct PrimitiveDeleter
126 {
128 void CORE_EXPORT operator()( primitive *prim ) const;
129 };
130
132 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
134 using shared_prim = std::shared_ptr< primitive >; // NO DELETER ==> added with function make_shared_prim!!!!!
135
137 shared_prim make_shared_prim( primitive *prim );
138
140 struct PrimitiveParameterDesc
141 {
142 std::string name;
143 std::string type;
144 std::variant<int, double, QgsPoint, QgsVector3D> value;
145 };
146
148 void to_json( json &j, const PrimitiveParameterDesc &p );
149
151 void from_json( const json &j, PrimitiveParameterDesc &p );
152} // namespace sfcgal
153
154namespace sfcgal
155{
156 // ==== SFCGAL errors
158 int errorCallback( const char *, ... );
159
161 int warningCallback( const char *, ... );
162
170 class CORE_EXPORT ErrorHandler
171 {
172 public:
174 ErrorHandler();
175
184 bool hasSucceedOrStack( QString *errorMsg = nullptr, const std::source_location &location = std::source_location::current() );
185
189 void clearText( QString *errorMsg = nullptr );
190
192 bool isTextEmpty() const;
193
195 QString getMainText() const;
196
198 QString getFullText() const;
199
201 void addText( const QString &msg, const std::source_location &location = std::source_location::current() );
202
203 private:
204 QStringList errorMessages;
205 };
206
208 CORE_EXPORT ErrorHandler *errorHandler();
209
211 using func_geomgeom_to_geom = sfcgal_geometry_t *( * ) ( const sfcgal_geometry_t *, const sfcgal_geometry_t * );
213 using func_geom_to_geom = sfcgal_geometry_t *( * ) ( const sfcgal_geometry_t * );
214} // namespace sfcgal
215
222class CORE_EXPORT QgsSfcgalEngine
223{
224 public:
231 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
232
239 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg = nullptr );
240
247 static sfcgal::shared_geom fromAbstractGeometry( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr );
248
255 static sfcgal::shared_geom cloneGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
256
265 static QString geometryType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
266
273 static sfcgal::shared_geom fromWkb( const QgsConstWkbPtr &wkbPtr, QString *errorMsg = nullptr );
274
281 static sfcgal::shared_geom fromWkt( const QString &wkt, QString *errorMsg = nullptr );
282
289 static QByteArray toWkb( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
290
298 static QString toWkt( const sfcgal::geometry *geom, int numDecimals = -1, QString *errorMsg = nullptr );
299
306 static Qgis::WkbType wkbType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
307
316 static int dimension( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
317
328 static int partCount( const sfcgal::geometry *geom, QString *errorMsg );
329
340 static bool addZValue( sfcgal::geometry *geom, double zValue = 0, QString *errorMsg = nullptr );
341
352 static bool addMValue( sfcgal::geometry *geom, double mValue = 0, QString *errorMsg = nullptr );
353
363 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
364
374 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
375
384 static void swapXy( sfcgal::geometry *geom, QString *errorMsg = nullptr );
385
396 static bool isEqual( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double tolerance = -1.0, QString *errorMsg = nullptr );
397
404 static bool isEmpty( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
405
415 static bool isValid( const sfcgal::geometry *geom, QString *errorMsg = nullptr, QgsGeometry *errorLoc = nullptr );
416
432 static bool isSimple( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
433
442 static sfcgal::shared_geom boundary( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
443
452 static QgsPoint centroid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
453
463 static sfcgal::shared_geom translate( const sfcgal::geometry *geom, const QgsVector3D &translation, QString *errorMsg = nullptr );
464
473 static sfcgal::shared_geom scale( const sfcgal::geometry *geom, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
474
483 static sfcgal::shared_geom rotate2D( const sfcgal::geometry *geom, double angle, const QgsPoint &center, QString *errorMsg = nullptr );
484
494 static sfcgal::shared_geom rotate3D( const sfcgal::geometry *geom, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
495
503 static double distance( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
504
513 static bool distanceWithin( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double maxdistance, QString *errorMsg );
514
521 static double area( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
522
531 static double length( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
532
541 static bool intersects( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
542
550 static sfcgal::shared_geom intersection( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
551
559 static sfcgal::shared_geom difference( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
560
567 static sfcgal::shared_geom combine( const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg = nullptr );
568
575 static sfcgal::shared_geom triangulate( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
576
586 static bool covers( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
587
594 static sfcgal::shared_geom convexHull( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
595
604 static sfcgal::shared_geom envelope( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
605
616 static sfcgal::shared_geom offsetCurve( const sfcgal::geometry *geom, double distance, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
617
629 static sfcgal::shared_geom buffer3D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle3D joinStyle3D, QString *errorMsg = nullptr );
630
641 static sfcgal::shared_geom buffer2D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
642
650 static sfcgal::shared_geom extrude( const sfcgal::geometry *geom, const QgsVector3D &extrusion, QString *errorMsg = nullptr );
651
662 static sfcgal::shared_geom simplify( const sfcgal::geometry *geom, double tolerance, bool preserveTopology, QString *errorMsg = nullptr );
663
675 static sfcgal::shared_geom approximateMedialAxis( const sfcgal::geometry *geom, bool extendToEdges = false, QString *errorMsg = nullptr );
676
688 static sfcgal::shared_geom toSolid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
689
701 static sfcgal::shared_geom toPolyhedralSurface( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
702
703#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
704
712 static sfcgal::shared_geom transform( const sfcgal::geometry *geom, const QgsMatrix4x4 &mat, QString *errorMsg = nullptr );
713
720 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg = nullptr );
721
727 static sfcgal::shared_prim createCube( double size, QString *errorMsg = nullptr );
728
735 static sfcgal::shared_prim primitiveClone( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
736
744 static sfcgal::shared_geom primitiveAsPolyhedral( const sfcgal::primitive *prim, const QgsMatrix4x4 &mat = QgsMatrix4x4(), QString *errorMsg = nullptr );
745
755 static bool primitiveIsEqual( const sfcgal::primitive *primA, const sfcgal::primitive *primB, double tolerance = -1.0, QString *errorMsg = nullptr );
756
766 static double primitiveArea( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
767
777 static double primitiveVolume( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
778
787 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
788
796 static QVariant primitiveParameter( const sfcgal::primitive *prim, const QString &name, QString *errorMsg = nullptr );
797
806 static void primitiveSetParameter( sfcgal::primitive *prim, const QString &name, const QVariant &value, QString *errorMsg = nullptr );
807
808#endif
809};
810
812
813
814class SFCGALException : public std::runtime_error
815{
816 public:
817 explicit SFCGALException( const QString &message )
818 : std::runtime_error( message.toUtf8().constData() )
819 {}
820};
821
823
824#endif // QGSSFCGALENGINE_H
825#endif
JoinStyle3D
Join styles for 3D buffers.
Definition qgis.h:2255
JoinStyle
Join styles for buffers.
Definition qgis.h:2242
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
A geometry is the spatial representation of a feature.