65 {
66 char *input = NULL, *output = NULL;
68 SCANNED_ARG *scanned;
69 long i_arg;
70 unsigned long pipe_flags = 0;
71
72 int i;
73 int64_t rows = 0;
74 int32_t row, bsrows;
75 float *normal_vector[3];
76 float *vertex1[3];
77 float *vertex2[3];
78 float *vertex3[3];
79 short attribute = 0;
80 FILE *fd;
81 int32_t big_endian_machine = 0;
82 float temp;
83
85 argc =
scanargs(&scanned, argc, argv);
86 if (argc < 2) {
87 fprintf(stderr, "%s", usage);
88 return 1;
89 }
90
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)) {
94 case SET_PIPE:
95 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipe_flags)) {
96 fprintf(stderr, "sdds2stl: invalid -pipe syntax\n");
97 return 1;
98 }
99 break;
100 default:
101 fprintf(stderr, "sdds2stl: invalid option seen\n%s", usage);
102 return 1;
103 }
104 } else {
105 if (!input)
107 else if (!output)
109 else {
110 fprintf(stderr, "sdds2stl: too many filenames\n%s", usage);
111 return 1;
112 }
113 }
114 }
115
116 if (pipe_flags & USE_STDIN)
118
121
123 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
124 return 1;
125 }
126
128 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
129 return 1;
130 }
131
133 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
134 return 1;
135 }
136
138 fprintf(stderr, "sdds2stl: Vertex1X column not found.\n");
139 return 1;
140 }
141
143 fprintf(stderr, "sdds2stl: Vertex1Y column not found.\n");
144 return 1;
145 }
146
148 fprintf(stderr, "sdds2stl: Vertex1Z column not found.\n");
149 return 1;
150 }
151
153 fprintf(stderr, "sdds2stl: Vertex2X column not found.\n");
154 return 1;
155 }
156
158 fprintf(stderr, "sdds2stl: Vertex2Y column not found.\n");
159 return 1;
160 }
161
163 fprintf(stderr, "sdds2stl: Vertex2Z column not found.\n");
164 return 1;
165 }
166
168 fprintf(stderr, "sdds2stl: Vertex3X column not found.\n");
169 return 1;
170 }
171
173 fprintf(stderr, "sdds2stl: Vertex3Y column not found.\n");
174 return 1;
175 }
176
178 fprintf(stderr, "sdds2stl: Vertex3Z column not found.\n");
179 return 1;
180 }
181
182 if (SDDS_ReadTable(&SDDS_dataset) <= 0) {
183 fprintf(stderr, "sdds2stl: Unable to read SDDS page.\n");
184 return 1;
185 }
186
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");
190 return 1;
191 }
192
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);
198 }
199
212
215
216 if (!output) {
217#if defined(_WIN32)
218 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
219 fprintf(stderr, "error: unable to set stdout to binary mode\n");
220 exit(1);
221 }
222#endif
223 fd = stdout;
224 } else
225 fd = fopen(output, "wb");
226
228
229 fprintf(fd, "STL BINARY FILE CREATED BY SDDS2STL --------------------------------------------");
230 if (big_endian_machine) {
231 bsrows = rows;
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);
238 }
239 for (i = 0; i < 3; i++) {
240 temp = float_reverse_bytes(vertex1[i][row]);
241 fwrite(&temp, sizeof(float), 1, fd);
242 }
243 for (i = 0; i < 3; i++) {
244 temp = float_reverse_bytes(vertex2[i][row]);
245 fwrite(&temp, sizeof(float), 1, fd);
246 }
247 for (i = 0; i < 3; i++) {
248 temp = float_reverse_bytes(vertex3[i][row]);
249 fwrite(&temp, sizeof(float), 1, fd);
250 }
251 fwrite(&attribute, sizeof(short), 1, fd);
252 }
253 } else {
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);
265 }
266 }
267
268 fclose(fd);
269
270 return 1;
271}
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)