MIRA
PolygonObject.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_POLYGONOBJECT_H_
48 #define _MIRA_POLYGONOBJECT_H_
49 
51 
52 #include <geometry/Polygon.h>
53 
54 #ifndef Q_MOC_RUN
55 #include <OGRE/OgreColourValue.h>
56 #include <OGRE/OgreSceneManager.h>
57 #include <OGRE/OgreSceneNode.h>
58 #include <OGRE/OgreManualObject.h>
59 #include <OGRE/OgreMaterialManager.h>
60 #endif
61 
62 namespace mira {
63 
65 
66 template <typename T>
68 {
69 public:
72  typedef boost::geometry::model::ring<Point2> Polygon2;
73  typedef boost::geometry::model::ring<Point3> Polygon3;
74 
75 public:
76  PolygonObject(Ogre::SceneManager* sceneManager, Ogre::SceneNode* parent = NULL);
77  virtual ~PolygonObject();
78 
79  // required implementation of VisualizationObject::setColor()
80  virtual void setColor(const Ogre::ColourValue& color);
81 
82  virtual void setOutlineColor(const Ogre::ColourValue& color);
83 
84  // filling is only guaranteed to work correctly for convex flat polygons!
85  virtual void setFillColor(const Ogre::ColourValue& color);
86 
87  void setPolygon(const Polygon2& polygon);
88  void setPolygon(const Polygon3& polygon);
89 
90  void setPolygons(const std::vector<Polygon2>& polygons);
91  void setPolygons(const std::vector<Polygon3>& polygons);
92 
93 protected:
94  void setupPolygons();
95 
96  Ogre::ManualObject* mOutlineObject;
97  Ogre::ManualObject* mAreaObject;
98  Ogre::ColourValue mOutlineColor;
99  Ogre::ColourValue mFillColor;
100 
101  std::vector<Polygon3> mPolygons;
102 };
103 
105 
106 template <typename T>
107 PolygonObject<T>::PolygonObject(Ogre::SceneManager* sceneManager, Ogre::SceneNode* parent) :
108  VisualizationObject(sceneManager, parent),
109  mOutlineColor(Ogre::ColourValue::Black), mFillColor(0.f, 0.f, 0.f, 0.f)
110 {
111  mOutlineObject = mSceneManager->createManualObject("PolygonObjectOutline"+ toString(this));
112  mOutlineObject->setCastShadows(false);
113  mNode->attachObject(mOutlineObject);
114  mAreaObject = mSceneManager->createManualObject("PolygonObjectArea"+ toString(this));
115  mAreaObject->setCastShadows(false);
116  mNode->attachObject(mAreaObject);
119  setupPolygons();
120 }
121 
122 template <typename T>
124 {
125  mNode->detachObject(mOutlineObject);
126  mSceneManager->destroyManualObject(mOutlineObject);
127  mNode->detachObject(mAreaObject);
128  mSceneManager->destroyManualObject(mAreaObject);
129 }
130 
131 template <typename T>
132 void PolygonObject<T>::setColor(const Ogre::ColourValue& color)
133 {
134  setOutlineColor(color);
135 }
136 
137 template <typename T>
138 void PolygonObject<T>::setOutlineColor(const Ogre::ColourValue& color)
139 {
140  mOutlineColor = color;
141  setupPolygons();
142 }
143 
144 template <typename T>
145 void PolygonObject<T>::setFillColor(const Ogre::ColourValue& color)
146 {
147  mFillColor = color;
148  setupPolygons();
149 }
150 
151 template <typename T>
153 {
154  Polygon2 p = polygon;
155  boost::geometry::correct(p);
156 
157  mPolygons.clear();
158  mPolygons.push_back(Polygon3());
159 
160  for (std::size_t i = 0; i < p.size(); ++i)
161  mPolygons.back().push_back(Point3(p[i].x(), p[i].y(), 0));
162 
163  setupPolygons();
164 }
165 
166 template <typename T>
168 {
169  Polygon3 p = polygon;
170  // TODO correct 3D polygon (close it) since the following won't compile
171  //boost::geometry::correct(p);
172 
173  mPolygons.clear();
174  mPolygons.push_back(p);
175 
176  setupPolygons();
177 }
178 
179 template <typename T>
180 void PolygonObject<T>::setPolygons(const std::vector<Polygon2>& polygons)
181 {
182  mPolygons.clear();
183  foreach(const Polygon2& polygon, polygons)
184  {
185  Polygon2 p = polygon;
186  boost::geometry::correct(p);
187 
188  mPolygons.push_back(Polygon3());
189  for (std::size_t i = 0; i < p.size(); ++i)
190  mPolygons.back().push_back(Point3(p[i].x(), p[i].y(), 0));
191  }
192 
193  setupPolygons();
194 }
195 
196 template <typename T>
197 void PolygonObject<T>::setPolygons(const std::vector<Polygon3>& polygons)
198 {
199  mPolygons = polygons;
200  setupPolygons();
201 }
202 
203 template <typename T>
205 {
206  std::size_t count=0;
207  foreach(const Polygon3& polygon, mPolygons)
208  count += polygon.size();
209 
210  mOutlineObject->clear();
211  mOutlineObject->estimateVertexCount(count * 2);
212 
213  mOutlineObject->begin("TransparentNoLight", Ogre::RenderOperation::OT_LINE_LIST);
214 
215  foreach(const Polygon3& polygon, mPolygons)
216  {
217  for (std::size_t i = 1; i < polygon.size(); ++i)
218  {
219  const Point3& a = polygon[i-1];
220  const Point3& b = polygon[i];
221 
222  mOutlineObject->position(Ogre::Vector3(a.x(), a.y(), a.z()));
223  mOutlineObject->colour(mOutlineColor);
224  mOutlineObject->position(Ogre::Vector3(b.x(), b.y(), b.z()));
225  mOutlineObject->colour(mOutlineColor);
226  }
227  }
228 
229  mOutlineObject->end();
230 
231  mAreaObject->clear();
232 
233  foreach(const Polygon3& polygon, mPolygons)
234  {
235  mAreaObject->begin("TransparentNoLightTwoSided",
236  Ogre::RenderOperation::OT_TRIANGLE_FAN);
237  for (std::size_t i = 0; i < polygon.size(); ++i)
238  {
239  const Point3& p = polygon[i];
240  mAreaObject->position(Ogre::Vector3(p.x(), p.y(), p.z()));
241  mAreaObject->colour(mFillColor);
242  }
243  mAreaObject->end();
244  }
245 }
246 
248 
249 }
250 
251 #endif
virtual void setColor(const Ogre::ColourValue &color)
Definition: PolygonObject.h:132
Ogre::ManualObject * mOutlineObject
Definition: PolygonObject.h:96
Ogre::ColourValue mOutlineColor
Definition: PolygonObject.h:98
void setupPolygons()
Definition: PolygonObject.h:204
Point< T, 3 > Point3
Definition: PolygonObject.h:71
Declaration of VisualizationObject.
Definition: PolygonObject.h:67
void setPolygon(const Polygon2 &polygon)
Definition: PolygonObject.h:152
Point< T, 2 > Point2
Definition: PolygonObject.h:70
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::vector< Polygon3 > mPolygons
Definition: PolygonObject.h:101
Definition: VisualizationObject.h:71
virtual ~PolygonObject()
Definition: PolygonObject.h:123
Specialization of Point for 2 dimensions with specialized constructors and converters.
Definition: Point.h:169
Specialization of Point for 3 dimensions with specialized constructors and converters.
Definition: Point.h:252
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
const RGB Black(0.0f, 0.0f, 0.0f)
Ogre::SceneNode * mNode
Definition: VisualizationObject.h:99
void setPolygons(const std::vector< Polygon2 > &polygons)
Definition: PolygonObject.h:180
virtual void setOutlineColor(const Ogre::ColourValue &color)
Definition: PolygonObject.h:138
Ogre::SceneManager * mSceneManager
Definition: VisualizationObject.h:98
virtual void setFillColor(const Ogre::ColourValue &color)
Definition: PolygonObject.h:145
Simple Wrapper for Boost::geometry polygon.
boost::geometry::model::ring< Point3 > Polygon3
Definition: PolygonObject.h:73
Definition: ImageObject.h:60
PolygonObject(Ogre::SceneManager *sceneManager, Ogre::SceneNode *parent=NULL)
Definition: PolygonObject.h:107
Ogre::ManualObject * mAreaObject
Definition: PolygonObject.h:97
Non intrusive reflect for OGRE color class.
boost::geometry::model::ring< Point2 > Polygon2
Definition: PolygonObject.h:72
Ogre::ColourValue mFillColor
Definition: PolygonObject.h:99