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