From Sylvain Marie, "changed raw pointers to smart pointers in the dxfBlock, dxfTables and

dxfSection classes, so their members data are correctly deleted.
- changed some methods signatures to pass arguments by reference instead of
by value. The performance and memory usage are enhanced (the reader was
clogging the heap when reading some large DXF files)

The updated files have been compiled and tested with a variety of DXF files
on XP with VS2003, but the changes should not disturb any other compiler."
This commit is contained in:
Robert Osfield
2006-12-05 15:31:07 +00:00
parent 8e0bb5f6ef
commit d70cf68ae6
7 changed files with 95 additions and 94 deletions

View File

@@ -19,6 +19,7 @@
#include <osg/Referenced> #include <osg/Referenced>
#include <osg/Vec3d> #include <osg/Vec3d>
#include <osg/ref_ptr>
class dxfFile; class dxfFile;
class codeValue; class codeValue;
@@ -27,17 +28,17 @@ class dxfEntity;
class dxfBlock : public osg::Referenced class dxfBlock : public osg::Referenced
{ {
public: public:
dxfBlock() : _currentEntity(NULL) {} dxfBlock() : _currentEntity(NULL) {}
virtual ~dxfBlock() {} virtual ~dxfBlock() {}
inline const std::string& getName() const { return _name; } inline const std::string& getName() const { return _name; }
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
std::vector<dxfEntity*> getEntityList() { return _entityList; } std::vector<osg::ref_ptr<dxfEntity> > & getEntityList() { return _entityList; }
const osg::Vec3d& getPosition() const; const osg::Vec3d& getPosition() const;
protected: protected:
std::vector<dxfEntity*> _entityList; std::vector<osg::ref_ptr<dxfEntity> > _entityList;
dxfEntity* _currentEntity; dxfEntity* _currentEntity;
std::string _name; std::string _name;
osg::Vec3d _position; osg::Vec3d _position;
}; };
#endif #endif

View File

