This commit is contained in:
Alessandro Ranellucci 2014-01-07 19:08:37 +01:00
parent c8a48b4527
commit 6da98a6ecc
3 changed files with 36 additions and 20 deletions

View File

@ -12,7 +12,7 @@ use Slic3r::Geometry qw(X Y Z MIN MAX triangle_normal normalize deg2rad tan);
use Wx::GLCanvas qw(:all); use Wx::GLCanvas qw(:all);
__PACKAGE__->mk_accessors( qw(quat dirty init mview_init __PACKAGE__->mk_accessors( qw(quat dirty init mview_init
object_center object_size object_bounding_box object_shift
volumes initpos volumes initpos
sphi stheta) ); sphi stheta) );
@ -28,15 +28,18 @@ sub new {
$self->sphi(45); $self->sphi(45);
$self->stheta(-45); $self->stheta(-45);
my $bounding_box = $object->raw_mesh->bounding_box; my $bb = $object->raw_mesh->bounding_box;
$self->object_center($bounding_box->center); my $center = $bb->center;
$self->object_size($bounding_box->size); $self->object_shift(Slic3r::Pointf3->new(-$center->x, -$center->y, -$bb->z_min)); #,,
$bb->translate(@{ $self->object_shift });
$self->object_bounding_box($bb);
# group mesh(es) by material # group mesh(es) by material
my @materials = (); my @materials = ();
$self->volumes([]); $self->volumes([]);
foreach my $volume (@{$object->volumes}) { foreach my $volume (@{$object->volumes}) {
my $mesh = $volume->mesh; my $mesh = $volume->mesh->clone;
$mesh->translate(@{ $self->object_shift });
my $material_id = $volume->material_id // '_'; my $material_id = $volume->material_id // '_';
my $color_idx = first { $materials[$_] eq $material_id } 0..$#materials; my $color_idx = first { $materials[$_] eq $material_id } 0..$#materials;
@ -297,7 +300,7 @@ sub ResetModelView {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
my $win_size = $self->GetClientSize(); my $win_size = $self->GetClientSize();
my $ratio = $factor * min($win_size->width, $win_size->height) / max(@{ $self->object_size }); my $ratio = $factor * min($win_size->width, $win_size->height) / max(@{ $self->object_bounding_box->size });
glScalef($ratio, $ratio, 1); glScalef($ratio, $ratio, 1);
} }
@ -312,8 +315,7 @@ sub Resize {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
my $object_size = $self->object_size; glOrtho(-$x/2, $x/2, -$y/2, $y/2, 0.5, 2 * max(@{ $self->object_bounding_box->size }));
glOrtho(-$x/2, $x/2, -$y/2, $y/2, 0.5, 2 * max(@$object_size));
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
unless ($self->mview_init) { unless ($self->mview_init) {
@ -366,37 +368,40 @@ sub Render {
glPushMatrix(); glPushMatrix();
my $object_size = $self->object_size; my $object_size = $self->object_bounding_box->size;
glTranslatef(0, 0, -max(@$object_size[0..1])); glTranslatef(0, 0, -max(@$object_size[0..1]));
my @rotmat = quat_to_rotmatrix($self->quat); my @rotmat = quat_to_rotmatrix($self->quat);
glMultMatrixd_p(@rotmat[0..15]); glMultMatrixd_p(@rotmat[0..15]);
glRotatef($self->stheta, 1, 0, 0); glRotatef($self->stheta, 1, 0, 0);
glRotatef($self->sphi, 0, 0, 1); glRotatef($self->sphi, 0, 0, 1);
glTranslatef(map -$_, @{ $self->object_center });
my $center = $self->object_bounding_box->center;
glTranslatef(-$center->x, -$center->y, -$center->z); #,,
$self->draw_mesh; $self->draw_mesh;
my $z0 = 0;
# draw axes # draw axes
{ {
my $axis_len = 2 * max(@{ $self->object_size }); my $axis_len = 2 * max(@{ $object_size });
glLineWidth(2); glLineWidth(2);
glBegin(GL_LINES); glBegin(GL_LINES);
# draw line for x axis # draw line for x axis
glColor3f(1, 0, 0); glColor3f(1, 0, 0);
glVertex3f(0, 0, 0); glVertex3f(0, 0, $z0);
glVertex3f($axis_len, 0, 0); glVertex3f($axis_len, 0, $z0);
# draw line for y axis # draw line for y axis
glColor3f(0, 1, 0); glColor3f(0, 1, 0);
glVertex3f(0, 0, 0); glVertex3f(0, 0, $z0);
glVertex3f(0, $axis_len, 0); glVertex3f(0, $axis_len, $z0);
# draw line for Z axis # draw line for Z axis
glColor3f(0, 0, 1); glColor3f(0, 0, 1);
glVertex3f(0, 0, 0); glVertex3f(0, 0, $z0);
glVertex3f(0, 0, $axis_len); glVertex3f(0, 0, $z0+$axis_len);
glEnd(); glEnd();
# draw ground # draw ground
my $ground_z = -0.02; my $ground_z = $z0-0.02;
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

@ -414,11 +414,17 @@ sub center_around_origin {
my $bb = $self->raw_mesh->bounding_box; my $bb = $self->raw_mesh->bounding_box;
# first align to origin on XYZ # first align to origin on XYZ
my @shift = @{ $bb->vector_to_origin }; my @shift = (
-$bb->x_min,
-$bb->y_min,
-$bb->z_min,
);
# then center it on XY # then center it on XY
my $size = $bb->size; my $size = $bb->size;
$shift[$_] -= $size->[$_]/2 for X,Y; $shift[X] -= $size->x/2;
$shift[Y] -= $size->y/2; #//
$shift[Z] -= $size->z/2;
$self->translate(@shift); $self->translate(@shift);

View File

@ -28,6 +28,11 @@ our @ISA = 'Slic3r::Point';
sub DESTROY {} sub DESTROY {}
package Slic3r::Pointf3;
use overload
'@{}' => sub { [ $_[0]->x, $_[0]->y, $_[0]->z ] }, #,
'fallback' => 1;
package Slic3r::ExPolygon; package Slic3r::ExPolygon;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },