Skip to content

Commit

Permalink
Merge branch 'production' into support_robotControllerAxisManufacture…
Browse files Browse the repository at this point in the history
…rCode
  • Loading branch information
rdiankov authored Apr 3, 2024
2 parents 727c88d + c1a3000 commit 87e757b
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 74 deletions.
6 changes: 6 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Version 0.142.0

- Add robotControllerAxisManufacturerCode so that servo drives from different manufacturer connected to daisy chain can be handled.
- Fix unbounded growth of _vmimic
* Add ViewerBase::SetUserText to customize HUD text size

Version 0.141.2
===============

* Fix the issue that second-to-last configuration along the given path segment may not be checked in `Check` function.

Version 0.141.1
===============
Expand Down
2 changes: 2 additions & 0 deletions include/openrave/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class OPENRAVE_API ViewerBase : public InterfaceBase

virtual void SetUserText(const std::string& userText) OPENRAVE_DUMMY_IMPLEMENTATION;

virtual void SetTextSize(double size) OPENRAVE_DUMMY_IMPLEMENTATION;

/// \brief controls showing the viewer.
///
/// \param showtype If zero, will hide all viewers. If != 0, should show viewers (dependent on plugin could have different meanings)
Expand Down
129 changes: 120 additions & 9 deletions plugins/qtcoinrave/qtcoinviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,34 @@ void QtCoinViewer::_InitConstructor(std::istream& sinput)

// add the message texts
SoSeparator* pmsgsep = new SoSeparator();
SoTranslation* pmsgtrans0 = new SoTranslation();
pmsgtrans0->translation.setValue(SbVec3f(-0.978f,0.93f,0));
pmsgsep->addChild(pmsgtrans0);

_messageFont = new SoFont();
_messageFont->size = 10;
pmsgsep->addChild(_messageFont);

_messageBaseTranslation = new SoTranslation();
_messageBaseTranslation->translation.setValue(SbVec3f(-0.978f,0.874f,0));
pmsgsep->addChild(_messageBaseTranslation);
SoBaseColor* pcolor0 = new SoBaseColor();
pcolor0->rgb.setValue(0.0f,0.0f,0.0f);
pmsgsep->addChild(pcolor0);
_messageNodes[0] = new SoText2();
pmsgsep->addChild(_messageNodes[0]);

_messageShadowTranslation = new SoTranslation();
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f,0.032f,0));
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f,0.0540f,0));
pmsgsep->addChild(_messageShadowTranslation);
SoBaseColor* pcolor1 = new SoBaseColor();
pcolor1->rgb.setValue(0.99f,0.99f,0.99f);
pmsgsep->addChild(pcolor1);
_messageNodes[1] = new SoText2();
pmsgsep->addChild(_messageNodes[1]);

_ivRoot->addChild(pmsgsep);
_messageSwitch = new SoSwitch();
_messageSwitch->whichChild.setValue(SO_SWITCH_ALL);
_messageSwitch->addChild(pmsgsep);

_ivRoot->addChild(_messageSwitch);
_ivRoot->addChild(_ivCamera);

SoEventCallback * ecb = new SoEventCallback;
Expand Down Expand Up @@ -640,6 +649,109 @@ void QtCoinViewer::SetUserText(const string& userText)
_userText = userText;
}

class SetTextSizeMessage : public QtCoinViewer::EnvMessage
{
public:
SetTextSizeMessage(QtCoinViewerPtr pviewer, void** ppreturn, double size)
: EnvMessage(pviewer, ppreturn, false), _textSize(size) {
}

virtual void viewerexecute() {
QtCoinViewerPtr pviewer = _pviewer.lock();
if( !pviewer ) {
return;
}
pviewer->_SetTextSize(_textSize);
EnvMessage::viewerexecute();
}

private:
double _textSize;
};

void QtCoinViewer::SetTextSize(double size)
{
if (_timerSensor->isScheduled() && _bUpdateEnvironment) {
EnvMessagePtr pmsg(new SetTextSizeMessage(shared_viewer(), (void**)NULL, size));
pmsg->callerexecute(false);
}
}

void QtCoinViewer::_SetTextSize(double size)
{
if ( size >= 0 ) {
// TODO: use a font that does not rely on hardcoded breakpoints
// move down to next text size breakpoint so that message shadow aligns nicely
_messageFont->size = _GetTextBaseSize(size);
// hide HUD text if requested size is 0
_messageSwitch->whichChild.setValue(size == 0 ? SO_SWITCH_NONE : SO_SWITCH_ALL);
// adjust the text offsets
_messageBaseTranslation->translation.setValue(_GetMessageBaseTranslation());
_messageShadowTranslation->translation.setValue(_GetMessageShadowTranslation());
}
}

// determines a text size that works best with the default qtcoin font (one of 10px, 14px, 18px, 26px)
// \param size requested text size
double QtCoinViewer::_GetTextBaseSize(double size)
{
if (size < 14.0) {
return 10.0;
}
if (size < 18.0) {
return 14.0;
}
if (size < 26.0) {
return 18.0;
}
return 26.0;
}

