rework the former property origin_translation

This commit is contained in:
Michael Kirsch 2019-07-21 21:26:39 +02:00 committed by Joseph Lenox
parent c2580bd02e
commit 4745d13955
8 changed files with 53 additions and 44 deletions

View File

@ -2593,6 +2593,7 @@ sub reload_from_disk {
$new_obj->clear_instances;
$new_obj->add_instance($_) for @{$org_obj->instances};
$new_obj->config->apply($org_obj->config);
$new_obj->set_trafo_obj($org_obj->get_trafo_obj()) if $reload_preserve_trafo;
my $new_vol_idx = 0;
my $org_vol_idx = 0;
@ -2605,8 +2606,8 @@ sub reload_from_disk {
$new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_volume($org_vol_idx)->get_transformation) if $reload_preserve_trafo;
$new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume($org_vol_idx++)->config);
} else {
# reload has more volumes than original (first file), apply config and trafo from the first volume
$new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_volume(0)->get_transformation) if $reload_preserve_trafo;
# reload has more volumes than original (first file), apply config and trafo from the parent object
$new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_trafo_obj()) if $reload_preserve_trafo;
$new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume(0)->config);
$volume_unmatched=1;
}
@ -2623,8 +2624,6 @@ sub reload_from_disk {
if ($reload_behavior==1) { # Reload behavior: copy
my $new_volume = $new_obj->add_volume($org_volume);
#$new_volume->mesh->translate(@{$org_obj->origin_translation->negative});
#$new_volume->mesh->translate(@{$new_obj->origin_translation});
if ($new_volume->name =~ m/link to path\z/) {
my $new_name = $new_volume->name;
$new_name =~ s/ - no link to path$/ - copied/;
@ -2658,14 +2657,11 @@ sub reload_from_disk {
$new_volume->set_input_file_vol_idx($org_volume->input_file_vol_idx);
$new_volume->config->apply($org_volume->config);
$new_volume->set_modifier($org_volume->modifier);
#$new_volume->mesh->translate(@{$new_obj->origin_translation});
}
}
}
if (!$org_volume->input_file) {
my $new_volume = $new_obj->add_volume($org_volume); # error -> copy old mesh
#$new_volume->mesh->translate(@{$org_obj->origin_translation->negative});
#$new_volume->mesh->translate(@{$new_obj->origin_translation});
if ($new_volume->name =~ m/copied\z/) {
my $new_name = $new_volume->name;
$new_name =~ s/ - copied$/ - no link to path/;
@ -2678,6 +2674,7 @@ sub reload_from_disk {
}
$org_vol_idx++;
}
$new_obj->center_around_origin();
}
$self->remove($obj_idx);

View File

@ -36,8 +36,8 @@ sub add_object {
if defined $args{config};
$new_object->set_layer_height_ranges($args{layer_height_ranges})
if defined $args{layer_height_ranges};
$new_object->set_origin_translation($args{origin_translation})
if defined $args{origin_translation};
$new_object->apply_transformation($args{trafo_obj})
if defined $args{trafo_obj};
return $new_object;
}

View File

@ -539,6 +539,8 @@ AMF::write(const Model& model, std::string output_file)
std::vector<size_t> vertices_offsets;
size_t num_vertices = 0;
Pointf3 origin_translation = object->origin_translation();
for (ModelVolume *volume : object->volumes) {
volume->mesh.require_shared_vertices();
vertices_offsets.push_back(num_vertices);
@ -552,9 +554,9 @@ AMF::write(const Model& model, std::string output_file)
// below.
file << " <vertex>" << endl
<< " <coordinates>" << endl
<< " <x>" << (stl.v_shared[i].x - object->origin_translation.x) << "</x>" << endl
<< " <y>" << (stl.v_shared[i].y - object->origin_translation.y) << "</y>" << endl
<< " <z>" << (stl.v_shared[i].z - object->origin_translation.z) << "</z>" << endl
<< " <x>" << (stl.v_shared[i].x - origin_translation.x) << "</x>" << endl
<< " <y>" << (stl.v_shared[i].y - origin_translation.y) << "</y>" << endl
<< " <z>" << (stl.v_shared[i].z - origin_translation.z) << "</z>" << endl
<< " </coordinates>" << endl
<< " </vertex>" << endl;
@ -597,8 +599,8 @@ AMF::write(const Model& model, std::string output_file)
for (const ModelInstance* instance : object->instances)
instances
<< " <instance objectid=\"" << object_id << "\">" << endl
<< " <deltax>" << instance->offset.x + object->origin_translation.x << "</deltax>" << endl
<< " <deltay>" << instance->offset.y + object->origin_translation.y << "</deltay>" << endl
<< " <deltax>" << instance->offset.x + origin_translation.x << "</deltax>" << endl
<< " <deltay>" << instance->offset.y + origin_translation.y << "</deltay>" << endl
<< " <rz>" << instance->rotation << "</rz>" << endl
<< " <scale>" << instance->scaling_factor << "</scale>" << endl
<< " </instance>" << endl;

View File

@ -144,6 +144,8 @@ TMFEditor::write_object(boost::nowide::ofstream& fout, const ModelObject* object
std::vector<int> vertices_offsets;
int num_vertices = 0;
Pointf3 origin_translation = object->origin_translation();
for (const auto volume : object->volumes){
// Require mesh vertices.
volume->mesh.require_shared_vertices();
@ -160,9 +162,9 @@ TMFEditor::write_object(boost::nowide::ofstream& fout, const ModelObject* object
// In order to do this we compensate for this translation in the instance placement
// below.
fout << " <vertex";
fout << " x=\"" << (stl.v_shared[i].x - object->origin_translation.x) << "\"";
fout << " y=\"" << (stl.v_shared[i].y - object->origin_translation.y) << "\"";
fout << " z=\"" << (stl.v_shared[i].z - object->origin_translation.z) << "\"/>\n";
fout << " x=\"" << (stl.v_shared[i].x - origin_translation.x) << "\"";
fout << " y=\"" << (stl.v_shared[i].y - origin_translation.y) << "\"";
fout << " z=\"" << (stl.v_shared[i].z - origin_translation.z) << "\"/>\n";
}
num_vertices += stl.stats.shared_vertices;
}

View File

@ -452,7 +452,7 @@ ModelObject::ModelObject(Model *model, const ModelObject &other, bool copy_volum
layer_height_ranges(other.layer_height_ranges),
part_number(other.part_number),
layer_height_spline(other.layer_height_spline),
origin_translation(other.origin_translation),
trafo_obj(other.trafo_obj),
_bounding_box(other._bounding_box),
_bounding_box_valid(other._bounding_box_valid),
model(model)
@ -483,7 +483,7 @@ ModelObject::swap(ModelObject &other)
std::swap(this->config, other.config);
std::swap(this->layer_height_ranges, other.layer_height_ranges);
std::swap(this->layer_height_spline, other.layer_height_spline);
std::swap(this->origin_translation, other.origin_translation);
std::swap(this->trafo_obj, other.trafo_obj);
std::swap(this->_bounding_box, other._bounding_box);
std::swap(this->_bounding_box_valid, other._bounding_box_valid);
std::swap(this->part_number, other.part_number);
@ -604,6 +604,13 @@ ModelObject::repair()
(*v)->mesh.repair();
}
Pointf3
ModelObject::origin_translation() const
{
return Pointf3(trafo_obj.m03, trafo_obj.m13, trafo_obj.m23);
}
// flattens all volumes and instances into a single mesh
TriangleMesh
ModelObject::mesh() const
@ -684,7 +691,6 @@ ModelObject::align_to_ground()
bb.merge(v->bounding_box());
this->translate(0, 0, -bb.min.z);
this->origin_translation.translate(0, 0, -bb.min.z);
}
void
@ -706,7 +712,6 @@ ModelObject::center_around_origin()
vector.y -= size.y/2;
this->translate(vector);
this->origin_translation.translate(vector);
if (!this->instances.empty()) {
for (ModelInstancePtrs::const_iterator i = this->instances.begin(); i != this->instances.end(); ++i) {
@ -760,8 +765,6 @@ ModelObject::scale(const Pointf3 &versor)
this->apply_transformation(trafo);
// reset origin translation since it doesn't make sense anymore
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}
@ -790,7 +793,6 @@ ModelObject::rotate(double angle, const Axis &axis)
this->apply_transformation(trafo);
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}
@ -805,7 +807,6 @@ ModelObject::rotate(double angle, const Vectorf3 &axis)
this->apply_transformation(trafo);
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}
@ -819,7 +820,6 @@ ModelObject::rotate(const Vectorf3 &origin, const Vectorf3 &target)
this->apply_transformation(trafo);
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}
@ -832,7 +832,6 @@ ModelObject::mirror(const Axis &axis)
this->apply_transformation(trafo);
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}
@ -849,6 +848,7 @@ TransformationMatrix ModelObject::get_undo_trafo() const
void
ModelObject::apply_transformation(const TransformationMatrix & trafo)
{
this->trafo_obj.applyLeft(trafo);
this->trafo_undo_stack.applyLeft(trafo);
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
(*v)->apply_transformation(trafo);
@ -886,7 +886,6 @@ ModelObject::transform_by_instance(ModelInstance instance, bool dont_translate)
for (ModelInstance* i : this->instances) {
i->set_complete_trafo(i->get_trafo_matrix().multiplyRight(temp_trafo));
}
this->origin_translation = Pointf3(0,0,0);
this->invalidate_bounding_box();
}

