48float float_reverse_bytes(
float x);
56char *option[N_OPTIONS] =
61 "sdds2stl [-pipe[=input]] [<inputFile>] [<outputFile>]\n"
62 "Converts an SDDS file to a binary STL file.\n"
63 "Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
65int main(
int argc,
char **argv) {
66 char *input = NULL, *output = NULL;
70 unsigned long pipe_flags = 0;
75 float *normal_vector[3];
81 int32_t big_endian_machine = 0;
85 argc =
scanargs(&scanned, argc, argv);
87 fprintf(stderr,
"%s", usage);
91 for (i_arg = 1; i_arg < argc; i_arg++) {
92 if (scanned[i_arg].arg_type == OPTION) {
93 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
95 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipe_flags)) {
96 fprintf(stderr,
"sdds2stl: invalid -pipe syntax\n");
101 fprintf(stderr,
"sdds2stl: invalid option seen\n%s", usage);
110 fprintf(stderr,
"sdds2stl: too many filenames\n%s", usage);
116 if (pipe_flags & USE_STDIN)
123 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
128 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
133 fprintf(stderr,
"sdds2stl: NormalVectorX column not found.\n");
138 fprintf(stderr,
"sdds2stl: Vertex1X column not found.\n");
143 fprintf(stderr,
"sdds2stl: Vertex1Y column not found.\n");
148 fprintf(stderr,
"sdds2stl: Vertex1Z column not found.\n");
153 fprintf(stderr,
"sdds2stl: Vertex2X column not found.\n");
158 fprintf(stderr,
"sdds2stl: Vertex2Y column not found.\n");
163 fprintf(stderr,
"sdds2stl: Vertex2Z column not found.\n");
168 fprintf(stderr,
"sdds2stl: Vertex3X column not found.\n");
173 fprintf(stderr,
"sdds2stl: Vertex3Y column not found.\n");
178 fprintf(stderr,
"sdds2stl: Vertex3Z column not found.\n");
182 if (SDDS_ReadTable(&SDDS_dataset) <= 0) {
183 fprintf(stderr,
"sdds2stl: Unable to read SDDS page.\n");
187 rows = SDDS_RowCount(&SDDS_dataset);
188 if (rows > INT32_MAX) {
189 fprintf(stderr,
"sdds2stl: Too many rows input file for conversion to STL format\n");
193 for (i = 0; i < 3; i++) {
194 normal_vector[i] = malloc(
sizeof(*(normal_vector[i])) * rows);
195 vertex1[i] = malloc(
sizeof(*(vertex1[i])) * rows);
196 vertex2[i] = malloc(
sizeof(*(vertex2[i])) * rows);
197 vertex3[i] = malloc(
sizeof(*(vertex3[i])) * rows);
218 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
219 fprintf(stderr,
"error: unable to set stdout to binary mode\n");
225 fd = fopen(output,
"wb");
229 fprintf(fd,
"STL BINARY FILE CREATED BY SDDS2STL --------------------------------------------");
230 if (big_endian_machine) {
233 fwrite(&bsrows,
sizeof(int32_t), 1, fd);
234 for (row = 0; row < rows; row++) {
235 for (i = 0; i < 3; i++) {
236 temp = float_reverse_bytes(normal_vector[i][row]);
237 fwrite(&temp,
sizeof(
float), 1, fd);
239 for (i = 0; i < 3; i++) {
240 temp = float_reverse_bytes(vertex1[i][row]);
241 fwrite(&temp,
sizeof(
float), 1, fd);
243 for (i = 0; i < 3; i++) {
244 temp = float_reverse_bytes(vertex2[i][row]);
245 fwrite(&temp,
sizeof(
float), 1, fd);
247 for (i = 0; i < 3; i++) {
248 temp = float_reverse_bytes(vertex3[i][row]);
249 fwrite(&temp,
sizeof(
float), 1, fd);
251 fwrite(&attribute,
sizeof(
short), 1, fd);
254 fwrite(&rows,
sizeof(int32_t), 1, fd);
255 for (row = 0; row < rows; row++) {
256 for (i = 0; i < 3; i++)
257 fwrite(&(normal_vector[i][row]),
sizeof(
float), 1, fd);
258 for (i = 0; i < 3; i++)
259 fwrite(&(vertex1[i][row]),
sizeof(
float), 1, fd);
260 for (i = 0; i < 3; i++)
261 fwrite(&(vertex2[i][row]),
sizeof(
float), 1, fd);
262 for (i = 0; i < 3; i++)
263 fwrite(&(vertex3[i][row]),
sizeof(
float), 1, fd);
264 fwrite(&attribute,
sizeof(
short), 1, fd);
273float float_reverse_bytes(
float x) {
283 y.ychar[0] = y.ychar[3];
287 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)