trajectory/Velocity3d: New (tested) features

- Define plus() transform function
- rename getSphericalVelocity() to getSpeed()
- Define genSpherical() generator (mapper to
spherical constructor from doubles).
- Define toStringSpherical() (Outputs Velocity3d
string w/ spherical coordinates)
This commit is contained in:
Ray Healy 2026-02-21 15:09:12 -05:00
parent 41d878dab8
commit 5617a3a91b
2 changed files with 40 additions and 4 deletions

View File

@ -49,6 +49,13 @@ public class Velocity3d {
this.cache_horizontalVelocity = horizontalVelocity.copy(); this.cache_horizontalVelocity = horizontalVelocity.copy();
} }
public static Velocity3d genSpherical(double radialVelocityMPS, double elevationAngleDeg, double azimuthAngleDeg) {
return new Velocity3d(
Units.MetersPerSecond.of(radialVelocityMPS),
Units.Degrees.of(elevationAngleDeg),
Units.Degrees.of(azimuthAngleDeg)
);
}
public Velocity3d(LinearVelocity radialVelocity, Angle elevationAngle, Angle azimuthAngle) { public Velocity3d(LinearVelocity radialVelocity, Angle elevationAngle, Angle azimuthAngle) {
this( this(
@ -191,7 +198,7 @@ public class Velocity3d {
/** /**
* @return The radial component of the spherical velocity (alias of this.getMagnitude()) * @return The radial component of the spherical velocity (alias of this.getMagnitude())
*/ */
public LinearVelocity getSphericalVelocity() { public LinearVelocity getSpeed() {
return this.getMagnitude(); return this.getMagnitude();
} }
@ -200,6 +207,14 @@ public class Velocity3d {
// Transforms and Conversions // Transforms and Conversions
// //
public Velocity3d plus(Velocity3d other) {
return new Velocity3d(
this.xVelocity.plus(other.xVelocity),
this.yVelocity.plus(other.yVelocity),
this.zVelocity.plus(other.zVelocity)
);
}
/** /**
* Subtract other from this vector and return the resulting vector * Subtract other from this vector and return the resulting vector
* @param other The vector to subtract from this one * @param other The vector to subtract from this one
@ -242,6 +257,12 @@ public class Velocity3d {
return String.format("Velocity3d(X: %s, Y: %s, Z: %s)", xVelocity, yVelocity, zVelocity); return String.format("Velocity3d(X: %s, Y: %s, Z: %s)", xVelocity, yVelocity, zVelocity);
} }
public String toStringSpherical() {
return String.format(
"Velocity3d(Speed: %s, Elevation: %s, Azimuth: %s)",
this.getSpeed(), this.getElevationAngle(), this.getAzimuthAngle()
);
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View File

@ -27,7 +27,7 @@ public class Velocity3dTest {
// Check spherical // Check spherical
assertEquals(45, test.getElevationAngle().in(Units.Degrees), epsilon, "Elevation"); assertEquals(45, test.getElevationAngle().in(Units.Degrees), epsilon, "Elevation");
assertEquals(Math.sqrt(2.0), test.getSphericalVelocity().in(Units.MetersPerSecond), epsilon, "Magnitude"); assertEquals(Math.sqrt(2.0), test.getSpeed().in(Units.MetersPerSecond), epsilon, "Magnitude");
} }
@ -58,11 +58,17 @@ public class Velocity3dTest {
} }
@Test void testSphericalConstructor() { @Test void testSphericalConstructor() {
testConstructorBase(new Velocity3d( Velocity3d test = new Velocity3d(
Units.MetersPerSecond.of(Math.sqrt(2)), Units.MetersPerSecond.of(Math.sqrt(2)),
Units.Degrees.of(45), Units.Degrees.of(45),
Units.Degrees.of(45) Units.Degrees.of(45)
)); );
testConstructorBase(test);
assertEquals(
test,
Velocity3d.genSpherical(Math.sqrt(2), 45, 45),
"genSpherical Test"
);
} }
// //
@ -79,6 +85,15 @@ public class Velocity3dTest {
// Test transforms // Test transforms
// //
@Test void testPlus() {
assertEquals(
new Velocity3d(5.0, 6.0, 7.0),
new Velocity3d(4.0, 5.0, 6.0).plus(
new Velocity3d(1.0, 1.0, 1.0)
)
);
}
@Test void testMinus() { @Test void testMinus() {
Velocity3d test = new Velocity3d(1.0, 1.0, 1.0).minus( Velocity3d test = new Velocity3d(1.0, 1.0, 1.0).minus(
new Velocity3d(2.0, 2.0, 2.0) new Velocity3d(2.0, 2.0, 2.0)