Skip to content

Commit

Permalink
Jolt: add 6 public hashing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jan 31, 2025
1 parent c6b1496 commit 0fc3160
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
92 changes: 91 additions & 1 deletion src/main/java/com/github/stephengold/joltjni/Jolt.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,6 +21,8 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.readonly.ConstBodyId;
import com.github.stephengold.joltjni.readonly.RVec3Arg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -165,6 +167,84 @@ public static long hashBytes(RVec3 vector, long oldHash) {
return result;
}

/**
* Combine the specified character ID with the specified hash code.
*
* @param id the ID to combine (not null, unaffected)
* @param oldHash the old hash code
* @return the new hash code
*/
public static long hashCombine(long oldHash, CharacterId id) {
long idVa = id.va();
long result = hashCombineCharacterId(oldHash, idVa);

return result;
}

/**
* Combine the specified body ID with the specified hash code.
*
* @param id the ID to combine (not null, unaffected)
* @param oldHash the old hash code
* @return the new hash code
*/
public static long hashCombine(long oldHash, ConstBodyId id) {
long idVa = id.targetVa();
long result = hashCombineBodyId(oldHash, idVa);

return result;
}

/**
* Combine the specified 32-bit integer with the specified hash code.
*
* @param oldHash the old hash code
* @param iValue the integer value to combine
* @return the new hash code
*/
native public static long hashCombine(long oldHash, int iValue);

/**
* Combine the specified 64-bit integer with the specified hash code.
*
* @param oldHash the old hash code
* @param lValue the integer value to combine
* @return the new hash code
*/
native public static long hashCombine(long oldHash, long lValue);

/**
* Combine the specified location vector with the specified hash code.
*
* @param rvec the vector to combine (not null, unaffected)
* @param oldHash the old hash code
* @return the new hash code
*/
public static long hashCombine(long oldHash, RVec3Arg rvec) {
double xx = rvec.xx();
double yy = rvec.yy();
double zz = rvec.zz();
long result = hashCombineRVec3(oldHash, xx, yy, zz);

return result;
}

/**
* Combine the specified vector with the specified hash code.
*
* @param vec the vector to combine (not null, unaffected)
* @param oldHash the old hash code
* @return the new hash code
*/
public static long hashCombine(long oldHash, Vec3Arg vec) {
float x = vec.getX();
float y = vec.getY();
float z = vec.getZ();
long result = hashCombineVec3(oldHash, x, y, z);

return result;
}

/**
* Test whether the native library implements debug rendering. (native
* macro: JPH_DEBUG_RENDERER)
Expand Down Expand Up @@ -427,6 +507,16 @@ native private static long hashBytes(
native private static long hashBytes(
float qx, float qy, float qz, float qw, long oldHash);

native private static long hashCombineBodyId(long oldHash, long idVa);

native private static long hashCombineCharacterId(long oldHash, long idVa);

native private static long hashCombineRVec3(
long oldHash, double xx, double yy, double zz);

native private static long hashCombineVec3(
long oldHash, float x, float y, float z);

native private static boolean rayAaBoxHits(float startX, float startY,
float startZ, float offsetX, float offsetY, float offsetZ,
float minX, float minY, float minZ, float maxX, float maxY,
Expand Down
79 changes: 79 additions & 0 deletions src/main/native/glue/j/Jolt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ SOFTWARE.
#include "Jolt/Jolt.h"
#include "Jolt/ConfigurationString.h"
#include "Jolt/Core/Factory.h"
#include "Jolt/Core/HashCombine.h"
#include "Jolt/Core/Profiler.h"
#include "Jolt/Core/TempAllocator.h"
#include "Jolt/Geometry/RayAABox.h"
#include "Jolt/Physics/Body/BodyID.h"
#include "Jolt/Physics/Character/CharacterID.h"
#include "Jolt/Physics/DeterminismLog.h"
#include "Jolt/RegisterTypes.h"
#include "TestFramework/External/Perlin.h"
Expand Down Expand Up @@ -115,6 +118,30 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashBytes__JI
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombine
* Signature: (JI)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombine__JI
(JNIEnv *, jclass, jlong oldHash, jint iValue) {
uint64 result = oldHash;
HashCombine(result, iValue);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombine
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombine__JJ
(JNIEnv *, jclass, jlong oldHash, jlong lValue) {
uint64 result = oldHash;
HashCombine(result, (uint64)lValue);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: implementsDebugRendering
Expand Down Expand Up @@ -374,6 +401,58 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashBytes__FFFF
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombineBodyId
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombineBodyId
(JNIEnv *, jclass, jlong oldHash, jlong idVa) {
uint64 result = oldHash;
const BodyID * const pId = reinterpret_cast<BodyID *> (idVa);
HashCombine(result, *pId);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombineCharacterId
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombineCharacterId
(JNIEnv *, jclass, jlong oldHash, jlong idVa) {
uint64 result = oldHash;
const CharacterID * const pId = reinterpret_cast<CharacterID *> (idVa);
HashCombine(result, *pId);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombineRVec3
* Signature: (JDDD)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombineRVec3
(JNIEnv *, jclass, jlong oldHash, jdouble xx, jdouble yy, jdouble zz) {
uint64 result = oldHash;
const RVec3 rvec(xx, yy, zz);
HashCombine(result, rvec);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: hashCombineVec3
* Signature: (JFFF)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Jolt_hashCombineVec3
(JNIEnv *, jclass, jlong oldHash, jfloat x, jfloat y, jfloat z) {
uint64 result = oldHash;
const Vec3 vec(x, y, z);
HashCombine(result, vec);
return result;
}

/*
* Class: com_github_stephengold_joltjni_Jolt
* Method: rayAaBoxHits
Expand Down

0 comments on commit 0fc3160

Please sign in to comment.