Пример #1
0
void GreenLight::parseHardwareFile()
{
    // Parse str
    char * a = new char[_hardwareContents.size()];
    memcpy(a, _hardwareContents.c_str(), _hardwareContents.size()); 

    char delim[] = "[\",]";
    int state = 0;
    Hardware * hw;
    std::list<Hardware *> hardware;
    for (char * stk = strtok(a, delim); stk != NULL; stk = strtok(NULL, delim))
    {
        state++;
        if(state == 1) // name
        {
            hw = new Hardware();
            hw->name = stk; 
        }
        else if (state == 2)
        {
 	    hw->rack = atoi(stk);
        }
        else if (state == 3)
        {
 	    hw->slot = atoi(stk);
        }
        else if (state == 4)
        {
 	    hw->height = atoi(stk);
            hardware.push_back(hw);
            state = 0;
        }
    }
    delete[] a;
// TODO validation checks (make sure proper type [string/integer] for token before assignment & state should be 0 after we exit the for loop)

    /*
     * 1) Read in component which we wish to texture
     * 2) Create model (geode) and cluster
     * 3) Repeat 1 & 2 for all components in config file
     * 4) Put components (from hardware file) in to scene via created models
     * +)Create default models (one per height, as needed) for untextured models
     * 5) Add component to appropriate rack
     */

    std::map< std::string, osg::ref_ptr<osg::Geode> > nameToGeode;
    std::map< std::string, std::pair<int,int> > nameToWattage;
    std::map< int, osg::ref_ptr<osg::Geode> > defaultModels;

    std::vector<std::string> components;
    std::string compBase = "Plugin.GreenLight.Components";
    std::string texDir = cvr::ConfigManager::getEntry("textureDir","Plugin.GreenLight.Components","");
    cvr::ConfigManager::getChildren(compBase, components);

    for(int c = 0; c < components.size(); c++)
    {
        std::string startname = cvr::ConfigManager::getEntry("startname", compBase + "." + components[c], "");
        int height = cvr::ConfigManager::getInt("height", compBase + "." + components[c],1);
        std::string texture = cvr::ConfigManager::getEntry("texture", compBase + "." + components[c], "");

        // map name to model, if valid
        if (startname != "" && texture != "")
            nameToGeode[startname] = makeComponentGeode(height, texDir + texture);

        int minWatt = cvr::ConfigManager::getInt("minWattage", compBase + "." + components[c], 0);
        int maxWatt = cvr::ConfigManager::getInt("maxWattage", compBase + "." + components[c], 0);

        if (minWatt > 0 && maxWatt > 0)
        {
            nameToWattage[startname] = std::make_pair(minWatt,maxWatt);
        }

        _cluster[startname] = new std::set< Component * >;
    }

    std::map< std::string, osg::ref_ptr<osg::Geode> >::iterator mit;
    std::map< std::string, std::pair<int,int> >::iterator wit;
    osg::ref_ptr<osg::Geode> geode;
    osg::CopyOp cOp = osg::CopyOp(osg::CopyOp::DEEP_COPY_ALL &  ~(osg::CopyOp::DEEP_COPY_TEXTURES & osg::CopyOp::DEEP_COPY_IMAGES));
    std::list<Hardware *>::iterator lit;
    std::map< std::string, std::set< Component * > * >::iterator cit;

    for (lit = hardware.begin(); lit != hardware.end(); lit++)
    {
        Component * hwComp;

        // Does hardware name start with any textured model names?
        for (mit = nameToGeode.begin(); mit != nameToGeode.end(); mit++)
        {
            if (((*lit)->name.substr(0,mit->first.size())).compare(mit->first) == 0)
            {
                geode = new osg::Geode(*mit->second,cOp);
                break;
            }
        }

        // No textured model available -- use a default
        if (mit == nameToGeode.end())
        {
            std::cerr << "Warning: Model does not exist for component: " << (*lit)->name << std::endl;

            int height = (*lit)->height;
            std::map< int, osg::ref_ptr<osg::Geode> >::iterator mit;

            // re-use model of the same height if it exists
            if ((mit = defaultModels.find(height)) != defaultModels.end())
                geode = new osg::Geode(*mit->second, cOp);
            else
            {
                geode = makeComponentGeode(height);
                defaultModels[height] = geode.get();
            }

        }

        // Create component from geode, name, and proper translation matrix
        hwComp = new Component(geode,(*lit)->name, osg::Matrix::translate(0,0,18+getZCoord((*lit)->slot)));

        // Does entity belong to a cluster?
        for (cit = _cluster.begin(); cit != _cluster.end(); cit++)
        {
            if (((*lit)->name.substr(0,cit->first.size())).compare(cit->first) == 0)
            {
                cit->second->insert(hwComp);
                hwComp->cluster = cit->first;
            }
        }

        hwComp->setDefaultMaterial();

        // if min/max wattages were given, set them up
        for (wit = nameToWattage.begin(); wit != nameToWattage.end(); wit++)
        {
            if (((*lit)->name.substr(0,wit->first.size())).compare(wit->first) == 0)
            {
                hwComp->minWattage = wit->second.first;
                hwComp->maxWattage = wit->second.second;
                break;
            }
        }

        if (wit == nameToWattage.end())
            std::cerr << "Warning: " << (*lit)->name << " does not have a min/max wattage set in the config file." << std::endl;

        // if item is in rack1, rotate it (since rack1 isn't rotated, but is on the left side of the box)
        if ((*lit)->rack == 1)
        {
            osg::Matrix mat = hwComp->transform->getMatrix();
            mat.setRotate(osg::Quat(osg::PI,osg::Vec3(0,0,1)));
            hwComp->transform->setMatrix(mat);
        }

        // finally add entity to the rack
        _rack[(*lit)->rack-1]->addChild(hwComp);

        // keep a reference to the component
        _components.insert(hwComp);

        // clean up our mess
        delete (*lit);
    }
}
Пример #2
0
void
Simbox::getCoord(int xInd, int yInd, int zInd, double &x, double &y, double &z) const
{
  getXYCoord(xInd, yInd, x, y);
  getZCoord(zInd, x, y, z);
}