perception_interfaces 1.2.0
Loading...
Searching...
No Matches
convenience_state_getters.hpp
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
12#include <perception_msgs_utils/covariance_transform.hpp>
15
16#include <algorithm>
17#include <cmath>
18
19namespace perception_msgs {
20
21namespace object_access {
22
23// --- full state/covariance -------------------------------------------------
24
31inline std::vector<double> getContinuousState(const ObjectState& state) {
33 return state.continuous_state;
34}
35
43template <typename T>
44inline std::vector<double> getContinuousState(const T& obj) {
45 return getContinuousState(obj.state);
46}
47
54inline std::vector<long int> getDiscreteState(const ObjectState& state) {
56 return state.discrete_state;
57}
58
66template <typename T>
67inline std::vector<long int> getDiscreteState(const T& obj) {
68 return getDiscreteState(obj.state);
69}
70
78inline std::vector<double> getContinuousStateCovariance(const ObjectState& state) {
80 if (!state.continuous_state_covariance.empty()) return state.continuous_state_covariance;
81 const int n = getContinuousStateSize(state.model_id);
82 std::vector<double> covariance(n * n, CONTINUOUS_STATE_COVARIANCE_INIT);
83 for (int i = 0; i < n; ++i) covariance[n * i + i] = CONTINUOUS_STATE_COVARIANCE_INVALID;
84 return covariance;
85}
86
94template <typename T>
95inline std::vector<double> getContinuousStateCovariance(const T& obj) {
96 return getContinuousStateCovariance(obj.state);
97}
98
107inline double getContinuousStateCovarianceAt(const ObjectState& state, const unsigned int i, const unsigned int j) {
108 const int n = getContinuousStateSize(state);
109 if (i >= static_cast<unsigned int>(n) || j >= static_cast<unsigned int>(n))
110 throw std::out_of_range("Covariance coordinates out of range");
112 const auto& covariance = state.continuous_state_covariance;
113 if (covariance.empty()) return i == j ? CONTINUOUS_STATE_COVARIANCE_INVALID : CONTINUOUS_STATE_COVARIANCE_INIT;
114 return covariance.at(n * i + j);
115}
116
126template <typename T>
127inline double getContinuousStateCovarianceAt(const T& obj, const unsigned int i, const unsigned int j) {
128 return getContinuousStateCovarianceAt(obj.state, i, j);
129}
130
137inline std::vector<double> getContinuousStateCovarianceDiagonal(const ObjectState& state) {
138 const int n = getContinuousStateSize(state);
139 std::vector<double> diagonal(n);
140 for (int i = 0; i < n; i++) diagonal[i] = getContinuousStateCovarianceAt(state, i, i);
141 return diagonal;
142}
143
151template <typename T>
152inline std::vector<double> getContinuousStateCovarianceDiagonal(const T& obj) {
153 return getContinuousStateCovarianceDiagonal(obj.state);
154}
155
156// --- vector quantities -----------------------------------------------------
157
164inline gm::Point getPosition(const ObjectState& state) {
165 gm::Point position;
166 position.x = getX(state);
167 position.y = getY(state);
168 position.z = getZ(state);
169 return position;
170}
171
179template <typename T>
180inline gm::Point getPosition(const T& obj) {
181 return getPosition(obj.state);
182}
183
190inline gm::Quaternion getOrientation(const ObjectState& state) {
191 tf2::Quaternion q;
192 double roll{0}, pitch{0}, yaw{0};
193 if (hasRoll(state.model_id)) roll = getRoll(state);
194 if (hasPitch(state.model_id)) pitch = getPitch(state);
195 if (hasYaw(state.model_id)) yaw = getYaw(state);
196 q.setRPY(roll, pitch, yaw);
197 return tf2::toMsg(q);
198}
199
207template <typename T>
208inline gm::Quaternion getOrientation(const T& obj) {
209 return getOrientation(obj.state);
210}
211
218inline gm::Pose getPose(const ObjectState& state) {
219 gm::Pose pose;
220 pose.position = getPosition(state);
221 pose.orientation = getOrientation(state);
222 return pose;
223}
224
231inline gm::Point getCenterPosition(const ObjectState& state) {
232 gm::Point position = getPosition(state);
233 const auto orientation = getOrientation(state);
234 const gm::Vector3 offset_to_center = state.reference_point.translation_to_geometric_center;
235 tf2::Quaternion q;
236 tf2::fromMsg(orientation, q);
237 const tf2::Vector3 offset_to_center_tf2(offset_to_center.x, offset_to_center.y, offset_to_center.z);
238 const tf2::Vector3 rotated_offset_to_center = tf2::quatRotate(q, offset_to_center_tf2);
239 position.x += rotated_offset_to_center.x();
240 position.y += rotated_offset_to_center.y();
241 position.z += rotated_offset_to_center.z();
242 return position;
243}
244
252template <typename T>
253inline gm::Point getCenterPosition(const T& object) {
254 return getCenterPosition(object.state);
255}
256
264template <typename T>
265inline gm::Pose getPose(const T& obj) {
266 return getPose(obj.state);
267}
268
275inline std::vector<double> getPoseCovariance(const ObjectState& state) {
276 const int n = 6;
277 const int model_id = state.model_id;
278 std::vector<double> pose_covariance(n * n, CONTINUOUS_STATE_COVARIANCE_INIT);
279 int ix, jx;
280 for (int i = 0; i < n; i++) {
281 for (int j = 0; j < n; j++) {
282 if (i == 0 && hasX(model_id))
283 ix = indexX(model_id);
284 else if (i == 1 && hasY(model_id))
285 ix = indexY(model_id);
286 else if (i == 2 && hasZ(model_id))
287 ix = indexZ(model_id);
288 else if (i == 3 && hasRoll(model_id))
289 ix = indexRoll(model_id);
290 else if (i == 4 && hasPitch(model_id))
291 ix = indexPitch(model_id);
292 else if (i == 5 && hasYaw(model_id))
293 ix = indexYaw(model_id);
294 else
295 continue;
296
297 if (j == 0 && hasX(model_id))
298 jx = indexX(model_id);
299 else if (j == 1 && hasY(model_id))
300 jx = indexY(model_id);
301 else if (j == 2 && hasZ(model_id))
302 jx = indexZ(model_id);
303 else if (j == 3 && hasRoll(model_id))
304 jx = indexRoll(model_id);
305 else if (j == 4 && hasPitch(model_id))
306 jx = indexPitch(model_id);
307 else if (j == 5 && hasYaw(model_id))
308 jx = indexYaw(model_id);
309 else
310 continue;
311
312 pose_covariance[n * i + j] = getContinuousStateCovarianceAt(state, ix, jx);
313 }
314 }
315 return pose_covariance;
316}
317
325template <typename T>
326inline std::vector<double> getPoseCovariance(const T& obj) {
327 return getPoseCovariance(obj.state);
328}
329
337inline gm::PoseWithCovariance getPoseWithCovariance(const ObjectState& state) {
338 gm::PoseWithCovariance pose_with_covariance;
339 pose_with_covariance.pose = getPose(state);
340 std::vector<double> covariance_vector = getPoseCovariance(state);
341 gm::PoseWithCovariance::_covariance_type covariance;
342 std::copy(covariance_vector.begin(), covariance_vector.end(), covariance.begin());
343 pose_with_covariance.covariance = covariance;
344 return pose_with_covariance;
345}
346
354template <typename T>
355inline gm::PoseWithCovariance getPoseWithCovariance(const T& obj) {
356 return getPoseWithCovariance(obj.state);
357}
358
365inline gm::Vector3 getVelocity(const ObjectState& state) {
366 gm::Vector3 velocity;
367 velocity.x = getVelLon(state);
368 velocity.y = getVelLat(state);
369 velocity.z = 0.0;
370 return velocity;
371}
372
380template <typename T>
381inline gm::Vector3 getVelocity(const T& obj) {
382 return getVelocity(obj.state);
383}
384
391inline double getVelocityMagnitude(const ObjectState& state) {
392 gm::Vector3 vel = getVelocity(state);
393 using namespace std;
394 return sqrt(pow(vel.x, 2) + pow(vel.y, 2) + pow(vel.z, 2));
395}
396
404template <typename T>
405inline double getVelocityMagnitude(const T& obj) {
406 return getVelocityMagnitude(obj.state);
407}
408
414inline gm::Vector3 getAcceleration(const ObjectState& state) {
415 gm::Vector3 acceleration;
416 acceleration.x = getAccLon(state);
417 acceleration.y = getAccLat(state);
418 acceleration.z = 0.0;
419 return acceleration;
420}
421
429template <typename T>
430inline gm::Vector3 getAcceleration(const T& obj) {
431 return getAcceleration(obj.state);
432}
433
440inline double getAccelerationMagnitude(const ObjectState& state) {
441 gm::Vector3 acc = getAcceleration(state);
442 using namespace std;
443 return sqrt(pow(acc.x, 2) + pow(acc.y, 2) + pow(acc.z, 2));
444}
445
453template <typename T>
454inline double getAccelerationMagnitude(const T& obj) {
455 return getAccelerationMagnitude(obj.state);
456}
457
458// --- alternative state entries ---------------------------------------------
459
466inline double getRollInDeg(const ObjectState& state) { return getRoll(state) * 180.0 / M_PI; }
467
475template <typename T>
476inline double getRollInDeg(const T& obj) {
477 return getRollInDeg(obj.state);
478}
479
486inline double getPitchInDeg(const ObjectState& state) { return getPitch(state) * 180.0 / M_PI; }
487
495template <typename T>
496inline double getPitchInDeg(const T& obj) {
497 return getPitchInDeg(obj.state);
498}
499
506inline double getYawInDeg(const ObjectState& state) { return getYaw(state) * 180.0 / M_PI; }
507
515template <typename T>
516inline double getYawInDeg(const T& obj) {
517 return getYawInDeg(obj.state);
518}
519
526inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const ObjectState& state) {
527 gm::PoseWithCovariance vel_lon_lat, vel_xyz;
528 vel_lon_lat.pose.position.x = getVelLon(state);
529 vel_lon_lat.pose.position.y = getVelLat(state);
530 vel_lon_lat.pose.position.z = 0.0;
531
532 int ix, jx, n = 2;
533 auto model_id = state.model_id;
534 for (int i = 0; i < n; i++) {
535 for (int j = 0; j < n; j++) {
536 if (i == 0 && hasVelLon(model_id))
537 ix = indexVelLon(model_id);
538 else if (i == 1 && hasVelLat(model_id))
539 ix = indexVelLat(model_id);
540 else
541 continue;
542
543 if (j == 0 && hasVelLon(model_id))
544 jx = indexVelLon(model_id);
545 else if (j == 1 && hasVelLat(model_id))
546 jx = indexVelLat(model_id);
547 else
548 continue;
549
550 vel_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
551 }
552 }
553
554 tf2::Quaternion q;
555 q.setRPY(0.0, 0.0, getYaw(state));
556 gm::TransformStamped tf;
557 tf.transform.rotation = tf2::toMsg(q);
558
559 gm::PoseWithCovarianceStamped vel_lon_lat_stamped, vel_xyz_stamped;
560 vel_lon_lat_stamped.pose = vel_lon_lat;
561 transformPoseWithCovariance(vel_lon_lat_stamped, vel_xyz_stamped, tf);
562 vel_xyz = vel_xyz_stamped.pose;
563
564 return vel_xyz;
565}
566
574template <typename T>
575inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const T& obj) {
576 return getVelocityXYZWithCovariance(obj.state);
577}
578
585inline gm::Vector3 getVelocityXYZ(const ObjectState& state) {
586 gm::Vector3 vel_lon_lat, vel_xyz;
587 vel_lon_lat = getVelocity(state);
588 tf2::Quaternion q;
589 q.setRPY(0.0, 0.0, getYaw(state));
590 gm::TransformStamped tf;
591 tf.transform.rotation = tf2::toMsg(q);
592 tf2::doTransform(vel_lon_lat, vel_xyz, tf);
593 return vel_xyz;
594}
595
603template <typename T>
604inline gm::Vector3 getVelocityXYZ(const T& obj) {
605 return getVelocityXYZ(obj.state);
606}
607
614inline double getVelX(const ObjectState& state) { return getVelocityXYZ(state).x; }
615
623template <typename T>
624inline double getVelX(const T& obj) {
625 return getVelX(obj.state);
626}
627
634inline double getVelY(const ObjectState& state) { return getVelocityXYZ(state).y; }
635
643template <typename T>
644inline double getVelY(const T& obj) {
645 return getVelY(obj.state);
646}
647
654inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const ObjectState& state) {
655 gm::PoseWithCovariance acc_lon_lat, acc_xyz;
656 acc_lon_lat.pose.position.x = getAccLon(state);
657 acc_lon_lat.pose.position.y = getAccLat(state);
658 acc_lon_lat.pose.position.z = 0.0;
659
660 int ix, jx, n = 2;
661 auto model_id = state.model_id;
662 for (int i = 0; i < n; i++) {
663 for (int j = 0; j < n; j++) {
664 if (i == 0 && hasAccLon(model_id))
665 ix = indexAccLon(model_id);
666 else if (i == 1 && hasAccLat(model_id))
667 ix = indexAccLat(model_id);
668 else
669 continue;
670
671 if (j == 0 && hasAccLon(model_id))
672 jx = indexAccLon(model_id);
673 else if (j == 1 && hasAccLat(model_id))
674 jx = indexAccLat(model_id);
675 else
676 continue;
677
678 acc_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
679 }
680 }
681
682 tf2::Quaternion q;
683 q.setRPY(0.0, 0.0, getYaw(state));
684 gm::TransformStamped tf;
685 tf.transform.rotation = tf2::toMsg(q);
686 gm::PoseWithCovarianceStamped acc_lon_lat_stamped, acc_xyz_stamped;
687 acc_lon_lat_stamped.pose = acc_lon_lat;
688 transformPoseWithCovariance(acc_lon_lat_stamped, acc_xyz_stamped, tf);
689 acc_xyz = acc_xyz_stamped.pose;
690 return acc_xyz;
691}
692
700template <typename T>
701inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const T& obj) {
702 return getAccelerationXYZWithCovariance(obj.state);
703}
704
711inline gm::Vector3 getAccelerationXYZ(const ObjectState& state) {
712 gm::Vector3 acc_lon_lat, acc_xyz;
713 acc_lon_lat = getAcceleration(state);
714 tf2::Quaternion q;
715 q.setRPY(0.0, 0.0, getYaw(state));
716 gm::TransformStamped tf;
717 tf.transform.rotation = tf2::toMsg(q);
718 tf2::doTransform(acc_lon_lat, acc_xyz, tf);
719 return acc_xyz;
720}
721
729template <typename T>
730inline gm::Vector3 getAccelerationXYZ(const T& obj) {
731 return getAccelerationXYZ(obj.state);
732}
733
740inline double getAccX(const ObjectState& state) { return getAccelerationXYZ(state).x; }
741
749template <typename T>
750inline double getAccX(const T& obj) {
751 return getAccX(obj.state);
752}
753
760inline double getAccY(const ObjectState& state) { return getAccelerationXYZ(state).y; }
761
769template <typename T>
770inline double getAccY(const T& obj) {
771 return getAccY(obj.state);
772}
773
774// --- misc ------------------------------------------------------------------
775
782inline ObjectClassification getClassWithHighestProbability(const ObjectState& state) {
783 ObjectClassification highest_prob_class;
784 auto highest_prob_class_it = std::max_element(
785 state.classifications.begin(), state.classifications.end(),
786 [](const ObjectClassification& c1, const ObjectClassification& c2) { return c1.probability < c2.probability; });
787 if (highest_prob_class_it == state.classifications.end()) {
788 highest_prob_class.probability = -1.0;
789 highest_prob_class.type = ObjectClassification::UNKNOWN;
790 } else {
791 highest_prob_class = *highest_prob_class_it;
792 }
793 return highest_prob_class;
794}
795
803template <typename T>
804inline ObjectClassification getClassWithHighestProbability(const T& obj) {
805 return getClassWithHighestProbability(obj.state);
806}
807
808} // namespace object_access
809
810} // namespace perception_msgs
Object state sanity checks.
void sanityCheckContinuousState(const ObjectState &state)
Perform sanity check on continuous state of given object state.
Definition checks.hpp:63
void sanityCheckContinuousStateCovariance(const ObjectState &state)
Perform sanity check on continuous state covariance of given object state.
Definition checks.hpp:103
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 full continuous state covariance for a given object state. Empty storage yields INVALID diago...
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.
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.
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.
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.
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.hpp:30