2011-09-01 06:09:06 +04:00
|
|
|
#include "CGAL_Nef_polyhedron.h"
|
2011-08-22 21:31:56 +04:00
|
|
|
#include "cgal.h"
|
2011-09-08 00:04:59 +04:00
|
|
|
#include "cgalutils.h"
|
2011-09-08 11:24:10 +04:00
|
|
|
#include "printutils.h"
|
2011-08-22 21:31:56 +04:00
|
|
|
#include "polyset.h"
|
2015-03-02 22:47:28 +03:00
|
|
|
#include "svg.h"
|
2011-11-27 02:31:11 +04:00
|
|
|
|
|
|
|
CGAL_Nef_polyhedron::CGAL_Nef_polyhedron(CGAL_Nef_polyhedron3 *p)
|
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
if (p) p3.reset(p);
|
2011-11-27 02:31:11 +04:00
|
|
|
}
|
|
|
|
|
2014-07-18 01:03:03 +04:00
|
|
|
// Copy constructor
|
|
|
|
CGAL_Nef_polyhedron::CGAL_Nef_polyhedron(const CGAL_Nef_polyhedron &src)
|
|
|
|
{
|
|
|
|
if (src.p3) this->p3.reset(new CGAL_Nef_polyhedron3(*src.p3));
|
|
|
|
}
|
2011-11-27 02:31:11 +04:00
|
|
|
|
2011-09-01 06:09:06 +04:00
|
|
|
CGAL_Nef_polyhedron& CGAL_Nef_polyhedron::operator+=(const CGAL_Nef_polyhedron &other)
|
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
(*this->p3) += (*other.p3);
|
2011-09-01 06:09:06 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGAL_Nef_polyhedron& CGAL_Nef_polyhedron::operator*=(const CGAL_Nef_polyhedron &other)
|
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
(*this->p3) *= (*other.p3);
|
2011-09-01 06:09:06 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGAL_Nef_polyhedron& CGAL_Nef_polyhedron::operator-=(const CGAL_Nef_polyhedron &other)
|
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
(*this->p3) -= (*other.p3);
|
2011-09-01 22:03:35 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGAL_Nef_polyhedron &CGAL_Nef_polyhedron::minkowski(const CGAL_Nef_polyhedron &other)
|
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
(*this->p3) = CGAL::minkowski_sum_3(*this->p3, *other.p3);
|
2011-09-01 06:09:06 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-21 10:25:15 +04:00
|
|
|
size_t CGAL_Nef_polyhedron::memsize() const
|
2011-09-01 06:09:06 +04:00
|
|
|
{
|
2013-12-29 06:37:08 +04:00
|
|
|
if (this->isEmpty()) return 0;
|
2012-02-19 16:09:02 +04:00
|
|
|
|
2012-01-06 22:10:33 +04:00
|
|
|
size_t memsize = sizeof(CGAL_Nef_polyhedron);
|
2013-12-29 09:37:49 +04:00
|
|
|
memsize += this->p3->bytes();
|
2012-01-06 22:10:33 +04:00
|
|
|
return memsize;
|
2011-09-01 06:09:06 +04:00
|
|
|
}
|
2011-08-22 21:31:56 +04:00
|
|
|
|
2014-12-09 02:25:48 +03:00
|
|
|
bool CGAL_Nef_polyhedron::isEmpty() const
|
|
|
|
{
|
|
|
|
return !this->p3 || this->p3->is_empty();
|
|
|
|
}
|
|
|
|
|
2011-08-22 21:31:56 +04:00
|
|
|
/*!
|
|
|
|
Creates a new PolySet and initializes it with the data from this polyhedron
|
2011-08-25 00:25:15 +04:00
|
|
|
|
2011-12-12 03:48:07 +04:00
|
|
|
Note: Can return NULL if an error occurred
|
2011-08-22 21:31:56 +04:00
|
|
|
*/
|
2014-08-21 21:20:32 +04:00
|
|
|
// FIXME: Deprecated by CGALUtils::createPolySetFromNefPolyhedron3
|
|
|
|
#if 0
|
2013-11-24 04:52:03 +04:00
|
|
|
PolySet *CGAL_Nef_polyhedron::convertToPolyset() const
|
2011-08-22 21:31:56 +04:00
|
|
|
{
|
2013-12-29 09:37:49 +04:00
|
|
|
if (this->isEmpty()) return new PolySet(3);
|
2011-09-08 00:04:59 +04:00
|
|
|
PolySet *ps = NULL;
|
2013-12-29 09:37:49 +04:00
|
|
|
CGAL::Failure_behaviour old_behaviour = CGAL::set_error_behaviour(CGAL::THROW_EXCEPTION);
|
|
|
|
ps = new PolySet(3);
|
|
|
|
ps->setConvexity(this->convexity);
|
|
|
|
bool err = true;
|
|
|
|
std::string errmsg("");
|
|
|
|
CGAL_Polyhedron P;
|
|
|
|
try {
|
|
|
|
// Cast away constness:
|
|
|
|
// convert_to_Polyhedron() wasn't const in earlier versions of CGAL.
|
|
|
|
CGAL_Nef_polyhedron3 *nonconst_nef3 = const_cast<CGAL_Nef_polyhedron3*>(this->p3.get());
|
|
|
|
err = nefworkaround::convert_to_Polyhedron<CGAL_Kernel3>( *(nonconst_nef3), P );
|
|
|
|
//this->p3->convert_to_Polyhedron(P);
|
|
|
|
}
|
|
|
|
catch (const CGAL::Failure_exception &e) {
|
|
|
|
err = true;
|
|
|
|
errmsg = std::string(e.what());
|
|
|
|
}
|
2014-08-20 06:02:08 +04:00
|
|
|
if (!err) err = CGALUtils::createPolySetFromPolyhedron(P, *ps);
|
2013-12-29 09:37:49 +04:00
|
|
|
if (err) {
|
|
|
|
PRINT("ERROR: CGAL NefPolyhedron->Polyhedron conversion failed.");
|
|
|
|
if (errmsg!="") PRINTB("ERROR: %s",errmsg);
|
|
|
|
delete ps; ps = NULL;
|
2011-08-22 21:31:56 +04:00
|
|
|
}
|
2013-12-29 09:37:49 +04:00
|
|
|
CGAL::set_error_behaviour(old_behaviour);
|
2011-08-22 21:31:56 +04:00
|
|
|
return ps;
|
|
|
|
}
|
2014-08-21 21:20:32 +04:00
|
|
|
#endif
|
2011-09-01 06:09:06 +04:00
|
|
|
|
2014-07-18 01:03:03 +04:00
|
|
|
void CGAL_Nef_polyhedron::resize(Vector3d newsize,
|
|
|
|
const Eigen::Matrix<bool,3,1> &autosize)
|
2011-09-01 06:09:06 +04:00
|
|
|
{
|
2014-07-18 01:03:03 +04:00
|
|
|
// Based on resize() in Giles Bathgate's RapCAD (but not exactly)
|
|
|
|
if (this->isEmpty()) return;
|
|
|
|
|
|
|
|
CGAL_Iso_cuboid_3 bb = CGALUtils::boundingBox(*this->p3);
|
|
|
|
|
|
|
|
std::vector<NT3> scale, bbox_size;
|
|
|
|
for (unsigned int i=0;i<3;i++) {
|
|
|
|
scale.push_back(NT3(1));
|
|
|
|
bbox_size.push_back(bb.max_coord(i) - bb.min_coord(i));
|
|
|
|
}
|
|
|
|
int newsizemax_index = 0;
|
|
|
|
for (unsigned int i=0;i<this->getDimension();i++) {
|
|
|
|
if (newsize[i]) {
|
|
|
|
if (bbox_size[i] == NT3(0)) {
|
|
|
|
PRINT("WARNING: Resize in direction normal to flat object is not implemented");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scale[i] = NT3(newsize[i]) / bbox_size[i];
|
|
|
|
}
|
|
|
|
if (newsize[i] > newsize[newsizemax_index]) newsizemax_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NT3 autoscale = NT3(1);
|
|
|
|
if (newsize[newsizemax_index] != 0) {
|
|
|
|
autoscale = NT3(newsize[newsizemax_index]) / bbox_size[newsizemax_index];
|
|
|
|
}
|
|
|
|
for (unsigned int i=0;i<this->getDimension();i++) {
|
|
|
|
if (autosize[i] && newsize[i]==0) scale[i] = autoscale;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eigen::Matrix4d t;
|
|
|
|
t << CGAL::to_double(scale[0]), 0, 0, 0,
|
|
|
|
0, CGAL::to_double(scale[1]), 0, 0,
|
|
|
|
0, 0, CGAL::to_double(scale[2]), 0,
|
|
|
|
0, 0, 0, 1;
|
|
|
|
|
|
|
|
this->transform(Transform3d(t));
|
2011-09-01 06:09:06 +04:00
|
|
|
}
|
2015-03-02 22:47:28 +03:00
|
|
|
|
|
|
|
std::string CGAL_Nef_polyhedron::dump() const
|
|
|
|
{
|
|
|
|
return OpenSCAD::dump_svg( *this->p3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CGAL_Nef_polyhedron::transform( const Transform3d &matrix )
|
|
|
|
{
|
|
|
|
if (!this->isEmpty()) {
|
|
|
|
if (matrix.matrix().determinant() == 0) {
|
|
|
|
PRINT("WARNING: Scaling a 3D object with 0 - removing object");
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CGAL_Aff_transformation t(
|
|
|
|
matrix(0,0), matrix(0,1), matrix(0,2), matrix(0,3),
|
|
|
|
matrix(1,0), matrix(1,1), matrix(1,2), matrix(1,3),
|
|
|
|
matrix(2,0), matrix(2,1), matrix(2,2), matrix(2,3), matrix(3,3));
|
|
|
|
this->p3->transform(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|