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:
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <osg/Referenced>
|
||||
#include <osg/Vec3d>
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
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<dxfEntity*> 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<osg::ref_ptr<dxfEntity> > & getEntityList() { return _entityList; }
|
||||
const osg::Vec3d& getPosition() const;
|
||||
protected:
|
||||
std::vector<dxfEntity*> _entityList;
|
||||
dxfEntity* _currentEntity;
|
||||
std::string _name;
|
||||
osg::Vec3d _position;
|
||||
std::vector<osg::ref_ptr<dxfEntity> > _entityList;
|
||||
dxfEntity* _currentEntity;
|
||||
std::string _name;
|
||||
osg::Vec3d _position;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<dxfEntity*> l = _block->getEntityList();
|
||||
for (std::vector<dxfEntity*>::iterator itr = l.begin(); itr != l.end(); ++itr) {
|
||||
std::vector<osg::ref_ptr<dxfEntity> > l = _block->getEntityList();
|
||||
for (std::vector<osg::ref_ptr<dxfEntity> >::iterator itr = l.begin(); itr != l.end(); ++itr) {
|
||||
dxfBasicEntity* e = (*itr)->getEntity();
|
||||
if (e) {
|
||||
e->drawScene(sc);
|
||||
|
||||
@@ -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<osg::ref_ptr<dxfVertex> > _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
|
||||
|
||||
@@ -78,7 +78,7 @@ void dxfEntities::assign(dxfFile* dxf, codeValue& cv)
|
||||
void
|
||||
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)->drawScene(sc);
|
||||
}
|
||||
|
||||
@@ -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 <map>
|
||||
#include <vector>
|
||||
@@ -31,77 +33,75 @@
|
||||
#include <string>
|
||||
|
||||
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<std::string, VariableList> _variables;
|
||||
bool _inVariable;
|
||||
std::string _currentVariable;
|
||||
std::map<std::string, VariableList> _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<dxfLayerTable> _layerTable;
|
||||
std::vector<osg::ref_ptr<dxfTable> > _others;
|
||||
osg::ref_ptr<dxfTable> _currentTable;
|
||||
bool _inLayerTable;
|
||||
osg::ref_ptr<dxfLayerTable> _layerTable;
|
||||
std::vector<osg::ref_ptr<dxfTable> > _others;
|
||||
osg::ref_ptr<dxfTable> _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<dxfEntity*> _entityList;
|
||||
dxfEntity* _currentEntity;
|
||||
std::vector<osg::ref_ptr<dxfEntity> > _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<std::string, dxfBlock*> _blockNameList;
|
||||
std::vector<dxfBlock*> _blockList;
|
||||
dxfBlock* _currentBlock;
|
||||
std::map<std::string, dxfBlock*> _blockNameList;
|
||||
std::vector<osg::ref_ptr<dxfBlock> > _blockList;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<Vec3d> vertices)
|
||||
void scene::addLineStrip(const std::string & l, unsigned short color, std::vector<Vec3d> & 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<Vec3d>
|
||||
}
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
if (layer->getFrozen()) return;
|
||||
@@ -170,7 +170,7 @@ void scene::addQuads(std::string l, unsigned short color, std::vector<Vec3d> 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)
|
||||
{
|
||||
|
||||
@@ -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<osg::Vec3d> vertices);
|
||||
void addLineLoop(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 addQuads(std::string l, unsigned short color, std::vector<osg::Vec3d> 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<osg::Vec3d> & vertices);
|
||||
void addLineLoop(const std::string & l, unsigned short color, std::vector<osg::Vec3d> & vertices);
|
||||
void addTriangles(const 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* 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<std::string, osg::ref_ptr<sceneLayer> > _layers;
|
||||
std::vector<osg::Matrixd> _mStack;
|
||||
dxfLayerTable* _layerTable;
|
||||
|
||||
std::vector<osg::Matrixd> _mStack;
|
||||
dxfLayerTable* _layerTable;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user