5package org.wpilib.math.geometry;
7import static org.wpilib.math.util.UnitConversions.radiansToRotations;
8import static org.wpilib.math.util.UnitConversions.rotationsToRadians;
9import static org.wpilib.units.Units.Radians;
11import io.avaje.jsonb.Json;
12import java.util.Objects;
13import org.wpilib.math.geometry.proto.Rotation2dProto;
14import org.wpilib.math.geometry.struct.Rotation2dStruct;
15import org.wpilib.math.interpolation.Interpolatable;
16import org.wpilib.math.linalg.MatBuilder;
17import org.wpilib.math.linalg.Matrix;
18import org.wpilib.math.numbers.N2;
19import org.wpilib.math.util.MathSharedStore;
20import org.wpilib.math.util.Nat;
21import org.wpilib.units.measure.Angle;
22import org.wpilib.util.protobuf.ProtobufSerializable;
23import org.wpilib.util.struct.StructSerializable;
80 @Json.Ignore
private final double m_cos;
81 @Json.Ignore
private final double m_sin;
95 public Rotation2d(@Json.Alias(value =
"radians")
double value) {
96 m_cos = Math.cos(value);
97 m_sin = Math.sin(value);
107 double magnitude = Math.hypot(x, y);
108 if (magnitude > 1e-6) {
109 m_cos = x / magnitude;
110 m_sin = y / magnitude;
115 "x and y components of Rotation2d are zero\n", Thread.currentThread().getStackTrace());
125 this(angle.in(Radians));
135 final var R = rotationMatrix;
139 if (R.times(R.transpose()).minus(
Matrix.
eye(
Nat.
N2())).normF() > 1e-9) {
140 var msg =
"Rotation matrix isn't orthogonal\n\nR =\n" + R.getStorage().toString() +
'\n';
142 throw new IllegalArgumentException(msg);
144 if (Math.abs(R.det() - 1.0) > 1e-9) {
146 "Rotation matrix is orthogonal but not special orthogonal\n\nR =\n"
147 + R.getStorage().toString()
150 throw new IllegalArgumentException(msg);
176 return new Rotation2d(Math.toRadians(degrees));
186 return new Rotation2d(rotationsToRadians(rotations));
242 return times(1.0 / scalar);
261 m_cos * other.m_cos - m_sin * other.m_sin, m_cos * other.m_sin + m_sin * other.m_cos);
300 @Json.Property(
"radians")
302 return Math.atan2(m_sin, m_cos);
347 return m_sin / m_cos;
351 public String toString() {
364 && Math.hypot(m_cos - other.m_cos, m_sin - other.m_sin) < 1E-9;
368 public int hashCode() {
374 return plus(endValue.minus(
this).times(t));
static Rotation2d fromDegrees(double degrees)
Rotation2d relativeTo(Rotation2d other)
Rotation2d minus(Rotation2d other)
Rotation2d plus(Rotation2d other)
static final Rotation2d CW_90DEG
Rotation2d rotateBy(Rotation2d other)
static final Rotation2dProto proto
static final Rotation2d CW_PI_2
Rotation2d times(double scalar)
static final Rotation2d CCW_90DEG
Rotation2d div(double scalar)
Rotation2d(Matrix< N2, N2 > rotationMatrix)
static Rotation2d fromRadians(double radians)
Rotation2d(@Json.Alias(value="radians") double value)
static final Rotation2d k180deg
boolean equals(Object obj)
static final Rotation2d ZERO
static final Rotation2d PI
Matrix< N2, N2 > toMatrix()
static final Rotation2d CCW_PI_2
static Rotation2d fromRotations(double rotations)
Rotation2d(double x, double y)
static< R extends Num, C extends Num > Matrix< R, C > fill(Nat< R > rows, Nat< C > cols, double... data)
static< D extends Num > Matrix< D, D > eye(Nat< D > dim)
final double get(int row, int col)
static void reportError(String error, StackTraceElement[] stackTrace)