1st installment of 3d offset for ModelInstance

This commit is contained in:
Enrico Turri
2018-09-13 15:15:00 +02:00
parent bef19fe6da
commit af19713a27
16 changed files with 400 additions and 7 deletions

View File

@@ -242,10 +242,27 @@ void Model::center_instances_around_point(const Vec2d &point)
for (size_t i = 0; i < o->instances.size(); ++ i)
bb.merge(o->instance_bounding_box(i, false));
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
Vec2d shift2 = point - to_2d(bb.center());
Vec3d shift3 = Vec3d(shift2(0), shift2(1), 0.0);
#else
//################################################################################################################################
Vec2d shift = point - 0.5 * to_2d(bb.size()) - to_2d(bb.min);
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
for (ModelObject *o : this->objects) {
for (ModelInstance *i : o->instances)
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
i->set_offset(i->get_offset() + shift3);
#else
//################################################################################################################################
i->offset += shift;
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
o->invalidate_bounding_box();
}
}
@@ -311,8 +328,17 @@ bool Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb)
size_t idx = 0;
for (ModelObject *o : this->objects) {
for (ModelInstance *i : o->instances) {
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
Vec2d offset_xy = positions[idx] - instance_centers[idx];
i->set_offset(Vec3d(offset_xy(0), offset_xy(1), i->get_offset(Z)));
#else
//################################################################################################################################
i->offset = positions[idx] - instance_centers[idx];
++ idx;
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
++idx;
}
o->invalidate_bounding_box();
}
@@ -336,7 +362,15 @@ void Model::duplicate(size_t copies_num, coordf_t dist, const BoundingBoxf* bb)
for (const ModelInstance *i : instances) {
for (const Vec2d &pos : positions) {
ModelInstance *instance = o->add_instance(*i);
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance->set_offset(instance->get_offset() + Vec3d(pos(0), pos(1), 0.0));
#else
//################################################################################################################################
instance->offset += pos;
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
}
}
o->invalidate_bounding_box();
@@ -366,13 +400,29 @@ void Model::duplicate_objects_grid(size_t x, size_t y, coordf_t dist)
ModelObject* object = this->objects.front();
object->clear_instances();
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
Vec3d ext_size = object->bounding_box().size() + dist * Vec3d::Ones();
#else
//################################################################################################################################
Vec3d size = object->bounding_box().size();
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
for (size_t x_copy = 1; x_copy <= x; ++x_copy) {
for (size_t y_copy = 1; y_copy <= y; ++y_copy) {
ModelInstance* instance = object->add_instance();
instance->offset(0) = (size(0) + dist) * (x_copy-1);
instance->offset(1) = (size(1) + dist) * (y_copy-1);
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance->set_offset(Vec3d(ext_size(0) * (double)(x_copy - 1), ext_size(1) * (double)(y_copy - 1), 0.0));
#else
//################################################################################################################################
instance->offset(0) = (size(0) + dist) * (x_copy - 1);
instance->offset(1) = (size(1) + dist) * (y_copy - 1);
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
}
}
}
@@ -676,12 +726,27 @@ void ModelObject::center_around_origin()
this->translate(shift);
this->origin_translation += shift;
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
// set z to zero, translation in z has already been done within the mesh
shift(2) = 0.0;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
if (!this->instances.empty()) {
for (ModelInstance *i : this->instances) {
// apply rotation and scaling to vector as well before translating instance,
// in order to leave final position unaltered
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
i->set_offset(i->get_offset() + i->transform_vector(-shift, true));
#else
//################################################################################################################################
Vec3d i_shift = i->world_matrix(true) * shift;
i->offset -= to_2d(i_shift);
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
}
this->invalidate_bounding_box();
}
@@ -1004,8 +1069,17 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes
}
// Translate the bounding box.
if (! dont_translate) {
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
bbox.min += this->m_offset;
bbox.max += this->m_offset;
#else
//################################################################################################################################
Eigen::Map<Vec2d>(bbox.min.data()) += this->offset;
Eigen::Map<Vec2d>(bbox.max.data()) += this->offset;
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
}
}
return bbox;
@@ -1032,7 +1106,15 @@ Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, b
Transform3d m = Transform3d::Identity();
if (!dont_translate)
//################################################################################################################################
#if ENABLE_MODELINSTANCE_3D_OFFSET
m.translate(m_offset);
#else
//################################################################################################################################
m.translate(Vec3d(offset(0), offset(1), 0.0));
//################################################################################################################################
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
//################################################################################################################################
if (!dont_rotate)
m.rotate(Eigen::AngleAxisd(rotation, Vec3d::UnitZ()));