📄 rubyscenegraph.cpp
字号:
joint->setType( (TJoint)msg.m_Shape ); for(unsigned i = 0; i < m_Nodes.size(); i++) if( m_Nodes[i]->getName() == msg.m_Name ) joint->setParrent( m_Nodes[i] ); if( msg.m_ParrentIndex >= 0 && (unsigned)msg.m_ParrentIndex < m_Nodes.size() ) joint->setParrent( m_Nodes[msg.m_ParrentIndex] ); if( joint->getParrent() != NULL && joint->getChild() != NULL ) joint->calculateRelativePositions(); m_Joints.push_back( joint ); if( OpenGLEngine != NULL ) OpenGLEngine->addObject( joint ); return true;}/*! This method edits a joint from the recieved message \param msg Message to edit the joint from \return bool Indicating wheather update was successfull */bool TRubySceneGraph::cmdEditJoint( TMessage msg ){ unsigned index = msg.m_Index; if( index < 0 || index >= m_Joints.size() ) { cerr << "TRubySceneGraph::cmdEditJoint: Joint does not exist: '" << msg.m_Index << "'" << endl; return false; } if( msg.m_Command == CMD_JOINT_RETYPE ) { m_Joints[ index ]->setType( (TJoint)msg.m_Shape ); return true; } if( msg.m_Command == CMD_JOINT_REPARRENT ) { if( msg.m_Name == "Unknown" ) { m_Joints[index]->setParrent( NULL ); return true; } for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( msg.m_Name == m_Nodes[i]->getName() ) { m_Joints[index]->setParrent( m_Nodes[i] ); return true; } cerr << "TRubySceneGraph::cmdEditJoint: Parrent specified does not exist: '" << msg.m_Name << "'" << endl; return false; } if( msg.m_Command == CMD_JOINT_RECHILD ) { if( msg.m_Name == "Unknown" ) { m_Joints[index]->setChild( NULL ); return true; } for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( msg.m_Name == m_Nodes[i]->getName() ) { m_Joints[index]->setChild( m_Nodes[i] ); return true; } cerr << "TRubySceneGraph::cmdEditJoint: Child specified does not exist: '" << msg.m_Name << "'" << endl; return false; } if( msg.m_Command == CMD_JOINT_ANCHOR ) { m_Joints[index]->setAnchor( msg.m_Position ); return true; } if( msg.m_Command == CMD_JOINT_PERCEPTOR ) { m_Joints[index]->setPerceptorName( msg.m_Name ); return true; } if( msg.m_Command == CMD_JOINT_EFFECTOR ) { m_Joints[index]->setEffectorName( msg.m_Name ); return true; } if( msg.m_Command == CMD_JOINT_MOTOR ) { if( msg.m_Number != 1 && msg.m_Number != 2 ) { cerr << "TRubySceneGraph::cmdEditJoint: Unknown motor specified:" << msg.m_Number << endl; return false; } m_Joints[index]->setMaxMotorForce( msg.m_Number, (int)msg.m_Force ); return true; } if( msg.m_Command == CMD_JOINT_AXIS ) { if( msg.m_Number != 1 && msg.m_Number != 2 ) { cerr << "TRubySceneGraph::cmdEditJoint: Unknown axis specified:" << msg.m_Number << endl; return false; } m_Joints[index]->setAxis( msg.m_Position, msg.m_Number ); return true; } return false;}/*! This method clears the internal data of the scene */void TRubySceneGraph::clearScene(){ unsigned i; if( OpenGLEngine != NULL ) OpenGLEngine->clearScene(); for( i = 0; i < m_Joints.size(); i++ ) delete m_Joints[i]; m_Joints.clear(); for( i = 0; i < m_Nodes.size(); i++ ) if( !m_Nodes[i]->getParrent() ) delete m_Nodes[i]; m_Nodes.clear(); m_Changed = false; m_FileName = "";}/*! This method loads a scene into memory \param strFile File name that contains scene informtion \return bool Indicating wheather scene was imported successfully */bool TRubySceneGraph::LoadScene( const char * strFile ){ bool bResult; clearScene(); bResult = ParseMain( strFile ); initializeRendering(); if( bResult ) { m_FileName = strFile; m_Changed = false; } return bResult;}void TRubySceneGraph::SaveScene( const char * strFileName ){ ofstream file(strFileName); if( !file ) return; unsigned i = 0; for( i = 0; i < m_Nodes.size(); i++ ) m_Nodes[i]->setNotSaved(); for( i = 0; i < m_Joints.size(); i++ ) m_Joints[i]->setNotSaved(); file << ";\n;This file is generated by ZigoBot Designer\n;\n\n(RSG 0 1)\n\n(\n"; for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( m_Nodes[i]->getParrent() == NULL ) m_Nodes[i]->saveToStream( file, " " ); file << ")\n\n;End of file\n" << endl; file.close(); m_FileName = strFileName; m_Changed = false;}/** * This method adds all of the nodes of robot body to the OpenGL/MESA engine so * that they can be viewed * @return indicating wheather all of the nodes were added successfully */bool TRubySceneGraph::initializeRendering(){ if( OpenGLEngine == NULL ) return false; OpenGLEngine->clearScene(); for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( m_Nodes[i]->getParrent() == NULL ) OpenGLEngine->addObject( m_Nodes[i] ); for( unsigned i = 0; i < m_Joints.size(); i++ ) OpenGLEngine->addObject( m_Joints[i] ); return true;}/*! This method updates graph scene from the message recieved from the GTK Engine \param msg Data of the update \return bool indicating wheather update was successfull */bool TRubySceneGraph::respondToCommand( TMessage msg ){ if( msg.m_Command == CMD_ILLEGAL ) return false; unsigned index = msg.m_Index; bool bResult = false; if( msg.m_Command == CMD_NODE_CREATE ) bResult = cmdAddNode( msg ); else if( msg.m_Command == CMD_JOINT_CREATE ) bResult = cmdAddJoint( msg ); else if( msg.m_Command < CMD_NODE_MAX ) /// Nodes part { const TSceneNode *node = getNodeWithID( msg.m_Index, &index ); if( node == NULL ) { cerr << "TRubySceneGraph::respondToCommand: Unknown node recieved: '" << msg.m_Index << "'" << endl; return false; } if( msg.m_Command == CMD_NODE_DELETE ) bResult = cmdRemoveNode( index ); else bResult = cmdEditNode( msg, index ); } else /// Joint part { if( index < 0 || index >= m_Joints.size() ) /// Check for the joint to exist { cerr << "TRubySceneGraph::respondToCommand: Specified joint '" << msg.m_Index << "' doesn't exist" << endl; bResult = false; } else if( msg.m_Command == CMD_JOINT_DELETE ) bResult = cmdRemoveJoint( index ); else bResult = cmdEditJoint( msg ); } if( bResult ) m_Changed = true; return bResult;}/*! This method returns index of a node \param name Name of the node \return int Index of the node */int TRubySceneGraph::getNodeIndex( string name ) const{ for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( m_Nodes[i]->getName() == name ) return i; return -1;}/*! This method searches for a node named 'name' \return bool Indicating node exists or not */bool TRubySceneGraph::nodeExists( string name ) const{ for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( m_Nodes[ i ]->getName() == name ) return true; return false;}/*! This method returns the current nodes count \return Count of the nodes */int TRubySceneGraph::getNodesCount() const{ return m_Nodes.size();}/*! This method returns the properties of the specified node \param number Node number \return TSceneNode* Porperties of the node */TSceneNode* TRubySceneGraph::getNode( unsigned number ) const{ if( number < 0 || number >= m_Nodes.size() ) { cerr << "TRubySceneGraph::getNode(): Wanted access to node #" << number; cerr << ", which is out of bounds (" << m_Nodes.size() << ")" << endl; return NULL; } return m_Nodes[ number ];}/** * This method returns a node which its ID is the one passed here * @param id ID of node to return * @return The node with desired global ID */const TSceneNode * TRubySceneGraph::getNodeWithID( unsigned id, unsigned *index ) const{ if( index ) *index = -1; for( unsigned i = 0; i < m_Nodes.size(); i++ ) if( m_Nodes[i]->getID() == id ) { if( index ) *index = i; return m_Nodes[i]; } return NULL;}/*! This method returns wheather specified joint exists or not \param number Number of the joint \return bool Whether joint existed */bool TRubySceneGraph::jointExists( int number ) const{ for( unsigned i = 0; i < m_Joints.size(); i++ ) if( m_Joints[ i ]->getNumber() == number ) return true; return false;}/*! This method returns the current joints count \return Count of the joints */int TRubySceneGraph::getJointCount() const{ return m_Joints.size();}/*! This method returns the properties of the specified joint \param number Joint number \return TSceneJoint Porperties of the joint */const TSceneJoint * TRubySceneGraph::getJoint( unsigned number ) const{ if( number < 0 || number >= m_Joints.size() ) { cerr << "TRubySceneGraph::getJoint(): Wanted access to joint #" << number; cerr << ", which is out of bounds (" << m_Joints.size() << ")" << endl; return NULL; } return m_Joints[number];}/*! This method gets wheather data has been changed or not \return bool Wheather data has been changed */bool TRubySceneGraph::changed() const{ return m_Changed;}/*! This method returns the scene file name \return string Scene file name */string TRubySceneGraph::getSceneName() const{ return m_FileName;}/*-----------------------------------------------------------------------------------*//*--------------------------- External Variables Decleration ----------------------*//*-----------------------------------------------------------------------------------*/TRubySceneGraph * RubySceneGraph;void InitializeScene(){ RubySceneGraph = new TRubySceneGraph();}void FinalizeScene(){ delete RubySceneGraph; RubySceneGraph = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -