mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 02:02:22 +00:00
Store actual subentities in CgalShape instead of the strange degeneracies convention
This commit is contained in:
@@ -30,6 +30,140 @@ typedef Polyhedron::Facet_const_handle Facet_const_handle;
|
||||
typedef Polyhedron::Halfedge_around_facet_const_circulator Halfedge_around_facet_circulator;
|
||||
|
||||
namespace {
|
||||
cgal_placement_t make_transform(const ifcopenshell::geometry::taxonomy::matrix4& place) {
|
||||
const auto& m = place.ccomponents();
|
||||
return cgal_placement_t(
|
||||
m(0, 0), m(0, 1), m(0, 2), m(0, 3),
|
||||
m(1, 0), m(1, 1), m(1, 2), m(1, 3),
|
||||
m(2, 0), m(2, 1), m(2, 2), m(2, 3));
|
||||
}
|
||||
|
||||
OpaqueCoordinate<3> opaque_point(const cgal_point_t& p) {
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(p.cartesian(0)),
|
||||
new NumberType(p.cartesian(1)),
|
||||
new NumberType(p.cartesian(2))
|
||||
);
|
||||
}
|
||||
|
||||
typename Kernel_::FT max_abs3(const typename Kernel_::FT& a, const typename Kernel_::FT& b, const typename Kernel_::FT& c) {
|
||||
std::array<typename Kernel_::FT, 3> abc{ a, b, c };
|
||||
auto minel = std::min_element(abc.begin(), abc.end());
|
||||
auto maxel = std::max_element(abc.begin(), abc.end());
|
||||
return ((-*minel) > *maxel) ? (-*minel) : *maxel;
|
||||
}
|
||||
|
||||
OpaqueCoordinate<3> opaque_axis(const cgal_vector_t& v) {
|
||||
auto maxval = max_abs3(v.x(), v.y(), v.z());
|
||||
if (maxval == 0) {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(v.x() / maxval),
|
||||
new NumberType(v.y() / maxval),
|
||||
new NumberType(v.z() / maxval)
|
||||
);
|
||||
}
|
||||
|
||||
OpaqueCoordinate<4> opaque_plane(const cgal_plane_t& p) {
|
||||
auto maxval = max_abs3(p.a(), p.b(), p.c());
|
||||
if (maxval == 0) {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
return OpaqueCoordinate<4>(
|
||||
new NumberType(p.a() / maxval),
|
||||
new NumberType(p.b() / maxval),
|
||||
new NumberType(p.c() / maxval),
|
||||
new NumberType(p.d() / maxval)
|
||||
);
|
||||
}
|
||||
|
||||
cgal_vector_t wire_normal(const cgal_wire_t& wire) {
|
||||
typename Kernel_::FT a(0), b(0), c(0);
|
||||
if (wire.size() < 3) {
|
||||
return cgal_vector_t(a, b, c);
|
||||
}
|
||||
for (std::size_t i = 0; i < wire.size(); ++i) {
|
||||
const auto& curr = wire[i];
|
||||
const auto& next = wire[(i + 1) % wire.size()];
|
||||
a += (curr.y() - next.y()) * (curr.z() + next.z());
|
||||
b += (curr.z() - next.z()) * (curr.x() + next.x());
|
||||
c += (curr.x() - next.x()) * (curr.y() + next.y());
|
||||
}
|
||||
return cgal_vector_t(a, b, c);
|
||||
}
|
||||
|
||||
cgal_point_t wire_centroid(const cgal_wire_t& wire) {
|
||||
if (wire.empty()) {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
std::array<Kernel_::FT, 3> p{ Kernel_::FT(0), Kernel_::FT(0), Kernel_::FT(0) };
|
||||
for (const auto& point : wire) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
p[i] += point.cartesian(i);
|
||||
}
|
||||
}
|
||||
Kernel_::FT n(wire.size());
|
||||
return cgal_point_t(p[0] / n, p[1] / n, p[2] / n);
|
||||
}
|
||||
|
||||
Kernel_::FT wire_length(const cgal_wire_t& wire) {
|
||||
Kernel_::FT len(0);
|
||||
if (wire.size() < 2) {
|
||||
return len;
|
||||
}
|
||||
for (std::size_t i = 1; i < wire.size(); ++i) {
|
||||
len += CGAL::approximate_sqrt(CGAL::Segment_3<Kernel_>(wire[i - 1], wire[i]).squared_length());
|
||||
}
|
||||
if (wire.size() > 2) {
|
||||
len += CGAL::approximate_sqrt(CGAL::Segment_3<Kernel_>(wire.back(), wire.front()).squared_length());
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
Kernel_::FT wire_area(const cgal_wire_t& wire) {
|
||||
Kernel_::FT area(0);
|
||||
if (wire.size() < 3) {
|
||||
return area;
|
||||
}
|
||||
const auto& origin = wire.front();
|
||||
for (std::size_t i = 1; i + 1 < wire.size(); ++i) {
|
||||
auto v1 = wire[i] - origin;
|
||||
auto v2 = wire[i + 1] - origin;
|
||||
area += CGAL::approximate_sqrt(CGAL::cross_product(v1, v2).squared_length()) / Kernel_::FT(2);
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
cgal_wire_t moved_wire(const cgal_wire_t& wire, const cgal_placement_t& trsf) {
|
||||
cgal_wire_t result;
|
||||
result.reserve(wire.size());
|
||||
for (const auto& point : wire) {
|
||||
result.push_back(point.transform(trsf));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void write_off_point(std::stringstream& sstream, const cgal_point_t& point) {
|
||||
sstream << "OFF\n1 0 0\n";
|
||||
sstream << point.x() << " " << point.y() << " " << point.z() << "\n";
|
||||
}
|
||||
|
||||
void write_off_wire(std::stringstream& sstream, const cgal_wire_t& wire) {
|
||||
const bool face = wire.size() >= 3;
|
||||
sstream << "OFF\n" << wire.size() << " " << (face ? 1 : 0) << " 0\n";
|
||||
for (const auto& point : wire) {
|
||||
sstream << point.x() << " " << point.y() << " " << point.z() << "\n";
|
||||
}
|
||||
if (face) {
|
||||
sstream << wire.size();
|
||||
for (std::size_t i = 0; i < wire.size(); ++i) {
|
||||
sstream << " " << i;
|
||||
}
|
||||
sstream << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Facet>
|
||||
CGAL::Direction_3<Kernel_> newell(Facet& face) {
|
||||
typename Kernel_::FT a(0), b(0), c(0);
|
||||
@@ -102,10 +236,11 @@ namespace {
|
||||
ifcopenshell::geometry::CgalShape::CgalShape(const cgal_shape_t& shape, bool convex, Logger& logger) {
|
||||
shape_ = shape;
|
||||
convex_tag_ = convex;
|
||||
auto& poly = std::get<cgal_shape_t>(*shape_);
|
||||
|
||||
std::set<cgal_shape_t::Facet_handle> faces_to_remove;
|
||||
|
||||
for (const auto& face : CGAL::faces(*shape_)) {
|
||||
for (const auto& face : CGAL::faces(poly)) {
|
||||
auto V = newell(*face).to_vector();
|
||||
CGAL::Plane_3<Kernel_> plane(CGAL::Point_3<Kernel_>(), V);
|
||||
auto b1 = plane.base1();
|
||||
@@ -127,7 +262,7 @@ ifcopenshell::geometry::CgalShape::CgalShape(const cgal_shape_t& shape, bool con
|
||||
|
||||
std::vector<CGAL::Point_2<Kernel_>> ps;
|
||||
|
||||
for (auto& he1 : CGAL::halfedges_around_face(face->halfedge(), *shape_)) {
|
||||
for (auto& he1 : CGAL::halfedges_around_face(face->halfedge(), poly)) {
|
||||
const auto& source = he1->vertex()->point();
|
||||
ps.push_back(transform_point(source));
|
||||
}
|
||||
@@ -140,33 +275,41 @@ ifcopenshell::geometry::CgalShape::CgalShape(const cgal_shape_t& shape, bool con
|
||||
|
||||
{
|
||||
for (auto& face : faces_to_remove) {
|
||||
CGAL::Euler::remove_face(face->halfedge(), *shape_);
|
||||
CGAL::Euler::remove_face(face->halfedge(), poly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shape.size_of_facets() != 1) {
|
||||
// the size_of_facets() == 1 check is for handling the specical case of
|
||||
// storing a single point in a polyhedron as a degenerate triangle
|
||||
//
|
||||
// @todo come up with a proper variant for storing lower dimensional entities
|
||||
|
||||
// @todo we don't have access to settings here so we don't know whether we should triangulate
|
||||
// remove_degenerate_faces() is also called in the triangulate() call below though...
|
||||
// CGAL::Polygon_mesh_processing::triangulate_faces(*shape_);
|
||||
// CGAL::Polygon_mesh_processing::remove_degenerate_faces(*shape_);
|
||||
ifcopenshell::geometry::CgalShape::CgalShape(const cgal_point_t& point, bool convex) {
|
||||
shape_ = point;
|
||||
convex_tag_ = convex;
|
||||
}
|
||||
|
||||
ifcopenshell::geometry::CgalShape::CgalShape(const cgal_wire_t& wire, bool convex) {
|
||||
shape_ = wire;
|
||||
convex_tag_ = convex;
|
||||
}
|
||||
|
||||
const cgal_shape_t& ifcopenshell::geometry::CgalShape::poly() const {
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
to_poly();
|
||||
#endif
|
||||
if (!shape_ || !std::holds_alternative<cgal_shape_t>(*shape_)) {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
return std::get<cgal_shape_t>(*shape_);
|
||||
}
|
||||
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
void ifcopenshell::geometry::CgalShape::to_poly() const {
|
||||
if (!shape_) {
|
||||
shape_.emplace();
|
||||
|
||||
convert_to_polyhedron(*nef_, *shape_);
|
||||
if (shape_->size_of_vertices() > 0) {
|
||||
cgal_shape_t poly;
|
||||
convert_to_polyhedron(*nef_, poly);
|
||||
if (poly.size_of_vertices() > 0) {
|
||||
// @todo why is this necessary? we have the mark of the volumes?
|
||||
CGAL::Polygon_mesh_processing::orient_to_bound_a_volume(*shape_);
|
||||
CGAL::Polygon_mesh_processing::orient_to_bound_a_volume(poly);
|
||||
}
|
||||
shape_ = poly;
|
||||
|
||||
// nef_->convert_to_polyhedron(*shape_);
|
||||
}
|
||||
@@ -174,18 +317,23 @@ void ifcopenshell::geometry::CgalShape::to_poly() const {
|
||||
|
||||
void ifcopenshell::geometry::CgalShape::to_nef() const {
|
||||
if (!nef_) {
|
||||
auto shp = poly();
|
||||
if (!convex_tag_) {
|
||||
if (CGAL::Polygon_mesh_processing::does_self_intersect(*shape_)) {
|
||||
if (CGAL::Polygon_mesh_processing::does_self_intersect(shp)) {
|
||||
throw std::runtime_error("Self-intersections detected, unable to proceed");
|
||||
}
|
||||
}
|
||||
nef_ = utils::create_nef_polyhedron(*shape_);
|
||||
nef_ = utils::create_nef_polyhedron(shp);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void ifcopenshell::geometry::CgalShape::Triangulate(ifcopenshell::geometry::Settings settings, const ifcopenshell::geometry::taxonomy::matrix4& place, IfcGeom::Representation::Triangulation* t, int item_id, int surface_style_id, Logger& logger) const {
|
||||
const bool all_triangles = std::all_of(shape_->facets_begin(), shape_->facets_end(), [](auto f) { return f.is_triangle(); });
|
||||
if (is_point() || is_wire()) {
|
||||
return;
|
||||
}
|
||||
const auto& base_shape = poly();
|
||||
const bool all_triangles = std::all_of(base_shape.facets_begin(), base_shape.facets_end(), [](auto f) { return f.is_triangle(); });
|
||||
const bool has_iden_transform = place.is_identity();
|
||||
|
||||
std::unique_ptr<cgal_shape_t> shape_copy_holder;
|
||||
@@ -193,10 +341,10 @@ void ifcopenshell::geometry::CgalShape::Triangulate(ifcopenshell::geometry::Sett
|
||||
|
||||
if (!all_triangles || !has_iden_transform) {
|
||||
// A copy is made when triangulate_faces() is required or when vertex positions need be transformed
|
||||
shape_copy_holder.reset(new cgal_shape_t(*this));
|
||||
shape_copy_holder.reset(new cgal_shape_t(base_shape));
|
||||
shape_to_use = shape_copy_holder.get();
|
||||
} else {
|
||||
shape_to_use = &*shape_;
|
||||
shape_to_use = const_cast<cgal_shape_t*>(&base_shape);
|
||||
}
|
||||
|
||||
const bool setting_use_original_edges = settings.get<ifcopenshell::geometry::settings::CgalEmitOriginalEdges>().get();
|
||||
@@ -403,25 +551,33 @@ void ifcopenshell::geometry::CgalShape::Triangulate(ifcopenshell::geometry::Sett
|
||||
}
|
||||
|
||||
void ifcopenshell::geometry::CgalShape::Serialize(const ifcopenshell::geometry::taxonomy::matrix4& place, std::string& r) const {
|
||||
cgal_shape_t s = *this;
|
||||
|
||||
if (!place.is_identity()) {
|
||||
const auto& m = place.ccomponents();
|
||||
|
||||
// @todo check
|
||||
const cgal_placement_t trsf(
|
||||
m(0, 0), m(0, 1), m(0, 2), m(0, 3),
|
||||
m(1, 0), m(1, 1), m(1, 2), m(1, 3),
|
||||
m(2, 0), m(2, 1), m(2, 2), m(2, 3));
|
||||
|
||||
// Apply transformation
|
||||
for (auto &vertex : s.vertex_handles()) {
|
||||
vertex->point() = vertex->point().transform(trsf);
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream sstream;
|
||||
sstream << s;
|
||||
if (is_point()) {
|
||||
auto p = point();
|
||||
if (!place.is_identity()) {
|
||||
p = p.transform(make_transform(place));
|
||||
}
|
||||
write_off_point(sstream, p);
|
||||
} else if (is_wire()) {
|
||||
auto w = wire();
|
||||
if (!place.is_identity()) {
|
||||
w = moved_wire(w, make_transform(place));
|
||||
}
|
||||
write_off_wire(sstream, w);
|
||||
} else {
|
||||
cgal_shape_t s = poly();
|
||||
|
||||
if (!place.is_identity()) {
|
||||
const auto trsf = make_transform(place);
|
||||
|
||||
// Apply transformation
|
||||
for (auto &vertex : s.vertex_handles()) {
|
||||
vertex->point() = vertex->point().transform(trsf);
|
||||
}
|
||||
}
|
||||
|
||||
sstream << s;
|
||||
}
|
||||
r = sstream.str();
|
||||
}
|
||||
|
||||
@@ -432,12 +588,26 @@ double ifcopenshell::geometry::CgalShape::bounding_box(void *& b) const {
|
||||
b = new CGAL::Bbox_3;
|
||||
}
|
||||
auto& bb = (*((CGAL::Bbox_3*)b));
|
||||
bb += CGAL::Polygon_mesh_processing::bbox(static_cast<cgal_shape_t>(*this));
|
||||
if (is_point()) {
|
||||
bb += point().bbox();
|
||||
} else if (is_wire()) {
|
||||
for (const auto& point : wire()) {
|
||||
bb += point.bbox();
|
||||
}
|
||||
} else {
|
||||
bb += CGAL::Polygon_mesh_processing::bbox(poly());
|
||||
}
|
||||
return (bb.xmax() - bb.xmin()) * (bb.ymax() - bb.ymin()) * (bb.zmax() - bb.zmin());
|
||||
}
|
||||
|
||||
int ifcopenshell::geometry::CgalShape::num_vertices() const {
|
||||
return (int) static_cast<cgal_shape_t>(*this).size_of_vertices();
|
||||
if (is_point()) {
|
||||
return 1;
|
||||
}
|
||||
if (is_wire()) {
|
||||
return (int) wire().size();
|
||||
}
|
||||
return (int) poly().size_of_vertices();
|
||||
}
|
||||
|
||||
void ifcopenshell::geometry::CgalShape::set_box(void * b) {
|
||||
@@ -448,10 +618,13 @@ void ifcopenshell::geometry::CgalShape::set_box(void * b) {
|
||||
}
|
||||
|
||||
int ifcopenshell::geometry::CgalShape::surface_genus() const {
|
||||
to_poly();
|
||||
auto nv = shape_->size_of_vertices();
|
||||
auto ne = shape_->size_of_halfedges() / 2;
|
||||
auto nf = shape_->size_of_facets();
|
||||
if (is_point() || is_wire()) {
|
||||
return 0;
|
||||
}
|
||||
const auto& shp = poly();
|
||||
auto nv = shp.size_of_vertices();
|
||||
auto ne = shp.size_of_halfedges() / 2;
|
||||
auto nf = shp.size_of_facets();
|
||||
|
||||
auto euler = nv - ne + nf;
|
||||
auto genus = (2 - euler) / 2;
|
||||
@@ -461,14 +634,22 @@ int ifcopenshell::geometry::CgalShape::surface_genus() const {
|
||||
|
||||
bool ifcopenshell::geometry::CgalShape::is_manifold() const {
|
||||
// @todo ?
|
||||
to_poly();
|
||||
return shape_->is_valid();
|
||||
return (is_point() || is_wire()) ? true : poly().is_valid();
|
||||
}
|
||||
|
||||
int ifcopenshell::geometry::CgalShape::num_edges() const
|
||||
{
|
||||
to_poly();
|
||||
return (int) shape_->size_of_halfedges() / 2;
|
||||
if (is_point()) {
|
||||
return 0;
|
||||
}
|
||||
if (is_wire()) {
|
||||
const auto n = wire().size();
|
||||
if (n < 2) {
|
||||
return 0;
|
||||
}
|
||||
return (int)(n == 2 ? 1 : n);
|
||||
}
|
||||
return (int) poly().size_of_halfedges() / 2;
|
||||
}
|
||||
|
||||
int ifcopenshell::geometry::CgalShape::num_faces() const
|
||||
@@ -479,7 +660,13 @@ int ifcopenshell::geometry::CgalShape::num_faces() const
|
||||
} else
|
||||
#endif
|
||||
if (shape_) {
|
||||
return (int) shape_->size_of_facets();
|
||||
if (is_poly()) {
|
||||
return (int) poly().size_of_facets();
|
||||
}
|
||||
if (is_wire() && wire().size() >= 3) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
@@ -487,46 +674,63 @@ int ifcopenshell::geometry::CgalShape::num_faces() const
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::CgalShape::length()
|
||||
{
|
||||
to_poly();
|
||||
Kernel_::FT len = 0;
|
||||
for (auto it = shape_->edges_begin(); it != shape_->edges_end(); ++it) {
|
||||
len += CGAL::approximate_sqrt(CGAL::Segment_3<Kernel_>(
|
||||
it->vertex()->point(),
|
||||
it->next()->vertex()->point()
|
||||
).squared_length());
|
||||
if (is_wire()) {
|
||||
len = wire_length(wire());
|
||||
} else if (!is_point()) {
|
||||
const auto& shp = poly();
|
||||
for (auto it = shp.edges_begin(); it != shp.edges_end(); ++it) {
|
||||
len += CGAL::approximate_sqrt(CGAL::Segment_3<Kernel_>(
|
||||
it->vertex()->point(),
|
||||
it->opposite()->vertex()->point()
|
||||
).squared_length());
|
||||
}
|
||||
}
|
||||
return new NumberType(len);
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::area()
|
||||
{
|
||||
to_poly();
|
||||
auto s = *shape_;
|
||||
if (is_wire()) {
|
||||
return new NumberType(wire_area(wire()));
|
||||
}
|
||||
if (is_point()) {
|
||||
return new NumberType(Kernel_::FT(0));
|
||||
}
|
||||
auto s = poly();
|
||||
CGAL::Polygon_mesh_processing::triangulate_faces(s);
|
||||
return new NumberType(CGAL::Polygon_mesh_processing::area(s));
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::volume()
|
||||
{
|
||||
to_poly();
|
||||
auto s = *shape_;
|
||||
if (is_point() || is_wire()) {
|
||||
return new NumberType(Kernel_::FT(0));
|
||||
}
|
||||
auto s = poly();
|
||||
CGAL::Polygon_mesh_processing::triangulate_faces(s);
|
||||
return new NumberType(CGAL::Polygon_mesh_processing::volume(s));
|
||||
}
|
||||
|
||||
OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::position()
|
||||
{
|
||||
to_poly();
|
||||
if (shape_->size_of_facets() == 1) {
|
||||
if (is_point()) {
|
||||
return opaque_point(point());
|
||||
}
|
||||
if (is_wire()) {
|
||||
return opaque_point(wire_centroid(wire()));
|
||||
}
|
||||
const auto& shp = poly();
|
||||
if (shp.size_of_facets() == 1) {
|
||||
// return centroid;
|
||||
// CGAL::Vector_3<Kernel_> p;
|
||||
std::array<Kernel_::FT, 3> p;
|
||||
for (auto it = shape_->points_begin(); it != shape_->points_end(); ++it) {
|
||||
std::array<Kernel_::FT, 3> p{ Kernel_::FT(0), Kernel_::FT(0), Kernel_::FT(0) };
|
||||
for (auto it = shp.points_begin(); it != shp.points_end(); ++it) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
p[i] += it->cartesian(i);
|
||||
}
|
||||
}
|
||||
Kernel_::FT N(std::distance(shape_->points_begin(), shape_->points_end()));
|
||||
Kernel_::FT N(std::distance(shp.points_begin(), shp.points_end()));
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
p[i] /= N;
|
||||
}
|
||||
@@ -542,19 +746,19 @@ OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::position()
|
||||
|
||||
OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::axis()
|
||||
{
|
||||
to_poly();
|
||||
if (shape_->size_of_facets() == 1) {
|
||||
auto pl = Plane_equation()(*shape_->facets_begin());
|
||||
std::array<typename Kernel_::FT, 3> abc{ pl.a(), pl.b(), pl.c() };
|
||||
auto minel = std::min_element(abc.begin(), abc.end());
|
||||
auto maxel = std::max_element(abc.begin(), abc.end());
|
||||
auto maxval = ((-*minel) > *maxel) ? (-*minel) : *maxel;
|
||||
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(pl.a() / maxval),
|
||||
new NumberType(pl.b() / maxval),
|
||||
new NumberType(pl.c() / maxval)
|
||||
);
|
||||
if (is_wire()) {
|
||||
if (wire().size() == 2) {
|
||||
return opaque_axis(wire()[1] - wire()[0]);
|
||||
}
|
||||
if (wire().size() >= 3) {
|
||||
return opaque_axis(wire_normal(wire()));
|
||||
}
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
auto shp = poly();
|
||||
if (shp.size_of_facets() == 1) {
|
||||
auto pl = Plane_equation()(*shp.facets_begin());
|
||||
return opaque_axis(cgal_vector_t(pl.a(), pl.b(), pl.c()));
|
||||
} else {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
@@ -562,6 +766,14 @@ OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::axis()
|
||||
|
||||
OpaqueCoordinate<4> ifcopenshell::geometry::CgalShape::plane_equation()
|
||||
{
|
||||
if (is_wire() && wire().size() >= 3) {
|
||||
auto normal = wire_normal(wire());
|
||||
return opaque_plane(cgal_plane_t(wire().front(), CGAL::Direction_3<Kernel_>(normal)));
|
||||
}
|
||||
auto shp = poly();
|
||||
if (shp.size_of_facets() == 1) {
|
||||
return opaque_plane(Plane_equation()(*shp.facets_begin()));
|
||||
}
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
}
|
||||
|
||||
@@ -612,75 +824,71 @@ ConversionResultShape * ifcopenshell::geometry::CgalShape::box()
|
||||
|
||||
ConversionResultShape* ifcopenshell::geometry::CgalShape::wrap_in_compound()
|
||||
{
|
||||
return new CgalShape(poly(), convex_tag_);
|
||||
return clone();
|
||||
}
|
||||
|
||||
std::vector<ConversionResultShape*> ifcopenshell::geometry::CgalShape::vertices()
|
||||
{
|
||||
// @todo this is ridiculous
|
||||
to_poly();
|
||||
std::vector<ConversionResultShape*> result;
|
||||
for (auto& p : shape_->points()) {
|
||||
std::vector<cgal_point_t> ps = {
|
||||
p, p, p
|
||||
};
|
||||
|
||||
std::vector<std::vector<size_t>> ids(1);
|
||||
ids.front().push_back(0);
|
||||
ids.front().push_back(1);
|
||||
ids.front().push_back(2);
|
||||
|
||||
cgal_shape_t poly;
|
||||
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(ps, ids, poly);
|
||||
|
||||
result.push_back(new CgalShape(poly));
|
||||
if (is_point()) {
|
||||
result.push_back(new CgalShape(point()));
|
||||
return result;
|
||||
}
|
||||
if (is_wire()) {
|
||||
for (const auto& p : wire()) {
|
||||
result.push_back(new CgalShape(p));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
for (const auto& p : poly().points()) {
|
||||
result.push_back(new CgalShape(p));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<ConversionResultShape*> ifcopenshell::geometry::CgalShape::edges()
|
||||
{
|
||||
// @todo this is ridiculous
|
||||
to_poly();
|
||||
std::vector<ConversionResultShape*> result;
|
||||
for (auto& ed : shape_->edges()) {
|
||||
std::vector<cgal_point_t> ps = {
|
||||
ed.vertex()->point(),
|
||||
ed.vertex()->point(),
|
||||
ed.next()->vertex()->point()
|
||||
};
|
||||
|
||||
std::vector<std::vector<size_t>> ids(1);
|
||||
ids.front().push_back(0);
|
||||
ids.front().push_back(1);
|
||||
ids.front().push_back(2);
|
||||
|
||||
cgal_shape_t poly;
|
||||
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(ps, ids, poly);
|
||||
|
||||
result.push_back(new CgalShape(poly));
|
||||
if (is_point()) {
|
||||
return result;
|
||||
}
|
||||
if (is_wire()) {
|
||||
const auto& w = wire();
|
||||
for (std::size_t i = 1; i < w.size(); ++i) {
|
||||
result.push_back(new CgalShape(cgal_wire_t{ w[i - 1], w[i] }));
|
||||
}
|
||||
if (w.size() > 2) {
|
||||
result.push_back(new CgalShape(cgal_wire_t{ w.back(), w.front() }));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
for (auto ed : poly().edges()) {
|
||||
result.push_back(new CgalShape(cgal_wire_t{ ed.vertex()->point(), ed.opposite()->vertex()->point() }));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<ConversionResultShape*> ifcopenshell::geometry::CgalShape::facets()
|
||||
{
|
||||
to_poly();
|
||||
std::vector<ConversionResultShape*> result;
|
||||
for (auto &face : faces(*shape_)) {
|
||||
if (is_point()) {
|
||||
return result;
|
||||
}
|
||||
if (is_wire()) {
|
||||
if (wire().size() >= 3) {
|
||||
result.push_back(new CgalShape(wire()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
for (auto face : faces(poly())) {
|
||||
std::vector<cgal_point_t> ps;
|
||||
std::vector<std::vector<size_t>> ids(1);
|
||||
|
||||
auto it = face->facet_begin();
|
||||
do {
|
||||
ps.push_back(it->vertex()->point());
|
||||
ids.front().push_back(ids.front().size());
|
||||
} while (++it != face->facet_begin());
|
||||
|
||||
cgal_shape_t poly;
|
||||
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(ps, ids, poly);
|
||||
|
||||
result.push_back(new CgalShape(poly));
|
||||
result.push_back(new CgalShape(ps));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -756,21 +964,21 @@ std::pair<OpaqueCoordinate<3>, OpaqueCoordinate<3>> ifcopenshell::geometry::Cgal
|
||||
|
||||
ConversionResultShape* ifcopenshell::geometry::CgalShape::moved(ifcopenshell::geometry::taxonomy::matrix4::ptr place) const
|
||||
{
|
||||
cgal_shape_t s = *this;
|
||||
if (place->is_identity()) {
|
||||
return clone();
|
||||
}
|
||||
|
||||
if (!place->is_identity()) {
|
||||
const auto& m = place->ccomponents();
|
||||
const auto trsf = make_transform(*place);
|
||||
if (is_point()) {
|
||||
return new CgalShape(point().transform(trsf), convex_tag_);
|
||||
}
|
||||
if (is_wire()) {
|
||||
return new CgalShape(moved_wire(wire(), trsf), convex_tag_);
|
||||
}
|
||||
|
||||
// @todo check
|
||||
const cgal_placement_t trsf(
|
||||
m(0, 0), m(0, 1), m(0, 2), m(0, 3),
|
||||
m(1, 0), m(1, 1), m(1, 2), m(1, 3),
|
||||
m(2, 0), m(2, 1), m(2, 2), m(2, 3));
|
||||
|
||||
// Apply transformation
|
||||
for (auto &vertex : s.vertex_handles()) {
|
||||
vertex->point() = vertex->point().transform(trsf);
|
||||
}
|
||||
cgal_shape_t s = poly();
|
||||
for (auto &vertex : s.vertex_handles()) {
|
||||
vertex->point() = vertex->point().transform(trsf);
|
||||
}
|
||||
|
||||
return new CgalShape(s, convex_tag_);
|
||||
@@ -1012,4 +1220,4 @@ ConversionResultShape* ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
|
||||
#include <CGAL/Polygon_mesh_processing/self_intersections.h>
|
||||
|
||||
#include <variant>
|
||||
|
||||
#ifdef IFOPSH_SIMPLE_KERNEL
|
||||
|
||||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
||||
@@ -179,13 +181,17 @@ namespace ifcopenshell { namespace geometry {
|
||||
|
||||
class IFC_GEOMLIBRARY_API CgalShape : public IfcGeom::ConversionResultShape {
|
||||
private:
|
||||
typedef std::variant<cgal_shape_t, cgal_point_t, cgal_wire_t> cgal_shape_storage_t;
|
||||
|
||||
bool convex_tag_ = false;
|
||||
mutable boost::optional<cgal_shape_t> shape_;
|
||||
mutable boost::optional<cgal_shape_storage_t> shape_;
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
mutable boost::optional<CGAL::Nef_polyhedron_3<Kernel_>> nef_;
|
||||
#endif
|
||||
public:
|
||||
CgalShape(const cgal_shape_t& shape, bool convex = false, Logger& logger = Logger::Root());
|
||||
CgalShape(const cgal_point_t& point, bool convex = false);
|
||||
CgalShape(const cgal_wire_t& wire, bool convex = false);
|
||||
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
CgalShape(const CGAL::Nef_polyhedron_3<Kernel_>& shape, bool convex = false) {
|
||||
@@ -206,14 +212,29 @@ namespace ifcopenshell { namespace geometry {
|
||||
void to_poly() const {}
|
||||
#endif
|
||||
|
||||
operator const cgal_shape_t& () const { to_poly(); return *shape_; }
|
||||
const cgal_shape_t& poly() const { to_poly(); return *shape_; }
|
||||
operator const cgal_shape_t& () const { return poly(); }
|
||||
const cgal_shape_t& poly() const;
|
||||
bool is_poly() const { return shape_ && std::holds_alternative<cgal_shape_t>(*shape_); }
|
||||
bool is_point() const { return shape_ && std::holds_alternative<cgal_point_t>(*shape_); }
|
||||
bool is_wire() const { return shape_ && std::holds_alternative<cgal_wire_t>(*shape_); }
|
||||
const cgal_point_t& point() const { return std::get<cgal_point_t>(*shape_); }
|
||||
const cgal_wire_t& wire() const { return std::get<cgal_wire_t>(*shape_); }
|
||||
|
||||
virtual void Triangulate(ifcopenshell::geometry::Settings settings, const ifcopenshell::geometry::taxonomy::matrix4& place, IfcGeom::Representation::Triangulation* t, int item_id, int surface_style_id, Logger& logger = Logger::Root()) const;
|
||||
virtual void Serialize(const ifcopenshell::geometry::taxonomy::matrix4& place, std::string&) const;
|
||||
|
||||
virtual IfcGeom::ConversionResultShape* clone() const {
|
||||
return new CgalShape(*shape_);
|
||||
if (shape_) {
|
||||
return std::visit([this](const auto& value) -> IfcGeom::ConversionResultShape* {
|
||||
return new CgalShape(value, convex_tag_);
|
||||
}, *shape_);
|
||||
}
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
if (nef_) {
|
||||
return new CgalShape(*nef_, convex_tag_);
|
||||
}
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual bool is_manifold() const;
|
||||
|
||||
Reference in New Issue
Block a user