mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-09 13:52:23 +00:00
Auto mem mngt in conversion result number types; more arithmetic on OpaqueCoordinate
This commit is contained in:
@@ -40,9 +40,9 @@ namespace {
|
||||
|
||||
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))
|
||||
NumberType(p.cartesian(0)),
|
||||
NumberType(p.cartesian(1)),
|
||||
NumberType(p.cartesian(2))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ namespace {
|
||||
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)
|
||||
NumberType(v.x() / maxval),
|
||||
NumberType(v.y() / maxval),
|
||||
NumberType(v.z() / maxval)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,27 +71,27 @@ namespace {
|
||||
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)
|
||||
NumberType(p.a() / maxval),
|
||||
NumberType(p.b() / maxval),
|
||||
NumberType(p.c() / maxval),
|
||||
NumberType(p.d() / maxval)
|
||||
);
|
||||
}
|
||||
|
||||
cgal_plane_t plane_from_opaque(const OpaqueCoordinate<4>& p) {
|
||||
#ifdef IFOPSH_SIMPLE_KERNEL
|
||||
return cgal_plane_t(
|
||||
p.get(0)->to_double(),
|
||||
p.get(1)->to_double(),
|
||||
p.get(2)->to_double(),
|
||||
p.get(3)->to_double()
|
||||
p.get(0).to_double(),
|
||||
p.get(1).to_double(),
|
||||
p.get(2).to_double(),
|
||||
p.get(3).to_double()
|
||||
);
|
||||
#else
|
||||
return cgal_plane_t(
|
||||
static_cast<NumberEpeck*>(p.get(0))->value(),
|
||||
static_cast<NumberEpeck*>(p.get(1))->value(),
|
||||
static_cast<NumberEpeck*>(p.get(2))->value(),
|
||||
static_cast<NumberEpeck*>(p.get(3))->value()
|
||||
p.get(0).value_as<CGAL::Epeck::FT>(),
|
||||
p.get(1).value_as<CGAL::Epeck::FT>(),
|
||||
p.get(2).value_as<CGAL::Epeck::FT>(),
|
||||
p.get(3).value_as<CGAL::Epeck::FT>()
|
||||
);
|
||||
#endif
|
||||
}
|
||||
@@ -697,7 +697,7 @@ int ifcopenshell::geometry::CgalShape::num_faces() const
|
||||
}
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::CgalShape::length()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShape::CgalShape::length()
|
||||
{
|
||||
Kernel_::FT len = 0;
|
||||
if (is_wire()) {
|
||||
@@ -711,30 +711,30 @@ OpaqueNumber* ifcopenshell::geometry::CgalShape::CgalShape::length()
|
||||
).squared_length());
|
||||
}
|
||||
}
|
||||
return new NumberType(len);
|
||||
return NumberType(len);
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::area()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShape::area()
|
||||
{
|
||||
if (is_wire()) {
|
||||
return new NumberType(wire_area(wire()));
|
||||
return NumberType(wire_area(wire()));
|
||||
}
|
||||
if (is_point()) {
|
||||
return new NumberType(Kernel_::FT(0));
|
||||
return NumberType(Kernel_::FT(0));
|
||||
}
|
||||
auto s = poly();
|
||||
CGAL::Polygon_mesh_processing::triangulate_faces(s);
|
||||
return new NumberType(CGAL::Polygon_mesh_processing::area(s));
|
||||
return NumberType(CGAL::Polygon_mesh_processing::area(s));
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShape::volume()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShape::volume()
|
||||
{
|
||||
if (is_point() || is_wire()) {
|
||||
return new NumberType(Kernel_::FT(0));
|
||||
return NumberType(Kernel_::FT(0));
|
||||
}
|
||||
auto s = poly();
|
||||
CGAL::Polygon_mesh_processing::triangulate_faces(s);
|
||||
return new NumberType(CGAL::Polygon_mesh_processing::volume(s));
|
||||
return NumberType(CGAL::Polygon_mesh_processing::volume(s));
|
||||
}
|
||||
|
||||
OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::position()
|
||||
@@ -760,9 +760,9 @@ OpaqueCoordinate<3> ifcopenshell::geometry::CgalShape::position()
|
||||
p[i] /= N;
|
||||
}
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(p[0]),
|
||||
new NumberType(p[1]),
|
||||
new NumberType(p[2])
|
||||
NumberType(p[0]),
|
||||
NumberType(p[1]),
|
||||
NumberType(p[2])
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
@@ -1058,17 +1058,17 @@ int ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::num_faces() const
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::CgalShapeHalfSpaceDecomposition::length()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::CgalShapeHalfSpaceDecomposition::length()
|
||||
{
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::area()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::area()
|
||||
{
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::volume()
|
||||
OpaqueNumber ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::volume()
|
||||
{
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
@@ -1078,9 +1078,9 @@ OpaqueCoordinate<3> ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::pos
|
||||
if (planes_.size() == 1) {
|
||||
auto xyz = CGAL::ORIGIN + planes_.front().d() * CGAL::Vector_3<Kernel_>(planes_.front().a(), planes_.front().b(), planes_.front().c());
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(xyz.cartesian(0)),
|
||||
new NumberType(xyz.cartesian(1)),
|
||||
new NumberType(xyz.cartesian(2))
|
||||
NumberType(xyz.cartesian(0)),
|
||||
NumberType(xyz.cartesian(1)),
|
||||
NumberType(xyz.cartesian(2))
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
@@ -1095,9 +1095,9 @@ OpaqueCoordinate<3> ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::axi
|
||||
auto maxel = std::max_element(abc.begin(), abc.end());
|
||||
auto maxval = ((-*minel) > *maxel) ? (-*minel) : *maxel;
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberType(planes_.front().a() / maxval),
|
||||
new NumberType(planes_.front().b() / maxval),
|
||||
new NumberType(planes_.front().c() / maxval)
|
||||
NumberType(planes_.front().a() / maxval),
|
||||
NumberType(planes_.front().b() / maxval),
|
||||
NumberType(planes_.front().c() / maxval)
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
@@ -1112,10 +1112,10 @@ OpaqueCoordinate<4> ifcopenshell::geometry::CgalShapeHalfSpaceDecomposition::pla
|
||||
auto maxel = std::max_element(abc.begin(), abc.end());
|
||||
auto maxval = ((-*minel) > *maxel) ? (-*minel) : *maxel;
|
||||
return OpaqueCoordinate<4>(
|
||||
new NumberType(planes_.front().a() / maxval),
|
||||
new NumberType(planes_.front().b() / maxval),
|
||||
new NumberType(planes_.front().c() / maxval),
|
||||
new NumberType(planes_.front().d() / maxval)
|
||||
NumberType(planes_.front().a() / maxval),
|
||||
NumberType(planes_.front().b() / maxval),
|
||||
NumberType(planes_.front().c() / maxval),
|
||||
NumberType(planes_.front().d() / maxval)
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid shape type");
|
||||
|
||||
@@ -95,86 +95,80 @@ namespace ifcopenshell { namespace geometry {
|
||||
using IfcGeom::OpaqueCoordinate;
|
||||
using IfcGeom::OpaqueNumber;
|
||||
|
||||
using IfcGeom::add_;
|
||||
using IfcGeom::subtract_;
|
||||
using IfcGeom::multiply_;
|
||||
using IfcGeom::divide_;
|
||||
using IfcGeom::equals_;
|
||||
using IfcGeom::less_than_;
|
||||
using IfcGeom::negate_;
|
||||
|
||||
#ifndef IFOPSH_SIMPLE_KERNEL
|
||||
class IFC_GEOMLIBRARY_API NumberEpeck : public OpaqueNumber {
|
||||
private:
|
||||
CGAL::Epeck::FT value_;
|
||||
struct Model : OpaqueNumber::NumberConcept {
|
||||
CGAL::Epeck::FT value;
|
||||
|
||||
template <CGAL::Epeck::FT(*Fn)(CGAL::Epeck::FT, CGAL::Epeck::FT)>
|
||||
OpaqueNumber* binary_op(OpaqueNumber* other) const {
|
||||
auto nnd = dynamic_cast<NumberEpeck*>(other);
|
||||
if (nnd) {
|
||||
return new NumberEpeck(Fn(value_, nnd->value_));
|
||||
} else {
|
||||
return nullptr;
|
||||
Model(const CGAL::Epeck::FT& v)
|
||||
: value(v) {}
|
||||
|
||||
static const Model& as_same(const NumberConcept& other) {
|
||||
auto same = dynamic_cast<const Model*>(&other);
|
||||
if (same == nullptr) {
|
||||
throw std::runtime_error("Incompatible opaque number types");
|
||||
}
|
||||
return *same;
|
||||
}
|
||||
}
|
||||
|
||||
template <bool(*Fn)(CGAL::Epeck::FT, CGAL::Epeck::FT)>
|
||||
bool binary_op_bool(OpaqueNumber* other) const {
|
||||
auto nnd = dynamic_cast<NumberEpeck*>(other);
|
||||
if (nnd) {
|
||||
return Fn(value_, nnd->value_);
|
||||
} else {
|
||||
return false;
|
||||
virtual double to_double() const {
|
||||
return CGAL::to_double(value);
|
||||
}
|
||||
}
|
||||
|
||||
template <CGAL::Epeck::FT(*Fn)(CGAL::Epeck::FT)>
|
||||
OpaqueNumber* unary_op() const {
|
||||
return new NumberEpeck(Fn(value_));
|
||||
}
|
||||
virtual std::string to_string() const {
|
||||
std::stringstream ss;
|
||||
ss << value.exact();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> add(const NumberConcept& other) const {
|
||||
return std::make_shared<Model>(value + as_same(other).value);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> subtract(const NumberConcept& other) const {
|
||||
return std::make_shared<Model>(value - as_same(other).value);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> multiply(const NumberConcept& other) const {
|
||||
return std::make_shared<Model>(value * as_same(other).value);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> divide(const NumberConcept& other) const {
|
||||
return std::make_shared<Model>(value / as_same(other).value);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> negate() const {
|
||||
return std::make_shared<Model>(-value);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<const NumberConcept> from_double(double v) const {
|
||||
return std::make_shared<Model>(CGAL::Epeck::FT(v));
|
||||
}
|
||||
|
||||
virtual bool equals(const NumberConcept& other) const {
|
||||
return value == as_same(other).value;
|
||||
}
|
||||
|
||||
virtual bool less_than(const NumberConcept& other) const {
|
||||
return value < as_same(other).value;
|
||||
}
|
||||
|
||||
virtual const std::type_info& type() const {
|
||||
return typeid(CGAL::Epeck::FT);
|
||||
}
|
||||
|
||||
virtual const void* value_ptr() const {
|
||||
return &value;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
NumberEpeck(const CGAL::Epeck::FT& v)
|
||||
: value_(v) {}
|
||||
|
||||
virtual ~NumberEpeck() { }
|
||||
|
||||
virtual double to_double() const {
|
||||
return CGAL::to_double(value_);
|
||||
}
|
||||
|
||||
virtual std::string to_string() const {
|
||||
std::stringstream ss;
|
||||
ss << value_.exact();
|
||||
return ss.str();
|
||||
}
|
||||
: OpaqueNumber(std::make_shared<Model>(v)) {}
|
||||
|
||||
const CGAL::Epeck::FT& value() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
virtual OpaqueNumber* operator+(OpaqueNumber* other) const {
|
||||
return binary_op<add_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual OpaqueNumber* operator-(OpaqueNumber* other) const {
|
||||
return binary_op<subtract_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual OpaqueNumber* operator*(OpaqueNumber* other) const {
|
||||
return binary_op<multiply_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual OpaqueNumber* operator/(OpaqueNumber* other) const {
|
||||
return binary_op<divide_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual bool operator==(OpaqueNumber* other) const {
|
||||
return binary_op_bool<equals_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual bool operator<(OpaqueNumber* other) const {
|
||||
return binary_op_bool<less_than_<CGAL::Epeck::FT>>(other);
|
||||
}
|
||||
virtual OpaqueNumber* operator-() const {
|
||||
return unary_op<negate_<CGAL::Epeck::FT>>();
|
||||
}
|
||||
virtual OpaqueNumber* clone() const {
|
||||
return new NumberEpeck(value_);
|
||||
return value_as<CGAL::Epeck::FT>();
|
||||
}
|
||||
};
|
||||
#endif
|
||||
@@ -253,9 +247,9 @@ namespace ifcopenshell { namespace geometry {
|
||||
// @todo this must be something with a virtual dtor so that we can delete it.
|
||||
virtual std::pair<OpaqueCoordinate<3>, OpaqueCoordinate<3>> bounding_box() const;
|
||||
|
||||
virtual OpaqueNumber* length();
|
||||
virtual OpaqueNumber* area();
|
||||
virtual OpaqueNumber* volume();
|
||||
virtual OpaqueNumber length();
|
||||
virtual OpaqueNumber area();
|
||||
virtual OpaqueNumber volume();
|
||||
|
||||
virtual OpaqueCoordinate<3> position();
|
||||
virtual OpaqueCoordinate<3> axis();
|
||||
@@ -322,9 +316,9 @@ namespace ifcopenshell { namespace geometry {
|
||||
virtual std::pair<OpaqueCoordinate<3>, OpaqueCoordinate<3>> bounding_box() const;
|
||||
virtual void set_box(void* b);
|
||||
|
||||
virtual OpaqueNumber* length();
|
||||
virtual OpaqueNumber* area();
|
||||
virtual OpaqueNumber* volume();
|
||||
virtual OpaqueNumber length();
|
||||
virtual OpaqueNumber area();
|
||||
virtual OpaqueNumber volume();
|
||||
|
||||
virtual OpaqueCoordinate<3> position();
|
||||
virtual OpaqueCoordinate<3> axis();
|
||||
|
||||
@@ -385,28 +385,28 @@ int ifcopenshell::geometry::OpenCascadeShape::num_faces() const
|
||||
return IfcGeom::util::count(shape_, TopAbs_FACE);
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::OpenCascadeShape::OpenCascadeShape::length()
|
||||
OpaqueNumber ifcopenshell::geometry::OpenCascadeShape::OpenCascadeShape::length()
|
||||
{
|
||||
GProp_GProps prop;
|
||||
BRepGProp::LinearProperties(shape_, prop);
|
||||
double l = prop.Mass();
|
||||
return new NumberNativeDouble(l);
|
||||
return NumberNativeDouble(l);
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::OpenCascadeShape::area()
|
||||
OpaqueNumber ifcopenshell::geometry::OpenCascadeShape::area()
|
||||
{
|
||||
GProp_GProps prop;
|
||||
BRepGProp::SurfaceProperties(shape_, prop);
|
||||
double l = prop.Mass();
|
||||
return new NumberNativeDouble(l);
|
||||
return NumberNativeDouble(l);
|
||||
}
|
||||
|
||||
OpaqueNumber* ifcopenshell::geometry::OpenCascadeShape::volume()
|
||||
OpaqueNumber ifcopenshell::geometry::OpenCascadeShape::volume()
|
||||
{
|
||||
GProp_GProps prop;
|
||||
BRepGProp::VolumeProperties(shape_, prop);
|
||||
double l = prop.Mass();
|
||||
return new NumberNativeDouble(l);
|
||||
return NumberNativeDouble(l);
|
||||
}
|
||||
|
||||
#include <Geom_Plane.hxx>
|
||||
@@ -419,9 +419,9 @@ OpaqueCoordinate<3> ifcopenshell::geometry::OpenCascadeShape::position()
|
||||
if (plane) {
|
||||
auto loc = plane->Location();
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberNativeDouble(loc.X()),
|
||||
new NumberNativeDouble(loc.Y()),
|
||||
new NumberNativeDouble(loc.Z())
|
||||
NumberNativeDouble(loc.X()),
|
||||
NumberNativeDouble(loc.Y()),
|
||||
NumberNativeDouble(loc.Z())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -436,9 +436,9 @@ OpaqueCoordinate<3> ifcopenshell::geometry::OpenCascadeShape::axis()
|
||||
if (plane) {
|
||||
auto dir = plane->Axis().Direction();
|
||||
return OpaqueCoordinate<3>(
|
||||
new NumberNativeDouble(dir.X()),
|
||||
new NumberNativeDouble(dir.Y()),
|
||||
new NumberNativeDouble(dir.Z())
|
||||
NumberNativeDouble(dir.X()),
|
||||
NumberNativeDouble(dir.Y()),
|
||||
NumberNativeDouble(dir.Z())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -454,10 +454,10 @@ OpaqueCoordinate<4> ifcopenshell::geometry::OpenCascadeShape::plane_equation()
|
||||
double a, b, c, d;
|
||||
plane->Pln().Coefficients(a, b, c, d);
|
||||
return OpaqueCoordinate<4>(
|
||||
new NumberNativeDouble(a),
|
||||
new NumberNativeDouble(b),
|
||||
new NumberNativeDouble(c),
|
||||
new NumberNativeDouble(d)
|
||||
NumberNativeDouble(a),
|
||||
NumberNativeDouble(b),
|
||||
NumberNativeDouble(c),
|
||||
NumberNativeDouble(d)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,9 +78,9 @@ namespace ifcopenshell {
|
||||
// @todo this must be something with a virtual dtor so that we can delete it.
|
||||
virtual std::pair<OpaqueCoordinate<3>, OpaqueCoordinate<3>> bounding_box() const;
|
||||
|
||||
virtual OpaqueNumber* length();
|
||||
virtual OpaqueNumber* area();
|
||||
virtual OpaqueNumber* volume();
|
||||
virtual OpaqueNumber length();
|
||||
virtual OpaqueNumber area();
|
||||
virtual OpaqueNumber volume();
|
||||
|
||||
virtual OpaqueCoordinate<3> position();
|
||||
virtual OpaqueCoordinate<3> axis();
|
||||
|
||||
Reference in New Issue
Block a user