19#ifndef QGSSFCGALENGINE_H
20#define QGSSFCGALENGINE_H
23#include <SFCGAL/capi/sfcgal_c.h>
24#include <source_location>
37class QgsSfcgalGeometry;
41#ifndef SFCGAL_MAKE_VERSION
42#define SFCGAL_MAKE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
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 )
51#define CHECK_NOT_NULL( ptr, defaultObj ) \
54 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ) ); \
55 return ( defaultObj ); \
59#define CHECK_SUCCESS( errorMsg, defaultObj ) \
60 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
62 return ( defaultObj ); \
66#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
67 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
69 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
70 return ( defaultObj ); \
74#define THROW_ON_ERROR( errorMsg ) \
75 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
77 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
78 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
91 using geometry = sfcgal_geometry_t;
94 struct GeometryDeleter
97 void CORE_EXPORT operator()( geometry *geom )
const;
101 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
103 using shared_geom = std::shared_ptr< geometry >;
106 shared_geom make_shared_geom( geometry *geom );
112#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
114 using primitive = sfcgal_primitive_t;
116 using primitiveType = sfcgal_primitive_type_t;
119 using primitive = int;
121 using primitiveType = int;
125 struct PrimitiveDeleter
128 void CORE_EXPORT operator()( primitive *prim )
const;
132 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
134 using shared_prim = std::shared_ptr< primitive >;
137 shared_prim make_shared_prim( primitive *prim );
140 struct PrimitiveParameterDesc
144 std::variant<int, double, QgsPoint, QgsVector3D> value;
148 void to_json( json &j,
const PrimitiveParameterDesc &p );
151 void from_json(
const json &j, PrimitiveParameterDesc &p );
158 int errorCallback(
const char *, ... );
161 int warningCallback(
const char *, ... );
170 class CORE_EXPORT ErrorHandler
184 bool hasSucceedOrStack( QString *errorMsg =
nullptr,
const std::source_location &location = std::source_location::current() );
189 void clearText( QString *errorMsg =
nullptr );
192 bool isTextEmpty()
const;
195 QString getMainText()
const;
198 QString getFullText()
const;
201 void addText(
const QString &msg,
const std::source_location &location = std::source_location::current() );
204 QStringList errorMessages;
208 CORE_EXPORT ErrorHandler *errorHandler();
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 * );
222class CORE_EXPORT QgsSfcgalEngine
231 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
239 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg =
nullptr );
247 static sfcgal::shared_geom fromAbstractGeometry(
const QgsAbstractGeometry *geom, QString *errorMsg =
nullptr );
255 static sfcgal::shared_geom cloneGeometry(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
265 static QString geometryType(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
273 static sfcgal::shared_geom fromWkb(
const QgsConstWkbPtr &wkbPtr, QString *errorMsg =
nullptr );
281 static sfcgal::shared_geom fromWkt(
const QString &wkt, QString *errorMsg =
nullptr );
289 static QByteArray toWkb(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
298 static QString toWkt(
const sfcgal::geometry *geom,
int numDecimals = -1, QString *errorMsg =
nullptr );
306 static Qgis::WkbType wkbType(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
316 static int dimension(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
328 static int partCount(
const sfcgal::geometry *geom, QString *errorMsg );
340 static bool addZValue( sfcgal::geometry *geom,
double zValue = 0, QString *errorMsg =
nullptr );
352 static bool addMValue( sfcgal::geometry *geom,
double mValue = 0, QString *errorMsg =
nullptr );
363 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
374 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
384 static void swapXy( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
396 static bool isEqual(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB,
double tolerance = -1.0, QString *errorMsg =
nullptr );
404 static bool isEmpty(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
415 static bool isValid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr, QgsGeometry *errorLoc =
nullptr );
432 static bool isSimple(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
442 static sfcgal::shared_geom boundary(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
452 static QgsPoint centroid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
463 static sfcgal::shared_geom translate(
const sfcgal::geometry *geom,
const QgsVector3D &translation, QString *errorMsg =
nullptr );
473 static sfcgal::shared_geom scale(
const sfcgal::geometry *geom,
const QgsVector3D &scaleFactor,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
483 static sfcgal::shared_geom rotate2D(
const sfcgal::geometry *geom,
double angle,
const QgsPoint ¢er, QString *errorMsg =
nullptr );
494 static sfcgal::shared_geom rotate3D(
const sfcgal::geometry *geom,
double angle,
const QgsVector3D &axisVector,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
503 static double distance(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
513 static bool distanceWithin(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB,
double maxdistance, QString *errorMsg );
521 static double area(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
531 static double length(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
541 static bool intersects(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
550 static sfcgal::shared_geom intersection(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
559 static sfcgal::shared_geom difference(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
567 static sfcgal::shared_geom combine(
const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg =
nullptr );
575 static sfcgal::shared_geom triangulate(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
586 static bool covers(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
594 static sfcgal::shared_geom convexHull(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
604 static sfcgal::shared_geom envelope(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
616 static sfcgal::shared_geom offsetCurve(
const sfcgal::geometry *geom,
double distance,
int segments,
Qgis::JoinStyle joinStyle, QString *errorMsg =
nullptr );
629 static sfcgal::shared_geom buffer3D(
const sfcgal::geometry *geom,
double radius,
int segments,
Qgis::JoinStyle3D joinStyle3D, QString *errorMsg =
nullptr );
641 static sfcgal::shared_geom buffer2D(
const sfcgal::geometry *geom,
double radius,
int segments,
Qgis::JoinStyle joinStyle, QString *errorMsg =
nullptr );
650 static sfcgal::shared_geom extrude(
const sfcgal::geometry *geom,
const QgsVector3D &extrusion, QString *errorMsg =
nullptr );
662 static sfcgal::shared_geom simplify(
const sfcgal::geometry *geom,
double tolerance,
bool preserveTopology, QString *errorMsg =
nullptr );
675 static sfcgal::shared_geom approximateMedialAxis(
const sfcgal::geometry *geom,
bool extendToEdges =
false, QString *errorMsg =
nullptr );
688 static sfcgal::shared_geom toSolid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
701 static sfcgal::shared_geom toPolyhedralSurface(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
703#if SFCGAL_VERSION_NUM >= SFCGAL_MAKE_VERSION( 2, 3, 0 )
712 static sfcgal::shared_geom transform(
const sfcgal::geometry *geom,
const QgsMatrix4x4 &mat, QString *errorMsg =
nullptr );
720 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg =
nullptr );
727 static sfcgal::shared_prim createCube(
double size, QString *errorMsg =
nullptr );
735 static sfcgal::shared_prim primitiveClone(
const sfcgal::primitive *prim, QString *errorMsg =
nullptr );
744 static sfcgal::shared_geom primitiveAsPolyhedral(
const sfcgal::primitive *prim,
const QgsMatrix4x4 &mat = QgsMatrix4x4(), QString *errorMsg =
nullptr );
755 static bool primitiveIsEqual(
const sfcgal::primitive *primA,
const sfcgal::primitive *primB,
double tolerance = -1.0, QString *errorMsg =
nullptr );
766 static double primitiveArea(
const sfcgal::primitive *prim,
bool withDiscretization =
false, QString *errorMsg =
nullptr );
777 static double primitiveVolume(
const sfcgal::primitive *prim,
bool withDiscretization =
false, QString *errorMsg =
nullptr );
787 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters(
const sfcgal::primitive *prim, QString *errorMsg =
nullptr );
796 static QVariant primitiveParameter(
const sfcgal::primitive *prim,
const QString &name, QString *errorMsg =
nullptr );
806 static void primitiveSetParameter( sfcgal::primitive *prim,
const QString &name,
const QVariant &value, QString *errorMsg =
nullptr );
814class SFCGALException :
public std::runtime_error
817 explicit SFCGALException(
const QString &message )
818 : std::runtime_error( message.toUtf8().constData() )
JoinStyle3D
Join styles for 3D buffers.
JoinStyle
Join styles for buffers.
WkbType
The WKB type describes the number of dimensions a geometry has.
A geometry is the spatial representation of a feature.