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-2018, 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 
15 #include <mrpt/opengl/gl_utils.h>
16 
17 #include "opengl_internals.h"
18 #include <algorithm>
19 
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::poses;
23 
24 using namespace mrpt::math;
25 using namespace std;
26 
29 
31 
32 /*---------------------------------------------------------------
33  render
34  ---------------------------------------------------------------*/
36 {
37  m_objects.clear(); // clear the list and delete objects (if there are no
38  // more copies out there!)
39 }
40 
41 /*---------------------------------------------------------------
42  render
43  ---------------------------------------------------------------*/
45 {
46  // Render all the objects:
48 }
49 
52 {
53  writeToStreamRender(out);
54 
55  out.WriteAs<uint32_t>(m_objects.size());
56  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
57  it != m_objects.end(); ++it)
58  out << **it;
59 }
60 
61 /*---------------------------------------------------------------
62  Implements the reading from a CStream capability of
63  CSerializable objects
64  ---------------------------------------------------------------*/
67 {
68  switch (version)
69  {
70  case 0:
71  {
72  readFromStreamRender(in);
73 
74  uint32_t n;
75  in >> n;
76  clear();
77  m_objects.resize(n);
78 
79  for_each(
80  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
81  }
82  break;
83  default:
85  };
86 }
87 
88 /*---------------------------------------------------------------
89  initializeAllTextures
90  ---------------------------------------------------------------*/
92 {
93 #if MRPT_HAS_OPENGL_GLUT
95  for (it = m_objects.begin(); it != m_objects.end(); ++it++)
96  {
97  if (IS_DERIVED(*it, CTexturedObject))
98  dynamic_cast<CTexturedObject*>(it->get())->loadTextureInOpenGL();
99  else if (IS_CLASS(*it, CSetOfObjects))
100  dynamic_cast<CSetOfObjects*>(it->get())->initializeAllTextures();
101  }
102 #endif
103 }
104 
108 {
109  ASSERTMSG_(
110  newObject.get() != this,
111  "Error: Trying to insert container into itself!");
112  m_objects.push_back(newObject);
113 }
114 
115 /*--------------------------------------------------------------
116  dumpListOfObjects
117  ---------------------------------------------------------------*/
118 void CSetOfObjects::dumpListOfObjects(std::vector<std::string>& lst)
119 {
120  for (CListOpenGLObjects::iterator it = m_objects.begin();
121  it != m_objects.end(); ++it)
122  {
123  // Single obj:
124  string s((*it)->GetRuntimeClass()->className);
125  if ((*it)->m_name.size())
126  s += string(" (") + (*it)->m_name + string(")");
127  lst.emplace_back(s);
128 
129  if ((*it)->GetRuntimeClass() ==
131  {
132  CSetOfObjects* objs = dynamic_cast<CSetOfObjects*>(it->get());
133 
134  std::vector<std::string> auxLst;
135  objs->dumpListOfObjects(auxLst);
136  for (size_t i = 0; i < auxLst.size(); i++)
137  lst.emplace_back(string(" ") + auxLst[i]);
138  }
139  }
140 }
141 
142 /*--------------------------------------------------------------
143  removeObject
144  ---------------------------------------------------------------*/
146 {
147  for (CListOpenGLObjects::iterator it = m_objects.begin();
148  it != m_objects.end(); ++it)
149  if (*it == obj)
150  {
151  m_objects.erase(it);
152  return;
153  }
154  else if (
155  (*it)->GetRuntimeClass() ==
157  dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj);
158 }
159 
160 bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
161 {
162  CPose3D nueva = (CPose3D() - this->m_pose) + o;
163  bool found = false;
164  double tmp;
165  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
166  it != m_objects.end(); ++it)
167  if ((*it)->traceRay(nueva, tmp))
168  {
169  if (!found)
170  {
171  found = true;
172  dist = tmp;
173  }
174  else if (tmp < dist)
175  dist = tmp;
176  }
177  return found;
178 }
179 
181 {
182  public:
183  uint8_t r, g, b, a;
184  void operator()(CRenderizable::Ptr& p) { p->setColor_u8(r, g, b, a); }
186  : r(R), g(G), b(B), a(A)
187  {
188  }
190 };
191 
193 {
194  for_each(
195  m_objects.begin(), m_objects.end(),
196  FSetColor(
197  m_color.R = c.R, m_color.G = c.G, m_color.B = c.B,
198  m_color.A = c.A));
199  return *this;
200 }
201 
203 {
204  return find(m_objects.begin(), m_objects.end(), obj) != m_objects.end();
205 }
206 
208 {
209  for (CListOpenGLObjects::iterator it = m_objects.begin();
210  it != m_objects.end(); ++it)
211  (*it)->setColorR_u8(m_color.R = r);
212  return *this;
213 }
214 
216 {
217  for (CListOpenGLObjects::iterator it = m_objects.begin();
218  it != m_objects.end(); ++it)
219  (*it)->setColorG_u8(m_color.G = g);
220  return *this;
221 }
222 
224 {
225  for (CListOpenGLObjects::iterator it = m_objects.begin();
226  it != m_objects.end(); ++it)
227  (*it)->setColorB_u8(m_color.B = b);
228  return *this;
229 }
230 
232 {
233  for (CListOpenGLObjects::iterator it = m_objects.begin();
234  it != m_objects.end(); ++it)
235  (*it)->setColorA_u8(m_color.A = a);
236  return *this;
237 }
238 
239 /*---------------------------------------------------------------
240  getByName
241  ---------------------------------------------------------------*/
243 {
244  for (CListOpenGLObjects::iterator it = m_objects.begin();
245  it != m_objects.end(); ++it)
246  {
247  if ((*it)->m_name == str)
248  return *it;
249  else if (
250  (*it)->GetRuntimeClass() ==
252  {
253  CRenderizable::Ptr ret =
254  dynamic_cast<CSetOfObjects*>(it->get())->getByName(str);
255  if (ret) return ret;
256  }
257  }
258  return CRenderizable::Ptr();
259 }
260 
261 /** Evaluates the bounding box of this object (including possible children) in
262  * the coordinate frame of the object parent. */
264  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
265 {
266  bb_min = TPoint3D(
267  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
268  std::numeric_limits<double>::max());
269  bb_max = TPoint3D(
270  -std::numeric_limits<double>::max(),
271  -std::numeric_limits<double>::max(),
272  -std::numeric_limits<double>::max());
273 
274  for (CListOpenGLObjects::const_iterator it = m_objects.begin();
275  it != m_objects.end(); ++it)
276  {
277  TPoint3D child_bbmin(
278  std::numeric_limits<double>::max(),
279  std::numeric_limits<double>::max(),
280  std::numeric_limits<double>::max());
281  TPoint3D child_bbmax(
282  -std::numeric_limits<double>::max(),
283  -std::numeric_limits<double>::max(),
284  -std::numeric_limits<double>::max());
285  (*it)->getBoundingBox(child_bbmin, child_bbmax);
286 
287  keep_min(bb_min.x, child_bbmin.x);
288  keep_min(bb_min.y, child_bbmin.y);
289  keep_min(bb_min.z, child_bbmin.z);
290 
291  keep_max(bb_max.x, child_bbmax.x);
292  keep_max(bb_max.y, child_bbmax.y);
293  keep_max(bb_max.z, child_bbmax.z);
294  }
295 
296  // Convert to coordinates of my parent:
297  m_pose.composePoint(bb_min, bb_min);
298  m_pose.composePoint(bb_max, bb_max);
299 }
An object for reading objects from a stream, intended for being used in STL algorithms.
Scalar * iterator
Definition: eigen_plugins.h:26
A base class for all OpenGL objects with loadable textures.
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:219
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:28
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.
const double G
bool contains(const CRenderizable::Ptr &obj) const
void render() const override
Render child objects.
GLenum GLsizei n
Definition: glext.h:5074
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.
void WriteAs(const TYPE_FROM_ACTUAL &value)
Definition: CArchive.h:152
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...
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
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
virtual CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
unsigned char uint8_t
Definition: rptypes.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
#define IS_DERIVED(ptrObj, class_name)
Evaluates to true if a pointer to an object (derived from mrpt::rtti::CObject) is an instance of the ...
Definition: CObject.h:109
This base provides a set of functions for maths stuff.
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
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...
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].
const GLubyte * c
Definition: glext.h:6313
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
double x
X,Y,Z coordinates.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
GLsizei const GLchar ** string
Definition: glext.h:4101
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
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 base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
const float R
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
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
A RGB color - 8bit.
Definition: TColor.h:22
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:35
virtual CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
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.
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 clear()
Clear the contents of this container.
Definition: ts_hash_map.h:188
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019