From d70cf68ae61e7c20e3cd00a595c15df3bc488e37 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 5 Dec 2006 15:31:07 +0000 Subject: [PATCH] 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." --- src/osgPlugins/dxf/dxfBlock.h | 21 ++++---- src/osgPlugins/dxf/dxfEntity.cpp | 18 +++---- src/osgPlugins/dxf/dxfEntity.h | 20 ++++---- src/osgPlugins/dxf/dxfSection.cpp | 2 +- src/osgPlugins/dxf/dxfSection.h | 82 +++++++++++++++---------------- src/osgPlugins/dxf/scene.cpp | 12 ++--- src/osgPlugins/dxf/scene.h | 34 ++++++------- 7 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/osgPlugins/dxf/dxfBlock.h b/src/osgPlugins/dxf/dxfBlock.h index 820b2ab29..8d568f8db 100644 --- a/src/osgPlugins/dxf/dxfBlock.h +++ b/src/osgPlugins/dxf/dxfBlock.h @@ -19,6 +19,7 @@ #include #include +#include class dxfFile; class codeValue; @@ -27,17 +28,17 @@ class dxfEntity; class dxfBlock : public osg::Referenced { public: - dxfBlock() : _currentEntity(NULL) {} - virtual ~dxfBlock() {} - inline const std::string& getName() const { return _name; } - virtual void assign(dxfFile* dxf, codeValue& cv); - std::vector getEntityList() { return _entityList; } - const osg::Vec3d& getPosition() const; + dxfBlock() : _currentEntity(NULL) {} + virtual ~dxfBlock() {} + inline const std::string& getName() const { return _name; } + virtual void assign(dxfFile* dxf, codeValue& cv); + std::vector > & getEntityList() { return _entityList; } + const osg::Vec3d& getPosition() const; protected: - std::vector _entityList; - dxfEntity* _currentEntity; - std::string _name; - osg::Vec3d _position; + std::vector > _entityList; + dxfEntity* _currentEntity; + std::string _name; + osg::Vec3d _position; }; #endif diff --git a/src/osgPlugins/dxf/dxfEntity.cpp b/src/osgPlugins/dxf/dxfEntity.cpp index 959eb249f..7881cf570 100644 --- a/src/osgPlugins/dxf/dxfEntity.cpp +++ b/src/osgPlugins/dxf/dxfEntity.cpp @@ -418,8 +418,8 @@ dxfPolyline::drawScene(scene* sc) Vec3d nr; bool nset = false; //dxfVertex* v = NULL; - unsigned short ncount; - unsigned short mcount; + unsigned int ncount; + unsigned int mcount; if (_surfacetype == 6) { // I dont have examples of type 5 and 8, but they may be the same as 6 mcount = _mdensity; @@ -428,8 +428,8 @@ dxfPolyline::drawScene(scene* sc) mcount = _mcount; ncount = _ncount; } - for (unsigned short n = 0; n < ncount-1; n++) { - for (unsigned short m = 1; m < mcount; m++) { + for (unsigned int n = 0; n < ncount-1; n++) { + for (unsigned int m = 1; m < mcount; m++) { // 0 a = _vertices[(m-1)*ncount+n].get()->getVertex(); // 1 @@ -463,7 +463,7 @@ dxfPolyline::drawScene(scene* sc) } } if (_flag & 1) { - for (unsigned short n = 0; n < ncount-1; n++) { + for (unsigned int n = 0; n < ncount-1; n++) { // 0 a = _vertices[(mcount-1)*ncount+n].get()->getVertex(); // 1 @@ -492,7 +492,7 @@ dxfPolyline::drawScene(scene* sc) } } if (_flag & 32) { - for (unsigned short m = 1; m < mcount; m++) { + for (unsigned int m = 1; m < mcount; m++) { // 0 a = _vertices[(m-1)*ncount+(ncount-1)].get()->getVertex(); // 1 @@ -553,7 +553,7 @@ dxfPolyline::drawScene(scene* sc) } else if (_flag & 64) { 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(); if (!vindice) continue; if (vindice->getIndice4()) { @@ -751,8 +751,8 @@ dxfInsert::drawScene(scene* sc) sc->pushMatrix(m); sc->pushMatrix(back); - std::vector l = _block->getEntityList(); - for (std::vector::iterator itr = l.begin(); itr != l.end(); ++itr) { + std::vector > l = _block->getEntityList(); + for (std::vector >::iterator itr = l.begin(); itr != l.end(); ++itr) { dxfBasicEntity* e = (*itr)->getEntity(); if (e) { e->drawScene(sc); diff --git a/src/osgPlugins/dxf/dxfEntity.h b/src/osgPlugins/dxf/dxfEntity.h index 95787f5ca..b6591c0f1 100644 --- a/src/osgPlugins/dxf/dxfEntity.h +++ b/src/osgPlugins/dxf/dxfEntity.h @@ -147,14 +147,14 @@ public: virtual void assign(dxfFile* dxf, codeValue& cv); 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 unsigned short getIndice1() const { return _indice1; } - const unsigned short getIndice2() const { return _indice2; } - const unsigned short getIndice3() const { return _indice3; } - const unsigned short getIndice4() const { return _indice4; } + const unsigned int getIndice1() const { return _indice1; } + const unsigned int getIndice2() const { return _indice2; } + const unsigned int getIndice3() const { return _indice3; } + const unsigned int getIndice4() const { return _indice4; } protected: osg::Vec3d _vertex; - unsigned short _indice1, _indice2, _indice3, _indice4; + unsigned int _indice1, _indice2, _indice3, _indice4; }; class dxfPolyline : public dxfBasicEntity @@ -185,11 +185,11 @@ protected: std::vector > _indices; double _elevation; unsigned short _flag; - unsigned short _mcount; - unsigned short _ncount; + unsigned int _mcount; + unsigned int _ncount; unsigned short _nstart; // 71 unsigned short _nend; //72 - osg::Vec3d _ocs; //210 220 230 + osg::Vec3d _ocs; //210 220 230 unsigned short _mdensity; // 73 unsigned short _ndensity; // 74 unsigned short _surfacetype; //75 @@ -246,8 +246,8 @@ protected: // entities (dxf garble things) in the sequence double _rotation; osg::Vec3d _scale; - osg::Vec3d _point; - osg::Vec3d _ocs; + osg::Vec3d _point; + osg::Vec3d _ocs; }; class dxfEntity : public osg::Referenced diff --git a/src/osgPlugins/dxf/dxfSection.cpp b/src/osgPlugins/dxf/dxfSection.cpp index 1e2bc8670..92bd682b9 100644 --- a/src/osgPlugins/dxf/dxfSection.cpp +++ b/src/osgPlugins/dxf/dxfSection.cpp @@ -78,7 +78,7 @@ void dxfEntities::assign(dxfFile* dxf, codeValue& cv) void dxfEntities::drawScene(scene* sc) { - for (std::vector::iterator itr = _entityList.begin(); + for (std::vector >::iterator itr = _entityList.begin(); itr != _entityList.end(); ++itr) (*itr)->drawScene(sc); } diff --git a/src/osgPlugins/dxf/dxfSection.h b/src/osgPlugins/dxf/dxfSection.h index 276c67092..15a52dfcb 100644 --- a/src/osgPlugins/dxf/dxfSection.h +++ b/src/osgPlugins/dxf/dxfSection.h @@ -11,10 +11,10 @@ */ /** - Classes used to parse each section of a DXF file. Not all - types of section has been defined here, just the ones - I found of interest, ie HEADER, TABLES, BLOCKS, and ENTITIES. - Yet to be implemented: CLASSES, OBJECTS, and THUMBNAILIMAGE. + Classes used to parse each section of a DXF file. Not all + types of section has been defined here, just the ones + I found of interest, ie HEADER, TABLES, BLOCKS, and ENTITIES. + Yet to be implemented: CLASSES, OBJECTS, and THUMBNAILIMAGE. */ #ifndef DXF_SECTION #define DXF_SECTION 1 @@ -24,6 +24,8 @@ #include "dxfTable.h" #include "codeValue.h" #include "scene.h" +#include "dxfEntity.h" +#include "dxfBlock.h" #include #include @@ -31,77 +33,75 @@ #include class dxfFile; -class dxfEntity; -class dxfBlock; class dxfSection : public dxfSectionBase { public: - dxfSection() {} - virtual ~dxfSection() {} + dxfSection() {} + virtual ~dxfSection() {} }; class dxfHeader : public dxfSection { public: - dxfHeader() : _inVariable(false) {} - virtual ~dxfHeader() {} - virtual void assign(dxfFile* dxf, codeValue& cv); - VariableList& getVariable(std::string inVar) { return _variables[inVar]; } + dxfHeader() : _inVariable(false) {} + virtual ~dxfHeader() {} + virtual void assign(dxfFile* dxf, codeValue& cv); + VariableList& getVariable(std::string inVar) { return _variables[inVar]; } protected: - std::map _variables; - bool _inVariable; - std::string _currentVariable; + std::map _variables; + bool _inVariable; + std::string _currentVariable; }; class dxfTables : public dxfSection { public: - dxfTables() : _inLayerTable(false) {} - virtual ~dxfTables() {} - virtual void assign(dxfFile* dxf, codeValue& cv); - dxfLayerTable* getOrCreateLayerTable() - { - if (!_layerTable.get()) - _layerTable = new dxfLayerTable; - return _layerTable.get(); - } + dxfTables() : _inLayerTable(false) {} + virtual ~dxfTables() {} + virtual void assign(dxfFile* dxf, codeValue& cv); + dxfLayerTable* getOrCreateLayerTable() + { + if (!_layerTable.get()) + _layerTable = new dxfLayerTable; + return _layerTable.get(); + } protected: - bool _inLayerTable; - osg::ref_ptr _layerTable; - std::vector > _others; - osg::ref_ptr _currentTable; + bool _inLayerTable; + osg::ref_ptr _layerTable; + std::vector > _others; + osg::ref_ptr _currentTable; }; class dxfEntities : public dxfSection { public: - dxfEntities() : _currentEntity(NULL) {} - virtual ~dxfEntities() {} - virtual void assign(dxfFile* dxf, codeValue& cv); - virtual void drawScene(scene* sc); + dxfEntities() : _currentEntity(NULL) {} + virtual ~dxfEntities() {} + virtual void assign(dxfFile* dxf, codeValue& cv); + virtual void drawScene(scene* sc); protected: - dxfEntity* _currentEntity; - std::vector _entityList; + dxfEntity* _currentEntity; + std::vector > _entityList; }; class dxfBlocks : public dxfSection { public: - dxfBlocks() : _currentBlock(NULL) {} - virtual ~dxfBlocks() {} - virtual void assign(dxfFile* dxf, codeValue& cv); - dxfBlock* findBlock(std::string s); + dxfBlocks() : _currentBlock(NULL) {} + virtual ~dxfBlocks() {} + virtual void assign(dxfFile* dxf, codeValue& cv); + dxfBlock* findBlock(std::string s); protected: - dxfBlock* _currentBlock; - std::map _blockNameList; - std::vector _blockList; + dxfBlock* _currentBlock; + std::map _blockNameList; + std::vector > _blockList; }; #endif diff --git a/src/osgPlugins/dxf/scene.cpp b/src/osgPlugins/dxf/scene.cpp index 064b1c30d..6075e9617 100644 --- a/src/osgPlugins/dxf/scene.cpp +++ b/src/osgPlugins/dxf/scene.cpp @@ -52,7 +52,7 @@ Vec3d scene::addNormal(Vec3d v) } 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); 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(b); } -void scene::addLineStrip(std::string l, unsigned short color, std::vector vertices) +void scene::addLineStrip(const std::string & l, unsigned short color, std::vector & vertices) { dxfLayer* layer = _layerTable->findOrCreateLayer(l); if (layer->getFrozen()) return; @@ -73,7 +73,7 @@ void scene::addLineStrip(std::string l, unsigned short color, std::vector } ly->_linestrips[correctedColorIndex(l, color)].push_back(converted); } -void scene::addLineLoop(std::string l, unsigned short color, std::vector vertices) +void scene::addLineLoop(const std::string & l, unsigned short color, std::vector & vertices) { dxfLayer* layer = _layerTable->findOrCreateLayer(l); if (layer->getFrozen()) return; @@ -88,7 +88,7 @@ void scene::addLineLoop(std::string l, unsigned short color, std::vector } -void scene::addTriangles(std::string l, unsigned short color, std::vector vertices, bool inverted) +void scene::addTriangles(const std::string & l, unsigned short color, std::vector & vertices, bool inverted) { dxfLayer* layer = _layerTable->findOrCreateLayer(l); if (layer->getFrozen()) return; @@ -119,7 +119,7 @@ void scene::addTriangles(std::string l, unsigned short color, std::vector } } } -void scene::addQuads(std::string l, unsigned short color, std::vector vertices, bool inverted) +void scene::addQuads(const std::string & l, unsigned short color, std::vector & vertices, bool inverted) { dxfLayer* layer = _layerTable->findOrCreateLayer(l); if (layer->getFrozen()) return; @@ -170,7 +170,7 @@ void scene::addQuads(std::string l, unsigned short color, std::vector ver 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) { diff --git a/src/osgPlugins/dxf/scene.h b/src/osgPlugins/dxf/scene.h index 188b648be..d6370daf2 100644 --- a/src/osgPlugins/dxf/scene.h +++ b/src/osgPlugins/dxf/scene.h @@ -32,7 +32,7 @@ class dxfLayerTable; class bounds { public: 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()>_max.x()) _max.x() = v.x(); @@ -57,7 +57,7 @@ public: 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; geom->setVertexArray(vertices); @@ -75,7 +75,7 @@ osg::Geometry* createLnGeometry( osg::PrimitiveSet::Mode lineType, osg::Vec3Arra 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; geom->setVertexArray(vertices); @@ -90,7 +90,7 @@ osg::Geometry* createTriGeometry( osg::Vec3Array* vertices, osg::Vec3Array* norm } 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; geom->setVertexArray(vertices); @@ -103,8 +103,9 @@ osg::Geometry* createQuadGeometry( osg::Vec3Array* vertices, osg::Vec3Array* nor geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); return geom; } + 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; geom_geode->addDrawable(geom); @@ -275,7 +276,7 @@ public: osg::Vec3d addVertex(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(); if (!ly) { @@ -284,13 +285,13 @@ public: } 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 addLineStrip(std::string l, unsigned short color, std::vector vertices); - void addLineLoop(std::string l, unsigned short color, std::vector vertices); - void addTriangles(std::string l, unsigned short color, std::vector vertices, bool inverted=false); - void addQuads(std::string l, unsigned short color, std::vector vertices, bool inverted=false); + void addLine(const std::string & l, unsigned short color, osg::Vec3d & s, osg::Vec3d & e); + void addLineStrip(const std::string & l, unsigned short color, std::vector & vertices); + void addLineLoop(const std::string & l, unsigned short color, std::vector & vertices); + void addTriangles(const std::string & l, unsigned short color, std::vector & vertices, bool inverted=false); + void addQuads(const std::string & l, unsigned short color, std::vector & vertices, bool inverted=false); osg::Group* scene2osg() { osg::Group* root = NULL; @@ -324,12 +325,11 @@ public: protected: osg::Matrixd _m; osg::Matrixd _r; - osg::Vec3d _t; - bounds _b; + osg::Vec3d _t; + bounds _b; std::map > _layers; - std::vector _mStack; - dxfLayerTable* _layerTable; - + std::vector _mStack; + dxfLayerTable* _layerTable; }; #endif