Files
IfcOpenShell/src/ifcgeom/hybrid_kernel.h
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

275 lines
10 KiB
C++
Raw Normal View History

2025-09-26 14:24:49 +02:00
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef HYBRID_KERNEL_H
#define HYBRID_KERNEL_H
#include "AbstractKernel.h"
#ifdef IFOPSH_WITH_OPENCASCADE
#include "../ifcgeom/kernels/opencascade/OpenCascadeKernel.h"
#undef Handle
#endif
#ifdef IFOPSH_WITH_CGAL
#include "../ifcgeom/kernels/cgal/CgalKernel.h"
#undef CGAL_KERNEL_H
#undef CGALCONVERSIONRESULT_H
#define IFOPSH_SIMPLE_KERNEL
#include "../ifcgeom/kernels/cgal/CgalKernel.h"
#undef CgalKernel
#endif
#ifdef IFOPSH_WITH_MANIFOLD
#include "../ifcgeom/kernels/manifold/ManifoldKernel.h"
#endif
2026-04-09 17:17:53 +02:00
#include "../ifcgeom/kernels/passthrough/PassthroughKernel.h"
2025-09-26 14:24:49 +02:00
namespace {
inline bool is_valid_for_kernel(const ifcopenshell::geometry::kernels::AbstractKernel* k, const IfcGeom::ConversionResult& shp) {
#ifdef IFOPSH_WITH_OPENCASCADE
if (k->geometry_library() == "opencascade") {
return dynamic_cast<ifcopenshell::geometry::OpenCascadeShape*>(shp.Shape().get()) != nullptr;
}
#endif
#ifdef IFOPSH_WITH_CGAL
if (k->geometry_library() == "cgal-simple") {
return dynamic_cast<ifcopenshell::geometry::SimpleCgalShape*>(shp.Shape().get()) != nullptr;
}
if (k->geometry_library() == "cgal") {
return dynamic_cast<ifcopenshell::geometry::CgalShape*>(shp.Shape().get()) != nullptr;
}
#endif
#ifdef IFOPSH_WITH_MANIFOLD
if (k->geometry_library() == "manifold") {
return dynamic_cast<ifcopenshell::geometry::ManifoldShape*>(shp.Shape().get()) != nullptr;
}
2025-09-26 14:24:49 +02:00
#endif
2026-04-09 17:17:53 +02:00
if (k->geometry_library() == "passthrough") {
return dynamic_cast<ifcopenshell::geometry::PassthroughShape*>(shp.Shape().get()) != nullptr;
}
2025-09-26 14:24:49 +02:00
return false;
}
}
namespace ifcopenshell {
namespace geometry {
namespace kernels {
class HybridKernel : public ifcopenshell::geometry::kernels::AbstractKernel {
std::vector<std::unique_ptr<AbstractKernel>> kernels_;
ifcopenshell::geometry::abstract_mapping* mapping_;
2026-03-31 15:32:36 +02:00
ifcopenshell::file* file_;
2025-09-26 14:24:49 +02:00
public:
2026-03-31 15:32:36 +02:00
HybridKernel(const std::string& name, ifcopenshell::file* file, Settings& settings, std::vector<std::unique_ptr<AbstractKernel>>&& kernels)
2025-09-26 14:24:49 +02:00
: AbstractKernel(name, settings)
, kernels_(std::move(kernels))
, mapping_(ifcopenshell::geometry::impl::mapping_implementations().construct(file, settings))
, file_(file)
{
}
virtual bool supports_boolean_operations() const
{
for (auto& k : kernels_) {
if (k->supports_boolean_operations()) {
return true;
}
}
return false;
}
virtual bool convert(const taxonomy::ptr item, IfcGeom::ConversionResults& rs)
{
auto ops = mapping_->find_openings(item->instance);
bool has_openings = ops.size();
2025-09-26 14:24:49 +02:00
for (auto& k : kernels_) {
#ifdef IFOPSH_WITH_CGAL
if (has_openings && !k->supports_boolean_operations()) {
// @todo this would fail later on in the find_openings() call, because we have a
// SimpleCgalShape which cannot be used on a kernel that supports booleans.
// @todo 1 implement the translation between various conversion result shapes
// @todo 2 fold the boolean result openings into the taxonomy item. This should be possible
// now that we have shared_ptr<item> and caching in place. So the inability
// to instance wouldn't matter as much.
continue;
}
#endif
2026-04-09 17:17:53 +02:00
if (has_openings && k->geometry_library() == "passthrough") {
continue;
}
2025-09-26 14:24:49 +02:00
bool success = false;
try {
success = k->convert(item, rs);
} catch (...) {}
if (success) {
return true;
}
}
return false;
}
virtual bool apply_layerset(IfcGeom::ConversionResults& items, const ifcopenshell::geometry::layerset_information& layers)
{
for (auto& k : kernels_) {
bool success = false;
try {
success = k->apply_layerset(items, layers);
} catch (...) {}
if (success) {
return true;
}
}
return false;
}
virtual bool apply_folded_layerset(IfcGeom::ConversionResults& items, const ifcopenshell::geometry::layerset_information& layers, const std::map<express::Base, ifcopenshell::geometry::layerset_information>& folds)
2025-09-26 14:24:49 +02:00
{
for (auto& k : kernels_) {
bool success = false;
try {
success = k->apply_folded_layerset(items, layers, folds);
} catch (...) {}
if (success) {
return true;
}
}
return false;
}
virtual bool convert_openings(const express::Base& entity, const std::vector<std::pair<taxonomy::ptr, ifcopenshell::geometry::taxonomy::matrix4>>& openings,
2025-09-26 14:24:49 +02:00
const IfcGeom::ConversionResults& entity_shapes, const ifcopenshell::geometry::taxonomy::matrix4& entity_trsf, IfcGeom::ConversionResults& cut_shapes)
{
for (auto& k : kernels_) {
bool is_valid = true;
for (auto& s : entity_shapes) {
if (!is_valid_for_kernel(k.get(), s)) {
is_valid = false;
break;
}
}
if (!is_valid) {
continue;
}
bool success = false;
try {
success = k->convert_openings(entity, openings, entity_shapes, entity_trsf, cut_shapes);
} catch (...) {}
if (success) {
return true;
}
}
return false;
}
virtual AbstractKernel* clone() const
{
std::vector<std::unique_ptr<AbstractKernel>> ks;
for (auto& k : kernels_) {
ks.emplace_back(k->clone());
}
// @todo ugly
return new HybridKernel(geometry_library(), file_, const_cast<Settings&>(settings()), std::move(ks));
}
};
2026-03-31 15:32:36 +02:00
inline std::unique_ptr<AbstractKernel> construct(ifcopenshell::file* file, const std::string& geometry_library, Settings& conv_settings) {
2025-09-26 14:24:49 +02:00
std::string geometry_library_lower = boost::to_lower_copy(geometry_library);
#ifdef IFOPSH_WITH_OPENCASCADE
if (geometry_library_lower == "opencascade") {
return std::make_unique<IfcGeom::OpenCascadeKernel>(conv_settings);
}
#endif
#ifdef IFOPSH_WITH_CGAL
if (geometry_library_lower == "cgal") {
return std::make_unique<CgalKernel>(conv_settings);
}
if (geometry_library_lower == "cgal-simple") {
return std::make_unique<SimpleCgalKernel>(conv_settings);
}
#endif
#ifdef IFOPSH_WITH_MANIFOLD
if (geometry_library_lower == "manifold") {
return std::make_unique<ManifoldKernel>(conv_settings);
}
#endif
2026-04-09 17:17:53 +02:00
if (geometry_library_lower == "passthrough") {
return std::make_unique<PassthroughKernel>(conv_settings);
}
2025-09-26 14:24:49 +02:00
if (geometry_library_lower.rfind("hybrid-", 0) == 0) {
geometry_library_lower = geometry_library_lower.substr(strlen("hybrid"));
std::vector<std::unique_ptr<AbstractKernel>> kernels;
while (!geometry_library_lower.empty()) {
if (geometry_library_lower.find("-", 0) == 0) {
geometry_library_lower = geometry_library_lower.substr(strlen("-"));
} else {
2026-03-31 15:32:36 +02:00
throw ifcopenshell::exception("Invalid hybrid kernel " + geometry_library);
2025-09-26 14:24:49 +02:00
}
auto n = kernels.size();
#ifdef IFOPSH_WITH_OPENCASCADE
if (geometry_library_lower.find("opencascade", 0) == 0) {
kernels.emplace_back(new IfcGeom::OpenCascadeKernel(conv_settings));
geometry_library_lower = geometry_library_lower.substr(strlen("opencascade"));
}
#endif
#ifdef IFOPSH_WITH_CGAL
if (geometry_library_lower.find("cgal-simple", 0) == 0) {
kernels.emplace_back(new SimpleCgalKernel(conv_settings));
geometry_library_lower = geometry_library_lower.substr(strlen("cgal-simple"));
}
if (geometry_library_lower.find("cgal", 0) == 0) {
kernels.emplace_back(new CgalKernel(conv_settings));
geometry_library_lower = geometry_library_lower.substr(strlen("cgal"));
}
#endif
#ifdef IFOPSH_WITH_MANIFOLD
if (geometry_library_lower.find("manifold", 0) == 0) {
kernels.emplace_back(new ManifoldKernel(conv_settings));
geometry_library_lower = geometry_library_lower.substr(strlen("manifold"));
}
2025-09-26 14:24:49 +02:00
#endif
2026-04-09 17:17:53 +02:00
if (geometry_library_lower.find("passthrough", 0) == 0) {
kernels.emplace_back(new PassthroughKernel(conv_settings));
geometry_library_lower = geometry_library_lower.substr(strlen("passthrough"));
}
2025-09-26 14:24:49 +02:00
if (kernels.size() != n + 1) {
2026-03-31 15:32:36 +02:00
throw ifcopenshell::exception("Invalid hybrid kernel " + geometry_library);
2025-09-26 14:24:49 +02:00
}
}
for (auto it = kernels.begin(); it != kernels.end(); ++it) {
(**it).propagate_exceptions = it == kernels.begin();
(**it).partial_success_is_success = it == kernels.end() - 1;
}
if (!kernels.empty()) {
return std::make_unique<HybridKernel>(geometry_library, file, conv_settings, std::move(kernels));
}
}
2026-03-31 15:32:36 +02:00
throw ifcopenshell::exception("No geometry kernel registered for " + geometry_library);
2025-09-26 14:24:49 +02:00
}
}
}
}
#endif