示例#1
0
void ModuleWalls::loadBbox(MapGrid::Box bbox) {
    auto mc = RealWorld::get()->getCoordinator();
    OSMMapPtr osmMap = RealWorld::get()->getMap(bbox.str);
    if (!osmMap) return;

    cout << "LOADING WALLS FOR " << bbox.str << "\n" << flush;
    VRGeoData geo;

    for (auto way : osmMap->getWays()) {
        for (WallMaterial* mat : wallList) {
            if (way.second->tags[mat->k] == mat->v) {
                Wall* wall = new Wall(way.second->id);
                for(string nID: way.second->nodes) {
                    auto node = osmMap->getNode(nID);
                    Vec2d pos = mc->realToWorld(Vec2d(node->lat, node->lon));
                    wall->positions.push_back(pos);
                }

                if (wall->positions.size() < 3) continue;
                addWall(wall, geo, mat->width, mat->height);
            }
        }
    }

    VRGeometryPtr geom = geo.asGeometry("Wall");
    geom->setMaterial(wallList[0]->material);
    root->addChild(geom);
    meshes[bbox.str] = geom;
}
示例#2
0
void VRGeometry::loadContent(xmlpp::Element* e) {
    VRTransform::loadContent(e);

    source.type = toInt(e->get_attribute("sourcetype")->get_value().c_str());
    source.parameter = e->get_attribute("sourceparam")->get_value();

    string p1, p2;
    stringstream ss;
    VRGeometryPtr g;
    // get source info
    // construct data from that

    switch(source.type) {
        case CODE:
            return;
        case SCRIPT:
            break;
        case FILE:
            ss << source.parameter;
            ss >> p1; ss >> p2;
            g = VRImport::get()->loadGeometry(p1, p2);
            if (g) setMesh( g->getMesh(), source, true );
            else cout << "failed to load " << p2 << " from file " << p1 << endl;
            break;
        case PRIMITIVE:
            ss << source.parameter;
            ss >> p1; getline(ss, p2);
            setPrimitive(p1, p2);
            break;
    }
}
示例#3
0
    //void init(vector<VRGeometryPtr>& geos, VRMaterialPtr mat) {
    void init(vector<Geo>& geos, VRMaterialPtr mat, string path, bool thread) {
        geo = VRGeometry::create("factory_part"); // init new object

        pos = GeoPnt3fProperty::create();
        norms = GeoVec3fProperty::create();
        inds_p = GeoUInt32Property::create();
        inds_n = GeoUInt32Property::create();

        geo->setType(GL_TRIANGLES);
        geo->setPositions( pos );
        geo->setNormals( norms );
        geo->setIndices( inds_p );
        geo->getMesh()->setIndex(inds_n, Geometry::NormalsIndex);
        geo->setMaterial(mat);

        VRGeometry::Reference ref(VRGeometry::FILE, path + " " + geo->getName() + " SOLIDWORKS-VRML2 " + toString(thread));
        geo->setReference( ref );

        geos.push_back(*this);

        vmin = Vec3f(1e9, 1e9, 1e9);
        vmax = Vec3f(-1e9, -1e9, -1e9);

        Np = Nn = 0;
        r = 0;
    }
