/* * The Npic library and tools * * Copyright (C) 2003 Edouard Thiel * * This program is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* * npic-geomv.c - 06/10/2008 * */ #include void ShowUsage () { printf ( "npic-geomv - Convert a 3-d image file to a geomview file.\n" "Usage:\n" " npic-geomv -h | -help | --help : print help\n" " npic-geomv in1 out1 [options] : convert in1 to out1\n" "\n" "in1 : volume in " NPIC_KNOWN_IMAGE_EXT " format\n" "out1 : result in the geomview .geom format\n" "\n" "Options:\n" " -vobb : add the real volume bounding box\n" " -rad r : face radius, default is 0.5\n" " -show : show result with geomview\n" "\n" "Geomview is an interactive 3D viewing program using OpenGL,\n" "available on most Unix platforms. See: http://www.geomview.org\n" "To get on Ubuntu/Debian, type: sudo apt-get install geomview\n" "\n" "Example:\n" " ./npic-geomv ../images/klette_3c.pan.gz tmp1.geom\n" " geomview tmp1.geom &\n" "\n" "Example:\n" " ./npic-new tmp2.npz -3c 50:40:60\n" " ./npic-draw tmp2.npz tmp2.npz \\\n" " -rect 10:20:30 15:26:37 100 \\\n" " -ellip 30:20:30 35:26:37 100\n" " ./npic-geomv tmp2.npz tmp3.geom -vobb -show\n" "\n" ); } void ArgcExit (int argc, int n) { if (argc < n) { fprintf (stderr, "ERROR: %d argument(s) missing, " "type \"npic-geomv -h\" to get help.\n", n-argc); exit (1); } } void ArgShift (int *argc, char **argv[], int n) { *argc -= n; *argv += n; } void draw_geom_3l (FILE *f, Npic_image *np1, double r, int show_vobb) { Npic_image_3l *p1 = NpicCastImage (np1); int x, y, z, i, j, a; char *colors[] = { "0.098 0.098 0.800", "0.098 0.098 0.700", "0.098 0.800 0.098", "0.098 0.700 0.098", "0.800 0.098 0.098", "0.700 0.098 0.098" }; NpicSetBorderWidthMin (np1, 1); NpicFillBorder_i (np1, 0); fprintf (f, "{ LIST\n"); if (show_vobb) { fprintf (f, " { BBOX\n" " -0.5 -0.5 -0.5\n" " %.1lf %.1lf %.1lf\n" " }\n", p1->xmax-0.5, p1->ymax-0.5, p1->zmax -0.5); } /* -------- faces z -------- */ for (i = 0, j = 0; i < 2; i++) { a = 0; for (y = 0; y < p1->ymax; y++) for (x = 0; x < p1->xmax; x++) { for (z = -1; z < p1->zmax; z++) if ( (x+y+z+2) % 2 == i && ( (p1->pix[z][y][x] == 0 && p1->pix[z+1][y][x] != 0) || (p1->pix[z][y][x] != 0 && p1->pix[z+1][y][x] == 0) ) ) { if (a == 0) { a = 1; fprintf (f, " { INST\n geom {\n CQUAD \n" " -%lf %lf %lf %s 1\n -%lf -%lf %lf %s 1\n" " %lf -%lf %lf %s 1\n %lf %lf %lf %s 1\n" " }\n transforms {\n TLIST\n", r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i] ); } fprintf (f, "1 0 0 0 0 1 0 0 0 0 1 0 %d %d %d 1\n", x, y, z); } } if (a) fprintf (f, " }\n }\n"); } /* -------- faces y -------- */ for (i = 0, j = 2; i < 2; i++) { a = 0; for (z = 0; z < p1->zmax; z++) for (x = 0; x < p1->xmax; x++) { for (y = -1; y < p1->ymax; y++) if ( (x+y+z+2) % 2 == i && ( (p1->pix[z][y][x] == 0 && p1->pix[z][y+1][x] != 0) || (p1->pix[z][y][x] != 0 && p1->pix[z][y+1][x] == 0) ) ) { if (a == 0) { a = 1; fprintf (f, " { INST\n geom {\n CQUAD \n" " -%lf %lf %lf %s 1\n -%lf %lf -%lf %s 1\n" " %lf %lf -%lf %s 1\n %lf %lf %lf %s 1\n" " }\n transforms {\n TLIST\n", r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i] ); } fprintf (f, "1 0 0 0 0 1 0 0 0 0 1 0 %d %d %d 1\n", x, y, z); } } if (a) fprintf (f, " }\n }\n"); } /* -------- faces x -------- */ for (i = 0, j = 4; i < 2; i++) { a = 0; for (z = 0; z < p1->zmax; z++) for (y = 0; y < p1->ymax; y++) { for (x = -1; x < p1->xmax; x++) if ( (x+y+z+2) % 2 == i && ( (p1->pix[z][y][x] == 0 && p1->pix[z][y][x+1] != 0) || (p1->pix[z][y][x] != 0 && p1->pix[z][y][x+1] == 0) ) ) { if (a == 0) { a = 1; fprintf (f, " { INST\n geom {\n CQUAD \n" " %lf -%lf %lf %s 1\n %lf -%lf -%lf %s 1\n" " %lf %lf -%lf %s 1\n %lf %lf %lf %s 1\n" " }\n transforms {\n TLIST\n", r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i], r, r, r, colors[j+i] ); } fprintf (f, "1 0 0 0 0 1 0 0 0 0 1 0 %d %d %d 1\n", x, y, z); } } if (a) fprintf (f, " }\n }\n"); } /* -------- end -------- */ fprintf (f, "}\n"); } int main (int argc, char *argv[]) { char *in1, *out1; Npic_image *np1; FILE *f; double r = 0.5; int show_result = 0, show_vobb = 0; ArgcExit (argc, 2); if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "-help") == 0 || strcmp (argv[1], "--help") == 0) { ShowUsage (); exit (0); } ArgcExit (argc, 3); in1 = argv[1]; out1 = argv[2]; ArgShift (&argc, &argv, 2); while (argc > 1) { if (strcmp (argv[1], "-rad") == 0) { ArgcExit (argc, 3); r = atof (argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-vobb") == 0) { show_vobb = 1; ArgShift (&argc, &argv, 1); } else if (strcmp (argv[1], "-show") == 0) { show_result = 1; ArgShift (&argc, &argv, 1); } else { fprintf (stderr, "ERROR: unknown argument \"%s\", " "type \"npic-geomv -h\" to get help.\n", argv[1]); exit (1); } } printf ("Loading \"%s\"\n", in1); np1 = NpicReadImage (in1); if (np1 == NULL) exit (1); switch (np1->type) { case NPIC_IMAGE_3C : NpicConvertImage_l (np1); break; case NPIC_IMAGE_3L : break; default : fprintf (stderr, "ERROR, not a 3C or 3L image\n"); exit (1); } f = fopen (out1, "w"); if (f == NULL) { fprintf (stderr, "ERROR: can't write file \"%s\"\n", out1); exit (1); } printf ("Saving \"%s\" ...\n", out1); draw_geom_3l (f, np1, r, show_vobb); fclose (f); printf ("Done\n"); if (show_result) { printf ("Showing result with geomview ...\n"); execlp ("geomview", "geomview", out1, NULL); perror ("exec geomview"); exit (1); } exit (0); }