From 5d155674b9f6acf44cab5c4c4a61c5c7159f6701 Mon Sep 17 00:00:00 2001 From: stephengold Date: Sat, 7 Sep 2024 20:59:00 -0700 Subject: [PATCH] ConvexHullShape: add 4 public methods --- .../stephengold/joltjni/ConvexHullShape.java | 64 +++++++++++++++++++ src/main/native/glue/co/ConvexHullShape.cpp | 49 ++++++++++++++ src/test/java/testjoltjni/junit/Test007.java | 3 + 3 files changed, 116 insertions(+) diff --git a/src/main/java/com/github/stephengold/joltjni/ConvexHullShape.java b/src/main/java/com/github/stephengold/joltjni/ConvexHullShape.java index 9b0c3f96..708dab6a 100644 --- a/src/main/java/com/github/stephengold/joltjni/ConvexHullShape.java +++ b/src/main/java/com/github/stephengold/joltjni/ConvexHullShape.java @@ -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. * @@ -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. @@ -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); diff --git a/src/main/native/glue/co/ConvexHullShape.cpp b/src/main/native/glue/co/ConvexHullShape.cpp index b0b1f28b..6e77f1ed 100644 --- a/src/main/native/glue/co/ConvexHullShape.cpp +++ b/src/main/native/glue/co/ConvexHullShape.cpp @@ -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 (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 (shapeVa); + const uint result = pShape->GetNumFaces(); + return result; +} + /* * Class: com_github_stephengold_joltjni_ConvexHullShape * Method: getNumPoints @@ -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 (shapeVa); + const uint result = pShape->GetNumVerticesInFace(faceIndex); + return result; +} + inline static const Vec3 getPoint(jlong shapeVa, jint pointIndex) { const ConvexHullShape * const pShape = reinterpret_cast (shapeVa); diff --git a/src/test/java/testjoltjni/junit/Test007.java b/src/test/java/testjoltjni/junit/Test007.java index 6bbf993e..12ff0bfa 100644 --- a/src/test/java/testjoltjni/junit/Test007.java +++ b/src/test/java/testjoltjni/junit/Test007.java @@ -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());