2DToolpaths.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 :keycode wxWHITE wxWANTS_CHARS);
  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, wxDefaultSize, wxWANTS_CHARS);
  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. if ($event->HasModifiers) {
  47. $event->Skip;
  48. } else {
  49. my $key = $event->GetKeyCode;
  50. if ($key == ord('D') || $key == WXK_LEFT) {
  51. # Keys: 'D' or WXK_LEFT
  52. $slider->SetValue($slider->GetValue - 1);
  53. $self->set_z($self->{layers_z}[$slider->GetValue]);
  54. } elsif ($key == ord('U') || $key == WXK_RIGHT) {
  55. # Keys: 'U' or WXK_RIGHT
  56. $slider->SetValue($slider->GetValue + 1);
  57. $self->set_z($self->{layers_z}[$slider->GetValue]);
  58. } elsif ($key >= ord('1') && $key <= ord('3')) {
  59. # Keys: '1' to '3'
  60. $canvas->set_simulation_mode($key - ord('1'));
  61. } else {
  62. $event->Skip;
  63. }
  64. }
  65. });
  66. $self->SetSizer($sizer);
  67. $self->SetMinSize($self->GetSize);
  68. $sizer->SetSizeHints($self);
  69. # init print
  70. $self->{print} = $print;
  71. $self->reload_print;
  72. return $self;
  73. }
  74. sub reload_print {
  75. my ($self) = @_;
  76. # we require that there's at least one object and the posSlice step
  77. # is performed on all of them (this ensures that _shifted_copies was
  78. # populated and we know the number of layers)
  79. if (!$self->print->object_step_done(STEP_SLICE)) {
  80. $self->enabled(0);
  81. $self->{slider}->Hide;
  82. $self->{canvas}->Refresh; # clears canvas
  83. return;
  84. }
  85. $self->{canvas}->bb($self->print->total_bounding_box);
  86. $self->{canvas}->_dirty(1);
  87. my %z = (); # z => 1
  88. foreach my $object (@{$self->{print}->objects}) {
  89. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  90. $z{$layer->print_z} = 1;
  91. }
  92. }
  93. $self->enabled(1);
  94. $self->{layers_z} = [ sort { $a <=> $b } keys %z ];
  95. $self->{slider}->SetRange(0, scalar(@{$self->{layers_z}})-1);
  96. if ((my $z_idx = $self->{slider}->GetValue) <= $#{$self->{layers_z}}) {
  97. $self->set_z($self->{layers_z}[$z_idx]);
  98. } else {
  99. $self->{slider}->SetValue(0);
  100. $self->set_z($self->{layers_z}[0]) if @{$self->{layers_z}};
  101. }
  102. $self->{slider}->Show;
  103. $self->Layout;
  104. }
  105. sub set_z {
  106. my ($self, $z) = @_;
  107. return if !$self->enabled;
  108. $self->{z_label}->SetLabel(sprintf '%.2f', $z);
  109. $self->{canvas}->set_z($z);
  110. }
  111. package Slic3r::GUI::Plater::2DToolpaths::Canvas;
  112. use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
  113. use OpenGL qw(:glconstants :glfunctions :glufunctions :gluconstants);
  114. use base qw(Wx::GLCanvas Class::Accessor);
  115. use Wx::GLCanvas qw(:all);
  116. use List::Util qw(min max first);
  117. use Slic3r::Geometry qw(scale epsilon X Y);
  118. use Slic3r::Print::State ':steps';
  119. __PACKAGE__->mk_accessors(qw(
  120. print z layers color init
  121. bb
  122. _camera_bb
  123. _dirty
  124. _zoom
  125. _camera_target
  126. _drag_start_xy
  127. _texture_name
  128. _texture_size
  129. _extrusion_simulator
  130. _simulation_mode
  131. ));
  132. # make OpenGL::Array thread-safe
  133. {
  134. no warnings 'redefine';
  135. *OpenGL::Array::CLONE_SKIP = sub { 1 };
  136. }
  137. sub new {
  138. my ($class, $parent, $print) = @_;
  139. my $self = (Wx::wxVERSION >= 3.000003) ?
  140. # The wxWidgets 3.0.3-beta have a bug, they crash with NULL attribute list.
  141. $class->SUPER::new($parent, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, 0, "",
  142. [WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 24, 0]) :
  143. $class->SUPER::new($parent);
  144. # Immediatelly force creation of the OpenGL context to consume the static variable s_wglContextAttribs.
  145. $self->GetContext();
  146. $self->print($print);
  147. $self->_zoom(1);
  148. # 2D point in model space
  149. $self->_camera_target(Slic3r::Pointf->new(0,0));
  150. # Texture for the extrusion simulator. The texture will be allocated / reallocated on Resize.
  151. $self->_texture_name(0);
  152. $self->_texture_size(Slic3r::Point->new(0,0));
  153. $self->_extrusion_simulator(Slic3r::ExtrusionSimulator->new());
  154. $self->_simulation_mode(0);
  155. EVT_PAINT($self, sub {
  156. my $dc = Wx::PaintDC->new($self);
  157. $self->Render($dc);
  158. });
  159. EVT_SIZE($self, sub { $self->_dirty(1) });
  160. EVT_IDLE($self, sub {
  161. return unless $self->_dirty;
  162. return if !$self->IsShownOnScreen;
  163. $self->Resize;
  164. $self->Refresh;
  165. });
  166. EVT_MOUSEWHEEL($self, sub {
  167. my ($self, $e) = @_;
  168. return if !$self->GetParent->enabled;
  169. my $old_zoom = $self->_zoom;
  170. # Calculate the zoom delta and apply it to the current zoom factor
  171. my $zoom = $e->GetWheelRotation() / $e->GetWheelDelta();
  172. $zoom = max(min($zoom, 4), -4);
  173. $zoom /= 10;
  174. $self->_zoom($self->_zoom / (1-$zoom));
  175. $self->_zoom(1) if $self->_zoom > 1; # prevent from zooming out too much
  176. {
  177. # In order to zoom around the mouse point we need to translate
  178. # the camera target. This math is almost there but not perfect yet...
  179. my $camera_bb_size = $self->_camera_bb->size;
  180. my $size = Slic3r::Pointf->new($self->GetSizeWH);
  181. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  182. # calculate the zooming center in pixel coordinates relative to the viewport center
  183. my $vec = Slic3r::Pointf->new($pos->x - $size->x/2, $pos->y - $size->y/2); #-
  184. # calculate where this point will end up after applying the new zoom
  185. my $vec2 = $vec->clone;
  186. $vec2->scale($old_zoom / $self->_zoom);
  187. # move the camera target by the difference of the two positions
  188. $self->_camera_target->translate(
  189. -($vec->x - $vec2->x) * $camera_bb_size->x / $size->x,
  190. ($vec->y - $vec2->y) * $camera_bb_size->y / $size->y, #//
  191. );
  192. }
  193. $self->_dirty(1);
  194. $self->Refresh;
  195. });
  196. EVT_MOUSE_EVENTS($self, \&mouse_event);
  197. return $self;
  198. }
  199. sub Destroy {
  200. my ($self) = @_;
  201. # Deallocate the OpenGL resources.
  202. my $context = $self->GetContext;
  203. if ($context and $self->texture_id) {
  204. $self->SetCurrent($context);
  205. glDeleteTextures(1, ($self->texture_id));
  206. $self->SetCurrent(0);
  207. $self->texture_id(0);
  208. $self->texture_size(new Slic3r::Point(0, 0));
  209. }
  210. return $self->SUPER::Destroy;
  211. }
  212. sub mouse_event {
  213. my ($self, $e) = @_;
  214. return if !$self->GetParent->enabled;
  215. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  216. if ($e->Entering && &Wx::wxMSW) {
  217. # wxMSW needs focus in order to catch mouse wheel events
  218. $self->SetFocus;
  219. } elsif ($e->Dragging) {
  220. if ($e->LeftIsDown || $e->MiddleIsDown || $e->RightIsDown) {
  221. # if dragging, translate view
  222. if (defined $self->_drag_start_xy) {
  223. my $move = $self->_drag_start_xy->vector_to($pos); # in pixels
  224. # get viewport and camera size in order to convert pixel to model units
  225. my ($x, $y) = $self->GetSizeWH;
  226. my $camera_bb_size = $self->_camera_bb->size;
  227. # compute translation in model units
  228. $self->_camera_target->translate(
  229. -$move->x * $camera_bb_size->x / $x,
  230. $move->y * $camera_bb_size->y / $y, # /**
  231. );
  232. $self->_dirty(1);
  233. $self->Refresh;
  234. }
  235. $self->_drag_start_xy($pos);
  236. }
  237. } elsif ($e->LeftUp || $e->MiddleUp || $e->RightUp) {
  238. $self->_drag_start_xy(undef);
  239. } else {
  240. $e->Skip();
  241. }
  242. }
  243. sub set_z {
  244. my ($self, $z) = @_;
  245. my $print = $self->print;
  246. # can we have interlaced layers?
  247. my $interlaced = (defined first { $_->config->support_material } @{$print->objects})
  248. || (defined first { $_->config->infill_every_layers > 1 } @{$print->regions});
  249. my $max_layer_height = $print->max_allowed_layer_height;
  250. my @layers = ();
  251. foreach my $object (@{$print->objects}) {
  252. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  253. if ($interlaced) {
  254. push @layers, $layer
  255. if $z > ($layer->print_z - $max_layer_height - epsilon)
  256. && $z <= $layer->print_z + epsilon;
  257. } else {
  258. push @layers, $layer if abs($layer->print_z - $z) < epsilon;
  259. }
  260. }
  261. }
  262. # reverse layers so that we draw the lowermost (i.e. current) on top
  263. $self->z($z);
  264. $self->layers([ reverse @layers ]);
  265. $self->Refresh;
  266. }
  267. sub set_simulation_mode
  268. {
  269. my ($self, $mode) = @_;
  270. $self->_simulation_mode($mode);
  271. $self->_dirty(1);
  272. $self->Refresh;
  273. }
  274. sub Render {
  275. my ($self, $dc) = @_;
  276. # prevent calling SetCurrent() when window is not shown yet
  277. return unless $self->IsShownOnScreen;
  278. return unless my $context = $self->GetContext;
  279. $self->SetCurrent($context);
  280. $self->InitGL;
  281. glClearColor(1, 1, 1, 0);
  282. glClear(GL_COLOR_BUFFER_BIT);
  283. if (!$self->GetParent->enabled || !$self->layers) {
  284. glFlush();
  285. $self->SwapBuffers;
  286. return;
  287. }
  288. glDisable(GL_DEPTH_TEST);
  289. glMatrixMode(GL_MODELVIEW);
  290. glLoadIdentity();
  291. if ($self->_simulation_mode and $self->_texture_name and $self->_texture_size->x() > 0 and $self->_texture_size->y() > 0) {
  292. $self->_simulate_extrusion();
  293. my ($x, $y) = $self->GetSizeWH;
  294. glEnable(GL_TEXTURE_2D);
  295. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
  296. glBindTexture(GL_TEXTURE_2D, $self->_texture_name);
  297. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  298. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  299. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  300. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  301. glTexImage2D_c(GL_TEXTURE_2D,
  302. 0, # level (0 normal, heighr is form mip-mapping)
  303. GL_RGBA, # internal format
  304. $self->_texture_size->x(), $self->_texture_size->y(),
  305. 0, # border
  306. GL_RGBA, # format RGBA color data
  307. GL_UNSIGNED_BYTE, # unsigned byte data
  308. $self->_extrusion_simulator->image_ptr()); # ptr to texture data
  309. glMatrixMode(GL_PROJECTION);
  310. glPushMatrix();
  311. glLoadIdentity();
  312. glOrtho(0, 1, 0, 1, 0, 1);
  313. glBegin(GL_QUADS);
  314. glTexCoord2f(0, 0);
  315. glVertex2f(0, 0);
  316. glTexCoord2f($x/$self->_texture_size->x(), 0);
  317. glVertex2f(1, 0);
  318. glTexCoord2f($x/$self->_texture_size->x(), $y/$self->_texture_size->y());
  319. glVertex2f(1, 1);
  320. glTexCoord2f(0, $y/$self->_texture_size->y());
  321. glVertex2f(0, 1);
  322. glEnd();
  323. glPopMatrix();
  324. glBindTexture(GL_TEXTURE_2D, 0);
  325. }
  326. # anti-alias
  327. if (0) {
  328. glEnable(GL_LINE_SMOOTH);
  329. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  330. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  331. glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
  332. }
  333. # Tesselator triangulates polygons with holes on the fly for the rendering purposes only.
  334. my $tess;
  335. if ($self->_simulation_mode() == 0 and !(&Wx::wxMSW && $OpenGL::VERSION < 0.6704)) {
  336. # We can't use the GLU tesselator on MSW with older OpenGL versions
  337. # because of an upstream bug:
  338. # http://sourceforge.net/p/pogl/bugs/16/
  339. $tess = gluNewTess();
  340. gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
  341. gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
  342. gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
  343. gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
  344. gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
  345. gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
  346. }
  347. foreach my $layer (@{$self->layers}) {
  348. my $object = $layer->object;
  349. # only draw the slice for the current layer
  350. next unless abs($layer->print_z - $self->z) < epsilon;
  351. # draw slice contour
  352. glLineWidth(1);
  353. foreach my $copy (@{ $object->_shifted_copies }) {
  354. glPushMatrix();
  355. glTranslatef(@$copy, 0);
  356. foreach my $slice (@{$layer->slices}) {
  357. glColor3f(0.95, 0.95, 0.95);
  358. if ($tess) {
  359. gluTessBeginPolygon($tess);
  360. foreach my $polygon (@$slice) {
  361. gluTessBeginContour($tess);
  362. gluTessVertex_p($tess, @$_, 0) for @$polygon;
  363. gluTessEndContour($tess);
  364. }
  365. gluTessEndPolygon($tess);
  366. }
  367. glColor3f(0.9, 0.9, 0.9);
  368. foreach my $polygon (@$slice) {
  369. foreach my $line (@{$polygon->lines}) {
  370. glBegin(GL_LINES);
  371. glVertex2f(@{$line->a});
  372. glVertex2f(@{$line->b});
  373. glEnd();
  374. }
  375. }
  376. }
  377. glPopMatrix();
  378. }
  379. }
  380. my $skirt_drawn = 0;
  381. my $brim_drawn = 0;
  382. foreach my $layer (@{$self->layers}) {
  383. my $object = $layer->object;
  384. my $print_z = $layer->print_z;
  385. # draw brim
  386. if ($self->print->step_done(STEP_BRIM) && $layer->id == 0 && !$brim_drawn) {
  387. $self->color([0, 0, 0]);
  388. $self->_draw(undef, $print_z, $_) for @{$self->print->brim};
  389. $brim_drawn = 1;
  390. }
  391. if ($self->print->step_done(STEP_SKIRT)
  392. && ($self->print->has_infinite_skirt() || $self->print->config->skirt_height > $layer->id)
  393. && !$skirt_drawn) {
  394. $self->color([0, 0, 0]);
  395. $self->_draw(undef, $print_z, $_) for @{$self->print->skirt};
  396. $skirt_drawn = 1;
  397. }
  398. foreach my $layerm (@{$layer->regions}) {
  399. if ($object->step_done(STEP_PERIMETERS)) {
  400. $self->color([0.7, 0, 0]);
  401. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->perimeters};
  402. }
  403. if ($object->step_done(STEP_INFILL)) {
  404. $self->color([0, 0, 0.7]);
  405. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->fills};
  406. }
  407. }
  408. if ($object->step_done(STEP_SUPPORTMATERIAL)) {
  409. if ($layer->isa('Slic3r::Layer::Support')) {
  410. $self->color([0, 0, 0]);
  411. $self->_draw($object, $print_z, $_) for @{$layer->support_fills};
  412. }
  413. }
  414. }
  415. gluDeleteTess($tess) if $tess;
  416. glFlush();
  417. $self->SwapBuffers;
  418. }
  419. sub _draw {
  420. my ($self, $object, $print_z, $path) = @_;
  421. my @paths = ($path->isa('Slic3r::ExtrusionLoop') || $path->isa('Slic3r::ExtrusionMultiPath'))
  422. ? @$path
  423. : ($path);
  424. $self->_draw_path($object, $print_z, $_) for @paths;
  425. }
  426. sub _draw_path {
  427. my ($self, $object, $print_z, $path) = @_;
  428. return if $print_z - $path->height > $self->z - epsilon;
  429. if (abs($print_z - $self->z) < epsilon) {
  430. glColor3f(@{$self->color});
  431. } else {
  432. glColor3f(0.8, 0.8, 0.8);
  433. }
  434. glLineWidth(1);
  435. if (defined $object) {
  436. foreach my $copy (@{ $object->_shifted_copies }) {
  437. glPushMatrix();
  438. glTranslatef(@$copy, 0);
  439. foreach my $line (@{$path->polyline->lines}) {
  440. glBegin(GL_LINES);
  441. glVertex2f(@{$line->a});
  442. glVertex2f(@{$line->b});
  443. glEnd();
  444. }
  445. glPopMatrix();
  446. }
  447. } else {
  448. foreach my $line (@{$path->polyline->lines}) {
  449. glBegin(GL_LINES);
  450. glVertex2f(@{$line->a});
  451. glVertex2f(@{$line->b});
  452. glEnd();
  453. }
  454. }
  455. }
  456. sub _simulate_extrusion {
  457. my ($self) = @_;
  458. $self->_extrusion_simulator->reset_accumulator();
  459. foreach my $layer (@{$self->layers}) {
  460. if (abs($layer->print_z - $self->z) < epsilon) {
  461. my $object = $layer->object;
  462. my @shifts = (defined $object) ? @{$object->_shifted_copies} : (Slic3r::Point->new(0, 0));
  463. foreach my $layerm (@{$layer->regions}) {
  464. my @extrusions = ();
  465. if ($object->step_done(STEP_PERIMETERS)) {
  466. push @extrusions, @$_ for @{$layerm->perimeters};
  467. }
  468. if ($object->step_done(STEP_INFILL)) {
  469. push @extrusions, @$_ for @{$layerm->fills};
  470. }
  471. foreach my $extrusion_entity (@extrusions) {
  472. my @paths = ($extrusion_entity->isa('Slic3r::ExtrusionLoop') || $extrusion_entity->isa('Slic3r::ExtrusionMultiPath'))
  473. ? @$extrusion_entity
  474. : ($extrusion_entity);
  475. foreach my $path (@paths) {
  476. print "width: ", $path->width,
  477. " height: ", $path->height,
  478. " mm3_per_mm: ", $path->mm3_per_mm,
  479. " height2: ", $path->mm3_per_mm / $path->height,
  480. "\n";
  481. $self->_extrusion_simulator->extrude_to_accumulator($path, $_, $self->_simulation_mode()) for @shifts;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. $self->_extrusion_simulator->evaluate_accumulator($self->_simulation_mode());
  488. }
  489. sub InitGL {
  490. my $self = shift;
  491. return if $self->init;
  492. return unless $self->GetContext;
  493. my $texture_id = 0;
  494. ($texture_id) = glGenTextures_p(1);
  495. $self->_texture_name($texture_id);
  496. $self->init(1);
  497. }
  498. sub GetContext {
  499. my ($self) = @_;
  500. return $self->{context} ||= Wx::GLContext->new($self);
  501. }
  502. sub SetCurrent {
  503. my ($self, $context) = @_;
  504. return $self->SUPER::SetCurrent($context);
  505. }
  506. sub Resize {
  507. my ($self) = @_;
  508. return unless $self->GetContext;
  509. return unless $self->bb;
  510. $self->_dirty(0);
  511. $self->SetCurrent($self->GetContext);
  512. my ($x, $y) = $self->GetSizeWH;
  513. if ($self->_texture_size->x() < $x or $self->_texture_size->y() < $y) {
  514. # Allocate a large enough OpenGL texture with power of 2 dimensions.
  515. $self->_texture_size->set_x(1) if ($self->_texture_size->x() == 0);
  516. $self->_texture_size->set_y(1) if ($self->_texture_size->y() == 0);
  517. $self->_texture_size->set_x($self->_texture_size->x() * 2) while ($self->_texture_size->x() < $x);
  518. $self->_texture_size->set_y($self->_texture_size->y() * 2) while ($self->_texture_size->y() < $y);
  519. #print "screen size ", $x, "x", $y;
  520. #print "texture size ", $self->_texture_size->x(), "x", $self->_texture_size->y();
  521. # Initialize an empty texture.
  522. glBindTexture(GL_TEXTURE_2D, $self->_texture_name);
  523. if (1) {
  524. glTexImage2D_c(GL_TEXTURE_2D,
  525. 0, # level (0 normal, heighr is form mip-mapping)
  526. GL_RGBA, # internal format
  527. $self->_texture_size->x(), $self->_texture_size->y(),
  528. 0, # border
  529. GL_RGBA, # format RGBA color data
  530. GL_UNSIGNED_BYTE, # unsigned byte data
  531. 0); # ptr to texture data
  532. }
  533. glBindTexture(GL_TEXTURE_2D, 0);
  534. $self->_extrusion_simulator->set_image_size($self->_texture_size);
  535. }
  536. $self->_extrusion_simulator->set_viewport(Slic3r::Geometry::BoundingBox->new_from_points(
  537. [Slic3r::Point->new(0, 0), Slic3r::Point->new($x, $y)]));
  538. glViewport(0, 0, $x, $y);
  539. glMatrixMode(GL_PROJECTION);
  540. glLoadIdentity();
  541. my $bb = $self->bb->clone;
  542. # center bounding box around origin before scaling it
  543. my $bb_center = $bb->center;
  544. $bb->translate(@{$bb_center->negative});
  545. # scale bounding box according to zoom factor
  546. $bb->scale($self->_zoom);
  547. # reposition bounding box around original center
  548. $bb->translate(@{$bb_center});
  549. # translate camera
  550. $bb->translate(@{$self->_camera_target});
  551. # keep camera_bb within total bb
  552. # (i.e. prevent user from panning outside the bounding box)
  553. {
  554. my @translate = (0,0);
  555. if ($bb->x_min < $self->bb->x_min) {
  556. $translate[X] += $self->bb->x_min - $bb->x_min;
  557. }
  558. if ($bb->y_min < $self->bb->y_min) {
  559. $translate[Y] += $self->bb->y_min - $bb->y_min;
  560. }
  561. if ($bb->x_max > $self->bb->x_max) {
  562. $translate[X] -= $bb->x_max - $self->bb->x_max;
  563. }
  564. if ($bb->y_max > $self->bb->y_max) {
  565. $translate[Y] -= $bb->y_max - $self->bb->y_max;
  566. }
  567. $self->_camera_target->translate(@translate);
  568. $bb->translate(@translate);
  569. }
  570. # save camera
  571. $self->_camera_bb($bb);
  572. my ($x1, $y1, $x2, $y2) = ($bb->x_min, $bb->y_min, $bb->x_max, $bb->y_max);
  573. if (($x2 - $x1)/($y2 - $y1) > $x/$y) {
  574. # adjust Y
  575. my $new_y = $y * ($x2 - $x1) / $x;
  576. $y1 = ($y2 + $y1)/2 - $new_y/2;
  577. $y2 = $y1 + $new_y;
  578. } else {
  579. my $new_x = $x * ($y2 - $y1) / $y;
  580. $x1 = ($x2 + $x1)/2 - $new_x/2;
  581. $x2 = $x1 + $new_x;
  582. }
  583. glOrtho($x1, $x2, $y1, $y2, 0, 1);
  584. # Set the adjusted bounding box at the extrusion simulator.
  585. #print "Scene bbox ", $bb->x_min, ",", $bb->y_min, " ", $bb->x_max, ",", $bb->y_max, "\n";
  586. #print "Setting simulator bbox ", $x1, ",", $y1, " ", $x2, ",", $y2, "\n";
  587. $self->_extrusion_simulator->set_bounding_box(
  588. Slic3r::Geometry::BoundingBox->new_from_points(
  589. [Slic3r::Point->new($x1, $y1), Slic3r::Point->new($x2, $y2)]));
  590. glMatrixMode(GL_MODELVIEW);
  591. }
  592. # Thick line drawing is not used anywhere. Probably not tested?
  593. sub line {
  594. my (
  595. $x1, $y1, $x2, $y2, # coordinates of the line
  596. $w, # width/thickness of the line in pixel
  597. $Cr, $Cg, $Cb, # RGB color components
  598. $Br, $Bg, $Bb, # color of background when alphablend=false
  599. # Br=alpha of color when alphablend=true
  600. $alphablend, # use alpha blend or not
  601. ) = @_;
  602. my $t;
  603. my $R;
  604. my $f = $w - int($w);
  605. my $A;
  606. if ($alphablend) {
  607. $A = $Br;
  608. } else {
  609. $A = 1;
  610. }
  611. # determine parameters t,R
  612. if ($w >= 0 && $w < 1) {
  613. $t = 0.05; $R = 0.48 + 0.32 * $f;
  614. if (!$alphablend) {
  615. $Cr += 0.88 * (1-$f);
  616. $Cg += 0.88 * (1-$f);
  617. $Cb += 0.88 * (1-$f);
  618. $Cr = 1.0 if ($Cr > 1.0);
  619. $Cg = 1.0 if ($Cg > 1.0);
  620. $Cb = 1.0 if ($Cb > 1.0);
  621. } else {
  622. $A *= $f;
  623. }
  624. } elsif ($w >= 1.0 && $w < 2.0) {
  625. $t = 0.05 + $f*0.33; $R = 0.768 + 0.312*$f;
  626. } elsif ($w >= 2.0 && $w < 3.0) {
  627. $t = 0.38 + $f*0.58; $R = 1.08;
  628. } elsif ($w >= 3.0 && $w < 4.0) {
  629. $t = 0.96 + $f*0.48; $R = 1.08;
  630. } elsif ($w >= 4.0 && $w < 5.0) {
  631. $t= 1.44 + $f*0.46; $R = 1.08;
  632. } elsif ($w >= 5.0 && $w < 6.0) {
  633. $t= 1.9 + $f*0.6; $R = 1.08;
  634. } elsif ($w >= 6.0) {
  635. my $ff = $w - 6.0;
  636. $t = 2.5 + $ff*0.50; $R = 1.08;
  637. }
  638. #printf( "w=%f, f=%f, C=%.4f\n", $w, $f, $C);
  639. # determine angle of the line to horizontal
  640. my $tx = 0; my $ty = 0; # core thinkness of a line
  641. my $Rx = 0; my $Ry = 0; # fading edge of a line
  642. my $cx = 0; my $cy = 0; # cap of a line
  643. my $ALW = 0.01;
  644. my $dx = $x2 - $x1;
  645. my $dy = $y2 - $y1;
  646. if (abs($dx) < $ALW) {
  647. # vertical
  648. $tx = $t; $ty = 0;
  649. $Rx = $R; $Ry = 0;
  650. if ($w > 0.0 && $w < 1.0) {
  651. $tx *= 8;
  652. } elsif ($w == 1.0) {
  653. $tx *= 10;
  654. }
  655. } elsif (abs($dy) < $ALW) {
  656. #horizontal
  657. $tx = 0; $ty = $t;
  658. $Rx = 0; $Ry = $R;
  659. if ($w > 0.0 && $w < 1.0) {
  660. $ty *= 8;
  661. } elsif ($w == 1.0) {
  662. $ty *= 10;
  663. }
  664. } else {
  665. if ($w < 3) { # approximate to make things even faster
  666. my $m = $dy/$dx;
  667. # and calculate tx,ty,Rx,Ry
  668. if ($m > -0.4142 && $m <= 0.4142) {
  669. # -22.5 < $angle <= 22.5, approximate to 0 (degree)
  670. $tx = $t * 0.1; $ty = $t;
  671. $Rx = $R * 0.6; $Ry = $R;
  672. } elsif ($m > 0.4142 && $m <= 2.4142) {
  673. # 22.5 < $angle <= 67.5, approximate to 45 (degree)
  674. $tx = $t * -0.7071; $ty = $t * 0.7071;
  675. $Rx = $R * -0.7071; $Ry = $R * 0.7071;
  676. } elsif ($m > 2.4142 || $m <= -2.4142) {
  677. # 67.5 < $angle <= 112.5, approximate to 90 (degree)
  678. $tx = $t; $ty = $t*0.1;
  679. $Rx = $R; $Ry = $R*0.6;
  680. } elsif ($m > -2.4142 && $m < -0.4142) {
  681. # 112.5 < angle < 157.5, approximate to 135 (degree)
  682. $tx = $t * 0.7071; $ty = $t * 0.7071;
  683. $Rx = $R * 0.7071; $Ry = $R * 0.7071;
  684. } else {
  685. # error in determining angle
  686. printf("error in determining angle: m=%.4f\n", $m);
  687. }
  688. } else { # calculate to exact
  689. $dx= $y1 - $y2;
  690. $dy= $x2 - $x1;
  691. my $L = sqrt($dx*$dx + $dy*$dy);
  692. $dx /= $L;
  693. $dy /= $L;
  694. $cx = -0.6*$dy; $cy=0.6*$dx;
  695. $tx = $t*$dx; $ty = $t*$dy;
  696. $Rx = $R*$dx; $Ry = $R*$dy;
  697. }
  698. }
  699. # draw the line by triangle strip
  700. glBegin(GL_TRIANGLE_STRIP);
  701. if (!$alphablend) {
  702. glColor3f($Br, $Bg, $Bb);
  703. } else {
  704. glColor4f($Cr, $Cg, $Cb, 0);
  705. }
  706. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry); # fading edge
  707. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  708. if (!$alphablend) {
  709. glColor3f($Cr, $Cg, $Cb);
  710. } else {
  711. glColor4f($Cr, $Cg, $Cb, $A);
  712. }
  713. glVertex2f($x1 - $tx, $y1 - $ty); # core
  714. glVertex2f($x2 - $tx, $y2 - $ty);
  715. glVertex2f($x1 + $tx, $y1 + $ty);
  716. glVertex2f($x2 + $tx, $y2 + $ty);
  717. if ((abs($dx) < $ALW || abs($dy) < $ALW) && $w <= 1.0) {
  718. # printf("skipped one fading edge\n");
  719. } else {
  720. if (!$alphablend) {
  721. glColor3f($Br, $Bg, $Bb);
  722. } else {
  723. glColor4f($Cr, $Cg, $Cb, 0);
  724. }
  725. glVertex2f($x1 + $tx+ $Rx, $y1 + $ty + $Ry); # fading edge
  726. glVertex2f($x2 + $tx+ $Rx, $y2 + $ty + $Ry);
  727. }
  728. glEnd();
  729. # cap
  730. if ($w < 3) {
  731. # do not draw cap
  732. } else {
  733. # draw cap
  734. glBegin(GL_TRIANGLE_STRIP);
  735. if (!$alphablend) {
  736. glColor3f($Br, $Bg, $Bb);
  737. } else {
  738. glColor4f($Cr, $Cg, $Cb, 0);
  739. }
  740. glVertex2f($x1 - $Rx + $cx, $y1 - $Ry + $cy);
  741. glVertex2f($x1 + $Rx + $cx, $y1 + $Ry + $cy);
  742. glColor3f($Cr, $Cg, $Cb);
  743. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry);
  744. glVertex2f($x1 + $tx + $Rx, $y1 + $ty + $Ry);
  745. glEnd();
  746. glBegin(GL_TRIANGLE_STRIP);
  747. if (!$alphablend) {
  748. glColor3f($Br, $Bg, $Bb);
  749. } else {
  750. glColor4f($Cr, $Cg, $Cb, 0);
  751. }
  752. glVertex2f($x2 - $Rx - $cx, $y2 - $Ry - $cy);
  753. glVertex2f($x2 + $Rx - $cx, $y2 + $Ry - $cy);
  754. glColor3f($Cr, $Cg, $Cb);
  755. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  756. glVertex2f($x2 + $tx + $Rx, $y2 + $ty + $Ry);
  757. glEnd();
  758. }
  759. }
  760. package Slic3r::GUI::Plater::2DToolpaths::Dialog;
  761. use Wx qw(:dialog :id :misc :sizer);
  762. use Wx::Event qw(EVT_CLOSE);
  763. use base 'Wx::Dialog';
  764. sub new {
  765. my $class = shift;
  766. my ($parent, $print) = @_;
  767. my $self = $class->SUPER::new($parent, -1, "Toolpaths", wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  768. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  769. $sizer->Add(Slic3r::GUI::Plater::2DToolpaths->new($self, $print), 1, wxEXPAND, 0);
  770. $self->SetSizer($sizer);
  771. $self->SetMinSize($self->GetSize);
  772. # needed to actually free memory
  773. EVT_CLOSE($self, sub {
  774. $self->EndModal(wxID_OK);
  775. $self->Destroy;
  776. });
  777. return $self;
  778. }
  779. 1;