Main MRPT website > C++ reference for MRPT 1.9.9
CSetOfObjects.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
14 #include <mrpt/utils/CStringList.h>
15 #include <mrpt/utils/CStream.h>
16 #include <mrpt/opengl/gl_utils.h>
17 
18 #include "opengl_internals.h"
19 #include <algorithm>
20 
21 using namespace mrpt;
22 using namespace mrpt::opengl;
23 using namespace mrpt::poses;
24 using namespace mrpt::utils;
25 using namespace mrpt::math;
26 using namespace std;
27 
29 using namespace mrpt::utils::metaprogramming;
30 
32 
33 /*---------------------------------------------------------------
34  render
35  ---------------------------------------------------------------*/
37 {
38  m_objects.clear(); // clear the list and delete objects (if there are no
39  // more copies out there!)
40 }
41 
42 /*---------------------------------------------------------------
43  render
44  ---------------------------------------------------------------*/
46 {
47  // Render all the objects:
49 }
50 
51 /*---------------------------------------------------------------
52  Implements the writing to a CStream capability of
53  CSerializable objects
54  ---------------------------------------------------------------*/
55 void CSetOfObjects::writeToStream(mrpt::utils::CStream& out, int* version) const
56 {
57  if (version)
58  *version = 0;
59  else
60  {
61  writeToStreamRender(out);
62 
63  uint32_t n;
64  n = (uint32_t)m_objects.size();
65  out << n;
66  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
67  it != m_objects.end(); ++it)
68  out << **it;
69  }
70 }
71 
72 /*---------------------------------------------------------------
73  Implements the reading from a CStream capability of
74  CSerializable objects
75  ---------------------------------------------------------------*/
77 {
78  switch (version)
79  {
80  case 0:
81  {
82  readFromStreamRender(in);
83 
84  uint32_t n;
85  in >> n;
86  clear();
87  m_objects.resize(n);
88 
89  for_each(
90  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
91  }
92  break;
93  default:
95  };
96 }
97 
98 /*---------------------------------------------------------------
99  initializeAllTextures
100  ---------------------------------------------------------------*/
102 {
103 #if MRPT_HAS_OPENGL_GLUT
105  for (it = m_objects.begin(); it != m_objects.end(); ++it++)
106  {
107  if (IS_DERIVED(*it, CTexturedObject))
108  getAs<CTexturedObject>(*it)->loadTextureInOpenGL();
109  else if (IS_CLASS(*it, CSetOfObjects))
110  getAs<CSetOfObjects>(*it)->initializeAllTextures();
111  }
112 #endif
113 }
114 
118 {
119  ASSERTMSG_(
120  newObject.get() != this,
121  "Error: Trying to insert container into itself!");
122  m_objects.push_back(newObject);
123 }
124 
125 /*--------------------------------------------------------------
126  dumpListOfObjects
127  ---------------------------------------------------------------*/
129 {
130  for (CListOpenGLObjects::iterator it = m_objects.begin();
131  it != m_objects.end(); ++it)
132  {
133  // Single obj:
134  string s((*it)->GetRuntimeClass()->className);
135  if ((*it)->m_name.size())
136  s += string(" (") + (*it)->m_name + string(")");
137  lst.add(s);
138 
139  if ((*it)->GetRuntimeClass() ==
141  {
142  CSetOfObjects* objs = getAs<CSetOfObjects>(*it);
143 
144  utils::CStringList auxLst;
145  objs->dumpListOfObjects(auxLst);
146  for (size_t i = 0; i < auxLst.size(); i++)
147  lst.add(string(" ") + auxLst(i));
148  }
149  }
150 }
151 
152 /*--------------------------------------------------------------
153  removeObject
154  ---------------------------------------------------------------*/
156 {
157  for (CListOpenGLObjects::iterator it = m_objects.begin();
158  it != m_objects.end(); ++it)
159  if (*it == obj)
160  {
161  m_objects.erase(it);
162  return;
163  }
164  else if (
165  (*it)->GetRuntimeClass() ==
167  getAs<CSetOfObjects>(*it)->removeObject(obj);
168 }
169 
170 bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
171 {
172  CPose3D nueva = (CPose3D() - this->m_pose) + o;
173  bool found = false;
174  double tmp;
175  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
176  it != m_objects.end(); ++it)
177  if ((*it)->traceRay(nueva, tmp))
178  {
179  if (!found)
180  {
181  found = true;
182  dist = tmp;
183  }
184  else if (tmp < dist)
185  dist = tmp;
186  }
187  return found;
188 }
189 
191 {
192  public:
193  uint8_t r, g, b, a;
194  void operator()(CRenderizable::Ptr& p) { p->setColor_u8(r, g, b, a); }
196  : r(R), g(G), b(B), a(A)
197  {
198  }
200 };
201 
203 {
204  for_each(
205  m_objects.begin(), m_objects.end(),
206  FSetColor(
207  m_color.R = c.R, m_color.G = c.G, m_color.B = c.B,
208  m_color.A = c.A));
209  return *this;
210 }
211 
213 {
214  return find(m_objects.begin(), m_objects.end(), obj) != m_objects.end();
215 }
216 
218 {
219  for (CListOpenGLObjects::iterator it = m_objects.begin();
220  it != m_objects.end(); ++it)
221  (*it)->setColorR_u8(m_color.R = r);
222  return *this;
223 }
224 
226 {
227  for (CListOpenGLObjects::iterator it = m_objects.begin();
228  it != m_objects.end(); ++it)
229  (*it)->setColorG_u8(m_color.G = g);
230  return *this;
231 }
232 
234 {
235  for (CListOpenGLObjects::iterator it = m_objects.begin();
236  it != m_objects.end(); ++it)
237  (*it)->setColorB_u8(m_color.B = b);
238  return *this;
239 }
240 
242 {
243  for (CListOpenGLObjects::iterator it = m_objects.begin();
244  it != m_objects.end(); ++it)
245  (*it)->setColorA_u8(m_color.A = a);
246  return *this;
247 }
248 
249 /*---------------------------------------------------------------
250  getByName
251  ---------------------------------------------------------------*/
253 {
254  for (CListOpenGLObjects::iterator it = m_objects.begin();
255  it != m_objects.end(); ++it)
256  {
257  if ((*it)->m_name == str)
258  return *it;
259  else if (
260  (*it)->GetRuntimeClass() ==
262  {
263  CRenderizable::Ptr ret = getAs<CSetOfObjects>(*it)->getByName(str);
264  if (ret) return ret;
265  }
266  }
267  return CRenderizable::Ptr();
268 }
269 
270 /** Evaluates the bounding box of this object (including possible children) in
271  * the coordinate frame of the object parent. */
273  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
274 {
275  bb_min = TPoint3D(
276  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
277  std::numeric_limits<double>::max());
278  bb_max = TPoint3D(
279  -std::numeric_limits<double>::max(),
280  -std::numeric_limits<double>::max(),
281  -std::numeric_limits<double>::max());
282 
283  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
284  it != m_objects.end(); ++it)
285  {
286  TPoint3D child_bbmin(
287  std::numeric_limits<double>::max(),
288  std::numeric_limits<double>::max(),
289  std::numeric_limits<double>::max());
290  TPoint3D child_bbmax(
291  -std::numeric_limits<double>::max(),
292  -std::numeric_limits<double>::max(),
293  -std::numeric_limits<double>::max());
294  (*it)->getBoundingBox(child_bbmin, child_bbmax);
295 
296  keep_min(bb_min.x, child_bbmin.x);
297  keep_min(bb_min.y, child_bbmin.y);
298  keep_min(bb_min.z, child_bbmin.z);
299 
300  keep_max(bb_max.x, child_bbmax.x);
301  keep_max(bb_max.y, child_bbmax.y);
302  keep_max(bb_max.z, child_bbmax.z);
303  }
304 
305  // Convert to coordinates of my parent:
306  m_pose.composePoint(bb_min, bb_min);
307  m_pose.composePoint(bb_max, bb_max);
308 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
A base class for all OpenGL objects with loadable textures.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:30
CSetOfObjects()
Default constructor.
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
virtual ~CSetOfObjects()
Private, virtual destructor: only can be deleted from smart pointers.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
bool contains(const CRenderizable::Ptr &obj) const
void render() const override
Render child objects.
GLenum GLsizei n
Definition: glext.h:5074
Scalar * iterator
Definition: eigen_plugins.h:26
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:220
virtual CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
GLdouble s
Definition: glext.h:3676
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
virtual CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
unsigned char uint8_t
Definition: rptypes.h:41
#define IS_DERIVED(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is an i...
Definition: CObject.h:109
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A class for storing a list of text lines.
Definition: CStringList.h:32
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
virtual CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
virtual CRenderizable & setColorG_u8(const uint8_t g)
Color components in the range [0,255].
void dumpListOfObjects(mrpt::utils::CStringList &lst)
Retrieves a list of all objects in text form.
const GLubyte * c
Definition: glext.h:6313
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLubyte g
Definition: glext.h:6279
A RGB color - 8bit.
Definition: TColor.h:25
GLubyte GLubyte b
Definition: glext.h:6279
An object for reading objects from a stream, intended for being used in STL algorithms.
A set of utility objects for metaprogramming with STL algorithms.
double x
X,Y,Z coordinates.
GLsizei const GLchar ** string
Definition: glext.h:4101
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
Definition: bits.h:227
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
size_t size() const
Returns the number of text lines in the list.
void removeObject(const CRenderizable::Ptr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:88
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual CRenderizable & setColor_u8(const mrpt::utils::TColor &c) override
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
Definition: bits.h:220
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
Lightweight 3D point.
void renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:32
unsigned __int32 uint32_t
Definition: rptypes.h:47
CRenderizable::Ptr getByName(const std::string &str)
Returns the first object with a given name, or a nullptr pointer if not found.
#define ASSERTMSG_(f, __ERROR_MSG)
void insert(const CRenderizable::Ptr &newObject)
Insert a new object to the list.
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
virtual CRenderizable & setColorB_u8(const uint8_t b) override
Color components in the range [0,255].
void operator()(CRenderizable::Ptr &p)
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
void add(const std::string &str)
Appends a new string at the end of the string list.
Definition: CStringList.cpp:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019