RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Translation3d.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 org.wpilib.math.geometry.proto.Translation3dProto;
15import org.wpilib.math.geometry.struct.Translation3dStruct;
16import org.wpilib.math.interpolation.Interpolatable;
17import org.wpilib.math.linalg.VecBuilder;
18import org.wpilib.math.linalg.Vector;
19import org.wpilib.math.numbers.N3;
20import org.wpilib.math.util.MathUtil;
21import org.wpilib.units.measure.Distance;
22import org.wpilib.util.protobuf.ProtobufSerializable;
23import org.wpilib.util.struct.StructSerializable;
24
32@Json
33public final class Translation3d
34 implements Interpolatable<Translation3d>, ProtobufSerializable, StructSerializable {
40 public static final Translation3d ZERO = new Translation3d();
41
42 @Json.Property("x")
43 private final double m_x;
44
45 @Json.Property("y")
46 private final double m_y;
47
48 @Json.Property("z")
49 private final double m_z;
50
52 public Translation3d() {
53 this(0.0, 0.0, 0.0);
54 }
55
63 @Json.Creator
64 public Translation3d(double x, double y, double z) {
65 m_x = x;
66 m_y = y;
67 m_z = z;
68 }
69
77 public Translation3d(double distance, Rotation3d angle) {
78 final var rectangular = new Translation3d(distance, 0.0, 0.0).rotateBy(angle);
79 m_x = rectangular.getX();
80 m_y = rectangular.getY();
81 m_z = rectangular.getZ();
82 }
83
92 public Translation3d(Distance x, Distance y, Distance z) {
93 this(x.in(Meters), y.in(Meters), z.in(Meters));
94 }
95
103 public Translation3d(Translation2d translation) {
104 this(translation.getX(), translation.getY(), 0.0);
105 }
106
113 public Translation3d(Vector<N3> vector) {
114 this(vector.get(0), vector.get(1), vector.get(2));
115 }
116
125 public double getDistance(Translation3d other) {
126 double dx = other.m_x - m_x;
127 double dy = other.m_y - m_y;
128 double dz = other.m_z - m_z;
129 return Math.sqrt(dx * dx + dy * dy + dz * dz);
130 }
131
141 public double getSquaredDistance(Translation3d other) {
142 double dx = other.m_x - m_x;
143 double dy = other.m_y - m_y;
144 double dz = other.m_z - m_z;
145 return dx * dx + dy * dy + dz * dz;
146 }
147
153 public double getX() {
154 return m_x;
155 }
156
162 public double getY() {
163 return m_y;
164 }
165
171 public double getZ() {
172 return m_z;
173 }
174
180 public Distance getMeasureX() {
181 return Meters.of(m_x);
182 }
183
189 public Distance getMeasureY() {
190 return Meters.of(m_y);
191 }
192
198 public Distance getMeasureZ() {
199 return Meters.of(m_z);
200 }
201
208 return VecBuilder.fill(m_x, m_y, m_z);
209 }
210
216 public double getNorm() {
217 return Math.sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
218 }
219
226 public double getSquaredNorm() {
227 return m_x * m_x + m_y * m_y + m_z * m_z;
228 }
229
240 final var p = new Quaternion(0.0, m_x, m_y, m_z);
241 final var qprime = other.getQuaternion().times(p).times(other.getQuaternion().inverse());
242 return new Translation3d(qprime.getX(), qprime.getY(), qprime.getZ());
243 }
244
253 return this.minus(other).rotateBy(rot).plus(other);
254 }
255
264 public double dot(Translation3d other) {
265 return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
266 }
267
279 return VecBuilder.fill(
280 m_y * other.m_z - other.m_y * m_z,
281 m_z * other.m_x - other.m_z * m_x,
282 m_x * other.m_y - other.m_x * m_y);
283 }
284
291 return new Translation2d(m_x, m_y);
292 }
293
304 return new Translation3d(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z);
305 }
306
317 return new Translation3d(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z);
318 }
319
327 return new Translation3d(-m_x, -m_y, -m_z);
328 }
329
338 public Translation3d times(double scalar) {
339 return new Translation3d(m_x * scalar, m_y * scalar, m_z * scalar);
340 }
341
350 public Translation3d div(double scalar) {
351 return new Translation3d(m_x / scalar, m_y / scalar, m_z / scalar);
352 }
353
360 public Translation3d nearest(Collection<Translation3d> translations) {
361 return Collections.min(translations, Comparator.comparing(this::getDistance));
362 }
363
364 @Override
365 public String toString() {
366 return String.format("Translation3d(X: %.2f, Y: %.2f, Z: %.2f)", m_x, m_y, m_z);
367 }
368
375 @Override
376 public boolean equals(Object obj) {
377 return obj instanceof Translation3d other
378 && Math.abs(other.m_x - m_x) < 1E-9
379 && Math.abs(other.m_y - m_y) < 1E-9
380 && Math.abs(other.m_z - m_z) < 1E-9;
381 }
382
383 @Override
384 public int hashCode() {
385 return Objects.hash(m_x, m_y, m_z);
386 }
387
388 @Override
389 public Translation3d interpolate(Translation3d endValue, double t) {
390 return new Translation3d(
391 MathUtil.lerp(this.getX(), endValue.getX(), t),
392 MathUtil.lerp(this.getY(), endValue.getY(), t),
393 MathUtil.lerp(this.getZ(), endValue.getZ(), t));
394 }
395
397 public static final Translation3dProto proto = new Translation3dProto();
398
400 public static final Translation3dStruct struct = new Translation3dStruct();
401}
Quaternion times(double scalar)
Translation3d(double distance, Rotation3d angle)
Translation3d(double x, double y, double z)
Vector< N3 > cross(Translation3d other)
Translation3d rotateBy(Rotation3d other)
Translation3d rotateAround(Translation3d other, Rotation3d rot)
Translation3d plus(Translation3d other)
double getSquaredDistance(Translation3d other)
Translation3d nearest(Collection< Translation3d > translations)
Translation3d times(double scalar)
Translation3d minus(Translation3d other)
double getDistance(Translation3d other)
Translation3d(Translation2d translation)
Translation3d(Distance x, Distance y, Distance z)
Translation3d div(double scalar)
static final Translation3dProto proto
static Vector< N1 > fill(double n1)