MRPT  1.9.9
CSetOfObjects.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
14 #include <mrpt/opengl/gl_utils.h>
16 
17 #include <algorithm>
18 #include "opengl_internals.h"
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 
50 uint8_t CSetOfObjects::serializeGetVersion() const { return 0; }
52 {
53  writeToStreamRender(out);
54 
55  out.WriteAs<uint32_t>(m_objects.size());
56  for (const auto& m_object : m_objects) out << *m_object;
57 }
58 
59 /*---------------------------------------------------------------
60  Implements the reading from a CStream capability of
61  CSerializable objects
62  ---------------------------------------------------------------*/
64  mrpt::serialization::CArchive& in, uint8_t version)
65 {
66  switch (version)
67  {
68  case 0:
69  {
70  readFromStreamRender(in);
71 
72  uint32_t n;
73  in >> n;
74  clear();
75  m_objects.resize(n);
76 
77  for_each(
78  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
79  }
80  break;
81  default:
83  };
84 }
85 
86 /*---------------------------------------------------------------
87  initializeAllTextures
88  ---------------------------------------------------------------*/
90 {
91 #if MRPT_HAS_OPENGL_GLUT
92  CListOpenGLObjects::iterator it;
93  for (auto& obj : m_objects)
94  {
96  dynamic_cast<CTexturedObject&>(*obj).loadTextureInOpenGL();
97  else if (IS_CLASS(*obj, CSetOfObjects))
98  dynamic_cast<CSetOfObjects&>(*obj).initializeAllTextures();
99  }
100 #endif
101 }
102 
103 CSetOfObjects::CSetOfObjects() = default;
106 {
107  ASSERTMSG_(
108  newObject.get() != this,
109  "Error: Trying to insert container into itself!");
110  m_objects.push_back(newObject);
111 }
112 
113 /*--------------------------------------------------------------
114  dumpListOfObjects
115  ---------------------------------------------------------------*/
116 void CSetOfObjects::dumpListOfObjects(std::vector<std::string>& lst)
117 {
118  for (auto& m_object : m_objects)
119  {
120  // Single obj:
121  string s(m_object->GetRuntimeClass()->className);
122  if (m_object->m_name.size())
123  s += string(" (") + m_object->m_name + string(")");
124  lst.emplace_back(s);
125 
126  if (m_object->GetRuntimeClass() ==
128  {
129  auto* objs = dynamic_cast<CSetOfObjects*>(m_object.get());
130 
131  std::vector<std::string> auxLst;
132  objs->dumpListOfObjects(auxLst);
133  for (const auto& i : auxLst) lst.emplace_back(string(" ") + i);
134  }
135  }
136 }
137 
138 /*--------------------------------------------------------------
139  removeObject
140  ---------------------------------------------------------------*/
142 {
143  for (auto it = m_objects.begin(); it != m_objects.end(); ++it)
144  if (*it == obj)
145  {
146  m_objects.erase(it);
147  return;
148  }
149  else if (
150  (*it)->GetRuntimeClass() ==
152  dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj);
153 }
154 
155 bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
156 {
157  CPose3D nueva = (CPose3D() - this->m_pose) + o;
158  bool found = false;
159  double tmp;
160  for (const auto& m_object : m_objects)
161  if (m_object->traceRay(nueva, tmp))
162  {
163  if (!found)
164  {
165  found = true;
166  dist = tmp;
167  }
168  else if (tmp < dist)
169  dist = tmp;
170  }
171  return found;
172 }
173 
175 {
176  public:
177  uint8_t r, g, b, a;
178  void operator()(CRenderizable::Ptr& p) { p->setColor_u8(r, g, b, a); }
179  FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
180  : r(R), g(G), b(B), a(A)
181  {
182  }
183  ~FSetColor() = default;
184 };
185 
187 {
188  for_each(
189  m_objects.begin(), m_objects.end(),
190  FSetColor(
191  m_color.R = c.R, m_color.G = c.G, m_color.B = c.B,
192  m_color.A = c.A));
193  return *this;
194 }
195 
197 {
198  return find(m_objects.begin(), m_objects.end(), obj) != m_objects.end();
199 }
200 
202 {
203  for (auto& m_object : m_objects) m_object->setColorR_u8(m_color.R = r);
204  return *this;
205 }
206 
208 {
209  for (auto& m_object : m_objects) m_object->setColorG_u8(m_color.G = g);
210  return *this;
211 }
212 
214 {
215  for (auto& m_object : m_objects) m_object->setColorB_u8(m_color.B = b);
216  return *this;
217 }
218 
220 {
221  for (auto& m_object : m_objects) m_object->setColorA_u8(m_color.A = a);
222  return *this;
223 }
224 
225 /*---------------------------------------------------------------
226  getByName
227  ---------------------------------------------------------------*/
229 {
230  for (auto& m_object : m_objects)
231  {
232  if (m_object->m_name == str)
233  return m_object;
234  else if (
235  m_object->GetRuntimeClass() ==
237  {
238  CRenderizable::Ptr ret =
239  dynamic_cast<CSetOfObjects*>(m_object.get())->getByName(str);
240  if (ret) return ret;
241  }
242  }
243  return CRenderizable::Ptr();
244 }
245 
246 /** Evaluates the bounding box of this object (including possible children) in
247  * the coordinate frame of the object parent. */
250 {
251  bb_min = TPoint3D(
252  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
253  std::numeric_limits<double>::max());
254  bb_max = TPoint3D(
255  -std::numeric_limits<double>::max(),
256  -std::numeric_limits<double>::max(),
257  -std::numeric_limits<double>::max());
258 
259  for (const auto& m_object : m_objects)
260  {
261  TPoint3D child_bbmin(
262  std::numeric_limits<double>::max(),
263  std::numeric_limits<double>::max(),
264  std::numeric_limits<double>::max());
265  TPoint3D child_bbmax(
266  -std::numeric_limits<double>::max(),
267  -std::numeric_limits<double>::max(),
268  -std::numeric_limits<double>::max());
269  m_object->getBoundingBox(child_bbmin, child_bbmax);
270 
271  keep_min(bb_min.x, child_bbmin.x);
272  keep_min(bb_min.y, child_bbmin.y);
273  keep_min(bb_min.z, child_bbmin.z);
274 
275  keep_max(bb_max.x, child_bbmax.x);
276  keep_max(bb_max.y, child_bbmax.y);
277  keep_max(bb_max.z, child_bbmax.z);
278  }
279 
280  // Convert to coordinates of my parent:
281  m_pose.composePoint(bb_min, bb_min);
282  m_pose.composePoint(bb_max, bb_max);
283 }
An object for reading objects from a stream, intended for being used in STL algorithms.
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...
double x
X,Y,Z coordinates.
Definition: TPoint3D.h:83
A base class for all OpenGL objects with loadable textures.
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:224
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:26
CSetOfObjects()
Default constructor.
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to 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:5136
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:40
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:42
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
STL namespace.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
GLdouble s
Definition: glext.h:3682
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
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.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
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].
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:6406
~CSetOfObjects() override
Private, virtual destructor: only can be deleted from smart pointers.
#define IS_DERIVED(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is an instance of the given ...
Definition: CObject.h:151
GLubyte g
Definition: glext.h:6372
GLubyte GLubyte b
Definition: glext.h:6372
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
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:4116
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).
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...
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:105
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:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
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:85
mrpt::vision::TStereoCalibResults out
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:20
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
Lightweight 3D point.
Definition: TPoint3D.h:90
void renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:35
void loadTextureInOpenGL() const
VERY IMPORTANT: If you use a multi-thread application, you MUST call this from the same thread that w...
CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
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:6372
GLfloat GLfloat p
Definition: glext.h:6398
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:183
void operator()(CRenderizable::Ptr &p)
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: 479715d5b Tue Nov 12 07:26:21 2019 +0100 at mar nov 12 07:30:12 CET 2019