RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Translation2d.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.math.geometry;
6
7import static org.wpilib.units.Units.Meters;
8
9import io.avaje.jsonb.Json;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Comparator;
13import java.util.Objects;
14import java.util.Optional;
15import org.wpilib.math.geometry.proto.Translation2dProto;
16import org.wpilib.math.geometry.struct.Translation2dStruct;
17import org.wpilib.math.interpolation.Interpolatable;
18import org.wpilib.math.linalg.VecBuilder;
19import org.wpilib.math.linalg.Vector;
20import org.wpilib.math.numbers.N2;
21import org.wpilib.math.util.MathUtil;
22import org.wpilib.units.measure.Distance;
23import org.wpilib.util.protobuf.ProtobufSerializable;
24import org.wpilib.util.struct.StructSerializable;
25
32@Json
33public final class Translation2d
34 implements Interpolatable<Translation2d>, ProtobufSerializable, StructSerializable {
40 public static final Translation2d ZERO = new Translation2d();
41
42 @Json.Property("x")
43 private final double m_x;
44
45 @Json.Property("y")
46 private final double m_y;
47
49 public Translation2d() {
50 this(0.0, 0.0);
51 }
52
59 @Json.Creator
60 public Translation2d(double x, double y) {
61 m_x = x;
62 m_y = y;
63 }
64
72 public Translation2d(double distance, Rotation2d angle) {
73 m_x = distance * angle.getCos();
74 m_y = distance * angle.getSin();
75 }
76
84 public Translation2d(Distance x, Distance y) {
85 this(x.in(Meters), y.in(Meters));
86 }
87
94 public Translation2d(Vector<N2> vector) {
95 this(vector.get(0), vector.get(1));
96 }
97
106 public double getDistance(Translation2d other) {
107 return Math.hypot(other.m_x - m_x, other.m_y - m_y);
108 }
109
120 public double getSquaredDistance(Translation2d other) {
121 double dx = other.m_x - m_x;
122 double dy = other.m_y - m_y;
123 return dx * dx + dy * dy;
124 }
125
131 public double getX() {
132 return m_x;
133 }
134
140 public double getY() {
141 return m_y;
142 }
143
149 public Distance getMeasureX() {
150 return Meters.of(m_x);
151 }
152
158 public Distance getMeasureY() {
159 return Meters.of(m_y);
160 }
161
168 return VecBuilder.fill(m_x, m_y);
169 }
170
176 public double getNorm() {
177 return Math.hypot(m_x, m_y);
178 }
179
186 public double getSquaredNorm() {
187 return m_x * m_x + m_y * m_y;
188 }
189
195 public Optional<Rotation2d> getAngle() {
196 if (Math.hypot(m_x, m_y) > 1e-6) {
197 return Optional.of(new Rotation2d(m_x, m_y));
198 } else {
199 return Optional.empty();
200 }
201 }
202
221 return new Translation2d(
222 m_x * other.getCos() - m_y * other.getSin(), m_x * other.getSin() + m_y * other.getCos());
223 }
224
238 return new Translation2d(
239 (m_x - other.getX()) * rot.getCos() - (m_y - other.getY()) * rot.getSin() + other.getX(),
240 (m_x - other.getX()) * rot.getSin() + (m_y - other.getY()) * rot.getCos() + other.getY());
241 }
242
251 public double dot(Translation2d other) {
252 return m_x * other.m_x + m_y * other.m_y;
253 }
254
263 public double cross(Translation2d other) {
264 return m_x * other.m_y - m_y * other.m_x;
265 }
266
276 return new Translation2d(m_x + other.m_x, m_y + other.m_y);
277 }
278
288 return new Translation2d(m_x - other.m_x, m_y - other.m_y);
289 }
290
298 return new Translation2d(-m_x, -m_y);
299 }
300
309 public Translation2d times(double scalar) {
310 return new Translation2d(m_x * scalar, m_y * scalar);
311 }
312
321 public Translation2d div(double scalar) {
322 return new Translation2d(m_x / scalar, m_y / scalar);
323 }
324
331 public Translation2d nearest(Collection<Translation2d> translations) {
332 return Collections.min(translations, Comparator.comparing(this::getDistance));
333 }
334
335 @Override
336 public String toString() {
337 return String.format("Translation2d(X: %.2f, Y: %.2f)", m_x, m_y);
338 }
339
346 @Override
347 public boolean equals(Object obj) {
348 return obj instanceof Translation2d other
349 && Math.abs(other.m_x - m_x) < 1E-9
350 && Math.abs(other.m_y - m_y) < 1E-9;
351 }
352
353 @Override
354 public int hashCode() {
355 return Objects.hash(m_x, m_y);
356 }
357
358 @Override
359 public Translation2d interpolate(Translation2d endValue, double t) {
360 return new Translation2d(
361 MathUtil.lerp(this.getX(), endValue.getX(), t),
362 MathUtil.lerp(this.getY(), endValue.getY(), t));
363 }
364
366 public static final Translation2dProto proto = new Translation2dProto();
367
369 public static final Translation2dStruct struct = new Translation2dStruct();
370}
Translation2d rotateAround(Translation2d other, Rotation2d rot)
double getDistance(Translation2d other)
static final Translation2dProto proto
Translation2d minus(Translation2d other)
Translation2d rotateBy(Rotation2d other)
Translation2d div(double scalar)
Translation2d nearest(Collection< Translation2d > translations)
Translation2d plus(Translation2d other)
Translation2d(double distance, Rotation2d angle)
double cross(Translation2d other)
double getSquaredDistance(Translation2d other)
Translation2d times(double scalar)
static Vector< N1 > fill(double n1)