Skip to content

Commit

Permalink
ConvexHullShape: add 4 public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 8, 2024
1 parent 3d79e70 commit 5d15567
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/ConvexHullShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,50 @@ public float getConvexRadius() {
return result;
}

/**
* Enumerate the vertices in the specified face. The shape is unaffected.
*
* @param faceIndex the index of the face to query (≥0, <numFaces)
* @param storeIndices storage for the result (not null, modified)
* @return the number of vertices in the face
*/
public int getFaceVertices(int faceIndex, int[] storeIndices) {
int result = getFaceVertices(
faceIndex, storeIndices.length, storeIndices);
return result;
}

/**
* Enumerate the vertices in the specified face. The shape is unaffected.
*
* @param faceIndex the index of the face to query (≥0, <numFaces)
* @param maxVertices the maximum number of vertices to return (≥0,
* default=storeIndices.length)
* @param storeIndices storage for the result (not null,
* length≥maxVertices, modified)
* @return the number of vertices in the face (≥0)
*/
public int getFaceVertices(
int faceIndex, int maxVertices, int[] storeIndices) {
long shapeVa = va();
int result = getFaceVertices(
shapeVa, faceIndex, maxVertices, storeIndices);

return result;
}

/**
* Count the faces in the convex hull. The shape is unaffected.
*
* @return the count (≥0)
*/
public int getNumFaces() {
long shapeVa = va();
int result = getNumFaces(shapeVa);

return result;
}

/**
* Count the vertices of the convex hull. The shape is unaffected.
*
Expand All @@ -68,6 +112,19 @@ public int getNumPoints() {
return result;
}

/**
* Count the vertices in the specified face. The shape is unaffected.
*
* @param faceIndex the index of the face to query (≥0, <numFaces)
* @return the count (≥0)
*/
public int getNumVerticesInFace(int faceIndex) {
long shapeVa = va();
int result = getNumVerticesInFace(shapeVa, faceIndex);

return result;
}

/**
* Locate the specified vertex of the convex hull relative to its center of
* mass. The shape is unaffected.
Expand All @@ -89,8 +146,15 @@ public Vec3 getPoint(int pointIndex) {

native private static float getConvexRadius(long shapeVa);

native private static int getFaceVertices(
long shapeVa, int faceIndex, int maxVertices, int[] storeIndices);

native private static int getNumFaces(long shapeVa);

native private static int getNumPoints(long shapeVa);

native private static int getNumVerticesInFace(long shapeVa, int faceIndex);

native private static float getPointX(long shapeVa, int pointIndex);

native private static float getPointY(long shapeVa, int pointIndex);
Expand Down
49 changes: 49 additions & 0 deletions src/main/native/glue/co/ConvexHullShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ConvexHullShape_get
return result;
}

/*
* Class: com_github_stephengold_joltjni_ConvexHullShape
* Method: getFaceVertices
* Signature: (JII[I)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_ConvexHullShape_getFaceVertices
(JNIEnv *pEnv, jclass, jlong shapeVa, jint faceIndex, jint maxVertices,
jintArray storeIndices) {
const ConvexHullShape * const pShape
= reinterpret_cast<ConvexHullShape *> (shapeVa);
uint * const pTempArray = new uint[maxVertices];
const uint result
= pShape->GetFaceVertices(faceIndex, maxVertices, pTempArray);
jboolean isCopy;
jint * const pStoreJints = pEnv->GetIntArrayElements(storeIndices, &isCopy);
for (int i = 0; i < maxVertices; ++i) {
pStoreJints[i] = pTempArray[i];
}
delete[] pTempArray;
pEnv->ReleaseIntArrayElements(storeIndices, pStoreJints, 0);
return result;
}

/*
* Class: com_github_stephengold_joltjni_ConvexHullShape
* Method: getNumFaces
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_ConvexHullShape_getNumFaces
(JNIEnv *, jclass, jlong shapeVa) {
const ConvexHullShape * const pShape
= reinterpret_cast<ConvexHullShape *> (shapeVa);
const uint result = pShape->GetNumFaces();
return result;
}

/*
* Class: com_github_stephengold_joltjni_ConvexHullShape
* Method: getNumPoints
Expand All @@ -55,6 +91,19 @@ JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_ConvexHullShape_getNu
return result;
}

/*
* Class: com_github_stephengold_joltjni_ConvexHullShape
* Method: getNumVerticesInFace
* Signature: (JI)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_ConvexHullShape_getNumVerticesInFace
(JNIEnv *, jclass, jlong shapeVa, jint faceIndex) {
const ConvexHullShape * const pShape
= reinterpret_cast<ConvexHullShape *> (shapeVa);
const uint result = pShape->GetNumVerticesInFace(faceIndex);
return result;
}

inline static const Vec3 getPoint(jlong shapeVa, jint pointIndex) {
const ConvexHullShape * const pShape
= reinterpret_cast<ConvexHullShape *> (shapeVa);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/testjoltjni/junit/Test007.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ private static void doConvexHullShape() {
ConvexHullShape shape = (ConvexHullShape) ref.getPtr();

TestUtils.assertEquals(0f, 0f, 0f, shape.getCenterOfMass(), 0f);
Assert.assertEquals(3, shape.getFaceVertices(3, new int[3]));
Assert.assertEquals(Math.sqrt(3.), shape.getInnerRadius(), 1e-6f);
Assert.assertEquals(4, shape.getNumFaces());
Assert.assertEquals(3, shape.getNumVerticesInFace(3));
Assert.assertEquals(3, shape.getRefCount());
Assert.assertEquals(EShapeSubType.ConvexHull, shape.getSubType());
Assert.assertEquals(EShapeType.Convex, shape.getType());
Expand Down

0 comments on commit 5d15567

Please sign in to comment.