Main MRPT website > C++ reference for MRPT 1.5.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 
12 
15 #include <mrpt/utils/CStringList.h>
16 #include <mrpt/utils/CStream.h>
17 #include <mrpt/opengl/gl_utils.h>
18 
19 #include "opengl_internals.h"
20 #include <algorithm>
21 
22 using namespace mrpt;
23 using namespace mrpt::opengl;
24 using namespace mrpt::poses;
25 using namespace mrpt::utils;
26 using namespace mrpt::math;
27 using namespace std;
28 
30 using namespace mrpt::utils::metaprogramming;
31 
33 
34 /*---------------------------------------------------------------
35  render
36  ---------------------------------------------------------------*/
38 {
39  m_objects.clear(); // clear the list and delete objects (if there are no 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  ---------------------------------------------------------------*/
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();it!=m_objects.end();++it)
67  out << **it;
68  }
69 }
70 
71 /*---------------------------------------------------------------
72  Implements the reading from a CStream capability of
73  CSerializable objects
74  ---------------------------------------------------------------*/
76 {
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(m_objects.begin(),m_objects.end(), ObjectReadFromStream(&in) );
90 
91  } break;
92  default:
94 
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 
115 
117 {
118 }
119 
120 
122 {
123  clear();
124 }
125 
126 
127 void CSetOfObjects::insert( const CRenderizablePtr &newObject )
128 {
129  ASSERTMSG_(newObject.pointer() != this, "Error: Trying to insert container into itself!");
130  m_objects.push_back(newObject);
131 }
132 
133 
134 
135 
136 /*--------------------------------------------------------------
137  dumpListOfObjects
138  ---------------------------------------------------------------*/
140 {
141  for (CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it)
142  {
143  // Single obj:
144  string s( (*it)->GetRuntimeClass()->className );
145  if ((*it)->m_name.size())
146  s+= string(" (") +(*it)->m_name + string(")");
147  lst.add( s );
148 
149  if ((*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl))
150  {
151  CSetOfObjects *objs = getAs<CSetOfObjects>(*it);
152 
153  utils::CStringList auxLst;
154  objs->dumpListOfObjects(auxLst);
155  for (size_t i=0;i<auxLst.size();i++)
156  lst.add( string(" ")+auxLst(i) );
157  }
158  }
159 }
160 
161 /*--------------------------------------------------------------
162  removeObject
163  ---------------------------------------------------------------*/
164 void CSetOfObjects::removeObject( const CRenderizablePtr &obj )
165 {
166  for (CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it)
167  if (it->pointer() == obj.pointer())
168  {
169  m_objects.erase(it);
170  return;
171  }
172  else if ( (*it)->GetRuntimeClass()==CLASS_ID_NAMESPACE(CSetOfObjects,opengl) )
173  getAs<CSetOfObjects>(*it)->removeObject(obj);
174 }
175 
176 bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D &o,double &dist) const {
177  CPose3D nueva=(CPose3D()-this->m_pose)+o;
178  bool found=false;
179  double tmp;
180  for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it) if ((*it)->traceRay(nueva,tmp)) {
181  if (!found) {
182  found=true;
183  dist=tmp;
184  } else if (tmp<dist) dist=tmp;
185  }
186  return found;
187 }
188 
189 class FSetColor {
190 public:
192  void operator()(CRenderizablePtr &p) {
193  p->setColor_u8(r,g,b,a);
194  }
195  FSetColor(uint8_t R,uint8_t G,uint8_t B,uint8_t A):r(R),g(G),b(B),a(A) {}
197 };
198 
200  for_each(m_objects.begin(),m_objects.end(),FSetColor(m_color.R=c.R,m_color.G=c.G,m_color.B=c.B,m_color.A=c.A));
201  return *this;
202 }
203 
204 
205 bool CSetOfObjects::contains(const CRenderizablePtr &obj) const {
206  return find(m_objects.begin(),m_objects.end(),obj)!=m_objects.end();
207 }
208 
210  for(CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it) (*it)->setColorR_u8(m_color.R=r);
211  return *this;
212 }
213 
215  for(CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it) (*it)->setColorG_u8(m_color.G=g);
216  return *this;
217 }
218 
220  for(CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it) (*it)->setColorB_u8(m_color.B=b);
221  return *this;
222 }
223 
225  for(CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it) (*it)->setColorA_u8(m_color.A=a);
226  return *this;
227 }
228 
229 
230 /*---------------------------------------------------------------
231  getByName
232  ---------------------------------------------------------------*/
233 CRenderizablePtr CSetOfObjects::getByName( const string &str )
234 {
235  for (CListOpenGLObjects::iterator it=m_objects.begin();it!=m_objects.end();++it)
236  {
237  if ((*it)->m_name == str)
238  return *it;
239  else if ( (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,opengl))
240  {
241  CRenderizablePtr ret = getAs<CSetOfObjects>(*it)->getByName(str);
242  if (ret.present())
243  return ret;
244  }
245  }
246  return CRenderizablePtr();
247 }
248 
249 
250 /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
252 {
253  bb_min = TPoint3D( std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max() );
254  bb_max = TPoint3D(-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max() );
255 
256  for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it)
257  {
258  TPoint3D child_bbmin( std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max() );
259  TPoint3D child_bbmax(-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max() );
260  (*it)->getBoundingBox(child_bbmin, child_bbmax);
261 
262  keep_min(bb_min.x, child_bbmin.x);
263  keep_min(bb_min.y, child_bbmin.y);
264  keep_min(bb_min.z, child_bbmin.z);
265 
266  keep_max(bb_max.x, child_bbmax.x);
267  keep_max(bb_max.y, child_bbmax.y);
268  keep_max(bb_max.z, child_bbmax.z);
269  }
270 
271  // Convert to coordinates of my parent:
272  m_pose.composePoint(bb_min, bb_min);
273  m_pose.composePoint(bb_max, bb_max);
274 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
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:32
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.
GLenum GLsizei n
Definition: glext.h:4618
Scalar * iterator
Definition: eigen_plugins.h:23
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
virtual CRenderizable & setColorA_u8(const uint8_t a) MRPT_OVERRIDE
Color components in the range [0,255].
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:24
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:139
double z
X,Y,Z coordinates.
virtual CRenderizable & setColor_u8(const mrpt::utils::TColor &c) MRPT_OVERRIDE
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
GLdouble s
Definition: glext.h:3602
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
unsigned char uint8_t
Definition: rptypes.h:43
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
#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:96
virtual CRenderizable & setColorB_u8(const uint8_t b) MRPT_OVERRIDE
Color components in the range [0,255].
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
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 & 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:5590
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
bool contains(const CRenderizablePtr &obj) const
GLubyte g
Definition: glext.h:5575
A RGB color - 8bit.
Definition: TColor.h:26
GLubyte GLubyte b
Definition: glext.h:5575
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.
int version
Definition: mrpt_jpeglib.h:898
void render() const MRPT_OVERRIDE
Render child objects.
virtual CRenderizable & setColorG_u8(const uint8_t g) MRPT_OVERRIDE
Color components in the range [0,255].
GLsizei const GLchar ** string
Definition: glext.h:3919
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:176
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
void insert(const CRenderizablePtr &newObject)
Insert a new object to the list.
size_t size() const
Returns the number of text lines in the list.
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Access to runtime class ID for a defined class name.
Definition: CObject.h:86
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void removeObject(const CRenderizablePtr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
virtual CRenderizable & setColorR_u8(const uint8_t r) MRPT_OVERRIDE
Color components in the range [0,255].
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
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:171
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Simulation of ray-trace, given a pose.
#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:93
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
CRenderizablePtr getByName(const std::string &str)
Returns the first object with a given name, or a NULL pointer if not found.
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
Lightweight 3D point.
void OPENGL_IMPEXP renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:33
unsigned __int32 uint32_t
Definition: rptypes.h:49
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
GLfloat GLfloat p
Definition: glext.h:5587
void operator()(CRenderizablePtr &p)
void add(const std::string &str)
Appends a new string at the end of the string list.
Definition: CStringList.cpp:49



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020