Files
IfcOpenShell/src/ifcgeom/kernels/opencascade/boolean_result.cpp
T
Stephen Boddy 96e2efebc8 Route boolean-op kernel logging through the injected Logger
src/ifcgeom/kernels/opencascade/boolean_utils.cpp, OpenCascadeKernel.cpp,
and boolean_result.cpp logged diagnostics (including the "Processed
fully in 2D" family of messages) through the global Logger::Root()
singleton. IfcConvert's main() constructs its own Logger and wires it
to --log-file via SetOutput(), then threads that instance through
Converter/kernel constructors as logger_ (see AbstractKernel). Since
Logger::Root() is never itself configured with an output stream, every
Notice/Warning/Message call through it was silently dropped instead of
reaching the log file - Logger::Message's log1_/log2_ null checks just
no-op.

This made src/ifcopenshell-python/test/test_wall_opening.py fail: it
asserts on specific log messages that the underlying boolean-op code
was still emitting correctly, just to nowhere. The geometry itself was
never wrong.

Add a Logger*, defaulting to null, to boolean_settings (with a log()
accessor falling back to Logger::Root() for the few remaining
call sites with no injected logger available), thread it through
eliminate_narrow_operands and boolean_subtraction_2d_using_builder,
and have OpenCascadeKernel/boolean_result.cpp populate it from their
inherited logger_ member instead of relying on the global singleton.

Generated with the assistance of an AI coding tool.
2026-07-18 15:03:32 +01:00

203 lines
5.7 KiB
C++

#include "OpenCascadeKernel.h"
#include "boolean_utils.h"
#include "base_utils.h"
using namespace IfcGeom;
using namespace ifcopenshell::geometry;
using namespace ifcopenshell::geometry::kernels;
// @todo should we reapply the technique to apply openings in batches?
namespace {
struct opening_sorter {
bool operator()(const std::pair<double, TopoDS_Shape>& a, const std::pair<double, TopoDS_Shape>& b) const {
return a.first > b.first;
}
};
bool apply_in_batches(IfcGeom::util::boolean_settings bst, const TopoDS_Shape& first_operand, std::vector< std::pair<double, TopoDS_Shape> >& opening_vector, BOPAlgo_Operation occ_op, TopoDS_Shape& result) {
auto it = opening_vector.begin();
auto jt = it;
result = first_operand;
for (;; ++it) {
if (it == opening_vector.end() || jt->first / it->first > 10.) {
NCollection_List<TopoDS_Shape> opening_list;
for (auto kt = jt; kt < it; ++kt) {
opening_list.Append(kt->second);
}
TopoDS_Shape intermediate_result;
if (IfcGeom::util::boolean_operation(bst, result, opening_list, occ_op, intermediate_result)) {
result = intermediate_result;
} else {
return false;
}
jt = it;
}
if (it == opening_vector.end()) {
break;
}
}
return true;
}
}
namespace {
BOPAlgo_Operation op_to_occt(taxonomy::boolean_result::operation_t t) {
switch (t) {
case taxonomy::boolean_result::UNION: return BOPAlgo_FUSE;
case taxonomy::boolean_result::INTERSECTION: return BOPAlgo_COMMON;
case taxonomy::boolean_result::SUBTRACTION: return BOPAlgo_CUT;
}
}
bool get_single_child(const TopoDS_Shape& s, TopoDS_Shape& child) {
TopoDS_Iterator it(s);
if (!it.More()) {
return false;
}
child = it.Value();
it.Next();
return !it.More();
}
bool is_unbounded_halfspace(const TopoDS_Shape& solid) {
if (solid.ShapeType() != TopAbs_SOLID) {
return false;
}
TopoDS_Shape shell;
if (!get_single_child(solid, shell)) {
return false;
}
TopoDS_Shape face;
if (!get_single_child(shell, face)) {
return false;
}
TopoDS_Iterator it(face);
return !it.More();
}
}
bool OpenCascadeKernel::convert_impl(const taxonomy::boolean_result::ptr br, ConversionResults& results) {
return handle_occt_exception([&]() -> bool {
bool valid_result = false;
bool first = true;
const double tol = settings_.get<settings::Precision>().get();
TopoDS_Shape a;
NCollection_List<TopoDS_Shape> b;
taxonomy::style::ptr first_item_style;
for (auto& c : br->children) {
IfcGeom::ConversionResults cr;
AbstractKernel::convert(c, cr);
if (first && br->operation == taxonomy::boolean_result::SUBTRACTION) {
// @todo A will be null on union/intersection, intended?
IfcGeom::util::flatten_shape_list(cr, a, false, true, settings_.get<settings::Precision>().get());
first_item_style = c->surface_style;
if (!first_item_style && c->kind() == taxonomy::COLLECTION) {
// @todo recursively right?
first_item_style = taxonomy::cast<taxonomy::geom_item>(taxonomy::cast<taxonomy::collection>(c)->children[0])->surface_style;
}
if (settings_.get<settings::DisableBooleanResult>().get()) {
results.emplace_back(IfcGeom::ConversionResult(
br->instance->as<IfcUtil::IfcBaseEntity>()->id(),
br->matrix,
new OpenCascadeShape(a),
br->surface_style ? br->surface_style : first_item_style
));
return true;
}
const double first_operand_volume = util::shape_volume(a);
if (first_operand_volume <= ALMOST_ZERO) {
logger_.Message(Logger::LOG_WARNING, "GEO", 119, "Empty solid for:", c->instance);
}
} else {
for (auto& r : cr) {
auto S = std::static_pointer_cast<OpenCascadeShape>(r.Shape())->shape();
if (S.IsNull()) {
logger_.Error("GEO", 120, "Null operand");
continue;
}
gp_GTrsf trsf;
convert(r.Placement(), trsf);
// @todo it really confuses me why I cannot use Moved() here instead
S.Location(S.Location() * trsf.Trsf());
if (is_unbounded_halfspace(S)) {
double d;
TopoDS_Shape result;
util::fit_halfspace(a, S, result, d, tol * 1e3);
// #2665 we also set a precision-independent threshold, because in the boolean op routine
// the working fuzziness might still be increased.
if (d < tol * 20. || d < 0.00002) {
logger_.Message(Logger::LOG_WARNING, "GEO", 121, "Halfspace subtraction yields unchanged volume:", c->instance);
continue;
} else {
S = result;
}
} else {
S = util::ensure_fit_for_subtraction(S, tol);
}
b.Append(S);
}
}
first = false;
}
util::boolean_settings bst;
bst.attempt_2d = settings_.get<settings::BooleanAttempt2d>().get();
bst.debug = settings_.get<settings::DebugBooleanOperations>().get();
bst.precision = settings_.get<settings::Precision>().get();
bst.logger = &logger_;
TopoDS_Shape r;
if (br->operation == taxonomy::boolean_result::SUBTRACTION && !a.IsNull() && a.ShapeType() == TopAbs_COMPOUND && TopoDS_Iterator(a).More() && util::is_nested_compound_of_solid(a)) {
TopoDS_Compound C;
BRep_Builder B;
B.MakeCompound(C);
TopoDS_Iterator it(a);
valid_result = true;
for (; it.More(); it.Next()) {
TopoDS_Shape part;
if (util::boolean_operation(bst, it.Value(), b, op_to_occt(br->operation), part)) {
B.Add(C, part);
} else {
valid_result = false;
}
}
r = C;
} else {
if (br->operation != taxonomy::boolean_result::SUBTRACTION && a.IsNull()) {
a = b.First();
b.RemoveFirst();
}
valid_result = util::boolean_operation(bst, a, b, op_to_occt(br->operation), r);
}
if (valid_result) {
std::swap(r, a);
}
results.emplace_back(IfcGeom::ConversionResult(
br->instance->as<IfcUtil::IfcBaseEntity>()->id(),
br->matrix,
new OpenCascadeShape(a),
br->surface_style ? br->surface_style : first_item_style
));
return true;
});
}