Changeset 11238 for OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/3dc/ReaderWriter3DC.cpp
- Timestamp:
- 03/17/10 15:33:47 (3 years ago)
- Location:
- OpenSceneGraph/branches/OpenSceneGraph-2.8
- Files:
-
- 3 modified
-
. (modified) (1 prop)
-
src/osgPlugins (modified) (1 prop)
-
src/osgPlugins/3dc/ReaderWriter3DC.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/branches/OpenSceneGraph-2.8
- Property svn:mergeinfo
-
old new 1 1 /OpenSceneGraph/branches/OpenSceneGraph-2.8.2:10664 2 /OpenSceneGraph/trunk:9769,9879-9880,9895,9932,10 208,10340,10417,10456,10487,10622-10623,10625,10671-10672,10697,10722,10753,10788,10818,10854-10855,10858,10887,10891,10923,10933,11019,11032,11034-11035,11111,11127,111312 /OpenSceneGraph/trunk:9769,9879-9880,9895,9932,10010,10208,10340,10417,10456,10487,10622-10623,10625,10671-10672,10697,10722,10753,10758,10788,10818,10854-10855,10858,10887,10891,10923,10933,11019,11032,11034-11035,11111,11127,11131,11175
-
- Property svn:mergeinfo
-
OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins
- Property svn:mergeinfo
-
old new 1 1 /OpenSceneGraph/branches/OpenSceneGraph-2.8.2/src/osgPlugins:10664 2 2 /OpenSceneGraph/trunk:10539 3 /OpenSceneGraph/trunk/src/osgPlugins:9769,9879-9880,9895,9932,10 208,10340,10417,10456,10487,10622-10623,10625,10671-10672,10697,10722,10753,10788,10818,10854-10855,10858,10887,10891,10923,10933,11019,11032,11034-11035,11111,11127,111313 /OpenSceneGraph/trunk/src/osgPlugins:9769,9879-9880,9895,9932,10010,10208,10340,10417,10456,10487,10622-10623,10625,10671-10672,10697,10722,10753,10758,10788,10818,10854-10855,10858,10887,10891,10923,10933,11019,11032,11034-11035,11111,11127,11131,11175
-
- Property svn:mergeinfo
-
OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/3dc/ReaderWriter3DC.cpp
r9343 r11238 12 12 #include <string.h> 13 13 14 15 using namespace osg; 14 class Writer3DCNodeVisitor: public osg::NodeVisitor { 15 16 public: 17 Writer3DCNodeVisitor(std::ostream& fout) : 18 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), 19 _fout(fout) 20 { 21 // _fout << "# file written by OpenSceneGraph" << std::endl << std::endl; 22 } 23 24 virtual void apply(osg::Geode &node); 25 26 protected: 27 28 Writer3DCNodeVisitor& operator = (const Writer3DCNodeVisitor&) { return *this; } 29 std::ostream& _fout; 30 31 }; 32 33 void Writer3DCNodeVisitor::apply( osg::Geode &node ) 34 { 35 osg::Matrix matrix = osg::computeLocalToWorld(getNodePath()); 36 37 unsigned int count = node.getNumDrawables(); 38 for ( unsigned int i = 0; i < count; i++ ) 39 { 40 osg::Geometry *geometry = node.getDrawable( i )->asGeometry(); 41 if ( geometry ) 42 { 43 osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); 44 osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geometry->getNormalArray()); 45 osg::Vec3Array* colours = dynamic_cast<osg::Vec3Array*>(geometry->getColorArray()); 46 47 if ( vertices ) { 48 for (unsigned int ii=0;ii<vertices->size();ii++) { 49 50 // update nodes with world coords 51 osg::Vec3d v = vertices->at(ii) * matrix; 52 _fout << v[0] << ' ' << v[1] << ' ' << v[2]; 53 54 if ( colours ) 55 { 56 v=colours->at(ii); 57 _fout << ' ' << (int)v[0]*255.0 << ' ' << (int)v[1]*255.0 << ' ' << (int)v[2]*255.0; 58 } 59 else 60 { 61 _fout << " 255 255 255"; 62 } 63 64 if ( normals ) 65 { 66 v = normals->at(ii); 67 _fout << ' ' << v[0] << ' ' << v[1] << ' ' << v[2]; 68 } 69 else 70 { 71 _fout << " 0.0 0.0 1.0"; 72 } 73 74 75 _fout << std::endl; 76 } 77 } 78 79 } 80 } 81 } 16 82 17 83 class ReaderWriter3DC : public osgDB::ReaderWriter 18 84 { 19 85 public: 20 86 21 87 ReaderWriter3DC() 22 88 { … … 24 90 supportsExtension("asc","3DC point cloud format"); 25 91 } 26 92 27 93 virtual const char* className() const { return "3DC point cloud reader"; } 28 94 29 95 virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const 30 96 { … … 34 100 std::string fileName = osgDB::findDataFile( file, options ); 35 101 if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 36 102 37 103 osg::notify(osg::INFO) << "Reading file "<<fileName<<std::endl; 38 104 39 105 const int LINE_SIZE = 1024; 40 106 char line[LINE_SIZE]; 41 107 108 unsigned int targetNumVertices = 10000; 109 110 osg::Geode* geode = new osg::Geode; 111 112 osg::Geometry* geometry = new osg::Geometry; 113 114 osg::Vec3Array* vertices = new osg::Vec3Array; 115 osg::Vec3Array* normals = new osg::Vec3Array; 116 osg::Vec4ubArray* colours = new osg::Vec4ubArray; 117 118 osg::Vec3 pos; 119 osg::Vec3 normal(0.0,0.0,1.0); 120 int r=255,g=255,b=255,a=255; 121 char sep; 122 42 123 osgDB::ifstream fin(fileName.c_str()); 43 44 unsigned int num = 0;45 124 while (fin) 46 125 { … … 51 130 osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl; 52 131 } 53 else54 {55 ++num;56 }57 }58 59 60 osg::notify(osg::INFO) << "num="<<num<<std::endl;61 62 unsigned int targetNumVertices = 10000;63 64 65 osg::Geode* geode = new osg::Geode;66 67 osg::Geometry* geometry = new osg::Geometry;68 69 osg::Vec3Array* vertices = new osg::Vec3Array;70 osg::Vec3Array* normals = new osg::Vec3Array;71 osg::Vec4ubArray* colours = new osg::Vec4ubArray;72 73 vertices->reserve(targetNumVertices);74 normals->reserve(targetNumVertices);75 colours->reserve(targetNumVertices);76 77 fin.close();78 79 osgDB::ifstream fin2(fileName.c_str());80 while (fin2)81 {82 fin2.getline(line,LINE_SIZE);83 if (line[0]=='#')84 {85 // comment line86 osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl;87 }88 132 else if (strlen(line)>0) 89 133 { 90 ++num; 91 92 osg::Vec3 pos,normal; 93 int r,g,b; 94 95 int a = sscanf(line,"%f %f %f %d %d %d %f %f %f", 96 &pos.x(),&pos.y(),&pos.z(), 97 &r,&g,&b, 98 &normal.x(),&normal.y(),&normal.z()); 99 100 101 if (a) 102 { 103 134 int matched = sscanf(line,"%f%c%f%c%f%c%d%c%d%c%d%c%f%c%f%c%f", 135 &pos.x(),&sep,&pos.y(),&sep,&pos.z(),&sep, 136 &r,&sep,&g,&sep,&b,&sep, 137 &normal.x(),&sep,&normal.y(),&sep,&normal.z()); 138 139 if (matched) 140 { 141 104 142 if (vertices->size()>=targetNumVertices) 105 143 { … … 113 151 geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 114 152 geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,vertices->size())); 115 153 116 154 geode->addDrawable(geometry); 117 155 118 // allocate a new geometry 156 // allocate a new geometry 119 157 geometry = new osg::Geometry; 120 158 … … 128 166 129 167 } 130 168 131 169 vertices->push_back(pos); 132 170 normals->push_back(normal); 133 colours->push_back(osg::Vec4ub(r,g,b,255)); 134 171 colours->push_back(osg::Vec4ub(r,g,b,a)); 135 172 } 136 173 } 137 138 174 } 139 175 … … 149 185 150 186 geode->addDrawable(geometry); 151 187 152 188 return geode; 153 154 } 155 189 190 } 191 192 virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options =NULL) const 193 { 194 if (!acceptsExtension(osgDB::getFileExtension(fileName))) 195 return WriteResult(WriteResult::FILE_NOT_HANDLED); 196 197 osgDB::ofstream f(fileName.c_str()); 198 199 Writer3DCNodeVisitor nv(f); 200 201 // we must cast away constness 202 (const_cast<osg::Node*>(&node))->accept(nv); 203 204 return WriteResult(WriteResult::FILE_SAVED); 205 } 156 206 }; 157 207
