| [1975] | 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/Geode> |
|---|
| 3 | #include <osg/Geometry> |
|---|
| 4 | |
|---|
| 5 | #include <osgDB/FileNameUtils> |
|---|
| [2501] | 6 | #include <osgDB/FileUtils> |
|---|
| [9124] | 7 | #include <osgDB/fstream> |
|---|
| [1975] | 8 | #include <osgDB/Registry> |
|---|
| 9 | |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #include <stdio.h> |
|---|
| [9343] | 12 | #include <string.h> |
|---|
| [1975] | 13 | |
|---|
| 14 | |
|---|
| 15 | using namespace osg; |
|---|
| 16 | |
|---|
| 17 | class ReaderWriter3DC : public osgDB::ReaderWriter |
|---|
| 18 | { |
|---|
| 19 | public: |
|---|
| [8578] | 20 | |
|---|
| 21 | ReaderWriter3DC() |
|---|
| 22 | { |
|---|
| 23 | supportsExtension("3dc","3DC point cloud format"); |
|---|
| 24 | supportsExtension("asc","3DC point cloud format"); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| [3539] | 27 | virtual const char* className() const { return "3DC point cloud reader"; } |
|---|
| [1975] | 28 | |
|---|
| [3694] | 29 | virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| [1975] | 30 | { |
|---|
| [2501] | 31 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| [1975] | 32 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| [2501] | 33 | |
|---|
| [3691] | 34 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| [2501] | 35 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| [1975] | 36 | |
|---|
| [8300] | 37 | osg::notify(osg::INFO) << "Reading file "<<fileName<<std::endl; |
|---|
| [1975] | 38 | |
|---|
| 39 | const int LINE_SIZE = 1024; |
|---|
| 40 | char line[LINE_SIZE]; |
|---|
| 41 | |
|---|
| [9124] | 42 | osgDB::ifstream fin(fileName.c_str()); |
|---|
| [1975] | 43 | |
|---|
| 44 | unsigned int num = 0; |
|---|
| 45 | while (fin) |
|---|
| 46 | { |
|---|
| 47 | fin.getline(line,LINE_SIZE); |
|---|
| 48 | if (line[0]=='#') |
|---|
| 49 | { |
|---|
| 50 | |
|---|
| [2772] | 51 | osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl; |
|---|
| [1975] | 52 | } |
|---|
| 53 | else |
|---|
| 54 | { |
|---|
| 55 | ++num; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| [8300] | 60 | osg::notify(osg::INFO) << "num="<<num<<std::endl; |
|---|
| [2001] | 61 | |
|---|
| 62 | unsigned int targetNumVertices = 10000; |
|---|
| 63 | |
|---|
| [1975] | 64 | |
|---|
| [2001] | 65 | osg::Geode* geode = new osg::Geode; |
|---|
| 66 | |
|---|
| [1975] | 67 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 68 | |
|---|
| 69 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 70 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| [4390] | 71 | osg::Vec4ubArray* colours = new osg::Vec4ubArray; |
|---|
| [1975] | 72 | |
|---|
| [2001] | 73 | vertices->reserve(targetNumVertices); |
|---|
| 74 | normals->reserve(targetNumVertices); |
|---|
| 75 | colours->reserve(targetNumVertices); |
|---|
| [1975] | 76 | |
|---|
| 77 | fin.close(); |
|---|
| 78 | |
|---|
| [9124] | 79 | osgDB::ifstream fin2(fileName.c_str()); |
|---|
| [1975] | 80 | while (fin2) |
|---|
| 81 | { |
|---|
| 82 | fin2.getline(line,LINE_SIZE); |
|---|
| 83 | if (line[0]=='#') |
|---|
| 84 | { |
|---|
| 85 | |
|---|
| [8300] | 86 | osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl; |
|---|
| [1975] | 87 | } |
|---|
| 88 | else if (strlen(line)>0) |
|---|
| 89 | { |
|---|
| 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) |
|---|
| [2001] | 102 | { |
|---|
| 103 | |
|---|
| 104 | if (vertices->size()>=targetNumVertices) |
|---|
| 105 | { |
|---|
| 106 | |
|---|
| [2057] | 107 | geometry->setUseDisplayList(true); |
|---|
| 108 | geometry->setUseVertexBufferObjects(true); |
|---|
| [2001] | 109 | geometry->setVertexArray(vertices); |
|---|
| 110 | geometry->setNormalArray(normals); |
|---|
| 111 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 112 | geometry->setColorArray(colours); |
|---|
| 113 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 114 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,vertices->size())); |
|---|
| 115 | |
|---|
| 116 | geode->addDrawable(geometry); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | geometry = new osg::Geometry; |
|---|
| 120 | |
|---|
| 121 | vertices = new osg::Vec3Array; |
|---|
| 122 | normals = new osg::Vec3Array; |
|---|
| [4390] | 123 | colours = new osg::Vec4ubArray; |
|---|
| [2001] | 124 | |
|---|
| 125 | vertices->reserve(targetNumVertices); |
|---|
| 126 | normals->reserve(targetNumVertices); |
|---|
| 127 | colours->reserve(targetNumVertices); |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| [1975] | 131 | vertices->push_back(pos); |
|---|
| 132 | normals->push_back(normal); |
|---|
| [4390] | 133 | colours->push_back(osg::Vec4ub(r,g,b,255)); |
|---|
| [1975] | 134 | |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| [1983] | 139 | |
|---|
| [2001] | 140 | |
|---|
| [2057] | 141 | geometry->setUseDisplayList(true); |
|---|
| 142 | geometry->setUseVertexBufferObjects(true); |
|---|
| [1975] | 143 | geometry->setVertexArray(vertices); |
|---|
| 144 | geometry->setNormalArray(normals); |
|---|
| 145 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 146 | geometry->setColorArray(colours); |
|---|
| 147 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 148 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,vertices->size())); |
|---|
| 149 | |
|---|
| 150 | geode->addDrawable(geometry); |
|---|
| 151 | |
|---|
| 152 | return geode; |
|---|
| 153 | |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| [7076] | 160 | REGISTER_OSGPLUGIN(3dc, ReaderWriter3DC) |
|---|