16 #include "string_utils.h" 17 #include "virtualFileSystem.h" 33 { 0.65, 0.325, 0.325 },
39 { 0.15, 0.075, 0.075 },
43 { 0.65, 0.4063, 0.325 },
45 { 0.5, 0.3125, 0.25 },
47 { 0.3, 0.1875, 0.15 },
49 { 0.15, 0.0938, 0.075 },
53 { 0.65, 0.4875, 0.325 },
59 { 0.15, 0.1125, 0.075 },
63 { 0.65, 0.5688, 0.325 },
65 { 0.5, 0.4375, 0.25 },
67 { 0.3, 0.2625, 0.15 },
69 { 0.15, 0.1313, 0.075 },
73 { 0.65, 0.65, 0.325 },
79 { 0.15, 0.15, 0.075 },
83 { 0.5688, 0.65, 0.325 },
85 { 0.4375, 0.5, 0.25 },
87 { 0.2625, 0.3, 0.15 },
89 { 0.1313, 0.15, 0.075 },
93 { 0.4875, 0.65, 0.325 },
99 { 0.1125, 0.15, 0.075 },
103 { 0.4063, 0.65, 0.325 },
105 { 0.3125, 0.5, 0.25 },
107 { 0.1875, 0.3, 0.15 },
109 { 0.0938, 0.15, 0.075 },
113 { 0.325, 0.65, 0.325 },
119 { 0.075, 0.15, 0.075 },
123 { 0.325, 0.65, 0.4063 },
125 { 0.25, 0.5, 0.3125 },
127 { 0.15, 0.3, 0.1875 },
129 { 0.075, 0.15, 0.0938 },
133 { 0.325, 0.65, 0.4875 },
135 { 0.25, 0.5, 0.375 },
137 { 0.15, 0.3, 0.225 },
139 { 0.075, 0.15, 0.1125 },
143 { 0.325, 0.65, 0.5688 },
145 { 0.25, 0.5, 0.4375 },
147 { 0.15, 0.3, 0.2625 },
149 { 0.075, 0.15, 0.1313 },
153 { 0.325, 0.65, 0.65 },
159 { 0.075, 0.15, 0.15 },
163 { 0.325, 0.5688, 0.65 },
165 { 0.25, 0.4375, 0.5 },
167 { 0.15, 0.2625, 0.3 },
169 { 0.075, 0.1313, 0.15 },
173 { 0.325, 0.4875, 0.65 },
175 { 0.25, 0.375, 0.5 },
177 { 0.15, 0.225, 0.3 },
179 { 0.075, 0.1125, 0.15 },
183 { 0.325, 0.4063, 0.65 },
185 { 0.25, 0.3125, 0.5 },
187 { 0.15, 0.1875, 0.3 },
189 { 0.075, 0.0938, 0.15 },
193 { 0.325, 0.325, 0.65 },
199 { 0.075, 0.075, 0.15 },
203 { 0.4063, 0.325, 0.65 },
205 { 0.3125, 0.25, 0.5 },
207 { 0.1875, 0.15, 0.3 },
209 { 0.0938, 0.075, 0.15 },
213 { 0.4875, 0.325, 0.65 },
215 { 0.375, 0.25, 0.5 },
217 { 0.225, 0.15, 0.3 },
219 { 0.1125, 0.075, 0.15 },
223 { 0.5688, 0.325, 0.65 },
225 { 0.4375, 0.25, 0.5 },
227 { 0.2625, 0.15, 0.3 },
229 { 0.1313, 0.075, 0.15 },
233 { 0.65, 0.325, 0.65 },
239 { 0.15, 0.075, 0.15 },
243 { 0.65, 0.325, 0.5688 },
245 { 0.5, 0.25, 0.4375 },
247 { 0.3, 0.15, 0.2625 },
249 { 0.15, 0.075, 0.1313 },
253 { 0.65, 0.325, 0.4875 },
255 { 0.5, 0.25, 0.375 },
257 { 0.3, 0.15, 0.225 },
259 { 0.15, 0.075, 0.1125 },
263 { 0.65, 0.325, 0.4063 },
265 { 0.5, 0.25, 0.3125 },
267 { 0.3, 0.15, 0.1875 },
269 { 0.15, 0.075, 0.0938 },
270 { 0.33, 0.33, 0.33 },
271 { 0.464, 0.464, 0.464 },
272 { 0.598, 0.598, 0.598 },
273 { 0.732, 0.732, 0.732 },
274 { 0.866, 0.866, 0.866 },
319 if (in == (istream *)NULL) {
345 while (_state != ST_done && _state != ST_error) {
474 double best_diff = 4.0;
477 for (
int i = 0; i < 255; i++) {
478 double diff = ((r - _colors[i].r) * (r - _colors[i].r) +
479 (g - _colors[i].g) * (g - _colors[i].g) +
480 (b - _colors[i].b) * (b - _colors[i].b));
481 if (diff < best_diff) {
500 if (_color_index >= 0 && _color_index <= 255) {
501 return _colors[_color_index];
530 DXFVertices::iterator vi;
531 for (vi = _verts.begin(); vi != _verts.end(); ++vi) {
532 (*vi)._p = (*vi)._p * _ocs2wcs;
553 if (fabs(_z[0]) < 1.0/64.0 && fabs(_z[1]) < 1.0/64.0) {
564 ocs( x[0], x[1], x[2], 0,
566 _z[0], _z[1], _z[2], 0,
585 change_state(ST_error);
591 while (in && in.peek() !=
'\n') {
595 while (in && isspace(in.peek()) && in.peek() !=
'\n') {
599 getline(in, _string);
600 _string = trim_right(_string);
603 change_state(ST_error);
608 }
while (_code == 999);
621 change_state(State new_state) {
622 if (_state == ST_verts) {
624 _p.set(0.0, 0.0, 0.0);
625 _q.set(0.0, 0.0, 0.0);
626 _r.set(0.0, 0.0, 0.0);
627 _s.set(0.0, 0.0, 0.0);
629 if ((_state == ST_entity || _state == ST_verts) &&
630 new_state != ST_verts) {
659 change_section(Section new_section) {
660 change_state(ST_section);
661 _section = new_section;
675 change_layer(
const string &layer_name) {
676 if (_layer == NULL || _layer->get_name() != layer_name) {
677 _layer = _layers.
get_layer(layer_name,
this);
688 change_entity(Entity new_entity) {
689 if (new_entity == EN_vertex && _vertices_follow) {
693 change_state(ST_verts);
697 change_state(ST_entity);
698 _entity = new_entity;
711 _p.set(0.0, 0.0, 0.0);
712 _q.set(0.0, 0.0, 0.0);
713 _r.set(0.0, 0.0, 0.0);
714 _s.set(0.0, 0.0, 0.0);
715 _z.set(0.0, 0.0, 1.0);
716 _vertices_follow =
false;
719 _verts.erase(_verts.begin(), _verts.end());
732 nout <<
"Group code 0 not found at top level; found code " << _code
734 change_state(ST_error);
736 if (_string ==
"SECTION") {
739 nout <<
"Group code 0 not immediately followed by code 2; found code " 740 << _code <<
" instead.\n";
742 if (_string ==
"HEADER") {
743 change_section(SE_header);
744 }
else if (_string ==
"TABLES") {
745 change_section(SE_tables);
746 }
else if (_string ==
"BLOCKS") {
747 change_section(SE_blocks);
748 }
else if (_string ==
"ENTITIES") {
749 change_section(SE_entities);
750 }
else if (_string ==
"OBJECTS") {
751 change_section(SE_objects);
753 change_section(SE_unknown);
757 }
else if (_string ==
"EOF") {
758 change_state(ST_done);
760 nout <<
"Unexpected section at top level: '" << _string <<
"'\n";
761 change_state(ST_error);
780 if (_string ==
"ENDSEC") {
781 change_state(ST_top);
783 if (_section == SE_entities) {
784 if (_string ==
"3DFACE") {
785 change_entity(EN_3dface);
786 }
else if (_string ==
"POINT") {
787 change_entity(EN_point);
788 }
else if (_string ==
"INSERT") {
789 change_entity(EN_insert);
790 }
else if (_string ==
"VERTEX") {
791 change_entity(EN_vertex);
792 }
else if (_string ==
"POLYLINE") {
793 change_entity(EN_polyline);
795 change_entity(EN_unknown);
802 change_layer(_string);
806 _color_index = string_to_int(_string, tail);
831 change_layer(_string);
835 _p[0] = string_to_double(_string, tail);
839 _q[0] = string_to_double(_string, tail);
843 _r[0] = string_to_double(_string, tail);
847 _s[0] = string_to_double(_string, tail);
851 _p[1] = string_to_double(_string, tail);
855 _q[1] = string_to_double(_string, tail);
859 _r[1] = string_to_double(_string, tail);
863 _s[1] = string_to_double(_string, tail);
867 _p[2] = string_to_double(_string, tail);
871 _q[2] = string_to_double(_string, tail);
875 _r[2] = string_to_double(_string, tail);
879 _s[2] = string_to_double(_string, tail);
883 _color_index = string_to_int(_string, tail);
887 _vertices_follow = (string_to_int(_string, tail) != 0);
891 _flags = string_to_int(_string, tail);
895 _z[0] = string_to_double(_string, tail);
899 _z[1] = string_to_double(_string, tail);
903 _z[2] = string_to_double(_string, tail);
928 change_layer(_string);
932 _p[0] = string_to_double(_string, tail);
936 _p[1] = string_to_double(_string, tail);
940 _p[2] = string_to_double(_string, tail);
949 ostream &operator << (ostream &out,
const DXFFile::State &state) {
951 case DXFFile::ST_top:
952 return out <<
"ST_top";
953 case DXFFile::ST_section:
954 return out <<
"ST_section";
955 case DXFFile::ST_entity:
956 return out <<
"ST_entity";
957 case DXFFile::ST_verts:
958 return out <<
"ST_verts";
959 case DXFFile::ST_error:
960 return out <<
"ST_error";
961 case DXFFile::ST_done:
962 return out <<
"ST_done";
964 return out <<
"Unknown state";
967 ostream &operator << (ostream &out,
const DXFFile::Section §ion) {
969 case DXFFile::SE_unknown:
970 return out <<
"SE_unknown";
971 case DXFFile::SE_header:
972 return out <<
"SE_header";
973 case DXFFile::SE_tables:
974 return out <<
"SE_tables";
975 case DXFFile::SE_blocks:
976 return out <<
"SE_blocks";
977 case DXFFile::SE_entities:
978 return out <<
"SE_entities";
979 case DXFFile::SE_objects:
980 return out <<
"SE_objects";
982 return out <<
"Unknown section";
985 ostream &operator << (ostream &out,
const DXFFile::Entity &entity) {
987 case DXFFile::EN_unknown:
988 return out <<
"EN_unknown";
989 case DXFFile::EN_3dface:
990 return out <<
"EN_3dface";
991 case DXFFile::EN_point:
992 return out <<
"EN_point";
993 case DXFFile::EN_insert:
994 return out <<
"EN_insert";
995 case DXFFile::EN_vertex:
996 return out <<
"EN_vertex";
997 case DXFFile::EN_polyline:
998 return out <<
"EN_polyline";
1000 return out <<
"Unknown entity";
This is a 4-by-4 transform matrix.
virtual void end_file()
A hook for user code, if desired.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read...
virtual void begin_section()
A hook for user code, if desired.
DXFLayer * get_layer(const string &name, DXFFile *dxffile)
Looks up the layer name in the map, and returns a pointer to the associated DXFLayer.
void set_text()
Indicates that the filename represents a text file.
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
bool normalize()
Normalizes the vector in place.
virtual void error()
A hook for user code, if desired.
The name of a file, such as a texture file or an Egg file.
virtual void begin_file()
A hook for user code, if desired.
virtual void done_entity()
This is the primary hook for user code.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
bool invert_from(const LMatrix4d &other)
Computes the inverse of the other matrix, and stores the result in this matrix.
virtual void end_section()
A hook for user code, if desired.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Stored within DXFFile, this is the basic Vertex data of a DXF file.
void ocs_2_wcs()
Assuming the current entity is a planar-based entity, for instance, a 2-d polygon (as opposed to a 3-...
static int find_color(double r, double g, double b)
Returns the index of the closest matching AutoCAD color to the indicated r, g, b. ...
virtual void done_vertex()
A hook for user code, if desired.
void process(Filename filename)
Opens the indicated filename and reads it as a DXF file.
const Color & get_color() const
This is a convenience function to return the r,g,b color of the current entity (at the time of done_e...