示例#4
0
void CarDynamics::setWheelGeo(VRGeometryPtr geo) { // TODO
    w1 = static_pointer_cast<VRGeometry>( geo->duplicate() );
    w2 = static_pointer_cast<VRGeometry>( geo->duplicate() );
    w3 = static_pointer_cast<VRGeometry>( geo->duplicate() );
    w4 = static_pointer_cast<VRGeometry>( geo->duplicate() );
    w1->setPersistency(0);
    w2->setPersistency(0);
    w3->setPersistency(0);
    w4->setPersistency(0);

    root->addChild(w1);
    root->addChild(w2);
    root->addChild(w3);
    root->addChild(w4);

    cout << "\nset wheel geo " << geo->getName() << endl;
}
示例#5
0
VRObjectPtr VRGeometry::copy(vector<VRObjectPtr> children) {
    VRGeometryPtr geo = VRGeometry::create(getBaseName());
    geo->setMesh(mesh);
    geo->setMaterial(mat);
    geo->source = source;
    geo->setVisible(isVisible());
    geo->setPickable(isPickable());
    geo->setMatrix(getMatrix());
    return geo;
}
示例#6
0
void CarDynamics::setChassisGeo(VRGeometryPtr geo) {
    geo->setMatrix(Matrix());
    geo->getPhysics()->setShape("Convex");
    geo->getPhysics()->setMass(m_mass);
    geo->getPhysics()->setDynamic(true);
    geo->getPhysics()->setPhysicalized(true);
    geo->getPhysics()->updateTransformation(geo);

    if (geo->getPhysics()->getRigidBody() == 0) {
        cout<<"!!!!!!!!chassis is 0!!!!!!!!\ncreating vehicle with standard parameters && shapes"<<endl;
        initVehicle();
        return;
    }

    {
        PLock lock(mtx());
        m_carChassis = geo->getPhysics()->getRigidBody();
    }

    cout << "\nset chassis geo " << geo->getName() << endl;

    initVehicle();
    root->addChild(geo);
}
示例#7
0
void VRGeometry::showGeometricData(string type, bool b) {
    if (dataLayer.count(type)) dataLayer[type]->destroy();

    VRGeometryPtr geo = VRGeometry::create("DATALAYER_"+getName()+"_"+type, true);
    dataLayer[type] = geo;
    addChild(geo);

    GeoColor3fPropertyRecPtr cols = GeoColor3fProperty::create();
    GeoPnt3fPropertyRecPtr pos = GeoPnt3fProperty::create();
    GeoUInt32PropertyRecPtr inds = GeoUInt32Property::create();

    Pnt3f p;
    Vec3f n;

    if (type == "Normals") {
        GeoVectorPropertyRecPtr g_norms = mesh->getNormals();
        GeoVectorPropertyRecPtr g_pos = mesh->getPositions();
        for (uint i=0; i<g_norms->size(); i++) {
            p = g_pos->getValue<Pnt3f>(i);
            n = g_norms->getValue<Vec3f>(i);
            pos->addValue(p);
            pos->addValue(p+n*0.1);
            cols->addValue(Vec3f(1,1,1));
            cols->addValue(Vec3f(abs(n[0]),abs(n[1]),abs(n[2])));
            inds->addValue(2*i);
            inds->addValue(2*i+1);
        }

        geo->setPositions(pos);
        geo->setType(GL_LINE);
        geo->setColors(cols);
        geo->setIndices(inds);
    }

    VRMaterialPtr m = VRMaterial::create("some-mat");
    geo->setMaterial(m);
    m->setLit(false);
}
示例#8
0
    //void init(vector<VRGeometryPtr>& geos, VRMaterialPtr mat) {
    void init(vector<Geo>& geos, VRMaterialPtr mat) {
        geo = VRGeometry::create("factory_part"); // init new object

        pos = GeoPnt3fProperty::create();
        norms = GeoVec3fProperty::create();
        inds_p = GeoUInt32Property::create();
        inds_n = GeoUInt32Property::create();

        geo->setType(GL_TRIANGLES);
        geo->setPositions( pos );
        geo->setNormals( norms );
        geo->setIndices( inds_p );
        geo->getMesh()->setIndex(inds_n, Geometry::NormalsIndex);
        geo->setMaterial(mat);

        geos.push_back(*this);

        vmin = Vec3f(1e9, 1e9, 1e9);
        vmax = Vec3f(-1e9, -1e9, -1e9);

        Np = Nn = 0;
        r = 0;
    }
