1st installment of ModelInstance 3D scale components

This commit is contained in:
Enrico Turri
2018-09-24 15:54:09 +02:00
parent e3d44b07fe
commit 0e1843a871
26 changed files with 558 additions and 9 deletions

View File

@@ -1270,9 +1270,11 @@ namespace Slic3r {
if ((sx == 0.0) || (sy == 0.0) || (sz == 0.0))
return;
#if !ENABLE_MODELINSTANCE_3D_SCALE
// non-uniform scale value, return
if ((std::abs(sx - sy) > 0.00001) || (std::abs(sx - sz) > 0.00001))
return;
#endif // !ENABLE_MODELINSTANCE_3D_SCALE
double inv_sx = 1.0 / sx;
double inv_sy = 1.0 / sy;
@@ -1303,7 +1305,11 @@ namespace Slic3r {
instance.offset(0) = offset_x;
instance.offset(1) = offset_y;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
#if ENABLE_MODELINSTANCE_3D_SCALE
instance.set_scaling_factor(Vec3d(sx, sy, sz));
#else
instance.scaling_factor = sx;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#if ENABLE_MODELINSTANCE_3D_ROTATION
instance.set_rotation(rotation);
#else

View File

@@ -31,8 +31,14 @@
// 1 : Introduction of amf versioning. No other change in data saved into amf files.
#if ENABLE_MODELINSTANCE_3D_OFFSET
#if ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
// 2 : Added z component of offset
// Added x and y components of rotation
// Added x, y and z components of scale
#else
// 2 : Added z component of offset
// Added x and y components of rotation
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#else
// 2 : Added z component of offset.
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
@@ -138,13 +144,22 @@ struct AMFParserContext
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
NODE_TYPE_RZ, // amf/constellation/instance/rz
NODE_TYPE_SCALE, // amf/constellation/instance/scale
#if ENABLE_MODELINSTANCE_3D_SCALE
NODE_TYPE_SCALEX, // amf/constellation/instance/scalex
NODE_TYPE_SCALEY, // amf/constellation/instance/scaley
NODE_TYPE_SCALEZ, // amf/constellation/instance/scalez
#endif // ENABLE_MODELINSTANCE_3D_SCALE
NODE_TYPE_METADATA, // anywhere under amf/*/metadata
};
struct Instance {
#if ENABLE_MODELINSTANCE_3D_OFFSET
#if ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scalex_set(false), scaley_set(false), scalez_set(false) {}
#else
Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scale_set(false) {}
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#else
Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rz_set(false), scale_set(false) {}
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
@@ -173,9 +188,19 @@ struct AMFParserContext
// Rotation around the Z axis.
float rz;
bool rz_set;
#if ENABLE_MODELINSTANCE_3D_SCALE
// Scaling factors
float scalex;
bool scalex_set;
float scaley;
bool scaley_set;
float scalez;
bool scalez_set;
#else
// Scaling factor
float scale;
bool scale_set;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
};
struct Object {
@@ -304,6 +329,14 @@ void AMFParserContext::startElement(const char *name, const char **atts)
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
else if (strcmp(name, "rz") == 0)
node_type_new = NODE_TYPE_RZ;
#if ENABLE_MODELINSTANCE_3D_SCALE
else if (strcmp(name, "scalex") == 0)
node_type_new = NODE_TYPE_SCALEX;
else if (strcmp(name, "scaley") == 0)
node_type_new = NODE_TYPE_SCALEY;
else if (strcmp(name, "scalez") == 0)
node_type_new = NODE_TYPE_SCALEZ;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
else if (strcmp(name, "scale") == 0)
node_type_new = NODE_TYPE_SCALE;
}
@@ -371,7 +404,14 @@ void AMFParserContext::characters(const XML_Char *s, int len)
m_path.back() == NODE_TYPE_RY ||
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
m_path.back() == NODE_TYPE_RZ ||
#if ENABLE_MODELINSTANCE_3D_SCALE
m_path.back() == NODE_TYPE_SCALEX ||
m_path.back() == NODE_TYPE_SCALEY ||
m_path.back() == NODE_TYPE_SCALEZ ||
m_path.back() == NODE_TYPE_SCALE)
#else
m_path.back() == NODE_TYPE_SCALE)
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#else
if (m_path.back() == NODE_TYPE_DELTAX || m_path.back() == NODE_TYPE_DELTAY || m_path.back() == NODE_TYPE_RZ || m_path.back() == NODE_TYPE_SCALE)
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
@@ -444,10 +484,39 @@ void AMFParserContext::endElement(const char * /* name */)
break;
case NODE_TYPE_SCALE:
assert(m_instance);
#if ENABLE_MODELINSTANCE_3D_SCALE
m_instance->scalex = float(atof(m_value[0].c_str()));
m_instance->scalex_set = true;
m_instance->scaley = float(atof(m_value[0].c_str()));
m_instance->scaley_set = true;
m_instance->scalez = float(atof(m_value[0].c_str()));
m_instance->scalez_set = true;
#else
m_instance->scale = float(atof(m_value[0].c_str()));
m_instance->scale_set = true;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
m_value[0].clear();
break;
#if ENABLE_MODELINSTANCE_3D_SCALE
case NODE_TYPE_SCALEX:
assert(m_instance);
m_instance->scalex = float(atof(m_value[0].c_str()));
m_instance->scalex_set = true;
m_value[0].clear();
break;
case NODE_TYPE_SCALEY:
assert(m_instance);
m_instance->scaley = float(atof(m_value[0].c_str()));
m_instance->scaley_set = true;
m_value[0].clear();
break;
case NODE_TYPE_SCALEZ:
assert(m_instance);
m_instance->scalez = float(atof(m_value[0].c_str()));
m_instance->scalez_set = true;
m_value[0].clear();
break;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
// Object vertices:
case NODE_TYPE_VERTEX:
@@ -596,7 +665,11 @@ void AMFParserContext::endDocument()
#else
mi->rotation = instance.rz_set ? instance.rz : 0.f;
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0));
#else
mi->scaling_factor = instance.scale_set ? instance.scale : 1.f;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
}
}
}
@@ -904,7 +977,13 @@ bool store_amf(const char *path, Model *model, Print* print, bool export_print_c
" <ry>%lf</ry>\n"
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
" <rz>%lf</rz>\n"
#if ENABLE_MODELINSTANCE_3D_SCALE
" <scalex>%lf</scalex>\n"
" <scaley>%lf</scaley>\n"
" <scalez>%lf</scalez>\n"
#else
" <scale>%lf</scale>\n"
#endif // ENABLE_MODELINSTANCE_3D_SCALE
" </instance>\n",
object_id,
#if ENABLE_MODELINSTANCE_3D_OFFSET
@@ -922,7 +1001,13 @@ bool store_amf(const char *path, Model *model, Print* print, bool export_print_c
#else
instance->rotation,
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
instance->get_scaling_factor(X),
instance->get_scaling_factor(Y),
instance->get_scaling_factor(Z));
#else
instance->scaling_factor);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
//FIXME missing instance->scaling_factor
instances.append(buf);

