QGIS API Documentation 3.99.0-Master (26c88405ac0)
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#define SIP_NO_FILE
23
24#include <SFCGAL/capi/sfcgal_c.h>
25#include <source_location>
26
27#include "qgis_core.h"
28#include "qgsexception.h"
29#include "qgsgeometry.h"
30#include "qgslogger.h"
31#include "qgspoint.h"
32#include "qgsvector3d.h"
33
34#include <QtGui/qmatrix4x4.h>
35
36class QgsGeometry;
37class QgsSfcgalGeometry;
38
40#define SFCGAL_MAKE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
42#define SFCGAL_VERSION SFCGAL_MAKE_VERSION( SFCGAL_VERSION_MAJOR_INT, SFCGAL_VERSION_MINOR_INT, SFCGAL_VERSION_PATCH_INT )
43
45#define CHECK_NOT_NULL( ptr, defaultObj ) \
46 if ( !( ptr ) ) \
47 { \
48 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ), __FILE__, __FUNCTION__, __LINE__ ); \
49 return ( defaultObj ); \
50 }
51
53#define CHECK_SUCCESS( errorMsg, defaultObj ) \
54 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ), __FILE__, __FUNCTION__, __LINE__ ) ) \
55 { \
56 return ( defaultObj ); \
57 }
58
60#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
61 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ), __FILE__, __FUNCTION__, __LINE__ ) ) \
62 { \
63 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
64 return ( defaultObj ); \
65 }
66
68#define THROW_ON_ERROR(errorMsg) \
69 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ), __FILE__, __FUNCTION__, __LINE__ ) ) \
70 { \
71 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
72 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
73 }
74
75
76
82namespace sfcgal
83{
84 // ==== SFCGAL geometry
86 using geometry = sfcgal_geometry_t;
87
89 struct GeometryDeleter
90 {
92 void CORE_EXPORT operator()( geometry *geom ) const;
93 };
94
96 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
98 using shared_geom = std::shared_ptr< geometry >; // NO DELETER ==> added with function make_shared_geom!!!!!
99
101 shared_geom make_shared_geom( geometry *geom );
102} // namespace sfcgal
103
104namespace sfcgal
105{
106 // ==== SFCGAL primitive
107#if SFCGAL_VERSION >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
109 using primitive = sfcgal_primitive_t;
111 using primitiveType = sfcgal_primitive_type_t;
112#else
114 using primitive = int;
116 using primitiveType = int;
117#endif
118
120 struct PrimitiveDeleter
121 {
123 void CORE_EXPORT operator()( primitive *prim ) const;
124 };
125
127 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
129 using shared_prim = std::shared_ptr< primitive >; // NO DELETER ==> added with function make_shared_prim!!!!!
130
132 shared_prim make_shared_prim( primitive *prim );
133
135 struct PrimitiveParameterDesc
136 {
137 std::string name;
138 std::string type;
139 std::variant<int, double, QgsPoint, QgsVector3D> value;
140 };
141
143 void to_json( json &j, const PrimitiveParameterDesc &p );
144
146 void from_json( const json &j, PrimitiveParameterDesc &p );
147} // namespace sfcgal
148
149namespace sfcgal
150{
151 // ==== SFCGAL errors
153 int errorCallback( const char *, ... );
154
156 int warningCallback( const char *, ... );
157
165 class CORE_EXPORT ErrorHandler
166 {
167 public:
169 ErrorHandler();
170
179 bool hasSucceedOrStack( QString *errorMsg = nullptr, const char *fromFile = nullptr, const char *fromFunc = nullptr, int fromLine = 0 );
180
184 void clearText( QString *errorMsg = nullptr );
185
187 bool isTextEmpty() const;
188
190 QString getMainText() const;
191
193 QString getFullText() const;
194
196 void addText( const QString &msg, const char *fromFile = nullptr, const char *fromFunc = nullptr, int fromLine = 0 );
197
198 private:
199 QStringList errorMessages;
200 };
201
203 CORE_EXPORT ErrorHandler *errorHandler();
204
206 using func_geomgeom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t *, const sfcgal_geometry_t * );
208 using func_geom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t * );
209} // namespace sfcgal
210
217class CORE_EXPORT QgsSfcgalEngine
218{
219 public:
220
227 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
228
235 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg = nullptr );
236
243 static sfcgal::shared_geom fromAbstractGeometry( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr );
244
251 static sfcgal::shared_geom cloneGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
252
261 static QString geometryType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
262
269 static sfcgal::shared_geom fromWkb( const QgsConstWkbPtr &wkbPtr, QString *errorMsg = nullptr );
270
277 static sfcgal::shared_geom fromWkt( const QString &wkt, QString *errorMsg = nullptr );
278
285 static QByteArray toWkb( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
286
294 static QString toWkt( const sfcgal::geometry *geom, int numDecimals = -1, QString *errorMsg = nullptr );
295
302 static Qgis::WkbType wkbType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
303
312 static int dimension( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
313
324 static int partCount( const sfcgal::geometry *geom, QString *errorMsg );
325
336 static bool addZValue( sfcgal::geometry *geom, double zValue = 0, QString *errorMsg = nullptr );
337
348 static bool addMValue( sfcgal::geometry *geom, double mValue = 0, QString *errorMsg = nullptr );
349
359 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
360
370 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
371
380 static void swapXy( sfcgal::geometry *geom, QString *errorMsg = nullptr );
381
392 static bool isEqual( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double tolerance = -1.0, QString *errorMsg = nullptr );
393
400 static bool isEmpty( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
401
411 static bool isValid( const sfcgal::geometry *geom, QString *errorMsg = nullptr, QgsGeometry *errorLoc = nullptr );
412
428 static bool isSimple( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
429
438 static sfcgal::shared_geom boundary( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
439
448 static QgsPoint centroid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
449
459 static sfcgal::shared_geom translate( const sfcgal::geometry *geom, const QgsVector3D &translation, QString *errorMsg = nullptr );
460
469 static sfcgal::shared_geom scale( const sfcgal::geometry *geom, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
470
479 static sfcgal::shared_geom rotate2D( const sfcgal::geometry *geom, double angle, const QgsPoint &center, QString *errorMsg = nullptr );
480
490 static sfcgal::shared_geom rotate3D( const sfcgal::geometry *geom, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
491
499 static double distance( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
500
509 static bool distanceWithin( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double maxdistance, QString *errorMsg );
510
517 static double area( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
518
527 static double length( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
528
537 static bool intersects( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
538
546 static sfcgal::shared_geom intersection( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
547
555 static sfcgal::shared_geom difference( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
556
563 static sfcgal::shared_geom combine( const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg = nullptr );
564
571 static sfcgal::shared_geom triangulate( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
572
582 static bool covers( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
583
590 static sfcgal::shared_geom convexHull( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
591
600 static sfcgal::shared_geom envelope( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
601
612 static sfcgal::shared_geom offsetCurve( const sfcgal::geometry *geom, double distance, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
613
625 static sfcgal::shared_geom buffer3D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle3D joinStyle3D, QString *errorMsg = nullptr );
626
637 static sfcgal::shared_geom buffer2D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
638
646 static sfcgal::shared_geom extrude( const sfcgal::geometry *geom, const QgsVector3D &extrusion, QString *errorMsg = nullptr );
647
658 static sfcgal::shared_geom simplify( const sfcgal::geometry *geom, double tolerance, bool preserveTopology, QString *errorMsg = nullptr );
659
669 static sfcgal::shared_geom approximateMedialAxis( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
670
671#if SFCGAL_VERSION >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
672
680 static sfcgal::shared_geom transform( const sfcgal::geometry *geom, const QMatrix4x4 &mat, QString *errorMsg = nullptr );
681
688 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg = nullptr );
689
695 static sfcgal::shared_prim createCube( double size, QString *errorMsg = nullptr );
696
703 static sfcgal::shared_prim primitiveClone( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
704
712 static sfcgal::shared_geom primitiveAsPolyhedral( const sfcgal::primitive *prim, const QMatrix4x4 &mat = QMatrix4x4(), QString *errorMsg = nullptr );
713
723 static bool primitiveIsEqual( const sfcgal::primitive *primA, const sfcgal::primitive *primB, double tolerance = -1.0, QString *errorMsg = nullptr );
724
734 static double primitiveArea( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
735
745 static double primitiveVolume( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
746
755 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
756
764 static QVariant primitiveParameter( const sfcgal::primitive *prim, const QString &name, QString *errorMsg = nullptr );
765
774 static void primitiveSetParameter( sfcgal::primitive *prim, const QString &name, const QVariant &value, QString *errorMsg = nullptr );
775
776#endif
777};
778
780
781
782class SFCGALException : public std::runtime_error
783{
784 public:
785 explicit SFCGALException( const QString &message )
786 : std::runtime_error( message.toUtf8().constData() )
787 {
788 }
789};
790
792
793#endif // QGSSFCGALENGINE_H
794#endif
JoinStyle3D
Join styles for 3D buffers.
Definition qgis.h:2134
JoinStyle
Join styles for buffers.
Definition qgis.h:2121
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
A geometry is the spatial representation of a feature.