#3660 setSegmentProjection() / --svg-segment-projection options

This commit is contained in:
Thomas Krijnen
2023-08-30 10:57:33 +02:00
parent 26e292dd34
commit fbd8ea1edb
3 changed files with 124 additions and 73 deletions
+3
View File
@@ -416,6 +416,8 @@ int main(int argc, char** argv) {
"Uses the polygonal algorithm for hidden line rendering") "Uses the polygonal algorithm for hidden line rendering")
("svg-prefilter", ("svg-prefilter",
"Prefilter faces and shapes before feeding to HLR algorithm") "Prefilter faces and shapes before feeding to HLR algorithm")
("svg-segment-projection",
"Segment result of projection wrt original products")
("svg-write-poly", ("svg-write-poly",
"Approximate every curve as polygonal in SVG output") "Approximate every curve as polygonal in SVG output")
("svg-project", ("svg-project",
@@ -1085,6 +1087,7 @@ int main(int argc, char** argv) {
static_cast<SvgSerializer*>(serializer.get())->setUseNamespace(vmap.count("svg-xmlns") > 0); static_cast<SvgSerializer*>(serializer.get())->setUseNamespace(vmap.count("svg-xmlns") > 0);
static_cast<SvgSerializer*>(serializer.get())->setUseHlrPoly(vmap.count("svg-poly") > 0); static_cast<SvgSerializer*>(serializer.get())->setUseHlrPoly(vmap.count("svg-poly") > 0);
static_cast<SvgSerializer*>(serializer.get())->setUsePrefiltering(vmap.count("svg-prefilter") > 0); static_cast<SvgSerializer*>(serializer.get())->setUsePrefiltering(vmap.count("svg-prefilter") > 0);
static_cast<SvgSerializer*>(serializer.get())->setSegmentProjection(vmap.count("svg-segment-projection") > 0);
static_cast<SvgSerializer*>(serializer.get())->setPolygonal(vmap.count("svg-write-poly") > 0); static_cast<SvgSerializer*>(serializer.get())->setPolygonal(vmap.count("svg-write-poly") > 0);
static_cast<SvgSerializer*>(serializer.get())->setAlwaysProject(vmap.count("svg-project") > 0); static_cast<SvgSerializer*>(serializer.get())->setAlwaysProject(vmap.count("svg-project") > 0);
static_cast<SvgSerializer*>(serializer.get())->setWithoutStoreys(vmap.count("svg-without-storeys") > 0); static_cast<SvgSerializer*>(serializer.get())->setWithoutStoreys(vmap.count("svg-without-storeys") > 0);
+65 -53
View File
@@ -1149,14 +1149,14 @@ void SvgSerializer::write(const geometry_data& data) {
if (storey) { if (storey) {
auto it = storey_hlr.find(storey); auto it = storey_hlr.find(storey);
if (it == storey_hlr.end()) { if (it == storey_hlr.end()) {
it = storey_hlr.insert({ storey, hlr_t(use_prefiltering_, use_hlr_poly_, projection_plane) }).first; it = storey_hlr.insert({ storey, hlr_t(use_prefiltering_, use_hlr_poly_, segment_projection_, projection_plane) }).first;
} }
it->second.add(*compound_to_hlr); it->second.add(*compound_to_hlr, data.product);
} else { } else {
Logger::Warning("Unable to invoke HLR due to absence of storey containment", data.product); Logger::Warning("Unable to invoke HLR due to absence of storey containment", data.product);
} }
} else if (hlr) { } else if (hlr) {
hlr->add(*compound_to_hlr); hlr->add(*compound_to_hlr, data.product);
} }
} }
} }
@@ -1699,55 +1699,66 @@ std::array<std::array<double, 3>, 3> SvgSerializer::resize() {
} }
void SvgSerializer::draw_hlr(const gp_Pln& pln, const drawing_key& drawing_name) { void SvgSerializer::draw_hlr(const gp_Pln& pln, const drawing_key& drawing_name) {
TopoDS_Shape hlr_compound_unmirrored = (drawing_name.first ? this->storey_hlr.find(drawing_name.first)->second : *hlr).build(); auto hlr_items = (drawing_name.first ? this->storey_hlr.find(drawing_name.first)->second : *hlr).build();
if (!hlr_compound_unmirrored.IsNull()) { for (auto& p : hlr_items) {
// Compound 3D curves for mirroring to work const TopoDS_Shape& hlr_compound_unmirrored = p.second;
ShapeFix_Edge sfe;
TopExp_Explorer exp(hlr_compound_unmirrored, TopAbs_EDGE);
for (; exp.More(); exp.Next()) {
sfe.FixAddCurve3d(TopoDS::Edge(exp.Current()));
}
// Mirror to match SVG coord system. if (!hlr_compound_unmirrored.IsNull()) {
// @todo this is very wasteful. We better do the Y-mirror in the SVG writing and // Compound 3D curves for mirroring to work
// not on the TopoDS_Shape input. ShapeFix_Edge sfe;
TopExp_Explorer exp(hlr_compound_unmirrored, TopAbs_EDGE);
TopoDS_Shape hlr_compound; for (; exp.More(); exp.Next()) {
if (drawing_name.first == nullptr) { sfe.FixAddCurve3d(TopoDS::Edge(exp.Current()));
gp_Trsf trsf_mirror;
if (!mirror_y_) {
trsf_mirror.SetMirror(gp_Ax2(gp::Origin(), gp::DY()));
} }
if (mirror_x_) {
gp_Trsf mirror_x; // Mirror to match SVG coord system.
mirror_x.SetMirror(gp_Ax2(gp::Origin(), gp::DX())); // @todo this is very wasteful. We better do the Y-mirror in the SVG writing and
trsf_mirror.PreMultiply(mirror_x); // not on the TopoDS_Shape input.
TopoDS_Shape hlr_compound;
if (drawing_name.first == nullptr) {
gp_Trsf trsf_mirror;
if (!mirror_y_) {
trsf_mirror.SetMirror(gp_Ax2(gp::Origin(), gp::DY()));
}
if (mirror_x_) {
gp_Trsf mirror_x;
mirror_x.SetMirror(gp_Ax2(gp::Origin(), gp::DX()));
trsf_mirror.PreMultiply(mirror_x);
}
BRepBuilderAPI_Transform make_transform_mirror(hlr_compound_unmirrored, trsf_mirror, true);
make_transform_mirror.Build();
hlr_compound = make_transform_mirror.Shape();
} else {
// In case of building storey-based floor plan the mirroring has already
// been taken into account before projection.
hlr_compound = hlr_compound_unmirrored;
} }
BRepBuilderAPI_Transform make_transform_mirror(hlr_compound_unmirrored, trsf_mirror, true);
make_transform_mirror.Build();
hlr_compound = make_transform_mirror.Shape();
} else {
// In case of building storey-based floor plan the mirroring has already
// been taken into account before projection.
hlr_compound = hlr_compound_unmirrored;
}
exp.Init(hlr_compound, TopAbs_EDGE); exp.Init(hlr_compound, TopAbs_EDGE);
BRep_Builder B; BRep_Builder B;
path_object* po; path_object* po;
if (drawing_name.first) { std::string name;
po = &start_path(pln, drawing_name.first, "class=\"projection\""); if (p.first) {
} else { name = nameElement(p.first);
po = &start_path(pln, drawing_name.second, "class=\"projection\""); boost::replace_all(name, "class=\"", "class=\"projection ");
} } else {
for (; exp.More(); exp.Next()) { name = "class=\"projection\"";
TopoDS_Wire w; }
B.MakeWire(w); if (drawing_name.first) {
B.Add(w, exp.Current()); po = &start_path(pln, drawing_name.first, name);
write(*po, w); } else {
} po = &start_path(pln, drawing_name.second, name);
}
for (; exp.More(); exp.Next()) {
TopoDS_Wire w;
B.MakeWire(w);
B.Add(w, exp.Current());
write(*po, w);
}
}
} }
} }
@@ -1812,18 +1823,19 @@ void SvgSerializer::addTextAnnotations(const drawing_key& k) {
v.Transform(trsf_view); v.Transform(trsf_view);
auto svg_name = nameElement(ann); auto svg_name = nameElement(ann);
path_object* po;
if (k.first) {
po = &start_path(meta.pln_3d, k.first, svg_name);
} else {
po = &start_path(meta.pln_3d, k.second, svg_name);
}
if (object_type.size()) { if (object_type.size()) {
// postfix the object_type for CSS matching // postfix the object_type for CSS matching
boost::replace_all(svg_name, "class=\"IfcAnnotation\"", "class=\"IfcAnnotation " + object_type + "\""); boost::replace_all(svg_name, "class=\"IfcAnnotation\"", "class=\"IfcAnnotation " + object_type + "\"");
} }
path_object* po;
if (k.first) {
po = &start_path(meta.pln_3d, k.first, svg_name);
} else {
po = &start_path(meta.pln_3d, k.second, svg_name);
}
boost::optional<double> font_size; boost::optional<double> font_size;
std::vector<std::string> tokens; std::vector<std::string> tokens;
boost::split(tokens, name, boost::is_any_of("_")); boost::split(tokens, name, boost::is_any_of("_"));
@@ -1987,7 +1999,7 @@ void SvgSerializer::finalize() {
// @todo do we have always have pln here? // @todo do we have always have pln here?
if (use_hlr && pln) { if (use_hlr && pln) {
hlr = new hlr_t(use_prefiltering_, use_hlr_poly_, *pln); hlr = new hlr_t(use_prefiltering_, use_hlr_poly_, segment_projection_, *pln);
} }
section_data_ = std::vector<section_data>{ sd }; section_data_ = std::vector<section_data>{ sd };
+56 -20
View File
@@ -205,31 +205,52 @@ namespace {
class hlr_calc { class hlr_calc {
private: private:
const HLRAlgo_Projector& projector_; const HLRAlgo_Projector& projector_;
const std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>>* product_shapes_ = nullptr;
public: public:
typedef TopoDS_Shape result_type; typedef std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>> result_type;
hlr_calc(const HLRAlgo_Projector& projector) : projector_(projector) hlr_calc(const HLRAlgo_Projector& projector) : projector_(projector)
{} {}
TopoDS_Shape operator()(boost::blank&) const { void set_product_shape(const std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>>* product_shapes) {
product_shapes_ = product_shapes;
}
result_type operator()(boost::blank&) const {
throw std::runtime_error(""); throw std::runtime_error("");
} }
TopoDS_Shape operator()(opencascade::handle<HLRBRep_Algo>& algo) { result_type operator()(opencascade::handle<HLRBRep_Algo>& algo) {
algo->Projector(projector_); algo->Projector(projector_);
algo->Update(); algo->Update();
algo->Hide(); algo->Hide();
HLRBRep_HLRToShape hlr_shapes(algo); HLRBRep_HLRToShape hlr_shapes(algo);
return occt_join(hlr_shapes.OutLineVCompound(), hlr_shapes.VCompound()); if (product_shapes_) {
std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>> r;
for (auto& p : *product_shapes_) {
r.push_back({ p.first, occt_join(hlr_shapes.OutLineVCompound(p.second), hlr_shapes.VCompound(p.second)) });
}
return r;
} else {
return { {nullptr, occt_join(hlr_shapes.OutLineVCompound(), hlr_shapes.VCompound())}};
}
} }
TopoDS_Shape operator()(opencascade::handle<HLRBRep_PolyAlgo>& algo) { result_type operator()(opencascade::handle<HLRBRep_PolyAlgo>& algo) {
algo->Projector(projector_); algo->Projector(projector_);
algo->Update(); algo->Update();
HLRBRep_PolyHLRToShape hlr_shapes; HLRBRep_PolyHLRToShape hlr_shapes;
hlr_shapes.Update(algo); hlr_shapes.Update(algo);
return occt_join(hlr_shapes.OutLineVCompound(), hlr_shapes.VCompound()); if (product_shapes_) {
std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>> r;
for (auto& p : *product_shapes_) {
r.push_back({ p.first, occt_join(hlr_shapes.OutLineVCompound(p.second), hlr_shapes.VCompound(p.second)) });
}
return r;
} else {
return { {nullptr, occt_join(hlr_shapes.OutLineVCompound(), hlr_shapes.VCompound()) } };
}
} }
}; };
@@ -240,13 +261,13 @@ namespace {
gp_XYZ dxyz, xdir, ydir; gp_XYZ dxyz, xdir, ydir;
public: public:
std::list<TopoDS_Shape>::const_iterator item; TopoDS_Shape* item;
TopoDS_Face face; TopoDS_Face face;
bool is_convex; bool is_convex;
// @note copying the BRepTopAdaptor_FClass2d didn't work so it's a pointer // @note copying the BRepTopAdaptor_FClass2d didn't work so it's a pointer
BRepTopAdaptor_FClass2d* fclass; BRepTopAdaptor_FClass2d* fclass;
face_info(std::list<TopoDS_Shape>::const_iterator it, const TopoDS_Face& fa) face_info(TopoDS_Shape* it, const TopoDS_Face& fa)
: item(it) : item(it)
, face(fa) , face(fa)
, fclass(nullptr) , fclass(nullptr)
@@ -334,17 +355,19 @@ namespace {
hlr_brep_or_poly_t engine_; hlr_brep_or_poly_t engine_;
bool use_prefiltering_; bool use_prefiltering_;
bool use_hlr_poly_; bool use_hlr_poly_;
bool segment_projection_;
gp_Ax1 view_direction_; gp_Ax1 view_direction_;
HLRAlgo_Projector projector_; HLRAlgo_Projector projector_;
std::multimap<double, face_info> large_ortho_faces_; std::multimap<double, face_info> large_ortho_faces_;
std::list<TopoDS_Shape> items_; std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>> items_;
public: public:
prefiltered_hlr(bool use_prefiltering, bool use_hlr_poly, const gp_Pln& view_direction) prefiltered_hlr(bool use_prefiltering, bool use_hlr_poly, bool segment_projection, const gp_Pln& view_direction)
: use_prefiltering_(use_prefiltering) : use_prefiltering_(use_prefiltering)
, use_hlr_poly_(use_hlr_poly) , use_hlr_poly_(use_hlr_poly)
, segment_projection_(segment_projection)
// @nb negative z in accordance with occt projector convention (and opengl) // @nb negative z in accordance with occt projector convention (and opengl)
, view_direction_(view_direction.Axis()) , view_direction_(view_direction.Axis())
{ {
@@ -359,7 +382,7 @@ namespace {
projector_ = HLRAlgo_Projector(trsf, false, 1.); projector_ = HLRAlgo_Projector(trsf, false, 1.);
} }
bool is_obscured_(std::list<TopoDS_Shape>::const_iterator sit) { bool is_obscured_(TopoDS_Shape* sit) {
const TopoDS_Shape& s = *sit; const TopoDS_Shape& s = *sit;
double min_d = std::numeric_limits<double>::infinity(); double min_d = std::numeric_limits<double>::infinity();
@@ -393,9 +416,9 @@ namespace {
return false; return false;
} }
void add(const TopoDS_Shape& s) { void add(const TopoDS_Shape& s, const IfcUtil::IfcBaseEntity* product) {
if (!use_prefiltering_) { if (!use_prefiltering_) {
items_.insert(items_.end(), s); items_.insert(items_.end(), {product, s});
return; return;
} }
@@ -434,7 +457,7 @@ namespace {
Logger::Notice("Included " + std::to_string(n_faces_included) + " faces out of " + std::to_string(n_total) + " after prefiltering"); Logger::Notice("Included " + std::to_string(n_faces_included) + " faces out of " + std::to_string(n_total) + " after prefiltering");
auto it = items_.insert(items_.end(), C); auto it = items_.insert(items_.end(), { product, C });
{ {
TopExp_Explorer exp(C, TopAbs_FACE); TopExp_Explorer exp(C, TopAbs_FACE);
@@ -458,7 +481,7 @@ namespace {
auto d = -(pnt.XYZ() - view_direction_.Location().XYZ()).Dot(view_direction_.Direction().XYZ()); auto d = -(pnt.XYZ() - view_direction_.Location().XYZ()).Dot(view_direction_.Direction().XYZ());
if (d > 1.e-5) { if (d > 1.e-5) {
large_ortho_faces_.insert({ d, face_info(it, face) }); large_ortho_faces_.insert({ d, face_info(&it->second, face) });
} }
} }
} }
@@ -467,15 +490,15 @@ namespace {
} }
} }
} else { } else {
items_.insert(items_.end(), s); items_.insert(items_.end(), { product, s });
} }
} }
TopoDS_Shape build() { std::list<std::pair<const IfcUtil::IfcBaseEntity*, TopoDS_Shape>> build() {
size_t n_included = 0; size_t n_included = 0;
for (auto it = items_.begin(); it != items_.end(); ++it) { for (auto it = items_.begin(); it != items_.end(); ++it) {
if (!use_prefiltering_ || !is_obscured_(it)) { if (!use_prefiltering_ || !is_obscured_(&it->second)) {
hlr_writer vis(*it); hlr_writer vis(it->second);
boost::apply_visitor(vis, engine_); boost::apply_visitor(vis, engine_);
n_included++; n_included++;
} }
@@ -483,7 +506,11 @@ namespace {
if (use_prefiltering_) { if (use_prefiltering_) {
Logger::Notice("Included " + std::to_string(n_included) + " elements out of " + std::to_string(items_.size()) + " after prefiltering"); Logger::Notice("Included " + std::to_string(n_included) + " elements out of " + std::to_string(items_.size()) + " after prefiltering");
} }
hlr_calc vis(projector_); hlr_calc vis(projector_);
if (true) {
vis.set_product_shape(&items_);
}
return boost::apply_visitor(vis, engine_); return boost::apply_visitor(vis, engine_);
} }
}; };
@@ -517,7 +544,7 @@ protected:
storey_height_display_types storey_height_display_; storey_height_display_types storey_height_display_;
bool draw_door_arcs_, is_floor_plan_; bool draw_door_arcs_, is_floor_plan_;
bool auto_section_, auto_elevation_; bool auto_section_, auto_elevation_;
bool use_namespace_, use_hlr_poly_, use_prefiltering_, always_project_, polygonal_; bool use_namespace_, use_hlr_poly_, use_prefiltering_, segment_projection_, always_project_, polygonal_;
bool emit_building_storeys_; bool emit_building_storeys_;
bool no_css_; bool no_css_;
bool unify_inputs_; bool unify_inputs_;
@@ -570,6 +597,7 @@ public:
, use_namespace_(false) , use_namespace_(false)
, use_hlr_poly_(false) , use_hlr_poly_(false)
, use_prefiltering_(false) , use_prefiltering_(false)
, segment_projection_(false)
, always_project_(false) , always_project_(false)
, polygonal_(false) , polygonal_(false)
, emit_building_storeys_(true) , emit_building_storeys_(true)
@@ -657,6 +685,14 @@ public:
return use_prefiltering_; return use_prefiltering_;
} }
void setSegmentProjection(bool b) {
segment_projection_ = b;
}
bool getSegmentProjection() const {
return segment_projection_;
}
void setPolygonal(bool b) { void setPolygonal(bool b) {
polygonal_ = b; polygonal_ = b;
} }