23#ifndef QGSSFCGALENGINE_H
24#define QGSSFCGALENGINE_H
27#include <SFCGAL/capi/sfcgal_c.h>
28#include <source_location>
40class QgsSfcgalGeometry;
44#ifndef SFCGAL_COMPUTE_VERSION
45#define SFCGAL_COMPUTE_VERSION( major, minor, patch ) ( ( major ) * 10000 + ( minor ) * 100 + ( patch ) )
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 )
54#define CHECK_NOT_NULL( ptr, defaultObj ) \
57 sfcgal::errorHandler()->addText( QString( "Null pointer for '%1'" ).arg( #ptr ) ); \
58 return ( defaultObj ); \
62#define CHECK_SUCCESS( errorMsg, defaultObj ) \
63 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
65 return ( defaultObj ); \
69#define CHECK_SUCCESS_LOG( errorMsg, defaultObj ) \
70 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
72 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
73 return ( defaultObj ); \
77#define THROW_ON_ERROR( errorMsg ) \
78 if ( !sfcgal::errorHandler()->hasSucceedOrStack( ( errorMsg ) ) ) \
80 QgsDebugError( sfcgal::errorHandler()->getFullText() ); \
81 throw QgsSfcgalException( sfcgal::errorHandler()->getFullText() ); \
94 using geometry = sfcgal_geometry_t;
97 struct GeometryDeleter
100 void CORE_EXPORT operator()( geometry *geom )
const;
104 using unique_geom = std::unique_ptr< geometry, GeometryDeleter >;
106 using shared_geom = std::shared_ptr< geometry >;
109 shared_geom make_shared_geom( geometry *geom );
115#if SFCGAL_VERSION_NUM >= SFCGAL_COMPUTE_VERSION( 2, 3, 0 )
117 using primitive = sfcgal_primitive_t;
119 using primitiveType = sfcgal_primitive_type_t;
122 using primitive = int;
124 using primitiveType = int;
128 struct PrimitiveDeleter
131 void CORE_EXPORT operator()( primitive *prim )
const;
135 using unique_prim = std::unique_ptr< primitive, PrimitiveDeleter >;
137 using shared_prim = std::shared_ptr< primitive >;
140 shared_prim make_shared_prim( primitive *prim );
143 struct PrimitiveParameterDesc
147 std::variant<int, double, QgsPoint, QgsVector3D> value;
151 void to_json( json &j,
const PrimitiveParameterDesc &p );
154 void from_json(
const json &j, PrimitiveParameterDesc &p );
161 int errorCallback(
const char *, ... );
164 int warningCallback(
const char *, ... );
173 class CORE_EXPORT ErrorHandler
187 bool hasSucceedOrStack( QString *errorMsg =
nullptr,
const std::source_location &location = std::source_location::current() );
192 void clearText( QString *errorMsg =
nullptr );
195 bool isTextEmpty()
const;
198 QString getMainText()
const;
201 QString getFullText()
const;
204 void addText(
const QString &msg,
const std::source_location &location = std::source_location::current() );
207 QStringList errorMessages;
211 CORE_EXPORT ErrorHandler *errorHandler();
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 * );
225class CORE_EXPORT QgsSfcgalEngine
234 static std::unique_ptr< QgsAbstractGeometry > toAbstractGeometry(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
242 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_geom &geom, QString *errorMsg =
nullptr );
250 static sfcgal::shared_geom fromAbstractGeometry(
const QgsAbstractGeometry *geom, QString *errorMsg =
nullptr );
258 static sfcgal::shared_geom cloneGeometry(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
268 static QString geometryType(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
276 static sfcgal::shared_geom fromWkb(
const QgsConstWkbPtr &wkbPtr, QString *errorMsg =
nullptr );
284 static sfcgal::shared_geom fromWkt(
const QString &wkt, QString *errorMsg =
nullptr );
292 static QByteArray toWkb(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
301 static QString toWkt(
const sfcgal::geometry *geom,
int numDecimals = -1, QString *errorMsg =
nullptr );
309 static Qgis::WkbType wkbType(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
319 static int dimension(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
331 static int partCount(
const sfcgal::geometry *geom, QString *errorMsg );
343 static bool addZValue( sfcgal::geometry *geom,
double zValue = 0, QString *errorMsg =
nullptr );
355 static bool addMValue( sfcgal::geometry *geom,
double mValue = 0, QString *errorMsg =
nullptr );
366 static bool dropZValue( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
377 static bool dropMValue( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
387 static void swapXy( sfcgal::geometry *geom, QString *errorMsg =
nullptr );
399 static bool isEqual(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB,
double tolerance = -1.0, QString *errorMsg =
nullptr );
407 static bool isEmpty(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
419 static bool isValid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr, QString *reasonMsg =
nullptr, QgsGeometry *errorLoc =
nullptr );
436 static bool isSimple(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
453 static sfcgal::shared_geom geometryN(
const sfcgal::geometry *geom,
unsigned int index, QString *errorMsg =
nullptr );
463 static sfcgal::shared_geom boundary(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
473 static QgsPoint centroid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
484 static sfcgal::shared_geom translate(
const sfcgal::geometry *geom,
const QgsVector3D &translation, QString *errorMsg =
nullptr );
494 static sfcgal::shared_geom scale(
const sfcgal::geometry *geom,
const QgsVector3D &scaleFactor,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
504 static sfcgal::shared_geom rotate2D(
const sfcgal::geometry *geom,
double angle,
const QgsPoint ¢er, QString *errorMsg =
nullptr );
515 static sfcgal::shared_geom rotate3D(
const sfcgal::geometry *geom,
double angle,
const QgsVector3D &axisVector,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
524 static double distance(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
534 static bool distanceWithin(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB,
double maxdistance, QString *errorMsg );
542 static double area(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
552 static double length(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
562 static bool intersects(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
571 static sfcgal::shared_geom intersection(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
580 static sfcgal::shared_geom difference(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
588 static sfcgal::shared_geom combine(
const QVector< sfcgal::shared_geom > &geomList, QString *errorMsg =
nullptr );
596 static sfcgal::shared_geom triangulate(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
607 static bool covers(
const sfcgal::geometry *geomA,
const sfcgal::geometry *geomB, QString *errorMsg =
nullptr );
615 static sfcgal::shared_geom convexHull(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
625 static sfcgal::shared_geom envelope(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
637 static sfcgal::shared_geom offsetCurve(
const sfcgal::geometry *geom,
double distance,
int segments,
Qgis::JoinStyle joinStyle, QString *errorMsg =
nullptr );
650 static sfcgal::shared_geom buffer3D(
const sfcgal::geometry *geom,
double radius,
int segments,
Qgis::JoinStyle3D joinStyle3D, QString *errorMsg =
nullptr );
662 static sfcgal::shared_geom buffer2D(
const sfcgal::geometry *geom,
double radius,
int segments,
Qgis::JoinStyle joinStyle, QString *errorMsg =
nullptr );
671 static sfcgal::shared_geom extrude(
const sfcgal::geometry *geom,
const QgsVector3D &extrusion, QString *errorMsg =
nullptr );
683 static sfcgal::shared_geom simplify(
const sfcgal::geometry *geom,
double tolerance,
bool preserveTopology, QString *errorMsg =
nullptr );
696 static sfcgal::shared_geom approximateMedialAxis(
const sfcgal::geometry *geom,
bool extendToEdges =
false, QString *errorMsg =
nullptr );
709 static sfcgal::shared_geom toSolid(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
722 static sfcgal::shared_geom toPolyhedralSurface(
const sfcgal::geometry *geom, QString *errorMsg =
nullptr );
724#if SFCGAL_VERSION_NUM >= SFCGAL_COMPUTE_VERSION( 2, 3, 0 )
733 static sfcgal::shared_geom transform(
const sfcgal::geometry *geom,
const QgsMatrix4x4 &mat, QString *errorMsg =
nullptr );
749 static sfcgal::shared_geom split3D(
const sfcgal::geometry *geom,
const QgsPoint &planePoint,
const QgsVector3D &planeNormal,
bool closeGeometries, QString *errorMsg =
nullptr );
757 static std::unique_ptr< QgsSfcgalGeometry > toSfcgalGeometry( sfcgal::shared_prim &prim, sfcgal::primitiveType type, QString *errorMsg =
nullptr );
772 static sfcgal::shared_prim createBox(
double sizeX,
double sizeY,
double sizeZ, QString *errorMsg =
nullptr );
788 static sfcgal::shared_prim createCone(
double bottomRadius,
double height,
double topRadius,
unsigned int radial, QString *errorMsg =
nullptr );
795 static sfcgal::shared_prim createCube(
double size, QString *errorMsg =
nullptr );
810 static sfcgal::shared_prim createCylinder(
double radius,
double height,
unsigned int radial, QString *errorMsg =
nullptr );
824 static sfcgal::shared_prim createSphere(
double radius,
unsigned int subdivisions, QString *errorMsg =
nullptr );
840 static sfcgal::shared_prim createTorus(
double mainRadius,
double tubeRadius,
unsigned int mainRadial,
unsigned int tubeRadial, QString *errorMsg =
nullptr );
848 static sfcgal::shared_prim primitiveClone(
const sfcgal::primitive *prim, QString *errorMsg =
nullptr );
856 static sfcgal::shared_geom primitiveAsPolyhedral(
const sfcgal::primitive *prim, QString *errorMsg =
nullptr );
867 static bool primitiveIsEqual(
const sfcgal::primitive *primA,
const sfcgal::primitive *primB,
double tolerance = -1.0, QString *errorMsg =
nullptr );
878 static double primitiveArea(
const sfcgal::primitive *prim,
bool withDiscretization =
false, QString *errorMsg =
nullptr );
889 static double primitiveVolume(
const sfcgal::primitive *prim,
bool withDiscretization =
false, QString *errorMsg =
nullptr );
899 static QVector<sfcgal::PrimitiveParameterDesc> primitiveParameters(
const sfcgal::primitive *prim, QString *errorMsg =
nullptr );
908 static QVariant primitiveParameter(
const sfcgal::primitive *prim,
const QString &name, QString *errorMsg =
nullptr );
918 static void primitiveSetParameter( sfcgal::primitive *prim,
const QString &name,
const QVariant &value, QString *errorMsg =
nullptr );
929 static sfcgal::shared_prim primitiveTranslate(
const sfcgal::primitive *prim,
const QgsVector3D &translation, QString *errorMsg =
nullptr );
942 static sfcgal::shared_prim primitiveRotate(
const sfcgal::primitive *prim,
double angle,
const QgsVector3D &axisVector,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
954 static sfcgal::shared_prim primitiveScale(
const sfcgal::primitive *prim,
const QgsVector3D &scaleFactor,
const QgsPoint ¢er = QgsPoint(), QString *errorMsg =
nullptr );
961class SFCGALException :
public std::runtime_error
964 explicit SFCGALException(
const QString &message )
965 : 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.