Skip to content

Commit 73aef0f

Browse files
committed
Add --flip-x argument to optionally mirror the output.
When used with the usual cookie cutter profile, this makes the input shape correspond to the shape of the final cookie rather than the shape of the cookie cutter sitting upside down on the printer. This doesn't really belong in here (sweep should not know anything about cookie cutters, but just process the input files as they are), but it was the easiest point to insert it in the Inkscape integration.
1 parent efb6d25 commit 73aef0f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

sweep.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,34 @@ void writeStlTriangle(const Vertex& v1, const Vertex& v2, const Vertex& v3, FILE
379379
}
380380

381381
int main(int argc, char **argv) {
382-
if (argc < 4) {
383-
fprintf(stderr, "Usage: %s <crosssection.png> <shape.png> <output.stl>\n\n"
382+
const char* filenames[3] = {NULL, NULL, NULL};
383+
bool flipX = false;
384+
for (int i = 1, j = 0; i < argc; i++) {
385+
if (strcmp(argv[i], "--flip-x") == 0) {
386+
flipX = !flipX;
387+
}
388+
else {
389+
if (j < 3) filenames[j++] = argv[i];
390+
}
391+
}
392+
393+
if (filenames[2] == NULL) {
394+
fprintf(stderr, "Usage: %s [--flip-x] <crosssection.png> <shape.png> <output.stl>\n\n"
384395
"Sweep (extrude) a cross-section shape along the edge of a shape to make a solid.\n"
385396
"Input: black = inside, white or transparent = outside, antialiasing recommended.\n"
386397
"Left half of cross section goes inside, right half outside.\n"
387-
"Output is scaled according to resolution of cross-section image.\n\n"
388-
"Version 1.1\n"
398+
"Output is scaled according to resolution of cross-section image.\n"
399+
"--flip-x: Mirror output in x direction.\n\n"
400+
"Version 1.2\n"
389401
"Copyright (c) 2013-2015 Christian Walther <cwalther%cgmx.ch>\n"
390402
"https://github.com/cwalther/cookie-cutter-sweeper\n", argv[0], '@');
391403
return 2;
392404
}
405+
393406
try {
394407
// compute the 2D distance fields of section and shape
395-
FloatMatrix section = distanceField(readPng(argv[1]), 2);
396-
FloatMatrix shape = distanceField(readPng(argv[2]), (section.getWidth() + 1)/2);
408+
FloatMatrix section = distanceField(readPng(filenames[0]), 2);
409+
FloatMatrix shape = distanceField(readPng(filenames[1]), (section.getWidth() + 1)/2);
397410

398411
// initialization for working in z-slices, keeping only the last two slices in memory
399412
FloatMatrix slice1(shape.getWidth(), shape.getHeight());
@@ -460,7 +473,7 @@ int main(int argc, char **argv) {
460473
v.count++;
461474
face.index[j] = vi - 1;
462475
}
463-
if (q >= 0) {
476+
if ((q >= 0) != flipX) {
464477
// outwards-pointing edge, reverse face orientation
465478
unsigned int t = face.index[1];
466479
face.index[1] = face.index[3];
@@ -482,23 +495,25 @@ int main(int argc, char **argv) {
482495
// finish averaging
483496
it->normalize();
484497
// - invert in y and z directions to convert from upside-down image coordinate system
498+
// - invert in x direction if requested
485499
// - shift by (1, 1, 1) to ensure all coordinates are positive as required by STL spec
486500
// - scale by the resolution read from the section image, if any
501+
if (flipX) it->xsum = shape.getWidth() - it->xsum;
487502
it->xsum = (it->xsum + 1.0f)/section.getScale();
488503
it->ysum = (shape.getHeight() - it->ysum + 1.0f)/section.getScale();
489504
it->zsum = (section.getHeight() - it->zsum + 1.0f)/section.getScale();
490505
}
491506

492507
// now we have a mesh in a typical list-of-vertex-coordinates + list-of-faces-by-vertex-indices form, save it in STL format, which only stores individual triangles
493508
FILE* of;
494-
if (strcmp(argv[3], "-") == 0) {
509+
if (strcmp(filenames[2], "-") == 0) {
495510
of = stdout;
496511
}
497512
else {
498-
of = fopen(argv[3], "wb");
513+
of = fopen(filenames[2], "wb");
499514
}
500515
if (of == NULL) {
501-
fprintf(stderr, "cannot open file %s: %s\n", argv[3], strerror(errno));
516+
fprintf(stderr, "cannot open file %s: %s\n", filenames[2], strerror(errno));
502517
return 1;
503518
}
504519
for (int i = 0; i < 80; i++) fputc(0, of);

0 commit comments

Comments
 (0)