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 }
n
GLenum GLsizei n
Definition: glext.h:5074
mrpt::keep_min
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: core/include/mrpt/core/bits_math.h:124
FSetColor::r
uint8_t r
Definition: CSetOfObjects.cpp:183
mrpt::opengl::CSetOfObjects::dumpListOfObjects
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form
Definition: CSetOfObjects.cpp:118
mrpt::containers::clear
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:188
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::opengl::CRenderizable::setColorG_u8
virtual CRenderizable & setColorG_u8(const uint8_t g)
Color components in the range [0,255].
Definition: CRenderizable.h:193
s
GLdouble s
Definition: glext.h:3676
mrpt::serialization::metaprogramming::ObjectReadFromStream
An object for reading objects from a stream, intended for being used in STL algorithms.
Definition: metaprogramming_serialization.h:24
G
const double G
Definition: vision_stereo_rectify/test.cpp:31
mrpt::opengl::CSetOfObjects::contains
bool contains(const CRenderizable::Ptr &obj) const
Definition: CSetOfObjects.cpp:202
IS_DERIVED
#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
mrpt::opengl::CSetOfObjects::removeObject
void removeObject(const CRenderizable::Ptr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
Definition: CSetOfObjects.cpp:145
CSetOfObjects.h
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
c
const GLubyte * c
Definition: glext.h:6313
mrpt::opengl::CSetOfObjects::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CSetOfObjects.cpp:65
mrpt::math::TPoint3D::z
double z
Definition: lightweight_geom_data.h:385
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::opengl::CSetOfObjects::setColorG_u8
virtual CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
Definition: CSetOfObjects.cpp:215
mrpt::opengl::CRenderizable::setColorA_u8
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
Definition: CRenderizable.h:205
mrpt::containers::find
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:219
mrpt::opengl::CRenderizable::setColorB_u8
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
Definition: CRenderizable.h:199
mrpt::opengl::CSetOfObjects::getByName
CRenderizable::Ptr getByName(const std::string &str)
Returns the first object with a given name, or a nullptr pointer if not found.
Definition: CSetOfObjects.cpp:242
FSetColor::FSetColor
FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
Definition: CSetOfObjects.cpp:185
mrpt::opengl::CSetOfObjects::insert
void insert(const CRenderizable::Ptr &newObject)
Insert a new object to the list.
Definition: CSetOfObjects.cpp:107
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
g
GLubyte g
Definition: glext.h:6279
mrpt::opengl::CRenderizable::setColorR_u8
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
Definition: CRenderizable.h:187
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
R
const float R
Definition: CKinematicChain.cpp:138
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::opengl::CSetOfObjects::setColorB_u8
virtual CRenderizable & setColorB_u8(const uint8_t b) override
Color components in the range [0,255].
Definition: CSetOfObjects.cpp:223
mrpt::opengl::CSetOfObjects::getBoundingBox
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...
Definition: CSetOfObjects.cpp:263
FSetColor
Definition: CSetOfObjects.cpp:180
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::opengl::gl_utils::renderSetOfObjects
void renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:35
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
FSetColor::operator()
void operator()(CRenderizable::Ptr &p)
Definition: CSetOfObjects.cpp:184
mrpt::opengl::CSetOfObjects
A set of object, which are referenced to the coordinates framework established in this object.
Definition: CSetOfObjects.h:28
mrpt::serialization::CArchive::WriteAs
void WriteAs(const TYPE_FROM_ACTUAL &value)
Definition: CArchive.h:152
mrpt::opengl::CSetOfObjects::setColorA_u8
virtual CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
Definition: CSetOfObjects.cpp:231
FSetColor::~FSetColor
~FSetColor()
Definition: CSetOfObjects.cpp:189
mrpt::keep_max
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: core/include/mrpt/core/bits_math.h:131
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
b
GLubyte GLubyte b
Definition: glext.h:6279
IS_CLASS
#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
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
CLASS_ID_NAMESPACE
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:88
mrpt::opengl::CTexturedObject
A base class for all OpenGL objects with loadable textures.
Definition: CTexturedObject.h:25
mrpt::opengl::CSetOfObjects::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CSetOfObjects.cpp:50
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
mrpt::opengl::CSetOfObjects::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CSetOfObjects.cpp:51
metaprogramming_serialization.h
mrpt::opengl::CSetOfObjects::traceRay
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
Definition: CSetOfObjects.cpp:160
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
mrpt::opengl::CSetOfObjects::render
void render() const override
Render child objects.
Definition: CSetOfObjects.cpp:44
gl_utils.h
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
opengl-precomp.h
mrpt::math::TPoint3D::y
double y
Definition: lightweight_geom_data.h:385
opengl_internals.h
void
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
mrpt::opengl::CSetOfObjects::~CSetOfObjects
virtual ~CSetOfObjects()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CSetOfObjects.cpp:106
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::opengl::CSetOfObjects::initializeAllTextures
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
Definition: CSetOfObjects.cpp:91
mrpt::serialization::metaprogramming
Definition: metaprogramming_serialization.h:17
in
GLuint in
Definition: glext.h:7274
string
GLsizei const GLchar ** string
Definition: glext.h:4101
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mrpt::opengl::CSetOfObjects::setColorR_u8
virtual CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
Definition: CSetOfObjects.cpp:207
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
mrpt::opengl::CSetOfObjects::setColor_u8
virtual CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
Definition: CSetOfObjects.cpp:192
mrpt::opengl::CSetOfObjects::CSetOfObjects
CSetOfObjects()
Default constructor.
Definition: CSetOfObjects.cpp:105
CTexturedPlane.h
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST