1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dxfrenderer.cpp
3 // Purpose: DXF reader and renderer
4 // Author: Sandro Sigala
8 // Copyright: (c) Sandro Sigala
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/wfstream.h"
24 #include "wx/txtstrm.h"
27 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
31 #include <OpenGL/glu.h>
36 #include "dxfrenderer.h"
38 #include "wx/listimpl.cpp"
39 WX_DEFINE_LIST(DXFEntityList
)
40 WX_DEFINE_LIST(DXFLayerList
)
42 // Conversion table from AutoCAD ACI colours to RGB values
43 static const struct { unsigned char r
, g
, b
; } aci_to_rgb
[256] = {
44 /* 0 */ {255, 255, 255},
46 /* 2 */ {255, 255, 0},
48 /* 4 */ { 0, 255, 255},
50 /* 6 */ {255, 0, 255},
51 /* 7 */ {255, 255, 255},
52 /* 8 */ {128, 128, 128},
53 /* 9 */ {192, 192, 192},
55 /* 11 */ {255, 127, 127},
57 /* 13 */ {204, 102, 102},
59 /* 15 */ {153, 76, 76},
61 /* 17 */ {127, 63, 63},
63 /* 19 */ { 76, 38, 38},
64 /* 20 */ {255, 63, 0},
65 /* 21 */ {255, 159, 127},
66 /* 22 */ {204, 51, 0},
67 /* 23 */ {204, 127, 102},
68 /* 24 */ {153, 38, 0},
69 /* 25 */ {153, 95, 76},
70 /* 26 */ {127, 31, 0},
71 /* 27 */ {127, 79, 63},
72 /* 28 */ { 76, 19, 0},
73 /* 29 */ { 76, 47, 38},
74 /* 30 */ {255, 127, 0},
75 /* 31 */ {255, 191, 127},
76 /* 32 */ {204, 102, 0},
77 /* 33 */ {204, 153, 102},
78 /* 34 */ {153, 76, 0},
79 /* 35 */ {153, 114, 76},
80 /* 36 */ {127, 63, 0},
81 /* 37 */ {127, 95, 63},
82 /* 38 */ { 76, 38, 0},
83 /* 39 */ { 76, 57, 38},
84 /* 40 */ {255, 191, 0},
85 /* 41 */ {255, 223, 127},
86 /* 42 */ {204, 153, 0},
87 /* 43 */ {204, 178, 102},
88 /* 44 */ {153, 114, 0},
89 /* 45 */ {153, 133, 76},
90 /* 46 */ {127, 95, 0},
91 /* 47 */ {127, 111, 63},
92 /* 48 */ { 76, 57, 0},
93 /* 49 */ { 76, 66, 38},
94 /* 50 */ {255, 255, 0},
95 /* 51 */ {255, 255, 127},
96 /* 52 */ {204, 204, 0},
97 /* 53 */ {204, 204, 102},
98 /* 54 */ {153, 153, 0},
99 /* 55 */ {153, 153, 76},
100 /* 56 */ {127, 127, 0},
101 /* 57 */ {127, 127, 63},
102 /* 58 */ { 76, 76, 0},
103 /* 59 */ { 76, 76, 38},
104 /* 60 */ {191, 255, 0},
105 /* 61 */ {223, 255, 127},
106 /* 62 */ {153, 204, 0},
107 /* 63 */ {178, 204, 102},
108 /* 64 */ {114, 153, 0},
109 /* 65 */ {133, 153, 76},
110 /* 66 */ { 95, 127, 0},
111 /* 67 */ {111, 127, 63},
112 /* 68 */ { 57, 76, 0},
113 /* 69 */ { 66, 76, 38},
114 /* 70 */ {127, 255, 0},
115 /* 71 */ {191, 255, 127},
116 /* 72 */ {102, 204, 0},
117 /* 73 */ {153, 204, 102},
118 /* 74 */ { 76, 153, 0},
119 /* 75 */ {114, 153, 76},
120 /* 76 */ { 63, 127, 0},
121 /* 77 */ { 95, 127, 63},
122 /* 78 */ { 38, 76, 0},
123 /* 79 */ { 57, 76, 38},
124 /* 80 */ { 63, 255, 0},
125 /* 81 */ {159, 255, 127},
126 /* 82 */ { 51, 204, 0},
127 /* 83 */ {127, 204, 102},
128 /* 84 */ { 38, 153, 0},
129 /* 85 */ { 95, 153, 76},
130 /* 86 */ { 31, 127, 0},
131 /* 87 */ { 79, 127, 63},
132 /* 88 */ { 19, 76, 0},
133 /* 89 */ { 47, 76, 38},
134 /* 90 */ { 0, 255, 0},
135 /* 91 */ {127, 255, 127},
136 /* 92 */ { 0, 204, 0},
137 /* 93 */ {102, 204, 102},
138 /* 94 */ { 0, 153, 0},
139 /* 95 */ { 76, 153, 76},
140 /* 96 */ { 0, 127, 0},
141 /* 97 */ { 63, 127, 63},
142 /* 98 */ { 0, 76, 0},
143 /* 99 */ { 38, 76, 38},
144 /* 100 */ { 0, 255, 63},
145 /* 101 */ {127, 255, 159},
146 /* 102 */ { 0, 204, 51},
147 /* 103 */ {102, 204, 127},
148 /* 104 */ { 0, 153, 38},
149 /* 105 */ { 76, 153, 95},
150 /* 106 */ { 0, 127, 31},
151 /* 107 */ { 63, 127, 79},
152 /* 108 */ { 0, 76, 19},
153 /* 109 */ { 38, 76, 47},
154 /* 110 */ { 0, 255, 127},
155 /* 111 */ {127, 255, 191},
156 /* 112 */ { 0, 204, 102},
157 /* 113 */ {102, 204, 153},
158 /* 114 */ { 0, 153, 76},
159 /* 115 */ { 76, 153, 114},
160 /* 116 */ { 0, 127, 63},
161 /* 117 */ { 63, 127, 95},
162 /* 118 */ { 0, 76, 38},
163 /* 119 */ { 38, 76, 57},
164 /* 120 */ { 0, 255, 191},
165 /* 121 */ {127, 255, 223},
166 /* 122 */ { 0, 204, 153},
167 /* 123 */ {102, 204, 178},
168 /* 124 */ { 0, 153, 114},
169 /* 125 */ { 76, 153, 133},
170 /* 126 */ { 0, 127, 95},
171 /* 127 */ { 63, 127, 111},
172 /* 128 */ { 0, 76, 57},
173 /* 129 */ { 38, 76, 66},
174 /* 130 */ { 0, 255, 255},
175 /* 131 */ {127, 255, 255},
176 /* 132 */ { 0, 204, 204},
177 /* 133 */ {102, 204, 204},
178 /* 134 */ { 0, 153, 153},
179 /* 135 */ { 76, 153, 153},
180 /* 136 */ { 0, 127, 127},
181 /* 137 */ { 63, 127, 127},
182 /* 138 */ { 0, 76, 76},
183 /* 139 */ { 38, 76, 76},
184 /* 140 */ { 0, 191, 255},
185 /* 141 */ {127, 223, 255},
186 /* 142 */ { 0, 153, 204},
187 /* 143 */ {102, 178, 204},
188 /* 144 */ { 0, 114, 153},
189 /* 145 */ { 76, 133, 153},
190 /* 146 */ { 0, 95, 127},
191 /* 147 */ { 63, 111, 127},
192 /* 148 */ { 0, 57, 76},
193 /* 149 */ { 38, 66, 76},
194 /* 150 */ { 0, 127, 255},
195 /* 151 */ {127, 191, 255},
196 /* 152 */ { 0, 102, 204},
197 /* 153 */ {102, 153, 204},
198 /* 154 */ { 0, 76, 153},
199 /* 155 */ { 76, 114, 153},
200 /* 156 */ { 0, 63, 127},
201 /* 157 */ { 63, 95, 127},
202 /* 158 */ { 0, 38, 76},
203 /* 159 */ { 38, 57, 76},
204 /* 160 */ { 0, 63, 255},
205 /* 161 */ {127, 159, 255},
206 /* 162 */ { 0, 51, 204},
207 /* 163 */ {102, 127, 204},
208 /* 164 */ { 0, 38, 153},
209 /* 165 */ { 76, 95, 153},
210 /* 166 */ { 0, 31, 127},
211 /* 167 */ { 63, 79, 127},
212 /* 168 */ { 0, 19, 76},
213 /* 169 */ { 38, 47, 76},
214 /* 170 */ { 0, 0, 255},
215 /* 171 */ {127, 127, 255},
216 /* 172 */ { 0, 0, 204},
217 /* 173 */ {102, 102, 204},
218 /* 174 */ { 0, 0, 153},
219 /* 175 */ { 76, 76, 153},
220 /* 176 */ { 0, 0, 127},
221 /* 177 */ { 63, 63, 127},
222 /* 178 */ { 0, 0, 76},
223 /* 179 */ { 38, 38, 76},
224 /* 180 */ { 63, 0, 255},
225 /* 181 */ {159, 127, 255},
226 /* 182 */ { 51, 0, 204},
227 /* 183 */ {127, 102, 204},
228 /* 184 */ { 38, 0, 153},
229 /* 185 */ { 95, 76, 153},
230 /* 186 */ { 31, 0, 127},
231 /* 187 */ { 79, 63, 127},
232 /* 188 */ { 19, 0, 76},
233 /* 189 */ { 47, 38, 76},
234 /* 190 */ {127, 0, 255},
235 /* 191 */ {191, 127, 255},
236 /* 192 */ {102, 0, 204},
237 /* 193 */ {153, 102, 204},
238 /* 194 */ { 76, 0, 153},
239 /* 195 */ {114, 76, 153},
240 /* 196 */ { 63, 0, 127},
241 /* 197 */ { 95, 63, 127},
242 /* 198 */ { 38, 0, 76},
243 /* 199 */ { 57, 38, 76},
244 /* 200 */ {191, 0, 255},
245 /* 201 */ {223, 127, 255},
246 /* 202 */ {153, 0, 204},
247 /* 203 */ {178, 102, 204},
248 /* 204 */ {114, 0, 153},
249 /* 205 */ {133, 76, 153},
250 /* 206 */ { 95, 0, 127},
251 /* 207 */ {111, 63, 127},
252 /* 208 */ { 57, 0, 76},
253 /* 209 */ { 66, 38, 76},
254 /* 210 */ {255, 0, 255},
255 /* 211 */ {255, 127, 255},
256 /* 212 */ {204, 0, 204},
257 /* 213 */ {204, 102, 204},
258 /* 214 */ {153, 0, 153},
259 /* 215 */ {153, 76, 153},
260 /* 216 */ {127, 0, 127},
261 /* 217 */ {127, 63, 127},
262 /* 218 */ { 76, 0, 76},
263 /* 219 */ { 76, 38, 76},
264 /* 220 */ {255, 0, 191},
265 /* 221 */ {255, 127, 223},
266 /* 222 */ {204, 0, 153},
267 /* 223 */ {204, 102, 178},
268 /* 224 */ {153, 0, 114},
269 /* 225 */ {153, 76, 133},
270 /* 226 */ {127, 0, 95},
271 /* 227 */ {127, 63, 111},
272 /* 228 */ { 76, 0, 57},
273 /* 229 */ { 76, 38, 66},
274 /* 230 */ {255, 0, 127},
275 /* 231 */ {255, 127, 191},
276 /* 232 */ {204, 0, 102},
277 /* 233 */ {204, 102, 153},
278 /* 234 */ {153, 0, 76},
279 /* 235 */ {153, 76, 114},
280 /* 236 */ {127, 0, 63},
281 /* 237 */ {127, 63, 95},
282 /* 238 */ { 76, 0, 38},
283 /* 239 */ { 76, 38, 57},
284 /* 240 */ {255, 0, 63},
285 /* 241 */ {255, 127, 159},
286 /* 242 */ {204, 0, 51},
287 /* 243 */ {204, 102, 127},
288 /* 244 */ {153, 0, 38},
289 /* 245 */ {153, 76, 95},
290 /* 246 */ {127, 0, 31},
291 /* 247 */ {127, 63, 79},
292 /* 248 */ { 76, 0, 19},
293 /* 249 */ { 76, 38, 47},
294 /* 250 */ { 51, 51, 51},
295 /* 251 */ { 91, 91, 91},
296 /* 252 */ {132, 132, 132},
297 /* 253 */ {173, 173, 173},
298 /* 254 */ {214, 214, 214},
299 /* 255 */ {255, 255, 255}
302 inline DXFVector
Cross(const DXFVector
& v1
, const DXFVector
& v2
)
304 return DXFVector(v1
.y
*v2
.z
- v1
.z
*v2
.y
, v1
.z
*v2
.x
- v1
.x
*v2
.z
, v1
.x
*v2
.y
- v1
.y
*v2
.x
);
307 void DXFFace::CalculateNormal()
317 float mod
= sqrt(n
.x
*n
.x
+ n
.y
*n
.y
+ n
.z
*n
.z
);
323 // convert an AutoCAD ACI colour to wxWidgets RGB colour
324 inline wxColour
ACIColourToRGB(int col
)
326 wxASSERT(col
>= 0 && col
<= 255);
327 return wxColour(aci_to_rgb
[col
].r
, aci_to_rgb
[col
].g
, aci_to_rgb
[col
].b
);
330 // DXFReader constructor
331 DXFRenderer::DXFRenderer()
336 // DXFReader destructor
337 DXFRenderer::~DXFRenderer()
342 // deallocate all the dynamic data
343 void DXFRenderer::Clear()
347 for (DXFLayerList::compatibility_iterator node
= m_layers
.GetFirst(); node
; node
= node
->GetNext())
349 DXFLayer
*current
= node
->GetData();
355 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
357 DXFEntity
*current
= node
->GetData();
364 int DXFRenderer::GetLayerColour(const wxString
& layer
) const
366 for (DXFLayerList::compatibility_iterator node
= m_layers
.GetFirst(); node
; node
= node
->GetNext())
368 DXFLayer
*current
= node
->GetData();
369 if (current
->name
== layer
)
370 return current
->colour
;
375 // read two sequential lines
376 inline void GetLines(wxTextInputStream
& text
, wxString
& line1
, wxString
& line2
)
378 line1
= text
.ReadLine().Trim().Trim(false);
379 line2
= text
.ReadLine().Trim().Trim(false);
382 // parse header section: just skip everything
383 bool DXFRenderer::ParseHeader(wxInputStream
& stream
)
385 wxTextInputStream
text(stream
);
386 wxString line1
, line2
;
387 while (stream
.CanRead())
389 GetLines(text
, line1
, line2
);
390 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
396 // parse tables section: save layers name and colour
397 bool DXFRenderer::ParseTables(wxInputStream
& stream
)
399 wxTextInputStream
text(stream
);
400 wxString line1
, line2
;
403 while (stream
.CanRead())
405 GetLines(text
, line1
, line2
);
406 if (line1
== wxT("0") && inlayer
)
409 if (!layer
.name
.IsEmpty() && layer
.colour
!= -1)
411 DXFLayer
*p
= new DXFLayer
;
412 p
->name
= layer
.name
;
413 p
->colour
= layer
.colour
;
419 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
421 else if (line1
== wxT("0") && line2
== wxT("LAYER"))
425 if (line1
== wxT("2")) // layer name
427 else if (line1
== wxT("62")) // ACI colour
438 // parse entities section: save 3DFACE and LINE entities
439 bool DXFRenderer::ParseEntities(wxInputStream
& stream
)
441 wxTextInputStream
text(stream
);
442 wxString line1
, line2
;
443 int state
= 0; // 0: none, 1: 3DFACE, 2: LINE
447 while (stream
.CanRead())
449 GetLines(text
, line1
, line2
);
450 if (line1
== wxT("0") && state
> 0)
453 if (state
== 1) // 3DFACE
455 DXFFace
*p
= new DXFFace
;
460 p
->CalculateNormal();
464 p
->colour
= GetLayerColour(layer
);
465 m_entities
.Append(p
);
466 colour
= -1; layer
= wxEmptyString
;
467 v
[0] = v
[1] = v
[2] = v
[3] = DXFVector();
470 else if (state
== 2) // LINE
472 DXFLine
*p
= new DXFLine
;
478 p
->colour
= GetLayerColour(layer
);
479 m_entities
.Append(p
);
480 colour
= -1; layer
= wxEmptyString
;
481 v
[0] = v
[1] = v
[2] = v
[3] = DXFVector();
485 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
487 else if (line1
== wxT("0") && line2
== wxT("3DFACE"))
489 else if (line1
== wxT("0") && line2
== wxT("LINE"))
495 if (line1
== wxT("10"))
497 else if (line1
== wxT("20"))
499 else if (line1
== wxT("30"))
501 else if (line1
== wxT("11"))
503 else if (line1
== wxT("21"))
505 else if (line1
== wxT("31"))
507 else if (line1
== wxT("12"))
509 else if (line1
== wxT("22"))
511 else if (line1
== wxT("32"))
513 else if (line1
== wxT("13"))
515 else if (line1
== wxT("23"))
517 else if (line1
== wxT("33"))
519 else if (line1
== wxT("8")) // layer
521 else if (line1
== wxT("62")) // colour
532 // parse and load a DXF file
533 // currently pretty limited, but knows enought do handle 3DFACEs and LINEs
534 bool DXFRenderer::Load(wxInputStream
& stream
)
537 wxTextInputStream
text(stream
);
539 wxString line1
, line2
;
540 while (stream
.CanRead())
542 GetLines(text
, line1
, line2
);
543 if (line1
== wxT("999")) // comment
545 else if (line1
== wxT("0") && line2
== wxT("SECTION"))
547 GetLines(text
, line1
, line2
);
548 if (line1
== wxT("2"))
550 if (line2
== wxT("HEADER"))
552 if (!ParseHeader(stream
))
555 else if (line2
== wxT("TABLES"))
557 if (!ParseTables(stream
))
560 else if (line2
== wxT("ENTITIES"))
562 if (!ParseEntities(stream
))
574 inline float mymin(float x
, float y
) { return x
< y
? x
: y
; }
575 inline float mymax(float x
, float y
) { return x
> y
? x
: y
; }
577 // Scale object boundings to [-5,5]
578 void DXFRenderer::NormalizeEntities()
580 // calculate current min and max boundings of object
581 DXFVector
minv(10e20f
, 10e20f
, 10e20f
);
582 DXFVector
maxv(-10e20f
, -10e20f
, -10e20f
);
583 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
585 DXFEntity
*p
= node
->GetData();
586 if (p
->type
== DXFEntity::Line
)
588 DXFLine
*line
= (DXFLine
*)p
;
589 const DXFVector
*v
[2] = { &line
->v0
, &line
->v1
};
590 for (int i
= 0; i
< 2; ++i
)
592 minv
.x
= mymin(v
[i
]->x
, minv
.x
);
593 minv
.y
= mymin(v
[i
]->y
, minv
.y
);
594 minv
.z
= mymin(v
[i
]->z
, minv
.z
);
595 maxv
.x
= mymax(v
[i
]->x
, maxv
.x
);
596 maxv
.y
= mymax(v
[i
]->y
, maxv
.y
);
597 maxv
.z
= mymax(v
[i
]->z
, maxv
.z
);
599 } else if (p
->type
== DXFEntity::Face
)
601 DXFFace
*face
= (DXFFace
*)p
;
602 const DXFVector
*v
[4] = { &face
->v0
, &face
->v1
, &face
->v2
, &face
->v3
};
603 for (int i
= 0; i
< 4; ++i
)
605 minv
.x
= mymin(v
[i
]->x
, minv
.x
);
606 minv
.y
= mymin(v
[i
]->y
, minv
.y
);
607 minv
.z
= mymin(v
[i
]->z
, minv
.z
);
608 maxv
.x
= mymax(v
[i
]->x
, maxv
.x
);
609 maxv
.y
= mymax(v
[i
]->y
, maxv
.y
);
610 maxv
.z
= mymax(v
[i
]->z
, maxv
.z
);
615 // rescale object down to [-5,5]
616 DXFVector
span(maxv
.x
- minv
.x
, maxv
.y
- minv
.y
, maxv
.z
- minv
.z
);
617 float factor
= mymin(mymin(10.0f
/ span
.x
, 10.0f
/ span
.y
), 10.0f
/ span
.z
);
618 for (DXFEntityList::compatibility_iterator node2
= m_entities
.GetFirst(); node2
; node2
= node2
->GetNext())
620 DXFEntity
*p
= node2
->GetData();
621 if (p
->type
== DXFEntity::Line
)
623 DXFLine
*line
= (DXFLine
*)p
;
624 DXFVector
*v
[2] = { &line
->v0
, &line
->v1
};
625 for (int i
= 0; i
< 2; ++i
)
627 v
[i
]->x
-= minv
.x
+ span
.x
/2; v
[i
]->x
*= factor
;
628 v
[i
]->y
-= minv
.y
+ span
.y
/2; v
[i
]->y
*= factor
;
629 v
[i
]->z
-= minv
.z
+ span
.z
/2; v
[i
]->z
*= factor
;
631 } else if (p
->type
== DXFEntity::Face
)
633 DXFFace
*face
= (DXFFace
*)p
;
634 DXFVector
*v
[4] = { &face
->v0
, &face
->v1
, &face
->v2
, &face
->v3
};
635 for (int i
= 0; i
< 4; ++i
)
637 v
[i
]->x
-= minv
.x
+ span
.x
/2; v
[i
]->x
*= factor
;
638 v
[i
]->y
-= minv
.y
+ span
.y
/2; v
[i
]->y
*= factor
;
639 v
[i
]->z
-= minv
.z
+ span
.z
/2; v
[i
]->z
*= factor
;
645 // OpenGL renderer for DXF entities
646 void DXFRenderer::Render() const
651 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
653 DXFEntity
*p
= node
->GetData();
654 wxColour c
= ACIColourToRGB(p
->colour
);
655 if (p
->type
== DXFEntity::Line
)
657 DXFLine
*line
= (DXFLine
*)p
;
659 glColor3f(c
.Red()/255.0,c
.Green()/255.0,c
.Blue()/255.0);
660 glVertex3f(line
->v0
.x
, line
->v0
.y
, line
->v0
.z
);
661 glVertex3f(line
->v1
.x
, line
->v1
.y
, line
->v1
.z
);
664 else if (p
->type
== DXFEntity::Face
)
666 DXFFace
*face
= (DXFFace
*)p
;
667 glBegin(GL_TRIANGLES
);
668 glColor3f(c
.Red()/255.0,c
.Green()/255.0,c
.Blue()/255.0);
669 glNormal3f(face
->n
.x
, face
->n
.y
, face
->n
.z
);
670 glVertex3f(face
->v0
.x
, face
->v0
.y
, face
->v0
.z
);
671 glVertex3f(face
->v1
.x
, face
->v1
.y
, face
->v1
.z
);
672 glVertex3f(face
->v2
.x
, face
->v2
.y
, face
->v2
.z
);