Skip to content

Commit acfd133

Browse files
Fix phases to properly reflect rhombic dodecahedron (3 distinct periods)
1 parent 161a0a6 commit acfd133

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/libslic3r/Fill/FillRhodo.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ static inline void reset_polyline(
1717
Polyline &polyline,
1818
Polylines &all_polylines)
1919
{
20-
if (polyline.points.size() < 2) return;
21-
polyline.rotate(-angle, hex_center); // rotate back to model frame if any points were collected
22-
all_polylines.emplace_back(std::move(polyline));
20+
if (polyline.points.size() == 0) return;
21+
if (polyline.points.size() > 1) {
22+
polyline.rotate(-angle, hex_center); // rotate back to model frame if any points were collected
23+
all_polylines.emplace_back(std::move(polyline));
24+
}
2325
polyline.points.clear();
2426
}
2527

@@ -37,28 +39,48 @@ void FillRhodo::_fill_surface_single(
3739
const coord_t tile_height = coord_t(hex_side * 3); // tilable / periodic every other row
3840
const Point hex_center = Point(hex_width / 2, hex_side);
3941

40-
// Compute normalized z phase in [0, 3) relative to hex_side, using double for phase only.
42+
// Compute normalized z phase in [0, 4.5) relative to hex_side, using double for phase only.
4143
const double unscaled_z = this->z; // mm
4244
const double unscaled_hex_side = this->spacing / params.density;
43-
const double z_phase = std::fmod(unscaled_z / unscaled_hex_side, 3.0);
45+
const double z_phase = std::fmod(unscaled_z / unscaled_hex_side, 4.5);
4446

4547
int permutation = 0; // 0: upright triangles, 1: upside-down
4648
double tri_frac = 0.0;
49+
coord_t phase_y_offset = 0;
50+
coord_t phase_x_offset = 0;
4751
if (z_phase < 1.0) {
4852
tri_frac = 0.0;
4953
} else if (z_phase < 1.25) {
5054
tri_frac = (z_phase - 1.0) * 4.0;
5155
} else if (z_phase < 1.5) {
5256
tri_frac = (1.5 - z_phase) * 4.0;
5357
permutation = 1;
58+
phase_y_offset = hex_side / 2.0;
5459
} else if (z_phase < 2.5) {
5560
tri_frac = 0.0;
5661
permutation = 1;
62+
phase_y_offset = hex_side / 2.0;
5763
} else if (z_phase < 2.75) {
5864
tri_frac = (z_phase - 2.5) * 4.0;
65+
phase_y_offset = hex_side / 2.0;
66+
phase_x_offset = -hex_width / 2.0;
67+
} else if (z_phase < 3.0) {
68+
tri_frac = (3.0 - z_phase) * 4.0;
69+
permutation = 1;
70+
phase_y_offset = hex_side;
71+
phase_x_offset = -hex_width / 2.0;
72+
} else if (z_phase < 4.0) {
73+
tri_frac = 0.0;
5974
permutation = 1;
75+
phase_y_offset = hex_side;
76+
phase_x_offset = -hex_width / 2.0;
77+
} else if (z_phase < 4.25) {
78+
tri_frac = (z_phase - 4.0) * 4.0;
79+
phase_y_offset = hex_side;
6080
} else {
61-
tri_frac = (3.0 - z_phase) * 4.0;
81+
tri_frac = (4.5 - z_phase) * 4.0;
82+
permutation = 1;
83+
phase_x_offset = -hex_width / 2.0;
6284
}
6385

6486
const coord_t tri_w = coord_t(std::llround(hex_width * tri_frac));
@@ -90,13 +112,13 @@ void FillRhodo::_fill_surface_single(
90112
for (size_t i = 0; i < num_rows; ++i) {
91113
Polyline polyline;
92114
// Row y origin in aligned grid frame
93-
coord_t y_offset = y_start + coord_t(i) * pattern_height;
115+
coord_t y_offset = y_start + phase_y_offset + coord_t(i) * pattern_height;
94116
if (permutation == 0) {
95117
// phase with triangles at top-left/top-center transitions
96118
// even/odd rows alternate direction
97119
if ((i % 2) == 0) {
98120
for (size_t j = 0; j < num_cols; ++j) {
99-
coord_t x_offset = bbox.min(0) + coord_t(j) * hex_width;
121+
coord_t x_offset = bbox.min(0) + phase_x_offset + coord_t(j) * hex_width;
100122
// top-left tri right
101123
polyline.points.emplace_back(x_offset - hex_width / 2 + tri_half_w, y_offset - hex_side / 2 + coord_t(std::llround(double(tri_half_w) / sqrt(3))));
102124
// hex top left
@@ -119,7 +141,7 @@ void FillRhodo::_fill_surface_single(
119141
}
120142
} else {
121143
for (size_t j = num_cols; j-- > 0; ) {
122-
coord_t x_offset = bbox.min(0) + coord_t(j) * hex_width - hex_width / 2;
144+
coord_t x_offset = bbox.min(0) + phase_x_offset + coord_t(j) * hex_width - hex_width / 2;
123145
// hex top right
124146
polyline.points.emplace_back(x_offset, y_offset);
125147
// right tri top
@@ -142,11 +164,9 @@ void FillRhodo::_fill_surface_single(
142164
}
143165
}
144166
} else {
145-
// permutation 1, shifted by hex_side/2 vertically
146-
y_offset += hex_side / 2;
147167
if ((i % 2) == 0) {
148168
for (size_t j = 0; j < num_cols; ++j) {
149-
coord_t x_offset = bbox.min(0) + coord_t(j) * hex_width - hex_width / 2;
169+
coord_t x_offset = bbox.min(0) + phase_x_offset + coord_t(j) * hex_width - hex_width / 2;
150170
// left tri right
151171
polyline.points.emplace_back(x_offset + tri_half_w, y_offset - coord_t(std::llround(double(tri_half_w) / sqrt(3))));
152172
// left tri bottom
@@ -169,7 +189,7 @@ void FillRhodo::_fill_surface_single(
169189
}
170190
} else {
171191
for (size_t j = num_cols; j-- > 0; ) {
172-
coord_t x_offset = bbox.min(0) + coord_t(j) * hex_width;
192+
coord_t x_offset = bbox.min(0) + phase_x_offset + coord_t(j) * hex_width;
173193
// right tri left
174194
polyline.points.emplace_back(x_offset - tri_half_w, y_offset - coord_t(std::llround(double(tri_half_w) / sqrt(3))));
175195
// right tri bottom

0 commit comments

Comments
 (0)