Model.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. package Slic3r::Model;
  2. use List::Util qw(first max);
  3. use Slic3r::Geometry qw(X Y Z move_points);
  4. sub read_from_file {
  5. my $class = shift;
  6. my ($input_file) = @_;
  7. my $model = $input_file =~ /\.stl$/i ? Slic3r::Format::STL->read_file($input_file)
  8. : $input_file =~ /\.obj$/i ? Slic3r::Format::OBJ->read_file($input_file)
  9. : $input_file =~ /\.amf(\.xml)?$/i ? Slic3r::Format::AMF->read_file($input_file)
  10. : die "Input file must have .stl, .obj or .amf(.xml) extension\n";
  11. $_->set_input_file($input_file) for @{$model->objects};
  12. return $model;
  13. }
  14. sub merge {
  15. my $class = shift;
  16. my @models = @_;
  17. my $new_model = ref($class)
  18. ? $class
  19. : $class->new;
  20. $new_model->add_object($_) for map @{$_->objects}, @models;
  21. return $new_model;
  22. }
  23. sub add_object {
  24. my $self = shift;
  25. if (@_ == 1) {
  26. # we have a Model::Object
  27. my ($object) = @_;
  28. return $self->_add_object_clone($object);
  29. } else {
  30. my (%args) = @_;
  31. my $new_object = $self->_add_object;
  32. $new_object->set_input_file($args{input_file})
  33. if defined $args{input_file};
  34. $new_object->config->apply($args{config})
  35. if defined $args{config};
  36. $new_object->set_layer_height_ranges($args{layer_height_ranges})
  37. if defined $args{layer_height_ranges};
  38. $new_object->set_origin_translation($args{origin_translation})
  39. if defined $args{origin_translation};
  40. return $new_object;
  41. }
  42. }
  43. sub set_material {
  44. my $self = shift;
  45. my ($material_id, $attributes) = @_;
  46. my $material = $self->add_material($material_id);
  47. $material->apply($attributes // {});
  48. return $material;
  49. }
  50. sub duplicate_objects_grid {
  51. my ($self, $grid, $distance) = @_;
  52. die "Grid duplication is not supported with multiple objects\n"
  53. if @{$self->objects} > 1;
  54. my $object = $self->objects->[0];
  55. $object->clear_instances;
  56. my $size = $object->bounding_box->size;
  57. for my $x_copy (1..$grid->[X]) {
  58. for my $y_copy (1..$grid->[Y]) {
  59. $object->add_instance(
  60. offset => Slic3r::Pointf->new(
  61. ($size->[X] + $distance) * ($x_copy-1),
  62. ($size->[Y] + $distance) * ($y_copy-1),
  63. ),
  64. );
  65. }
  66. }
  67. }
  68. # this will append more instances to each object
  69. # and then automatically rearrange everything
  70. sub duplicate_objects {
  71. my ($self, $copies_num, $distance, $bb) = @_;
  72. foreach my $object (@{$self->objects}) {
  73. my @instances = @{$object->instances};
  74. foreach my $instance (@instances) {
  75. $object->add_instance($instance) for 2..$copies_num;
  76. }
  77. }
  78. $self->arrange_objects($distance, $bb);
  79. }
  80. # arrange objects preserving their instance count
  81. # but altering their instance positions
  82. sub arrange_objects {
  83. my ($self, $distance, $bb) = @_;
  84. # get the (transformed) size of each instance so that we take
  85. # into account their different transformations when packing
  86. my @instance_sizes = ();
  87. foreach my $object (@{$self->objects}) {
  88. push @instance_sizes, map $object->instance_bounding_box($_)->size, 0..$#{$object->instances};
  89. }
  90. my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
  91. foreach my $object (@{$self->objects}) {
  92. $_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
  93. $object->update_bounding_box;
  94. }
  95. }
  96. # duplicate the entire model preserving instance relative positions
  97. sub duplicate {
  98. my ($self, $copies_num, $distance, $bb) = @_;
  99. my $model_size = $self->bounding_box->size;
  100. my @positions = $self->_arrange([ map $model_size, 2..$copies_num ], $distance, $bb);
  101. # note that this will leave the object count unaltered
  102. foreach my $object (@{$self->objects}) {
  103. my @instances = @{$object->instances}; # store separately to avoid recursion from add_instance() below
  104. foreach my $instance (@instances) {
  105. foreach my $pos (@positions) {
  106. $object->add_instance(
  107. offset => Slic3r::Pointf->new($instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y]),
  108. rotation => $instance->rotation,
  109. scaling_factor => $instance->scaling_factor,
  110. );
  111. }
  112. }
  113. $object->update_bounding_box;
  114. }
  115. }
  116. sub _arrange {
  117. my ($self, $sizes, $distance, $bb) = @_;
  118. # we supply unscaled data to arrange()
  119. return Slic3r::Geometry::arrange(
  120. scalar(@$sizes), # number of parts
  121. max(map $_->x, @$sizes), # cell width
  122. max(map $_->y, @$sizes), # cell height ,
  123. $distance, # distance between cells
  124. $bb, # bounding box of the area to fill (can be undef)
  125. );
  126. }
  127. sub has_objects_with_no_instances {
  128. my ($self) = @_;
  129. return (first { !defined $_->instances } @{$self->objects}) ? 1 : 0;
  130. }
  131. # makes sure all objects have at least one instance
  132. sub add_default_instances {
  133. my ($self) = @_;
  134. # apply a default position to all objects not having one
  135. my $added = 0;
  136. foreach my $object (@{$self->objects}) {
  137. if ($object->instances_count == 0) {
  138. $object->add_instance(offset => Slic3r::Pointf->new(0,0));
  139. $added = 1;
  140. }
  141. }
  142. return $added;
  143. }
  144. # this returns the bounding box of the *transformed* instances
  145. sub bounding_box {
  146. my $self = shift;
  147. return undef if !@{$self->objects};
  148. my $bb = $self->objects->[0]->bounding_box;
  149. $bb->merge($_->bounding_box) for @{$self->objects}[1..$#{$self->objects}];
  150. return $bb;
  151. }
  152. # input point is expressed in unscaled coordinates
  153. sub center_instances_around_point {
  154. my ($self, $point) = @_;
  155. my $bb = $self->bounding_box;
  156. return if !defined $bb;
  157. my $size = $bb->size;
  158. my @shift = (
  159. -$bb->x_min + $point->[X] - $size->x/2,
  160. -$bb->y_min + $point->[Y] - $size->y/2, #//
  161. );
  162. foreach my $object (@{$self->objects}) {
  163. foreach my $instance (@{$object->instances}) {
  164. $instance->set_offset(Slic3r::Pointf->new(
  165. $instance->offset->x + $shift[X],
  166. $instance->offset->y + $shift[Y], #++
  167. ));
  168. }
  169. $object->update_bounding_box;
  170. }
  171. }
  172. sub align_instances_to_origin {
  173. my ($self) = @_;
  174. my $bb = $self->bounding_box;
  175. return if !defined $bb;
  176. my $new_center = $bb->size;
  177. $new_center->translate(-$new_center->x/2, -$new_center->y/2); #//
  178. $self->center_instances_around_point($new_center);
  179. }
  180. sub translate {
  181. my $self = shift;
  182. my @shift = @_;
  183. $_->translate(@shift) for @{$self->objects};
  184. }
  185. # flattens everything to a single mesh
  186. sub mesh {
  187. my $self = shift;
  188. my $mesh = Slic3r::TriangleMesh->new;
  189. $mesh->merge($_->mesh) for @{$self->objects};
  190. return $mesh;
  191. }
  192. # flattens everything to a single mesh
  193. sub raw_mesh {
  194. my $self = shift;
  195. my $mesh = Slic3r::TriangleMesh->new;
  196. $mesh->merge($_->raw_mesh) for @{$self->objects};
  197. return $mesh;
  198. }
  199. # this method splits objects into multiple distinct objects by walking their meshes
  200. sub split_meshes {
  201. my $self = shift;
  202. my @objects = @{$self->objects};
  203. @{$self->objects} = ();
  204. foreach my $object (@objects) {
  205. if (@{$object->volumes} > 1) {
  206. # We can't split meshes if there's more than one material, because
  207. # we can't group the resulting meshes by object afterwards
  208. $self->_add_object($object);
  209. next;
  210. }
  211. my $volume = $object->volumes->[0];
  212. foreach my $mesh (@{$volume->mesh->split}) {
  213. my $new_object = $self->add_object(
  214. input_file => $object->input_file,
  215. config => $object->config->clone,
  216. layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned
  217. origin_translation => $object->origin_translation,
  218. );
  219. $new_object->add_volume(
  220. mesh => $mesh,
  221. material_id => $volume->material_id,
  222. );
  223. # add one instance per original instance
  224. $new_object->add_instance(
  225. offset => Slic3r::Pointf->new(@{$_->offset}),
  226. rotation => $_->rotation,
  227. scaling_factor => $_->scaling_factor,
  228. ) for @{ $object->instances // [] };
  229. }
  230. }
  231. }
  232. sub print_info {
  233. my $self = shift;
  234. $_->print_info for @{$self->objects};
  235. }
  236. sub get_material_name {
  237. my $self = shift;
  238. my ($material_id) = @_;
  239. my $name;
  240. if ($self->has_material($material_id)) {
  241. $name //= $self->get_material($material_id)
  242. ->attributes->{$_} for qw(Name name);
  243. }
  244. $name //= $material_id;
  245. return $name;
  246. }
  247. package Slic3r::Model::Material;
  248. sub apply {
  249. my ($self, $attributes) = @_;
  250. $self->set_attribute($_, $attributes{$_}) for keys %$attributes;
  251. }
  252. package Slic3r::Model::Object;
  253. use File::Basename qw(basename);
  254. use List::Util qw(first sum);
  255. use Slic3r::Geometry qw(X Y Z rad2deg);
  256. sub add_volume {
  257. my $self = shift;
  258. my $new_volume;
  259. if (@_ == 1) {
  260. # we have a Model::Volume
  261. my ($volume) = @_;
  262. $new_volume = $self->_add_volume_clone($volume);
  263. # TODO: material_id can't be undef.
  264. if (defined $volume->material_id) {
  265. # merge material attributes and config (should we rename materials in case of duplicates?)
  266. if (my $material = $volume->object->model->get_material($volume->material_id)) {
  267. my %attributes = %{ $material->attributes };
  268. if ($self->model->has_material($volume->material_id)) {
  269. %attributes = (%attributes, %{ $self->model->get_material($volume->material_id)->attributes })
  270. }
  271. my $new_material = $self->model->set_material($volume->material_id, {%attributes});
  272. $new_material->config->apply($material->config);
  273. }
  274. }
  275. } else {
  276. my %args = @_;
  277. $new_volume = $self->_add_volume($args{mesh});
  278. $new_volume->set_material_id($args{material_id})
  279. if defined $args{material_id};
  280. $new_volume->set_modifier($args{modifier})
  281. if defined $args{modifier};
  282. }
  283. if (defined $new_volume->material_id && !defined $self->model->get_material($new_volume->material_id)) {
  284. # TODO: this should be a trigger on Volume::material_id
  285. $self->model->set_material($new_volume->material_id);
  286. }
  287. $self->invalidate_bounding_box;
  288. return $new_volume;
  289. }
  290. sub add_instance {
  291. my $self = shift;
  292. my %params = @_;
  293. if (@_ == 1) {
  294. # we have a Model::Instance
  295. my ($instance) = @_;
  296. return $self->_add_instance_clone($instance);
  297. } else {
  298. my (%args) = @_;
  299. my $new_instance = $self->_add_instance;
  300. $new_instance->set_rotation($args{rotation})
  301. if defined $args{rotation};
  302. $new_instance->set_scaling_factor($args{scaling_factor})
  303. if defined $args{scaling_factor};
  304. $new_instance->set_offset($args{offset})
  305. if defined $args{offset};
  306. return $new_instance;
  307. }
  308. }
  309. sub raw_mesh {
  310. my $self = shift;
  311. my $mesh = Slic3r::TriangleMesh->new;
  312. $mesh->merge($_->mesh) for grep !$_->modifier, @{ $self->volumes };
  313. return $mesh;
  314. }
  315. sub raw_bounding_box {
  316. my $self = shift;
  317. my @meshes = map $_->mesh, grep !$_->modifier, @{ $self->volumes };
  318. die "No meshes found" if !@meshes;
  319. my $bb = (shift @meshes)->bounding_box;
  320. $bb->merge($_->bounding_box) for @meshes;
  321. return $bb;
  322. }
  323. # flattens all volumes and instances into a single mesh
  324. sub mesh {
  325. my $self = shift;
  326. my $mesh = $self->raw_mesh;
  327. my @instance_meshes = ();
  328. foreach my $instance (@{ $self->instances }) {
  329. my $m = $mesh->clone;
  330. $instance->transform_mesh($m);
  331. push @instance_meshes, $m;
  332. }
  333. my $full_mesh = Slic3r::TriangleMesh->new;
  334. $full_mesh->merge($_) for @instance_meshes;
  335. return $full_mesh;
  336. }
  337. sub update_bounding_box {
  338. my ($self) = @_;
  339. $self->_bounding_box($self->mesh->bounding_box);
  340. }
  341. # this returns the bounding box of the *transformed* instances
  342. sub bounding_box {
  343. my $self = shift;
  344. $self->update_bounding_box if !defined $self->_bounding_box;
  345. return $self->_bounding_box->clone;
  346. }
  347. # this returns the bounding box of the *transformed* given instance
  348. sub instance_bounding_box {
  349. my ($self, $instance_idx) = @_;
  350. my $mesh = $self->raw_mesh;
  351. $self->instances->[$instance_idx]->transform_mesh($mesh);
  352. return $mesh->bounding_box;
  353. }
  354. sub center_around_origin {
  355. my $self = shift;
  356. # calculate the displacements needed to
  357. # center this object around the origin
  358. my $bb = $self->raw_mesh->bounding_box;
  359. # first align to origin on XY
  360. my @shift = (
  361. -$bb->x_min,
  362. -$bb->y_min,
  363. 0,
  364. );
  365. # then center it on XY
  366. my $size = $bb->size;
  367. $shift[X] -= $size->x/2;
  368. $shift[Y] -= $size->y/2; #//
  369. $self->translate(@shift);
  370. $self->origin_translation->translate(@shift[X,Y]);
  371. if ($self->instances_count > 0) {
  372. foreach my $instance (@{ $self->instances }) {
  373. $instance->set_offset(Slic3r::Pointf->new(
  374. $instance->offset->x - $shift[X],
  375. $instance->offset->y - $shift[Y], #--
  376. ));
  377. }
  378. $self->update_bounding_box;
  379. }
  380. return @shift;
  381. }
  382. sub translate {
  383. my $self = shift;
  384. my @shift = @_;
  385. $_->mesh->translate(@shift) for @{$self->volumes};
  386. $self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
  387. }
  388. sub rotate {
  389. my ($self, $angle, $axis) = @_;
  390. # we accept angle in radians but mesh currently uses degrees
  391. $angle = rad2deg($angle);
  392. if ($axis == X) {
  393. $_->mesh->rotate_x($angle) for @{$self->volumes};
  394. } elsif ($axis == Y) {
  395. $_->mesh->rotate_y($angle) for @{$self->volumes};
  396. } elsif ($axis == Z) {
  397. $_->mesh->rotate_z($angle) for @{$self->volumes};
  398. }
  399. $self->invalidate_bounding_box;
  400. }
  401. sub flip {
  402. my ($self, $axis) = @_;
  403. if ($axis == X) {
  404. $_->mesh->flip_x for @{$self->volumes};
  405. } elsif ($axis == Y) {
  406. $_->mesh->flip_y for @{$self->volumes};
  407. } elsif ($axis == Z) {
  408. $_->mesh->flip_z for @{$self->volumes};
  409. }
  410. $self->invalidate_bounding_box;
  411. }
  412. sub scale_xyz {
  413. my ($self, $versor) = @_;
  414. $_->mesh->scale_xyz($versor) for @{$self->volumes};
  415. $self->invalidate_bounding_box;
  416. }
  417. sub materials_count {
  418. my $self = shift;
  419. my %materials = map { $_->material_id // '_default' => 1 } @{$self->volumes};
  420. return scalar keys %materials;
  421. }
  422. sub unique_materials {
  423. my $self = shift;
  424. my %materials = ();
  425. $materials{ $_->material_id } = 1
  426. for grep { defined $_->material_id } @{$self->volumes};
  427. return sort keys %materials;
  428. }
  429. sub facets_count {
  430. my $self = shift;
  431. return sum(map $_->mesh->facets_count, grep !$_->modifier, @{$self->volumes});
  432. }
  433. sub needed_repair {
  434. my $self = shift;
  435. return (first { !$_->mesh->needed_repair } grep !$_->modifier, @{$self->volumes}) ? 0 : 1;
  436. }
  437. sub mesh_stats {
  438. my $self = shift;
  439. # TODO: sum values from all volumes
  440. return $self->volumes->[0]->mesh->stats;
  441. }
  442. sub print_info {
  443. my $self = shift;
  444. printf "Info about %s:\n", basename($self->input_file);
  445. printf " size: x=%.3f y=%.3f z=%.3f\n", @{$self->raw_mesh->bounding_box->size};
  446. if (my $stats = $self->mesh_stats) {
  447. printf " number of facets: %d\n", $stats->{number_of_facets};
  448. printf " number of shells: %d\n", $stats->{number_of_parts};
  449. printf " volume: %.3f\n", $stats->{volume};
  450. if ($self->needed_repair) {
  451. printf " needed repair: yes\n";
  452. printf " degenerate facets: %d\n", $stats->{degenerate_facets};
  453. printf " edges fixed: %d\n", $stats->{edges_fixed};
  454. printf " facets removed: %d\n", $stats->{facets_removed};
  455. printf " facets added: %d\n", $stats->{facets_added};
  456. printf " facets reversed: %d\n", $stats->{facets_reversed};
  457. printf " backwards edges: %d\n", $stats->{backwards_edges};
  458. } else {
  459. printf " needed repair: no\n";
  460. }
  461. } else {
  462. printf " number of facets: %d\n", scalar(map @{$_->facets}, grep !$_->modifier, @{$self->volumes});
  463. }
  464. }
  465. sub cut {
  466. my ($self, $z) = @_;
  467. # clone this one to duplicate instances, materials etc.
  468. my $model = Slic3r::Model->new;
  469. my $upper = $model->add_object($self);
  470. my $lower = $model->add_object($self);
  471. $upper->clear_volumes;
  472. $lower->clear_volumes;
  473. foreach my $volume (@{$self->volumes}) {
  474. if ($volume->modifier) {
  475. # don't cut modifiers
  476. $upper->add_volume($volume);
  477. $lower->add_volume($volume);
  478. } else {
  479. my $upper_mesh = Slic3r::TriangleMesh->new;
  480. my $lower_mesh = Slic3r::TriangleMesh->new;
  481. $volume->mesh->cut($z + $volume->mesh->bounding_box->z_min, $upper_mesh, $lower_mesh);
  482. $upper_mesh->repair;
  483. $lower_mesh->repair;
  484. $upper_mesh->reset_repair_stats;
  485. $lower_mesh->reset_repair_stats;
  486. if ($upper_mesh->facets_count > 0) {
  487. $upper->add_volume(
  488. material_id => $volume->material_id,
  489. mesh => $upper_mesh,
  490. modifier => $volume->modifier,
  491. );
  492. }
  493. if ($lower_mesh->facets_count > 0) {
  494. $lower->add_volume(
  495. material_id => $volume->material_id,
  496. mesh => $lower_mesh,
  497. modifier => $volume->modifier,
  498. );
  499. }
  500. }
  501. }
  502. $upper = undef if !@{$upper->volumes};
  503. $lower = undef if !@{$lower->volumes};
  504. return ($model, $upper, $lower);
  505. }
  506. package Slic3r::Model::Volume;
  507. sub assign_unique_material {
  508. my ($self) = @_;
  509. my $model = $self->object->model;
  510. my $material_id = 1 + $model->material_count;
  511. $self->material_id($material_id);
  512. return $model->set_material($material_id);
  513. }
  514. package Slic3r::Model::Instance;
  515. sub transform_mesh {
  516. my ($self, $mesh, $dont_translate) = @_;
  517. $mesh->rotate($self->rotation, Slic3r::Point->new(0,0)); # rotate around mesh origin
  518. $mesh->scale($self->scaling_factor); # scale around mesh origin
  519. $mesh->translate(@{$self->offset}, 0) unless $dont_translate;
  520. }
  521. sub transform_polygon {
  522. my ($self, $polygon) = @_;
  523. $polygon->rotate($self->rotation, Slic3r::Point->new(0,0)); # rotate around origin
  524. $polygon->scale($self->scaling_factor); # scale around origin
  525. }
  526. 1;