perception_interfaces 1.1.2
Loading...
Searching...
No Matches
convenience_state_getters.h
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: MIT
3
9#pragma once
10
14
15#include <algorithm>
16#include <cmath>
17
18namespace perception_msgs {
19
20namespace object_access {
21
22// --- full state/covariance -------------------------------------------------
23
30inline std::vector<double> getContinuousState(const ObjectState& state) {
32 return state.continuous_state;
33}
34
42template <typename T>
43inline std::vector<double> getContinuousState(const T& obj) {
44 return getContinuousState(obj.state);
45}
46
53inline std::vector<long int> getDiscreteState(const ObjectState& state) {
55 return state.discrete_state;
56}
57
65template <typename T>
66inline std::vector<long int> getDiscreteState(const T& obj) {
67 return getDiscreteState(obj.state);
68}
69
76inline std::vector<double> getContinuousStateCovariance(const ObjectState& state) {
78 return state.continuous_state_covariance;
79}
80
88template <typename T>
89inline std::vector<double> getContinuousStateCovariance(const T& obj) {
90 return getContinuousStateCovariance(obj.state);
91}
92
101inline double getContinuousStateCovarianceAt(const ObjectState& state, const unsigned int i, const unsigned int j) {
102 const int n = getContinuousStateSize(state);
103 const std::vector<double> covariance = getContinuousStateCovariance(state);
104 return covariance[n * i + j];
105}
106
116template <typename T>
117inline double getContinuousStateCovarianceAt(const T& obj, const unsigned int i, const unsigned int j) {
118 return getContinuousStateCovarianceAt(obj.state, i, j);
119}
120
127inline std::vector<double> getContinuousStateCovarianceDiagonal(const ObjectState& state) {
128 const int n = getContinuousStateSize(state);
129 std::vector<double> diagonal(n);
130 for (int i = 0; i < n; i++) diagonal[i] = getContinuousStateCovarianceAt(state, i, i);
131 return diagonal;
132}
133
141template <typename T>
142inline std::vector<double> getContinuousStateCovarianceDiagonal(const T& obj) {
143 return getContinuousStateCovarianceDiagonal(obj.state);
144}
145
146// --- vector quantities -----------------------------------------------------
147
154inline gm::Point getPosition(const ObjectState& state) {
155 gm::Point position;
156 position.x = getX(state);
157 position.y = getY(state);
158 position.z = getZ(state);
159 return position;
160}
161
169template <typename T>
170inline gm::Point getPosition(const T& obj) {
171 return getPosition(obj.state);
172}
173
180inline gm::Quaternion getOrientation(const ObjectState& state) {
181 tf2::Quaternion q;
182 double roll{0}, pitch{0}, yaw{0};
183 if (hasRoll(state.model_id)) roll = getRoll(state);
184 if (hasPitch(state.model_id)) pitch = getPitch(state);
185 if (hasYaw(state.model_id)) yaw = getYaw(state);
186 q.setRPY(roll, pitch, yaw);
187 return tf2::toMsg(q);
188}
189
197template <typename T>
198inline gm::Quaternion getOrientation(const T& obj) {
199 return getOrientation(obj.state);
200}
201
208inline gm::Pose getPose(const ObjectState& state) {
209 gm::Pose pose;
210 pose.position = getPosition(state);
211 pose.orientation = getOrientation(state);
212 return pose;
213}
214
221inline gm::Point getCenterPosition(const ObjectState& state) {
222 gm::Point position = getPosition(state);
223 const auto orientation = getOrientation(state);
224 const gm::Vector3 offset_to_center = state.reference_point.translation_to_geometric_center;
225 tf2::Quaternion q;
226 tf2::fromMsg(orientation, q);
227 const tf2::Vector3 offset_to_center_tf2(offset_to_center.x, offset_to_center.y, offset_to_center.z);
228 const tf2::Vector3 rotated_offset_to_center = tf2::quatRotate(q, offset_to_center_tf2);
229 position.x += rotated_offset_to_center.x();
230 position.y += rotated_offset_to_center.y();
231 position.z += rotated_offset_to_center.z();
232 return position;
233}
234
242template <typename T>
243inline gm::Point getCenterPosition(const T& object) {
244 return getCenterPosition(object.state);
245}
246
254template <typename T>
255inline gm::Pose getPose(const T& obj) {
256 return getPose(obj.state);
257}
258
265inline std::vector<double> getPoseCovariance(const ObjectState& state) {
266 const int n = 6;
267 const int model_id = state.model_id;
268 std::vector<double> pose_covariance(n * n, 0.0);
269 int ix, jx;
270 for (int i = 0; i < n; i++) {
271 for (int j = 0; j < n; j++) {
272 if (i == 0 && hasX(model_id))
273 ix = indexX(model_id);
274 else if (i == 1 && hasY(model_id))
275 ix = indexY(model_id);
276 else if (i == 2 && hasZ(model_id))
277 ix = indexZ(model_id);
278 else if (i == 3 && hasRoll(model_id))
279 ix = indexRoll(model_id);
280 else if (i == 4 && hasPitch(model_id))
281 ix = indexPitch(model_id);
282 else if (i == 5 && hasYaw(model_id))
283 ix = indexYaw(model_id);
284 else
285 continue;
286
287 if (j == 0 && hasX(model_id))
288 jx = indexX(model_id);
289 else if (j == 1 && hasY(model_id))
290 jx = indexY(model_id);
291 else if (j == 2 && hasZ(model_id))
292 jx = indexZ(model_id);
293 else if (j == 3 && hasRoll(model_id))
294 jx = indexRoll(model_id);
295 else if (j == 4 && hasPitch(model_id))
296 jx = indexPitch(model_id);
297 else if (j == 5 && hasYaw(model_id))
298 jx = indexYaw(model_id);
299 else
300 continue;
301
302 pose_covariance[n * i + j] = getContinuousStateCovarianceAt(state, ix, jx);
303 }
304 }
305 return pose_covariance;
306}
307
315template <typename T>
316inline std::vector<double> getPoseCovariance(const T& obj) {
317 return getPoseCovariance(obj.state);
318}
319
327inline gm::PoseWithCovariance getPoseWithCovariance(const ObjectState& state) {
328 gm::PoseWithCovariance pose_with_covariance;
329 pose_with_covariance.pose = getPose(state);
330 std::vector<double> covariance_vector = getPoseCovariance(state);
331 gm::PoseWithCovariance::_covariance_type covariance;
332 std::copy(covariance_vector.begin(), covariance_vector.end(), covariance.begin());
333 pose_with_covariance.covariance = covariance;
334 return pose_with_covariance;
335}
336
344template <typename T>
345inline gm::PoseWithCovariance getPoseWithCovariance(const T& obj) {
346 return getPoseWithCovariance(obj.state);
347}
348
355inline gm::Vector3 getVelocity(const ObjectState& state) {
356 gm::Vector3 velocity;
357 velocity.x = getVelLon(state);
358 velocity.y = getVelLat(state);
359 velocity.z = 0.0;
360 return velocity;
361}
362
370template <typename T>
371inline gm::Vector3 getVelocity(const T& obj) {
372 return getVelocity(obj.state);
373}
374
381inline double getVelocityMagnitude(const ObjectState& state) {
382 gm::Vector3 vel = getVelocity(state);
383 using namespace std;
384 return sqrt(pow(vel.x, 2) + pow(vel.y, 2) + pow(vel.z, 2));
385}
386
394template <typename T>
395inline double getVelocityMagnitude(const T& obj) {
396 return getVelocityMagnitude(obj.state);
397}
398
404inline gm::Vector3 getAcceleration(const ObjectState& state) {
405 gm::Vector3 acceleration;
406 acceleration.x = getAccLon(state);
407 acceleration.y = getAccLat(state);
408 acceleration.z = 0.0;
409 return acceleration;
410}
411
419template <typename T>
420inline gm::Vector3 getAcceleration(const T& obj) {
421 return getAcceleration(obj.state);
422}
423
430inline double getAccelerationMagnitude(const ObjectState& state) {
431 gm::Vector3 acc = getAcceleration(state);
432 using namespace std;
433 return sqrt(pow(acc.x, 2) + pow(acc.y, 2) + pow(acc.z, 2));
434}
435
443template <typename T>
444inline double getAccelerationMagnitude(const T& obj) {
445 return getAccelerationMagnitude(obj.state);
446}
447
448// --- alternative state entries ---------------------------------------------
449
456inline double getRollInDeg(const ObjectState& state) { return getRoll(state) * 180.0 / M_PI; }
457
465template <typename T>
466inline double getRollInDeg(const T& obj) {
467 return getRollInDeg(obj.state);
468}
469
476inline double getPitchInDeg(const ObjectState& state) { return getPitch(state) * 180.0 / M_PI; }
477
485template <typename T>
486inline double getPitchInDeg(const T& obj) {
487 return getPitchInDeg(obj.state);
488}
489
496inline double getYawInDeg(const ObjectState& state) { return getYaw(state) * 180.0 / M_PI; }
497
505template <typename T>
506inline double getYawInDeg(const T& obj) {
507 return getYawInDeg(obj.state);
508}
509
516inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const ObjectState& state) {
517 gm::PoseWithCovariance vel_lon_lat, vel_xyz;
518 vel_lon_lat.pose.position.x = getVelLon(state);
519 vel_lon_lat.pose.position.y = getVelLat(state);
520 vel_lon_lat.pose.position.z = 0.0;
521
522 int ix, jx, n = 2;
523 auto model_id = state.model_id;
524 for (int i = 0; i < n; i++) {
525 for (int j = 0; j < n; j++) {
526 if (i == 0 && hasVelLon(model_id))
527 ix = indexVelLon(model_id);
528 else if (i == 1 && hasVelLat(model_id))
529 ix = indexVelLat(model_id);
530 else
531 continue;
532
533 if (j == 0 && hasVelLon(model_id))
534 jx = indexVelLon(model_id);
535 else if (j == 1 && hasVelLat(model_id))
536 jx = indexVelLat(model_id);
537 else
538 continue;
539
540 vel_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
541 }
542 }
543
544 tf2::Quaternion q;
545 q.setRPY(0.0, 0.0, getYaw(state));
546 gm::TransformStamped tf;
547 tf.transform.rotation = tf2::toMsg(q);
548
549#ifdef ROS1
550 gm::PoseWithCovarianceStamped vel_lon_lat_stamped, vel_xyz_stamped;
551 vel_lon_lat_stamped.pose = vel_lon_lat;
552 tf2::doTransform(vel_lon_lat_stamped, vel_xyz_stamped, tf);
553 vel_xyz = vel_xyz_stamped.pose;
554#else
555 tf2::doTransform(vel_lon_lat, vel_xyz, tf);
556#endif
557
558 return vel_xyz;
559}
560
568template <typename T>
569inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const T& obj) {
570 return getVelocityXYZWithCovariance(obj.state);
571}
572
579inline gm::Vector3 getVelocityXYZ(const ObjectState& state) {
580 gm::Vector3 vel_lon_lat, vel_xyz;
581 vel_lon_lat = getVelocity(state);
582 tf2::Quaternion q;
583 q.setRPY(0.0, 0.0, getYaw(state));
584 gm::TransformStamped tf;
585 tf.transform.rotation = tf2::toMsg(q);
586 tf2::doTransform(vel_lon_lat, vel_xyz, tf);
587 return vel_xyz;
588}
589
597template <typename T>
598inline gm::Vector3 getVelocityXYZ(const T& obj) {
599 return getVelocityXYZ(obj.state);
600}
601
608inline double getVelX(const ObjectState& state) { return getVelocityXYZ(state).x; }
609
617template <typename T>
618inline double getVelX(const T& obj) {
619 return getVelX(obj.state);
620}
621
628inline double getVelY(const ObjectState& state) { return getVelocityXYZ(state).y; }
629
637template <typename T>
638inline double getVelY(const T& obj) {
639 return getVelY(obj.state);
640}
641
648inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const ObjectState& state) {
649 gm::PoseWithCovariance acc_lon_lat, acc_xyz;
650 acc_lon_lat.pose.position.x = getAccLon(state);
651 acc_lon_lat.pose.position.y = getAccLat(state);
652 acc_lon_lat.pose.position.z = 0.0;
653
654 int ix, jx, n = 2;
655 auto model_id = state.model_id;
656 for (int i = 0; i < n; i++) {
657 for (int j = 0; j < n; j++) {
658 if (i == 0 && hasAccLon(model_id))
659 ix = indexAccLon(model_id);
660 else if (i == 1 && hasAccLat(model_id))
661 ix = indexAccLat(model_id);
662 else
663 continue;
664
665 if (j == 0 && hasAccLon(model_id))
666 jx = indexAccLon(model_id);
667 else if (j == 1 && hasAccLat(model_id))
668 jx = indexAccLat(model_id);
669 else
670 continue;
671
672 acc_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
673 }
674 }
675
676 tf2::Quaternion q;
677 q.setRPY(0.0, 0.0, getYaw(state));
678 gm::TransformStamped tf;
679 tf.transform.rotation = tf2::toMsg(q);
680#ifdef ROS1
681 gm::PoseWithCovarianceStamped acc_lon_lat_stamped, acc_xyz_stamped;
682 acc_lon_lat_stamped.pose = acc_lon_lat;
683 tf2::doTransform(acc_lon_lat_stamped, acc_xyz_stamped, tf);
684 acc_xyz = acc_xyz_stamped.pose;
685#else
686 tf2::doTransform(acc_lon_lat, acc_xyz, tf);
687#endif
688 return acc_xyz;
689}
690
698template <typename T>
699inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const T& obj) {
700 return getAccelerationXYZWithCovariance(obj.state);
701}
702
709inline gm::Vector3 getAccelerationXYZ(const ObjectState& state) {
710 gm::Vector3 acc_lon_lat, acc_xyz;
711 acc_lon_lat = getAcceleration(state);
712 tf2::Quaternion q;
713 q.setRPY(0.0, 0.0, getYaw(state));
714 gm::TransformStamped tf;
715 tf.transform.rotation = tf2::toMsg(q);
716 tf2::doTransform(acc_lon_lat, acc_xyz, tf);
717 return acc_xyz;
718}
719
727template <typename T>
728inline gm::Vector3 getAccelerationXYZ(const T& obj) {
729 return getAccelerationXYZ(obj.state);
730}
731
738inline double getAccX(const ObjectState& state) { return getAccelerationXYZ(state).x; }
739
747template <typename T>
748inline double getAccX(const T& obj) {
749 return getAccX(obj.state);
750}
751
758inline double getAccY(const ObjectState& state) { return getAccelerationXYZ(state).y; }
759
767template <typename T>
768inline double getAccY(const T& obj) {
769 return getAccY(obj.state);
770}
771
772// --- misc ------------------------------------------------------------------
773
780inline ObjectClassification getClassWithHighestProbability(const ObjectState& state) {
781 ObjectClassification highest_prob_class;
782 auto highest_prob_class_it = std::max_element(
783 state.classifications.begin(), state.classifications.end(),
784 [](const ObjectClassification& c1, const ObjectClassification& c2) { return c1.probability < c2.probability; });
785 if (highest_prob_class_it == state.classifications.end()) {
786 highest_prob_class.probability = -1.0;
787 highest_prob_class.type = ObjectClassification::UNKNOWN;
788 } else {
789 highest_prob_class = *highest_prob_class_it;
790 }
791 return highest_prob_class;
792}
793
801template <typename T>
802inline ObjectClassification getClassWithHighestProbability(const T& obj) {
803 return getClassWithHighestProbability(obj.state);
804}
805
806} // namespace object_access
807
808} // namespace perception_msgs
Object state sanity checks.
void sanityCheckContinuousState(const ObjectState &state)
Perform sanity check on continuous state of given object state.
Definition checks.h:63
gm::PoseWithCovariance getAccelerationXYZWithCovariance(const ObjectState &state)
Get the acceleration XYZ with covariance of a given object state.
double getAccX(const ObjectState &state)
Get the x-acceleration of a given object state.
ObjectClassification getClassWithHighestProbability(const ObjectState &state)
Get the classification with highest probability of a given object state.
std::vector< double > getContinuousStateCovariance(const ObjectState &state)
Get the continuous state covariance for a given object state.
gm::Point getCenterPosition(const ObjectState &state)
Get the object's geometric center position.
double getPitchInDeg(const ObjectState &state)
Get the pitch in degree of a given object state.
double getVelocityMagnitude(const ObjectState &state)
Get the velocity magnitude of a given object state.
double getAccelerationMagnitude(const ObjectState &state)
Get the acceleration magnitude of a given object state.
double getAccY(const ObjectState &state)
Get the y-acceleration of a given object state.
std::vector< double > getContinuousState(const ObjectState &state)
Get the continuous state for a given object state.
gm::Point getPosition(const ObjectState &state)
Get the position of a given object state.
gm::Quaternion getOrientation(const ObjectState &state)
Get the orientation of a given object state.
double getContinuousStateCovarianceAt(const ObjectState &state, const unsigned int i, const unsigned int j)
Get the continuous state covariance entry (i,j) for a given object state.
double getYawInDeg(const ObjectState &state)
Get the yaw in degree of a given object state.
gm::Vector3 getVelocityXYZ(const ObjectState &state)
Get the velocity XYZ of a given object state.
gm::PoseWithCovariance getPoseWithCovariance(const ObjectState &state)
Get the pose with covariance of a given object state.
gm::Vector3 getAcceleration(const ObjectState &state)
Get the acceleration of a given object state.
gm::Pose getPose(const ObjectState &state)
Get the pose of a given object state.
std::vector< double > getPoseCovariance(const ObjectState &state)
Get the pose covariance of a given object state.
gm::PoseWithCovariance getVelocityXYZWithCovariance(const ObjectState &state)
Get the velocity XYZ with covariance of a object state.
gm::Vector3 getVelocity(const ObjectState &state)
Get the velocity of a given object state.
gm::Vector3 getAccelerationXYZ(const ObjectState &state)
Get the acceleration XYZ of a given object state.
double getRollInDeg(const ObjectState &state)
Get the roll in degree of a given object state.
std::vector< double > getContinuousStateCovarianceDiagonal(const ObjectState &state)
Get the continuous state covariance diagonal for a given object state.
double getVelY(const ObjectState &state)
Get the y-velocity of a given object state.
std::vector< long int > getDiscreteState(const ObjectState &state)
Get the discrete state for a given object state.
double getVelX(const ObjectState &state)
Get the x-velocity of a given object state.
Getter functions for objects state members.
double getZ(const ObjectState &state)
Get the z-position for a given object state.
double getRoll(const ObjectState &state)
Get the roll for a given object state.
double getVelLon(const ObjectState &state)
Get the longitudinal velocity for a given object state.
double getAccLon(const ObjectState &state)
Get the longitudinal acceleration for a given object state.
double getPitch(const ObjectState &state)
Get the pitch for a given object state.
double getYaw(const ObjectState &state)
Get the yaw for a given object state.
double getAccLat(const ObjectState &state)
Get the lateral acceleration for a given object state.
double getY(const ObjectState &state)
Get the y-position for a given object state.
double getX(const ObjectState &state)
Get the x-position for a given object state.
double getVelLat(const ObjectState &state)
Get the longitudinal velocity for a given object state.
Object state vector indices based on state model.
bool hasYaw(const unsigned char &model_id)
Indicates if given model contains a yaw.
int indexVelLon(const unsigned char &model_id)
Get the vector-index that stores the longitudinal velocity for a given model-id.
Definition state_index.h:93
int indexRoll(const unsigned char &model_id)
Get the vector-index that stores the roll for a given model-id.
bool hasZ(const unsigned char &model_id)
Indicates if given model contains a z-position.
bool hasAccLat(const unsigned char &model_id)
Indicates if given model contains a lateral acceleration.
bool hasVelLat(const unsigned char &model_id)
Indicates if given model contains a lateral velocity.
int indexVelLat(const unsigned char &model_id)
Get the vector-index that stores the lateral velocity for a given model-id.
bool hasY(const unsigned char &model_id)
Indicates if given model contains a y-position.
bool hasRoll(const unsigned char &model_id)
Indicates if given model contains a roll.
int indexY(const unsigned char &model_id)
Get the vector-index that stores the y-position for a given model-id.
Definition state_index.h:47
bool hasX(const unsigned char &model_id)
Indicates if given model contains an x-position.
bool hasVelLon(const unsigned char &model_id)
Indicates if given model contains a longitudinal velocity.
int indexZ(const unsigned char &model_id)
Get the vector-index that stores the z-position for a given model-id.
Definition state_index.h:70
int indexAccLon(const unsigned char &model_id)
Get the vector-index that stores the longitudinal acceleration for a given model-id.
bool hasPitch(const unsigned char &model_id)
Indicates if given model contains a pitch.
int indexPitch(const unsigned char &model_id)
Get the vector-index that stores the pitch for a given model-id.
int indexAccLat(const unsigned char &model_id)
Get the vector-index that stores the lateral acceleration for a given model-id.
int indexX(const unsigned char &model_id)
Get the vector-index that stores the x-position for a given model-id.
Definition state_index.h:24
bool hasAccLon(const unsigned char &model_id)
Indicates if given model contains a longitudinal acceleration.
int indexYaw(const unsigned char &model_id)
Get the vector-index that stores the yaw for a given model-id.
int getContinuousStateSize(const ObjectState &state)
Get the continuous state size for a given object state.
Definition utils.h:30