Marcus Lindblom schrieb:
Sadi Tanis wrote:

Hi!

i'm trying to write a function which stores all nodenames of my loaded VRML-Model in an array. For each name i want to assign a KEY/ID so that i can access a specific node with it.
For example:

I have my wooden puppet and want to tell every joint on it how much to rotate in x,y and z direction. With an ID i can easily access a node and
determine the x,y,z values.

At the moment this function

>Action::ResultE enter(NodePtr& node)
>{
>    if (getName(node))
>    {
>            cout << getName(node) << endl;
>    }
>    else
>            cout << "No name was set!" << endl;
>            
>    return Action::Continue;
>}

returns all names used in the model on my panel. I want them in an array with a key.


Yesterday I tried the string <map> functionality described in the c++ stdlib.


That's your best bet if you want to map strings to NodePtrs.

The above function should help you, just do the following instead of cout:

map.insert(std::make_pair(std::string(getName(node)), node));

(Where map is a variable of type std::map<std::string, OSG::NodePtr>)


I have changed that part and also swaped it as an independent function.
It filters all nodes which contain "Gelenk" at the beginning of their names. These ones will be used later on in the program to allocate constraints to them. But now the traverse function used in "keyboard" part of the hello.cpp (files are attached) doesn't work. Do this two parts have to be in one file?

Cheers,
/Marcus

regards,
Sadi

--
Sadi Tanis
University of Koblenz/Landau
Student of Computer Vision
Simmerner Straße 134
56075 Koblenz Germany
Tel. +49.261.208.98.73
E-mail [EMAIL PROTECTED]
#include <iostream>
#include <map>
#include <string>
#include "checkName.h"

//------------  OPENSG SPECIFIC INCLUDE FILES:  ------------//
#include <OpenSG/OSGSimpleAttachments.h>
//----------------------------------------------------------//

using namespace std;
OSG_USING_NAMESPACE

typedef map<string, NodePtr> nodemap;
typedef pair<map<string, NodePtr>::iterator,bool> ret;

/************************************************************
 * FUNCTION: The function traverses the tree and fills the  *
 *           map with pairs <name,nodepointer>. At the be-  *
 *           ginning of each name has to be "Gelenk".       *
 ************************************************************/
Action::ResultE enter(NodePtr& node) {
        nodemap nlist;
        nodemap::iterator iter;

        string subStr = "Gelenk";
        
        for (iter = nlist.begin(); iter != nlist.end(); iter++) {
                if (subStr == string(getName(node)).substr(0,subStr.length())) {
                        nlist.insert(iter, pair<string, 
NodePtr>(string(getName(node)), node));
                }
                return Action::Continue;
        }
}

#ifndef STASH_H
#define STASH_H

#include <OpenSG/OSGAction.h>

OSG_BEGIN_NAMESPACE

Action::ResultE enter(NodePtr& node);

OSG_END_NAMESPACE

#endif /* STASH_H */
#include "checkName.h"

//------------  OPENSG SPECIFIC INCLUDE FILES:  ------------//

// GLUT is used for window handling
#include <OpenSG/OSGGLUT.h>
// General OpenSG configuration, needed everywhere
#include <OpenSG/OSGConfig.h>
// Methods to create simple geometry: boxes, spheres, tori etc.
#include <OpenSG/OSGSimpleGeometry.h>
// The GLUT-OpenSG connection class
#include <OpenSG/OSGGLUTWindow.h>
// A little helper to simplify scene management and interaction
#include <OpenSG/OSGSimpleSceneManager.h>
// Helper to add textures defined by an image
#include <OpenSG/OSGSimpleTexturedMaterial.h>
// Images are loaded as osg::Image instances
#include <OpenSG/OSGImage.h>
// The SceneFileHandler is used for loading the VRML-Files from the modeling 
tool
#include <OpenSG/OSGSceneFileHandler.h>
// Creates a BIN-File
#include <OpenSG/OSGBINWriter.h>
#include <OpenSG/OSGIntersectAction.h>
//----------------------------------------------------------//

