Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] Add vector projection and geometry vector conversions #6343

Merged
merged 19 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions wpimath/src/main/java/edu/wpi/first/math/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public Vector(Matrix<R, N1> other) {
super(other);
}

/**
* Returns an element of the vector at a specified row.
*
* @param row The row that the element is located at.
* @return An element of the vector.
*/
public double get(int row) {
return get(row, 0);
}

@Override
public Vector<R> times(double value) {
return new Vector<>(this.m_storage.scale(value));
Expand Down Expand Up @@ -105,12 +115,35 @@ public double dot(Vector<R> other) {
* @return The norm.
*/
public double norm() {
double squaredNorm = 0.0;
return Math.sqrt(dot(this));
}

for (int i = 0; i < getNumRows(); ++i) {
squaredNorm += get(i, 0) * get(i, 0);
}
/**
* Returns the unit vector parallel with this vector.
*
* @return The unit vector.
*/
public Vector<R> unit() {
return div(norm());
}

/**
* Returns the scalar component of this vector along another.
*
* @param other The vector to find the component of this along.
* @return The component.
*/
public double comp(Vector<R> other) {
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
return dot(other) / other.norm();
}

return Math.sqrt(squaredNorm);
/**
* Returns the projection of this vector along another.
*
* @param other The vector to project along.
* @return The projection.
*/
public Vector<R> proj(Vector<R> other) {
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
return other.unit().times(comp(other));
}
}
13 changes: 13 additions & 0 deletions wpimath/src/main/java/edu/wpi/first/math/geometry/Rotation2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.wpi.first.math.MatBuilder;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.Nat;
import edu.wpi.first.math.geometry.proto.Rotation2dProto;
import edu.wpi.first.math.geometry.struct.Rotation2dStruct;
import edu.wpi.first.math.interpolation.Interpolatable;
import edu.wpi.first.math.numbers.N2;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.units.Angle;
import edu.wpi.first.units.Measure;
Expand Down Expand Up @@ -245,6 +249,15 @@ public double getTan() {
return m_sin / m_cos;
}

/**
* Returns the rotation matrix representation of the Rotation2d.
*
* @return A rotation matrix.
*/
public Matrix<N2, N2> getRotationMatrix() {
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
return MatBuilder.fill(Nat.N2(), Nat.N2(), m_cos, -m_sin, m_sin, m_cos);
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString() {
return String.format("Rotation2d(Rads: %.2f, Deg: %.2f)", m_value, Math.toDegrees(m_value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.VecBuilder;
import edu.wpi.first.math.Vector;
import edu.wpi.first.math.geometry.proto.Translation2dProto;
import edu.wpi.first.math.geometry.struct.Translation2dStruct;
import edu.wpi.first.math.interpolation.Interpolatable;
import edu.wpi.first.math.numbers.N2;
import edu.wpi.first.units.Distance;
import edu.wpi.first.units.Measure;
import edu.wpi.first.util.protobuf.ProtobufSerializable;
Expand Down Expand Up @@ -78,6 +81,16 @@ public Translation2d(Measure<Distance> x, Measure<Distance> y) {
this(x.in(Meters), y.in(Meters));
}

/**
* Constructs a Translation2d from the provided translation vector's X and Y components. The
* values are assumed to be in meters.
*
* @param vector The translation vector to represent.
*/
public Translation2d(Vector<N2> vector) {
this(vector.get(0), vector.get(1));
}

/**
* Calculates the distance between two translations in 2D space.
*
Expand Down Expand Up @@ -110,6 +123,15 @@ public double getY() {
return m_y;
}

/**
* Returns a vector representation of this translation.
*
* @return A Vector.
*/
public Vector<N2> getVector() {
return VecBuilder.fill(m_x, m_y);
}

/**
* Returns the norm, or distance from the origin to the translation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.VecBuilder;
import edu.wpi.first.math.Vector;
import edu.wpi.first.math.geometry.proto.Translation3dProto;
import edu.wpi.first.math.geometry.struct.Translation3dStruct;
import edu.wpi.first.math.interpolation.Interpolatable;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.units.Distance;
import edu.wpi.first.units.Measure;
import edu.wpi.first.util.protobuf.ProtobufSerializable;
Expand Down Expand Up @@ -83,6 +86,16 @@ public Translation3d(Measure<Distance> x, Measure<Distance> y, Measure<Distance>
this(x.in(Meters), y.in(Meters), z.in(Meters));
}

/**
* Constructs a Translation3d from the provided translation vector's X, Y, and Z components. The
* values are assumed to be in meters.
*
* @param vector The translation vector to represent.
*/
public Translation3d(Vector<N3> vector) {
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
this(vector.get(0), vector.get(1), vector.get(2));
}

/**
* Calculates the distance between two translations in 3D space.
*
Expand Down Expand Up @@ -126,6 +139,15 @@ public double getZ() {
return m_z;
}

/**
* Returns a vector representation of this translation.
*
* @return A Vector.
*/
public Vector<N3> getVector() {
AngleSideAngle marked this conversation as resolved.
Show resolved Hide resolved
return VecBuilder.fill(m_x, m_y, m_z);
}

/**
* Returns the norm, or distance from the origin to the translation.
*
Expand Down
Loading