Files
IfcOpenShell/src/ifcgeom/kernels/cgal/CgalConversionFunctions.cpp
T

239 lines
9.1 KiB
C++
Raw Normal View History

2017-01-16 14:04:31 +01:00
#include "../../../ifcparse/IfcParse.h"
#include "CgalKernel.h"
#include "CgalConversionResult.h"
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcRepresentation* l, ConversionResults& shapes) {
IfcSchema::IfcRepresentationItem::list::ptr items = l->Items();
bool part_succes = false;
if (items->size()) {
for (IfcSchema::IfcRepresentationItem::list::it it = items->begin(); it != items->end(); ++it) {
IfcSchema::IfcRepresentationItem* representation_item = *it;
if (shape_type(representation_item) == ST_SHAPELIST) {
part_succes |= convert_shapes(*it, shapes);
} else {
cgal_shape_t s;
if (convert_shape(representation_item, s)) {
shapes.push_back(ConversionResult(new CgalShape(s), get_style(representation_item)));
part_succes |= true;
}
}
}
}
return part_succes;
}
2017-02-13 11:12:04 -06:00
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolid *l, cgal_shape_t &shape) {
const double height = l->Depth() * getValue(GV_LENGTH_UNIT);
if (height < getValue(GV_PRECISION)) {
Logger::Message(Logger::LOG_ERROR, "Non-positive extrusion height encountered for:", l->entity);
return false;
}
cgal_face_t face;
if ( !convert_face(l->SweptArea(),face) ) return false;
cgal_placement_t trsf;
bool has_position = true;
#ifdef USE_IFC4
has_position = l->hasPosition();
#endif
if (has_position) {
IfcGeom::CgalKernel::convert(l->Position(), trsf);
}
cgal_direction_t dir;
convert(l->ExtrudedDirection(),dir);
2017-02-22 18:15:27 -06:00
// std::cout << "Direction: " << dir << std::endl;
2017-02-13 11:12:04 -06:00
std::list<cgal_face_t> face_list;
face_list.push_back(face);
for (std::vector<Kernel::Point_3>::const_iterator current_vertex = face.outer.begin();
current_vertex != face.outer.end();
++current_vertex) {
std::vector<Kernel::Point_3>::const_iterator next_vertex = current_vertex;
++next_vertex;
if (next_vertex == face.outer.end()) {
next_vertex = face.outer.begin();
} cgal_face_t side_face;
side_face.outer.push_back(*next_vertex);
2017-02-13 11:45:17 -06:00
side_face.outer.push_back(*current_vertex);
2017-02-22 18:15:27 -06:00
side_face.outer.push_back(*current_vertex+height*dir);
side_face.outer.push_back(*next_vertex+height*dir);
2017-02-13 11:12:04 -06:00
face_list.push_back(side_face);
}
2017-02-13 11:45:17 -06:00
cgal_face_t top_face;
for (std::vector<Kernel::Point_3>::const_reverse_iterator vertex = face.outer.rbegin();
vertex != face.outer.rend();
++vertex) {
2017-02-22 18:15:27 -06:00
top_face.outer.push_back(*vertex+height*dir);
2017-02-13 11:45:17 -06:00
} face_list.push_back(top_face);
2017-02-20 20:14:49 -06:00
// Naive creation
2017-02-13 11:12:04 -06:00
cgal_shape_t polyhedron = CGAL::Polyhedron_3<Kernel>();
PolyhedronBuilder builder(&face_list);
polyhedron.delegate(builder);
2017-02-20 20:14:49 -06:00
// Stitch edges
// std::cout << "Before: " << polyhedron.size_of_vertices() << " vertices and " << polyhedron.size_of_facets() << " facets" << std::endl;
CGAL::Polygon_mesh_processing::stitch_borders(polyhedron);
if (!CGAL::Polygon_mesh_processing::is_outward_oriented(polyhedron)) {
CGAL::Polygon_mesh_processing::reverse_face_orientations(polyhedron);
}
// std::cout << "After: " << polyhedron.size_of_vertices() << " vertices and " << polyhedron.size_of_facets() << " facets" << std::endl;
2017-02-13 11:12:04 -06:00
shape = polyhedron;
return true;
2017-01-16 14:04:31 +01:00
}
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcCartesianPoint* l, cgal_point_t& point) {
2017-01-31 19:03:15 -06:00
std::vector<double> xyz = l->Coordinates();
if (xyz.size() == 3) {
point = Kernel::Point_3(xyz.size() ? (xyz[0]*getValue(GV_LENGTH_UNIT)) : 0.0f,
xyz.size() > 1 ? (xyz[1]*getValue(GV_LENGTH_UNIT)) : 0.0f,
xyz.size() > 2 ? (xyz[2]*getValue(GV_LENGTH_UNIT)) : 0.0f);
2017-02-20 20:14:49 -06:00
// std::cout << "Converted Point(" << point << ")" << std::endl;
2017-01-31 19:03:15 -06:00
return true;
} else {
2017-02-01 17:53:12 -06:00
throw std::runtime_error("Point without 3 coordinates");
2017-01-31 19:03:15 -06:00
}
}
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcDirection* l, cgal_direction_t& dir) {
// IN_CACHE(IfcDirection,l,cgal_direction_t,dir)
std::vector<double> xyz = l->DirectionRatios();
dir = Kernel::Vector_3(xyz.size() ? xyz[0] : 0.0f,
xyz.size() > 1 ? xyz[1] : 0.0f,
xyz.size() > 2 ? xyz[2] : 0.0f);
// CACHE(IfcDirection,l,dir)
return true;
}
2017-02-08 16:56:59 -06:00
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcAxis2Placement2D* l, cgal_placement_t& trsf) {
// IN_CACHE(IfcAxis2Placement3D,l,gp_Trsf,trsf)
cgal_point_t o;
cgal_direction_t axis = Kernel::Vector_3(0,0,1);
cgal_direction_t refDirection = Kernel::Vector_3(1,0,0); // TODO: Put identity for now. Check?
IfcGeom::CgalKernel::convert(l->Location(),o);
bool hasRef = l->hasRefDirection();
if ( hasRef ) IfcGeom::CgalKernel::convert(l->RefDirection(),refDirection);
// TODO: From Thomas' email. Should be checked.
Kernel::Vector_3 y = CGAL::cross_product(Kernel::Vector_3(0.0, 0.0, 1.0), refDirection);
trsf = Kernel::Aff_transformation_3(refDirection.cartesian(0), y.cartesian(0), 0.0, o.cartesian(0),
refDirection.cartesian(1), y.cartesian(1), 0.0, o.cartesian(1),
0.0, y.cartesian(2), 1.0, 0.0);
// CACHE(IfcAxis2Placement3D,l,trsf)
return true;
}
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcAxis2Placement3D* l, cgal_placement_t& trsf) {
// IN_CACHE(IfcAxis2Placement3D,l,gp_Trsf,trsf)
cgal_point_t o;
cgal_direction_t axis = Kernel::Vector_3(0,0,1);
cgal_direction_t refDirection = Kernel::Vector_3(1,0,0); // TODO: Put identity for now. Check?
IfcGeom::CgalKernel::convert(l->Location(),o);
bool hasRef = l->hasRefDirection();
if ( l->hasAxis() ) IfcGeom::CgalKernel::convert(l->Axis(),axis);
if ( hasRef ) IfcGeom::CgalKernel::convert(l->RefDirection(),refDirection);
// std::cout << "Ref direction: " << refDirection << std::endl;
// std::cout << "Axis: " << axis << std::endl;
// std::cout << "Origin: " << o << std::endl;
// TODO: From Thomas' email. Should be checked.
2017-02-08 16:56:59 -06:00
Kernel::Vector_3 y = CGAL::cross_product(axis, refDirection);
trsf = Kernel::Aff_transformation_3(refDirection.cartesian(0), y.cartesian(0), axis.cartesian(0), o.cartesian(0),
refDirection.cartesian(1), y.cartesian(1), axis.cartesian(1), o.cartesian(1),
refDirection.cartesian(2), y.cartesian(2), axis.cartesian(2), o.cartesian(2));
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
// CACHE(IfcAxis2Placement3D,l,trsf)
return true;
}
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcObjectPlacement* l, cgal_placement_t& trsf) {
// TODO: These macros don't work for the CGAL types. Need to check why.
// IN_CACHE(IfcObjectPlacement,l,cgal_placement_t,trsf)
if ( ! l->is(IfcSchema::Type::IfcLocalPlacement) ) {
Logger::Message(Logger::LOG_ERROR, "Unsupported IfcObjectPlacement:", l->entity);
return false;
}
// std::cout << "initial trsf (identity?)" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
IfcSchema::IfcLocalPlacement* current = (IfcSchema::IfcLocalPlacement*)l;
for (;;) {
cgal_placement_t trsf2;
IfcSchema::IfcAxis2Placement* relplacement = current->RelativePlacement();
if ( relplacement->is(IfcSchema::Type::IfcAxis2Placement3D) ) {
IfcGeom::CgalKernel::convert((IfcSchema::IfcAxis2Placement3D*)relplacement,trsf2);
// std::cout << "trsf2" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << trsf2.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
trsf = trsf * trsf2; // TODO: I think it's fine, but maybe should it be the other way around?
// std::cout << "trsf (after multiplication)" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
}
if ( current->hasPlacementRelTo() ) {
IfcSchema::IfcObjectPlacement* relto = current->PlacementRelTo();
if ( relto->is(IfcSchema::Type::IfcLocalPlacement) )
current = (IfcSchema::IfcLocalPlacement*)current->PlacementRelTo();
else break;
} else break;
}
// CACHE(IfcObjectPlacement,l,trsf)
return true;
}
2017-02-13 11:12:04 -06:00
bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcRectangleProfileDef* l, cgal_face_t& face) {
const double x = l->XDim() / 2.0f * getValue(GV_LENGTH_UNIT);
const double y = l->YDim() / 2.0f * getValue(GV_LENGTH_UNIT);
if ( x < ALMOST_ZERO || y < ALMOST_ZERO ) {
Logger::Message(Logger::LOG_NOTICE,"Skipping zero sized profile:",l->entity);
return false;
}
cgal_placement_t trsf2d;
bool has_position = true;
#ifdef USE_IFC4
has_position = l->hasPosition();
#endif
if (has_position) {
IfcGeom::CgalKernel::convert(l->Position(), trsf2d);
}
face = cgal_face_t();
face.outer.push_back(Kernel::Point_3(-x, -y, 0.0));
face.outer.push_back(Kernel::Point_3( x, -y, 0.0));
face.outer.push_back(Kernel::Point_3( x, y, 0.0));
face.outer.push_back(Kernel::Point_3(-x, y, 0.0));
return true;
}