void OSG::loadXYZ(string path, VRTransformPtr res) { cout << "load xyz pointcloud " << path << endl; res->setName(path); try { VRGeoData data; vector<float> vertex = vector<float>(6); int i=0; ifstream file(path); while (file >> vertex[i]) { i++; if (i >= 6) { i = 0; data.pushVert(Pnt3d(vertex[0], vertex[1], vertex[2]), Vec3d(0,1,0), Color3f(vertex[3]/255.0, vertex[4]/255.0, vertex[5]/255.0)); data.pushPoint(); } } if (data.size()) { cout << " assemble geometry.. " << endl; auto geo = data.asGeometry("points"); res->addChild(geo); } } catch (std::exception& ex) { cerr << "Got an std::exception, what=" << ex.what() << endl; return; } catch (...) { cerr << "Got an unknown exception" << endl; return; } }
void OSG::loadE57(string path, VRTransformPtr res) { cout << "load e57 pointcloud " << path << endl; res->setName(path); try { ImageFile imf(path, "r"); // Read file from disk StructureNode root = imf.root(); if (!root.isDefined("/data3D")) { cout << "File doesn't contain 3D images" << endl; return; } e57::Node n = root.get("/data3D"); if (n.type() != E57_VECTOR) { cout << "bad file" << endl; return; } VectorNode data3D(n); int64_t scanCount = data3D.childCount(); // number of scans in file cout << " file read succefully, it contains " << scanCount << " scans" << endl; for (int i = 0; i < scanCount; i++) { StructureNode scan(data3D.get(i)); string sname = scan.pathName(); CompressedVectorNode points( scan.get("points") ); string pname = points.pathName(); auto cN = points.childCount(); cout << " scan " << i << " contains " << cN << " points\n"; StructureNode proto(points.prototype()); bool hasPos = (proto.isDefined("cartesianX") && proto.isDefined("cartesianY") && proto.isDefined("cartesianZ")); bool hasCol = (proto.isDefined("colorRed") && proto.isDefined("colorGreen") && proto.isDefined("colorBlue")); if (!hasPos) continue; if (hasCol) cout << " scan has colors\n"; else cout << " scan has no colors\n"; for (int i=0; i<proto.childCount(); i++) { auto child = proto.get(i); cout << " scan data: " << child.pathName() << endl; } vector<SourceDestBuffer> destBuffers; const int N = 4; double x[N]; destBuffers.push_back(SourceDestBuffer(imf, "cartesianX", x, N, true)); double y[N]; destBuffers.push_back(SourceDestBuffer(imf, "cartesianY", y, N, true)); double z[N]; destBuffers.push_back(SourceDestBuffer(imf, "cartesianZ", z, N, true)); double r[N]; double g[N]; double b[N]; if (hasCol) { destBuffers.push_back(SourceDestBuffer(imf, "colorRed", r, N, true)); destBuffers.push_back(SourceDestBuffer(imf, "colorGreen", g, N, true)); destBuffers.push_back(SourceDestBuffer(imf, "colorBlue", b, N, true)); } int Nchunk = 1e6; // separate in chunks because of tcmalloc large alloc issues VRGeoData data; unsigned int gotCount = 0; CompressedVectorReader reader = points.reader(destBuffers); do { if (data.size() > Nchunk) { cout << " assemble geometry.. " << endl; auto geo = data.asGeometry(pname); res->addChild(geo); data = VRGeoData(); } gotCount = reader.read(); for (unsigned j=0; j < gotCount; j++) { int v; if (hasCol) v = data.pushVert(Pnt3d(x[j], y[j], z[j]), Vec3d(0,1,0), Color3f(r[j]/255.0, g[j]/255.0, b[j]/255.0)); else v = data.pushVert(Pnt3d(x[j], y[j], z[j]), Vec3d(0,1,0)); data.pushPoint(v); } } while(gotCount); reader.close(); if (data.size()) { cout << " assemble geometry.. " << endl; auto geo = data.asGeometry(pname); res->addChild(geo); } } imf.close(); } catch (E57Exception& ex) { ex.report(__FILE__, __LINE__, __FUNCTION__); return; } catch (std::exception& ex) { cerr << "Got an std::exception, what=" << ex.what() << endl; return; } catch (...) { cerr << "Got an unknown exception" << endl; return; } }
bool VRFactory::loadVRML(string path, VRProgressPtr progress, VRTransformPtr res, bool thread) { // wrl filepath ifstream file(path); if (!file.is_open()) { cout << "file " << path << " not found" << endl; return true; } // get file size file.seekg(0, ios_base::end); size_t fileSize = file.tellg(); file.seekg(0, ios_base::beg); if (progress == 0) progress = VRProgress::create(); progress->setup("load VRML " + path, fileSize); progress->reset(); int state = 0; map<int, string> states; states[0] = "Transform "; // 0 states[1] = "diffuseColor "; // 6 states[2] = "coord "; // 21 +2 states[3] = "normal "; // x +2 states[4] = "coordIndex "; // x +1 states[5] = "colorIndex "; // x +1 states[6] = "normalIndex "; // x +1 Vec3f color; Vec3f last_col(-1,-1,-1); Geo geo; Pnt3f v; Vec3f n; int i; //vector<VRGeometryPtr> geos; vector<Geo> geos; map<Vec3f, VRMaterialPtr> mats; bool new_obj = true; bool new_color = true; int li = 0; string line; while ( getline(file, line) ) { progress->update( line.size() ); li++; for (auto d : states) { //if ( line[d.second.size()-1] != ' ') continue; // optimization if ( line.compare(0, d.second.size(), d.second) == 0) { //if (state != d.first) cout << "got on line " << li << ": " << states[d.first] << " instead of: " << states[state] << endl; switch (d.first) { case 0: break; case 1: new_obj = true; if (line.size() > 12) color = toVec3f( line.substr(12) ); if (mats.count(color) == 0) { mats[color] = VRMaterial::create("fmat"); mats[color]->setDiffuse(color); } if (color != last_col) { new_color = true; last_col = color; } break; case 2: geo.updateN(); break; case 3: break; case 4: break; case 5: break; } state = d.first+1; if (state == 7) state = 0; break; } } if (line[0] != ' ') continue; if (state == 6) continue; // skip color indices stringstream ss(line); switch (state) { case 3: while(ss >> v[0] && ss >> v[1] && ss >> v[2] && ss.get()) { if (!new_color && new_obj) new_obj = !geo.inBB(v); // strange artifacts!! geo.updateBB(v); if (new_obj) { new_obj = false; new_color = false; geo.init(geos, mats[color], path, thread); } geo.pos->addValue(v); } break; case 4: while(ss >> n[0] && ss >> n[1] && ss >> n[2] && ss.get()) geo.norms->addValue( n ); break; case 5: while(ss >> i && ss.get()) if (i >= 0) geo.inds_p->addValue( geo.Np + i ); break; case 0: while(ss >> i && ss.get()) if (i >= 0) geo.inds_n->addValue( geo.Nn + i ); break; } } file.close(); cout << "\nloaded " << geos.size() << " geometries" << endl; if (geos.size() == 0) { progress->reset(); return false; } res->setName("factory"); res->setPersistency(0); for (auto g : geos) { //Vec3f d = g.vmax - g.vmin; //if (d.length() < 0.1) continue; // skip very small objects if (g.inds_n->size() != g.inds_p->size()) { // not happening cout << " wrong indices lengths: " << g.inds_p->size() << " " << g.inds_n->size() << endl; continue; } if (g.inds_p->size() == 0) { // not happening cout << " empty geo: " << g.inds_p->size() << " " << g.inds_n->size() << endl; continue; } res->addChild(g.geo); GeoUInt32PropertyRecPtr Length = GeoUInt32Property::create(); Length->addValue(g.geo->getMesh()->getIndices()->size()); g.geo->setLengths(Length); } cout << "\nloaded2 " << res->getChildrenCount() << " geometries" << endl; progress->finish(); return true; }