mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-31 20:51:12 -06:00 
			
		
		
		
	Finished porting BoundingBox to XS
This commit is contained in:
		
							parent
							
								
									ea47f3b6e7
								
							
						
					
					
						commit
						b17d06f9d1
					
				
					 24 changed files with 160 additions and 262 deletions
				
			
		|  | @ -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; | ||||
|  |  | |||
|  | @ -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 ]) ]; | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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; | ||||
|  | @ -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; | ||||
|      | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Alessandro Ranellucci
						Alessandro Ranellucci