2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-10-30 13:55:38 -07:00
|
|
|
|
|
|
|
|
#include "frc/smartdashboard/FieldObject2d.h"
|
|
|
|
|
|
[glass] Field2d enhancements (#3234)
- Add raw support for pose lists > 255/3 in length
- Improve drag selection, especially with closely overlapping objects
- Drag selection of corner also highlights center of object with smaller circle
- Multiple styles (box, line, closed line, track)
- Configurable line and arrow settings (color, weight)
- Add tooltip for object name, index, x, y, rotation
- Context menu for pose edit/add/remove
- View/edit in feet or inches as well as meters
- Configurable object selectability
Implementation: use vector of Pose2d internally, use units
2021-03-27 13:34:44 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2021-04-02 01:08:07 -04:00
|
|
|
#include "frc/trajectory/Trajectory.h"
|
|
|
|
|
|
2020-10-30 13:55:38 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2020-12-23 20:36:51 -08:00
|
|
|
FieldObject2d::FieldObject2d(FieldObject2d&& rhs) {
|
|
|
|
|
std::swap(m_name, rhs.m_name);
|
|
|
|
|
std::swap(m_entry, rhs.m_entry);
|
|
|
|
|
std::swap(m_poses, rhs.m_poses);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FieldObject2d& FieldObject2d::operator=(FieldObject2d&& rhs) {
|
|
|
|
|
std::swap(m_name, rhs.m_name);
|
|
|
|
|
std::swap(m_entry, rhs.m_entry);
|
|
|
|
|
std::swap(m_poses, rhs.m_poses);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 13:55:38 -07:00
|
|
|
void FieldObject2d::SetPose(const Pose2d& pose) {
|
2021-06-06 19:51:14 -07:00
|
|
|
SetPoses({pose});
|
2020-10-30 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FieldObject2d::SetPose(units::meter_t x, units::meter_t y,
|
|
|
|
|
Rotation2d rotation) {
|
2021-06-06 19:51:14 -07:00
|
|
|
SetPoses({{x, y, rotation}});
|
2020-10-30 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pose2d FieldObject2d::GetPose() const {
|
|
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
UpdateFromEntry();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_poses.empty()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2020-10-30 13:55:38 -07:00
|
|
|
return m_poses[0];
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 19:51:14 -07:00
|
|
|
void FieldObject2d::SetPoses(wpi::span<const Pose2d> poses) {
|
2020-10-30 13:55:38 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
m_poses.assign(poses.begin(), poses.end());
|
|
|
|
|
UpdateEntry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FieldObject2d::SetPoses(std::initializer_list<Pose2d> poses) {
|
2021-06-06 19:51:14 -07:00
|
|
|
SetPoses({poses.begin(), poses.end()});
|
2020-10-30 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 01:08:07 -04:00
|
|
|
void FieldObject2d::SetTrajectory(const Trajectory& trajectory) {
|
|
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
m_poses.clear();
|
|
|
|
|
m_poses.reserve(trajectory.States().size());
|
|
|
|
|
for (auto&& state : trajectory.States()) {
|
|
|
|
|
m_poses.push_back(state.pose);
|
|
|
|
|
}
|
|
|
|
|
UpdateEntry();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 13:55:38 -07:00
|
|
|
std::vector<Pose2d> FieldObject2d::GetPoses() const {
|
|
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
UpdateFromEntry();
|
|
|
|
|
return std::vector<Pose2d>(m_poses.begin(), m_poses.end());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<const Pose2d> FieldObject2d::GetPoses(
|
2020-10-30 13:55:38 -07:00
|
|
|
wpi::SmallVectorImpl<Pose2d>& out) const {
|
|
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
UpdateFromEntry();
|
|
|
|
|
out.assign(m_poses.begin(), m_poses.end());
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FieldObject2d::UpdateEntry(bool setDefault) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_entry) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
wpi::SmallVector<double, 9> arr;
|
|
|
|
|
for (auto&& pose : m_poses) {
|
|
|
|
|
auto& translation = pose.Translation();
|
|
|
|
|
arr.push_back(translation.X().value());
|
|
|
|
|
arr.push_back(translation.Y().value());
|
|
|
|
|
arr.push_back(pose.Rotation().Degrees().value());
|
|
|
|
|
}
|
|
|
|
|
if (setDefault) {
|
|
|
|
|
m_entry.SetDefault(arr);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_entry.Set(arr);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-10-30 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FieldObject2d::UpdateFromEntry() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_entry) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
auto arr = m_entry.Get();
|
|
|
|
|
auto size = arr.size();
|
|
|
|
|
if ((size % 3) != 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_poses.resize(size / 3);
|
|
|
|
|
for (size_t i = 0; i < size / 3; ++i) {
|
|
|
|
|
m_poses[i] =
|
|
|
|
|
Pose2d{units::meter_t{arr[i * 3 + 0]}, units::meter_t{arr[i * 3 + 1]},
|
|
|
|
|
units::degree_t{arr[i * 3 + 2]}};
|
2020-10-30 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
}
|