Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformer readability and visualization world frame selection #50

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
57 changes: 57 additions & 0 deletions src/OsgVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace vizkit3d
{
//
// FindNode
//
::osg::Node * FindNode::find(::osg::Node &node,std::string name)
{
FindNode finder(name);
Expand Down Expand Up @@ -33,4 +36,58 @@ namespace vizkit3d
else
traverse(node);
}


//
// SetOpacity
//
void SetOpacity::setOpacity(::osg::Node &node, double opacity)
{
SetOpacity visitor(opacity);
node.accept(visitor);
}

SetOpacity::SetOpacity()
: ::osg::NodeVisitor(::osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
{
}

SetOpacity::SetOpacity(double opacity) :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
opacity(opacity)
{
}

void SetOpacity::apply(osg::Node &node){
traverse(node);
}

void SetOpacity::apply(osg::Geode &geode)
{
osg::StateSet *state = NULL;
unsigned int vertNum = 0;


//
// We need to iterate through all the drawables check if
// the contain any geometry that we will need to process
//
unsigned int numGeoms = geode.getNumDrawables();
for( unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++ ) {
osg::Geometry *curGeom = geode.getDrawable( geodeIdx )->asGeometry();

//
// Only process if the drawable is geometry
//
if ( curGeom ) {
osg::Vec4Array *colorArrays = dynamic_cast< osg::Vec4Array *>(curGeom->getColorArray());
if ( colorArrays ) {
for ( unsigned int i = 0; i < colorArrays->size(); i++ ) {
osg::Vec4 *color = &colorArrays->operator [](i);
color->set( color->r(), color->g(), color->b(), opacity);
}
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/OsgVisitors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,21 @@ namespace vizkit3d
::osg::ref_ptr< ::osg::Node> node;
std::string name;
};


class SetOpacity : public ::osg::NodeVisitor
{
public:
static void setOpacity(::osg::Node &node, double opacity);

SetOpacity();
SetOpacity(double opacity);

void apply(osg::Node &node );
void apply(::osg::Geode &geode);
private:
::osg::ref_ptr< ::osg::Node> node;
double opacity;
};
}
#endif
55 changes: 55 additions & 0 deletions src/TransformerGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@ osg::PositionAttitudeTransform *createFrame(const std::string &name,bool root=fa
osgText::Text *text= new osgText::Text;
text->setText(name);
text->setCharacterSize(textSize);

bool screenCoords = false;
if(screenCoords){
maltewi marked this conversation as resolved.
Show resolved Hide resolved
text->setAxisAlignment(osgText::Text::SCREEN);
}

if(root)
text->setPosition(osg::Vec3d(textSize/2,-textSize*1.5,0));
else
text->setPosition(osg::Vec3d(textSize/2,textSize/2,0));

text_geode->addDrawable(text);
switch_node->addChild(text_geode,true);

Expand Down Expand Up @@ -267,6 +274,38 @@ class TextSizeSetter: public ::osg::NodeVisitor
float size;
};

class TextAxisAlignmentSetter: public ::osg::NodeVisitor
{
public:
static void set(::osg::Node &node, osgText::Text::AxisAlignment alignment)
{
TextAxisAlignmentSetter setter(alignment);
node.accept(setter);
}

TextAxisAlignmentSetter(osgText::Text::AxisAlignment alignment)
: ::osg::NodeVisitor(::osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
, alignment(alignment) {}

void apply(::osg::Node &node)
{
// Just stop if the node is not a PositionAttitudeTransform to prevent
// traversing the hole graph.
// The assumption here is that all custom nodes are always added
// to a group child node of the transformation nodes.
osg::PositionAttitudeTransform *trans = getTransform(&node, false);
if(!trans)
return;

osgText::Text* text = getFrameText(trans);
text->setAxisAlignment(alignment);

traverse(node);
}
private:
osgText::Text::AxisAlignment alignment;
};

class NodeRemover: public ::osg::NodeVisitor
{
public:
Expand Down Expand Up @@ -439,6 +478,11 @@ void TransformerGraph::setTextSize(osg::Node &transformer, float size)
TextSizeSetter::set(transformer, size);
}

void TransformerGraph::setTextAxisAlignment(osg::Node &transformer, osgText::Text::AxisAlignment alignment)
{
TextAxisAlignmentSetter::set(transformer, alignment);
}

float TransformerGraph::getTextSize(osg::Node &transformer)
{
osgText::Text *text= getFrameText(&transformer);
Expand Down Expand Up @@ -491,6 +535,14 @@ static void makeRoot(osg::Node& _transformer,
{
PositionAttitudeTransform* transformer = getTransform(&_transformer);

// Visit all child transforms and mark as outdated
// According to createFrame and addTRansform, the children are sorted like:
// [user data group, axes, text, link (line between frames), switch for annotation, transform_to_root_frame_1, transform_to_root_frame_2, ...]
for(size_t i = 5; i<transformer->getNumChildren(); i++){
SetOpacity::setOpacity(*transformer->getChild(6), 0.25);
}


osg::Vec3d trans = osg::Vec3d(0, 0, 0);
osg::Quat rot = osg::Quat(0, 0, 0, 1);

Expand All @@ -511,6 +563,9 @@ static void makeRoot(osg::Node& _transformer,
lastNode = currentNode;
currentNode = parent;
}

// Set unset opacity of all connected children of new root such they are fully visible
SetOpacity::setOpacity(*(desiredRoot->asNode()), 1);
}

void TransformerGraph::makeRoot(osg::Node& _transformer, std::string const& frame)
Expand Down
4 changes: 4 additions & 0 deletions src/TransformerGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <osg/Node>
#include <osg/Geometry>
#include <osgText/Text>

#include <vector>
#include <string>
Expand Down Expand Up @@ -46,6 +47,8 @@ namespace vizkit3d
*/
static void setTextSize(osg::Node &transformer, float size);

static void setTextAxisAlignment(osg::Node &transformer, osgText::Text::AxisAlignment alignment);

/**
* Returns the size of the frame annotations
*/
Expand Down Expand Up @@ -212,3 +215,4 @@ namespace vizkit3d
}

#endif

78 changes: 77 additions & 1 deletion src/Vizkit3DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ QStringList Vizkit3DConfig::getVisualizationFrames() const
return frames;
}

QStringList Vizkit3DConfig::getTransformerFrames() const
{
QStringList frames = getVisualizationFrames();
frames.removeAll(getWidget()->getWorldName());
return frames;
}

void Vizkit3DConfig::setVisualizationFrame(const QStringList &frames)
{
return getWidget()->setVisualizationFrame(frames.front(),false);
Expand Down Expand Up @@ -108,6 +115,12 @@ void Vizkit3DConfig::setTransformerTextSize(float value)
return getWidget()->setTransformerTextSize(value);
}

void Vizkit3DConfig::setRootFrame(const QStringList &frames) const
{
getWidget()->setRootFrame(frames.front());
}


QColor Vizkit3DConfig::getBackgroundColor() const
{
return getWidget()->getBackgroundColor();
Expand All @@ -128,6 +141,7 @@ bool Vizkit3DConfig::isAxesLabels() const
return getWidget()->isAxesLabels();
}

#include <osgText/Text>
namespace
{
struct ManipulatorDefinition
Expand All @@ -148,6 +162,21 @@ namespace
{ "None", NO_MANIPULATOR, true },
{ 0, FIRST_PERSON_MANIPULATOR } // only the empty string is used as guard
};

struct AxisAlignmentDefinition
{
QString name;
osgText::Text::AxisAlignment id;
bool is_public;
};
AxisAlignmentDefinition KNOWN_AXIS_ALIGNMENTS[] = {
{ "Default", osgText::Text::AxisAlignment::USER_DEFINED_ROTATION, true },
{ "SCREEN", osgText::Text::AxisAlignment::SCREEN, true },
{ "XY_PLANE", osgText::Text::AxisAlignment::XY_PLANE, true },
{ "XZ_PLANE", osgText::Text::AxisAlignment::XZ_PLANE, true },
{ "YZ_PLANE", osgText::Text::AxisAlignment::YZ_PLANE, true },
{ 0, osgText::Text::AxisAlignment::USER_DEFINED_ROTATION } // only the empty string is used as guard
};
}


Expand All @@ -173,6 +202,18 @@ QStringList Vizkit3DConfig::getAvailableCameraManipulators() const
return names;
}

QStringList Vizkit3DConfig::getAvailableAxisAlignments() const
{
QStringList names;
for (int i = 0; KNOWN_AXIS_ALIGNMENTS[i].name != 0; ++i)
{
if (KNOWN_AXIS_ALIGNMENTS[i].is_public)
names.append(KNOWN_AXIS_ALIGNMENTS[i].name);
}

return names;
}

QString Vizkit3DConfig::manipulatorIDToName(CAMERA_MANIPULATORS id)
{
for (int i = 0; KNOWN_MANIPULATORS[i].name != 0; ++i)
Expand Down Expand Up @@ -202,6 +243,35 @@ void Vizkit3DConfig::setCameraManipulator(QStringList const& manipulator)
return getWidget()->setCameraManipulator(id);
}

QString Vizkit3DConfig::axisAlignmentIDToName(osgText::Text::AxisAlignment id)
{
for (int i = 0; KNOWN_AXIS_ALIGNMENTS[i].name != 0; ++i)
{
if (id == KNOWN_AXIS_ALIGNMENTS[i].id)
return KNOWN_AXIS_ALIGNMENTS[i].name;
}
throw std::invalid_argument("axis alignment ID " + boost::lexical_cast<std::string>(id) + " has no declared name");
}

osgText::Text::AxisAlignment Vizkit3DConfig::axisAlignmentNameToID(QString const& name)
{
for (int i = 0; KNOWN_AXIS_ALIGNMENTS[i].name != 0; ++i)
{
if (name == KNOWN_AXIS_ALIGNMENTS[i].name)
return KNOWN_AXIS_ALIGNMENTS[i].id;
}
throw std::invalid_argument("axis alignment name " + name.toStdString() + " does not exist");
}

void Vizkit3DConfig::setTextAxisAlignment(QStringList const& axisAlignment)
{
osgText::Text::AxisAlignment id = axisAlignmentNameToID(axisAlignment.front());
return getWidget()->setTextAxisAlignment(id);
}




Vizkit3DWidget::Vizkit3DWidget(QWidget* parent,const QString &world_name,bool auto_update)
: QMainWindow(parent)
, env_plugin(NULL), clickHandler(new osgviz::ManipulationClickHandler),
Expand Down Expand Up @@ -926,6 +996,7 @@ void Vizkit3DWidget::setTransformation(const QString &source_frame,const QString
registerClickHandler(target_frame.toStdString());

emit propertyChanged("frame");
emit propertyChanged("transformerRootFrame");
// first: VizPluginBase*
// second: osg::ref_ptr<osg::Group>

Expand Down Expand Up @@ -993,6 +1064,11 @@ void Vizkit3DWidget::setTransformerTextSize(float size)
emit propertyChanged("transformerTextSize");
}

void Vizkit3DWidget::setTextAxisAlignment(osgText::Text::AxisAlignment alignment)
{
TransformerGraph::setTextAxisAlignment(*getRootNode(), alignment);
}

void Vizkit3DWidget::setAxesLabels(bool value)
{
osg::Node *node = FindNode::find(*getRootNode(),"axes_node");
Expand Down Expand Up @@ -1108,7 +1184,7 @@ QStringList* Vizkit3DWidget::getAvailablePlugins()
for(;iter3 != lib_plugins->end();++iter3)
*plugins_str_list << QString(*iter3 + "@" + file_info.absoluteFilePath());
}
catch(std::runtime_error e)
catch(std::runtime_error& e)
{
std::cerr << "WARN: cannot load vizkit plugin library " << e.what() << std::endl;
}
Expand Down
14 changes: 13 additions & 1 deletion src/Vizkit3DWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ namespace vizkit3d
Q_PROPERTY( bool transformer READ isTransformer WRITE setTransformer)
Q_ENUMS( CAMERA_MANIPULATORS )
Q_PROPERTY( QStringList manipulator READ getAvailableCameraManipulators WRITE setCameraManipulator )
Q_PROPERTY( double transformerTextSize READ getTransformerTextSize WRITE setTransformerTextSize );
Q_ENUMS( osgText::Text::AxisAlignment )
Q_PROPERTY( QStringList textAxisAlignment READ getAvailableAxisAlignments WRITE setTextAxisAlignment )
Q_PROPERTY( double transformerTextSize READ getTransformerTextSize WRITE setTransformerTextSize )
Q_PROPERTY( QStringList transformerRootFrame READ getTransformerFrames WRITE setRootFrame )

public:
Vizkit3DConfig(Vizkit3DWidget *parent);
Expand All @@ -183,6 +186,9 @@ namespace vizkit3d
float getTransformerTextSize() const;
void setTransformerTextSize(float value);

QStringList getTransformerFrames() const;
void setRootFrame(const QStringList &frames) const;

QColor getBackgroundColor()const;
void setBackgroundColor(QColor color);

Expand All @@ -197,6 +203,11 @@ namespace vizkit3d
* current one at the top
*/
QStringList getAvailableCameraManipulators() const;

static QString axisAlignmentIDToName(osgText::Text::AxisAlignment id);
static osgText::Text::AxisAlignment axisAlignmentNameToID(QString const& name);
void setTextAxisAlignment(QStringList const& axisAlignment);
QStringList getAvailableAxisAlignments() const;
};

class QDESIGNER_WIDGET_EXPORT Vizkit3DWidget : public QMainWindow
Expand Down Expand Up @@ -308,6 +319,7 @@ namespace vizkit3d
void setTransformer(bool value);
float getTransformerTextSize() const;
void setTransformerTextSize(float value);
void setTextAxisAlignment(osgText::TextBase::AxisAlignment alignment);
bool isAxes() const;
void setAxes(bool value);

Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ rock_testsuite(testGUI suite.cpp testWidget.cpp
rock_testsuite(testCore suite.cpp testTransformerGraph.cpp
DEPS vizkit3d
LIBS ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY})

rock_executable(vizkit3d-bin NOINSTALL
SOURCES vizkit3d.cpp
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just for testing purposes but not unit tests.. maybe should be removed, maintainer should decide

DEPS vizkit3d vizkit3d-viz
LIBS ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY}
)
Loading