perception_interfaces 1.1.2
Loading...
Searching...
No Matches
convenience_state_setters.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 <array>
16
17namespace perception_msgs
18{
19
20namespace object_access
21{
22
23// --- full state/covariance -------------------------------------------------
24
31inline void setContinuousState(ObjectState & state, const std::vector<double> & val)
32{
33 state.continuous_state = val;
35}
36
44template <typename T>
45inline void setContinuousState(T & obj, const std::vector<double> & val)
46{
47 setContinuousState(obj.state, val);
48}
49
56inline void setDiscreteState(ObjectState & state, const std::vector<long int> & val)
57{
58 state.discrete_state = val;
60}
61
69template <typename T>
70inline void setDiscreteState(T & obj, const std::vector<long int> & val)
71{
72 setDiscreteState(obj.state, val);
73}
74
81inline void setContinuousStateCovariance(ObjectState & state, const std::vector<double> & val)
82{
83 state.continuous_state_covariance = val;
85}
86
94template <typename T>
95inline void setContinuousStateCovariance(T & obj, const std::vector<double> & val)
96{
97 setContinuousStateCovariance(obj.state, val);
98}
99
109 ObjectState & state, const unsigned int i, const unsigned int j, const double val)
110{
112 const int n = getContinuousStateSize(state);
113 state.continuous_state_covariance[n * i + j] = val;
114}
115
125template <typename T>
127 T & obj, const unsigned int i, const unsigned int j, const double val)
128{
129 setContinuousStateCovarianceAt(obj.state, i, j, val);
130}
131
138inline void setContinuousStateCovarianceDiagonal(ObjectState & state, const std::vector<double> val)
139{
140 const int n = getContinuousStateSize(state);
141 for (int i = 0; i < n; i++) setContinuousStateCovarianceAt(state, i, i, val[i]);
142}
143
151template <typename T>
152inline void setContinuousStateCovarianceDiagonal(T & obj, const std::vector<double> val)
153{
155}
156
157// --- vector quantities -----------------------------------------------------
158
166inline void setPosition(ObjectState & state, const gm::Point val, const bool reset_covariance = true)
167{
168 setX(state, val.x, reset_covariance);
169 setY(state, val.y, reset_covariance);
170 setZ(state, val.z, reset_covariance);
171}
172
181template <typename T>
182inline void setPosition(T & obj, const gm::Point val, const bool reset_covariance = true)
183{
184 setPosition(obj.state, val, reset_covariance);
185}
186
194inline void setPosition(ObjectState & state, const std::array<double, 3> & val, const bool reset_covariance = true)
195{
196 setX(state, val[0], reset_covariance);
197 setY(state, val[1], reset_covariance);
198 setZ(state, val[2], reset_covariance);
199}
200
209template <typename T>
210inline void setPosition(T & obj, const std::array<double, 3> & val, const bool reset_covariance = true)
211{
212 setPosition(obj.state, val, reset_covariance);
213}
214
222inline void setOrientation(ObjectState & state, const gm::Quaternion & val, const bool reset_covariance = true)
223{
224 tf2::Quaternion q;
225 tf2::fromMsg(val, q);
226 double roll, pitch, yaw;
227 tf2::Matrix3x3(q).getRPY(roll, pitch, yaw);
228 if (hasRoll(state.model_id)) setRoll(state, roll, reset_covariance);
229 if (hasPitch(state.model_id)) setPitch(state, pitch, reset_covariance);
230 if (hasYaw(state.model_id)) setYaw(state, yaw, reset_covariance);
231}
232
241template <typename T>
242inline void setOrientation(T & obj, const gm::Quaternion & val, const bool reset_covariance = true)
243{
244 setOrientation(obj.state, val, reset_covariance);
245}
246
254inline void setOrientation(ObjectState & state, const std::array<double, 3> & val, const bool reset_covariance = true)
255{
256 if (hasRoll(state.model_id)) setRoll(state, val[0], reset_covariance);
257 if (hasPitch(state.model_id)) setPitch(state, val[1], reset_covariance);
258 if (hasYaw(state.model_id)) setYaw(state, val[2], reset_covariance);
259}
260
269template <typename T>
270inline void setOrientation(T & obj, const std::array<double, 3> & val, const bool reset_covariance = true)
271{
272 setOrientation(obj.state, val, reset_covariance);
273}
274
282inline void setPose(ObjectState & state, const gm::Pose & val, const bool reset_covariance = true)
283{
284 setPosition(state, val.position, reset_covariance);
285 setOrientation(state, val.orientation, reset_covariance);
286}
287
296template <typename T>
297inline void setPose(T & obj, const gm::Pose & val, const bool reset_covariance = true)
298{
299 setPose(obj.state, val, reset_covariance);
300}
301
310inline void setPose(
311 ObjectState & state, const std::array<double, 3> & xyz, const std::array<double, 3> & rpy, const bool reset_covariance = true)
312{
313 setPosition(state, xyz, reset_covariance);
314 setOrientation(state, rpy, reset_covariance);
315}
316
326template <typename T>
327inline void setPose(T & obj, const std::array<double, 3> & xyz, const std::array<double, 3> & rpy, const bool reset_covariance = true)
328{
329 setPose(obj.state, xyz, rpy, reset_covariance);
330}
331
339 ObjectState & state, const gm::PoseWithCovariance::_covariance_type & val)
340{
341 const int n = 6;
342 const int model_id = state.model_id;
343
344 int ix, jx;
345 for (int i = 0; i < n; i++) {
346 bool i_valid = true;
347 switch (i) {
348 case 0: i_valid = hasX(model_id); ix = i_valid ? indexX(model_id) : 0; break;
349 case 1: i_valid = hasY(model_id); ix = i_valid ? indexY(model_id) : 0; break;
350 case 2: i_valid = hasZ(model_id); ix = i_valid ? indexZ(model_id) : 0; break;
351 case 3: i_valid = hasRoll(model_id); ix = i_valid ? indexRoll(model_id) : 0; break;
352 case 4: i_valid = hasPitch(model_id); ix = i_valid ? indexPitch(model_id) : 0; break;
353 case 5: i_valid = hasYaw(model_id); ix = i_valid ? indexYaw(model_id) : 0; break;
354 }
355 if (!i_valid) continue;
356
357 for (int j = 0; j < n; j++) {
358 bool j_valid = true;
359 switch (j) {
360 case 0: j_valid = hasX(model_id); jx = j_valid ? indexX(model_id) : 0; break;
361 case 1: j_valid = hasY(model_id); jx = j_valid ? indexY(model_id) : 0; break;
362 case 2: j_valid = hasZ(model_id); jx = j_valid ? indexZ(model_id) : 0; break;
363 case 3: j_valid = hasRoll(model_id); jx = j_valid ? indexRoll(model_id) : 0; break;
364 case 4: j_valid = hasPitch(model_id); jx = j_valid ? indexPitch(model_id) : 0; break;
365 case 5: j_valid = hasYaw(model_id); jx = j_valid ? indexYaw(model_id) : 0; break;
366 }
367 if (!j_valid) continue;
368
369 setContinuousStateCovarianceAt(state, ix, jx, val[n * i + j]);
370 }
371 }
372}
380template <typename T>
381inline void setPoseCovariance(T & obj, const gm::PoseWithCovariance::_covariance_type & val)
382{
383 setPoseCovariance(obj.state, val);
384}
385
392inline void setPoseWithCovariance(ObjectState & state, const gm::PoseWithCovariance & val)
393{
394 setPose(state, val.pose, false);
395 setPoseCovariance(state, val.covariance);
396}
397
405template <typename T>
406inline void setPoseWithCovariance(T & obj, const gm::PoseWithCovariance & val)
407{
408 setPoseWithCovariance(obj.state, val);
409}
410
418inline void setVelocity(ObjectState & state, const gm::Vector3 & val, const bool reset_covariance = true)
419{
420 setVelLon(state, val.x, reset_covariance);
421 setVelLat(state, val.y, reset_covariance);
422}
431template <typename T>
432inline void setVelocity(T & obj, const gm::Vector3 & val, const bool reset_covariance = true)
433{
434 setVelocity(obj.state, val, reset_covariance);
435}
436
444inline void setVelocity(ObjectState & state, const std::array<double, 2> & val, const bool reset_covariance = true)
445{
446 setVelLon(state, val[0], reset_covariance);
447 setVelLat(state, val[1], reset_covariance);
448}
449
458template <typename T>
459inline void setVelocity(T & obj, const std::array<double, 2> & val, const bool reset_covariance = true)
460{
461 setVelocity(obj.state, val, reset_covariance);
462}
463
471inline void setAcceleration(ObjectState & state, const gm::Vector3 & val, const bool reset_covariance = true)
472{
473 setAccLon(state, val.x, reset_covariance);
474 setAccLat(state, val.y, reset_covariance);
475}
476
485template <typename T>
486inline void setAcceleration(T & obj, const gm::Vector3 & val, const bool reset_covariance = true)
487{
488 setAcceleration(obj.state, val, reset_covariance);
489}
490
498inline void setAcceleration(ObjectState & state, const std::array<double, 2> & val, const bool reset_covariance = true)
499{
500 setAccLon(state, val[0], reset_covariance);
501 setAccLat(state, val[1], reset_covariance);
502}
503
512template <typename T>
513inline void setAcceleration(T & obj, const std::array<double, 2> & val, const bool reset_covariance = true)
514{
515 setAcceleration(obj.state, val, reset_covariance);
516}
517
518// --- alternative state entries ---------------------------------------------
519
527inline void setRollInDeg(ObjectState & state, const double val, const bool reset_covariance = true)
528{
529 setRoll(state, val * M_PI / 180.0, reset_covariance);
530}
531
540template <typename T>
541inline void setRollInDeg(T & obj, const double val, const bool reset_covariance = true)
542{
543 setRollInDeg(obj.state, val, reset_covariance);
544}
545
553inline void setPitchInDeg(ObjectState & state, const double val, const bool reset_covariance = true)
554{
555 setPitch(state, val * M_PI / 180.0, reset_covariance);
556}
557
566template <typename T>
567inline void setPitchInDeg(T & obj, const double val, const bool reset_covariance = true)
568{
569 setPitchInDeg(obj.state, val, reset_covariance);
570}
571
579inline void setYawInDeg(ObjectState & state, const double val, const bool reset_covariance = true)
580{
581 setYaw(state, val * M_PI / 180.0, reset_covariance);
582}
583
592template <typename T>
593inline void setYawInDeg(T & obj, const double val, const bool reset_covariance = true)
594{
595 setYawInDeg(obj.state, val, reset_covariance);
596}
597
609 ObjectState & state, const gm::Vector3 & vel_xyz_in, const double yaw, const double var_vel_x,
610 const double var_vel_y, const double cov_vel_xy)
611{
612 gm::PoseWithCovariance vel_lon_lat, vel_xyz;
613 vel_xyz.pose.position.x = vel_xyz_in.x;
614 vel_xyz.pose.position.y = vel_xyz_in.y;
615 vel_xyz.pose.position.z = 0.0;
616
617 vel_xyz.covariance.at(0) = var_vel_x;
618 vel_xyz.covariance.at(1) = cov_vel_xy;
619 vel_xyz.covariance.at(6) = cov_vel_xy;
620 vel_xyz.covariance.at(7) = var_vel_y;
621
622 tf2::Quaternion q;
623 q.setRPY(0.0, 0.0, -yaw);
624 gm::TransformStamped tf;
625 tf.transform.rotation = tf2::toMsg(q);
626
627#ifdef ROS1
628 gm::PoseWithCovarianceStamped vel_lon_lat_stamped, vel_xyz_stamped;
629 vel_xyz_stamped.pose = vel_xyz;
630 tf2::doTransform(vel_xyz_stamped, vel_lon_lat_stamped, tf);
631 vel_lon_lat = vel_lon_lat_stamped.pose;
632#else
633 tf2::doTransform(vel_xyz, vel_lon_lat, tf);
634#endif
635
636 setVelocity(state, {vel_lon_lat.pose.position.x, vel_lon_lat.pose.position.y}, false);
637 setYaw(state, yaw, false);
638
639 int ix, jx, n = 2;
640 auto model_id = state.model_id;
641 for (int i = 0; i < n; i++) {
642 for (int j = 0; j < n; j++) {
643 if (i == 0 && hasVelLon(model_id))
644 ix = indexVelLon(model_id);
645 else if (i == 1 && hasVelLat(model_id))
646 ix = indexVelLat(model_id);
647 else
648 continue;
649
650 if (j == 0 && hasVelLon(model_id))
651 jx = indexVelLon(model_id);
652 else if (j == 1 && hasVelLat(model_id))
653 jx = indexVelLat(model_id);
654 else
655 continue;
656
657 setContinuousStateCovarianceAt(state, ix, jx, vel_lon_lat.covariance.at(6 * i + j));
658 }
659 }
660}
661
673template <typename T>
675 T & obj, const gm::Vector3 & vel_xyz_in, const double yaw, const double var_vel_x,
676 const double var_vel_y, const double cov_vel_xy)
677{
678 return setVelocityXYZYawWithCovariance(obj.state, vel_xyz_in, yaw, var_vel_x, var_vel_y, cov_vel_xy);
679}
680
689inline void setVelocityXYZYaw(ObjectState & state, const gm::Vector3 & vel_xyz, const double yaw, const bool reset_covariance = true)
690{
691 gm::Vector3 vel_lon_lat, vel_xyz_in;
692 vel_xyz_in = vel_xyz;
693 tf2::Quaternion q;
694 q.setRPY(0.0, 0.0, -yaw);
695 gm::TransformStamped tf;
696 tf.transform.rotation = tf2::toMsg(q);
697 tf2::doTransform(vel_xyz_in, vel_lon_lat, tf);
698 setVelocity(state, vel_lon_lat, reset_covariance);
699 setYaw(state, yaw, reset_covariance);
700}
701
711template <typename T>
712inline void setVelocityXYZYaw(T & obj, const gm::Vector3 & vel_xyz, const double yaw, const bool reset_covariance = true)
713{
714 setVelocityXYZYaw(obj.state, vel_xyz, yaw, reset_covariance);
715}
716
728 ObjectState & state, const gm::Vector3 & acc_xyz_in, const double yaw, const double var_acc_x,
729 const double var_acc_y, const double cov_acc_xy)
730{
731 gm::PoseWithCovariance acc_lon_lat, acc_xyz;
732 acc_xyz.pose.position.x = acc_xyz_in.x;
733 acc_xyz.pose.position.y = acc_xyz_in.y;
734 acc_xyz.pose.position.z = 0.0;
735
736 acc_xyz.covariance.at(0) = var_acc_x;
737 acc_xyz.covariance.at(1) = cov_acc_xy;
738 acc_xyz.covariance.at(6) = cov_acc_xy;
739 acc_xyz.covariance.at(7) = var_acc_y;
740
741 tf2::Quaternion q;
742 q.setRPY(0.0, 0.0, -yaw);
743 gm::TransformStamped tf;
744 tf.transform.rotation = tf2::toMsg(q);
745
746#ifdef ROS1
747 gm::PoseWithCovarianceStamped acc_lon_lat_stamped, acc_xyz_stamped;
748 acc_xyz_stamped.pose = acc_xyz;
749 tf2::doTransform(acc_xyz_stamped, acc_lon_lat_stamped, tf);
750 acc_lon_lat = acc_lon_lat_stamped.pose;
751#else
752 tf2::doTransform(acc_xyz, acc_lon_lat, tf);
753#endif
754
755 setAcceleration(state, {acc_lon_lat.pose.position.x, acc_lon_lat.pose.position.y}, false);
756 setYaw(state, yaw, false);
757
758 int ix, jx, n = 2;
759 auto model_id = state.model_id;
760 for (int i = 0; i < n; i++) {
761 for (int j = 0; j < n; j++) {
762 if (i == 0 && hasAccLon(model_id))
763 ix = indexAccLon(model_id);
764 else if (i == 1 && hasAccLat(model_id))
765 ix = indexAccLat(model_id);
766 else
767 continue;
768
769 if (j == 0 && hasAccLon(model_id))
770 jx = indexAccLon(model_id);
771 else if (j == 1 && hasAccLat(model_id))
772 jx = indexAccLat(model_id);
773 else
774 continue;
775
776 setContinuousStateCovarianceAt(state, ix, jx, acc_lon_lat.covariance.at(6 * i + j));
777 }
778 }
779}
780
792template <typename T>
794 T & obj, const gm::Vector3 & acc_xyz_in, const double yaw, const double var_acc_x,
795 const double var_acc_y, const double cov_acc_xy)
796{
797 return setAccelerationXYZYawWithCovariance(obj.state, acc_xyz_in, yaw, var_acc_x, var_acc_y, cov_acc_xy);
798}
799
809 ObjectState & state, const gm::Vector3 & acc_xyz, const double yaw, const bool reset_covariance = true)
810{
811 gm::Vector3 acc_lon_lat, acc_xyz_in;
812 acc_xyz_in = acc_xyz;
813 tf2::Quaternion q;
814 q.setRPY(0.0, 0.0, -yaw);
815 gm::TransformStamped tf;
816 tf.transform.rotation = tf2::toMsg(q);
817 tf2::doTransform(acc_xyz_in, acc_lon_lat, tf);
818 setAcceleration(state, acc_lon_lat, reset_covariance);
819 setYaw(state, yaw, reset_covariance);
820}
821
831template <typename T>
832inline void setAccelerationXYZYaw(T & obj, const gm::Vector3 & acc_xyz, const double yaw, const bool reset_covariance = true)
833{
834 setAccelerationXYZYaw(obj.state, acc_xyz, yaw, reset_covariance);
835}
836
837} // namespace object_access
838
839} // 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
void sanityCheckContinuousStateCovariance(const ObjectState &state)
Perform sanity check on continuous state covariance of given object state.
Definition checks.h:103
void sanityCheckDiscreteState(const ObjectState &state)
Perform sanity check on discrete state of given object state.
Definition checks.h:83
void setPosition(ObjectState &state, const gm::Point val, const bool reset_covariance=true)
Set the position of a given object state.
void setContinuousStateCovarianceDiagonal(ObjectState &state, const std::vector< double > val)
Set the continuous state covariance diagonal of a given object state.
void setContinuousState(ObjectState &state, const std::vector< double > &val)
Set the continuous state of a given object state.
void setPitchInDeg(ObjectState &state, const double val, const bool reset_covariance=true)
Set the pitch in degree of a given object state.
void setPose(ObjectState &state, const gm::Pose &val, const bool reset_covariance=true)
Set the pose of a given object state.
void setAccelerationXYZYawWithCovariance(ObjectState &state, const gm::Vector3 &acc_xyz_in, const double yaw, const double var_acc_x, const double var_acc_y, const double cov_acc_xy)
Set the acceleration XYZ and yaw with covariance of a given object state.
void setAcceleration(ObjectState &state, const gm::Vector3 &val, const bool reset_covariance=true)
Set the acceleration of a given object state.
void setVelocityXYZYawWithCovariance(ObjectState &state, const gm::Vector3 &vel_xyz_in, const double yaw, const double var_vel_x, const double var_vel_y, const double cov_vel_xy)
Set the velocity XYZ and yaw with covariance of a given object state.
void setRollInDeg(ObjectState &state, const double val, const bool reset_covariance=true)
Set the roll in degree of a given object state.
void setContinuousStateCovarianceAt(ObjectState &state, const unsigned int i, const unsigned int j, const double val)
Set the continuous state covariance entry at (i,j) of a given object state.
void setDiscreteState(ObjectState &state, const std::vector< long int > &val)
Set the discrete state of a given object state.
void setVelocity(ObjectState &state, const gm::Vector3 &val, const bool reset_covariance=true)
Set the velocity of a given object state.
void setOrientation(ObjectState &state, const gm::Quaternion &val, const bool reset_covariance=true)
Set the orientation of a given object state.
void setPoseCovariance(ObjectState &state, const gm::PoseWithCovariance::_covariance_type &val)
Set the pose covariance of a given object state.
void setYawInDeg(ObjectState &state, const double val, const bool reset_covariance=true)
Set the yaw in degree of a given object state.
void setAccelerationXYZYaw(ObjectState &state, const gm::Vector3 &acc_xyz, const double yaw, const bool reset_covariance=true)
Set the acceleration XYZ and yaw of a given template object that contains an object state.
void setVelocityXYZYaw(ObjectState &state, const gm::Vector3 &vel_xyz, const double yaw, const bool reset_covariance=true)
Set the velocity XYZ and yaw of a given object state.
void setContinuousStateCovariance(ObjectState &state, const std::vector< double > &val)
Set the continuous state covariance of a given object state.
void setPoseWithCovariance(ObjectState &state, const gm::PoseWithCovariance &val)
Set the pose with covariance of 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.
Setter functions for objects state members.
void setAccLat(ObjectState &state, const double val, const bool reset_covariance=true)
Set the lateral acceleration for a given object state.
void setAccLon(ObjectState &state, const double val, const bool reset_covariance=true)
Set the longitudinal acceleration for a given object state.
void setVelLon(ObjectState &state, const double val, const bool reset_covariance=true)
Set the longitdunial velocity for a given object state.
void setX(ObjectState &state, const double val, const bool reset_covariance=true)
Set the x-position for a given object state.
void setY(ObjectState &state, const double val, const bool reset_covariance=true)
Set the y-position for a given object state.
void setVelLat(ObjectState &state, const double val, const bool reset_covariance=true)
Set the lateral velocity for a given object state.
void setYaw(ObjectState &state, const double val, const bool reset_covariance=true)
Set the yaw for a given object state.
void setRoll(ObjectState &state, const double val, const bool reset_covariance=true)
Set the roll for a given object state.
void setPitch(ObjectState &state, const double val, const bool reset_covariance=true)
Set the pitch for a given object state.
void setZ(ObjectState &state, const double val, const bool reset_covariance=true)
Set the z-position for a given object state.
int getContinuousStateSize(const ObjectState &state)
Get the continuous state size for a given object state.
Definition utils.h:30