示例#9
0
void loadVtk(string path, VRTransformPtr res) {
    cout << "load VTK file " << path << endl;
    VRGeoData geo;

    vtkDataSetReader* reader = vtkDataSetReader::New();
    reader->SetFileName(path.c_str());
    reader->ReadAllScalarsOn();
    reader->ReadAllVectorsOn();
    reader->ReadAllNormalsOn();
    reader->ReadAllTensorsOn();
    reader->ReadAllTCoordsOn();
    reader->ReadAllFieldsOn();
    reader->ReadAllColorScalarsOn();
    reader->Update();

    vtkDataSet* dataset = reader->GetOutput();

    int npoints = dataset->GetNumberOfPoints();
    int ncells = dataset->GetNumberOfCells();
    int nscalars = reader->GetNumberOfScalarsInFile();
    int nvectors = reader->GetNumberOfVectorsInFile();
    int ntensors = reader->GetNumberOfTensorsInFile();
    cout << "dataset sizes: " << npoints << " " << ncells << " " << nscalars << " " << nvectors << " " << ntensors << endl;

    for (int i=0; i<npoints; i++) {
        auto p = dataset->GetPoint(i);
        Vec3d v(p[0], p[1], p[2]);
        geo.pushVert(v);
        cout << "point " << v << endl;
    }

    auto getCellPIDs = [](vtkCell* c) {
        vector<int> res;
        auto ids = c->GetPointIds();
        for (int k=0; k<ids->GetNumberOfIds(); k++) {
            res.push_back( ids->GetId(k) );
        }
        return res;
    };

    for (int i=0; i<ncells; i++) {
        vtkCell* c = dataset->GetCell(i);
        //int d = c->GetCellDimension();
        //int t = c->GetCellType();

        string type = c->GetClassName();
        cout << "cell type " << type << endl;
        if (type == "vtkQuad") {
            auto j = getCellPIDs(c);
            geo.pushQuad(j[0], j[1], j[2], j[3]);
        }
    }

    //vtkCellData* cells = dataset->GetCellData();
    vtkPointData* points = dataset->GetPointData();

    cout << "POINT_DATA:\n";
    if (points) {
        std::cout << " contains point data with " << points->GetNumberOfArrays() << " arrays." << std::endl;
        for (int i = 0; i < points->GetNumberOfArrays(); i++) {
            std::cout << "\tArray " << i << " is named " << (points->GetArrayName(i) ? points->GetArrayName(i) : "NULL") << std::endl;
        }

        for(int i=0; vtkDataArray* a = points->GetArray(i); i++ ) {
            int size = a->GetNumberOfTuples();
            int comp = a->GetNumberOfComponents();
            cout << " data array " << size << " " << comp << endl;

            for (int j=0; j<size; j++) {
                cout << "pnt:";
                for (int k=0; k<comp; k++) cout << " " << a->GetComponent(j, k);
                cout << endl;
            }
        }
    }


    cout << "FIELD_DATA:\n";
     if (dataset->GetFieldData()) {
       std::cout << " contains field data with "
            << dataset->GetFieldData()->GetNumberOfArrays()
             << " arrays." << std::endl;
        for (int i = 0; i < dataset->GetFieldData()->GetNumberOfArrays(); i++)
          {
          std::cout << "\tArray " << i
               << " is named " << dataset->GetFieldData()->GetArray(i)->GetName()
               << std::endl;
          }
        }


    cout << "CELL_DATA:\n";

     vtkCellData *cd = dataset->GetCellData();
      if (cd)
        {
        std::cout << " contains cell data with "
             << cd->GetNumberOfArrays()
             << " arrays." << std::endl;
        for (int i = 0; i < cd->GetNumberOfArrays(); i++)
          {
          std::cout << "\tArray " << i
               << " is named "
               << (cd->GetArrayName(i) ? cd->GetArrayName(i) : "NULL")
               << std::endl;
          }
        }

    /*if (cells) {
        for(int i=0; vtkDataArray* a = points->GetArray(i); i++ ) {
            int size = a->GetNumberOfTuples();
            int comp = a->GetNumberOfComponents();
            for (int j=0; j<size; j++) {
                cout << "cell:";
                for (int k=0; k<comp; k++) cout << " " << a->GetComponent(j, k);
                cout << endl;
            }
        }
    }*/

    string name = "vtk";

    auto m = VRMaterial::create(name + "_mat");
    m->setLit(0);
    m->setDiffuse(Color3f(0.3,0.7,1.0));

    VRGeometryPtr g = geo.asGeometry(name);
    g->setMaterial(m);
    //g->updateNormals();
    res->addChild( g );
}
示例#10
0
void loadVtk_old(string path, VRTransformPtr res) {
    cout << "load VTK file " << path << endl;
    ifstream file(path.c_str());
    string line;

    auto next = [&]() -> string& {
        getline(file, line);
        return line;
    };

    VTKProject project;
    project.version = splitString( next() )[4];
    project.title = next();
    project.format = next();
    project.dataset = splitString( next() )[1];

    VRGeoData geo; // build geometry

    if (project.dataset == "STRUCTURED_POINTS") {
        auto r = splitString( next() ); Vec3i dims = toValue<Vec3i>( r[1] + " " + r[2] + " " + r[3] );
             r = splitString( next() ); Vec3d p0 = toValue<Vec3d>( r[1] + " " + r[2] + " " + r[3] );
             r = splitString( next() ); Vec3d d = toValue<Vec3d>( r[1] + " " + r[2] + " " + r[3] );

        for (int k=0; k<dims[2]; k++) {
            for (int j=0; j<dims[1]; j++) {
                for (int i=0; i<dims[0]; i++) {
                    geo.pushVert(p0 + Vec3d(d[0]*i, d[1]*j, d[2]*k) );
                    geo.pushPoint();
                }
            }
        }
    }

    if (project.dataset == "STRUCTURED_GRID") {
        auto r = splitString( next() ); Vec3i dims = toValue<Vec3i>( r[1] + " " + r[2] + " " + r[3] );
             r = splitString( next() ); int N = toInt(r[1]); string type = r[2]; // points

        vector<Vec3d> points;
        for (int i=0; i<N; i++) {
            Vec3d p = toValue<Vec3d>( next() );
            points.push_back(p);
            geo.pushVert(p);
            geo.pushPoint();
        }
    }

    if (project.dataset == "RECTILINEAR_GRID") {
        ;
    }

    if (project.dataset == "UNSTRUCTURED_GRID") {
        ;
    }

    if (project.dataset == "POLYDATA") {
        auto r = splitString( next() ); int N = toInt(r[1]); string type = r[2]; // points
        for (int i=0; i<N; i++) geo.pushVert( toValue<Vec3d>( next() ) );

        while (next() != "") {
            r = splitString( line );
            string type = r[0];
            N = toInt(r[1]);
            //int size = toInt(r[2]);
            for (int i=0; i<N; i++) { // for each primitive
                r = splitString( next() );
                int Ni = toInt(r[0]); // length of primitive
                cout << line << "  " << Ni << endl;
                //if (Ni == 2) geo.pushLine(toInt(r[1]), toInt(r[2]));
                if (Ni == 3) geo.pushTri(toInt(r[1]), toInt(r[2]), toInt(r[3]));
                if (Ni == 4) geo.pushQuad(toInt(r[1]), toInt(r[2]), toInt(r[3]), toInt(r[4]));
            }
        }
    }

    if (project.dataset == "FIELD") {
        ;
    }

    // parsing finished
    cout << project.toString() << endl;
    file.close();

    auto m = VRMaterial::create(project.title + "_mat");
    m->setLit(0);
    m->setDiffuse(Color3f(0.3,0.7,1.0));

    VRGeometryPtr g = geo.asGeometry(project.title);
    g->setMaterial(m);
    //g->updateNormals();
    res->addChild( g );
}
示例#11
0
void VRNature::computeLODs(map<OctreeNode*, VRLodLeafPtr>& leafs) {
    auto simpleLeafMat = [](bool doAlpha) {
        auto m = VRMaterial::create("simpleLeafMat");
        m->setPointSize(3);
        //m->setDiffuse(Color3f(0.5,1,0));
        m->setDiffuse(Color3f(0,0,1));
        m->setAmbient(Color3f(0.1,0.3,0));
        m->setSpecular(Color3f(0.1,0.4,0));
        string wdir = VRSceneManager::get()->getOriginalWorkdir();
        m->readFragmentShader(wdir+"/shader/Trees/Shader_leafs_lod.fp");
        m->readVertexShader(wdir+"/shader/Trees/Shader_leafs_lod.vp");

        auto tg = VRTextureGenerator::create();
        tg->setSize(Vec3i(50,50,50), doAlpha);
        float r = 0.85;
        float g = 1.0;
        float b = 0.8;
        tg->add(PERLIN, 1, Color4f(r,g,b,0.9), Color4f(r,g,b,1) );
        tg->add(PERLIN, 0.5, Color4f(r,g,b,0.5), Color4f(r,g,b,1) );
        tg->add(PERLIN, 0.25, Color4f(r,g,b,0.8), Color4f(r,g,b,1) );
        m->setTexture(tg->compose(0));

        return m;
    };

    auto simpleTrunkMat = []() {
        auto m = VRMaterial::create("brown");
        m->setDiffuse(Color3f(0.6,0.3,0));
        string wdir = VRSceneManager::get()->getOriginalWorkdir();
        m->readFragmentShader(wdir+"/shader/Trees/Shader_trunc_lod.fp");
        m->readVertexShader(wdir+"/shader/Trees/Shader_trunc_lod.vp");
        return m;
    };

    auto simpleGrassMat = []() {
        return VRGrassPatch::getGrassSideMaterial();
    };

    // get all trees and grass patches for each leaf layer
    map<VRLodLeaf*, vector<VRTree*> > trees;
    map<VRLodLeaf*, vector<VRGrassPatch*> > grass;
    for (auto l : leafs) {
        auto& leaf = l.second;
        int lvl = leaf->getLevel();
        if (lvl == 0) continue;

        vector<void*> data = leaf->getOLeaf()->getAllData();
        for (auto v : data) {
            if (((VRObject*)v)->hasTag("tree")) trees[leaf.get()].push_back((VRTree*)v);
            if (((VRObject*)v)->hasTag("grass")) grass[leaf.get()].push_back((VRGrassPatch*)v);
        }
    }

    // create layer node geometries
    for (auto l : leafs) {
        auto& leaf = l.second;
        leaf->reset();
        int lvl = leaf->getLevel();
        if (lvl == 0) continue;
        bool doTrees = (trees.count(leaf.get()) >= 0);
        bool doGrass = (grass.count(leaf.get()) >= 0);

        Boundingbox bb;
        if (doTrees) for (auto t : trees[leaf.get()]) bb.update( t->getWorldPosition() );
        //if (doGrass) for (auto g : grass[leaf.get()]) bb.update( g->getWorldPosition() );
        Vec3d pos = bb.center();

        VRGeoData geoLeafs;
        VRGeoData geoTrunk;
        VRGeoData geoGrass;

        if (doTrees) for (auto t : trees[leaf.get()]) {
            if (treeRefs.count(t) == 0) continue;
            auto tRef = treeRefs[t];
            if (!tRef || !t) continue;
            Vec3d offset = t->getWorldPosition() - pos;
            tRef->createHullTrunkLod(geoTrunk, lvl, offset, t->getID());
            tRef->createHullLeafLod (geoLeafs, lvl, offset, t->getID());
        }

        if (doGrass) for (auto g : grass[leaf.get()]) {
            if (grassPatchRefs.count(g) == 0) continue;
            auto gRef = grassPatchRefs[g];
            if (!gRef || !g) continue;
            Vec3d offset = g->getWorldPosition() - pos;
            gRef->createLod(geoGrass, lvl, offset, g->getID());
        }

        VRGeometryPtr trunk;
        if (geoTrunk.size() > 0) {
            trunk = geoTrunk.asGeometry("trunk");
            if (!truncMat) truncMat = simpleTrunkMat();
            trunk->setMaterial(truncMat);

            leaf->set( trunk, 1 );
            trunk->setWorldPosition(pos);
            trunk->setDir(Vec3d(0,0,-1));
            trunk->setUp(Vec3d(0,1,0));

            if (geoLeafs.size() > 0) {
                auto leafs = geoLeafs.asGeometry("leafs");
                leafs->setPersistency(0);
                trunk->addChild( leafs );
                if (!leafMat1) leafMat1 = simpleLeafMat(true);
                if (!leafMat2) leafMat2 = simpleLeafMat(false);
                if (lvl < 3) leafs->setMaterial(leafMat1);
                else leafs->setMaterial(leafMat2);
            }
        }

        if (geoGrass.size() > 0) {
            auto patch = geoGrass.asGeometry("grass");
            if (!grassMat) grassMat = simpleGrassMat();
            patch->setMaterial(grassMat);

            if (trunk) trunk->addChild(patch);
            else leaf->set( patch, 1 );
            patch->setWorldPosition(pos);
            patch->setDir(Vec3d(0,0,-1));
            patch->setUp(Vec3d(0,1,0));
        }
    }
}
示例#12
0
void VRGeometry::merge(VRGeometryPtr geo) {
    if (!meshSet) {
        setIndices(GeoUInt32PropertyRecPtr( GeoUInt32Property::create()) );
        setTypes(GeoUInt8PropertyRecPtr( GeoUInt8Property::create()) );
        setNormals(GeoVec3fPropertyRecPtr( GeoVec3fProperty::create()) );
        if (geo->mesh->getColors()) setColors(GeoVec4fPropertyRecPtr( GeoVec4fProperty::create()) );
        setLengths(GeoUInt32PropertyRecPtr( GeoUInt32Property::create()) );
        setPositions(GeoPnt3fPropertyRecPtr( GeoPnt3fProperty::create()) );
    }

    Matrix M = getWorldMatrix();
    M.invert();
    M.mult( geo->getWorldMatrix() );

    GeoVectorProperty *v1, *v2;
    v1 = mesh->getPositions();
    v2 = geo->mesh->getPositions();
    int N = v1->size();
    for (uint i=0; i<v2->size(); i++) {
        Pnt3f p = v2->getValue<Pnt3f>(i);
        M.mult(p,p);
        v1->addValue(p);
    }

    v1 = mesh->getNormals();
    v2 = geo->mesh->getNormals();
    for (uint i=0; i<v2->size(); i++)  v1->addValue(v2->getValue<Vec3f>(i));

    auto c1 = mesh->getColors();
    auto c2 = geo->mesh->getColors();
    if (c1 && c2) {
        int Nc = getColorChannels( c2 );
        if (Nc == 3) for (uint i=0; i<c2->size(); i++) c1->addValue<Vec4f>( morphColor4( c2->getValue<Vec3f>(i) ) );
        if (Nc == 4) for (uint i=0; i<c2->size(); i++) c1->addValue<Vec4f>( morphColor4( c2->getValue<Vec4f>(i) ) );
    }

    GeoIntegralProperty *i1, *i2;
    i1 = mesh->getTypes();
    i2 = geo->mesh->getTypes();
    for (uint i=0; i<i2->size(); i++) i1->addValue(i2->getValue(i));

    i1 = mesh->getLengths();
    i2 = geo->mesh->getLengths();
    for (uint i=0; i<i2->size(); i++) i1->addValue(i2->getValue(i));

    i1 = mesh->getIndices();
    i2 = geo->mesh->getIndices();
    for (uint i=0; i<i2->size(); i++) i1->addValue(i2->getValue(i) + N);

    /*cout << "merge sizes:\n";
    if (mesh->getPositions()) cout << " pos: " << mesh->getPositions()->size() << endl;
    else cout << "no positions\n";
    if (mesh->getNormals()) cout << " norm: " << mesh->getNormals()->size() << endl;
    else cout << "no normals\n";
    if (mesh->getColors()) cout << " cols: " << mesh->getColors()->size() << endl;
    else cout << "no colors\n";
    if (mesh->getTypes()) cout << " types: " << mesh->getTypes()->size() << endl;
    else cout << "no types\n";
    if (mesh->getLengths()) {
        cout << " lengths: " << mesh->getLengths()->size() << endl;
        int lsum = 0;
        for (int i=0; i<mesh->getLengths()->size(); i++) lsum += mesh->getLengths()->getValue<int>(i);
        cout << " lengths sum: " << lsum << endl;
    } else cout << "no lengths\n";
    if (mesh->getIndices()) cout << " inds: " << mesh->getIndices()->size() << endl;
    else cout << "no indices\n";
    cout << "pos   idx " << mesh->getIndex(Geometry::PositionsIndex) << " " << geo->mesh->getIndex(Geometry::PositionsIndex) << endl;
    cout << "norms idx " << mesh->getIndex(Geometry::NormalsIndex) << " " << geo->mesh->getIndex(Geometry::NormalsIndex) << endl;
    cout << endl;*/
}