Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions calit2/Mugic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ SET(PLUGIN_HEADERS
shapes/LineShape.h
shapes/RectangleShape.h
shapes/TextShape.h
shapes/CubeShape.h
shapes/ModelShape.h
)

ADD_LIBRARY(${LIB_NAME}
Expand All @@ -41,6 +43,8 @@ ADD_LIBRARY(${LIB_NAME}
shapes/LineShape.cpp
shapes/RectangleShape.cpp
shapes/TextShape.cpp
shapes/CubeShape.cpp
shapes/ModelShape.cpp
)

FIND_PACKAGE(ZMQ REQUIRED)
Expand Down
289 changes: 282 additions & 7 deletions calit2/Mugic/CommandParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "shapes/LineShape.h"
#include "shapes/RectangleShape.h"
#include "shapes/TextShape.h"
#include "shapes/CubeShape.h"
#include "shapes/ModelShape.h"
#include "shapes/CapsuleShape.h"
#include "shapes/SphereShape.h"
#include "shapes/ConeShape.h"
#include "shapes/CylinderShape.h"

#include <iostream>
#include <sstream>
Expand All @@ -26,6 +32,12 @@ CommandParser::CommandParser(ThreadQueue<std::string>* queue, MainNode* root) :
_shapeDefinitions["line"] = new Factory<LineShape>();
_shapeDefinitions["rectangle"] = new Factory<RectangleShape>();
_shapeDefinitions["text"] = new Factory<TextShape>();
_shapeDefinitions["cube"] = new Factory<CubeShape>();
_shapeDefinitions["capsule"] = new Factory<CapsuleShape>();
_shapeDefinitions["sphere"] = new Factory<SphereShape>();
_shapeDefinitions["cone"] = new Factory<ConeShape>();
_shapeDefinitions["cylinder"] = new Factory<CylinderShape>();
_shapeDefinitions["model"] = new Factory<ModelShape>();

// init mutex
_mutex.lock();
Expand Down Expand Up @@ -88,6 +100,41 @@ void CommandParser::parseCommand(std::string command)
_shapes[elementName]->update(command);
}
}

//command to rotate an object
else if( commandType.compare("rotate") == 0 )
{
//extract key and look up shape in map
if( _shapes.find(elementName) != _shapes.end() )
{
//call rotation
rotate(elementName, command);
}
}

//command to scale an object
else if( commandType.compare("scale") == 0 )
{
//extract key and look up shape in map
if( _shapes.find(elementName) != _shapes.end() )
{
//call scaling
scale(elementName, command);
}
}

//command to translate an object
else if( commandType.compare("translate") == 0 )
{
//extract key and look up shape in map
if( _shapes.find(elementName) != _shapes.end() )
{
//call translation
translate(elementName, command);
}
}

//command to delete all objects
else if( commandType.compare("delete") == 0)
{
if(elementName.compare("all") == 0)
Expand All @@ -99,6 +146,26 @@ void CommandParser::parseCommand(std::string command)
remove(elementName);
}
}

//reading in model files
else if( commandType.compare("model") == 0)
{

//create a new object if it doesn't exist or override old one
remove(elementName);

//create basic shape that holds the node
ModelShape* newShape = new ModelShape(command, elementName);

//take the node within the new shape and add it to scenegraph
osg::MatrixTransform* matrix = new osg::MatrixTransform();
matrix->addChild(newShape->getModelNode());
_root->addElement(matrix);

//get name from object, add to table
_shapes[newShape->BasicShape::getName()] = newShape;
}