// based on the current font size of HUD text, computes the 3D offset of the black message "base"
// node relative to the center of the text plane such that the message aligns nicely with
// the upper left corner across all preset text sizes
SbVec3f QtCoinViewer::_GetMessageBaseTranslation()
{
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
float size = _messageFont->size.getValue();
// magic window-size-independent constants that ensure that all preset text sizes
// are roughly vertically aligned by the top of the first line
if (size < 14.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.07f*fhratio),0);
}
if (size < 18.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.082f*fhratio),0);
}
if (size < 26.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.106f*fhratio),0);
}
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.126f*fhratio),0);
}

// based on the current font size of HUD text, computes the 3D offset of the white message "shadow"
// node relative to the message "base" node such that the shadow lies roughly one pixel leftward
// and upward of the base message, helping to create a shading effect that improves overall HUD text
// visibility
SbVec3f QtCoinViewer::_GetMessageShadowTranslation()
{
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
float size = _messageFont->size.getValue();
// magic window-size-independent constants that ensure that the message shadow
// sits roughly one pixel above the base message text
if (size < 14.0f) {
return SbVec3f(-0.002f*fwratio,(0.032f*fhratio)*(size/10.0f),0);
}
if (size < 18.0f) {
return SbVec3f(-0.002f*fwratio,(0.0448f*fhratio)*(size/14.0f),0);
}
if (size < 26.0f) {
return SbVec3f(-0.002f*fwratio,(0.0540f*fhratio)*(size/18.0f),0);
}
return SbVec3f(-0.002f*fwratio,(0.0777f*fhratio)*(size/26.0f),0);
}

bool QtCoinViewer::LoadModel(const string& pfilename)
{
SoInput mySceneInput;
Expand Down Expand Up @@ -2800,10 +2912,9 @@ void QtCoinViewer::AdvanceFrame(bool bForward)
ss << _userText << endl;
}

// adjust the shadow text
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f*fwratio,0.032f*fhratio,0));
// adjust the text offsets
_messageBaseTranslation->translation.setValue(_GetMessageBaseTranslation());
_messageShadowTranslation->translation.setValue(_GetMessageShadowTranslation());

// search for all new lines
string msg = ss.str();
Expand Down
9 changes: 9 additions & 0 deletions plugins/qtcoinrave/qtcoinviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class QtCoinViewer : public QMainWindow, public ViewerBase
return _name;
}
virtual void SetUserText(const string& userText);
virtual void SetTextSize(double size);

virtual bool LoadModel(const string& filename);

Expand Down Expand Up @@ -340,6 +341,10 @@ public slots:
virtual void _SetTriangleMesh(SoSeparator* pparent, const float* ppoints, int stride, const int* pIndices, int numTriangles);
virtual void _Reset();
virtual void _SetBkgndColor(const RaveVector<float>& color);
virtual void _SetTextSize(double size);
virtual double _GetTextBaseSize(double size);
virtual SbVec3f _GetMessageBaseTranslation();
virtual SbVec3f _GetMessageShadowTranslation();
virtual void _closegraph(SoSwitch* handle);
virtual void _SetGraphTransform(SoSwitch* handle, const RaveTransform<float>& t);
virtual void _SetGraphShow(SoSwitch* handle, bool bshow);
Expand Down Expand Up @@ -412,7 +417,10 @@ public slots:
std::list< boost::shared_ptr<IvDragger> > _plistdraggers; /// draggers drawn
SoEventCallback* _eventKeyboardCB;

SoSwitch* _messageSwitch;
SoFont* _messageFont;
boost::array<SoText2*,2> _messageNodes;
SoTranslation* _messageBaseTranslation;
SoTranslation* _messageShadowTranslation;

