-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstl2dat.h
378 lines (332 loc) · 8.88 KB
/
stl2dat.h
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <string>
#include <iostream>
#include "ort.h"
#define LIST_OF(cl) \
class cl##_list \
{ \
public: \
cl * item; \
cl##_list * next; \
cl##_list(); \
cl##_list( cl * it, cl##_list * ne ) { item = it; next = ne; }; \
void list_append( cl * it, cl##_list * & list, cl##_list * & last ); \
}; \
cl##_list::cl##_list() \
{ \
next = 0; \
} \
void cl##_list::list_append( cl * it, cl##_list * & list, cl##_list * & last ) \
{ \
cl##_list * elem = new cl##_list; \
elem->item = it; \
elem->next = 0; \
if (list == 0) \
list = elem; \
if (last != 0) \
last->next = elem; \
}
/*
The standard ldraw primitives
*/
class stl_prim
{
public:
stl_lin transfo;
std::string name;
int color;
bool cw = false; // for bfc
stl_prim() {};
stl_prim(int col)
{
color = col;
};
stl_prim(const std::string & nm, int col, bool _cw = true)
{
name = nm;
color = col;
cw = _cw;
};
void write(std::ostream & os);
};
LIST_OF(stl_prim)
class stl_file;
class stl_facet;
class stl_facet_list;
class stl_curv;
class stl_curv_list;
enum surf_type
{
t_surf,
t_plane,
t_cylinder,
t_tangent
};
/*
The class for geometric primitives
*/
class stl_surf
{
public:
int count;
stl_prim * prim;
bool adj_cyl = false;
stl_surf();
virtual surf_type type() { return(t_surf); };
virtual bool compatible( stl_facet * fct, stl_facet * adj) = 0;
virtual void write( std::ostream & os);
virtual void set_facet( stl_facet * fct);
virtual void calc_prim( stl_facet * facet, stl_file & stl);
virtual void clean(stl_facet * fct);
virtual void calc_curv( stl_file & stl);
};
class stl_cylinder : public stl_surf
{
public:
stl_v od;
stl_v vd;
double radius;
double height;
stl_v normal;
bool cw = false; // for bfc
stl_cylinder( const stl_v & od, const stl_v & vd, double radius, double height);
virtual surf_type type() { return(t_cylinder); };
virtual void write( std::ostream & os);
virtual bool compatible( stl_facet * fct, stl_facet * adj);
virtual void calc_prim( stl_facet * facet, stl_file & stl);
virtual void clean(stl_facet * fct);
void calc_extrem_partial(
stl_facet * facet,
stl_facet * & fct1,
stl_facet * & fct2,
stl_facet * & fct3,
stl_facet * & fct4
);
void calc_extrem_full(
stl_facet * facet,
stl_facet * & fct1,
stl_facet * & fct2,
stl_facet * & fct3,
stl_facet * & fct4
);
void calc_center(
stl_facet * fcta,
stl_facet * fctb,
stl_v & max_o,
stl_v & min_o,
stl_v & max_p,
stl_v & min_p
);
virtual void calc_curv( stl_file & stl);
virtual const int nb_arete() { return 16; }
virtual const double angle_cyl() { return pi/8; }
};
class stl_cylinder48 : public stl_cylinder
{
public:
stl_cylinder48( const stl_v & od, const stl_v & vd, double radius, double height) :
stl_cylinder( od, vd, radius, height) {};
virtual const int nb_arete() { return 48; }
virtual const double angle_cyl() { return pi/24; }
};
// Plane
class stl_plane : public stl_surf
{
public:
stl_v op; // Origin of plane
stl_v vp; // Direction vector of plane
stl_plane( const stl_v & op, const stl_v & vp);
virtual surf_type type() { return(t_plane); };
virtual bool compatible( stl_facet * fct, stl_facet *);
virtual void write( std::ostream & os);
virtual void calc_curv( stl_file & stl);
};
// Tangent facet
class stl_tangent : public stl_surf
{
public:
stl_facet * facet; // Tangent facet
stl_tangent( stl_facet * facet);
virtual surf_type type() { return(t_tangent); };
virtual bool compatible( stl_facet * fct, stl_facet *);
virtual void set_facet( stl_facet * fct);
};
LIST_OF(stl_surf)
class stl_edge;
class stl_vertex;
// Curves : list of
class stl_curv
{
public:
int count;
stl_prim * prim;
bool adj_cyl = false;
stl_curv();
virtual bool compatible( stl_edge * edg, stl_edge * adj) = 0;
virtual void write( std::ostream & os);
virtual void set_facet( stl_edge * edg);
virtual void calc_prim( stl_edge * facet, stl_file & stl);
virtual void clean(stl_edge * edg);
};
LIST_OF(stl_curv)
/*
Store information for a facet with 3 or 4 points
*/
class stl_facet
{
public:
int usage; // 0 not used
// 3 edges
// 4 edges
int nbr_facet; // number of facet in the solid
int color; // color
stl_v normal; // normal calculated
stl_v read_normal; // normal read in stl file
stl_v coords[4]; // coordinates of vertices
stl_edge * edge[4]; // the edges defining the facet
stl_facet * adjacent[4]; // the facet adjacent on edge[n]
stl_vertex * vertex[4]; // the vertices
stl_surf * surf; // primitive surface
bool printed = false;
double m_surface;
int precision;
std::string ident();
void dump();
stl_facet();
void write(std::ostream & os, int col3 = 16, int col4 = 16, int edgecolor = 24, bool printface = true, bool printedge = true);
void facetplan( stl_v & oq, stl_v & wq);
void calculate_normal();
void clean();
bool is_convex(bool trace = false);
void calcul_angles( double & totag, double & minag, double & maxag);
double angle_between(stl_facet * fct);
bool is_border(const stl_v & pt);
bool is_adjacent(stl_facet * fct1);
void merge_facet(stl_edge * m_edge, stl_facet * m_facet, int ve1, int ve2);
void calc_surf(stl_facet * fct, stl_surf_list * & surf_list);
void replace_vertex(stl_vertex * ve_ori, stl_vertex * ve_repl);
void replace_adjacent(stl_vertex * ve_ori, stl_edge * ed, bool del_edge = true);
bool recalculate_precision();
stl_facet * next_surf( stl_facet * fct);
int side_edge( stl_facet * fct);
int side_edge( stl_edge * edge);
stl_plane * new_plane();
stl_cylinder * new_cylinder( stl_facet * adj);
stl_cylinder * new_cylinder( stl_facet * fct2, const stl_v & op, const stl_v & vp, const double angle_cyl);
stl_cylinder * new_cylinder( stl_facet * fct2, const stl_v & op, const stl_v & vp);
stl_cylinder * new_cylinder48( stl_facet * fct2, const stl_v & op, const stl_v & vp);
stl_tangent * new_tangent();
bool check();
double surface();
double surface(const stl_v & v_ori, const stl_v & v_repl);
double determinant(int precision = -1);
};
LIST_OF(stl_facet)
/*
Store information for an edge between 2 facets
*/
class stl_edge
{
public:
stl_facet * adjacent[2]; // The 2 facets adjacent
int linetype; // ldraw line type 2 or 5
int coords[2]; // number of vertex in facet adjacent[i]
stl_vertex * vertex[2]; // the vertices
bool printed = false;
bool deleted = false;
int color;
int nbr_edge;
stl_edge();
stl_v coord(int i, int face = 0);
stl_vertex * vertx(int i, int face = 0);
void set_coord(int i, int face, const stl_v & val);
void write(std::ostream & os, int col2 = 24, int col5 = 24);
std::string ident();
bool merge_adj();
int side(stl_facet * m_facet);
void reaffect(stl_facet * m_facet, stl_facet * repl_facet, int ve);
bool calc_edge();
bool check();
void dump();
bool colinear( const stl_v & vd);
stl_edge * next(int & vx, stl_facet * & s_fct);
};
LIST_OF(stl_edge)
/*
Store information for an vertex
*/
class stl_vertex
{
public:
stl_v coords;
stl_edge_list * edge_list;
bool deleted = false;
bool to_remove = false;
int precision;
stl_edge * edge_to_remove;
stl_vertex(const stl_v & _coords);
void add_edge( stl_edge * edge);
bool check();
void dump();
void scan_edges(int nb_face);
void remove_vertex(stl_edge * ed_supr);
friend std::ostream & operator << (
std::ostream & s,
const stl_vertex * v
);
/*-
! Use: Write a given point/vector in an ostream.
! remark: Format is < x1 , x2 , x3 >
*/
};
LIST_OF(stl_vertex)
class stl_file
{
std::ifstream is;
std::string desc;
std::string name;
public:
std::string partname;
std::string author;
std::string out_filename;
int ok;
stl_v origin;
stl_lin transf;
bool swapyz = false;
int nbr_facet;
private:
stl_facet_list * facet_list;
stl_facet_list * last_facet;
stl_edge_list * edge_list;
stl_edge_list * last_edge;
stl_vertex_list * vertex_list;
stl_prim_list * prim_list;
stl_prim_list * last_prim;
stl_surf_list * surf_list;
stl_curv_list * curv_list;
bool stl_bin = false;
public:
stl_file(const char * filename);
void dump();
bool read();
void write_dat(bool print_geom);
bool check();
void topo();
void optim3();
void optim4();
void optim_facets(int nb_face);
void primitives();
void calc_edge();
std::string get_desc()
{
return(desc);
};
void add_prim( stl_prim * prim);
private:
bool token(const char * token);
bool read_header();
int read_facet_text();
bool read_facet_bin();
bool new_facet(const stl_v & normal, const stl_v vertex[]);
int create_edge(stl_facet * fct1, stl_facet * fct2);
void add_edge(int usage, const stl_v& v0, const stl_v& v1, const stl_v& v2 = stl_v(0,0,0), const stl_v& v3 = stl_v(0,0,0));
};