View File

@@ -169,7 +169,11 @@ bool load_prus(const char *path, Model *model)
#else
double instance_rotation = 0.;
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
Vec3d instance_scaling_factor = Vec3d::Ones();
#else
double instance_scaling_factor = 1.f;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#if ENABLE_MODELINSTANCE_3D_OFFSET
Vec3d instance_offset = Vec3d::Zero();
#else
@@ -197,10 +201,14 @@ bool load_prus(const char *path, Model *model)
"[%f, %f, %f]", scale, scale+1, scale+2) == 3 &&
sscanf(zero_xml+strlen(zero_tag),
"[%f, %f, %f]", zero, zero+1, zero+2) == 3) {
#if ENABLE_MODELINSTANCE_3D_SCALE
instance_scaling_factor = Vec3d((double)scale[0], (double)scale[1], (double)scale[2]);
#else
if (scale[0] == scale[1] && scale[1] == scale[2]) {
instance_scaling_factor = scale[0];
scale[0] = scale[1] = scale[2] = 1.;
}
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#if ENABLE_MODELINSTANCE_3D_ROTATION
instance_rotation = Vec3d(-(double)rotation[0], -(double)rotation[1], -(double)rotation[2]);
#else
@@ -225,7 +233,12 @@ bool load_prus(const char *path, Model *model)
instance_offset(0) = position[0] - zero[0];
instance_offset(1) = position[1] - zero[1];
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
#if ENABLE_MODELINSTANCE_3D_SCALE
// CHECK_ME -> Is the following correct ?
trafo[2][3] = position[2] / instance_scaling_factor(2);
#else
trafo[2][3] = position[2] / instance_scaling_factor;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
trafo_set = true;
}
const char *group_tag = "<group>";
@@ -379,7 +392,11 @@ bool load_prus(const char *path, Model *model)
#else
instance->rotation = instance_rotation;
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
instance->set_scaling_factor(instance_scaling_factor);
#else
instance->scaling_factor = instance_scaling_factor;
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance->set_offset(instance_offset);
#else

