Files
Image-Heightmapper/STLTextWriter.h
2025-10-29 21:24:15 +00:00

76 lines
2.7 KiB
C

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "dotAndCrossProduct.h"
#define X 0
#define Y 1
#define Z 2
//draws one triangle
int drawTriangle(FILE *outfile, double coords1[], double coords2[], double coords3[]){
double vectn[] = {0,0,0}; // normal vector
double vectnlenin = 0; // normal vector length inverse
double vect1[] = {0,0,0}; // vector between coordinates 1 & 2
double vect2[] = {0,0,0}; // vector between coordinates 1 & 3
vect1[0] = coords1[0] - coords2[0];
vect1[1] = coords1[1] - coords2[1];
vect1[2] = coords1[2] - coords2[2];
vect2[0] = coords1[0] - coords3[0];
vect2[1] = coords1[1] - coords3[1];
vect2[2] = coords1[2] - coords3[2];
crossProduct(vectn, vect1, vect2);
/* if((!isnormal(vectn[0]) && vectn[0] != 0) || (!isnormal(vectn[1]) && vectn[1] != 0) || (!isnormal(vectn[2]) && vectn[2] != 0)){
printf("Troublemaker:\n\n");
printf("vectn = %f, %f, %f\n\n", vectn[0], vectn[1], vectn[2]);
printf("vect1 = %f, %f, %f\n", vect1[0], vect1[1], vect1[2]);
printf("vect2 = %f, %f, %f\n\n", vect2[0], vect2[1], vect2[2]);
printf("point1 = %f, %f, %f\n", coords1[0], coords1[1], coords1[2]);
printf("point2 = %f, %f, %f\n", coords2[0], coords2[1], coords2[2]);
printf("point3 = %f, %f, %f\n\n\n", coords3[0], coords3[1], coords3[2]);
}
*/
vectnlenin = 1 / sqrt((vectn[0]*vectn[0]) + (vectn[1]*vectn[1]) + (vectn[2]*vectn[2]));
vectn[0] = vectn[0] * vectnlenin;
vectn[1] = vectn[1] * vectnlenin;
vectn[2] = vectn[2] * vectnlenin;
fprintf(outfile, " facet normal %g %g %g\n", vectn[X], vectn[Y], vectn[Z]);
fprintf(outfile, " outer loop\n");
fprintf(outfile, " vertex %g %g %g\n", coords1[X], coords1[Y], coords1[Z]);
fprintf(outfile, " vertex %g %g %g\n", coords2[X], coords2[Y], coords2[Z]);
fprintf(outfile, " vertex %g %g %g\n", coords3[X], coords3[Y], coords3[Z]);
fprintf(outfile, " endloop\n");
fprintf(outfile, " endfacet\n");
return 0;
}
//draws one triangle and allows you to precompute the normal vector
int drawTriangleWithNormal(FILE *outfile, double coords1[], double coords2[], double coords3[], double vectn[]){
fprintf(outfile, " facet normal %g %g %g\n", vectn[X], vectn[Y], vectn[Z]);
fprintf(outfile, " outer loop\n");
fprintf(outfile, " vertex %g %g %g\n", coords1[X], coords1[Y], coords1[Z]);
fprintf(outfile, " vertex %g %g %g\n", coords2[X], coords2[Y], coords2[Z]);
fprintf(outfile, " vertex %g %g %g\n", coords3[X], coords3[Y], coords3[Z]);
fprintf(outfile, " endloop\n");
fprintf(outfile, " endfacet\n");
return 0;
}
//must be printed at the start of the file
int startSTL(FILE *outfile){
fprintf(outfile, "solid Mesh\n");
return 0;
}
//must be printed at the end of the file
int endSTL(FILE *outfile){
fprintf(outfile, "endsolid Mesh");
}