RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
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
5
package
org.wpilib.math.geometry;
6
7
import
static
org.wpilib.units.Units.Meters;
8
9
import
io.avaje.jsonb.Json;
10
import
java.util.Collection;
11
import
java.util.Collections;
12
import
java.util.Comparator;
13
import
java.util.Objects;
14
import
java.util.Optional;
15
import
org.wpilib.math.geometry.proto.Translation2dProto;
16
import
org.wpilib.math.geometry.struct.Translation2dStruct;
17
import
org.wpilib.math.interpolation.Interpolatable;
18
import
org.wpilib.math.linalg.VecBuilder;
19
import
org.wpilib.math.linalg.Vector;
20
import
org.wpilib.math.numbers.N2;
21
import
org.wpilib.math.util.MathUtil;
22
import
org.wpilib.units.measure.Distance;
23
import
org.wpilib.util.protobuf.ProtobufSerializable;
24
import
org.wpilib.util.struct.StructSerializable;
25
32
@Json
33
public
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
167
public
Vector<N2>
toVector
() {
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
220
public
Translation2d
rotateBy
(
Rotation2d
other) {
221
return
new
Translation2d
(
222
m_x * other.
getCos
() - m_y * other.
getSin
(), m_x * other.
getSin
() + m_y * other.
getCos
());
223
}
224
237
public
Translation2d
rotateAround
(
Translation2d
other,
Rotation2d
rot) {
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
275
public
Translation2d
plus
(
Translation2d
other) {
276
return
new
Translation2d
(m_x + other.m_x, m_y + other.m_y);
277
}
278
287
public
Translation2d
minus
(
Translation2d
other) {
288
return
new
Translation2d
(m_x - other.m_x, m_y - other.m_y);
289
}
290
297
public
Translation2d
unaryMinus
() {
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
}
org.wpilib.math.geometry.Rotation2d
Definition
Rotation2d.java:30
org.wpilib.math.geometry.Rotation2d.Rotation2d
Rotation2d()
Definition
Rotation2d.java:84
org.wpilib.math.geometry.Rotation2d.getCos
double getCos()
Definition
Rotation2d.java:328
org.wpilib.math.geometry.Rotation2d.getSin
double getSin()
Definition
Rotation2d.java:337
org.wpilib.math.geometry.Translation2d.rotateAround
Translation2d rotateAround(Translation2d other, Rotation2d rot)
Definition
Translation2d.java:237
org.wpilib.math.geometry.Translation2d.getDistance
double getDistance(Translation2d other)
Definition
Translation2d.java:106
org.wpilib.math.geometry.Translation2d.ZERO
static final Translation2d ZERO
Definition
Translation2d.java:40
org.wpilib.math.geometry.Translation2d.proto
static final Translation2dProto proto
Definition
Translation2d.java:366
org.wpilib.math.geometry.Translation2d.Translation2d
Translation2d(double x, double y)
Definition
Translation2d.java:60
org.wpilib.math.geometry.Translation2d.getY
double getY()
Definition
Translation2d.java:140
org.wpilib.math.geometry.Translation2d.dot
double dot(Translation2d other)
Definition
Translation2d.java:251
org.wpilib.math.geometry.Translation2d.minus
Translation2d minus(Translation2d other)
Definition
Translation2d.java:287
org.wpilib.math.geometry.Translation2d.unaryMinus
Translation2d unaryMinus()
Definition
Translation2d.java:297
org.wpilib.math.geometry.Translation2d.Translation2d
Translation2d()
Definition
Translation2d.java:49
org.wpilib.math.geometry.Translation2d.rotateBy
Translation2d rotateBy(Rotation2d other)
Definition
Translation2d.java:220
org.wpilib.math.geometry.Translation2d.getAngle
Optional< Rotation2d > getAngle()
Definition
Translation2d.java:195
org.wpilib.math.geometry.Translation2d.getSquaredNorm
double getSquaredNorm()
Definition
Translation2d.java:186
org.wpilib.math.geometry.Translation2d.div
Translation2d div(double scalar)
Definition
Translation2d.java:321
org.wpilib.math.geometry.Translation2d.nearest
Translation2d nearest(Collection< Translation2d > translations)
Definition
Translation2d.java:331
org.wpilib.math.geometry.Translation2d.plus
Translation2d plus(Translation2d other)
Definition
Translation2d.java:275
org.wpilib.math.geometry.Translation2d.Translation2d
Translation2d(Distance x, Distance y)
Definition
Translation2d.java:84
org.wpilib.math.geometry.Translation2d.getX
double getX()
Definition
Translation2d.java:131
org.wpilib.math.geometry.Translation2d.Translation2d
Translation2d(double distance, Rotation2d angle)
Definition
Translation2d.java:72
org.wpilib.math.geometry.Translation2d.cross
double cross(Translation2d other)
Definition
Translation2d.java:263
org.wpilib.math.geometry.Translation2d.getSquaredDistance
double getSquaredDistance(Translation2d other)
Definition
Translation2d.java:120
org.wpilib.math.geometry.Translation2d.Translation2d
Translation2d(Vector< N2 > vector)
Definition
Translation2d.java:94
org.wpilib.math.geometry.Translation2d.getMeasureY
Distance getMeasureY()
Definition
Translation2d.java:158
org.wpilib.math.geometry.Translation2d.getMeasureX
Distance getMeasureX()
Definition
Translation2d.java:149
org.wpilib.math.geometry.Translation2d.getNorm
double getNorm()
Definition
Translation2d.java:176
org.wpilib.math.geometry.Translation2d.toVector
Vector< N2 > toVector()
Definition
Translation2d.java:167
org.wpilib.math.geometry.Translation2d.equals
boolean equals(Object obj)
Definition
Translation2d.java:347
org.wpilib.math.geometry.Translation2d.times
Translation2d times(double scalar)
Definition
Translation2d.java:309
org.wpilib.math.geometry.proto.Translation2dProto
Definition
Translation2dProto.java:12
org.wpilib.math.geometry.struct.Translation2dStruct
Definition
Translation2dStruct.java:11
org.wpilib.math.linalg.VecBuilder
Definition
VecBuilder.java:23
org.wpilib.math.linalg.VecBuilder.fill
static Vector< N1 > fill(double n1)
Definition
VecBuilder.java:46
org.wpilib.math.linalg.Vector
Definition
Vector.java:28
org.wpilib.math.linalg.Vector.get
double get(int row)
Definition
Vector.java:67
org.wpilib.math.interpolation.Interpolatable
Definition
Interpolatable.java:15
org.wpilib.util.protobuf.ProtobufSerializable
Definition
ProtobufSerializable.java:16
org.wpilib.util.struct.StructSerializable
Definition
StructSerializable.java:16
org
wpilib
math
geometry
Translation2d.java
Generated by
1.18.0