-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstl.c
121 lines (120 loc) · 5.2 KB
/
stl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/****************************************************************************
* ArtraCFD *
* <By Huangrui Mo> *
* Copyright (C) Huangrui Mo <huangrui.mo@gmail.com> *
* This file is part of ArtraCFD. *
* ArtraCFD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
****************************************************************************/
/****************************************************************************
* Required Header Files
****************************************************************************/
#include "stl.h"
#include <stdio.h> /* standard library for input and output */
#include <string.h> /* manipulating strings */
#include <stdint.h> /* fixed width integer types */
#include "commons.h"
/****************************************************************************
* Data Structure Declarations
****************************************************************************/
typedef enum {
STLSTR = 80, /* STL header characters */
} StlConst;
/*
* STL data format and type control
*/
typedef uint8_t StlChar; /* STL char */
typedef StlChar StlStr[STLSTR]; /* STL string */
typedef uint16_t StlInt; /* STL unsigned integer */
typedef uint32_t StlLint; /* STL unsigned long integer */
typedef float StlReal; /* STL real data */
/****************************************************************************
* Static Function Declarations
****************************************************************************/
/****************************************************************************
* Function definitions
****************************************************************************/
void ReadStlFile(const char *fname, Polyhedron *poly)
{
StlStr header = {'\0'};
StlLint facetN = 0;
StlInt attribute = 0;
StlReal facetData = 0.0;
FILE *fp = Fopen(fname, "rb");
Fread(header, sizeof(StlStr), 1, fp);
Fread(&facetN, sizeof(StlLint), 1, fp);
poly->faceN = facetN;
poly->facet = AssignStorage(poly->faceN * sizeof(*poly->facet));
for (StlLint n = 0; n < facetN; ++n) {
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].N[X] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].N[Y] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].N[Z] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v0[X] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v0[Y] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v0[Z] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v1[X] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v1[Y] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v1[Z] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v2[X] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v2[Y] = facetData;
Fread(&facetData, sizeof(StlReal), 1, fp);
poly->facet[n].v2[Z] = facetData;
Fread(&attribute, sizeof(StlInt), 1, fp);
}
fclose(fp);
return;
}
void WriteStlFile(const char *fname, const Polyhedron *poly)
{
StlStr header = "binary stl";
StlLint facetN = 0;
StlInt attribute = 0;
StlReal facetData = 0.0;
FILE *fp = Fopen(fname, "wb");
fwrite(header, sizeof(StlStr), 1, fp);
facetN = poly->faceN;
fwrite(&facetN, sizeof(StlLint), 1, fp);
for (StlLint n = 0; n < facetN; ++n) {
facetData = poly->facet[n].N[X];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].N[Y];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].N[Z];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v0[X];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v0[Y];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v0[Z];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v1[X];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v1[Y];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v1[Z];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v2[X];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v2[Y];
fwrite(&facetData, sizeof(StlReal), 1, fp);
facetData = poly->facet[n].v2[Z];
fwrite(&facetData, sizeof(StlReal), 1, fp);
fwrite(&attribute, sizeof(StlInt), 1, fp);
}
fclose(fp);
return;
}
/* a good practice: end file with a newline */