2DToolpaths.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. # 2D preview of the tool paths of a single layer, using a thin line.
  2. # OpenGL is used to render the paths.
  3. # Vojtech also added a 2D simulation of under/over extrusion in a single layer.
  4. package Slic3r::GUI::Plater::2DToolpaths;
  5. use strict;
  6. use warnings;
  7. use utf8;
  8. use Slic3r::Print::State ':steps';
  9. use Wx qw(:misc :sizer :slider :statictext wxWHITE);
  10. use Wx::Event qw(EVT_SLIDER EVT_KEY_DOWN);
  11. use base qw(Wx::Panel Class::Accessor);
  12. __PACKAGE__->mk_accessors(qw(print enabled));
  13. sub new {
  14. my $class = shift;
  15. my ($parent, $print) = @_;
  16. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition);
  17. $self->SetBackgroundColour(wxWHITE);
  18. # init GUI elements
  19. my $canvas = $self->{canvas} = Slic3r::GUI::Plater::2DToolpaths::Canvas->new($self, $print);
  20. my $slider = $self->{slider} = Wx::Slider->new(
  21. $self, -1,
  22. 0, # default
  23. 0, # min
  24. # we set max to a bogus non-zero value because the MSW implementation of wxSlider
  25. # will skip drawing the slider if max <= min:
  26. 1, # max
  27. wxDefaultPosition,
  28. wxDefaultSize,
  29. wxVERTICAL | wxSL_INVERSE,
  30. );
  31. my $z_label = $self->{z_label} = Wx::StaticText->new($self, -1, "", wxDefaultPosition,
  32. [40,-1], wxALIGN_CENTRE_HORIZONTAL);
  33. $z_label->SetFont($Slic3r::GUI::small_font);
  34. my $vsizer = Wx::BoxSizer->new(wxVERTICAL);
  35. $vsizer->Add($slider, 1, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
  36. $vsizer->Add($z_label, 0, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
  37. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  38. $sizer->Add($canvas, 1, wxALL | wxEXPAND, 0);
  39. $sizer->Add($vsizer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
  40. EVT_SLIDER($self, $slider, sub {
  41. $self->set_z($self->{layers_z}[$slider->GetValue])
  42. if $self->enabled;
  43. });
  44. EVT_KEY_DOWN($canvas, sub {
  45. my ($s, $event) = @_;
  46. my $key = $event->GetKeyCode;
  47. if ($key == 85 || $key == 315) {
  48. $slider->SetValue($slider->GetValue + 1);
  49. $self->set_z($self->{layers_z}[$slider->GetValue]);
  50. } elsif ($key == 68 || $key == 317) {
  51. $slider->SetValue($slider->GetValue - 1);
  52. $self->set_z($self->{layers_z}[$slider->GetValue]);
  53. } else {
  54. $event->Skip;
  55. }
  56. });
  57. $self->SetSizer($sizer);
  58. $self->SetMinSize($self->GetSize);
  59. $sizer->SetSizeHints($self);
  60. # init print
  61. $self->{print} = $print;
  62. $self->reload_print;
  63. return $self;
  64. }
  65. sub reload_print {
  66. my ($self) = @_;
  67. # we require that there's at least one object and the posSlice step
  68. # is performed on all of them (this ensures that _shifted_copies was
  69. # populated and we know the number of layers)
  70. if (!$self->print->object_step_done(STEP_SLICE)) {
  71. $self->enabled(0);
  72. $self->{slider}->Hide;
  73. $self->{canvas}->Refresh; # clears canvas
  74. return;
  75. }
  76. $self->{canvas}->bb($self->print->total_bounding_box);
  77. $self->{canvas}->_dirty(1);
  78. my %z = (); # z => 1
  79. foreach my $object (@{$self->{print}->objects}) {
  80. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  81. $z{$layer->print_z} = 1;
  82. }
  83. }
  84. $self->enabled(1);
  85. $self->{layers_z} = [ sort { $a <=> $b } keys %z ];
  86. $self->{slider}->SetRange(0, scalar(@{$self->{layers_z}})-1);
  87. if ((my $z_idx = $self->{slider}->GetValue) <= $#{$self->{layers_z}}) {
  88. $self->set_z($self->{layers_z}[$z_idx]);
  89. } else {
  90. $self->{slider}->SetValue(0);
  91. $self->set_z($self->{layers_z}[0]) if @{$self->{layers_z}};
  92. }
  93. $self->{slider}->Show;
  94. $self->Layout;
  95. }
  96. sub set_z {
  97. my ($self, $z) = @_;
  98. return if !$self->enabled;
  99. $self->{z_label}->SetLabel(sprintf '%.2f', $z);
  100. $self->{canvas}->set_z($z);
  101. }
  102. package Slic3r::GUI::Plater::2DToolpaths::Canvas;
  103. use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
  104. use OpenGL qw(:glconstants :glfunctions :glufunctions :gluconstants);
  105. use base qw(Wx::GLCanvas Class::Accessor);
  106. use Wx::GLCanvas qw(:all);
  107. use List::Util qw(min max first);
  108. use Slic3r::Geometry qw(scale unscale epsilon X Y);
  109. use Slic3r::Print::State ':steps';
  110. __PACKAGE__->mk_accessors(qw(
  111. print z layers color init
  112. bb
  113. _camera_bb
  114. _dirty
  115. _zoom
  116. _camera_target
  117. _drag_start_xy
  118. ));
  119. # make OpenGL::Array thread-safe
  120. {
  121. no warnings 'redefine';
  122. *OpenGL::Array::CLONE_SKIP = sub { 1 };
  123. }
  124. sub new {
  125. my ($class, $parent, $print) = @_;
  126. my $self = (Wx::wxVERSION >= 3.000003) ?
  127. # The wxWidgets 3.0.3-beta have a bug, they crash with NULL attribute list.
  128. $class->SUPER::new($parent, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, 0, "",
  129. [WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 24, 0]) :
  130. $class->SUPER::new($parent);
  131. # Immediatelly force creation of the OpenGL context to consume the static variable s_wglContextAttribs.
  132. $self->GetContext();
  133. $self->print($print);
  134. $self->_zoom(1);
  135. # 2D point in model space
  136. $self->_camera_target(Slic3r::Pointf->new(0,0));
  137. EVT_PAINT($self, sub {
  138. my $dc = Wx::PaintDC->new($self);
  139. $self->Render($dc);
  140. });
  141. EVT_SIZE($self, sub { $self->_dirty(1) });
  142. EVT_IDLE($self, sub {
  143. return unless $self->_dirty;
  144. return if !$self->IsShownOnScreen;
  145. $self->Resize;
  146. $self->Refresh;
  147. });
  148. EVT_MOUSEWHEEL($self, sub {
  149. my ($self, $e) = @_;
  150. return if !$self->GetParent->enabled;
  151. my $old_zoom = $self->_zoom;
  152. # Calculate the zoom delta and apply it to the current zoom factor
  153. my $zoom = -$e->GetWheelRotation() / $e->GetWheelDelta();
  154. if ($Slic3r::GUI::Settings->{_}{invert_zoom}) {
  155. $zoom *= -1;
  156. }
  157. $zoom = max(min($zoom, 4), -4);
  158. $zoom /= 10;
  159. $self->_zoom($self->_zoom / (1-$zoom));
  160. $self->_zoom(1) if $self->_zoom > 1; # prevent from zooming out too much
  161. {
  162. # In order to zoom around the mouse point we need to translate
  163. # the camera target. This math is almost there but not perfect yet...
  164. my $camera_bb_size = $self->_camera_bb->size;
  165. my $size = Slic3r::Pointf->new($self->GetSizeWH);
  166. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  167. # calculate the zooming center in pixel coordinates relative to the viewport center
  168. my $vec = Slic3r::Pointf->new($pos->x - $size->x/2, $pos->y - $size->y/2); #-
  169. # calculate where this point will end up after applying the new zoom
  170. my $vec2 = $vec->clone;
  171. $vec2->scale($old_zoom / $self->_zoom);
  172. # move the camera target by the difference of the two positions
  173. $self->_camera_target->translate(
  174. -($vec->x - $vec2->x) * $camera_bb_size->x / $size->x,
  175. ($vec->y - $vec2->y) * $camera_bb_size->y / $size->y, #//
  176. );
  177. }
  178. $self->_dirty(1);
  179. $self->Refresh;
  180. });
  181. EVT_MOUSE_EVENTS($self, \&mouse_event);
  182. return $self;
  183. }
  184. sub zoom{
  185. my($self, $direction) = @_;
  186. if( $direction eq 'in'){
  187. $self->_zoom($self->_zoom / (1+0.3));
  188. }
  189. elsif($direction eq 'out'){
  190. $self->_zoom($self->_zoom / (1-0.3));
  191. $self->_zoom(1) if $self->_zoom > 1; # prevent from zooming out too much
  192. }
  193. #apply changes
  194. $self->_dirty(1);
  195. $self->Refresh;
  196. }
  197. sub mouse_event {
  198. my ($self, $e) = @_;
  199. return if !$self->GetParent->enabled;
  200. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  201. if ($e->Entering && &Wx::wxMSW) {
  202. # wxMSW needs focus in order to catch mouse wheel events
  203. $self->SetFocus;
  204. } elsif ($e->Dragging) {
  205. if ($e->LeftIsDown || $e->MiddleIsDown || $e->RightIsDown) {
  206. # if dragging, translate view
  207. if (defined $self->_drag_start_xy) {
  208. my $move = $self->_drag_start_xy->vector_to($pos); # in pixels
  209. # get viewport and camera size in order to convert pixel to model units
  210. my ($x, $y) = $self->GetSizeWH;
  211. my $camera_bb_size = $self->_camera_bb->size;
  212. # compute translation in model units
  213. $self->_camera_target->translate(
  214. -$move->x * $camera_bb_size->x / $x,
  215. $move->y * $camera_bb_size->y / $y, # /**
  216. );
  217. $self->_dirty(1);
  218. $self->Refresh;
  219. }
  220. $self->_drag_start_xy($pos);
  221. }
  222. } elsif ($e->LeftUp || $e->MiddleUp || $e->RightUp) {
  223. $self->_drag_start_xy(undef);
  224. } else {
  225. $e->Skip();
  226. }
  227. }
  228. sub set_z {
  229. my ($self, $z) = @_;
  230. my $print = $self->print;
  231. # can we have interlaced layers?
  232. my $interlaced = (defined first { $_->config->support_material } @{$print->objects})
  233. || (defined first { $_->config->infill_every_layers > 1 } @{$print->regions});
  234. my $max_layer_height = $print->max_allowed_layer_height;
  235. my @layers = ();
  236. foreach my $object (@{$print->objects}) {
  237. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  238. if ($interlaced) {
  239. push @layers, $layer
  240. if $z > ($layer->print_z - $max_layer_height - epsilon)
  241. && $z <= $layer->print_z + epsilon;
  242. } else {
  243. push @layers, $layer if abs($layer->print_z - $z) < epsilon;
  244. }
  245. }
  246. }
  247. # reverse layers so that we draw the lowermost (i.e. current) on top
  248. $self->z($z);
  249. $self->layers([ reverse @layers ]);
  250. $self->Refresh;
  251. }
  252. sub Render {
  253. my ($self, $dc) = @_;
  254. # prevent calling SetCurrent() when window is not shown yet
  255. return unless $self->IsShownOnScreen;
  256. return unless my $context = $self->GetContext;
  257. $self->SetCurrent($context);
  258. $self->InitGL;
  259. glClearColor(1, 1, 1, 0);
  260. glClear(GL_COLOR_BUFFER_BIT);
  261. if (!$self->GetParent->enabled || !$self->layers) {
  262. glFlush();
  263. $self->SwapBuffers;
  264. return;
  265. }
  266. glDisable(GL_DEPTH_TEST);
  267. glMatrixMode(GL_MODELVIEW);
  268. glLoadIdentity();
  269. # anti-alias
  270. if (0) {
  271. glEnable(GL_LINE_SMOOTH);
  272. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  273. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  274. glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
  275. }
  276. my $tess;
  277. if (!(&Wx::wxMSW && $OpenGL::VERSION < 0.6704)) {
  278. # We can't use the GLU tesselator on MSW with older OpenGL versions
  279. # because of an upstream bug:
  280. # http://sourceforge.net/p/pogl/bugs/16/
  281. $tess = gluNewTess();
  282. gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
  283. gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
  284. gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
  285. gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
  286. gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
  287. gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
  288. }
  289. foreach my $layer (@{$self->layers}) {
  290. my $object = $layer->object;
  291. # only draw the slice for the current layer
  292. next unless abs($layer->print_z - $self->z) < epsilon;
  293. # draw slice contour
  294. glLineWidth(1);
  295. foreach my $copy (@{ $object->_shifted_copies }) {
  296. glPushMatrix();
  297. glTranslatef(@$copy, 0);
  298. foreach my $slice (@{$layer->slices}) {
  299. glColor3f(0.95, 0.95, 0.95);
  300. if ($tess) {
  301. gluTessBeginPolygon($tess);
  302. foreach my $polygon (@$slice) {
  303. gluTessBeginContour($tess);
  304. gluTessVertex_p($tess, @$_, 0) for @$polygon;
  305. gluTessEndContour($tess);
  306. }
  307. gluTessEndPolygon($tess);
  308. }
  309. glColor3f(0.9, 0.9, 0.9);
  310. foreach my $polygon (@$slice) {
  311. foreach my $line (@{$polygon->lines}) {
  312. glBegin(GL_LINES);
  313. glVertex2f(@{$line->a});
  314. glVertex2f(@{$line->b});
  315. glEnd();
  316. }
  317. }
  318. }
  319. glPopMatrix();
  320. }
  321. }
  322. my $skirt_drawn = 0;
  323. my $brim_drawn = 0;
  324. foreach my $layer (@{$self->layers}) {
  325. my $object = $layer->object;
  326. my $print_z = $layer->print_z;
  327. # draw brim
  328. if ($self->print->step_done(STEP_BRIM) && $layer->id == 0 && !$brim_drawn) {
  329. $self->color([0, 0, 0]);
  330. $self->_draw(undef, $print_z, $_) for @{$self->print->brim};
  331. $brim_drawn = 1;
  332. }
  333. if ($self->print->step_done(STEP_SKIRT)
  334. && ($self->print->has_infinite_skirt() || $self->print->config->skirt_height > $layer->id)
  335. && !$skirt_drawn) {
  336. $self->color([0, 0, 0]);
  337. $self->_draw(undef, $print_z, $_) for @{$self->print->skirt};
  338. $skirt_drawn = 1;
  339. }
  340. foreach my $layerm (@{$layer->regions}) {
  341. if ($object->step_done(STEP_PERIMETERS)) {
  342. $self->color([0.7, 0, 0]);
  343. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->perimeters};
  344. }
  345. if ($object->step_done(STEP_INFILL)) {
  346. $self->color([0, 0, 0.7]);
  347. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->fills};
  348. }
  349. }
  350. if ($object->step_done(STEP_SUPPORTMATERIAL)) {
  351. if ($layer->isa('Slic3r::Layer::Support')) {
  352. $self->color([0, 0, 0]);
  353. $self->_draw($object, $print_z, $_) for @{$layer->support_fills};
  354. $self->_draw($object, $print_z, $_) for @{$layer->support_interface_fills};
  355. }
  356. }
  357. }
  358. gluDeleteTess($tess) if $tess;
  359. glFlush();
  360. $self->SwapBuffers;
  361. }
  362. sub _draw {
  363. my ($self, $object, $print_z, $path) = @_;
  364. my @paths = $path->isa('Slic3r::ExtrusionLoop')
  365. ? @$path
  366. : ($path);
  367. $self->_draw_path($object, $print_z, $_) for @paths;
  368. }
  369. sub _draw_path {
  370. my ($self, $object, $print_z, $path) = @_;
  371. return if $print_z - $path->height > $self->z - epsilon;
  372. if (abs($print_z - $self->z) < epsilon) {
  373. glColor3f(@{$self->color});
  374. } else {
  375. glColor3f(0.8, 0.8, 0.8);
  376. }
  377. glLineWidth(1);
  378. if (defined $object) {
  379. foreach my $copy (@{ $object->_shifted_copies }) {
  380. glPushMatrix();
  381. glTranslatef(@$copy, 0);
  382. foreach my $line (@{$path->polyline->lines}) {
  383. glBegin(GL_LINES);
  384. glVertex2f(@{$line->a});
  385. glVertex2f(@{$line->b});
  386. glEnd();
  387. }
  388. glPopMatrix();
  389. }
  390. } else {
  391. foreach my $line (@{$path->polyline->lines}) {
  392. glBegin(GL_LINES);
  393. glVertex2f(@{$line->a});
  394. glVertex2f(@{$line->b});
  395. glEnd();
  396. }
  397. }
  398. }
  399. sub InitGL {
  400. my $self = shift;
  401. return if $self->init;
  402. return unless $self->GetContext;
  403. $self->init(1);
  404. }
  405. sub GetContext {
  406. my ($self) = @_;
  407. if (Wx::wxVERSION >= 2.009) {
  408. return $self->{context} ||= Wx::GLContext->new($self);
  409. } else {
  410. return $self->SUPER::GetContext;
  411. }
  412. }
  413. sub SetCurrent {
  414. my ($self, $context) = @_;
  415. if (Wx::wxVERSION >= 2.009) {
  416. return $self->SUPER::SetCurrent($context);
  417. } else {
  418. return $self->SUPER::SetCurrent;
  419. }
  420. }
  421. sub Resize {
  422. my ($self) = @_;
  423. return unless $self->GetContext;
  424. return unless $self->bb;
  425. $self->_dirty(0);
  426. $self->SetCurrent($self->GetContext);
  427. my ($x, $y) = $self->GetSizeWH;
  428. glViewport(0, 0, $x, $y);
  429. glMatrixMode(GL_PROJECTION);
  430. glLoadIdentity();
  431. my $bb = $self->bb->clone;
  432. # center bounding box around origin before scaling it
  433. my $bb_center = $bb->center;
  434. $bb->translate(@{$bb_center->negative});
  435. # scale bounding box according to zoom factor
  436. $bb->scale($self->_zoom);
  437. # reposition bounding box around original center
  438. $bb->translate(@{$bb_center});
  439. # translate camera
  440. $bb->translate(@{$self->_camera_target});
  441. # keep camera_bb within total bb
  442. # (i.e. prevent user from panning outside the bounding box)
  443. {
  444. my @translate = (0,0);
  445. if ($bb->x_min < $self->bb->x_min) {
  446. $translate[X] += $self->bb->x_min - $bb->x_min;
  447. }
  448. if ($bb->y_min < $self->bb->y_min) {
  449. $translate[Y] += $self->bb->y_min - $bb->y_min;
  450. }
  451. if ($bb->x_max > $self->bb->x_max) {
  452. $translate[X] -= $bb->x_max - $self->bb->x_max;
  453. }
  454. if ($bb->y_max > $self->bb->y_max) {
  455. $translate[Y] -= $bb->y_max - $self->bb->y_max;
  456. }
  457. $self->_camera_target->translate(@translate);
  458. $bb->translate(@translate);
  459. }
  460. # save camera
  461. $self->_camera_bb($bb);
  462. my ($x1, $y1, $x2, $y2) = ($bb->x_min, $bb->y_min, $bb->x_max, $bb->y_max);
  463. if (($x2 - $x1)/($y2 - $y1) > $x/$y) {
  464. # adjust Y
  465. my $new_y = $y * ($x2 - $x1) / $x;
  466. $y1 = ($y2 + $y1)/2 - $new_y/2;
  467. $y2 = $y1 + $new_y;
  468. } else {
  469. my $new_x = $x * ($y2 - $y1) / $y;
  470. $x1 = ($x2 + $x1)/2 - $new_x/2;
  471. $x2 = $x1 + $new_x;
  472. }
  473. glOrtho($x1, $x2, $y1, $y2, 0, 1);
  474. glMatrixMode(GL_MODELVIEW);
  475. }
  476. sub line {
  477. my (
  478. $x1, $y1, $x2, $y2, # coordinates of the line
  479. $w, # width/thickness of the line in pixel
  480. $Cr, $Cg, $Cb, # RGB color components
  481. $Br, $Bg, $Bb, # color of background when alphablend=false
  482. # Br=alpha of color when alphablend=true
  483. $alphablend, # use alpha blend or not
  484. ) = @_;
  485. my $t;
  486. my $R;
  487. my $f = $w - int($w);
  488. my $A;
  489. if ($alphablend) {
  490. $A = $Br;
  491. } else {
  492. $A = 1;
  493. }
  494. # determine parameters t,R
  495. if ($w >= 0 && $w < 1) {
  496. $t = 0.05; $R = 0.48 + 0.32 * $f;
  497. if (!$alphablend) {
  498. $Cr += 0.88 * (1-$f);
  499. $Cg += 0.88 * (1-$f);
  500. $Cb += 0.88 * (1-$f);
  501. $Cr = 1.0 if ($Cr > 1.0);
  502. $Cg = 1.0 if ($Cg > 1.0);
  503. $Cb = 1.0 if ($Cb > 1.0);
  504. } else {
  505. $A *= $f;
  506. }
  507. } elsif ($w >= 1.0 && $w < 2.0) {
  508. $t = 0.05 + $f*0.33; $R = 0.768 + 0.312*$f;
  509. } elsif ($w >= 2.0 && $w < 3.0) {
  510. $t = 0.38 + $f*0.58; $R = 1.08;
  511. } elsif ($w >= 3.0 && $w < 4.0) {
  512. $t = 0.96 + $f*0.48; $R = 1.08;
  513. } elsif ($w >= 4.0 && $w < 5.0) {
  514. $t= 1.44 + $f*0.46; $R = 1.08;
  515. } elsif ($w >= 5.0 && $w < 6.0) {
  516. $t= 1.9 + $f*0.6; $R = 1.08;
  517. } elsif ($w >= 6.0) {
  518. my $ff = $w - 6.0;
  519. $t = 2.5 + $ff*0.50; $R = 1.08;
  520. }
  521. #printf( "w=%f, f=%f, C=%.4f\n", $w, $f, $C);
  522. # determine angle of the line to horizontal
  523. my $tx = 0; my $ty = 0; # core thinkness of a line
  524. my $Rx = 0; my $Ry = 0; # fading edge of a line
  525. my $cx = 0; my $cy = 0; # cap of a line
  526. my $ALW = 0.01;
  527. my $dx = $x2 - $x1;
  528. my $dy = $y2 - $y1;
  529. if (abs($dx) < $ALW) {
  530. # vertical
  531. $tx = $t; $ty = 0;
  532. $Rx = $R; $Ry = 0;
  533. if ($w > 0.0 && $w < 1.0) {
  534. $tx *= 8;
  535. } elsif ($w == 1.0) {
  536. $tx *= 10;
  537. }
  538. } elsif (abs($dy) < $ALW) {
  539. #horizontal
  540. $tx = 0; $ty = $t;
  541. $Rx = 0; $Ry = $R;
  542. if ($w > 0.0 && $w < 1.0) {
  543. $ty *= 8;
  544. } elsif ($w == 1.0) {
  545. $ty *= 10;
  546. }
  547. } else {
  548. if ($w < 3) { # approximate to make things even faster
  549. my $m = $dy/$dx;
  550. # and calculate tx,ty,Rx,Ry
  551. if ($m > -0.4142 && $m <= 0.4142) {
  552. # -22.5 < $angle <= 22.5, approximate to 0 (degree)
  553. $tx = $t * 0.1; $ty = $t;
  554. $Rx = $R * 0.6; $Ry = $R;
  555. } elsif ($m > 0.4142 && $m <= 2.4142) {
  556. # 22.5 < $angle <= 67.5, approximate to 45 (degree)
  557. $tx = $t * -0.7071; $ty = $t * 0.7071;
  558. $Rx = $R * -0.7071; $Ry = $R * 0.7071;
  559. } elsif ($m > 2.4142 || $m <= -2.4142) {
  560. # 67.5 < $angle <= 112.5, approximate to 90 (degree)
  561. $tx = $t; $ty = $t*0.1;
  562. $Rx = $R; $Ry = $R*0.6;
  563. } elsif ($m > -2.4142 && $m < -0.4142) {
  564. # 112.5 < angle < 157.5, approximate to 135 (degree)
  565. $tx = $t * 0.7071; $ty = $t * 0.7071;
  566. $Rx = $R * 0.7071; $Ry = $R * 0.7071;
  567. } else {
  568. # error in determining angle
  569. printf("error in determining angle: m=%.4f\n", $m);
  570. }
  571. } else { # calculate to exact
  572. $dx= $y1 - $y2;
  573. $dy= $x2 - $x1;
  574. my $L = sqrt($dx*$dx + $dy*$dy);
  575. $dx /= $L;
  576. $dy /= $L;
  577. $cx = -0.6*$dy; $cy=0.6*$dx;
  578. $tx = $t*$dx; $ty = $t*$dy;
  579. $Rx = $R*$dx; $Ry = $R*$dy;
  580. }
  581. }
  582. # draw the line by triangle strip
  583. glBegin(GL_TRIANGLE_STRIP);
  584. if (!$alphablend) {
  585. glColor3f($Br, $Bg, $Bb);
  586. } else {
  587. glColor4f($Cr, $Cg, $Cb, 0);
  588. }
  589. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry); # fading edge
  590. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  591. if (!$alphablend) {
  592. glColor3f($Cr, $Cg, $Cb);
  593. } else {
  594. glColor4f($Cr, $Cg, $Cb, $A);
  595. }
  596. glVertex2f($x1 - $tx, $y1 - $ty); # core
  597. glVertex2f($x2 - $tx, $y2 - $ty);
  598. glVertex2f($x1 + $tx, $y1 + $ty);
  599. glVertex2f($x2 + $tx, $y2 + $ty);
  600. if ((abs($dx) < $ALW || abs($dy) < $ALW) && $w <= 1.0) {
  601. # printf("skipped one fading edge\n");
  602. } else {
  603. if (!$alphablend) {
  604. glColor3f($Br, $Bg, $Bb);
  605. } else {
  606. glColor4f($Cr, $Cg, $Cb, 0);
  607. }
  608. glVertex2f($x1 + $tx+ $Rx, $y1 + $ty + $Ry); # fading edge
  609. glVertex2f($x2 + $tx+ $Rx, $y2 + $ty + $Ry);
  610. }
  611. glEnd();
  612. # cap
  613. if ($w < 3) {
  614. # do not draw cap
  615. } else {
  616. # draw cap
  617. glBegin(GL_TRIANGLE_STRIP);
  618. if (!$alphablend) {
  619. glColor3f($Br, $Bg, $Bb);
  620. } else {
  621. glColor4f($Cr, $Cg, $Cb, 0);
  622. }
  623. glVertex2f($x1 - $Rx + $cx, $y1 - $Ry + $cy);
  624. glVertex2f($x1 + $Rx + $cx, $y1 + $Ry + $cy);
  625. glColor3f($Cr, $Cg, $Cb);
  626. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry);
  627. glVertex2f($x1 + $tx + $Rx, $y1 + $ty + $Ry);
  628. glEnd();
  629. glBegin(GL_TRIANGLE_STRIP);
  630. if (!$alphablend) {
  631. glColor3f($Br, $Bg, $Bb);
  632. } else {
  633. glColor4f($Cr, $Cg, $Cb, 0);
  634. }
  635. glVertex2f($x2 - $Rx - $cx, $y2 - $Ry - $cy);
  636. glVertex2f($x2 + $Rx - $cx, $y2 + $Ry - $cy);
  637. glColor3f($Cr, $Cg, $Cb);
  638. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  639. glVertex2f($x2 + $tx + $Rx, $y2 + $ty + $Ry);
  640. glEnd();
  641. }
  642. }
  643. package Slic3r::GUI::Plater::2DToolpaths::Dialog;
  644. use Wx qw(:dialog :id :misc :sizer);
  645. use Wx::Event qw(EVT_CLOSE);
  646. use base 'Wx::Dialog';
  647. sub new {
  648. my $class = shift;
  649. my ($parent, $print) = @_;
  650. my $self = $class->SUPER::new($parent, -1, "Toolpaths", wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  651. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  652. $sizer->Add(Slic3r::GUI::Plater::2DToolpaths->new($self, $print), 1, wxEXPAND, 0);
  653. $self->SetSizer($sizer);
  654. $self->SetMinSize($self->GetSize);
  655. # needed to actually free memory
  656. EVT_CLOSE($self, sub {
  657. $self->EndModal(wxID_OK);
  658. $self->Destroy;
  659. });
  660. return $self;
  661. }
  662. 1;