@@ -418,8 +418,8 @@ dxfPolyline::drawScene(scene* sc)
Vec3d nr; Vec3d nr;
bool nset = false; bool nset = false;
//dxfVertex* v = NULL; //dxfVertex* v = NULL;
unsigned short ncount; unsigned int ncount;
unsigned short mcount; unsigned int mcount;
if (_surfacetype == 6) { if (_surfacetype == 6) {
// I dont have examples of type 5 and 8, but they may be the same as 6 // I dont have examples of type 5 and 8, but they may be the same as 6
mcount = _mdensity; mcount = _mdensity;
@@ -428,8 +428,8 @@ dxfPolyline::drawScene(scene* sc)
mcount = _mcount; mcount = _mcount;
ncount = _ncount; ncount = _ncount;
} }
for (unsigned short n = 0; n < ncount-1; n++) { for (unsigned int n = 0; n < ncount-1; n++) {
for (unsigned short m = 1; m < mcount; m++) { for (unsigned int m = 1; m < mcount; m++) {
// 0 // 0
a = _vertices[(m-1)*ncount+n].get()->getVertex(); a = _vertices[(m-1)*ncount+n].get()->getVertex();
// 1 // 1
@@ -463,7 +463,7 @@ dxfPolyline::drawScene(scene* sc)
} }
} }
if (_flag & 1) { if (_flag & 1) {
for (unsigned short n = 0; n < ncount-1; n++) { for (unsigned int n = 0; n < ncount-1; n++) {
// 0 // 0
a = _vertices[(mcount-1)*ncount+n].get()->getVertex(); a = _vertices[(mcount-1)*ncount+n].get()->getVertex();
// 1 // 1
@@ -492,7 +492,7 @@ dxfPolyline::drawScene(scene* sc)
} }
} }
if (_flag & 32) { if (_flag & 32) {
for (unsigned short m = 1; m < mcount; m++) { for (unsigned int m = 1; m < mcount; m++) {
// 0 // 0
a = _vertices[(m-1)*ncount+(ncount-1)].get()->getVertex(); a = _vertices[(m-1)*ncount+(ncount-1)].get()->getVertex();
// 1 // 1
@@ -553,7 +553,7 @@ dxfPolyline::drawScene(scene* sc)
} else if (_flag & 64) { } else if (_flag & 64) {
unsigned short _facetype = 3; unsigned short _facetype = 3;
for (unsigned short i = 0; i < _indices.size(); i++) { for (unsigned int i = 0; i < _indices.size(); i++) {
dxfVertex* vindice = _indices[i].get(); dxfVertex* vindice = _indices[i].get();
if (!vindice) continue; if (!vindice) continue;
if (vindice->getIndice4()) { if (vindice->getIndice4()) {
@@ -751,8 +751,8 @@ dxfInsert::drawScene(scene* sc)
sc->pushMatrix(m); sc->pushMatrix(m);
sc->pushMatrix(back); sc->pushMatrix(back);
std::vector<dxfEntity*> l = _block->getEntityList(); std::vector<osg::ref_ptr<dxfEntity> > l = _block->getEntityList();
for (std::vector<dxfEntity*>::iterator itr = l.begin(); itr != l.end(); ++itr) { for (std::vector<osg::ref_ptr<dxfEntity> >::iterator itr = l.begin(); itr != l.end(); ++itr) {
dxfBasicEntity* e = (*itr)->getEntity(); dxfBasicEntity* e = (*itr)->getEntity();
if (e) { if (e) {
e->drawScene(sc); e->drawScene(sc);

View File

@@ -147,14 +147,14 @@ public:
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
void getVertex(double &x, double &y, double &z) { x=_vertex.x();y=_vertex.y();z=_vertex.z(); } void getVertex(double &x, double &y, double &z) { x=_vertex.x();y=_vertex.y();z=_vertex.z(); }
const osg::Vec3d& getVertex() const { return _vertex; } const osg::Vec3d& getVertex() const { return _vertex; }
const unsigned short getIndice1() const { return _indice1; } const unsigned int getIndice1() const { return _indice1; }
const unsigned short getIndice2() const { return _indice2; } const unsigned int getIndice2() const { return _indice2; }
const unsigned short getIndice3() const { return _indice3; } const unsigned int getIndice3() const { return _indice3; }
const unsigned short getIndice4() const { return _indice4; } const unsigned int getIndice4() const { return _indice4; }
protected: protected:
osg::Vec3d _vertex; osg::Vec3d _vertex;
unsigned short _indice1, _indice2, _indice3, _indice4; unsigned int _indice1, _indice2, _indice3, _indice4;
}; };
class dxfPolyline : public dxfBasicEntity class dxfPolyline : public dxfBasicEntity
@@ -185,11 +185,11 @@ protected:
std::vector<osg::ref_ptr<dxfVertex> > _indices; std::vector<osg::ref_ptr<dxfVertex> > _indices;
double _elevation; double _elevation;
unsigned short _flag; unsigned short _flag;
unsigned short _mcount; unsigned int _mcount;
unsigned short _ncount; unsigned int _ncount;
unsigned short _nstart; // 71 unsigned short _nstart; // 71
unsigned short _nend; //72 unsigned short _nend; //72
osg::Vec3d _ocs; //210 220 230 osg::Vec3d _ocs; //210 220 230
unsigned short _mdensity; // 73 unsigned short _mdensity; // 73
unsigned short _ndensity; // 74 unsigned short _ndensity; // 74
unsigned short _surfacetype; //75 unsigned short _surfacetype; //75
@@ -246,8 +246,8 @@ protected:
// entities (dxf garble things) in the sequence // entities (dxf garble things) in the sequence
double _rotation; double _rotation;
osg::Vec3d _scale; osg::Vec3d _scale;
osg::Vec3d _point; osg::Vec3d _point;
osg::Vec3d _ocs; osg::Vec3d _ocs;
}; };
class dxfEntity : public osg::Referenced class dxfEntity : public osg::Referenced

View File

@@ -78,7 +78,7 @@ void dxfEntities::assign(dxfFile* dxf, codeValue& cv)
void void
dxfEntities::drawScene(scene* sc) dxfEntities::drawScene(scene* sc)
{ {
for (std::vector<dxfEntity*>::iterator itr = _entityList.begin(); for (std::vector<osg::ref_ptr<dxfEntity> >::iterator itr = _entityList.begin();
itr != _entityList.end(); ++itr) itr != _entityList.end(); ++itr)
(*itr)->drawScene(sc); (*itr)->drawScene(sc);
} }

View File

@@ -11,10 +11,10 @@
*/ */
/** /**
Classes used to parse each section of a DXF file. Not all Classes used to parse each section of a DXF file. Not all
types of section has been defined here, just the ones types of section has been defined here, just the ones
I found of interest, ie HEADER, TABLES, BLOCKS, and ENTITIES. I found of interest, ie HEADER, TABLES, BLOCKS, and ENTITIES.
Yet to be implemented: CLASSES, OBJECTS, and THUMBNAILIMAGE. Yet to be implemented: CLASSES, OBJECTS, and THUMBNAILIMAGE.
*/ */
#ifndef DXF_SECTION #ifndef DXF_SECTION
#define DXF_SECTION 1 #define DXF_SECTION 1
@@ -24,6 +24,8 @@
#include "dxfTable.h" #include "dxfTable.h"
#include "codeValue.h" #include "codeValue.h"
#include "scene.h" #include "scene.h"
#include "dxfEntity.h"
#include "dxfBlock.h"
#include <map> #include <map>
#include <vector> #include <vector>
@@ -31,77 +33,75 @@
#include <string> #include <string>
class dxfFile; class dxfFile;
class dxfEntity;
class dxfBlock;
class dxfSection : public dxfSectionBase class dxfSection : public dxfSectionBase
{ {
public: public:
dxfSection() {} dxfSection() {}
virtual ~dxfSection() {} virtual ~dxfSection() {}
}; };
class dxfHeader : public dxfSection class dxfHeader : public dxfSection
{ {
public: public:
dxfHeader() : _inVariable(false) {} dxfHeader() : _inVariable(false) {}
virtual ~dxfHeader() {} virtual ~dxfHeader() {}
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
VariableList& getVariable(std::string inVar) { return _variables[inVar]; } VariableList& getVariable(std::string inVar) { return _variables[inVar]; }
protected: protected:
std::map<std::string, VariableList> _variables; std::map<std::string, VariableList> _variables;
bool _inVariable; bool _inVariable;
std::string _currentVariable; std::string _currentVariable;
}; };
class dxfTables : public dxfSection class dxfTables : public dxfSection
{ {
public: public:
dxfTables() : _inLayerTable(false) {} dxfTables() : _inLayerTable(false) {}
virtual ~dxfTables() {} virtual ~dxfTables() {}
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
dxfLayerTable* getOrCreateLayerTable() dxfLayerTable* getOrCreateLayerTable()
{ {
if (!_layerTable.get()) if (!_layerTable.get())
_layerTable = new dxfLayerTable; _layerTable = new dxfLayerTable;
return _layerTable.get(); return _layerTable.get();
} }
protected: protected:
bool _inLayerTable; bool _inLayerTable;
osg::ref_ptr<dxfLayerTable> _layerTable; osg::ref_ptr<dxfLayerTable> _layerTable;
std::vector<osg::ref_ptr<dxfTable> > _others; std::vector<osg::ref_ptr<dxfTable> > _others;
osg::ref_ptr<dxfTable> _currentTable; osg::ref_ptr<dxfTable> _currentTable;
}; };
class dxfEntities : public dxfSection class dxfEntities : public dxfSection
{ {
public: public:
dxfEntities() : _currentEntity(NULL) {} dxfEntities() : _currentEntity(NULL) {}
virtual ~dxfEntities() {} virtual ~dxfEntities() {}
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
virtual void drawScene(scene* sc); virtual void drawScene(scene* sc);
protected: protected:
dxfEntity* _currentEntity; dxfEntity* _currentEntity;
std::vector<dxfEntity*> _entityList; std::vector<osg::ref_ptr<dxfEntity> > _entityList;
}; };
class dxfBlocks : public dxfSection class dxfBlocks : public dxfSection
{ {
public: public:
dxfBlocks() : _currentBlock(NULL) {} dxfBlocks() : _currentBlock(NULL) {}
virtual ~dxfBlocks() {} virtual ~dxfBlocks() {}
virtual void assign(dxfFile* dxf, codeValue& cv); virtual void assign(dxfFile* dxf, codeValue& cv);
dxfBlock* findBlock(std::string s); dxfBlock* findBlock(std::string s);
protected: protected:
dxfBlock* _currentBlock; dxfBlock* _currentBlock;
std::map<std::string, dxfBlock*> _blockNameList; std::map<std::string, dxfBlock*> _blockNameList;
std::vector<dxfBlock*> _blockList; std::vector<osg::ref_ptr<dxfBlock> > _blockList;
}; };
#endif #endif

View File

@@ -52,7 +52,7 @@ Vec3d scene::addNormal(Vec3d v)
} }
void void
scene::addLine(std::string l, unsigned short color, Vec3d s, Vec3d e) scene::addLine(const std::string & l, unsigned short color, Vec3d & s, Vec3d & e)
{ {
dxfLayer* layer = _layerTable->findOrCreateLayer(l); dxfLayer* layer = _layerTable->findOrCreateLayer(l);
if (layer->getFrozen()) return; if (layer->getFrozen()) return;
@@ -61,7 +61,7 @@ scene::addLine(std::string l, unsigned short color, Vec3d s, Vec3d e)
ly->_lines[correctedColorIndex(l, color)].push_back(a); ly->_lines[correctedColorIndex(l, color)].push_back(a);
ly->_lines[correctedColorIndex(l, color)].push_back(b); ly->_lines[correctedColorIndex(l, color)].push_back(b);
} }
void scene::addLineStrip(std::string l, unsigned short color, std::vector<Vec3d> vertices) void scene::addLineStrip(const std::string & l, unsigned short color, std::vector<Vec3d> & vertices)
{ {
dxfLayer* layer = _layerTable->findOrCreateLayer(l); dxfLayer* layer = _layerTable->findOrCreateLayer(l);
if (layer->getFrozen()) return; if (layer->getFrozen()) return;
@@ -73,7 +73,7 @@ void scene::addLineStrip(std::string l, unsigned short color, std::vector<Vec3d>
} }
ly->_linestrips[correctedColorIndex(l, color)].push_back(converted); ly->_linestrips[correctedColorIndex(l, color)].push_back(converted);
} }
void scene::addLineLoop(std::string l, unsigned short color, std::vector<Vec3d> vertices) void scene::addLineLoop(const std::string & l, unsigned short color, std::vector<Vec3d> & vertices)
{ {
dxfLayer* layer = _layerTable->findOrCreateLayer(l); dxfLayer* layer = _layerTable->findOrCreateLayer(l);
if (layer->getFrozen()) return; if (layer->getFrozen()) return;
@@ -88,7 +88,7 @@ void scene::addLineLoop(std::string l, unsigned short color, std::vector<Vec3d>
} }
void scene::addTriangles(std::string l, unsigned short color, std::vector<Vec3d> vertices, bool inverted) void scene::addTriangles(const std::string & l, unsigned short color, std::vector<Vec3d> & vertices, bool inverted)
{ {
dxfLayer* layer = _layerTable->findOrCreateLayer(l); dxfLayer* layer = _layerTable->findOrCreateLayer(l);
if (layer->getFrozen()) return; if (layer->getFrozen()) return;
@@ -119,7 +119,7 @@ void scene::addTriangles(std::string l, unsigned short color, std::vector<Vec3d>
} }
} }
} }
void scene::addQuads(std::string l, unsigned short color, std::vector<Vec3d> vertices, bool inverted) void scene::addQuads(const std::string & l, unsigned short color, std::vector<Vec3d> & vertices, bool inverted)
{ {
dxfLayer* layer = _layerTable->findOrCreateLayer(l); dxfLayer* layer = _layerTable->findOrCreateLayer(l);
if (layer->getFrozen()) return; if (layer->getFrozen()) return;
@@ -170,7 +170,7 @@ void scene::addQuads(std::string l, unsigned short color, std::vector<Vec3d> ver
unsigned short unsigned short
scene::correctedColorIndex(std::string l, unsigned short color) scene::correctedColorIndex(const std::string & l, unsigned short color)
{ {
if (color >= aci::MIN && color <= aci::MAX) if (color >= aci::MIN && color <= aci::MAX)
{ {

View File

@@ -32,7 +32,7 @@ class dxfLayerTable;
class bounds { class bounds {
public: public:
bounds() : _min(DBL_MAX, DBL_MAX, DBL_MAX), _max(-DBL_MAX, -DBL_MAX, -DBL_MAX) {} bounds() : _min(DBL_MAX, DBL_MAX, DBL_MAX), _max(-DBL_MAX, -DBL_MAX, -DBL_MAX) {}
inline void expandBy(osg::Vec3d v) { inline void expandBy(const osg::Vec3d & v) {
if(v.x()<_min.x()) _min.x() = v.x(); if(v.x()<_min.x()) _min.x() = v.x();
if(v.x()>_max.x()) _max.x() = v.x(); if(v.x()>_max.x()) _max.x() = v.x();
@@ -57,7 +57,7 @@ public:
static inline static inline
osg::Geometry* createLnGeometry( osg::PrimitiveSet::Mode lineType, osg::Vec3Array* vertices, osg::Vec4 color) osg::Geometry* createLnGeometry( osg::PrimitiveSet::Mode lineType, osg::Vec3Array* vertices, const osg::Vec4 & color)
{ {
osg::Geometry* geom = new osg::Geometry; osg::Geometry* geom = new osg::Geometry;
geom->setVertexArray(vertices); geom->setVertexArray(vertices);
@@ -75,7 +75,7 @@ osg::Geometry* createLnGeometry( osg::PrimitiveSet::Mode lineType, osg::Vec3Arra
static inline static inline
osg::Geometry* createTriGeometry( osg::Vec3Array* vertices, osg::Vec3Array* normals, osg::Vec4 color) osg::Geometry* createTriGeometry( osg::Vec3Array* vertices, osg::Vec3Array* normals, const osg::Vec4 & color)
{ {
osg::Geometry* geom = new osg::Geometry; osg::Geometry* geom = new osg::Geometry;
geom->setVertexArray(vertices); geom->setVertexArray(vertices);
@@ -90,7 +90,7 @@ osg::Geometry* createTriGeometry( osg::Vec3Array* vertices, osg::Vec3Array* norm
} }
static inline static inline
osg::Geometry* createQuadGeometry( osg::Vec3Array* vertices, osg::Vec3Array* normals, osg::Vec4 color) osg::Geometry* createQuadGeometry( osg::Vec3Array* vertices, osg::Vec3Array* normals, const osg::Vec4 & color)
{ {
osg::Geometry* geom = new osg::Geometry; osg::Geometry* geom = new osg::Geometry;
geom->setVertexArray(vertices); geom->setVertexArray(vertices);
@@ -103,8 +103,9 @@ osg::Geometry* createQuadGeometry( osg::Vec3Array* vertices, osg::Vec3Array* nor
geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
return geom; return geom;
} }
static inline static inline
osg::Geode* createModel(std::string name, osg::Geometry* geom) osg::Geode* createModel(const std::string & name, osg::Geometry* geom)
{ {
osg::Geode* geom_geode = new osg::Geode; osg::Geode* geom_geode = new osg::Geode;
geom_geode->addDrawable(geom); geom_geode->addDrawable(geom);
@@ -275,7 +276,7 @@ public:
osg::Vec3d addVertex(osg::Vec3d v); osg::Vec3d addVertex(osg::Vec3d v);
osg::Vec3d addNormal(osg::Vec3d v); osg::Vec3d addNormal(osg::Vec3d v);
sceneLayer* findOrCreateSceneLayer(std::string l) sceneLayer* findOrCreateSceneLayer(const std::string & l)
{ {
sceneLayer* ly = _layers[l].get(); sceneLayer* ly = _layers[l].get();
if (!ly) { if (!ly) {
@@ -284,13 +285,13 @@ public:
} }
return ly; return ly;
} }
unsigned short correctedColorIndex(std::string l, unsigned short color); unsigned short correctedColorIndex(const std::string & l, unsigned short color);
void addLine(std::string l, unsigned short color, osg::Vec3d s, osg::Vec3d e); void addLine(const std::string & l, unsigned short color, osg::Vec3d & s, osg::Vec3d & e);
void addLineStrip(std::string l, unsigned short color, std::vector<osg::Vec3d> vertices); void addLineStrip(const std::string & l, unsigned short color, std::vector<osg::Vec3d> & vertices);
void addLineLoop(std::string l, unsigned short color, std::vector<osg::Vec3d> vertices); void addLineLoop(const std::string & l, unsigned short color, std::vector<osg::Vec3d> & vertices);
void addTriangles(std::string l, unsigned short color, std::vector<osg::Vec3d> vertices, bool inverted=false); void addTriangles(const std::string & l, unsigned short color, std::vector<osg::Vec3d> & vertices, bool inverted=false);
void addQuads(std::string l, unsigned short color, std::vector<osg::Vec3d> vertices, bool inverted=false); void addQuads(const std::string & l, unsigned short color, std::vector<osg::Vec3d> & vertices, bool inverted=false);
osg::Group* scene2osg() osg::Group* scene2osg()
{ {
osg::Group* root = NULL; osg::Group* root = NULL;
@@ -324,12 +325,11 @@ public:
protected: protected:
osg::Matrixd _m; osg::Matrixd _m;
osg::Matrixd _r; osg::Matrixd _r;
osg::Vec3d _t; osg::Vec3d _t;
bounds _b; bounds _b;
std::map<std::string, osg::ref_ptr<sceneLayer> > _layers; std::map<std::string, osg::ref_ptr<sceneLayer> > _layers;
std::vector<osg::Matrixd> _mStack; std::vector<osg::Matrixd> _mStack;
dxfLayerTable* _layerTable; dxfLayerTable* _layerTable;
}; };
#endif #endif