Schema dispatch in helpers

This commit is contained in:
Thomas Krijnen
2026-07-03 13:26:30 +02:00
parent 55e97e5379
commit 971cef0170
9 changed files with 1690 additions and 477 deletions
+223 -149
View File
@@ -18,166 +18,235 @@
********************************************************************************/
#include "geolocation.h"
#include "placement.h"
#include "../ifcparse/express.h"
#include "../ifcparse/exception.h"
#include "../ifcparse/file.h"
#include "../ifcparse/instance_data.h"
#include "../ifcparse/schema.h"
#include "placement.h"
#include "pset.h"
#include "schema_dispatch.i"
#include <cmath>
#include <string>
#include <type_traits>
#include <vector>
namespace {
// Read a numeric NominalValue out of an IfcPropertySingleValue. IFC2X3
// ePSet_MapConversion stores eastings/northings/scale as IfcLengthMeasure or
// IfcReal wrapped inside IfcValue (a SELECT) — get_attribute_value(0) peels
// the wrapper. Returns nullopt if the value is missing or non-numeric.
std::optional<double> read_property_value_double(const express::Base& property) {
if (!property.declaration().is("IfcPropertySingleValue")) return std::nullopt;
auto pe = property.as<express::Entity>();
auto nv = pe.get("NominalValue");
if (nv.isNull()) return std::nullopt;
express::Base wrapper = nv;
auto inner = wrapper.get_attribute_value(0);
if (inner.isNull()) return std::nullopt;
switch (inner.type()) {
case ifcopenshell::Argument_DOUBLE: return (double) inner;
case ifcopenshell::Argument_INT: return (double)(int) inner;
default: return std::nullopt;
template <typename T>
struct is_optional : std::false_type {};
template <typename T>
struct is_optional<std::optional<T>> : std::true_type {};
template <typename Schema, typename = void>
struct is_ifc4_or_higher : std::false_type {};
template <typename Schema>
struct is_ifc4_or_higher<Schema, std::void_t<typename Schema::IfcCoordinateOperation>> : std::true_type {};
template <typename Schema, typename = void>
struct has_map_conversion_scaled : std::false_type {};
template <typename Schema>
struct has_map_conversion_scaled<Schema, std::void_t<typename Schema::IfcMapConversionScaled>> : std::true_type {};
template <typename T, typename = void>
struct has_factor_x : std::false_type {};
template <typename T>
struct has_factor_x<T, std::void_t<decltype(std::declval<T>().FactorX())>> : std::true_type {};
template <typename Schema, typename = void>
struct has_rigid_operation : std::false_type {};
template <typename Schema>
struct has_rigid_operation<Schema, std::void_t<typename Schema::IfcRigidOperation>> : std::true_type {};
[[noreturn]] void unsupported_schema(const std::string& name) {
throw ifcopenshell::exception("No helper implementation was built for schema " + name);
}
double numeric_property(const property_map& properties,
const std::string& name,
double fallback) {
const auto found = properties.find(name);
if (found == properties.end()) {
return fallback;
}
if (const auto value = found->second.get_if<double>()) {
return *value;
}
if (const auto value = found->second.get_if<std::int64_t>()) {
return static_cast<double>(*value);
}
return fallback;
}
double selected_number(const express::Base& selected, double fallback) {
if (!selected) {
return fallback;
}
const auto value = selected.get_attribute_value(0);
if (value.isNull()) {
return fallback;
}
if (value.type() == ifcopenshell::Argument_DOUBLE) {
return static_cast<double>(value);
}
if (value.type() == ifcopenshell::Argument_INT) {
return static_cast<double>(static_cast<int>(value));
}
return fallback;
}
template <typename T>
double optional_number(const T& value, double fallback) {
if constexpr (is_optional<T>::value) {
return value.value_or(fallback);
} else {
return value;
}
}
} // namespace
std::optional<HelmertTransformation>
get_helmert_transformation_parameters(ifcopenshell::file* ifc_file) {
HelmertTransformation p;
const std::string schema_name = ifc_file->schema()->name();
if (schema_name == "IFC2X3") {
auto projects = ifc_file->instances_by_type("IfcProject");
if (projects.empty()) return std::nullopt;
const auto& project = projects[0];
bool found = false;
auto rels = project.as<express::Entity>().get_inverse("IsDefinedBy");
for (const auto& rel : rels) {
if (!rel.declaration().is("IfcRelDefinesByProperties")) continue;
express::Base pset_base = rel.get("RelatingPropertyDefinition");
if (!pset_base.declaration().is("IfcPropertySet")) continue;
auto pset = pset_base.as<express::Entity>();
auto name_attr = pset.get("Name");
if (name_attr.isNull()) continue;
std::string pset_name = name_attr;
if (pset_name != "ePSet_MapConversion") continue;
std::vector<express::Base> props = pset.get("HasProperties");
for (const auto& prop : props) {
if (!prop.declaration().is("IfcPropertySingleValue")) continue;
auto pe = prop.as<express::Entity>();
auto pname_attr = pe.get("Name");
if (pname_attr.isNull()) continue;
std::string pname = pname_attr;
auto value = read_property_value_double(prop);
if (!value) continue;
if (pname == "Eastings") p.e = *value;
else if (pname == "Northings") p.n = *value;
else if (pname == "OrthogonalHeight") p.h = *value;
else if (pname == "XAxisAbscissa") p.xaa = *value;
else if (pname == "XAxisOrdinate") p.xao = *value;
else if (pname == "Scale") p.scale = *value;
}
found = true;
break;
}
if (!found) return std::nullopt;
// Python: `conversion.get("Scale", None) or 1` — 0 falls back to 1.
if (p.scale == 0.0) p.scale = 1.0;
p.factor_x = p.factor_y = p.factor_z = 1.0;
} else {
std::vector<express::Base> conversions;
try {
conversions = ifc_file->instances_by_type("IfcCoordinateOperation");
} catch (...) {
// Schema doesn't know IfcCoordinateOperation.
template <typename Schema>
std::optional<HelmertTransformation> get_helmert_transformation_parameters_s(ifcopenshell::file* ifc_file) {
HelmertTransformation result;
if constexpr (!is_ifc4_or_higher<Schema>::value) {
const auto projects = ifc_file->template instances_by_type<typename Schema::IfcProject>();
if (projects.empty()) {
return std::nullopt;
}
if (conversions.empty()) return std::nullopt;
const auto& conversion = conversions[0];
auto entity = conversion.as<express::Entity>();
const std::string type_name = conversion.declaration().name();
auto get_or = [&](const std::string& name, double fallback) {
auto a = entity.get(name);
return a.isNull() ? fallback : (double) a;
};
if (conversion.declaration().is("IfcMapConversion")) {
p.e = get_or("Eastings", 0.0);
p.n = get_or("Northings", 0.0);
p.h = get_or("OrthogonalHeight", 0.0);
p.xaa = get_or("XAxisAbscissa", 0.0);
p.xao = get_or("XAxisOrdinate", 0.0);
p.scale = get_or("Scale", 1.0);
if (p.scale == 0.0) p.scale = 1.0;
if (type_name == "IfcMapConversionScaled") {
p.factor_x = entity.get("FactorX");
p.factor_y = entity.get("FactorY");
p.factor_z = entity.get("FactorZ");
} else {
p.factor_x = p.factor_y = p.factor_z = 1.0;
const auto conversion = get_pset(projects.front(), "ePSet_MapConversion");
if (!conversion) {
return std::nullopt;
}
const auto* properties = conversion->template get_if<property_map>();
if (!properties) {
return std::nullopt;
}
result.e = numeric_property(*properties, "Eastings", 0.0);
result.n = numeric_property(*properties, "Northings", 0.0);
result.h = numeric_property(*properties, "OrthogonalHeight", 0.0);
result.xaa = numeric_property(*properties, "XAxisAbscissa", 0.0);
result.xao = numeric_property(*properties, "XAxisOrdinate", 0.0);
result.scale = numeric_property(*properties, "Scale", 1.0);
} else {
const auto conversions =
ifc_file->template instances_by_type<typename Schema::IfcCoordinateOperation>();
if (conversions.empty()) {
return std::nullopt;
}
const auto& conversion = conversions.front();
if (auto map_conversion = conversion.template as<typename Schema::IfcMapConversion>()) {
result.e = map_conversion.Eastings();
result.n = map_conversion.Northings();
result.h = map_conversion.OrthogonalHeight();
result.xaa = map_conversion.XAxisAbscissa().value_or(0.0);
result.xao = map_conversion.XAxisOrdinate().value_or(0.0);
result.scale = map_conversion.Scale().value_or(1.0);
if constexpr (has_map_conversion_scaled<Schema>::value) {
if (auto scaled = conversion.template as<typename Schema::IfcMapConversionScaled>()) {
if constexpr (has_factor_x<decltype(scaled)>::value) {
result.factor_x = scaled.FactorX();
result.factor_y = scaled.FactorY();
result.factor_z = scaled.FactorZ();
} else {
result.factor_x = scaled.ScaleX();
result.factor_y = scaled.ScaleY();
result.factor_z = scaled.ScaleZ();
}
}
}
} else if constexpr (has_rigid_operation<Schema>::value) {
if (auto rigid = conversion.template as<typename Schema::IfcRigidOperation>()) {
result.e = selected_number(rigid.FirstCoordinate().concrete(), 0.0);
result.n = selected_number(rigid.SecondCoordinate().concrete(), 0.0);
result.h = optional_number(rigid.Height(), 0.0);
} else {
return std::nullopt;
}
} else if (type_name == "IfcRigidOperation") {
// FirstCoordinate / SecondCoordinate are IfcLengthMeasure-typed
// values; the C++ binding auto-unwraps defined types of REAL.
p.e = get_or("FirstCoordinate", 0.0);
p.n = get_or("SecondCoordinate", 0.0);
p.h = get_or("Height", 0.0);
p.xaa = 1.0;
p.xao = 0.0;
p.scale = p.factor_x = p.factor_y = p.factor_z = 1.0;
} else {
return std::nullopt;
}
}
if (p.xaa == 0.0 && p.xao == 0.0) {
p.xaa = 1.0;
p.xao = 0.0;
if (result.scale == 0.0) {
result.scale = 1.0;
}
return p;
if (result.xaa == 0.0 && result.xao == 0.0) {
result.xaa = 1.0;
}
return result;
}
std::optional<Eigen::Matrix4d> get_wcs(ifcopenshell::file* ifc_file) {
auto contexts = ifc_file->instances_by_type_excl_subtypes(
"IfcGeometricRepresentationContext");
template <typename Schema>
std::optional<Eigen::Matrix4d> get_wcs_s(ifcopenshell::file* ifc_file) {
const auto contexts =
ifc_file->template instances_by_type_excl_subtypes<typename Schema::IfcGeometricRepresentationContext>();
express::Base wcs;
bool found = false;
for (const auto& ctx : contexts) {
auto entity = ctx.as<express::Entity>();
auto wcs_attr = entity.get("WorldCoordinateSystem");
if (wcs_attr.isNull()) continue;
wcs = (express::Base) wcs_attr;
found = true;
auto ctype_attr = entity.get("ContextType");
if (!ctype_attr.isNull()) {
std::string ctype = ctype_attr;
if (ctype == "Model") break;
for (const auto& context : contexts) {
const auto placement = context.WorldCoordinateSystem();
if (!placement) {
continue;
}
wcs = placement.concrete();
if (context.ContextType() == std::optional<std::string>("Model")) {
break;
}
}
if (!found) return std::nullopt;
const auto& decl = wcs.declaration();
if (!(decl.is("IfcAxis2Placement3D") || decl.is("IfcAxis2PlacementLinear"))) {
if (!wcs) {
return std::nullopt;
}
return get_axis2_placement(wcs);
}
template <typename Schema>
std::optional<express::Base> get_map_unit_s(ifcopenshell::file* ifc_file) {
if constexpr (!is_ifc4_or_higher<Schema>::value) {
return std::nullopt;
} else {
const auto operations =
ifc_file->template instances_by_type<typename Schema::IfcCoordinateOperation>();
if (operations.empty()) {
return std::nullopt;
}
const auto target = operations.front().TargetCRS();
const auto projected = target.template as<typename Schema::IfcProjectedCRS>();
if (!projected) {
return std::nullopt;
}
const auto unit = projected.MapUnit();
if (!unit) {
return std::nullopt;
}
return unit;
}
}
} // namespace
std::optional<HelmertTransformation>
get_helmert_transformation_parameters(ifcopenshell::file* ifc_file) {
const auto name = ifc_file->schema()->name();
#define IFCOPENSHELL_DISPATCH(Schema, Identifier) \
if (name == Identifier) \
return get_helmert_transformation_parameters_s<Schema>(ifc_file);
IFCOPENSHELL_HELPER_FOR_EACH_SCHEMA(IFCOPENSHELL_DISPATCH)
#undef IFCOPENSHELL_DISPATCH
unsupported_schema(name);
}
std::optional<Eigen::Matrix4d> get_wcs(ifcopenshell::file* ifc_file) {
const auto name = ifc_file->schema()->name();
#define IFCOPENSHELL_DISPATCH(Schema, Identifier) \
if (name == Identifier) \
return get_wcs_s<Schema>(ifc_file);
IFCOPENSHELL_HELPER_FOR_EACH_SCHEMA(IFCOPENSHELL_DISPATCH)
#undef IFCOPENSHELL_DISPATCH
unsupported_schema(name);
}
Eigen::Matrix4d local_to_global(const Eigen::Matrix4d& matrix,
const HelmertTransformation& p) {
const double theta = std::atan2(p.xao, p.xaa);
@@ -190,8 +259,10 @@ Eigen::Matrix4d local_to_global(const Eigen::Matrix4d& matrix,
S(2, 2) = p.scale * p.factor_z;
Eigen::Matrix4d R = Eigen::Matrix4d::Identity();
R(0, 0) = c; R(0, 1) = -s;
R(1, 0) = s; R(1, 1) = c;
R(0, 0) = c;
R(0, 1) = -s;
R(1, 0) = s;
R(1, 1) = c;
Eigen::Matrix4d result = R * S * matrix;
// The scale was baked into the rotation+scale matrix so each axis column
@@ -200,7 +271,9 @@ Eigen::Matrix4d local_to_global(const Eigen::Matrix4d& matrix,
for (int col = 0; col < 3; ++col) {
Eigen::Vector3d v = result.block<3, 1>(0, col);
const double n = v.norm();
if (n > 0.0) result.block<3, 1>(0, col) = v / n;
if (n > 0.0) {
result.block<3, 1>(0, col) = v / n;
}
}
result(0, 3) += p.e;
result(1, 3) += p.n;
@@ -212,7 +285,9 @@ Eigen::Matrix4d auto_local_to_global(ifcopenshell::file* ifc_file,
const Eigen::Matrix4d& matrix,
bool should_return_in_map_units) {
auto params = get_helmert_transformation_parameters(ifc_file);
if (!params) return matrix;
if (!params) {
return matrix;
}
Eigen::Matrix4d m = matrix;
if (auto wcs = get_wcs(ifc_file)) {
@@ -238,9 +313,15 @@ Eigen::Matrix4d helmert_meters_from_parameters(const HelmertTransformation& p,
// they apply to placement translations on compose; this is the behaviour
// IfcMapConversionScaled actually wants ("grid distance ≠ ground
// distance" — buildings on the grid should appear scaled by f).
M(0, 0) = c * p.factor_x; M(0, 1) = -s * p.factor_y; M(0, 2) = 0.0;
M(1, 0) = s * p.factor_x; M(1, 1) = c * p.factor_y; M(1, 2) = 0.0;
M(2, 0) = 0.0; M(2, 1) = 0.0; M(2, 2) = p.factor_z;
M(0, 0) = c * p.factor_x;
M(0, 1) = -s * p.factor_y;
M(0, 2) = 0.0;
M(1, 0) = s * p.factor_x;
M(1, 1) = c * p.factor_y;
M(1, 2) = 0.0;
M(2, 0) = 0.0;
M(2, 1) = 0.0;
M(2, 2) = p.factor_z;
M(0, 3) = p.e * map_unit_to_meters;
M(1, 3) = p.n * map_unit_to_meters;
M(2, 3) = p.h * map_unit_to_meters;
@@ -248,20 +329,13 @@ Eigen::Matrix4d helmert_meters_from_parameters(const HelmertTransformation& p,
}
std::optional<express::Base> get_map_unit(ifcopenshell::file* ifc_file) {
std::vector<express::Base> coordops;
try {
coordops = ifc_file->instances_by_type("IfcCoordinateOperation");
} catch (...) {
return std::nullopt;
}
if (coordops.empty()) return std::nullopt;
auto target_attr = coordops[0].as<express::Entity>().get("TargetCRS");
if (target_attr.isNull()) return std::nullopt;
express::Base target = target_attr;
if (!target.declaration().is("IfcProjectedCRS")) return std::nullopt;
auto mu_attr = target.as<express::Entity>().get("MapUnit");
if (mu_attr.isNull()) return std::nullopt;
return (express::Base) mu_attr;
const auto name = ifc_file->schema()->name();
#define IFCOPENSHELL_DISPATCH(Schema, Identifier) \
if (name == Identifier) \
return get_map_unit_s<Schema>(ifc_file);
IFCOPENSHELL_HELPER_FOR_EACH_SCHEMA(IFCOPENSHELL_DISPATCH)
#undef IFCOPENSHELL_DISPATCH
unsupported_schema(name);
}
double x_axis_to_angle_deg(double xaa, double xao) {