MRPT  2.0.5
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-2020, 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 
15 
16 #include <mrpt/opengl/opengl_api.h>
17 #include <algorithm>
18 
19 using namespace mrpt;
20 using namespace mrpt::opengl;
21 using namespace mrpt::poses;
22 using namespace mrpt::math;
23 using namespace std;
24 
27 
29 
30 void CSetOfObjects::clear() { m_objects.clear(); }
31 
33 {
34  // Do nothing:
35 }
36 
37 void CSetOfObjects::render(const RenderContext& rc) const
38 {
39  // Do nothing: the enqueForRenderRecursive() does the actual job.
40 }
41 
43  const mrpt::opengl::TRenderMatrices& state, RenderQueue& rq) const
44 {
45  mrpt::opengl::enqueForRendering(m_objects, state, rq);
46 }
47 
48 uint8_t CSetOfObjects::serializeGetVersion() const { return 0; }
50 {
51  writeToStreamRender(out);
52 
53  out.WriteAs<uint32_t>(m_objects.size());
54  for (const auto& m_object : m_objects) out << *m_object;
55 }
56 
57 /*---------------------------------------------------------------
58  Implements the reading from a CStream capability of
59  CSerializable objects
60  ---------------------------------------------------------------*/
62  mrpt::serialization::CArchive& in, uint8_t version)
63 {
64  switch (version)
65  {
66  case 0:
67  {
68  readFromStreamRender(in);
69 
70  uint32_t n;
71  in >> n;
72  clear();
73  m_objects.resize(n);
74 
75  for_each(
76  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
77  }
78  break;
79  default:
81  };
82 }
83 
85 {
86  ASSERTMSG_(
87  newObject.get() != this,
88  "Error: Trying to insert container into itself!");
89  m_objects.push_back(newObject);
90 }
91 
92 /*--------------------------------------------------------------
93  dumpListOfObjects
94  ---------------------------------------------------------------*/
95 void CSetOfObjects::dumpListOfObjects(std::vector<std::string>& lst)
96 {
97  for (auto& m_object : m_objects)
98  {
99  // Single obj:
100  string s(m_object->GetRuntimeClass()->className);
101  if (m_object->m_name.size())
102  s += string(" (") + m_object->m_name + string(")");
103  lst.emplace_back(s);
104 
105  if (m_object->GetRuntimeClass() ==
107  {
108  auto* objs = dynamic_cast<CSetOfObjects*>(m_object.get());
109 
110  std::vector<std::string> auxLst;
111  objs->dumpListOfObjects(auxLst);
112  for (const auto& i : auxLst) lst.emplace_back(string(" ") + i);
113  }
114  }
115 }
116 
117 /*--------------------------------------------------------------
118  removeObject
119  ---------------------------------------------------------------*/
121 {
122  for (auto it = m_objects.begin(); it != m_objects.end(); ++it)
123  if (*it == obj)
124  {
125  m_objects.erase(it);
126  return;
127  }
128  else if (
129  (*it)->GetRuntimeClass() ==
131  dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj);
132 }
133 
134 bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
135 {
136  CPose3D nueva = (CPose3D() - this->m_pose) + o;
137  bool found = false;
138  double tmp;
139  for (const auto& m_object : m_objects)
140  if (m_object->traceRay(nueva, tmp))
141  {
142  if (!found)
143  {
144  found = true;
145  dist = tmp;
146  }
147  else if (tmp < dist)
148  dist = tmp;
149  }
150  return found;
151 }
152 
154 {
155  public:
156  uint8_t r, g, b, a;
157  void operator()(CRenderizable::Ptr& p) { p->setColor_u8(r, g, b, a); }
158  FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
159  : r(R), g(G), b(B), a(A)
160  {
161  }
162  ~FSetColor() = default;
163 };
164 
166 {
167  for_each(
168  m_objects.begin(), m_objects.end(),
169  FSetColor(
170  m_color.R = c.R, m_color.G = c.G, m_color.B = c.B,
171  m_color.A = c.A));
172  return *this;
173 }
174 
176 {
177  return find(m_objects.begin(), m_objects.end(), obj) != m_objects.end();
178 }
179 
181 {
182  for (auto& m_object : m_objects) m_object->setColorR_u8(m_color.R = r);
183  return *this;
184 }
185 
187 {
188  for (auto& m_object : m_objects) m_object->setColorG_u8(m_color.G = g);
189  return *this;
190 }
191 
193 {
194  for (auto& m_object : m_objects) m_object->setColorB_u8(m_color.B = b);
195  return *this;
196 }
197 
199 {
200  for (auto& m_object : m_objects) m_object->setColorA_u8(m_color.A = a);
201  return *this;
202 }
203 
204 /*---------------------------------------------------------------
205  getByName
206  ---------------------------------------------------------------*/
208 {
209  for (auto& m_object : m_objects)
210  {
211  if (m_object->m_name == str)
212  return m_object;
213  else if (
214  m_object->GetRuntimeClass() ==
216  {
217  CRenderizable::Ptr ret =
218  dynamic_cast<CSetOfObjects*>(m_object.get())->getByName(str);
219  if (ret) return ret;
220  }
221  }
222  return CRenderizable::Ptr();
223 }
224 
225 /** Evaluates the bounding box of this object (including possible children) in
226  * the coordinate frame of the object parent. */
229 {
230  bb_min = TPoint3D(
231  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
232  std::numeric_limits<double>::max());
233  bb_max = TPoint3D(
234  -std::numeric_limits<double>::max(),
235  -std::numeric_limits<double>::max(),
236  -std::numeric_limits<double>::max());
237 
238  for (const auto& m_object : m_objects)
239  {
240  TPoint3D child_bbmin(
241  std::numeric_limits<double>::max(),
242  std::numeric_limits<double>::max(),
243  std::numeric_limits<double>::max());
244  TPoint3D child_bbmax(
245  -std::numeric_limits<double>::max(),
246  -std::numeric_limits<double>::max(),
247  -std::numeric_limits<double>::max());
248  m_object->getBoundingBox(child_bbmin, child_bbmax);
249 
250  keep_min(bb_min.x, child_bbmin.x);
251  keep_min(bb_min.y, child_bbmin.y);
252  keep_min(bb_min.z, child_bbmin.z);
253 
254  keep_max(bb_max.x, child_bbmax.x);
255  keep_max(bb_max.y, child_bbmax.y);
256  keep_max(bb_max.z, child_bbmax.z);
257  }
258 
259  // Convert to coordinates of my parent:
260  m_pose.composePoint(bb_min, bb_min);
261  m_pose.composePoint(bb_max, bb_max);
262 }
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...
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
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
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:48
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:50
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
STL namespace.
uint8_t B
Definition: TColor.h:51
uint8_t G
Definition: TColor.h:51
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].
CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
Context for calls to render()
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].
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
void enqueForRendering(const mrpt::opengl::CListOpenGLObjects &objs, const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq)
Processes, recursively, all objects in the list, classifying them by shader programs into a list suit...
Definition: RenderQueue.cpp:28
virtual CRenderizable & setColorG_u8(const uint8_t g)
Color components in the range [0,255].
TPoint3D_< double > TPoint3D
Lightweight 3D point.
Definition: TPoint3D.h:268
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
Rendering state related to the projection and model-view matrices.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
uint8_t R
Definition: TColor.h:51
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
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
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
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
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
void enqueForRenderRecursive(const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq) const override
FSetColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:25
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.
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)
uint8_t A
Definition: TColor.h:51
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...
std::map< shader_id_t, std::multimap< float, RenderQueueElement > > RenderQueue
A queue for rendering, sorted by shader program to minimize changes of OpenGL shader programs while r...
Definition: RenderQueue.h:46



Page generated by Doxygen 1.8.14 for MRPT 2.0.5 Git: fc2d69e97 Fri Jul 10 18:20:57 2020 +0200 at vie jul 10 18:30:13 CEST 2020