/*
else if( commandType.compare("var") == 0 && !elementName.empty()) // check for variable
{
Expand All @@ -121,7 +188,12 @@ void CommandParser::parseCommand(std::string command)
osg::Geode* geode = new osg::Geode();
geode->setDataVariance(osg::Object::DYNAMIC);
geode->addDrawable(newShape->asDrawable());
_root->addElement(geode);
//_root->addElement(geode);

//add in MatrixTransform for manipulations
osg::MatrixTransform* matrix = new osg::MatrixTransform();
matrix->addChild(geode);
_root->addElement(matrix);

// get name from object created and add to table
_shapes[newShape->getName()] = newShape;
Expand All @@ -137,11 +209,11 @@ void CommandParser::removeAll()
{
it = _shapes.begin();

osg::Geode* geode = NULL;
osg::MatrixTransform* mat = NULL;
// get geode drawable is attached too
if( (geode = it->second->getParent()) != NULL)
if( (mat = it->second->getMatrixParent()) != NULL)
{
_root->removeElement(geode);
_root->removeElement(mat);
}

// remove old shape from map and scenegraph
Expand All @@ -156,11 +228,11 @@ void CommandParser::remove(std::string elementName)
std::map<std::string, BasicShape* >::iterator it = _shapes.find(elementName);
if( it != _shapes.end() )
{
osg::Geode* geode = NULL;
osg::MatrixTransform* mat = NULL;
// get geode drawable is attached too
if( (geode = it->second->getParent()) != NULL)
if( (mat = it->second->getMatrixParent()) != NULL)
{
_root->removeElement(geode);
_root->removeElement(mat);
}

// remove old shape from map and scenegraph
Expand Down Expand Up @@ -190,3 +262,206 @@ CommandParser::~CommandParser()

join();
}

void CommandParser::rotate(std::string elementName, std::string command)
{

std::string value;
float head, pitch, roll;
osg::MatrixTransform* matNode;
BasicShape* geoNode;
osg::Matrixd* rotationMat;
osg::Matrixd tempMat;
osg::Vec3d scaleVec;
osg::Vec3d transVec;
float degtorad = osg::PI/180.0;

//find MatrixTransform parent
geoNode = _shapes[elementName];
matNode = geoNode->getMatrixParent();

//search for pitch
value = getParameter(command, "pitch");
if( !value.empty())
{
//extract value from parameter
std::stringstream ss(value);
ss >> pitch;
}
else
pitch = 0.0;

//search for roll
value = getParameter(command, "roll");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> roll;
}
else
roll = 0.0;

//search for heading
value = getParameter(command, "head");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> head;
}
else
head = 0.0;

//create new matrix to set
rotationMat = new osg::Matrixd();
rotationMat->makeRotate(pitch*degtorad, osg::Vec3f(1, 0, 0), roll*degtorad, osg::Vec3f(0, 1, 0), head*degtorad, osg::Vec3f(0, 0, 1));
tempMat = matNode->getMatrix();

scaleVec = tempMat.getScale();
transVec = tempMat.getTrans();
rotationMat->preMultScale(scaleVec);
rotationMat->postMultTranslate(transVec);
matNode->setMatrix(*rotationMat);

}

void CommandParser::scale(std::string elementName, std::string command)
{

std::string value;
float xscale, yscale, zscale;
osg::MatrixTransform* matNode;
BasicShape* geoNode;
osg::Matrixd* scaleMat;
osg::Matrixd tempMat;
osg::Quat rotateQuat;
osg::Vec3d transVec;

//find MatrixTransform parent
geoNode = _shapes[elementName];
matNode = geoNode->getMatrixParent();

//check whether it is uniform scale or not
value = getParameter(command, "factor");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> xscale;
yscale = xscale;
zscale = xscale;
}
else
{
//search for xscale
value = getParameter(command, "x");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> xscale;
}
else
xscale = 1.0;

//search for yscale
value = getParameter(command, "y");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> yscale;
}
else
yscale = 1.0;

//search for zscale
value = getParameter(command, "z");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> zscale;
}
else
zscale = 1.0;
}

//create new matrix to set
scaleMat = new osg::Matrixd();
scaleMat->makeScale(xscale, yscale, zscale);
tempMat = matNode->getMatrix();
rotateQuat = tempMat.getRotate();
transVec = tempMat.getTrans();
scaleMat->postMultRotate(rotateQuat);
scaleMat->postMultTranslate(transVec);
matNode->setMatrix(*scaleMat);

}

void CommandParser::translate(std::string elementName, std::string command)
{

std::string value;
float xtrans, ytrans, ztrans;
osg::MatrixTransform* matNode;
BasicShape* geoNode;
osg::Matrixd* transMat;
osg::Matrixd rotateMat;
osg::Matrixd scaleMat;
osg::Matrixd tempMat;
osg::Quat rotateQuat;
osg::Vec3d scaleVec;

//find MatrixTransform parent
geoNode = _shapes[elementName];
matNode = geoNode->getMatrixParent();

//search for xtrans
value = getParameter(command, "x");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> xtrans;
}
else
xtrans = 0.0;

//search for ytrans
value = getParameter(command, "y");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> ytrans;
}
else
ytrans = 0.0;

//search for ztrans
value = getParameter(command, "z");
if( !value.empty() )
{
//extract value from parameter
std::stringstream ss(value);
ss >> ztrans;
}
else
ztrans = 0.0;

//create new matrix to set
transMat = new osg::Matrixd();
transMat->makeTranslate(xtrans, ytrans, ztrans);
tempMat = matNode->getMatrix();
rotateQuat = tempMat.getRotate();
scaleVec = tempMat.getScale();
transMat->preMultRotate(rotateQuat);
transMat->preMultScale(scaleVec);
matNode->setMatrix(*transMat);

}



7 changes: 7 additions & 0 deletions calit2/Mugic/CommandParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <OpenThreads/Block>
#include <osg/Node>
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osg/Matrixd>

#include "ThreadQueue.h"
#include "MainNode.h"
Expand Down Expand Up @@ -40,6 +42,11 @@ class CommandParser : public OpenThreads::Thread
void remove(std::string elementName);
void removeAll();

//MatrixTransform manipulation commands
void rotate(std::string elementName, std::string command);
void scale(std::string elementName, std::string command);
void translate(std::string elementName, std::string command);

public:
CommandParser(ThreadQueue<std::string>* queue, MainNode* root);
~CommandParser();
Expand Down
Binary file added calit2/Mugic/router/router
Binary file not shown.
Loading