View File

@@ -1090,11 +1090,23 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes
BoundingBoxf3 bbox = copy.bounding_box();
if (!empty(bbox)) {
#if ENABLE_MODELINSTANCE_3D_SCALE
// Scale the bounding box along the three axes.
for (unsigned int i = 0; i < 3; ++i)
{
if (std::abs(this->m_scaling_factor(i) - 1.0) > EPSILON)
{
bbox.min(i) *= this->m_scaling_factor(i);
bbox.max(i) *= this->m_scaling_factor(i);
}
}
#else
// Scale the bounding box uniformly.
if (std::abs(this->scaling_factor - 1.) > EPSILON) {
bbox.min *= this->scaling_factor;
bbox.max *= this->scaling_factor;
}
#endif // ENABLE_MODELINSTANCE_3D_SCALE
// Translate the bounding box.
if (! dont_translate) {
#if ENABLE_MODELINSTANCE_3D_OFFSET
@@ -1127,7 +1139,12 @@ void ModelInstance::transform_polygon(Polygon* polygon) const
#else
polygon->rotate(this->rotation); // rotate around polygon origin
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
// CHECK_ME -> Is the following correct ?
polygon->scale(this->m_scaling_factor(0), this->m_scaling_factor(1)); // scale around polygon origin
#else
polygon->scale(this->scaling_factor); // scale around polygon origin
#endif // ENABLE_MODELINSTANCE_3D_SCALE
}
Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, bool dont_scale) const
@@ -1153,7 +1170,11 @@ Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, b
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
if (!dont_scale)
#if ENABLE_MODELINSTANCE_3D_SCALE
m.scale(m_scaling_factor);
#else
m.scale(scaling_factor);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
return m;
}

View File

@@ -249,6 +249,9 @@ private:
#if ENABLE_MODELINSTANCE_3D_ROTATION
Vec3d m_rotation; // Rotation around the three axes, in radians around mesh center point
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
Vec3d m_scaling_factor; // Scaling factors along the three axes
#endif // ENABLE_MODELINSTANCE_3D_SCALE
public:
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
@@ -256,7 +259,9 @@ public:
#if !ENABLE_MODELINSTANCE_3D_ROTATION
double rotation; // Rotation around the Z axis, in radians around mesh center point
#endif // !ENABLE_MODELINSTANCE_3D_ROTATION
#if !ENABLE_MODELINSTANCE_3D_SCALE
double scaling_factor;
#endif // !ENABLE_MODELINSTANCE_3D_SCALE
#if !ENABLE_MODELINSTANCE_3D_OFFSET
Vec2d offset; // in unscaled coordinates
#endif // !ENABLE_MODELINSTANCE_3D_OFFSET
@@ -282,6 +287,14 @@ public:
void set_rotation(Axis axis, double rotation);
#endif // ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
Vec3d get_scaling_factor() const { return m_scaling_factor; }
double get_scaling_factor(Axis axis) const { return m_scaling_factor(axis); }
void set_scaling_factor(const Vec3d& scaling_factor) { m_scaling_factor = scaling_factor; }
void set_scaling_factor(Axis axis, double scaling_factor) { m_scaling_factor(axis) = scaling_factor; }
#endif // ENABLE_MODELINSTANCE_3D_SCALE
// To be called on an external mesh
void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const;
// Calculate a bounding box of a transformed mesh. To be called on an external mesh.
@@ -303,9 +316,15 @@ private:
#if ENABLE_MODELINSTANCE_3D_OFFSET
#if ENABLE_MODELINSTANCE_3D_ROTATION
#if ENABLE_MODELINSTANCE_3D_SCALE
ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), m_scaling_factor(Vec3d::Ones()), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {}
ModelInstance(ModelObject *object, const ModelInstance &other) :
m_rotation(other.m_rotation), m_scaling_factor(other.m_scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {}
#else
ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {}
ModelInstance(ModelObject *object, const ModelInstance &other) :
m_rotation(other.m_rotation), scaling_factor(other.scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {}
#endif // ENABLE_MODELINSTANCE_3D_SCALE
#else
ModelInstance(ModelObject *object) : rotation(0), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {}
ModelInstance(ModelObject *object, const ModelInstance &other) :

View File

@@ -29,7 +29,12 @@ std::string toString(const Model& model, bool holes = true) {
if(!objinst) continue;
Slic3r::TriangleMesh tmpmesh = rmesh;
#if ENABLE_MODELINSTANCE_3D_SCALE
// CHECK_ME -> Is the following correct ?
tmpmesh.scale(objinst->get_scaling_factor());
#else
tmpmesh.scale(objinst->scaling_factor);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
objinst->transform_mesh(&tmpmesh);
ExPolygons expolys = tmpmesh.horizontal_projection();
for(auto& expoly_complex : expolys) {
@@ -87,7 +92,11 @@ void toSVG(SVG& svg, const Model& model) {
if(!objinst) continue;
Slic3r::TriangleMesh tmpmesh = rmesh;
#if ENABLE_MODELINSTANCE_3D_SCALE
tmpmesh.scale(objinst->get_scaling_factor());
#else
tmpmesh.scale(objinst->scaling_factor);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
objinst->transform_mesh(&tmpmesh);
ExPolygons expolys = tmpmesh.horizontal_projection();
svg.draw(expolys);
@@ -513,7 +522,12 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) {
Slic3r::TriangleMesh tmpmesh = rmesh;
ClipperLib::PolygonImpl pn;
#if ENABLE_MODELINSTANCE_3D_SCALE
// CHECK_ME -> is the following correct ?
tmpmesh.scale(objinst->get_scaling_factor());
#else
tmpmesh.scale(objinst->scaling_factor);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
// TODO export the exact 2D projection
auto p = tmpmesh.convex_hull();

View File

@@ -14,6 +14,17 @@ void MultiPoint::scale(double factor)
pt *= factor;
}
#if ENABLE_MODELINSTANCE_3D_SCALE
void MultiPoint::scale(double factor_x, double factor_y)
{
for (Point &pt : points)
{
pt(0) *= factor_x;
pt(1) *= factor_y;
}
}
#endif // ENABLE_MODELINSTANCE_3D_SCALE
void MultiPoint::translate(double x, double y)
{
Vector v(x, y);

View File

@@ -26,6 +26,9 @@ public:
MultiPoint& operator=(const MultiPoint &other) { points = other.points; return *this; }
MultiPoint& operator=(MultiPoint &&other) { points = std::move(other.points); return *this; }
void scale(double factor);
#if ENABLE_MODELINSTANCE_3D_SCALE
void scale(double factor_x, double factor_y);
#endif // ENABLE_MODELINSTANCE_3D_SCALE
void translate(double x, double y);
void translate(const Point &vector);
void rotate(double angle) { this->rotate(cos(angle), sin(angle)); }

View File

@@ -431,7 +431,14 @@ void Print::add_model_object(ModelObject* model_object, int idx)
std::vector<std::string> v_scale;
for (const PrintObject *object : m_objects) {
const ModelObject &mobj = *object->model_object();
v_scale.push_back(boost::lexical_cast<std::string>(mobj.instances[0]->scaling_factor*100) + "%");
#if ENABLE_MODELINSTANCE_3D_SCALE
// CHECK_ME -> Is the following correct ?
v_scale.push_back("x:" + boost::lexical_cast<std::string>(mobj.instances[0]->get_scaling_factor(X) * 100) +
"% y:" + boost::lexical_cast<std::string>(mobj.instances[0]->get_scaling_factor(Y) * 100) +
"% z:" + boost::lexical_cast<std::string>(mobj.instances[0]->get_scaling_factor(Z) * 100) + "%");
#else
v_scale.push_back(boost::lexical_cast<std::string>(mobj.instances[0]->scaling_factor * 100) + "%");
#endif // ENABLE_MODELINSTANCE_3D_SCALE
if (input_file.empty())
input_file = mobj.input_file;
}

View File

@@ -10,7 +10,8 @@
#define ENABLE_GIZMOS_RESET (1 && ENABLE_1_42_0)
// Add x and y rotation components to model instances' offset
#define ENABLE_MODELINSTANCE_3D_ROTATION (1 && ENABLE_MODELINSTANCE_3D_OFFSET)
// Add scaling factors for all the three axes to model instances
#define ENABLE_MODELINSTANCE_3D_SCALE (1 && ENABLE_MODELINSTANCE_3D_ROTATION)
#endif // _technologies_h_