QGIS API Documentation 4.5.0-Master (7108d05adee)
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#include "qgsconfig.h"
19
20#define SIP_NO_FILE
21
22#ifdef HAVE_SFCGAL
23#ifndef QGSSFCGALENGINE_H
24#define QGSSFCGALENGINE_H
25
26
27#include <SFCGAL/capi/sfcgal_c.h>
28#include <source_location>
29
30#include "qgis_core.h"
31#include "qgsexception.h"
32#include "qgsgeometry.h"
33#include "qgslogger.h"
34#include "qgsmatrix4x4.h"
35#include "qgspoint.h"
36#include "qgsvector3d.h"
37
38
39class QgsGeometry;
40class QgsSfcgalGeometry;
41
44#ifndef SFCGAL_COMPUTE_VERSION
45#define SFCGAL_COMPUTE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
46#endif
49#ifndef SFCGAL_VERSION_NUM
50#define SFCGAL_VERSION_NUM SFCGAL_COMPUTE_VERSION( SFCGAL_VERSION_MAJOR_INT, SFCGAL_VERSION_MINOR_INT, SFCGAL_VERSION_PATCH_INT )
51#endif
52
54#define CHECK_NOT_NULL( ptr, defaultObj ) \
55 if ( !( ptr ) ) \
56 { \
57 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ) ); \
58 return ( defaultObj ); \
59 }
60
62#define CHECK_SUCCESS( errorMsg, defaultObj ) \
63 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
64 { \
65 return ( defaultObj ); \
66 }
67
69#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
70 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
71 { \
72 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
73 return ( defaultObj ); \
74 }
75
77#define THROW_ON_ERROR( errorMsg ) \
78 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
79 { \
80 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
81 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
82 }
83
84
90namespace sfcgal
91{
92 // ==== SFCGAL geometry
94 using geometry = sfcgal_geometry_t;
95
97 struct GeometryDeleter
98 {
100 void CORE_EXPORT operator()( geometry *geom ) const;
101 };
102
104 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
106 using shared_geom = std::shared_ptr< geometry >; // NO DELETER ==> added with function make_shared_geom!!!!!
107
109 shared_geom make_shared_geom( geometry *geom );
110} // namespace sfcgal
111
112namespace sfcgal
113{
114 // ==== SFCGAL primitive
115#if SFCGAL_VERSION_NUM >= SFCGAL_COMPUTE_VERSION( 2, 3, 0 )
117 using primitive = sfcgal_primitive_t;
119 using primitiveType = sfcgal_primitive_type_t;
120#else
122 using primitive = int;
124 using primitiveType = int;
125#endif
126
128 struct PrimitiveDeleter
129 {
131 void CORE_EXPORT operator()( primitive *prim ) const;
132 };
133
135 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
137 using shared_prim = std::shared_ptr< primitive >; // NO DELETER ==> added with function make_shared_prim!!!!!
138
140 shared_prim make_shared_prim( primitive *prim );
141
143 struct PrimitiveParameterDesc
144 {
145 std::string name;
146 std::string type;
147 std::variant<int, double, QgsPoint, QgsVector3D> value;
148 };
149
151 void to_json( json &j, const PrimitiveParameterDesc &p );
152
154 void from_json( const json &j, PrimitiveParameterDesc &p );
155} // namespace sfcgal
156
157namespace sfcgal
158{
159 // ==== SFCGAL errors
161 int errorCallback( const char *, ... );
162
164 int warningCallback( const char *, ... );
165
173 class CORE_EXPORT ErrorHandler
174 {
175 public:
177 ErrorHandler();
178
187 bool hasSucceedOrStack( QString *errorMsg = nullptr, const std::source_location &location = std::source_location::current() );
188
192 void clearText( QString *errorMsg = nullptr );
193
195 bool isTextEmpty() const;
196
198 QString getMainText() const;
199
201 QString getFullText() const;
202
204 void addText( const QString &msg, const std::source_location &location = std::source_location::current() );
205
206 private:
207 QStringList errorMessages;
208 };
209
211 CORE_EXPORT ErrorHandler *errorHandler();
212
214 using func_geomgeom_to_geom = sfcgal_geometry_t *( * ) ( const sfcgal_geometry_t *, const sfcgal_geometry_t * );
216 using func_geom_to_geom = sfcgal_geometry_t *( * ) ( const sfcgal_geometry_t * );
217} // namespace sfcgal
218
225class CORE_EXPORT QgsSfcgalEngine
226{
227 public:
234 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
235
242 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg = nullptr );
243
250 static sfcgal::shared_geom fromAbstractGeometry( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr );
251
258 static sfcgal::shared_geom cloneGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
259
268 static QString geometryType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
269
276 static sfcgal::shared_geom fromWkb( const QgsConstWkbPtr &wkbPtr, QString *errorMsg = nullptr );
277
284 static sfcgal::shared_geom fromWkt( const QString &wkt, QString *errorMsg = nullptr );
285
292 static QByteArray toWkb( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
293
301 static QString toWkt( const sfcgal::geometry *geom, int numDecimals = -1, QString *errorMsg = nullptr );
302
309 static Qgis::WkbType wkbType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
310
319 static int dimension( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
320
331 static int partCount( const sfcgal::geometry *geom, QString *errorMsg );
332
343 static bool addZValue( sfcgal::geometry *geom, double zValue = 0, QString *errorMsg = nullptr );
344
355 static bool addMValue( sfcgal::geometry *geom, double mValue = 0, QString *errorMsg = nullptr );
356
366 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
367
377 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
378
387 static void swapXy( sfcgal::geometry *geom, QString *errorMsg = nullptr );
388
399 static bool isEqual( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double tolerance = -1.0, QString *errorMsg = nullptr );
400
407 static bool isEmpty( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
408
419 static bool isValid( const sfcgal::geometry *geom, QString *errorMsg = nullptr, QString *reasonMsg = nullptr, QgsGeometry *errorLoc = nullptr );
420
436 static bool isSimple( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
437
438
453 static sfcgal::shared_geom geometryN( const sfcgal::geometry *geom, unsigned int index, QString *errorMsg = nullptr );
454
463 static sfcgal::shared_geom boundary( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
464
473 static QgsPoint centroid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
474
484 static sfcgal::shared_geom translate( const sfcgal::geometry *geom, const QgsVector3D &translation, QString *errorMsg = nullptr );
485
494 static sfcgal::shared_geom scale( const sfcgal::geometry *geom, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
495
504 static sfcgal::shared_geom rotate2D( const sfcgal::geometry *geom, double angle, const QgsPoint &center, QString *errorMsg = nullptr );
505
515 static sfcgal::shared_geom rotate3D( const sfcgal::geometry *geom, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
516
524 static double distance( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
525
534 static bool distanceWithin( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double maxdistance, QString *errorMsg );
535
542 static double area( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
543
552 static double length( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
553
562 static bool intersects( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
563
571 static sfcgal::shared_geom intersection( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
572
580 static sfcgal::shared_geom difference( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
581
588 static sfcgal::shared_geom combine( const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg = nullptr );
589
596 static sfcgal::shared_geom triangulate( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
597
607 static bool covers( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
608
615 static sfcgal::shared_geom convexHull( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
616
625 static sfcgal::shared_geom envelope( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
626
637 static sfcgal::shared_geom offsetCurve( const sfcgal::geometry *geom, double distance, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
638
650 static sfcgal::shared_geom buffer3D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle3D joinStyle3D, QString *errorMsg = nullptr );
651
662 static sfcgal::shared_geom buffer2D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
663
671 static sfcgal::shared_geom extrude( const sfcgal::geometry *geom, const QgsVector3D &extrusion, QString *errorMsg = nullptr );
672
683 static sfcgal::shared_geom simplify( const sfcgal::geometry *geom, double tolerance, bool preserveTopology, QString *errorMsg = nullptr );
684
696 static sfcgal::shared_geom approximateMedialAxis( const sfcgal::geometry *geom, bool extendToEdges = false, QString *errorMsg = nullptr );
697
709 static sfcgal::shared_geom toSolid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
710
722 static sfcgal::shared_geom toPolyhedralSurface( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
723
724#if SFCGAL_VERSION_NUM >= SFCGAL_COMPUTE_VERSION( 2, 3, 0 )
725
733 static sfcgal::shared_geom transform( const sfcgal::geometry *geom, const QgsMatrix4x4 &mat, QString *errorMsg = nullptr );
734
749 static sfcgal::shared_geom split3D( const sfcgal::geometry *geom, const QgsPoint &planePoint, const QgsVector3D &planeNormal, bool closeGeometries, QString *errorMsg = nullptr );
750
757 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg = nullptr );
758
772 static sfcgal::shared_prim createBox( double sizeX, double sizeY, double sizeZ, QString *errorMsg = nullptr );
773
788 static sfcgal::shared_prim createCone( double bottomRadius, double height, double topRadius, unsigned int radial, QString *errorMsg = nullptr );
789
795 static sfcgal::shared_prim createCube( double size, QString *errorMsg = nullptr );
796
810 static sfcgal::shared_prim createCylinder( double radius, double height, unsigned int radial, QString *errorMsg = nullptr );
811
824 static sfcgal::shared_prim createSphere( double radius, unsigned int subdivisions, QString *errorMsg = nullptr );
825
840 static sfcgal::shared_prim createTorus( double mainRadius, double tubeRadius, unsigned int mainRadial, unsigned int tubeRadial, QString *errorMsg = nullptr );
841
848 static sfcgal::shared_prim primitiveClone( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
849
856 static sfcgal::shared_geom primitiveAsPolyhedral( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
857
867 static bool primitiveIsEqual( const sfcgal::primitive *primA, const sfcgal::primitive *primB, double tolerance = -1.0, QString *errorMsg = nullptr );
868
878 static double primitiveArea( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
879
889 static double primitiveVolume( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
890
899 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
900
908 static QVariant primitiveParameter( const sfcgal::primitive *prim, const QString &name, QString *errorMsg = nullptr );
909
918 static void primitiveSetParameter( sfcgal::primitive *prim, const QString &name, const QVariant &value, QString *errorMsg = nullptr );
919
929 static sfcgal::shared_prim primitiveTranslate( const sfcgal::primitive *prim, const QgsVector3D &translation, QString *errorMsg = nullptr );
930
942 static sfcgal::shared_prim primitiveRotate( const sfcgal::primitive *prim, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
943
954 static sfcgal::shared_prim primitiveScale( const sfcgal::primitive *prim, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
955#endif
956};
957
959
960
961class SFCGALException : public std::runtime_error
962{
963 public:
964 explicit SFCGALException( const QString &message )
965 : std::runtime_error( message.toUtf8().constData() )
966 {}
967};
968
970
971#endif // QGSSFCGALENGINE_H
972#endif
JoinStyle3D
Join styles for 3D buffers.
Definition qgis.h:2307
JoinStyle
Join styles for buffers.
Definition qgis.h:2294
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:314
A geometry is the spatial representation of a feature.