-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathDXFWriterNodeVisitor.h
More file actions
284 lines (220 loc) · 7.68 KB
/
DXFWriterNodeVisitor.h
File metadata and controls
284 lines (220 loc) · 7.68 KB
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
// -*-c++-*-
/*
* Wavefront DXF loader for Open Scene Graph
*
* Copyright (C) 2001 Ulrich Hertlein <u.hertlein@web.de>
*
* Modified by Robert Osfield to support per Drawable coord, normal and
* texture coord arrays, bug fixes, and support for texture mapping.
*
* Writing support added 2007 by Stephan Huber, http://digitalmind.de,
* some ideas taken from the dae-plugin
*
* The Open Scene Graph (OSG) is a cross platform C++/OpenGL library for
* real-time rendering of large 3D photo-realistic models.
* The OSG homepage is http://www.openscenegraph.org/
*/
#ifndef DXF_WRITER_NODE_VISITOR_HEADER__
#define DXF_WRITER_NODE_VISITOR_HEADER__
#include <string>
#include <stack>
#include <sstream>
#include <osg/Notify>
#include <osg/Node>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/Material>
#include <osg/Texture2D>
#include <osg/TexGen>
#include <osg/TexMat>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <map>
#include <set>
#include <iostream>
struct Layer
{
public:
Layer(const std::string name="",unsigned int color=7) : _name(name),_color(color) { }
std::string _name;
unsigned int _color;
};
// reuse aci class for autocad colors, see http://bitsy.sub-atomic.com/~moses/acadcolors.html for samples
#include "aci.h"
class AcadColor
{
public:
AcadColor()
{
int index=10;
for (int ii=10*3;ii<256*3; ) {
// find RGB for each Autocad index colour
unsigned int red = (int)floor(aci::table[ii++]*255.0f);
unsigned int green = (int)floor(aci::table[ii++]*255.0f);
unsigned int blue = (int)floor(aci::table[ii++]*255.0f);
unsigned int rgb = (red<<16) + (green<<8) + blue;
_indexColors[rgb]=index++;
}
//
}
// returns Autocad index color for supplied RGB.
// if no exact match is found returns nearest color based on hue
// also adds match to cache for future lookups.
int findColor(unsigned int rgb)
{
int aci = 255;
ColorMap::const_iterator itr = _indexColors.find(rgb);
if (itr != _indexColors.end() ) {
aci = itr->second;
} else {
// not found - match based on hue
aci = nearestColor(rgb);
// add matching colour to list to cache
_indexColors[rgb]=aci;
}
return aci;
}
protected:
// returns hue as an angle in range 0-360, saturation and value as 0-1
void hsv(unsigned int rgb,float &hue,float &sat,float &value)
{
int red = rgb>>16;
int green = (0x0000ff00&rgb)>>8;
int blue = 0x000000ff&rgb;
int H=std::max(std::max(red,green),blue);
int L=std::min(std::min(red,green),blue);
value = (float)H/255.0f; // note hsv and hsl define v differently!
sat=(float)(H-L)/(float)H;
if (H==L) {
hue=0.0;
}else if (H==red) {
hue=360.0 + (60.0 * (float)(green-blue)/(float)(H-L));
if ( hue > 360 ) { hue-=360; }
} else if (H==green) {
hue=120.0 + (60.0 * (float)(blue-red)/(float)(H-L));
} else if (H==blue) {
hue=240.0 + (60.0 * (float)(red-green)/(float)(H-L));
} else {
hue = 0.0;
}
}
int nearestColor(unsigned int rgb)
{
//- match based on hue
float h;
float s;
float v;
hsv(rgb,h,s,v);
// aci index format is
// last digit odd = 50% sat, even=100%
// last digit 0,1 = 100% value, 2,3=80%, 4,5=60% 6,7=50%, 8,9=30%
// first two digits are hue angle /1.5 but count starts at 10, first 9 values are dummy named colours
int aci=10 + (int)(h/1.5);
aci -= (aci%10); // ensure last digit is zero
if ( v < 0.3 ) {
aci += 9;
} else if ( v < 0.5 ) {
aci += 6;
} else if ( v < 0.6 ) {
aci += 4;
} else if ( v < 0.8 ) {
aci += 2;
} else {
// last digit=0;
}
if ( s<0.5 ) {
aci += 1;
}
return aci;
}
protected:
typedef std::map<unsigned int, unsigned char> ColorMap;
ColorMap _indexColors; // maps RGB to autocad index colour
ColorMap _hueColors; // maps hue angle to autocad index colour
};
class DXFWriterNodeVisitor: public osg::NodeVisitor {
public:
DXFWriterNodeVisitor(std::ostream& fout) :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_fout(fout),
_currentStateSet(new osg::StateSet()),
_count(0),
_firstPass(true),
_writeTriangleAs3DFace(true)
{
}
static unsigned int getNodeRGB(osg::Geometry *geo,unsigned int index=0)
{
osg::Vec4Array* data=static_cast<osg::Vec4Array*>(geo->getColorArray());
if ( data && index<data->size() ) {
return (data->at(index).asABGR())>>8;
}
return 0;
}
bool writeHeader(const osg::BoundingSphere &bound);// call after first pass to trigger draw pass
void writeFooter();
void buildColorMap();
virtual void apply(osg::Geode &node);
virtual void apply(osg::Group &node)
{
osg::NodeVisitor::traverse( node );
}
void traverse (osg::Node &node)
{
pushStateSet(node.getStateSet());
osg::NodeVisitor::traverse( node );
popStateSet(node.getStateSet());
}
void pushStateSet(osg::StateSet* ss)
{
if (NULL!=ss) {
// Save our current stateset
_stateSetStack.push(_currentStateSet.get());
// merge with node stateset
_currentStateSet = static_cast<osg::StateSet*>(_currentStateSet->clone(osg::CopyOp::SHALLOW_COPY));
_currentStateSet->merge(*ss);
}
}
void popStateSet(osg::StateSet* ss)
{
if (NULL!=ss) {
// restore the previous stateset
_currentStateSet = _stateSetStack.top();
_stateSetStack.pop();
}
}
int getNodeAcadColor(osg::Geometry* /*geo*/,int /*index*/=0) { return 0;}
protected:
struct CompareStateSet
{
bool operator()(const osg::ref_ptr<osg::StateSet>& ss1, const osg::ref_ptr<osg::StateSet>& ss2) const
{
return ss1->compare(*ss2, true) < 0;
}
};
private:
DXFWriterNodeVisitor& operator = (const DXFWriterNodeVisitor&) { return *this; }
// first pass get layer names and draw types
void makeGeometryLayer(osg::Geometry* geo);
// second pass - output data
void processGeometry(osg::Geometry* geo, osg::Matrix& m);
void processArray(osg::Array* array, const Layer& layer,const osg::Matrix& m = osg::Matrix::identity());
void processStateSet(osg::StateSet* stateset);
std::string getLayerName(const std::string& defaultValue = "");
typedef std::stack<osg::ref_ptr<osg::StateSet> > StateSetStack;
std::ostream& _fout;
std::list<std::string> _nameStack;
StateSetStack _stateSetStack;
osg::ref_ptr<osg::StateSet> _currentStateSet;
unsigned int _count;
std::vector<Layer> _layers;
bool _firstPass;
Layer _layer;
bool _writeTriangleAs3DFace;
AcadColor _acadColor;
};
#endif