Skip to content

Commit

Permalink
move 6 constants and 8 methods from the Jolt class to a new Std class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Nov 23, 2024
1 parent 0b5ab67 commit 4e939b0
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 207 deletions.
92 changes: 0 additions & 92 deletions src/main/java/com/github/stephengold/joltjni/Jolt.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ of this software and associated documentation files (the "Software"), to deal
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.readonly.Vec3Arg;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
Expand All @@ -42,14 +41,6 @@ final public class Jolt {
* padding around bodies (in meters)
*/
final public static float cDefaultConvexRadius = 0.05f;
/**
* difference between 1 and the smallest float greater than 1
*/
final public static float FLT_EPSILON = 1.1920929e-7f;
/**
* largest finite value of type {@code float}
*/
final public static float FLT_MAX = Float.MAX_VALUE;
/**
* single-precision value of Pi
*/
Expand All @@ -66,26 +57,10 @@ final public class Jolt {
* value should match Jolt/Physics/PhysicsSettings.h
*/
final public static int cMaxPhysicsJobs = 2_048;
/**
* largest value of type {@code int}
*/
final public static int INT_MAX = Integer.MAX_VALUE;
/**
* generic null pointer (to expedite translation of C++ code)
*/
final public static Object nullptr = null;
/**
* standard error stream (to expedite translation of C++ code)
*/
public static final PrintStream cerr = System.err;
/**
* standard output stream (to expedite translation of C++ code)
*/
public static final PrintStream cout = System.out;
/**
* line separator (to expedite translation of C++ code)
*/
final public static String endl = System.lineSeparator();
// *************************************************************************
// constructors

Expand All @@ -97,37 +72,13 @@ private Jolt() {
// *************************************************************************
// new methods exposed

/**
* Return the inverse cosine of the specified single-precision ratio.
*
* @param ratio the input cosine ratio (≥-1, ≤1)
* @return the angle (in radians)
*/
native public static float acos(float ratio);

/**
* Return the inverse tangent of the specified single-precision ratio.
*
* @param ratio the input tangent ratio
* @return the angle (in radians)
*/
native public static float atan(float ratio);

/**
* Return the jolt-jni build-type string.
*
* @return either "Debug" or "Release"
*/
native public static String buildType();

/**
* Return the cosine of the specified single-precision angle.
*
* @param angle the input angle (in radians)
* @return the cosine ratio
*/
native public static float cos(float angle);

/**
* Convert the specified angle from degrees to radians.
*
Expand All @@ -153,24 +104,6 @@ public static float degreesToRadians(float degrees) {
*/
native public static void detLog(String message);

/**
* Return the exponential of the specified single-precision value.
*
* @param value the input exponent
* @return the exponential
*/
native public static float exp(float value);

/**
* Return the remainder when {@code numerator} is divided by
* {@code denominator}.
*
* @param numerator the numerator
* @param denominator the denominator
* @return the remainder (with the same sign as {@code numerator})
*/
native public static float fmod(float numerator, float denominator);

/**
* Return a string containing important configuration settings.
*
Expand Down Expand Up @@ -309,15 +242,6 @@ public static IntBuffer newDirectIntBuffer(int numInts) {
native public static float perlinNoise3(
float x, float y, float z, int xWrap, int yWrap, int zWrap);

/**
* Return the specified power of the specified single-precision base.
*
* @param base the base value
* @param exponent the exponent value
* @return the power value
*/
native public static float pow(float base, float exponent);

/**
* Dump profiler data.
*
Expand Down Expand Up @@ -423,22 +347,6 @@ public static float sign(float input) {
return result;
}

/**
* Return the sine of the specified single-precision angle.
*
* @param angle the input angle (in radians)
* @return the sine ratio
*/
native public static float sin(float angle);

/**
* Return the square root of the specified single-precision value.
*
* @param value the input value
* @return the square root
*/
native public static float sqrt(float value);

/**
* Return the square of the specified single-precision value.
*
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/com/github/stephengold/joltjni/Quat.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.operator.Op;
import com.github.stephengold.joltjni.readonly.QuatArg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;
import com.github.stephengold.joltjni.std.Std;

/**
* A math object used to represent rotations and orientations in 3-dimensional
Expand Down Expand Up @@ -129,12 +130,12 @@ public static Quat sEulerAngles(Vec3 angles) {
float halfY = 0.5f * angles.getY();
float halfZ = 0.5f * angles.getZ();

float cx = Jolt.cos(halfX);
float cy = Jolt.cos(halfY);
float cz = Jolt.cos(halfZ);
float sx = Jolt.sin(halfX);
float sy = Jolt.sin(halfY);
float sz = Jolt.sin(halfZ);
float cx = Std.cos(halfX);
float cy = Std.cos(halfY);
float cz = Std.cos(halfZ);
float sx = Std.sin(halfX);
float sy = Std.sin(halfY);
float sz = Std.sin(halfZ);

Quat result = new Quat(
cz * sx * cy - sz * cx * sy,
Expand All @@ -153,7 +154,7 @@ public static Quat sEulerAngles(Vec3 angles) {
* @return a new quaternion
*/
public static Quat sFromTo(Vec3Arg from, Vec3Arg to) {
float lenV1V2 = Jolt.sqrt(from.lengthSq() * to.lengthSq());
float lenV1V2 = Std.sqrt(from.lengthSq() * to.lengthSq());
float w = lenV1V2 + from.dot(to);

if (w == 0f) {
Expand Down Expand Up @@ -193,16 +194,16 @@ public static Quat sRandom(DefaultRandomEngine engine) {
}

float x0 = distro.nextFloat(engine);
float r1 = Jolt.sqrt(1f - x0);
float r2 = Jolt.sqrt(x0);
float r1 = Std.sqrt(1f - x0);
float r2 = Std.sqrt(x0);

float px = 2f * Jolt.JPH_PI * distro.nextFloat(engine);
float py = 2f * Jolt.JPH_PI * distro.nextFloat(engine);

float x = r1 * Jolt.sin(px);
float y = r1 * Jolt.cos(px);
float z = r2 * Jolt.sin(py);
float w = r2 * Jolt.cos(py);
float x = r1 * Std.sin(px);
float y = r1 * Std.cos(px);
float z = r2 * Std.sin(py);
float w = r2 * Std.cos(py);
Quat result = new Quat(x, y, z, w);

return result;
Expand All @@ -218,8 +219,8 @@ public static Quat sRandom(DefaultRandomEngine engine) {
public static Quat sRotation(Vec3 axis, float angle) {
assert axis.isNormalized();

float qw = Jolt.cos(0.5f * angle);
float s = Jolt.sin(0.5f * angle);
float qw = Std.cos(0.5f * angle);
float s = Std.sin(0.5f * angle);
float qx = axis.getX() * s;
float qy = axis.getY() * s;
float qz = axis.getZ() * s;
Expand Down Expand Up @@ -322,7 +323,7 @@ public boolean isNormalized(float tolerance) {
@Override
public float length() {
float lengthSq = lengthSq();
float result = Jolt.sqrt(lengthSq);
float result = Std.sqrt(lengthSq);

return result;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/github/stephengold/joltjni/Vec3.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.operator.Op;
import com.github.stephengold.joltjni.readonly.RVec3Arg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;
import com.github.stephengold.joltjni.std.Std;
import java.util.Objects;

/**
Expand Down Expand Up @@ -323,10 +324,10 @@ public static Vec3 sum(Vec3Arg... vArray) {
* @return a new unit vector
*/
public static Vec3 sUnitSpherical(float theta, float phi) {
float sinTheta = Jolt.sin(theta);
float vx = sinTheta * Jolt.cos(phi);
float vy = sinTheta * Jolt.sin(phi);
float vz = Jolt.cos(theta);
float sinTheta = Std.sin(theta);
float vx = sinTheta * Std.cos(phi);
float vy = sinTheta * Std.sin(phi);
float vz = Std.cos(theta);
Vec3 result = new Vec3(vx, vy, vz);

return result;
Expand Down Expand Up @@ -413,10 +414,10 @@ public float get(int index) {
@Override
public Vec3 getNormalizedPerpendicular() {
if (Math.abs(x) > Math.abs(y)) {
float len = Jolt.sqrt(x * x + z * z);
float len = Std.sqrt(x * x + z * z);
return new Vec3(z / len, 0f, -x / len);
} else {
float len = Jolt.sqrt(y * y + z * z);
float len = Std.sqrt(y * y + z * z);
return new Vec3(0f, z / len, -y / len);
}
}
Expand Down Expand Up @@ -520,7 +521,7 @@ public boolean isNormalized(float tolerance) {
@Override
public float length() {
float lengthSq = lengthSq();
float result = Jolt.sqrt(lengthSq);
float result = Std.sqrt(lengthSq);

return result;
}
Expand Down Expand Up @@ -565,7 +566,7 @@ public Vec3 normalizedOr(Vec3Arg zeroValue) {
if (lengthSq == 0f) {
result = new Vec3(zeroValue);
} else {
float length = Jolt.sqrt(lengthSq);
float length = Std.sqrt(lengthSq);
if (length == 0f) {
result = new Vec3(zeroValue);
} else {
Expand Down
Loading

0 comments on commit 4e939b0

Please sign in to comment.