ADTF_DISPLAY_TOOLBOX  3.8.0 (ADTF 3.14.3)
Source Code for emo 3D Coordinate System Mixin
Location
./src/examples/src/coordsystemmixin
This example shows:
  • how to create a new coordinate system
  • how to add a mixin to a specific coordinate system
  • how to extend the context menu to add object specific actions
  • how to offer a possibility to transform a 3D object
  • how to offer a possibility to select (pick) an object
Header
#pragma once
#include <osg/Switch>
#include <osg/ShapeDrawable>
#include <memory>
using namespace adtf::ucom;
using namespace adtf::disptb::mixinlib;
class cCoordSystemMixin: public cMixin
{
public:
ADTF_CLASS_ID_NAME(cCoordSystemMixin, "demo_coord_system.3d_mixin.disptb.cid", "Demo 3D Coordinates System Mixin");
cCoordSystemMixin();
tResult InitScene() override;
private:
IChildCoordinateSystem* m_pCoordinateSystem;
adtf::base::property_variable<tBool> m_bShowAxis = tTrue;
adtf::base::property_variable<tFloat64> m_fTranslateX = 0;
adtf::base::property_variable<tFloat64> m_fTranslateY = 0;
adtf::base::property_variable<tFloat64> m_fTranslateZ = 0;
adtf::base::property_variable<tFloat64> m_fRotateX = 0;
adtf::base::property_variable<tFloat64> m_fRotateY = 0;
adtf::base::property_variable<tFloat64> m_fRotateZ = 0;
};
Copyright © Audi Electronics Venture GmbH.
Namespace for functionality provided by the Mixin Library.
Definition: baseobject.h:23
Implementation
#include "coordsystemmixin.h"
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Geode>
#include <osgFX/Scribe>
#include <osg/Transform>
#include <osg/ShapeDrawable>
#include <adtfstreaming3/adtf_streaming3.h>
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
ADTF_PLUGIN_VERSION("Demo 3D Coordinate System Mixin Plugin",
disptb,
DISPTB_VERSION_MAJOR,
DISPTB_VERSION_MINOR,
DISPTB_VERSION_PATCH,
cCoordSystemMixin)
cCoordSystemMixin::cCoordSystemMixin()
{
m_fTranslateX.SetDescription("The position of the coordinate system on x-axis.");
RegisterPropertyVariable("translate_x", m_fTranslateX);
m_fTranslateY.SetDescription("The position of the coordinate system on y-axis.");
RegisterPropertyVariable("translate_y", m_fTranslateY);
m_fTranslateZ.SetDescription("The position of the coordinate system on z-axis.");
RegisterPropertyVariable("translate_z", m_fTranslateZ);
m_fRotateX.SetDescription("The rotation on the x-axis in [radian].");
RegisterPropertyVariable("rotate_x", m_fRotateX);
m_fRotateY.SetDescription("The rotation on the y-axis in [radian].");
RegisterPropertyVariable("rotate_y", m_fRotateY);
m_fRotateZ.SetDescription("The rotation on the z-axis in [radian].");
RegisterPropertyVariable("rotate_z", m_fRotateZ);
m_bShowAxis.SetDescription("If enabled, the mixin will be visible.");
RegisterPropertyVariable("show_axis", m_bShowAxis);
m_pCoordinateSystem = CreateCoordinateSystem("coordinate_system");
SetDescription("coordinate_system", "The transformed coordinate system (server).");
SetDescription("Use this Mixin to create and use a movable coordinate system in the scene.");
adtf::streaming::set_help_link(*this, "$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_3d_coordinate_system_mixin_example_readme.html");
}
osg::ref_ptr<osg::Node> create_arrow(tFloat lineLength, tFloat lineRadius, tFloat arrowLength, tFloat arrowBase, const osg::Vec4& color)
{
osg::Cylinder* cyl = new osg::Cylinder(osg::Vec3(lineLength / 2.0f, 0, 0), lineRadius, lineLength);
// OSG cylinders have their cross-section in their xy plane and stretch in z.
// So we must rotate it by 90 degree around y to have it point in x direction.
cyl->setRotation(osg::Quat(M_PI_2, osg::Vec3(0, 1, 0)));
osg::ShapeDrawable* cyldrawable = new osg::ShapeDrawable(cyl);
cyldrawable->setColor(color);
auto cylgeode = new osg::Geode;
cylgeode->addDrawable(cyldrawable);
osg::Cone* cone = new osg::Cone(osg::Vec3(arrowLength / 4 + lineLength, 0, 0), arrowBase, arrowLength);
cone->setRotation(osg::Quat(M_PI_2, osg::Vec3(0, 1, 0)));
osg::ShapeDrawable* coneDrawable = new osg::ShapeDrawable(cone);
coneDrawable->setColor(color);
auto conegeode = new osg::Geode;
conegeode->addDrawable(coneDrawable);
auto pArrow = new osg::Group;
pArrow->addChild(cylgeode);
pArrow->addChild(conegeode);
return pArrow;
}
osg::ref_ptr<osg::Node> create_coords(tFloat lineLength, tFloat lineRadius, tFloat arrowLength, tFloat arrowBase)
{
// put a cube at zero as ultimate reference point
osg::Box* unitCube = new osg::Box(osg::Vec3(0, 0, 0), 0.12f);
osg::ShapeDrawable* unitCubeDrawable = new osg::ShapeDrawable(unitCube);
osg::Geode* basicShapesGeode = new osg::Geode();
basicShapesGeode->addDrawable(unitCubeDrawable);
auto coordinate_axis = new osg::Group();
coordinate_axis->addChild(create_arrow(lineLength, lineRadius, arrowLength, arrowBase,
osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)));
auto yAxis = new osg::PositionAttitudeTransform;
yAxis->setAttitude(osg::Quat(M_PI_2, osg::Vec3(0, 0, 1)));
yAxis->addChild(create_arrow(lineLength, lineRadius, arrowLength, arrowBase,
osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)));
coordinate_axis->addChild(yAxis);
auto zAxis = new osg::PositionAttitudeTransform;
zAxis->setAttitude(osg::Quat(-M_PI_2, osg::Vec3(0, 1, 0)));
zAxis->addChild(create_arrow(lineLength, lineRadius, arrowLength, arrowBase,
osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)));
coordinate_axis->addChild(zAxis);
return coordinate_axis;
}
tResult cCoordSystemMixin::InitScene()
{
m_pCoordinateSystem->SetPosition({m_fTranslateX, m_fTranslateY, m_fTranslateZ});
m_pCoordinateSystem->SetAttitude(osg::Quat(m_fRotateX, osg::Vec3(1, 0, 0),
m_fRotateZ, osg::Vec3(0, 0, 1),
m_fRotateY, osg::Vec3(0, 1, 0)));
if (m_bShowAxis)
{
m_pCoordinateSystem->GetRoot()->addChild(create_coords(1, .05, .2, .1));
}
RETURN_NOERROR;
}