View File

@ -277,12 +277,6 @@ class ModelObject
int part_number; ///< It's used for the 3MF items part numbers in the build element.
LayerHeightSpline layer_height_spline; ///< Spline based variations of layer thickness for interactive user manipulation
Pointf3 origin_translation;
///< This vector accumulates the total translation applied to the object by the
///< center_around_origin() method. Callers might want to apply the same translation
///< to new volumes before adding them to this object in order to preserve alignment
///< when user expects that.
// these should be private but we need to expose them via XS until all methods are ported
BoundingBoxf3 _bounding_box;
bool _bounding_box_valid;
@ -336,6 +330,17 @@ class ModelObject
/// Repair all TriangleMesh objects found in each ModelVolume.
void repair();
Pointf3 origin_translation() const;
///< This vector returns the total translation applied to the object by the
///< transformation methods. Callers might want to apply the same translation
///< to new volumes before adding them to this object in order to preserve alignment
///< when user expects that.
TransformationMatrix get_trafo_obj() const {return this->trafo_obj;};
/// return a copy of the private trafo_obj, that accumulates all top-level transformations
void set_trafo_obj(TransformationMatrix const & trafo) {this->trafo_obj = trafo;};
/// Flatten all volumes and instances into a single mesh and applying all the ModelInstances transformations.
TriangleMesh mesh() const;
@ -465,6 +470,9 @@ class ModelObject
/// Trafo to collect the transformation applied to all volumes over a series of manipulations
TransformationMatrix trafo_undo_stack;
/// Trafo that accumulates all transformations applied to the whole object
TransformationMatrix trafo_obj;
/// = Operator overloading
/// \param other ModelObject the other ModelObject to be copied
/// \return ModelObject& the current ModelObject to enable operator cascading

