mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add geometry classes (#1766)
These classes introduce ways to represent poses and provide easy ways to transform, rotate, and translate poses across 2d space. This classes will be especially useful for a planned odometry and kinematics suite. Furthermore, these classes can also be used to simply represent waypoints on a field, do superstructure motion planning, etc.
This commit is contained in:
committed by
Peter Johnson
parent
48fe54271a
commit
ee24101696
60
wpilibc/src/main/native/cpp/geometry/Rotation2d.cpp
Normal file
60
wpilibc/src/main/native/cpp/geometry/Rotation2d.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "frc/geometry/Rotation2d.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Rotation2d::Rotation2d(double value)
|
||||
: m_value(value), m_cos(std::cos(value)), m_sin(std::sin(value)) {}
|
||||
|
||||
Rotation2d::Rotation2d(double x, double y) {
|
||||
const auto magnitude = std::hypot(x, y);
|
||||
if (magnitude > 1e-6) {
|
||||
m_sin = y / magnitude;
|
||||
m_cos = x / magnitude;
|
||||
} else {
|
||||
m_sin = 0.0;
|
||||
m_cos = 1.0;
|
||||
}
|
||||
m_value = std::atan2(m_sin, m_cos);
|
||||
}
|
||||
|
||||
Rotation2d Rotation2d::FromDegrees(double degrees) {
|
||||
return Rotation2d(Deg2Rad(degrees));
|
||||
}
|
||||
|
||||
Rotation2d Rotation2d::operator+(const Rotation2d& other) const {
|
||||
return RotateBy(other);
|
||||
}
|
||||
|
||||
Rotation2d& Rotation2d::operator+=(const Rotation2d& other) {
|
||||
double cos = Cos() * other.Cos() - Sin() * other.Sin();
|
||||
double sin = Cos() * other.Sin() + Sin() * other.Cos();
|
||||
m_cos = cos;
|
||||
m_sin = sin;
|
||||
m_value = std::atan2(m_sin, m_cos);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Rotation2d Rotation2d::operator-(const Rotation2d& other) const {
|
||||
return *this + -other;
|
||||
}
|
||||
|
||||
Rotation2d& Rotation2d::operator-=(const Rotation2d& other) {
|
||||
*this += -other;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Rotation2d Rotation2d::operator-() const { return Rotation2d(-m_value); }
|
||||
|
||||
Rotation2d Rotation2d::RotateBy(const Rotation2d& other) const {
|
||||
return {Cos() * other.Cos() - Sin() * other.Sin(),
|
||||
Cos() * other.Sin() + Sin() * other.Cos()};
|
||||
}
|
||||
Reference in New Issue
Block a user