QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 <QMatrix4x4>
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
82
88namespace sfcgal
89{
90 // ==== SFCGAL geometry
92 using geometry = sfcgal_geometry_t;
93
95 struct GeometryDeleter
96 {
98 void CORE_EXPORT operator()( geometry *geom ) const;
99 };
100
102 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
104 using shared_geom = std::shared_ptr< geometry >; // NO DELETER ==> added with function make_shared_geom!!!!!
105
107 shared_geom make_shared_geom( geometry *geom );
108} // namespace sfcgal
109
110namespace sfcgal
111{
112 // ==== SFCGAL primitive
113#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
115 using primitive = sfcgal_primitive_t;
117 using primitiveType = sfcgal_primitive_type_t;
118#else
120 using primitive = int;
122 using primitiveType = int;
123#endif
124
126 struct PrimitiveDeleter
127 {
129 void CORE_EXPORT operator()( primitive *prim ) const;
130 };
131
133 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
135 using shared_prim = std::shared_ptr< primitive >; // NO DELETER ==> added with function make_shared_prim!!!!!
136
138 shared_prim make_shared_prim( primitive *prim );
139
141 struct PrimitiveParameterDesc
142 {
143 std::string name;
144 std::string type;
145 std::variant<int, double, QgsPoint, QgsVector3D> value;
146 };
147
149 void to_json( json &j, const PrimitiveParameterDesc &p );
150
152 void from_json( const json &j, PrimitiveParameterDesc &p );
153} // namespace sfcgal
154
155namespace sfcgal
156{
157 // ==== SFCGAL errors
159 int errorCallback( const char *, ... );
160
162 int warningCallback( const char *, ... );
163
171 class CORE_EXPORT ErrorHandler
172 {
173 public:
175 ErrorHandler();
176
185 bool hasSucceedOrStack( QString *errorMsg = nullptr, const std::source_location &location = std::source_location::current() );
186
190 void clearText( QString *errorMsg = nullptr );
191
193 bool isTextEmpty() const;
194
196 QString getMainText() const;
197
199 QString getFullText() const;
200
202 void addText( const QString &msg, const std::source_location &location = std::source_location::current() );
203
204 private:
205 QStringList errorMessages;
206 };
207
209 CORE_EXPORT ErrorHandler *errorHandler();
210
212 using func_geomgeom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t *, const sfcgal_geometry_t * );
214 using func_geom_to_geom = sfcgal_geometry_t *( * )( const sfcgal_geometry_t * );
215} // namespace sfcgal
216
223class CORE_EXPORT QgsSfcgalEngine
224{
225 public:
226
233 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
234
241 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg = nullptr );
242
249 static sfcgal::shared_geom fromAbstractGeometry( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr );
250
257 static sfcgal::shared_geom cloneGeometry( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
258
267 static QString geometryType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
268
275 static sfcgal::shared_geom fromWkb( const QgsConstWkbPtr &wkbPtr, QString *errorMsg = nullptr );
276
283 static sfcgal::shared_geom fromWkt( const QString &wkt, QString *errorMsg = nullptr );
284
291 static QByteArray toWkb( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
292
300 static QString toWkt( const sfcgal::geometry *geom, int numDecimals = -1, QString *errorMsg = nullptr );
301
308 static Qgis::WkbType wkbType( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
309
318 static int dimension( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
319
330 static int partCount( const sfcgal::geometry *geom, QString *errorMsg );
331
342 static bool addZValue( sfcgal::geometry *geom, double zValue = 0, QString *errorMsg = nullptr );
343
354 static bool addMValue( sfcgal::geometry *geom, double mValue = 0, QString *errorMsg = nullptr );
355
365 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
366
376 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg = nullptr );
377
386 static void swapXy( sfcgal::geometry *geom, QString *errorMsg = nullptr );
387
398 static bool isEqual( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double tolerance = -1.0, QString *errorMsg = nullptr );
399
406 static bool isEmpty( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
407
417 static bool isValid( const sfcgal::geometry *geom, QString *errorMsg = nullptr, QgsGeometry *errorLoc = nullptr );
418
434 static bool isSimple( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
435
444 static sfcgal::shared_geom boundary( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
445
454 static QgsPoint centroid( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
455
465 static sfcgal::shared_geom translate( const sfcgal::geometry *geom, const QgsVector3D &translation, QString *errorMsg = nullptr );
466
475 static sfcgal::shared_geom scale( const sfcgal::geometry *geom, const QgsVector3D &scaleFactor, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
476
485 static sfcgal::shared_geom rotate2D( const sfcgal::geometry *geom, double angle, const QgsPoint &center, QString *errorMsg = nullptr );
486
496 static sfcgal::shared_geom rotate3D( const sfcgal::geometry *geom, double angle, const QgsVector3D &axisVector, const QgsPoint &center = QgsPoint(), QString *errorMsg = nullptr );
497
505 static double distance( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
506
515 static bool distanceWithin( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, double maxdistance, QString *errorMsg );
516
523 static double area( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
524
533 static double length( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
534
543 static bool intersects( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
544
552 static sfcgal::shared_geom intersection( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
553
561 static sfcgal::shared_geom difference( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
562
569 static sfcgal::shared_geom combine( const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg = nullptr );
570
577 static sfcgal::shared_geom triangulate( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
578
588 static bool covers( const sfcgal::geometry *geomA, const sfcgal::geometry *geomB, QString *errorMsg = nullptr );
589
596 static sfcgal::shared_geom convexHull( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
597
606 static sfcgal::shared_geom envelope( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
607
618 static sfcgal::shared_geom offsetCurve( const sfcgal::geometry *geom, double distance, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
619
631 static sfcgal::shared_geom buffer3D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle3D joinStyle3D, QString *errorMsg = nullptr );
632
643 static sfcgal::shared_geom buffer2D( const sfcgal::geometry *geom, double radius, int segments, Qgis::JoinStyle joinStyle, QString *errorMsg = nullptr );
644
652 static sfcgal::shared_geom extrude( const sfcgal::geometry *geom, const QgsVector3D &extrusion, QString *errorMsg = nullptr );
653
664 static sfcgal::shared_geom simplify( const sfcgal::geometry *geom, double tolerance, bool preserveTopology, QString *errorMsg = nullptr );
665
675 static sfcgal::shared_geom approximateMedialAxis( const sfcgal::geometry *geom, QString *errorMsg = nullptr );
676
677#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
678
686 static sfcgal::shared_geom transform( const sfcgal::geometry *geom, const QMatrix4x4 &mat, QString *errorMsg = nullptr );
687
694 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg = nullptr );
695
701 static sfcgal::shared_prim createCube( double size, QString *errorMsg = nullptr );
702
709 static sfcgal::shared_prim primitiveClone( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
710
718 static sfcgal::shared_geom primitiveAsPolyhedral( const sfcgal::primitive *prim, const QMatrix4x4 &mat = QMatrix4x4(), QString *errorMsg = nullptr );
719
729 static bool primitiveIsEqual( const sfcgal::primitive *primA, const sfcgal::primitive *primB, double tolerance = -1.0, QString *errorMsg = nullptr );
730
740 static double primitiveArea( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
741
751 static double primitiveVolume( const sfcgal::primitive *prim, bool withDiscretization = false, QString *errorMsg = nullptr );
752
761 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters( const sfcgal::primitive *prim, QString *errorMsg = nullptr );
762
770 static QVariant primitiveParameter( const sfcgal::primitive *prim, const QString &name, QString *errorMsg = nullptr );
771
780 static void primitiveSetParameter( sfcgal::primitive *prim, const QString &name, const QVariant &value, QString *errorMsg = nullptr );
781
782#endif
783};
784
786
787
788class SFCGALException : public std::runtime_error
789{
790 public:
791 explicit SFCGALException( const QString &message )
792 : std::runtime_error( message.toUtf8().constData() )
793 {
794 }
795};
796
798
799#endif // QGSSFCGALENGINE_H
800#endif
JoinStyle3D
Join styles for 3D buffers.
Definition qgis.h:2192
JoinStyle
Join styles for buffers.
Definition qgis.h:2179
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
A geometry is the spatial representation of a feature.