| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/Geode> |
|---|
| 3 | #include <osg/Geometry> |
|---|
| 4 | |
|---|
| 5 | #include <osgDB/FileNameUtils> |
|---|
| 6 | #include <osgDB/FileUtils> |
|---|
| 7 | #include <osgDB/fstream> |
|---|
| 8 | #include <osgDB/Registry> |
|---|
| 9 | |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #include <stdio.h> |
|---|
| 12 | #include <string.h> |
|---|
| 13 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|
| 82 | |
|---|
| 83 | class ReaderWriter3DC : public osgDB::ReaderWriter |
|---|
| 84 | { |
|---|
| 85 | public: |
|---|
| 86 | |
|---|
| 87 | ReaderWriter3DC() |
|---|
| 88 | { |
|---|
| 89 | supportsExtension("3dc","3DC point cloud format"); |
|---|
| 90 | supportsExtension("asc","3DC point cloud format"); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | virtual const char* className() const { return "3DC point cloud reader"; } |
|---|
| 94 | |
|---|
| 95 | virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 96 | { |
|---|
| 97 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 98 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 99 | |
|---|
| 100 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 101 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 102 | |
|---|
| 103 | osg::notify(osg::INFO) << "Reading file "<<fileName<<std::endl; |
|---|
| 104 | |
|---|
| 105 | const int LINE_SIZE = 1024; |
|---|
| 106 | char line[LINE_SIZE]; |
|---|
| 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 | |
|---|
| 123 | osgDB::ifstream fin(fileName.c_str()); |
|---|
| 124 | while (fin) |
|---|
| 125 | { |
|---|
| 126 | fin.getline(line,LINE_SIZE); |
|---|
| 127 | if (line[0]=='#') |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl; |
|---|
| 131 | } |
|---|
| 132 | else if (strlen(line)>0) |
|---|
| 133 | { |
|---|
| 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 | |
|---|
| 142 | if (vertices->size()>=targetNumVertices) |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | geometry->setUseDisplayList(true); |
|---|
| 146 | geometry->setUseVertexBufferObjects(true); |
|---|
| 147 | geometry->setVertexArray(vertices); |
|---|
| 148 | geometry->setNormalArray(normals); |
|---|
| 149 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 150 | geometry->setColorArray(colours); |
|---|
| 151 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 152 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,vertices->size())); |
|---|
| 153 | |
|---|
| 154 | geode->addDrawable(geometry); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | geometry = new osg::Geometry; |
|---|
| 158 | |
|---|
| 159 | vertices = new osg::Vec3Array; |
|---|
| 160 | normals = new osg::Vec3Array; |
|---|
| 161 | colours = new osg::Vec4ubArray; |
|---|
| 162 | |
|---|
| 163 | vertices->reserve(targetNumVertices); |
|---|
| 164 | normals->reserve(targetNumVertices); |
|---|
| 165 | colours->reserve(targetNumVertices); |
|---|
| 166 | |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | vertices->push_back(pos); |
|---|
| 170 | normals->push_back(normal); |
|---|
| 171 | colours->push_back(osg::Vec4ub(r,g,b,a)); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | geometry->setUseDisplayList(true); |
|---|
| 178 | geometry->setUseVertexBufferObjects(true); |
|---|
| 179 | geometry->setVertexArray(vertices); |
|---|
| 180 | geometry->setNormalArray(normals); |
|---|
| 181 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 182 | geometry->setColorArray(colours); |
|---|
| 183 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 184 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,vertices->size())); |
|---|
| 185 | |
|---|
| 186 | geode->addDrawable(geometry); |
|---|
| 187 | |
|---|
| 188 | return geode; |
|---|
| 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 | |
|---|
| 202 | (const_cast<osg::Node*>(&node))->accept(nv); |
|---|
| 203 | |
|---|
| 204 | return WriteResult(WriteResult::FILE_SAVED); |
|---|
| 205 | } |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | REGISTER_OSGPLUGIN(3dc, ReaderWriter3DC) |
|---|