James Kuszmaul | 18008f8 | 2023-02-23 20:52:50 -0800 | [diff] [blame^] | 1 | #include "y2023/localizer/map_expander_lib.h" |
| 2 | |
| 3 | #include "y2023/localizer/utils.h" |
| 4 | |
| 5 | namespace y2023::localizer { |
| 6 | namespace { |
| 7 | flatbuffers::Offset<frc971::vision::Position> AbsolutePositionForTagAndRelative( |
| 8 | const Eigen::Affine3d &tag, const frc971::vision::Position *relative, |
| 9 | flatbuffers::FlatBufferBuilder *fbb) { |
| 10 | const Eigen::Vector3d absolute = |
| 11 | tag * Eigen::Vector3d(relative->x(), relative->y(), relative->z()); |
| 12 | return frc971::vision::CreatePosition(*fbb, absolute.x(), absolute.y(), |
| 13 | absolute.z()); |
| 14 | } |
| 15 | |
| 16 | flatbuffers::Offset<ScoringRow> AbsoluteRowForTagAndRelative( |
| 17 | const Eigen::Affine3d &tag, const ScoringRow *relative_row, |
| 18 | flatbuffers::FlatBufferBuilder *fbb) { |
| 19 | auto left_offset = |
| 20 | AbsolutePositionForTagAndRelative(tag, relative_row->left_cone(), fbb); |
| 21 | auto cube_offset = |
| 22 | AbsolutePositionForTagAndRelative(tag, relative_row->cube(), fbb); |
| 23 | auto right_offset = |
| 24 | AbsolutePositionForTagAndRelative(tag, relative_row->right_cone(), fbb); |
| 25 | return CreateScoringRow(*fbb, left_offset, cube_offset, right_offset); |
| 26 | } |
| 27 | |
| 28 | flatbuffers::Offset<ScoringGrid> AbsoluteGridForTagAndRelative( |
| 29 | const Eigen::Affine3d &tag, const ScoringGrid *relative_grid, |
| 30 | flatbuffers::FlatBufferBuilder *fbb) { |
| 31 | auto bottom_offset = |
| 32 | AbsoluteRowForTagAndRelative(tag, relative_grid->bottom(), fbb); |
| 33 | auto middle_offset = |
| 34 | AbsoluteRowForTagAndRelative(tag, relative_grid->middle(), fbb); |
| 35 | auto top_offset = |
| 36 | AbsoluteRowForTagAndRelative(tag, relative_grid->top(), fbb); |
| 37 | return CreateScoringGrid(*fbb, bottom_offset, middle_offset, top_offset); |
| 38 | } |
| 39 | |
| 40 | flatbuffers::Offset<ScoringGrid> RelativeGridForTagAndAbsolute( |
| 41 | const Eigen::Affine3d &tag, const ScoringGrid *absolute_grid, |
| 42 | flatbuffers::FlatBufferBuilder *fbb) { |
| 43 | return AbsoluteGridForTagAndRelative(tag.inverse(), absolute_grid, fbb); |
| 44 | } |
| 45 | |
| 46 | flatbuffers::Offset<DoubleSubstation> AbsoluteSubstationForTagAndRelative( |
| 47 | const Eigen::Affine3d &tag, const DoubleSubstation *relative_station, |
| 48 | flatbuffers::FlatBufferBuilder *fbb) { |
| 49 | auto left_offset = |
| 50 | AbsolutePositionForTagAndRelative(tag, relative_station->left(), fbb); |
| 51 | auto right_offset = |
| 52 | AbsolutePositionForTagAndRelative(tag, relative_station->right(), fbb); |
| 53 | return CreateDoubleSubstation(*fbb, left_offset, right_offset); |
| 54 | } |
| 55 | |
| 56 | flatbuffers::Offset<DoubleSubstation> RelativeSubstationForTagAndAbsolute( |
| 57 | const Eigen::Affine3d &tag, const DoubleSubstation *absolute_station, |
| 58 | flatbuffers::FlatBufferBuilder *fbb) { |
| 59 | return AbsoluteSubstationForTagAndRelative(tag.inverse(), absolute_station, |
| 60 | fbb); |
| 61 | } |
| 62 | |
| 63 | flatbuffers::Offset<HalfField> AbsoluteHalfForTagsAndRelative( |
| 64 | const std::map<uint64_t, Eigen::Affine3d> &april_tags, |
| 65 | const RelativeHalfField *relative_half, const RelativeScoringMap *map, |
| 66 | flatbuffers::FlatBufferBuilder *fbb) { |
| 67 | auto substation_offset = AbsoluteSubstationForTagAndRelative( |
| 68 | april_tags.at(relative_half->substation()), map->substation(), fbb); |
| 69 | auto left_offset = AbsoluteGridForTagAndRelative( |
| 70 | april_tags.at(relative_half->left()), map->nominal_grid(), fbb); |
| 71 | auto middle_offset = AbsoluteGridForTagAndRelative( |
| 72 | april_tags.at(relative_half->middle()), map->nominal_grid(), fbb); |
| 73 | auto right_offset = AbsoluteGridForTagAndRelative( |
| 74 | april_tags.at(relative_half->right()), map->nominal_grid(), fbb); |
| 75 | return CreateHalfField(*fbb, substation_offset, left_offset, middle_offset, |
| 76 | right_offset); |
| 77 | } |
| 78 | } // namespace |
| 79 | |
| 80 | std::map<uint64_t, Eigen::Affine3d> AprilTagPoses( |
| 81 | const frc971::vision::TargetMap *map) { |
| 82 | std::map<uint64_t, Eigen::Affine3d> april_tags; |
| 83 | for (const frc971::vision::TargetPoseFbs *target : *map->target_poses()) { |
| 84 | const uint64_t id = target->id(); |
| 85 | CHECK(april_tags.count(id) == 0); |
| 86 | april_tags[id] = PoseToTransform(target); |
| 87 | } |
| 88 | return april_tags; |
| 89 | } |
| 90 | |
| 91 | aos::FlatbufferDetachedBuffer<ScoringMap> ExpandMap( |
| 92 | const RelativeScoringMap *relative_map, |
| 93 | const frc971::vision::TargetMap *target_map) { |
| 94 | std::map<uint64_t, Eigen::Affine3d> april_tags = AprilTagPoses(target_map); |
| 95 | flatbuffers::FlatBufferBuilder fbb; |
| 96 | fbb.Finish(CreateScoringMap( |
| 97 | fbb, |
| 98 | AbsoluteHalfForTagsAndRelative(april_tags, relative_map->red(), |
| 99 | relative_map, &fbb), |
| 100 | AbsoluteHalfForTagsAndRelative(april_tags, relative_map->blue(), |
| 101 | relative_map, &fbb))); |
| 102 | return fbb.Release(); |
| 103 | } |
| 104 | |
| 105 | aos::FlatbufferDetachedBuffer<ScoringGrid> RelativeGridForTag( |
| 106 | const ScoringGrid *absolute_grid, |
| 107 | const frc971::vision::TargetMap *target_map, uint64_t tag) { |
| 108 | const Eigen::Affine3d tag_pose = AprilTagPoses(target_map).at(tag); |
| 109 | flatbuffers::FlatBufferBuilder fbb; |
| 110 | fbb.Finish(RelativeGridForTagAndAbsolute(tag_pose, absolute_grid, &fbb)); |
| 111 | return fbb.Release(); |
| 112 | } |
| 113 | |
| 114 | aos::FlatbufferDetachedBuffer<DoubleSubstation> RelativeSubstationForTag( |
| 115 | const DoubleSubstation *absolute_substation, |
| 116 | const frc971::vision::TargetMap *target_map, uint64_t tag) { |
| 117 | const Eigen::Affine3d tag_pose = AprilTagPoses(target_map).at(tag); |
| 118 | flatbuffers::FlatBufferBuilder fbb; |
| 119 | fbb.Finish( |
| 120 | RelativeSubstationForTagAndAbsolute(tag_pose, absolute_substation, &fbb)); |
| 121 | return fbb.Release(); |
| 122 | } |
| 123 | |
| 124 | } // namespace y2023::localizer |