perception_interfaces 1.0.0
Loading...
Searching...
No Matches
convenience_state_getters.h
Go to the documentation of this file.
1
30#pragma once
31
35
36#include <algorithm>
37#include <cmath>
38
39namespace perception_msgs
40{
41
42namespace object_access
43{
44
45// --- full state/covariance -------------------------------------------------
46
53inline std::vector<double> getContinuousState(const ObjectState & state)
54{
56 return state.continuous_state;
57}
58
66template <typename T>
67inline std::vector<double> getContinuousState(const T & obj)
68{
69 return getContinuousState(obj.state);
70}
71
78inline std::vector<long int> getDiscreteState(const ObjectState & state)
79{
81 return state.discrete_state;
82}
83
91template <typename T>
92inline std::vector<long int> getDiscreteState(const T & obj)
93{
94 return getDiscreteState(obj.state);
95}
96
103inline std::vector<double> getContinuousStateCovariance(const ObjectState & state)
104{
106 return state.continuous_state_covariance;
107}
108
116template <typename T>
117inline std::vector<double> getContinuousStateCovariance(const T & obj)
118{
119 return getContinuousStateCovariance(obj.state);
120}
121
131 const ObjectState & state, const unsigned int i, const unsigned int j)
132{
133 const int n = getContinuousStateSize(state);
134 const std::vector<double> covariance = getContinuousStateCovariance(state);
135 return covariance[n * i + j];
136}
137
147template <typename T>
149 const T & obj, const unsigned int i, const unsigned int j)
150{
151 return getContinuousStateCovarianceAt(obj.state, i, j);
152}
153
160inline std::vector<double> getContinuousStateCovarianceDiagonal(const ObjectState & state)
161{
162 const int n = getContinuousStateSize(state);
163 std::vector<double> diagonal(n);
164 for (int i = 0; i < n; i++) diagonal[i] = getContinuousStateCovarianceAt(state, i, i);
165 return diagonal;
166}
167
175template <typename T>
176inline std::vector<double> getContinuousStateCovarianceDiagonal(const T & obj)
177{
178 return getContinuousStateCovarianceDiagonal(obj.state);
179}
180
181// --- vector quantities -----------------------------------------------------
182
183
190inline gm::Point getPosition(const ObjectState & state)
191{
192 gm::Point position;
193 position.x = getX(state);
194 position.y = getY(state);
195 position.z = getZ(state);
196 return position;
197}
198
206template <typename T>
207inline gm::Point getPosition(const T & obj)
208{
209 return getPosition(obj.state);
210}
211
218inline gm::Quaternion getOrientation(const ObjectState & state)
219{
220 tf2::Quaternion q;
221 double roll{0}, pitch{0}, yaw{0};
222 if (hasRoll(state.model_id)) roll = getRoll(state);
223 if (hasPitch(state.model_id)) pitch = getPitch(state);
224 if (hasYaw(state.model_id)) yaw = getYaw(state);
225 q.setRPY(roll, pitch, yaw);
226 return tf2::toMsg(q);
227}
228
236template <typename T>
237inline gm::Quaternion getOrientation(const T & obj)
238{
239 return getOrientation(obj.state);
240}
241
248inline gm::Pose getPose(const ObjectState & state)
249{
250 gm::Pose pose;
251 pose.position = getPosition(state);
252 pose.orientation = getOrientation(state);
253 return pose;
254}
255
263template <typename T>
264inline gm::Pose getPose(const T & obj)
265{
266 return getPose(obj.state);
267}
268
275inline std::vector<double> getPoseCovariance(const ObjectState & state)
276{
277 const int n = 6;
278 const int model_id = state.model_id;
279 std::vector<double> pose_covariance(n * n, 0.0);
280 int ix, jx;
281 for (int i = 0; i < n; i++) {
282 for (int j = 0; j < n; j++) {
283 if (i == 0 && hasX(model_id))
284 ix = indexX(model_id);
285 else if (i == 1 && hasY(model_id))
286 ix = indexY(model_id);
287 else if (i == 2 && hasZ(model_id))
288 ix = indexZ(model_id);
289 else if (i == 3 && hasRoll(model_id))
290 ix = indexRoll(model_id);
291 else if (i == 4 && hasPitch(model_id))
292 ix = indexPitch(model_id);
293 else if (i == 5 && hasYaw(model_id))
294 ix = indexYaw(model_id);
295 else
296 continue;
297
298 if (j == 0 && hasX(model_id))
299 jx = indexX(model_id);
300 else if (j == 1 && hasY(model_id))
301 jx = indexY(model_id);
302 else if (j == 2 && hasZ(model_id))
303 jx = indexZ(model_id);
304 else if (j == 3 && hasRoll(model_id))
305 jx = indexRoll(model_id);
306 else if (j == 4 && hasPitch(model_id))
307 jx = indexPitch(model_id);
308 else if (j == 5 && hasYaw(model_id))
309 jx = indexYaw(model_id);
310 else
311 continue;
312
313 pose_covariance[n * i + j] = getContinuousStateCovarianceAt(state, ix, jx);
314 }
315 }
316 return pose_covariance;
317}
318
326template <typename T>
327inline std::vector<double> getPoseCovariance(const T & obj)
328{
329 return getPoseCovariance(obj.state);
330}
331
339inline gm::PoseWithCovariance getPoseWithCovariance(const ObjectState & state)
340{
341 gm::PoseWithCovariance pose_with_covariance;
342 pose_with_covariance.pose = getPose(state);
343 std::vector<double> covariance_vector = getPoseCovariance(state);
344 gm::PoseWithCovariance::_covariance_type covariance;
345 std::copy(covariance_vector.begin(), covariance_vector.end(), covariance.begin());
346 pose_with_covariance.covariance = covariance;
347 return pose_with_covariance;
348}
349
357template <typename T>
358inline gm::PoseWithCovariance getPoseWithCovariance(const T & obj)
359{
360 return getPoseWithCovariance(obj.state);
361}
362
369inline gm::Vector3 getVelocity(const ObjectState & state)
370{
371 gm::Vector3 velocity;
372 velocity.x = getVelLon(state);
373 velocity.y = getVelLat(state);
374 velocity.z = 0.0;
375 return velocity;
376}
377
385template <typename T>
386inline gm::Vector3 getVelocity(const T & obj)
387{
388 return getVelocity(obj.state);
389}
390
397inline double getVelocityMagnitude(const ObjectState & state)
398{
399 gm::Vector3 vel = getVelocity(state);
400 using namespace std;
401 return sqrt(pow(vel.x, 2) + pow(vel.y, 2) + pow(vel.z, 2));
402}
403
411template <typename T>
412inline double getVelocityMagnitude(const T & obj)
413{
414 return getVelocityMagnitude(obj.state);
415}
416
422inline gm::Vector3 getAcceleration(const ObjectState & state)
423{
424 gm::Vector3 acceleration;
425 acceleration.x = getAccLon(state);
426 acceleration.y = getAccLat(state);
427 acceleration.z = 0.0;
428 return acceleration;
429}
430
438template <typename T>
439inline gm::Vector3 getAcceleration(const T & obj)
440{
441 return getAcceleration(obj.state);
442}
443
450inline double getAccelerationMagnitude(const ObjectState & state)
451{
452 gm::Vector3 acc = getAcceleration(state);
453 using namespace std;
454 return sqrt(pow(acc.x, 2) + pow(acc.y, 2) + pow(acc.z, 2));
455}
456
464template <typename T>
465inline double getAccelerationMagnitude(const T & obj)
466{
467 return getAccelerationMagnitude(obj.state);
468}
469
470// --- alternative state entries ---------------------------------------------
471
472
479inline double getRollInDeg(const ObjectState & state) { return getRoll(state) * 180.0 / M_PI; }
480
488template <typename T>
489inline double getRollInDeg(const T & obj)
490{
491 return getRollInDeg(obj.state);
492}
493
500inline double getPitchInDeg(const ObjectState & state) { return getPitch(state) * 180.0 / M_PI; }
501
509template <typename T>
510inline double getPitchInDeg(const T & obj)
511{
512 return getPitchInDeg(obj.state);
513}
514
521inline double getYawInDeg(const ObjectState & state) { return getYaw(state) * 180.0 / M_PI; }
522
530template <typename T>
531inline double getYawInDeg(const T & obj)
532{
533 return getYawInDeg(obj.state);
534}
535
542inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const ObjectState & state)
543{
544 gm::PoseWithCovariance vel_lon_lat, vel_xyz;
545 vel_lon_lat.pose.position.x = getVelLon(state);
546 vel_lon_lat.pose.position.y = getVelLat(state);
547 vel_lon_lat.pose.position.z = 0.0;
548
549 int ix, jx, n = 2;
550 auto model_id = state.model_id;
551 for (int i = 0; i < n; i++) {
552 for (int j = 0; j < n; j++) {
553 if (i == 0 && hasVelLon(model_id))
554 ix = indexVelLon(model_id);
555 else if (i == 1 && hasVelLat(model_id))
556 ix = indexVelLat(model_id);
557 else
558 continue;
559
560 if (j == 0 && hasVelLon(model_id))
561 jx = indexVelLon(model_id);
562 else if (j == 1 && hasVelLat(model_id))
563 jx = indexVelLat(model_id);
564 else
565 continue;
566
567 vel_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
568 }
569 }
570
571 tf2::Quaternion q;
572 q.setRPY(0.0, 0.0, getYaw(state));
573 gm::TransformStamped tf;
574 tf.transform.rotation = tf2::toMsg(q);
575
576#ifdef ROS1
577 gm::PoseWithCovarianceStamped vel_lon_lat_stamped, vel_xyz_stamped;
578 vel_lon_lat_stamped.pose = vel_lon_lat;
579 tf2::doTransform(vel_lon_lat_stamped, vel_xyz_stamped, tf);
580 vel_xyz = vel_xyz_stamped.pose;
581#else
582 tf2::doTransform(vel_lon_lat, vel_xyz, tf);
583#endif
584
585 return vel_xyz;
586}
587
595template <typename T>
596inline gm::PoseWithCovariance getVelocityXYZWithCovariance(const T & obj)
597{
598 return getVelocityXYZWithCovariance(obj.state);
599}
600
607inline gm::Vector3 getVelocityXYZ(const ObjectState & state)
608{
609 gm::Vector3 vel_lon_lat, vel_xyz;
610 vel_lon_lat = getVelocity(state);
611 tf2::Quaternion q;
612 q.setRPY(0.0, 0.0, getYaw(state));
613 gm::TransformStamped tf;
614 tf.transform.rotation = tf2::toMsg(q);
615 tf2::doTransform(vel_lon_lat, vel_xyz, tf);
616 return vel_xyz;
617}
618
626template <typename T>
627inline gm::Vector3 getVelocityXYZ(const T & obj)
628{
629 return getVelocityXYZ(obj.state);
630}
631
638inline double getVelX(const ObjectState & state) { return getVelocityXYZ(state).x; }
639
647template <typename T>
648inline double getVelX(const T & obj)
649{
650 return getVelX(obj.state);
651}
652
659inline double getVelY(const ObjectState & state) { return getVelocityXYZ(state).y; }
660
668template <typename T>
669inline double getVelY(const T & obj)
670{
671 return getVelY(obj.state);
672}
673
680inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const ObjectState & state)
681{
682 gm::PoseWithCovariance acc_lon_lat, acc_xyz;
683 acc_lon_lat.pose.position.x = getAccLon(state);
684 acc_lon_lat.pose.position.y = getAccLat(state);
685 acc_lon_lat.pose.position.z = 0.0;
686
687 int ix, jx, n = 2;
688 auto model_id = state.model_id;
689 for (int i = 0; i < n; i++) {
690 for (int j = 0; j < n; j++) {
691 if (i == 0 && hasAccLon(model_id))
692 ix = indexAccLon(model_id);
693 else if (i == 1 && hasAccLat(model_id))
694 ix = indexAccLat(model_id);
695 else
696 continue;
697
698 if (j == 0 && hasAccLon(model_id))
699 jx = indexAccLon(model_id);
700 else if (j == 1 && hasAccLat(model_id))
701 jx = indexAccLat(model_id);
702 else
703 continue;
704
705 acc_lon_lat.covariance.at(6 * i + j) = getContinuousStateCovarianceAt(state, ix, jx);
706 }
707 }
708
709 tf2::Quaternion q;
710 q.setRPY(0.0, 0.0, getYaw(state));
711 gm::TransformStamped tf;
712 tf.transform.rotation = tf2::toMsg(q);
713#ifdef ROS1
714 gm::PoseWithCovarianceStamped acc_lon_lat_stamped, acc_xyz_stamped;
715 acc_lon_lat_stamped.pose = acc_lon_lat;
716 tf2::doTransform(acc_lon_lat_stamped, acc_xyz_stamped, tf);
717 acc_xyz = acc_xyz_stamped.pose;
718#else
719 tf2::doTransform(acc_lon_lat, acc_xyz, tf);
720#endif
721 return acc_xyz;
722}
723
731template <typename T>
732inline gm::PoseWithCovariance getAccelerationXYZWithCovariance(const T & obj)
733{
734 return getAccelerationXYZWithCovariance(obj.state);
735}
736
743inline gm::Vector3 getAccelerationXYZ(const ObjectState & state)
744{
745 gm::Vector3 acc_lon_lat, acc_xyz;
746 acc_lon_lat = getAcceleration(state);
747 tf2::Quaternion q;
748 q.setRPY(0.0, 0.0, getYaw(state));
749 gm::TransformStamped tf;
750 tf.transform.rotation = tf2::toMsg(q);
751 tf2::doTransform(acc_lon_lat, acc_xyz, tf);
752 return acc_xyz;
753}
754
762template <typename T>
763inline gm::Vector3 getAccelerationXYZ(const T & obj)
764{
765 return getAccelerationXYZ(obj.state);
766}
767
774inline double getAccX(const ObjectState & state) { return getAccelerationXYZ(state).x; }
775
783template <typename T>
784inline double getAccX(const T & obj)
785{
786 return getAccX(obj.state);
787}
788
795inline double getAccY(const ObjectState & state) { return getAccelerationXYZ(state).y; }
796
804template <typename T>
805inline double getAccY(const T & obj)
806{
807 return getAccY(obj.state);
808}
809
810// --- misc ------------------------------------------------------------------
811
818inline ObjectClassification getClassWithHighestProbability(const ObjectState & state)
819{
820 ObjectClassification highest_prob_class;
821 auto highest_prob_class_it = std::max_element(
822 state.classifications.begin(), state.classifications.end(),
823 [](const ObjectClassification & c1, const ObjectClassification & c2) {
824 return c1.probability < c2.probability;
825 });
826 if (highest_prob_class_it == state.classifications.end()) {
827 highest_prob_class.probability = -1.0;
828 highest_prob_class.type = ObjectClassification::UNKNOWN;
829 } else {
830 highest_prob_class = *highest_prob_class_it;
831 }
832 return highest_prob_class;
833}
834
842template <typename T>
843inline ObjectClassification getClassWithHighestProbability(const T & obj)
844{
845 return getClassWithHighestProbability(obj.state);
846}
847
848} // namespace object_access
849
850} // 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:84
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.
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.
Definition state_index.h:68
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:91
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:45
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:51