Class MathUtils
java.lang.Object
org.photonvision.common.util.math.MathUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic Transform3d
AprilTag returns a camera-to-tag transform in EDN, but the tag's z-axis points into the tag instead of away from it and towards the camera.static Pose3d
static Transform3d
convertOpenCVtoPhotonTransform
(Transform3d cameraToTarget3d) OpenCV uses the EDN coordinate system, but WPILib uses NWU.static double
getPercentile
(List<Double> list, double p) Get the value of the nTh percentile of a liststatic double
lerp
(double startValue, double endValue, double t) Linearly interpolates between two values.static double
map
(double value, double in_min, double in_max, double out_min, double out_max) static int
map
(int value, int inMin, int inMax, int outMin, int outMax) static long
microsToNanos
(long micros) static long
millisToNanos
(long millis) static long
nanosToMicros
(long nanos) static double
nanosToMillis
(double nanos) static double
nanosToMillis
(long nanos) static Pose3d
opencvRTtoPose3d
(org.opencv.core.Mat rVec, org.opencv.core.Mat tVec) Convert an Opencv rvec+tvec pair to a Pose3d.static int
quantize
(int value, int[] allowableSteps) Constrain a value to only take on certain values.static void
rotationToOpencvRvec
(Rotation3d rotation, org.opencv.core.Mat rvecOutput) static double
roundTo
(double value, int to) static int
safeDivide
(int quotient, int divisor) static double
static long
-
Method Details
-
toSlope
-
safeDivide
public static int safeDivide(int quotient, int divisor) -
roundTo
public static double roundTo(double value, int to) -
nanosToMillis
public static double nanosToMillis(long nanos) -
nanosToMillis
public static double nanosToMillis(double nanos) -
millisToNanos
public static long millisToNanos(long millis) -
microsToNanos
public static long microsToNanos(long micros) -
nanosToMicros
public static long nanosToMicros(long nanos) -
quantize
public static int quantize(int value, int[] allowableSteps) Constrain a value to only take on certain values. Pick the next-highest allowed value in the array if in-between.- Parameters:
value
- value to quantizeallowableSteps
- sorted array of the allowed values- Returns:
- quantized value
-
map
public static double map(double value, double in_min, double in_max, double out_min, double out_max) -
map
public static int map(int value, int inMin, int inMax, int outMin, int outMax) -
wpiNanoTime
public static long wpiNanoTime() -
getPercentile
Get the value of the nTh percentile of a list- Parameters:
list
- The list to evaluatep
- The percentile, in [0,100]- Returns:
-
lerp
public static double lerp(double startValue, double endValue, double t) Linearly interpolates between two values.- Parameters:
startValue
- The start value.endValue
- The end value.t
- The fraction for interpolation.- Returns:
- The interpolated value.
-
convertOpenCVtoPhotonTransform
OpenCV uses the EDN coordinate system, but WPILib uses NWU. Converts a camera-to-target transformation from EDN to NWU.Note: The detected target's rvec and tvec perform a rotation-translation transformation which converts points in the target's coordinate system to the camera's. This means applying the transformation to the target point (0,0,0) for example would give the target's center relative to the camera. Conveniently, if we make a translation-rotation transformation out of these components instead, we get the transformation from the camera to the target.
- Parameters:
cameraToTarget3d
- A camera-to-target Transform3d in EDN.- Returns:
- A camera-to-target Transform3d in NWU.
-
convertApriltagtoOpenCV
AprilTag returns a camera-to-tag transform in EDN, but the tag's z-axis points into the tag instead of away from it and towards the camera. This means we have to correct the transformation's rotation.- Parameters:
pose
- The Transform3d with translation and rotation directly from theAprilTagPoseEstimate
.
-
convertArucotoOpenCV
-
rotationToOpencvRvec
-
opencvRTtoPose3d
Convert an Opencv rvec+tvec pair to a Pose3d.- Parameters:
rVec
- Axis-angle rotation vector, where norm(rVec) is the angle about a unit vector in the direction of rVectVec
- 3D translation- Returns:
- Pose3d representing the same rigid transform
-