From b17d06f9d1fd7d8d1c1b9c176e876f17fc89aaaf Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Tue, 7 Jan 2014 12:48:09 +0100 Subject: [PATCH] Finished porting BoundingBox to XS --- lib/Slic3r.pm | 1 - lib/Slic3r/ExPolygon.pm | 8 -- lib/Slic3r/Fill/Base.pm | 4 +- lib/Slic3r/Fill/Flowsnake.pm | 2 +- lib/Slic3r/Fill/Honeycomb.pm | 6 +- lib/Slic3r/Fill/PlanePath.pm | 2 +- lib/Slic3r/Fill/Rectilinear.pm | 6 +- lib/Slic3r/Geometry/BoundingBox.pm | 170 ----------------------------- lib/Slic3r/Model.pm | 44 ++------ lib/Slic3r/Polyline.pm | 8 +- lib/Slic3r/Print.pm | 2 +- lib/Slic3r/Print/Object.pm | 13 ++- t/geometry.t | 2 +- t/print.t | 2 +- xs/src/BoundingBox.cpp | 56 ++++++++-- xs/src/BoundingBox.hpp | 22 ++-- xs/src/Model.hpp | 1 - xs/src/Point.cpp | 28 +++++ xs/src/Point.hpp | 4 + xs/t/17_boundingbox.t | 2 +- xs/xsp/BoundingBox.xsp | 22 +++- xs/xsp/Point.xsp | 13 +++ xs/xsp/my.map | 2 + xs/xsp/typemap.xspt | 2 + 24 files changed, 160 insertions(+), 262 deletions(-) delete mode 100644 lib/Slic3r/Geometry/BoundingBox.pm diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index f2bea526c..f7a594ea8 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -56,7 +56,6 @@ use Slic3r::GCode::Reader; use Slic3r::GCode::SpiralVase; use Slic3r::GCode::VibrationLimit; use Slic3r::Geometry qw(PI); -use Slic3r::Geometry::BoundingBox; use Slic3r::Geometry::Clipper; use Slic3r::Layer; use Slic3r::Layer::Region; diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 536ea3f94..83a31897e 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -209,14 +209,6 @@ sub _medial_axis_voronoi { package Slic3r::ExPolygon::Collection; use Slic3r::Geometry qw(X1 Y1); -sub align_to_origin { - my $self = shift; - - my @bb = Slic3r::Geometry::bounding_box([ map @$_, map @$_, @$self ]); - $self->translate(-$bb[X1], -$bb[Y1]); - $self; -} - sub size { my $self = shift; return [ Slic3r::Geometry::size_2D([ map @$_, map @$_, @$self ]) ]; diff --git a/lib/Slic3r/Fill/Base.pm b/lib/Slic3r/Fill/Base.pm index a4bd88d2a..348e22383 100644 --- a/lib/Slic3r/Fill/Base.pm +++ b/lib/Slic3r/Fill/Base.pm @@ -22,8 +22,8 @@ sub infill_direction { my (@rotate, @shift); $rotate[0] = Slic3r::Geometry::deg2rad($self->angle); $rotate[1] = $self->bounding_box - ? $self->bounding_box->center_2D - : $surface->expolygon->bounding_box->center_2D; + ? $self->bounding_box->center + : $surface->expolygon->bounding_box->center; @shift = @{$rotate[1]}; if (defined $self->layer_id) { diff --git a/lib/Slic3r/Fill/Flowsnake.pm b/lib/Slic3r/Fill/Flowsnake.pm index 6d3ee4947..c852b81c7 100644 --- a/lib/Slic3r/Fill/Flowsnake.pm +++ b/lib/Slic3r/Fill/Flowsnake.pm @@ -12,7 +12,7 @@ sub process_polyline { my $self = shift; my ($polyline, $bounding_box) = @_; - $_->[X] += $bounding_box->center_2D->[X] for @$polyline; + $_->[X] += $bounding_box->center->[X] for @$polyline; } 1; diff --git a/lib/Slic3r/Fill/Honeycomb.pm b/lib/Slic3r/Fill/Honeycomb.pm index 1e6df2742..e5c515bcc 100644 --- a/lib/Slic3r/Fill/Honeycomb.pm +++ b/lib/Slic3r/Fill/Honeycomb.pm @@ -47,8 +47,10 @@ sub fill_surface { # extend bounding box so that our pattern will be aligned with other layers # $bounding_box->[X1] and [Y1] represent the displacement between new bounding box offset and old one - $bounding_box->extents->[X][MIN] -= $bounding_box->x_min % $m->{hex_width}; - $bounding_box->extents->[Y][MIN] -= $bounding_box->y_min % $m->{pattern_height}; + $bounding_box->merge_point(Slic3r::Point->new( + $bounding_box->x_min - ($bounding_box->x_min % $m->{hex_width}), + $bounding_box->y_min - ($bounding_box->y_min % $m->{pattern_height}), + )); } my $x = $bounding_box->x_min; diff --git a/lib/Slic3r/Fill/PlanePath.pm b/lib/Slic3r/Fill/PlanePath.pm index d393f9049..4c2d0e387 100644 --- a/lib/Slic3r/Fill/PlanePath.pm +++ b/lib/Slic3r/Fill/PlanePath.pm @@ -33,7 +33,7 @@ sub fill_surface { (ref $self) =~ /::([^:]+)$/; my $path = "Math::PlanePath::$1"->new; - my @n = $self->get_n($path, [ map +($_ / $distance_between_lines), @{$bounding_box->bb} ]); + my @n = $self->get_n($path, [ map +($_ / $distance_between_lines), @{$bounding_box->min_point}, @{$bounding_box->max_point} ]); my $polyline = Slic3r::Polyline->new( map [ map {$_*$distance_between_lines} $path->n_to_xy($_) ], @n, diff --git a/lib/Slic3r/Fill/Rectilinear.pm b/lib/Slic3r/Fill/Rectilinear.pm index acf7c1090..854d15fe4 100644 --- a/lib/Slic3r/Fill/Rectilinear.pm +++ b/lib/Slic3r/Fill/Rectilinear.pm @@ -38,8 +38,10 @@ sub fill_surface { ); } else { # extend bounding box so that our pattern will be aligned with other layers - $bounding_box->extents->[X][MIN] -= $bounding_box->x_min % $line_spacing; - $bounding_box->extents->[Y][MIN] -= $bounding_box->y_min % $line_spacing; + $bounding_box->merge_point(Slic3r::Point->new( + $bounding_box->x_min - ($bounding_box->x_min % $line_spacing), + $bounding_box->y_min - ($bounding_box->y_min % $line_spacing), + )); } # generate the basic pattern diff --git a/lib/Slic3r/Geometry/BoundingBox.pm b/lib/Slic3r/Geometry/BoundingBox.pm deleted file mode 100644 index 2b8cae358..000000000 --- a/lib/Slic3r/Geometry/BoundingBox.pm +++ /dev/null @@ -1,170 +0,0 @@ -package Slic3r::Geometry::BoundingBox; -use Moo; - -use List::Util qw(min max); -use Slic3r::Geometry qw(X Y Z MIN MAX X1 Y1 X2 Y2 Z1 Z2); -use Storable qw(); - -has 'extents' => (is => 'ro', required => 1); - -sub clone { Storable::dclone($_[0]) } - -# 2D -sub new_from_points { - my $class = shift; - my ($points) = @_; - - my $bb = [ Slic3r::Geometry::bounding_box($points) ]; - return $class->new(extents => [ - [ $bb->[X1], $bb->[X2] ], - [ $bb->[Y1], $bb->[Y2] ], - ]); -} - -# 2D/3D -sub new_from_bb { - my $class = shift; - my ($bb) = @_; - - return $class->new(extents => [ - [ $bb->[X1], $bb->[X2] ], - [ $bb->[Y1], $bb->[Y2] ], - (@$bb == 6) ? [ $bb->[Z1], $bb->[Z2] ] : (), - ]); -} - -sub merge { - my $class = shift; - my (@bounding_boxes) = @_; - - my $self = ref($class) - ? $class - : shift @bounding_boxes; - - foreach my $bounding_box (@bounding_boxes) { - for my $axis (X .. $#{$self->extents}) { - $self->extents->[$axis][MIN] = min($self->extents->[$axis][MIN], $bounding_box->extents->[$axis][MIN]); - $self->extents->[$axis][MAX] = max($self->extents->[$axis][MAX], $bounding_box->extents->[$axis][MAX]); - } - } - - return $self; -} - -# four-arguments 2D bb -sub bb { - my $self = shift; - my $extents = $self->extents; - return [ $extents->[X][MIN], $extents->[Y][MIN], $extents->[X][MAX], $extents->[Y][MAX] ]; -} - -sub polygon { - my $self = shift; - - my $e = $self->extents; - return Slic3r::Polygon->new( - [ $e->[X][MIN], $e->[Y][MIN] ], - [ $e->[X][MAX], $e->[Y][MIN] ], - [ $e->[X][MAX], $e->[Y][MAX] ], - [ $e->[X][MIN], $e->[Y][MAX] ], - ); -} - -# note to $self -sub rotate { - die "Rotating an axis-aligned bounding box doesn't make any sense"; -} - -sub scale { - my $self = shift; - my ($factor) = @_; - - for (@{$self->extents}) { - $_ *= $factor for @$_[MIN,MAX]; - } - - $self; -} - -sub translate { - my $self = shift; - my @shift = @_; - - for my $axis (X .. $#{$self->extents}) { - $self->extents->[$axis][MIN] += $shift[$axis]; - $self->extents->[$axis][MAX] += $shift[$axis]; - } - - $self; -} - -sub size { - my $self = shift; - - my $extents = $self->extents; - return [ map $extents->[$_][MAX] - $extents->[$_][MIN], grep $extents->[$_], (X,Y,Z) ]; -} - -sub center { - my $self = shift; - - my $extents = $self->extents; - return [ map +($extents->[$_][MAX] + $extents->[$_][MIN])/2, grep $extents->[$_], (X,Y,Z) ]; -} - -sub center_2D { - my $self = shift; - return Slic3r::Point->new(@{$self->center}[X,Y]); -} - -sub min_point { - my $self = shift; - return Slic3r::Point->new($self->extents->[X][MIN], $self->extents->[Y][MIN]); -} - -sub min_point3 { - my $self = shift; - return [ map $self->extents->[$_][MIN], (X,Y,Z) ]; -} - -sub vector_to_origin { - my $self = shift; - return [ map -$_, @{$self->min_point3} ]; -} - -sub max_point { - my $self = shift; - return Slic3r::Point->new($self->extents->[X][MAX], $self->extents->[Y][MAX]); -} - -sub x_min { - my $self = shift; - return $self->extents->[X][MIN]; -} - -sub x_max { - my $self = shift; - return $self->extents->[X][MAX]; -} - -sub y_min { - my $self = shift; - return $self->extents->[Y][MIN]; -} - -sub y_max { - my $self = shift; - return $self->extents->[Y][MAX]; -} - -sub z_min { - my $self = shift; - return $self->extents->[Z][MIN]; -} - -sub z_max { - my $self = shift; - return $self->extents->[Z][MAX]; -} - -1; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 129f6fc27..1c0f54250 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -186,8 +186,8 @@ sub _arrange { return Slic3r::Geometry::arrange( scalar(@$sizes), # number of parts - max(map $_->[X], @$sizes), # cell width - max(map $_->[Y], @$sizes), # cell height + max(map $_->x, @$sizes), # cell width + max(map $_->y, @$sizes), # cell height , $distance, # distance between cells $bb, # bounding box of the area to fill (can be undef) ); @@ -201,28 +201,11 @@ sub has_objects_with_no_instances { # this returns the bounding box of the *transformed* instances sub bounding_box { my $self = shift; - return Slic3r::Geometry::BoundingBox->merge(map $_->bounding_box, @{ $self->objects }); -} - -sub align_to_origin { - my $self = shift; - # calculate the displacements needed to - # have lowest value for each axis at coordinate 0 - { - my $bb = $self->bounding_box; - $self->translate(map -$bb->extents->[$_][MIN], X,Y,Z); - } - - # align all instances to 0,0 as well - { - my @instances = map @{$_->instances}, @{$self->objects}; - my @extents = Slic3r::Geometry::bounding_box_3D([ map $_->offset, @instances ]); - foreach my $instance (@instances) { - $instance->offset->[X] -= $extents[X][MIN]; - $instance->offset->[Y] -= $extents[Y][MIN]; - } - } + return undef if !@{$self->objects}; + my $bb = $self->objects->[0]->bounding_box; + $bb->merge($_->bounding_box) for @{$self->objects}[1..$#{$self->objects}]; + return $bb; } # input point is expressed in unscaled coordinates @@ -234,8 +217,8 @@ sub center_instances_around_point { my $size = $bb->size; my @shift = ( - -$bb->x_min + $point->[X] - $size->[X]/2, - -$bb->y_min + $point->[Y] - $size->[Y]/2, + -$bb->x_min + $point->[X] - $size->x/2, + -$bb->y_min + $point->[Y] - $size->y/2, #// ); foreach my $object (@{$self->objects}) { @@ -422,17 +405,6 @@ sub instance_bounding_box { return $mesh->bounding_box; } -sub align_to_origin { - my $self = shift; - - # calculate the displacements needed to - # have lowest value for each axis at coordinate 0 - my $bb = $self->bounding_box; - my @shift = map -$bb->extents->[$_][MIN], X,Y,Z; - $self->translate(@shift); - return @shift; -} - sub center_around_origin { my $self = shift; diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 33234109f..a75f85379 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -19,7 +19,7 @@ sub wkt { sub bounding_box { my $self = shift; - return Slic3r::Geometry::BoundingBox->new_from_points($self); + return Slic3r::Geometry::BoundingBox->new_from_points([ @$self ]); } sub size { @@ -27,10 +27,4 @@ sub size { return [ Slic3r::Geometry::size_2D($self) ]; } -sub align_to_origin { - my $self = shift; - my $bb = $self->bounding_box; - return $self->translate(-$bb->x_min, -$bb->y_min); -} - 1; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index e24070354..c791bf8e4 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -260,7 +260,7 @@ sub validate { { my @object_height = (); foreach my $object (@{$self->objects}) { - my $height = $object->size->[Z]; + my $height = $object->size->z; push @object_height, $height for @{$object->copies}; } @object_height = sort { $a <=> $b } @object_height; diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index bf196c100..2fee618ac 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -34,7 +34,11 @@ sub BUILD { map @$_, grep defined $_, @{$self->region_volumes}; - my $bb = Slic3r::Geometry::BoundingBox->merge(map $_->bounding_box, @meshes); + + my $bb = @meshes + ? $meshes[0]->bounding_box + : Slic3r::Geometry::BoundingBoxf3->new; + $bb->merge($_->bounding_box) for @meshes[1..$#meshes]; # Translate meshes so that our toolpath generation algorithms work with smaller # XY coordinates; this translation is an optimization and not strictly required. @@ -103,7 +107,10 @@ sub bounding_box { my $self = shift; # since the object is aligned to origin, bounding box coincides with size - return Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_[X,Y]), [0,0], $self->size ]); + return Slic3r::Geometry::BoundingBox->new_from_points([ + Slic3r::Point->new(0,0), + map Slic3r::Point->new($_->x, $_->y), $self->size #)) + ]); } # this should be idempotent @@ -126,7 +133,7 @@ sub slice { } # loop until we have at least one layer and the max slice_z reaches the object height - my $max_z = unscale $self->size->[Z]; + my $max_z = unscale($self->size->z); while (!@{$self->layers} || ($slice_z - $height) <= $max_z) { # assign the default height to the layer according to the general settings $height = ($id == 0) diff --git a/t/geometry.t b/t/geometry.t index 910535586..7322ccf66 100644 --- a/t/geometry.t +++ b/t/geometry.t @@ -146,7 +146,7 @@ my $polygons = [ { my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_), [0, 1], [10, 2], [20, 2] ]); $bb->scale(2); - is_deeply $bb->extents, [ [0,40], [2,4] ], 'bounding box is scaled correctly'; + is_deeply [ $bb->min_point->pp, $bb->max_point->pp ], [ [0,2], [40,4] ], 'bounding box is scaled correctly'; } #========================================================== diff --git a/t/print.t b/t/print.t index a24f4742d..b4ff20145 100644 --- a/t/print.t +++ b/t/print.t @@ -25,7 +25,7 @@ use Slic3r::Test; } }); my $bb = Slic3r::Geometry::BoundingBox->new_from_points(\@extrusion_points); - my $center = $bb->center_2D; + my $center = $bb->center; ok abs(unscale($center->[X]) - $config->print_center->[X]) < epsilon, 'print is centered around print_center (X)'; ok abs(unscale($center->[Y]) - $config->print_center->[Y]) < epsilon, 'print is centered around print_center (Y)'; } diff --git a/xs/src/BoundingBox.cpp b/xs/src/BoundingBox.cpp index f45050278..f268ea1b9 100644 --- a/xs/src/BoundingBox.cpp +++ b/xs/src/BoundingBox.cpp @@ -6,23 +6,30 @@ namespace Slic3r { template BoundingBoxBase::BoundingBoxBase(const std::vector points) { - for (typename std::vector::const_iterator it = points.begin(); it != points.end(); ++it) { + typename std::vector::const_iterator it = points.begin(); + this->min.x = this->max.x = it->x; + this->min.y = this->max.y = it->y; + for (++it; it != points.end(); ++it) { this->min.x = std::min(it->x, this->min.x); this->min.y = std::min(it->y, this->min.y); this->max.x = std::max(it->x, this->max.x); this->max.y = std::max(it->y, this->max.y); } } +template BoundingBoxBase::BoundingBoxBase(const std::vector points); template BoundingBox3Base::BoundingBox3Base(const std::vector points) : BoundingBoxBase(points) { - for (typename std::vector::const_iterator it = points.begin(); it != points.end(); ++it) { + typename std::vector::const_iterator it = points.begin(); + this->min.z = this->max.z = it->z; + for (++it; it != points.end(); ++it) { this->min.z = std::min(it->z, this->min.z); this->max.z = std::max(it->z, this->max.z); } } +template BoundingBox3Base::BoundingBox3Base(const std::vector points); void BoundingBox::polygon(Polygon* polygon) const @@ -45,6 +52,18 @@ BoundingBoxBase::scale(double factor) this->min.scale(factor); this->max.scale(factor); } +template void BoundingBoxBase::scale(double factor); +template void BoundingBoxBase::scale(double factor); + +template void +BoundingBoxBase::merge(const PointClass &point) +{ + this->min.x = std::min(point.x, this->min.x); + this->min.y = std::min(point.y, this->min.y); + this->max.x = std::max(point.x, this->max.x); + this->max.y = std::max(point.y, this->max.y); +} +template void BoundingBoxBase::merge(const Point &point); template void BoundingBoxBase::merge(const BoundingBoxBase &bb) @@ -54,6 +73,16 @@ BoundingBoxBase::merge(const BoundingBoxBase &bb) this->max.x = std::max(bb.max.x, this->max.x); this->max.y = std::max(bb.max.y, this->max.y); } +template void BoundingBoxBase::merge(const BoundingBoxBase &bb); + +template void +BoundingBox3Base::merge(const PointClass &point) +{ + BoundingBoxBase::merge(point); + this->min.z = std::min(point.z, this->min.z); + this->max.z = std::max(point.z, this->max.z); +} +template void BoundingBox3Base::merge(const Pointf3 &point); template void BoundingBox3Base::merge(const BoundingBox3Base &bb) @@ -62,25 +91,29 @@ BoundingBox3Base::merge(const BoundingBox3Base &bb) this->min.z = std::min(bb.min.z, this->min.z); this->max.z = std::max(bb.max.z, this->max.z); } +template void BoundingBox3Base::merge(const BoundingBox3Base &bb); template PointClass -BoundingBox2Base::size() const +BoundingBoxBase::size() const { return PointClass(this->max.x - this->min.x, this->max.y - this->min.y); } +template Point BoundingBoxBase::size() const; template PointClass BoundingBox3Base::size() const { return PointClass(this->max.x - this->min.x, this->max.y - this->min.y, this->max.z - this->min.z); } +template Pointf3 BoundingBox3Base::size() const; template void -BoundingBox2Base::translate(coordf_t x, coordf_t y) +BoundingBoxBase::translate(coordf_t x, coordf_t y) { this->min.translate(x, y); this->max.translate(x, y); } +template void BoundingBoxBase::translate(coordf_t x, coordf_t y); template void BoundingBox3Base::translate(coordf_t x, coordf_t y, coordf_t z) @@ -88,24 +121,27 @@ BoundingBox3Base::translate(coordf_t x, coordf_t y, coordf_t z) this->min.translate(x, y, z); this->max.translate(x, y, z); } +template void BoundingBox3Base::translate(coordf_t x, coordf_t y, coordf_t z); template PointClass -BoundingBox2Base::center() const +BoundingBoxBase::center() const { return PointClass( - (this->max.x - this->min.x)/2, - (this->max.y - this->min.y)/2 + (this->max.x + this->min.x)/2, + (this->max.y + this->min.y)/2 ); } +template Point BoundingBoxBase::center() const; template PointClass BoundingBox3Base::center() const { return PointClass( - (this->max.x - this->min.x)/2, - (this->max.y - this->min.y)/2, - (this->max.z - this->min.z)/2 + (this->max.x + this->min.x)/2, + (this->max.y + this->min.y)/2, + (this->max.z + this->min.z)/2 ); } +template Pointf3 BoundingBox3Base::center() const; } diff --git a/xs/src/BoundingBox.hpp b/xs/src/BoundingBox.hpp index e0092c6dc..8623b4909 100644 --- a/xs/src/BoundingBox.hpp +++ b/xs/src/BoundingBox.hpp @@ -19,18 +19,11 @@ class BoundingBoxBase PointClass min; PointClass max; - BoundingBoxBase(); + BoundingBoxBase() {}; BoundingBoxBase(const std::vector points); + void merge(const PointClass &point); void merge(const BoundingBoxBase &bb); void scale(double factor); -}; - -template -class BoundingBox2Base : public BoundingBoxBase -{ - public: - BoundingBox2Base(); - BoundingBox2Base(const std::vector points) : BoundingBoxBase(points) {}; PointClass size() const; void translate(coordf_t x, coordf_t y); PointClass center() const; @@ -40,25 +33,28 @@ template class BoundingBox3Base : public BoundingBoxBase { public: - BoundingBox3Base(); + BoundingBox3Base() {}; BoundingBox3Base(const std::vector points); + void merge(const PointClass &point); void merge(const BoundingBox3Base &bb); PointClass size() const; void translate(coordf_t x, coordf_t y, coordf_t z); PointClass center() const; }; -class BoundingBox : public BoundingBox2Base +class BoundingBox : public BoundingBoxBase { public: void polygon(Polygon* polygon) const; BoundingBox() {}; - BoundingBox(const Points points) : BoundingBox2Base(points) {}; + BoundingBox(const Points points) : BoundingBoxBase(points) {}; }; -class BoundingBoxf : public BoundingBox2Base {}; +/* +class BoundingBoxf : public BoundingBoxBase {}; class BoundingBox3 : public BoundingBox3Base {}; +*/ class BoundingBoxf3 : public BoundingBox3Base { public: diff --git a/xs/src/Model.hpp b/xs/src/Model.hpp index 28dcc6008..6e16e8faa 100644 --- a/xs/src/Model.hpp +++ b/xs/src/Model.hpp @@ -77,7 +77,6 @@ class ModelObject void raw_mesh(TriangleMesh* mesh) const; void mesh(TriangleMesh* mesh) const; void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const; - void align_to_origin(); void center_around_origin(); void translate(coordf_t x, coordf_t y, coordf_t z); size_t materials_count() const; diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp index d35b3f0f8..b6742a9d2 100644 --- a/xs/src/Point.cpp +++ b/xs/src/Point.cpp @@ -193,4 +193,32 @@ Pointf::from_SV(SV* point_sv) } #endif +void +Pointf::scale(double factor) +{ + this->x *= factor; + this->y *= factor; +} + +void +Pointf::translate(double x, double y) +{ + this->x += x; + this->y += y; +} + +void +Pointf3::scale(double factor) +{ + Pointf::scale(factor); + this->z *= factor; +} + +void +Pointf3::translate(double x, double y, double z) +{ + Pointf::translate(x, y); + this->z += z; +} + } diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp index 0ca93bf66..9e621b603 100644 --- a/xs/src/Point.hpp +++ b/xs/src/Point.hpp @@ -57,6 +57,8 @@ class Pointf coordf_t x; coordf_t y; explicit Pointf(coordf_t _x = 0, coordf_t _y = 0): x(_x), y(_y) {}; + void scale(double factor); + void translate(double x, double y); #ifdef SLIC3RXS void from_SV(SV* point_sv); @@ -69,6 +71,8 @@ class Pointf3 : public Pointf public: coordf_t z; explicit Pointf3(coordf_t _x = 0, coordf_t _y = 0, coordf_t _z = 0): Pointf(_x, _y), z(_z) {}; + void scale(double factor); + void translate(double x, double y, double z); }; } diff --git a/xs/t/17_boundingbox.t b/xs/t/17_boundingbox.t index 28e74dd00..819aefbd2 100644 --- a/xs/t/17_boundingbox.t +++ b/xs/t/17_boundingbox.t @@ -12,7 +12,7 @@ use Test::More tests => 3; Slic3r::Point->new(500, -600), ); my $bb = Slic3r::Geometry::BoundingBox->new_from_points(\@points); - isa_ok $flow, 'Slic3r::Geometry::BoundingBox', 'new_from_points'; + isa_ok $bb, 'Slic3r::Geometry::BoundingBox', 'new_from_points'; is_deeply $bb->min_point->pp, [100,-600], 'min_point'; is_deeply $bb->max_point->pp, [500,200], 'max_point'; } diff --git a/xs/xsp/BoundingBox.xsp b/xs/xsp/BoundingBox.xsp index 5e496383d..f6d229493 100644 --- a/xs/xsp/BoundingBox.xsp +++ b/xs/xsp/BoundingBox.xsp @@ -11,6 +11,7 @@ BoundingBox* clone() %code{% const char* CLASS = "Slic3r::Geometry::BoundingBox"; RETVAL = new BoundingBox(*THIS); %}; void merge(BoundingBox* bb) %code{% THIS->merge(*bb); %}; + void merge_point(Point* point) %code{% THIS->merge(*point); %}; void scale(double factor); void translate(double x, double y); Polygon* polygon() @@ -31,7 +32,7 @@ %{ BoundingBox* -BoundingBox::new_from_points(CLASS, points) +new_from_points(CLASS, points) char* CLASS Points points CODE: @@ -41,3 +42,22 @@ BoundingBox::new_from_points(CLASS, points) %} }; + +%name{Slic3r::Geometry::BoundingBoxf3} class BoundingBoxf3 { + ~BoundingBoxf3(); + BoundingBoxf3* clone() + %code{% const char* CLASS = "Slic3r::Geometry::BoundingBoxf3"; RETVAL = new BoundingBoxf3(*THIS); %}; + void merge(BoundingBoxf3* bb) %code{% THIS->merge(*bb); %}; + void scale(double factor); + void translate(double x, double y, double z); + Pointf3* size() + %code{% const char* CLASS = "Slic3r::Pointf3"; RETVAL = new Pointf3(THIS->size()); %}; + Pointf3* center() + %code{% const char* CLASS = "Slic3r::Pointf3"; RETVAL = new Pointf3(THIS->center()); %}; + double x_min() %code{% RETVAL = THIS->min.x; %}; + double x_max() %code{% RETVAL = THIS->max.x; %}; + double y_min() %code{% RETVAL = THIS->min.y; %}; + double y_max() %code{% RETVAL = THIS->max.y; %}; + double z_min() %code{% RETVAL = THIS->min.z; %}; + double z_max() %code{% RETVAL = THIS->max.z; %}; +}; diff --git a/xs/xsp/Point.xsp b/xs/xsp/Point.xsp index ac0eb8dfc..453afd30a 100644 --- a/xs/xsp/Point.xsp +++ b/xs/xsp/Point.xsp @@ -49,3 +49,16 @@ Point::coincides_with(point_sv) %} }; + +%name{Slic3r::Pointf3} class Pointf3 { + Pointf3(double _x = 0, double _y = 0, double _z = 0); + ~Pointf3(); + Pointf3* clone() + %code{% const char* CLASS = "Slic3r::Pointf3"; RETVAL = new Pointf3(*THIS); %}; + double x() + %code{% RETVAL = THIS->x; %}; + double y() + %code{% RETVAL = THIS->y; %}; + double z() + %code{% RETVAL = THIS->z; %}; +}; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index c12d341fa..0b4645656 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -11,6 +11,7 @@ FullPrintConfig* O_OBJECT ZTable* O_OBJECT TriangleMesh* O_OBJECT Point* O_OBJECT +Pointf3* O_OBJECT Line* O_OBJECT Polyline* O_OBJECT PolylineCollection* O_OBJECT @@ -34,6 +35,7 @@ ClipperLib::PolyFillType T_UV # we return these types whenever we want the items to be cloned Points T_ARRAYREF +Pointfs T_ARRAYREF Lines T_ARRAYREF Polygons T_ARRAYREF Polylines T_ARRAYREF diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 4e351d43a..7fc2cfdc9 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -8,6 +8,7 @@ %typemap{SV*}; %typemap{AV*}; %typemap{Point*}; +%typemap{Pointf3*}; %typemap{BoundingBox*}; %typemap{BoundingBoxf3*}; %typemap{DynamicPrintConfig*}; @@ -25,6 +26,7 @@ %typemap{ExtrusionPath*}; %typemap{ExtrusionLoop*}; %typemap{Points}; +%typemap{Pointfs}; %typemap{Lines}; %typemap{Polygons}; %typemap{Polylines};