QGIS API Documentation 3.99.0-Master (18a1e75d814)
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 "qgspoint.h"
31#include "qgsvector3d.h"
32
33#include <QMatrix4x4>
34
35#define SIP_NO_FILE
36
37class QgsGeometry;
38class QgsSfcgalGeometry;
39
42#ifndef SFCGAL_MAKE_VERSION
43#define SFCGAL_MAKE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
44#endif
47#ifndef SFCGAL_VERSION_NUM
48#define SFCGAL_VERSION_NUM SFCGAL_MAKE_VERSION( SFCGAL_VERSION_MAJOR_INT, SFCGAL_VERSION_MINOR_INT, SFCGAL_VERSION_PATCH_INT )
49#endif
50
52#define CHECK_NOT_NULL( ptr, defaultObj ) \
53 if ( !( ptr ) ) \
54 { \
55 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ) ); \
56 return ( defaultObj ); \
57 }
58
60#define CHECK_SUCCESS( errorMsg, defaultObj ) \
61 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
62 { \
63 return ( defaultObj ); \
64 }
65
67#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
68 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
69 { \
70 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
71 return ( defaultObj ); \
72 }
73
75#define THROW_ON_ERROR(errorMsg) \
76 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
77 { \
78 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
79 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
80 }
81
82
83
89namespace sfcgal
90{
91 // ==== SFCGAL geometry
93 using geometry = sfcgal_geometry_t;
94
96 struct GeometryDeleter
97 {
99 void CORE_EXPORT operator()( geometry *geom ) const;
100 };
101
103 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
105 using shared_geom = std::shared_ptr< geometry >; // NO DELETER ==> added with function make_shared_geom!!!!!
106
108 shared_geom make_shared_geom( geometry *geom );
109} // namespace sfcgal
110
111namespace sfcgal
112{
113 // ==== SFCGAL primitive
114#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
116 using primitive = sfcgal_primitive_t;
118 using primitiveType = sfcgal_primitive_type_t;
119#else
121 using primitive = int;
123 using primitiveType = int;
124#endif
125
127 struct PrimitiveDeleter
128 {
130 void CORE_EXPORT operator()( primitive *prim ) const;
131 };
132
134 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
136 using shared_prim = std::shared_ptr< primitive >; // NO DELETER ==> added with function make_shared_prim!!!!!
137
139 shared_prim make_shared_prim( primitive *prim );
140
142 struct PrimitiveParameterDesc
143 {
144 std::string name;
145 std::string type;
146 std::variant<int, double, QgsPoint, QgsVector3D> value;
147 };
148
150 void to_json( json &j, const PrimitiveParameterDesc &p );
151
153 void from_json( const json &j, PrimitiveParameterDesc &p );
154} // namespace sfcgal
155
156namespace sfcgal
157{
158 // ==== SFCGAL errors
160 int errorCallback( const char *, ... );
161
163 int warningCallback( const char *, ... );
164
172 class CORE_EXPORT ErrorHandler
173 {
174 public:
176 ErrorHandler();
177
186 bool hasSucceedOrStack( QString *errorMsg = nullptr, const std::source_location &location = std::source_location::current() );
187
191 void clearText( QString *errorMsg = nullptr );
192
194 bool isTextEmpty() const;
195
197 QString getMainText() const;
198
200 QString getFullText() const;
201
203 void addText( const QString &msg, const std::source_location &location = std::source_location::current() );
204
205 private:
206 QStringList errorMessages;
207 };
208
210 CORE_EXPORT ErrorHandler *errorHandler();
211
213 using func_geomgeom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t *, const sfcgal_geometry_t * );
215 using func_geom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t * );
216} // namespace sfcgal
217
224class CORE_EXPORT QgsSfcgalEngine
225{
226 public:
227
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
418 static bool isValid( const sfcgal::geometry *geom, QString *errorMsg = nullptr, QgsGeometry *errorLoc = nullptr );
419
435 static bool isSimple( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
436
445 static sfcgal::shared_geom boundary( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
446
455 static QgsPoint centroid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
456
466 static sfcgal::shared_geom translate( const sfcgal::geometry *geom, const QgsVector3D &translation, QString *errorMsg = nullptr );
467
476 static sfcgal::shared_geom scale( const sfcgal::geometry *geom, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
477
486 static sfcgal::shared_geom rotate2D( const sfcgal::geometry *geom, double angle, const QgsPoint &center, QString *errorMsg = nullptr );
487
497 static sfcgal::shared_geom rotate3D( const sfcgal::geometry *geom, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
498
506 static double distance( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
507
516 static bool distanceWithin( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double maxdistance, QString *errorMsg );
517
524 static double area( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
525
534 static double length( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
535
544 static bool intersects( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
545
553 static sfcgal::shared_geom intersection( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
554
562 static sfcgal::shared_geom difference( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
563
570 static sfcgal::shared_geom combine( const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg = nullptr );
571
578 static sfcgal::shared_geom triangulate( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
579
589 static bool covers( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
590
597 static sfcgal::shared_geom convexHull( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
598
607 static sfcgal::shared_geom envelope( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
608
619 static sfcgal::shared_geom offsetCurve( const sfcgal::geometry *geom, double distance, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
620
632 static sfcgal::shared_geom buffer3D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle3D joinStyle3D, QString *errorMsg = nullptr );
633
644 static sfcgal::shared_geom buffer2D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
645
653 static sfcgal::shared_geom extrude( const sfcgal::geometry *geom, const QgsVector3D &extrusion, QString *errorMsg = nullptr );
654
665 static sfcgal::shared_geom simplify( const sfcgal::geometry *geom, double tolerance, bool preserveTopology, QString *errorMsg = nullptr );
666
676 static sfcgal::shared_geom approximateMedialAxis( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
677
678#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
679
687 static sfcgal::shared_geom transform( const sfcgal::geometry *geom, const QMatrix4x4 &mat, QString *errorMsg = nullptr );
688
695 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg = nullptr );
696
702 static sfcgal::shared_prim createCube( double size, QString *errorMsg = nullptr );
703
710 static sfcgal::shared_prim primitiveClone( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
711
719 static sfcgal::shared_geom primitiveAsPolyhedral( const sfcgal::primitive *prim, const QMatrix4x4 &mat = QMatrix4x4(), QString *errorMsg = nullptr );
720
730 static bool primitiveIsEqual( const sfcgal::primitive *primA, const sfcgal::primitive *primB, double tolerance = -1.0, QString *errorMsg = nullptr );
731
741 static double primitiveArea( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
742
752 static double primitiveVolume( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
753
762 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
763
771 static QVariant primitiveParameter( const sfcgal::primitive *prim, const QString &name, QString *errorMsg = nullptr );
772
781 static void primitiveSetParameter( sfcgal::primitive *prim, const QString &name, const QVariant &value, QString *errorMsg = nullptr );
782
783#endif
784};
785
787
788
789class SFCGALException : public std::runtime_error
790{
791 public:
792 explicit SFCGALException( const QString &message )
793 : std::runtime_error( message.toUtf8().constData() )
794 {
795 }
796};
797
799
800#endif // QGSSFCGALENGINE_H
801#endif
JoinStyle3D
Join styles for 3D buffers.
Definition qgis.h:2207
JoinStyle
Join styles for buffers.
Definition qgis.h:2194
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:291
A geometry is the spatial representation of a feature.