12#include <perception_msgs_utils/covariance_transform.hpp>
19namespace perception_msgs {
21namespace object_access {
33 return state.continuous_state;
56 return state.discrete_state;
80 if (!state.continuous_state_covariance.empty())
return state.continuous_state_covariance;
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;
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);
139 std::vector<double> diagonal(n);
166 position.x =
getX(state);
167 position.y =
getY(state);
168 position.z =
getZ(state);
192 double roll{0}, pitch{0}, yaw{0};
196 q.setRPY(roll, pitch, yaw);
197 return tf2::toMsg(q);
218inline gm::Pose
getPose(
const ObjectState& state) {
234 const gm::Vector3 offset_to_center = state.reference_point.translation_to_geometric_center;
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();
277 const int model_id = state.model_id;
278 std::vector<double> pose_covariance(n * n, CONTINUOUS_STATE_COVARIANCE_INIT);
280 for (
int i = 0; i < n; i++) {
281 for (
int j = 0; j < n; j++) {
282 if (i == 0 &&
hasX(model_id))
284 else if (i == 1 &&
hasY(model_id))
286 else if (i == 2 &&
hasZ(model_id))
288 else if (i == 3 &&
hasRoll(model_id))
290 else if (i == 4 &&
hasPitch(model_id))
292 else if (i == 5 &&
hasYaw(model_id))
297 if (j == 0 &&
hasX(model_id))
299 else if (j == 1 &&
hasY(model_id))
301 else if (j == 2 &&
hasZ(model_id))
303 else if (j == 3 &&
hasRoll(model_id))
305 else if (j == 4 &&
hasPitch(model_id))
307 else if (j == 5 &&
hasYaw(model_id))
315 return pose_covariance;
338 gm::PoseWithCovariance pose_with_covariance;
339 pose_with_covariance.pose =
getPose(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;
366 gm::Vector3 velocity;
394 return sqrt(pow(vel.x, 2) + pow(vel.y, 2) + pow(vel.z, 2));
415 gm::Vector3 acceleration;
418 acceleration.z = 0.0;
443 return sqrt(pow(acc.x, 2) + pow(acc.y, 2) + pow(acc.z, 2));
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;
533 auto model_id = state.model_id;
534 for (
int i = 0; i < n; i++) {
535 for (
int j = 0; j < n; j++) {
555 q.setRPY(0.0, 0.0,
getYaw(state));
556 gm::TransformStamped tf;
557 tf.transform.rotation = tf2::toMsg(q);
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;
586 gm::Vector3 vel_lon_lat, vel_xyz;
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);
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;
661 auto model_id = state.model_id;
662 for (
int i = 0; i < n; i++) {
663 for (
int j = 0; j < n; j++) {
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;
712 gm::Vector3 acc_lon_lat, acc_xyz;
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);
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;
791 highest_prob_class = *highest_prob_class_it;
793 return highest_prob_class;
Object state sanity checks.
void sanityCheckContinuousState(const ObjectState &state)
Perform sanity check on continuous state of given object state.
void sanityCheckContinuousStateCovariance(const ObjectState &state)
Perform sanity check on continuous state covariance of given object state.
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.