MIRA
Visualization2DBasic.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_VISUALIZATION2DBASIC_H_
48 #define _MIRA_VISUALIZATION2DBASIC_H_
49 
50 #include <QGraphicsScene>
51 #include <QGraphicsItem>
52 
54 
55 #include <widgets/QtUtils.h>
56 
57 namespace mira {
58 
60 
72 template <typename T>
74 {
75 public:
76 
80  Visualization2DBasic(const std::string channelDisplayName = "Channel") :
81  mChannelDisplayName(channelDisplayName),
82  mItem(NULL) {
83  mDataChannel.setDataChangedCallback(
84  boost::bind(&Visualization2DBasic::dataChangedCallback, this, _1));
85  mDataChannel.setChannelChangedCallback(
87  }
88 
90  delete mItem;
91  }
92 
93  template <typename Reflector>
94  void reflect(Reflector& r) {
96  channelProperty(r, mChannelDisplayName.c_str(), mDataChannel, "The channel to be visualized");
97  }
98 
99 public:
100 
101  virtual void setupScene(IVisualization2DSite* site) {
102  QGraphicsScene* mgr = site->getSceneManager();
103  mItem = setupScene(mgr);
104  }
105 
110  virtual QGraphicsItem* setupScene(QGraphicsScene* mgr) = 0;
111 
112  virtual QGraphicsItem* getItem() {
113  return mItem;
114  }
115 
116 public:
117 
118  void setEnabled(bool enabled) {
120  if (mItem != NULL)
121  mItem->setVisible(enabled);
122  }
123 
124 public:
125 
128  }
129 
130 protected:
131 
132  virtual void update(Duration dt) {
133  if (!mDataChannel.isValid() || mDataChannel.getDataUpdateCount() == 0)
134  return;
135  if (mDataFrameID.empty())
136  MIRA_THROW(TransformerBase::XTransform, "Data has no frame for transforming it into the views camera frame");
137 
139  try {
140  d = getAuthority().template getTransform<RigidTransform2f>(
144  }
145  catch(...) { // fall back to non interpolating versions
146  d = getAuthority().template getTransform<RigidTransform2f>(
149  getSite()->getFixedFrame());
150  }
152  }
153 
155  mDataFrameID = data->frameID;
156  mDataTimestamp = data->timestamp;
157  dataChanged(data);
158  }
159 
161  mDataFrameID = "";
162  try {
163  this->setName(mDataChannel.getID());
164  }
165  catch (XRuntime&) {}
166  channelChanged();
167  }
168 
172  virtual void channelChanged() {}
173 
177  virtual void dataChanged(ChannelRead<T> data) {
178  onDataChanged(data); // only for backward compatibility
179  }
180 
184  virtual void onDataChanged(ChannelRead<T> data) {}
185 
186 protected:
187 
189  std::string mChannelDisplayName;
190 
191  std::string mDataFrameID;
193 
194  QGraphicsItem* mItem;
195 };
196 
198 
199 }
200 
201 #endif
void reflect(Reflector &r)
Definition: Visualization2DBasic.h:94
void setName(const std::string &name)
Sets the name of this visualization.
Definition: Visualization.h:157
void setEnabled(bool enabled)
Changes the enabled-state and therefore the visibility of this visualization.
Definition: Visualization2DBasic.h:118
virtual QGraphicsScene * getSceneManager()=0
std::string mDataFrameID
Definition: Visualization2DBasic.h:191
virtual ~Visualization2DBasic()
Definition: Visualization2DBasic.h:89
virtual Authority & getAuthority()
Return the authority as reference.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
virtual const std::string & getFixedFrame() const =0
virtual IVisualization2DSite * getSite()
Returns a pointer to the IVisualization2DSite interface that was set in the init() method...
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
void channelProperty(Reflector &r, const std::string &name, ChannelProperty< T > &channel, const std::string &comment, OptionalMode optional=REQUIRED)
Special property-method that should be called within the reflect method to specify a ChannelProperty...
Definition: Visualization.h:249
void channelChangedCallback()
Definition: Visualization2DBasic.h:160
Abstract base class for all 2D visualization that are used by Visualization2DView.
Definition: Visualization2D.h:120
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:494
virtual void update(Duration dt)
This method can be implemented in the derived Visualization classes.
Definition: Visualization2DBasic.h:132
Declaration of helper functions related to Qt.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
The data structure that is returned by getDataConnection.
Definition: Visualization.h:183
virtual const std::string & getCameraFrame() const =0
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
void dataChangedCallback(ChannelRead< T > data)
Definition: Visualization2DBasic.h:154
QGraphicsItem * mItem
Definition: Visualization2DBasic.h:194
virtual void setupScene(IVisualization2DSite *site)
Must be implemented in derived classes.
Definition: Visualization2DBasic.h:101
Use this class to represent time durations.
Definition: Time.h:106
virtual void setEnabled(bool enabled)
Changes the enabled-state and therefore the visibility of this visualization.
Definition: Visualization.h:149
ChannelProperty< T > mDataChannel
Definition: Visualization2DBasic.h:188
std::string mChannelDisplayName
Definition: Visualization2DBasic.h:189
Inherit from this class if you want to implement a simple 2D visualization.
Definition: Visualization2DBasic.h:73
1D linear interpolator.
Definition: LinearInterpolator.h:137
Definition: Visualization2D.h:64
Time mDataTimestamp
Definition: Visualization2DBasic.h:192
virtual DataConnection getDataConnection()
Returns information about the direct data connection for this visualization.
Definition: Visualization2DBasic.h:126
The concrete typed ChannelProperty template class.
Definition: ChannelProperty.h:214
MIRA_GUI_WIDGETS_EXPORT void setTransform(QGraphicsItem *item, const RigidTransform2f &t)
Applies a transform (translation and rotation) to a graphics scene item.
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
Declaration of Visualization2D and related classes.
virtual void channelChanged()
Overwrite this method to get notified when the connected channel changes.
Definition: Visualization2DBasic.h:172
Visualization2DBasic(const std::string channelDisplayName="Channel")
Constructor taking a name of the visualized channel.
Definition: Visualization2DBasic.h:80
virtual void dataChanged(ChannelRead< T > data)
Overwrite this method to listen on channel callbacks.
Definition: Visualization2DBasic.h:177
virtual QGraphicsItem * getItem()
Must be implemented in derived classes to return a pointer to the QGraphicsItem used for drawing...
Definition: Visualization2DBasic.h:112
virtual void onDataChanged(ChannelRead< T > data)
Definition: Visualization2DBasic.h:184