2DToolpaths.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. $self->SwapBuffers;
  285. return;
  286. }
  287. glDisable(GL_DEPTH_TEST);
  288. glMatrixMode(GL_MODELVIEW);
  289. glLoadIdentity();
  290. if ($self->_simulation_mode and $self->_texture_name and $self->_texture_size->x() > 0 and $self->_texture_size->y() > 0) {
  291. $self->_simulate_extrusion();
  292. my ($x, $y) = $self->GetSizeWH;
  293. glEnable(GL_TEXTURE_2D);
  294. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
  295. glBindTexture(GL_TEXTURE_2D, $self->_texture_name);
  296. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  297. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  298. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  299. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  300. glTexImage2D_c(GL_TEXTURE_2D,
  301. 0, # level (0 normal, heighr is form mip-mapping)
  302. GL_RGBA, # internal format
  303. $self->_texture_size->x(), $self->_texture_size->y(),
  304. 0, # border
  305. GL_RGBA, # format RGBA color data
  306. GL_UNSIGNED_BYTE, # unsigned byte data
  307. $self->_extrusion_simulator->image_ptr()); # ptr to texture data
  308. glMatrixMode(GL_PROJECTION);
  309. glPushMatrix();
  310. glLoadIdentity();
  311. glOrtho(0, 1, 0, 1, 0, 1);
  312. glBegin(GL_QUADS);
  313. glTexCoord2f(0, 0);
  314. glVertex2f(0, 0);
  315. glTexCoord2f($x/$self->_texture_size->x(), 0);
  316. glVertex2f(1, 0);
  317. glTexCoord2f($x/$self->_texture_size->x(), $y/$self->_texture_size->y());
  318. glVertex2f(1, 1);
  319. glTexCoord2f(0, $y/$self->_texture_size->y());
  320. glVertex2f(0, 1);
  321. glEnd();
  322. glPopMatrix();
  323. glBindTexture(GL_TEXTURE_2D, 0);
  324. }
  325. # anti-alias
  326. if (0) {
  327. glEnable(GL_LINE_SMOOTH);
  328. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  329. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  330. glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
  331. }
  332. # Tesselator triangulates polygons with holes on the fly for the rendering purposes only.
  333. my $tess;
  334. if ($self->_simulation_mode() == 0 and !(&Wx::wxMSW && $OpenGL::VERSION < 0.6704)) {
  335. # We can't use the GLU tesselator on MSW with older OpenGL versions
  336. # because of an upstream bug:
  337. # http://sourceforge.net/p/pogl/bugs/16/
  338. $tess = gluNewTess();
  339. gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
  340. gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
  341. gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
  342. gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
  343. gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
  344. gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
  345. }
  346. foreach my $layer (@{$self->layers}) {
  347. my $object = $layer->object;
  348. # only draw the slice for the current layer
  349. next unless abs($layer->print_z - $self->z) < epsilon;
  350. # draw slice contour
  351. glLineWidth(1);
  352. foreach my $copy (@{ $object->_shifted_copies }) {
  353. glPushMatrix();
  354. glTranslatef(@$copy, 0);
  355. foreach my $slice (@{$layer->slices}) {
  356. glColor3f(0.95, 0.95, 0.95);
  357. if ($tess) {
  358. gluTessBeginPolygon($tess);
  359. foreach my $polygon (@$slice) {
  360. gluTessBeginContour($tess);
  361. gluTessVertex_p($tess, @$_, 0) for @$polygon;
  362. gluTessEndContour($tess);
  363. }
  364. gluTessEndPolygon($tess);
  365. }
  366. glColor3f(0.9, 0.9, 0.9);
  367. foreach my $polygon (@$slice) {
  368. foreach my $line (@{$polygon->lines}) {
  369. glBegin(GL_LINES);
  370. glVertex2f(@{$line->a});
  371. glVertex2f(@{$line->b});
  372. glEnd();
  373. }
  374. }
  375. }
  376. glPopMatrix();
  377. }
  378. }
  379. my $skirt_drawn = 0;
  380. my $brim_drawn = 0;
  381. foreach my $layer (@{$self->layers}) {
  382. my $object = $layer->object;
  383. my $print_z = $layer->print_z;
  384. # draw brim
  385. if ($self->print->step_done(STEP_BRIM) && $layer->id == 0 && !$brim_drawn) {
  386. $self->color([0, 0, 0]);
  387. $self->_draw(undef, $print_z, $_) for @{$self->print->brim};
  388. $brim_drawn = 1;
  389. }
  390. if ($self->print->step_done(STEP_SKIRT)
  391. && ($self->print->has_infinite_skirt() || $self->print->config->skirt_height > $layer->id)
  392. && !$skirt_drawn) {
  393. $self->color([0, 0, 0]);
  394. $self->_draw(undef, $print_z, $_) for @{$self->print->skirt};
  395. $skirt_drawn = 1;
  396. }
  397. foreach my $layerm (@{$layer->regions}) {
  398. if ($object->step_done(STEP_PERIMETERS)) {
  399. $self->color([0.7, 0, 0]);
  400. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->perimeters};
  401. }
  402. if ($object->step_done(STEP_INFILL)) {
  403. $self->color([0, 0, 0.7]);
  404. $self->_draw($object, $print_z, $_) for map @$_, @{$layerm->fills};
  405. }
  406. }
  407. if ($object->step_done(STEP_SUPPORTMATERIAL)) {
  408. if ($layer->isa('Slic3r::Layer::Support')) {
  409. $self->color([0, 0, 0]);
  410. $self->_draw($object, $print_z, $_) for @{$layer->support_fills};
  411. }
  412. }
  413. }
  414. gluDeleteTess($tess) if $tess;
  415. $self->SwapBuffers;
  416. }
  417. sub _draw {
  418. my ($self, $object, $print_z, $path) = @_;
  419. if ($path->isa('Slic3r::ExtrusionPath::Collection')) {
  420. $self->_draw($object, $print_z, $_) for @{$path};
  421. }else{
  422. my @paths = ($path->isa('Slic3r::ExtrusionLoop') || $path->isa('Slic3r::ExtrusionMultiPath'))
  423. ? @$path
  424. : ($path);
  425. $self->_draw_path($object, $print_z, $_) for @paths;
  426. }
  427. }
  428. sub _draw_path {
  429. my ($self, $object, $print_z, $path) = @_;
  430. return if $print_z - $path->height > $self->z - epsilon;
  431. if (abs($print_z - $self->z) < epsilon) {
  432. glColor3f(@{$self->color});
  433. } else {
  434. glColor3f(0.8, 0.8, 0.8);
  435. }
  436. glLineWidth(1);
  437. if (defined $object) {
  438. foreach my $copy (@{ $object->_shifted_copies }) {
  439. glPushMatrix();
  440. glTranslatef(@$copy, 0);
  441. foreach my $line (@{$path->polyline->lines}) {
  442. glBegin(GL_LINES);
  443. glVertex2f(@{$line->a});
  444. glVertex2f(@{$line->b});
  445. glEnd();
  446. }
  447. glPopMatrix();
  448. }
  449. } else {
  450. foreach my $line (@{$path->polyline->lines}) {
  451. glBegin(GL_LINES);
  452. glVertex2f(@{$line->a});
  453. glVertex2f(@{$line->b});
  454. glEnd();
  455. }
  456. }
  457. }
  458. sub _simulate_extrusion {
  459. my ($self) = @_;
  460. $self->_extrusion_simulator->reset_accumulator();
  461. foreach my $layer (@{$self->layers}) {
  462. if (abs($layer->print_z - $self->z) < epsilon) {
  463. my $object = $layer->object;
  464. my @shifts = (defined $object) ? @{$object->_shifted_copies} : (Slic3r::Point->new(0, 0));
  465. foreach my $layerm (@{$layer->regions}) {
  466. my @extrusions = ();
  467. if ($object->step_done(STEP_PERIMETERS)) {
  468. push @extrusions, @$_ for @{$layerm->perimeters};
  469. }
  470. if ($object->step_done(STEP_INFILL)) {
  471. push @extrusions, @$_ for @{$layerm->fills};
  472. }
  473. foreach my $extrusion_entity (@extrusions) {
  474. my @paths = ($extrusion_entity->isa('Slic3r::ExtrusionLoop') || $extrusion_entity->isa('Slic3r::ExtrusionMultiPath'))
  475. ? @$extrusion_entity
  476. : ($extrusion_entity);
  477. foreach my $path (@paths) {
  478. print "width: ", $path->width,
  479. " height: ", $path->height,
  480. " mm3_per_mm: ", $path->mm3_per_mm,
  481. " height2: ", $path->mm3_per_mm / $path->height,
  482. "\n";
  483. $self->_extrusion_simulator->extrude_to_accumulator($path, $_, $self->_simulation_mode()) for @shifts;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. $self->_extrusion_simulator->evaluate_accumulator($self->_simulation_mode());
  490. }
  491. sub InitGL {
  492. my $self = shift;
  493. return if $self->init;
  494. return unless $self->GetContext;
  495. my $texture_id = 0;
  496. ($texture_id) = glGenTextures_p(1);
  497. $self->_texture_name($texture_id);
  498. $self->init(1);
  499. }
  500. sub GetContext {
  501. my ($self) = @_;
  502. return $self->{context} ||= Wx::GLContext->new($self);
  503. }
  504. sub SetCurrent {
  505. my ($self, $context) = @_;
  506. return $self->SUPER::SetCurrent($context);
  507. }
  508. sub Resize {
  509. my ($self) = @_;
  510. return unless $self->GetContext;
  511. return unless $self->bb;
  512. $self->_dirty(0);
  513. $self->SetCurrent($self->GetContext);
  514. my ($x, $y) = $self->GetSizeWH;
  515. if ($self->_texture_size->x() < $x or $self->_texture_size->y() < $y) {
  516. # Allocate a large enough OpenGL texture with power of 2 dimensions.
  517. $self->_texture_size->set_x(1) if ($self->_texture_size->x() == 0);
  518. $self->_texture_size->set_y(1) if ($self->_texture_size->y() == 0);
  519. $self->_texture_size->set_x($self->_texture_size->x() * 2) while ($self->_texture_size->x() < $x);
  520. $self->_texture_size->set_y($self->_texture_size->y() * 2) while ($self->_texture_size->y() < $y);
  521. #print "screen size ", $x, "x", $y;
  522. #print "texture size ", $self->_texture_size->x(), "x", $self->_texture_size->y();
  523. # Initialize an empty texture.
  524. glBindTexture(GL_TEXTURE_2D, $self->_texture_name);
  525. if (1) {
  526. glTexImage2D_c(GL_TEXTURE_2D,
  527. 0, # level (0 normal, heighr is form mip-mapping)
  528. GL_RGBA, # internal format
  529. $self->_texture_size->x(), $self->_texture_size->y(),
  530. 0, # border
  531. GL_RGBA, # format RGBA color data
  532. GL_UNSIGNED_BYTE, # unsigned byte data
  533. 0); # ptr to texture data
  534. }
  535. glBindTexture(GL_TEXTURE_2D, 0);
  536. $self->_extrusion_simulator->set_image_size($self->_texture_size);
  537. }
  538. $self->_extrusion_simulator->set_viewport(Slic3r::Geometry::BoundingBox->new_from_points(
  539. [Slic3r::Point->new(0, 0), Slic3r::Point->new($x, $y)]));
  540. glViewport(0, 0, $x, $y);
  541. glMatrixMode(GL_PROJECTION);
  542. glLoadIdentity();
  543. my $bb = $self->bb->clone;
  544. # center bounding box around origin before scaling it
  545. my $bb_center = $bb->center;
  546. $bb->translate(@{$bb_center->negative});
  547. # scale bounding box according to zoom factor
  548. $bb->scale($self->_zoom);
  549. # reposition bounding box around original center
  550. $bb->translate(@{$bb_center});
  551. # translate camera
  552. $bb->translate(@{$self->_camera_target});
  553. # keep camera_bb within total bb
  554. # (i.e. prevent user from panning outside the bounding box)
  555. {
  556. my @translate = (0,0);
  557. if ($bb->x_min < $self->bb->x_min) {
  558. $translate[X] += $self->bb->x_min - $bb->x_min;
  559. }
  560. if ($bb->y_min < $self->bb->y_min) {
  561. $translate[Y] += $self->bb->y_min - $bb->y_min;
  562. }
  563. if ($bb->x_max > $self->bb->x_max) {
  564. $translate[X] -= $bb->x_max - $self->bb->x_max;
  565. }
  566. if ($bb->y_max > $self->bb->y_max) {
  567. $translate[Y] -= $bb->y_max - $self->bb->y_max;
  568. }
  569. $self->_camera_target->translate(@translate);
  570. $bb->translate(@translate);
  571. }
  572. # save camera
  573. $self->_camera_bb($bb);
  574. my ($x1, $y1, $x2, $y2) = ($bb->x_min, $bb->y_min, $bb->x_max, $bb->y_max);
  575. if (($x2 - $x1)/($y2 - $y1) > $x/$y) {
  576. # adjust Y
  577. my $new_y = $y * ($x2 - $x1) / $x;
  578. $y1 = ($y2 + $y1)/2 - $new_y/2;
  579. $y2 = $y1 + $new_y;
  580. } else {
  581. my $new_x = $x * ($y2 - $y1) / $y;
  582. $x1 = ($x2 + $x1)/2 - $new_x/2;
  583. $x2 = $x1 + $new_x;
  584. }
  585. glOrtho($x1, $x2, $y1, $y2, 0, 1);
  586. # Set the adjusted bounding box at the extrusion simulator.
  587. #print "Scene bbox ", $bb->x_min, ",", $bb->y_min, " ", $bb->x_max, ",", $bb->y_max, "\n";
  588. #print "Setting simulator bbox ", $x1, ",", $y1, " ", $x2, ",", $y2, "\n";
  589. $self->_extrusion_simulator->set_bounding_box(
  590. Slic3r::Geometry::BoundingBox->new_from_points(
  591. [Slic3r::Point->new($x1, $y1), Slic3r::Point->new($x2, $y2)]));
  592. glMatrixMode(GL_MODELVIEW);
  593. }
  594. # Thick line drawing is not used anywhere. Probably not tested?
  595. sub line {
  596. my (
  597. $x1, $y1, $x2, $y2, # coordinates of the line
  598. $w, # width/thickness of the line in pixel
  599. $Cr, $Cg, $Cb, # RGB color components
  600. $Br, $Bg, $Bb, # color of background when alphablend=false
  601. # Br=alpha of color when alphablend=true
  602. $alphablend, # use alpha blend or not
  603. ) = @_;
  604. my $t;
  605. my $R;
  606. my $f = $w - int($w);
  607. my $A;
  608. if ($alphablend) {
  609. $A = $Br;
  610. } else {
  611. $A = 1;
  612. }
  613. # determine parameters t,R
  614. if ($w >= 0 && $w < 1) {
  615. $t = 0.05; $R = 0.48 + 0.32 * $f;
  616. if (!$alphablend) {
  617. $Cr += 0.88 * (1-$f);
  618. $Cg += 0.88 * (1-$f);
  619. $Cb += 0.88 * (1-$f);
  620. $Cr = 1.0 if ($Cr > 1.0);
  621. $Cg = 1.0 if ($Cg > 1.0);
  622. $Cb = 1.0 if ($Cb > 1.0);
  623. } else {
  624. $A *= $f;
  625. }
  626. } elsif ($w >= 1.0 && $w < 2.0) {
  627. $t = 0.05 + $f*0.33; $R = 0.768 + 0.312*$f;
  628. } elsif ($w >= 2.0 && $w < 3.0) {
  629. $t = 0.38 + $f*0.58; $R = 1.08;
  630. } elsif ($w >= 3.0 && $w < 4.0) {
  631. $t = 0.96 + $f*0.48; $R = 1.08;
  632. } elsif ($w >= 4.0 && $w < 5.0) {
  633. $t= 1.44 + $f*0.46; $R = 1.08;
  634. } elsif ($w >= 5.0 && $w < 6.0) {
  635. $t= 1.9 + $f*0.6; $R = 1.08;
  636. } elsif ($w >= 6.0) {
  637. my $ff = $w - 6.0;
  638. $t = 2.5 + $ff*0.50; $R = 1.08;
  639. }
  640. #printf( "w=%f, f=%f, C=%.4f\n", $w, $f, $C);
  641. # determine angle of the line to horizontal
  642. my $tx = 0; my $ty = 0; # core thinkness of a line
  643. my $Rx = 0; my $Ry = 0; # fading edge of a line
  644. my $cx = 0; my $cy = 0; # cap of a line
  645. my $ALW = 0.01;
  646. my $dx = $x2 - $x1;
  647. my $dy = $y2 - $y1;
  648. if (abs($dx) < $ALW) {
  649. # vertical
  650. $tx = $t; $ty = 0;
  651. $Rx = $R; $Ry = 0;
  652. if ($w > 0.0 && $w < 1.0) {
  653. $tx *= 8;
  654. } elsif ($w == 1.0) {
  655. $tx *= 10;
  656. }
  657. } elsif (abs($dy) < $ALW) {
  658. #horizontal
  659. $tx = 0; $ty = $t;
  660. $Rx = 0; $Ry = $R;
  661. if ($w > 0.0 && $w < 1.0) {
  662. $ty *= 8;
  663. } elsif ($w == 1.0) {
  664. $ty *= 10;
  665. }
  666. } else {
  667. if ($w < 3) { # approximate to make things even faster
  668. my $m = $dy/$dx;
  669. # and calculate tx,ty,Rx,Ry
  670. if ($m > -0.4142 && $m <= 0.4142) {
  671. # -22.5 < $angle <= 22.5, approximate to 0 (degree)
  672. $tx = $t * 0.1; $ty = $t;
  673. $Rx = $R * 0.6; $Ry = $R;
  674. } elsif ($m > 0.4142 && $m <= 2.4142) {
  675. # 22.5 < $angle <= 67.5, approximate to 45 (degree)
  676. $tx = $t * -0.7071; $ty = $t * 0.7071;
  677. $Rx = $R * -0.7071; $Ry = $R * 0.7071;
  678. } elsif ($m > 2.4142 || $m <= -2.4142) {
  679. # 67.5 < $angle <= 112.5, approximate to 90 (degree)
  680. $tx = $t; $ty = $t*0.1;
  681. $Rx = $R; $Ry = $R*0.6;
  682. } elsif ($m > -2.4142 && $m < -0.4142) {
  683. # 112.5 < angle < 157.5, approximate to 135 (degree)
  684. $tx = $t * 0.7071; $ty = $t * 0.7071;
  685. $Rx = $R * 0.7071; $Ry = $R * 0.7071;
  686. } else {
  687. # error in determining angle
  688. printf("error in determining angle: m=%.4f\n", $m);
  689. }
  690. } else { # calculate to exact
  691. $dx= $y1 - $y2;
  692. $dy= $x2 - $x1;
  693. my $L = sqrt($dx*$dx + $dy*$dy);
  694. $dx /= $L;
  695. $dy /= $L;
  696. $cx = -0.6*$dy; $cy=0.6*$dx;
  697. $tx = $t*$dx; $ty = $t*$dy;
  698. $Rx = $R*$dx; $Ry = $R*$dy;
  699. }
  700. }
  701. # draw the line by triangle strip
  702. glBegin(GL_TRIANGLE_STRIP);
  703. if (!$alphablend) {
  704. glColor3f($Br, $Bg, $Bb);
  705. } else {
  706. glColor4f($Cr, $Cg, $Cb, 0);
  707. }
  708. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry); # fading edge
  709. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  710. if (!$alphablend) {
  711. glColor3f($Cr, $Cg, $Cb);
  712. } else {
  713. glColor4f($Cr, $Cg, $Cb, $A);
  714. }
  715. glVertex2f($x1 - $tx, $y1 - $ty); # core
  716. glVertex2f($x2 - $tx, $y2 - $ty);
  717. glVertex2f($x1 + $tx, $y1 + $ty);
  718. glVertex2f($x2 + $tx, $y2 + $ty);
  719. if ((abs($dx) < $ALW || abs($dy) < $ALW) && $w <= 1.0) {
  720. # printf("skipped one fading edge\n");
  721. } else {
  722. if (!$alphablend) {
  723. glColor3f($Br, $Bg, $Bb);
  724. } else {
  725. glColor4f($Cr, $Cg, $Cb, 0);
  726. }
  727. glVertex2f($x1 + $tx+ $Rx, $y1 + $ty + $Ry); # fading edge
  728. glVertex2f($x2 + $tx+ $Rx, $y2 + $ty + $Ry);
  729. }
  730. glEnd();
  731. # cap
  732. if ($w < 3) {
  733. # do not draw cap
  734. } else {
  735. # draw cap
  736. glBegin(GL_TRIANGLE_STRIP);
  737. if (!$alphablend) {
  738. glColor3f($Br, $Bg, $Bb);
  739. } else {
  740. glColor4f($Cr, $Cg, $Cb, 0);
  741. }
  742. glVertex2f($x1 - $Rx + $cx, $y1 - $Ry + $cy);
  743. glVertex2f($x1 + $Rx + $cx, $y1 + $Ry + $cy);
  744. glColor3f($Cr, $Cg, $Cb);
  745. glVertex2f($x1 - $tx - $Rx, $y1 - $ty - $Ry);
  746. glVertex2f($x1 + $tx + $Rx, $y1 + $ty + $Ry);
  747. glEnd();
  748. glBegin(GL_TRIANGLE_STRIP);
  749. if (!$alphablend) {
  750. glColor3f($Br, $Bg, $Bb);
  751. } else {
  752. glColor4f($Cr, $Cg, $Cb, 0);
  753. }
  754. glVertex2f($x2 - $Rx - $cx, $y2 - $Ry - $cy);
  755. glVertex2f($x2 + $Rx - $cx, $y2 + $Ry - $cy);
  756. glColor3f($Cr, $Cg, $Cb);
  757. glVertex2f($x2 - $tx - $Rx, $y2 - $ty - $Ry);
  758. glVertex2f($x2 + $tx + $Rx, $y2 + $ty + $Ry);
  759. glEnd();
  760. }
  761. }
  762. package Slic3r::GUI::Plater::2DToolpaths::Dialog;
  763. use Wx qw(:dialog :id :misc :sizer);
  764. use Wx::Event qw(EVT_CLOSE);
  765. use base 'Wx::Dialog';
  766. sub new {
  767. my $class = shift;
  768. my ($parent, $print) = @_;
  769. my $self = $class->SUPER::new($parent, -1, "Toolpaths", wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  770. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  771. $sizer->Add(Slic3r::GUI::Plater::2DToolpaths->new($self, $print), 1, wxEXPAND, 0);
  772. $self->SetSizer($sizer);
  773. $self->SetMinSize($self->GetSize);
  774. # needed to actually free memory
  775. EVT_CLOSE($self, sub {
  776. $self->EndModal(wxID_OK);
  777. $self->Destroy;
  778. });
  779. return $self;
  780. }
  781. 1;