2015-02-14 23:32:58 +01:00
|
|
|
/** \file
|
|
|
|
* Generate an OpenSCAD with connectors for each face.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "v3.h"
|
2015-02-14 23:59:08 +01:00
|
|
|
#include "stl_3d.h"
|
2015-02-14 23:32:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2015-02-15 01:11:52 +01:00
|
|
|
main(void)
|
2015-02-14 23:32:58 +01:00
|
|
|
{
|
2015-02-14 23:59:08 +01:00
|
|
|
stl_3d_t * const stl = stl_3d_parse(STDIN_FILENO);
|
|
|
|
if (!stl)
|
2015-02-14 23:32:58 +01:00
|
|
|
return EXIT_FAILURE;
|
2015-02-15 02:27:35 +01:00
|
|
|
const double thickness = 6;
|
2015-02-14 23:32:58 +01:00
|
|
|
|
2015-02-14 23:59:08 +01:00
|
|
|
// for each vertex, find the coplanar triangles
|
|
|
|
// \todo: do coplanar bits
|
|
|
|
for(int i = 0 ; i < stl->num_vertex ; i++)
|
|
|
|
{
|
|
|
|
const stl_vertex_t * const v = &stl->vertex[i];
|
2015-02-15 01:53:31 +01:00
|
|
|
const v3_t origin = v->p;
|
2015-02-14 23:32:58 +01:00
|
|
|
|
2015-02-15 01:53:31 +01:00
|
|
|
printf("// vertex %d\n"
|
2015-02-15 02:27:35 +01:00
|
|
|
"//translate([%f,%f,%f])\n"
|
|
|
|
"//render() difference()\n"
|
|
|
|
"{\n"
|
|
|
|
"//sphere(r=20);\n",
|
2015-02-15 01:53:31 +01:00
|
|
|
i, origin.p[0], origin.p[1], origin.p[2]);
|
|
|
|
|
2015-02-14 23:32:58 +01:00
|
|
|
|
2015-02-14 23:59:08 +01:00
|
|
|
for (int j = 0 ; j < v->num_face; j++)
|
|
|
|
{
|
2015-02-15 01:01:12 +01:00
|
|
|
const stl_face_t * const f = v->face[j];
|
2015-02-15 02:03:39 +01:00
|
|
|
v3_t v0 = v3_sub(f->vertex[0]->p, origin);
|
|
|
|
v3_t v1 = v3_sub(f->vertex[1]->p, origin);
|
|
|
|
v3_t v2 = v3_sub(f->vertex[2]->p, origin);
|
|
|
|
v3_t n;
|
|
|
|
|
|
|
|
if (v->face_num[j] == 0)
|
|
|
|
n = v3_cross(v1, v2);
|
|
|
|
else
|
|
|
|
if (v->face_num[j] == 1)
|
|
|
|
n = v3_cross(v2, v0);
|
|
|
|
else
|
|
|
|
if (v->face_num[j] == 2)
|
|
|
|
n = v3_cross(v0, v1);
|
|
|
|
|
2015-02-15 02:27:35 +01:00
|
|
|
n = v3_scale(n, (thickness+1)/v3_mag(n)/2);
|
|
|
|
v3_t n2 = v3_scale(n, 1/v3_mag(n));
|
2015-02-15 02:03:39 +01:00
|
|
|
|
2015-02-15 02:27:35 +01:00
|
|
|
v3_t v3 = v3_add(v0, n);
|
|
|
|
v3_t v4 = v3_add(v1, n);
|
|
|
|
v3_t v5 = v3_add(v2, n);
|
|
|
|
v0 = v3_sub(v0, n);
|
|
|
|
v1 = v3_sub(v1, n);
|
|
|
|
v2 = v3_sub(v2, n);
|
2015-02-15 01:53:31 +01:00
|
|
|
|
2015-02-15 02:10:17 +01:00
|
|
|
printf("polyhedron(\n"
|
2015-02-15 01:53:31 +01:00
|
|
|
"points=[\n"
|
|
|
|
"[%f,%f,%f],[%f,%f,%f],[%f,%f,%f],\n"
|
2015-02-15 02:03:39 +01:00
|
|
|
"[%f,%f,%f],[%f,%f,%f],[%f,%f,%f],\n"
|
2015-02-15 02:10:17 +01:00
|
|
|
"], faces = ["
|
2015-02-15 02:27:35 +01:00
|
|
|
" [0,1,2], [3,5,4],"
|
|
|
|
" [0,2,3], [2,5,3],"
|
|
|
|
" [0,3,4], [0,4,1],"
|
|
|
|
" [1,4,5], [1,5,2],"
|
2015-02-15 02:10:17 +01:00
|
|
|
"]);\n",
|
2015-02-15 02:03:39 +01:00
|
|
|
v0.p[0], v0.p[1], v0.p[2],
|
2015-02-15 01:53:31 +01:00
|
|
|
v1.p[0], v1.p[1], v1.p[2],
|
|
|
|
v2.p[0], v2.p[1], v2.p[2],
|
2015-02-15 02:03:39 +01:00
|
|
|
v3.p[0], v3.p[1], v3.p[2],
|
|
|
|
v4.p[0], v4.p[1], v4.p[2],
|
|
|
|
v5.p[0], v5.p[1], v5.p[2]
|
2015-02-15 01:53:31 +01:00
|
|
|
);
|
|
|
|
|
2015-02-15 02:27:35 +01:00
|
|
|
//break; // only do one right now
|
2015-02-14 23:32:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("}\n");
|
2015-02-15 02:27:35 +01:00
|
|
|
if (i == 0) break; // only do one right now
|
2015-02-14 23:32:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|