32 intensity(m_intensity),
33 validRange(m_validRange)
39 uint8_t CObservation2DRangeScan::serializeGetVersion()
const {
return 7; }
40 void CObservation2DRangeScan::serializeTo(
44 out << aperture << rightToLeft << maxRange << sensorPose;
47 ASSERT_(validRange.size() == scan.size());
51 out.
WriteBuffer(&m_validRange[0],
sizeof(m_validRange[0]) * N);
61 out << hasIntensity();
65 void CObservation2DRangeScan::truncateByDistanceAndAngle(
66 float min_distance,
float max_angle,
float min_height,
float max_height,
72 unsigned int nPts = scan.
size();
75 auto itValid = m_validRange.begin();
76 for (
auto itScan = m_scan.begin(); itScan != m_scan.end(); itScan++, itValid++, k++)
78 float ang = fabs(k * aperture / nPts - aperture * 0.5);
79 float x = (*itScan) * cos(ang);
81 if (min_height != 0 || max_height != 0)
83 ASSERT_(max_height > min_height);
84 if (*itScan < min_distance || ang > max_angle ||
85 x > h - min_height ||
x < h - max_height)
88 else if (*itScan < min_distance || ang > max_angle)
93 void CObservation2DRangeScan::serializeFrom(
104 in >> aperture >> rightToLeft >> maxRange >> sensorPose >>
111 if (N)
in.ReadBufferFixEndianness(&m_scan[0], N);
117 &m_validRange[0],
sizeof(m_validRange[0]) * N);
122 for (i = 0; i < N; i++) m_validRange[i] = scan[i] < maxRange;
155 in >> aperture >> rightToLeft >> maxRange >> sensorPose;
164 in.ReadBufferFixEndianness(&m_scan[0], N);
165 in.ReadBuffer(&m_validRange[0],
sizeof(m_validRange[0]) * N);
168 in.ReadBufferFixEndianness(×tamp, 1);
185 setScanHasIntensity(hasIntensity);
186 if (hasIntensity && N)
188 in.ReadBufferFixEndianness(&m_intensity[0], N);
208 mxArray* CObservation2DRangeScan::writeToMatlab()
const
211 const char* fields[] = {
"class",
214 "scan",
"validRange",
219 "stdError",
"beamAperture",
223 mexplus::MxArray obs_struct(
224 mexplus::MxArray::Struct(
sizeof(fields) /
sizeof(fields[0]), fields));
226 obs_struct.set(
"class", this->GetRuntimeClass()->className);
228 obs_struct.set(
"ts", this->timestamp);
229 obs_struct.set(
"sensorLabel", this->sensorLabel);
231 obs_struct.set(
"scan", this->m_scan);
235 obs_struct.set(
"validRange", this->m_validRange);
236 obs_struct.set(
"intensity", this->m_intensity);
237 obs_struct.set(
"aperture", this->aperture);
238 obs_struct.set(
"rightToLeft", this->rightToLeft);
239 obs_struct.set(
"maxRange", this->maxRange);
240 obs_struct.set(
"stdError", this->stdError);
241 obs_struct.set(
"beamAperture", this->beamAperture);
242 obs_struct.set(
"deltaPitch", this->deltaPitch);
243 obs_struct.set(
"pose", this->sensorPose);
245 return obs_struct.release();
254 bool CObservation2DRangeScan::isPlanarScan(
const double tolerance)
const
256 return sensorPose.isHorizontal(tolerance);
259 bool CObservation2DRangeScan::hasIntensity()
const {
return m_has_intensity; }
260 void CObservation2DRangeScan::setScanHasIntensity(
bool setHasIntensityFlag)
262 m_has_intensity = setHasIntensityFlag;
268 void CObservation2DRangeScan::filterByExclusionAreas(
271 if (areas.empty())
return;
276 size_t sizeRangeScan = scan.size();
278 ASSERT_(scan.size() == validRange.size());
280 if (!sizeRangeScan)
return;
284 Ang = -0.5 * aperture;
285 dA = aperture / (sizeRangeScan - 1);
289 Ang = +0.5 * aperture;
290 dA = -aperture / (sizeRangeScan - 1);
293 auto valid_it = m_validRange.begin();
294 for (
auto scan_it = m_scan.begin(); scan_it != m_scan.end(); scan_it++, valid_it++)
303 const double Lx = *scan_it * cos(Ang);
304 const double Ly = *scan_it * sin(Ang);
309 this->sensorPose.composePoint(Lx, Ly, 0, Gx, Gy, Gz);
313 i != areas.end(); ++i)
315 if (i->first.PointIntoPolygon(Gx, Gy) &&
316 (Gz >= i->second.first && Gz <= i->second.second))
330 void CObservation2DRangeScan::filterByExclusionAreas(
331 const std::vector<mrpt::math::CPolygon>& areas)
333 if (areas.empty())
return;
336 for (
size_t i = 0; i < areas.size(); i++)
338 TListExclusionAreasWithRanges::value_type dat;
339 dat.first = areas[i];
340 dat.second.first = -std::numeric_limits<double>::max();
341 dat.second.second = std::numeric_limits<double>::max();
345 filterByExclusionAreas(lst);
351 void CObservation2DRangeScan::filterByExclusionAngles(
352 const std::vector<std::pair<double, double>>& angles)
354 if (angles.empty())
return;
359 const size_t sizeRangeScan = scan.size();
361 ASSERT_(scan.size() == validRange.size());
363 if (!sizeRangeScan)
return;
367 Ang = -0.5 * aperture;
368 dA = aperture / (sizeRangeScan - 1);
372 Ang = +0.5 * aperture;
373 dA = -aperture / (sizeRangeScan - 1);
377 for (vector<pair<double, double>>::
const_iterator itA = angles.begin();
378 itA != angles.end(); ++itA)
384 if (ap_idx_ini < 0) ap_idx_ini = 0;
385 if (ap_idx_end < 0) ap_idx_end = 0;
387 if (ap_idx_ini > (
int)sizeRangeScan) ap_idx_ini = sizeRangeScan - 1;
388 if (ap_idx_end > (
int)sizeRangeScan) ap_idx_end = sizeRangeScan - 1;
390 const size_t idx_ini = ap_idx_ini;
391 const size_t idx_end = ap_idx_end;
393 if (idx_end >= idx_ini)
395 for (
size_t i = idx_ini; i <= idx_end; i++) m_validRange[i] =
false;
399 for (
size_t i = 0; i < idx_end; i++) m_validRange[i] =
false;
401 for (
size_t i = idx_ini; i < sizeRangeScan; i++)
402 m_validRange[i] =
false;
432 void CObservation2DRangeScan::internal_buildAuxPointsMap(
433 const void* options)
const
436 throw std::runtime_error(
437 "[CObservation2DRangeScan::buildAuxPointsMap] ERROR: This function "
438 "needs linking against mrpt-maps.\n");
440 (*ptr_internal_build_points_map_from_scan2D)(*
this, m_cachedMap, options);
446 p.nRays = this->scan.size();
447 p.aperture = this->aperture;
448 p.rightToLeft = this->rightToLeft;
454 if (
a.nRays !=
b.nRays)
return a.nRays <
b.nRays;
455 if (
a.aperture !=
b.aperture)
return a.aperture <
b.aperture;
456 if (
a.rightToLeft !=
b.rightToLeft)
return a.rightToLeft;
460 void CObservation2DRangeScan::getDescriptionAsText(std::ostream& o)
const
462 CObservation::getDescriptionAsText(o);
463 o <<
"Homogeneous matrix for the sensor's 3D pose, relative to robot "
465 o << sensorPose.getHomogeneousMatrixVal<
CMatrixDouble44>() << sensorPose
469 "Samples direction: %s\n",
470 (rightToLeft) ?
"Right->Left" :
"Left->Right");
471 o <<
format(
"Points in the scan: %u\n", (
unsigned)scan.size());
472 o <<
format(
"Estimated sensor 'sigma': %f\n", stdError);
474 "Increment in pitch during the scan: %f deg\n",
RAD2DEG(deltaPitch));
477 for (i = 0; i < scan.size(); i++)
478 if (!validRange[i]) inval++;
479 o <<
format(
"Invalid points in the scan: %u\n", (
unsigned)inval);
481 o <<
format(
"Sensor maximum range: %.02f m\n", maxRange);
483 "Sensor field-of-view (\"aperture\"): %.01f deg\n",
RAD2DEG(aperture));
485 o <<
"Raw scan values: [";
486 for (i = 0; i < scan.size(); i++) o <<
format(
"%.03f ", scan[i]);
489 o <<
"Raw valid-scan values: [";
490 for (i = 0; i < validRange.size(); i++)
491 o <<
format(
"%u ", validRange[i] ? 1 : 0);
496 o <<
"Raw intensity values: [";
497 for (i = 0; i < m_intensity.size(); i++)
498 o <<
format(
"%d ", m_intensity[i]);
503 float CObservation2DRangeScan::getScanRange(
const size_t i)
const
509 void CObservation2DRangeScan::setScanRange(
const size_t i,
const float val)
515 int CObservation2DRangeScan::getScanIntensity(
const size_t i)
const
518 return m_intensity[i];
520 void CObservation2DRangeScan::setScanIntensity(
const size_t i,
const int val)
523 m_intensity[i] =
val;
526 bool CObservation2DRangeScan::getScanRangeValidity(
const size_t i)
const
529 return m_validRange[i] != 0;
531 void CObservation2DRangeScan::setScanRangeValidity(
532 const size_t i,
const bool val)
535 m_validRange[i] =
val ? 1 : 0;
538 void CObservation2DRangeScan::resizeScan(
const size_t len)
541 m_intensity.resize(
len);
542 m_validRange.resize(
len);
545 void CObservation2DRangeScan::resizeScanAndAssign(
546 const size_t len,
const float rangeVal,
const bool rangeValidity,
549 m_scan.assign(
len, rangeVal);
550 m_validRange.assign(
len, rangeValidity);
551 m_intensity.assign(
len, rangeIntensity);
554 size_t CObservation2DRangeScan::getScanSize()
const {
return m_scan.size(); }
555 void CObservation2DRangeScan::loadFromVectors(
556 size_t nRays,
const float* scanRanges,
const char* scanValidity)
561 for (
size_t i = 0; i < nRays; i++)
563 m_scan[i] = scanRanges[i];
564 m_validRange[i] = scanValidity[i];