using namespace std;
// Activate the OpenSG namespace
// This is not strictly necessary, you can also prefix all OpenSG symbols
// with OSG::, but that would be a bit tedious for this example
OSG_USING_NAMESPACE
// The SimpleSceneManager is a useful class which helps us to
// manage simple configurations.
SimpleSceneManager *mgr;
NodePtr scene;
// we have a forward declaration here, just to have a better order
// of codepieces
int setupGLUT( int *argc, char *argv[] );

/************************************************************
 * FUNCTION: This function creates the scenegraph.          * 
 ************************************************************/
NodePtr createScenegraph() {
        // load the VRML data
        NodePtr puppet = SceneFileHandler::the().read("data/gliederpuppe.wrl");
        NodePtr stage = SceneFileHandler::the().read("data/bühne.wrl");

        // check the result
        if ((puppet == NullFC) && (stage == NullFC)) {
                cout << "Loading the specified files was not possible!" << endl;
                return NullFC;
        }
        
        //now create root and add all children 
        NodePtr root = Node::create();
        beginEditCP(root);
        {
                root->setCore(Group::create());
                root->addChild(puppet);
                root->addChild(stage);
        }
        endEditCP(root);

        return root;
}

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv) {
    // Init the OpenSG subsystem
    osgInit(argc,argv);

    // creates a GLUT Window (that is almost the same for most applications)
    int winid = setupGLUT(&argc, argv);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

        // This will be the scene for now
        scene = createScenegraph();

        // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin);
    mgr->setRoot  (scene);

    // show the whole scene
    mgr->showAll();
    
    // Give Control to the GLUT Main Loop
    glutMainLoop();

    return 0;
}

//------------  GLUT CALLBACK FUNCTIONS:  ------------//

// just redraw our scene if this GLUT callback is invoked
void display(void)
{
    mgr->redraw();
}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (state)
        mgr->mouseButtonRelease(button, x, y);
        else {

        mgr->mouseButtonPress(button, x, y);
                
                                // create & set ray starting point and 
direction according the viewpoint
                                Line ray = mgr->calcViewRay(x, y);

                                // new Intersection
                                IntersectAction *iAct = 
IntersectAction::create();
                
                                iAct->setLine(ray);
                                // iAct->apply(mgr->getRoot());
                                iAct->apply(scene);
        
                                if (iAct->didHit()){

                                        // get the hit point
                                        Pnt3f position = iAct->getHitPoint();

                                        cout << "Hit point: " << position[0] << 
" " << position[1] << " " << position[2] << endl;

                                        NodePtr n = iAct->getHitObject();
                                        
                                        // setHighlight draws a cube around the 
selected NodeObject
                                        mgr->setHighlight(n);                   
                
                                }
                        }
        
                        glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
    switch(k)
    {
                // --------------- here is teh problem --------------- //
                case 'p':

                        cout << endl <<endl;
                        cout << "Printing all node names";
                        cout << "-------------------------------------";
                        cout << endl << endl;

                        //now we invoke the traversal
                        traverse(scene,
                                                osgTypedFunctionFunctor1CPtrRef
                                                        <Action::ResultE, 
NodePtr>(enter));
                // --------------------------------------------------- //
        case 's':        
        
#if OSG_MINOR_VERSION > 2
                        
                        // create an output stream (this is STL code, and
            // is not OpenSG specific!)
                        ofstream out("data/output.bin");
                        if(!out) {
                                cout << "Output stream could not be created!" 
<< endl;
                                return;
                        }

                        //create the writer object
                        BINWriter writer(out);

#else

                        //this code applies to version 1.2
                        FILE* outFile = fopen("data/output.bin", "wb");
                        if(outFile == NULL) {
                                cout << "File could not be created!" << endl;
                                return;
                        }

                        //create the writer object
                        BINWriter writer(outFile);

#endif

                        //write the file now
                        writer.write(scene);

                        cout << "File written!" << endl;
                        break;
    }
}

// setup the GLUT library which handles the windows for us
//The GLUT subsystem is set up here. This is very similar to other GLUT
//applications. If you have worked with GLUT before, you may have the
//feeling of meeting old friends again, if you have not used GLUT before
//that is no problem. GLUT will be introduced briefly on the next section
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("OpenSG 3D file visualisation");
    
        // register the GLUT callback functions
        glutDisplayFunc(display);
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    glutKeyboardFunc(keyboard);
        
    return winid;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to