44float float_reverse_bytes(
float x);
52char *option[N_OPTIONS] =
57 "sdds2stl [-pipe[=input]] [<inputFile>] [<outputFile>]\n"
58 "Converts an SDDS file to a binary STL file.\n"
59 "Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
61int main(
int argc,
char **argv) {
62 char *input = NULL, *output = NULL;
66 unsigned long pipe_flags = 0;
71 float *normal_vector[3];
77 int32_t big_endian_machine = 0;
81 argc =
scanargs(&scanned, argc, argv);
83 fprintf(stderr,
"%s", usage);
87 for (i_arg = 1; i_arg < argc; i_arg++) {
88 if (scanned[i_arg].arg_type == OPTION) {
89 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
91 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipe_flags)) {
92 fprintf(stderr,
"sdds2stl: invalid -pipe syntax\n");
97 fprintf(stderr,
"sdds2stl: invalid option seen\n%s", usage);
106 fprintf(stderr,
"sdds2stl: too many filenames\n%s", usage);
112 if (pipe_flags & USE_STDIN)
119 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
124 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
129 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
134 fprintf(stderr,
"sdds2stl: Vertex1X column not found.\n");
139 fprintf(stderr,
"sdds2stl: Vertex1Y column not found.\n");
144 fprintf(stderr,
"sdds2stl: Vertex1Z column not found.\n");
149 fprintf(stderr,
"sdds2stl: Vertex2X column not found.\n");
154 fprintf(stderr,
"sdds2stl: Vertex2Y column not found.\n");
159 fprintf(stderr,
"sdds2stl: Vertex2Z column not found.\n");
164 fprintf(stderr,
"sdds2stl: Vertex3X column not found.\n");
169 fprintf(stderr,
"sdds2stl: Vertex3Y column not found.\n");
174 fprintf(stderr,
"sdds2stl: Vertex3Z column not found.\n");
178 if (SDDS_ReadTable(&SDDS_dataset) <= 0) {
179 fprintf(stderr,
"sdds2stl: Unable to read SDDS page.\n");
183 rows = SDDS_RowCount(&SDDS_dataset);
184 if (rows > INT32_MAX) {
185 fprintf(stderr,
"sdds2stl: Too many rows input file for conversion to STL format\n");
189 for (i = 0; i < 3; i++) {
190 normal_vector[i] = malloc(
sizeof(*(normal_vector[i])) * rows);
191 vertex1[i] = malloc(
sizeof(*(vertex1[i])) * rows);
192 vertex2[i] = malloc(
sizeof(*(vertex2[i])) * rows);
193 vertex3[i] = malloc(
sizeof(*(vertex3[i])) * rows);
214 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
215 fprintf(stderr,
"error: unable to set stdout to binary mode\n");
221 fd = fopen(output,
"wb");
225 fprintf(fd,
"STL BINARY FILE CREATED BY SDDS2STL --------------------------------------------");
226 if (big_endian_machine) {
229 fwrite(&bsrows,
sizeof(int32_t), 1, fd);
230 for (row = 0; row < rows; row++) {
231 for (i = 0; i < 3; i++) {
232 temp = float_reverse_bytes(normal_vector[i][row]);
233 fwrite(&temp,
sizeof(
float), 1, fd);
235 for (i = 0; i < 3; i++) {
236 temp = float_reverse_bytes(vertex1[i][row]);
237 fwrite(&temp,
sizeof(
float), 1, fd);
239 for (i = 0; i < 3; i++) {
240 temp = float_reverse_bytes(vertex2[i][row]);
241 fwrite(&temp,
sizeof(
float), 1, fd);
243 for (i = 0; i < 3; i++) {
244 temp = float_reverse_bytes(vertex3[i][row]);
245 fwrite(&temp,
sizeof(
float), 1, fd);
247 fwrite(&attribute,
sizeof(
short), 1, fd);
250 fwrite(&rows,
sizeof(int32_t), 1, fd);
251 for (row = 0; row < rows; row++) {
252 for (i = 0; i < 3; i++)
253 fwrite(&(normal_vector[i][row]),
sizeof(
float), 1, fd);
254 for (i = 0; i < 3; i++)
255 fwrite(&(vertex1[i][row]),
sizeof(
float), 1, fd);
256 for (i = 0; i < 3; i++)
257 fwrite(&(vertex2[i][row]),
sizeof(
float), 1, fd);
258 for (i = 0; i < 3; i++)
259 fwrite(&(vertex3[i][row]),
sizeof(
float), 1, fd);
260 fwrite(&attribute,
sizeof(
short), 1, fd);
269float float_reverse_bytes(
float x) {
279 y.ychar[0] = y.ychar[3];
283 y.ychar[1] = y.ychar[2];
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void SDDS_SwapLong(int32_t *data)
Swaps the endianness of a 32-bit integer.
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
int32_t SDDS_IsBigEndianMachine()
Determines whether the current machine uses big-endian byte ordering.
#define SDDS_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)