61 {
62 char *input = NULL, *output = NULL;
64 SCANNED_ARG *scanned;
65 long i_arg;
66 unsigned long pipe_flags = 0;
67
68 int i;
69 int64_t rows = 0;
70 int32_t row, bsrows;
71 float *normal_vector[3];
72 float *vertex1[3];
73 float *vertex2[3];
74 float *vertex3[3];
75 short attribute = 0;
76 FILE *fd;
77 int32_t big_endian_machine = 0;
78 float temp;
79
81 argc =
scanargs(&scanned, argc, argv);
82 if (argc < 2) {
83 fprintf(stderr, "%s", usage);
84 return 1;
85 }
86
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)) {
90 case SET_PIPE:
91 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipe_flags)) {
92 fprintf(stderr, "sdds2stl: invalid -pipe syntax\n");
93 return 1;
94 }
95 break;
96 default:
97 fprintf(stderr, "sdds2stl: invalid option seen\n%s", usage);
98 return 1;
99 }
100 } else {
101 if (!input)
103 else if (!output)
105 else {
106 fprintf(stderr, "sdds2stl: too many filenames\n%s", usage);
107 return 1;
108 }
109 }
110 }
111
112 if (pipe_flags & USE_STDIN)
114
117
119 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
120 return 1;
121 }
122
124 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
125 return 1;
126 }
127
129 fprintf(stderr, "sdds2stl: NormalVectorX column not found.\n");
130 return 1;
131 }
132
134 fprintf(stderr, "sdds2stl: Vertex1X column not found.\n");
135 return 1;
136 }
137
139 fprintf(stderr, "sdds2stl: Vertex1Y column not found.\n");
140 return 1;
141 }
142
144 fprintf(stderr, "sdds2stl: Vertex1Z column not found.\n");
145 return 1;
146 }
147
149 fprintf(stderr, "sdds2stl: Vertex2X column not found.\n");
150 return 1;
151 }
152
154 fprintf(stderr, "sdds2stl: Vertex2Y column not found.\n");
155 return 1;
156 }
157
159 fprintf(stderr, "sdds2stl: Vertex2Z column not found.\n");
160 return 1;
161 }
162
164 fprintf(stderr, "sdds2stl: Vertex3X column not found.\n");
165 return 1;
166 }
167
169 fprintf(stderr, "sdds2stl: Vertex3Y column not found.\n");
170 return 1;
171 }
172
174 fprintf(stderr, "sdds2stl: Vertex3Z column not found.\n");
175 return 1;
176 }
177
178 if (SDDS_ReadTable(&SDDS_dataset) <= 0) {
179 fprintf(stderr, "sdds2stl: Unable to read SDDS page.\n");
180 return 1;
181 }
182
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");
186 return 1;
187 }
188
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);
194 }
195
208
211
212 if (!output) {
213#if defined(_WIN32)
214 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
215 fprintf(stderr, "error: unable to set stdout to binary mode\n");
216 exit(1);
217 }
218#endif
219 fd = stdout;
220 } else
221 fd = fopen(output, "wb");
222
224
225 fprintf(fd, "STL BINARY FILE CREATED BY SDDS2STL --------------------------------------------");
226 if (big_endian_machine) {
227 bsrows = rows;
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);
234 }
235 for (i = 0; i < 3; i++) {
236 temp = float_reverse_bytes(vertex1[i][row]);
237 fwrite(&temp, sizeof(float), 1, fd);
238 }
239 for (i = 0; i < 3; i++) {
240 temp = float_reverse_bytes(vertex2[i][row]);
241 fwrite(&temp, sizeof(float), 1, fd);
242 }
243 for (i = 0; i < 3; i++) {
244 temp = float_reverse_bytes(vertex3[i][row]);
245 fwrite(&temp, sizeof(float), 1, fd);
246 }
247 fwrite(&attribute, sizeof(short), 1, fd);
248 }
249 } else {
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);
261 }
262 }
263
264 fclose(fd);
265
266 return 1;
267}
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)