View File

@ -4,15 +4,12 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 4;
use Test::More tests => 2;
{
my $model = Slic3r::Model->new;
my $object = $model->_add_object;
isa_ok $object, 'Slic3r::Model::Object::Ref';
isa_ok $object->origin_translation, 'Slic3r::Pointf3::Ref';
$object->origin_translation->translate(10,0,0);
is_deeply \@{$object->origin_translation}, [10,0,0], 'origin_translation is modified by ref';
my $lhr = [ [ 5, 10, 0.1 ] ];
$object->set_layer_height_ranges($lhr);

View File

@ -215,10 +215,14 @@ ModelMaterial::attributes()
void set_layer_height_spline(LayerHeightSpline* spline)
%code%{ THIS->layer_height_spline = *spline; %};
Ref<Pointf3> origin_translation()
%code%{ RETVAL = &THIS->origin_translation; %};
void set_origin_translation(Pointf3* point)
%code%{ THIS->origin_translation = *point; %};
Clone<Pointf3> origin_translation()
%code%{ RETVAL = THIS->origin_translation(); %};
Clone<TransformationMatrix> get_trafo_obj()
%code%{ RETVAL = THIS->get_trafo_obj(); %};
void set_trafo_obj(TransformationMatrix* trafo)
%code%{ THIS->set_trafo_obj(*trafo); %};
bool needed_repair() const;
int materials_count() const;