bool _altDown[2];
Expand Down Expand Up @@ -526,6 +534,7 @@ public slots:
friend class DeselectMessage;
friend class ResetMessage;
friend class SetBkgndColorMessage;
friend class SetTextSizeMessage;
friend class StartPlaybackTimerMessage;
friend class StopPlaybackTimerMessage;
friend class SetGraphTransformMessage;
Expand Down
22 changes: 17 additions & 5 deletions plugins/qtosgrave/osgviewerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ QOSGViewerWidget::QOSGViewerWidget(EnvironmentBasePtr penv, const std::string& u
_osgWorldAxis->addChild(CreateOSGXYZAxes(32.0, 2.0));

_vecTextScreenOffset = osg::Vec2(10.0, 0.0);
_hudTextSize = 18.0;

if( !!_osgCameraHUD ) {
// in order to get the axes to render without lighting:
Expand Down Expand Up @@ -1163,13 +1164,24 @@ void QOSGViewerWidget::SetViewport(int width, int height)
osg::Camera *hudcamera = _osghudview->getCamera();
hudcamera->setViewport(0, 0, width * scale, height * scale);

double textheight = 18*scale;
_osgHudText->setCharacterSize(textheight);
_osgHudText->setFontResolution(textheight, textheight);
SetHUDTextOffset(_vecTextScreenOffset.x(), _vecTextScreenOffset.y());
SetHUDTextSize(_hudTextSize);
_UpdateHUDAxisTransform(width, height);
}

void QOSGViewerWidget::SetHUDTextSize(double size)
{
if ( size >= 0 ) {
_hudTextSize = size;

float scale = this->devicePixelRatio();
double textheight = _hudTextSize*scale;
_osgHudText->setCharacterSize(textheight);
_osgHudText->setFontResolution(textheight, textheight);

SetHUDTextOffset(_vecTextScreenOffset.x(), _vecTextScreenOffset.y());
}
}

osg::Vec2 QOSGViewerWidget::GetHUDTextOffset()
{
return osg::Vec2(_vecTextScreenOffset.x(), _vecTextScreenOffset.y());
Expand All @@ -1180,7 +1192,7 @@ void QOSGViewerWidget::SetHUDTextOffset(double xOffset, double yOffset)
_vecTextScreenOffset.set(xOffset, yOffset);

double scale = this->devicePixelRatio();
double textheight = 18*scale;
double textheight = _hudTextSize*scale;
_osgHudText->setPosition(
osg::Vec3(
-_osgview->getCamera()->getViewport()->width() / 2 + _vecTextScreenOffset.x(),
Expand Down
4 changes: 4 additions & 0 deletions plugins/qtosgrave/osgviewerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class QOSGViewerWidget : public QOpenGLWidget
/// \brief called when the qt window size changes
void SetViewport(int width, int height);

/// \brief sets the HUD text size, scaled off of devicePixelRatio
void SetHUDTextSize(double size);

/// \brief gets the screen offset of HUD text (default with no control buttons is (10.0, 0.0))
osg::Vec2 GetHUDTextOffset();

Expand Down Expand Up @@ -345,6 +348,7 @@ class QOSGViewerWidget : public QOpenGLWidget
/// causing getProjectionMatrixAsXXX to return negative
/// values. Therefore, we manage zNear ourselves
double _currentOrthoFrustumSize; ///< coordinate for the right vertical clipping plane
double _hudTextSize; ///< size of HUD text, scaled off of devicePixelRatio

void GetSwitchedButtonValue(unsigned int &button);

Expand Down
5 changes: 5 additions & 0 deletions plugins/qtosgrave/qtosgviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,11 @@ void QtOSGViewer::SetUserText(const string& userText)
_posgWidget->SetUserHUDText(userText);
}

void QtOSGViewer::SetTextSize(double size)
{
_posgWidget->SetHUDTextSize(size);
}

bool QtOSGViewer::LoadModel(const string& filename)
{
if( filename == "") {
Expand Down
3 changes: 3 additions & 0 deletions plugins/qtosgrave/qtosgviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class QtOSGViewer : public QMainWindow, public ViewerBase
/// \brief Set User-defined text to be displayed in the viewer window
virtual void SetUserText(const string& userText);

/// \brief Set size of text to be displayed in the viewer window
virtual void SetTextSize(double size);

/// \brief notified when a body has been removed from the environment
virtual void RemoveKinBody(KinBodyPtr pbody) {
if( !!pbody ) {
Expand Down
4 changes: 4 additions & 0 deletions python/bindings/openravepy_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ class PyViewerBase : public PyInterfaceBase
void SetUserText(const std::string &userText) {
_pviewer->SetUserText(userText);
}
void SetTextSize(const double size) {
_pviewer->SetTextSize(size);
}
std::string GetName() {
return _pviewer->GetName();
}
Expand Down Expand Up @@ -386,6 +389,7 @@ void init_openravepy_viewer()
.def("Show",&PyViewerBase::Show, PY_ARGS("showtype") DOXY_FN(ViewerBase,Show))
.def("SetTitle",&PyViewerBase::SetName, PY_ARGS("title") DOXY_FN(ViewerBase,SetName))
.def("SetUserText",&PyViewerBase::SetUserText, PY_ARGS("userText") DOXY_FN(ViewerBase,SetUserText))
.def("SetTextSize",&PyViewerBase::SetTextSize, PY_ARGS("size") DOXY_FN(ViewerBase,SetTextSize))
.def("SetName",&PyViewerBase::SetName, PY_ARGS("title") DOXY_FN(ViewerBase,SetName))
.def("GetName",&PyViewerBase::GetName, DOXY_FN(ViewerBase,GetName))
.def("RegisterCallback",&PyViewerBase::RegisterCallback, PY_ARGS("properties", "callback") DOXY_FN(ViewerBase,RegisterItemSelectionCallback))
Expand Down
Loading

0 comments on commit 87e757b

Please sign in to comment.