From 1a37168a45e508e02f57df3641d1ac5abc6bba17 Mon Sep 17 00:00:00 2001 From: stephengold Date: Thu, 11 Jul 2024 11:34:46 -0700 Subject: [PATCH] add the ConstShape interface --- .../stephengold/joltjni/ConstShape.java | 78 +++++++++++++++ .../com/github/stephengold/joltjni/Shape.java | 96 ++++++++++--------- 2 files changed, 130 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/github/stephengold/joltjni/ConstShape.java diff --git a/src/main/java/com/github/stephengold/joltjni/ConstShape.java b/src/main/java/com/github/stephengold/joltjni/ConstShape.java new file mode 100644 index 00000000..cc66116a --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ConstShape.java @@ -0,0 +1,78 @@ +/* +Copyright (c) 2024 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 +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +import java.nio.FloatBuffer; + +/** + * An immutable {@code Shape}. + * + * @author Stephen Gold sgold@sonic.net + */ +public interface ConstShape { + // ************************************************************************* + // new methods exposed + + /** + * Copy the vertex coordinates of the shape's debug mesh to the specified + * buffer. + * + * @param storeBuffer the buffer to fill with vertex coordinates (not null, + * modified) + */ + void copyDebugTriangles(FloatBuffer storeBuffer); + + /** + * Return the number of triangles in the shape's debug mesh. + * + * @return the count (>0) + */ + int countDebugTriangles(); + + /** + * Copy the shape's mass properties. + * + * @return a new instance + */ + MassProperties getMassProperties(); + + /** + * Return the shape's subtype. + * + * @return an enum value + */ + EShapeSubType getSubType(); + + /** + * Return the shape's type. + * + * @return an enum value + */ + EShapeType getType(); + + /** + * Test whether the shape can be used in a dynamic/kinematic body. + * + * @return true if it can be only be static, otherwise false + */ + boolean mustBeStatic(); +} \ No newline at end of file diff --git a/src/main/java/com/github/stephengold/joltjni/Shape.java b/src/main/java/com/github/stephengold/joltjni/Shape.java index 03c75c94..06b6a438 100644 --- a/src/main/java/com/github/stephengold/joltjni/Shape.java +++ b/src/main/java/com/github/stephengold/joltjni/Shape.java @@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal * * @author Stephen Gold sgold@sonic.net */ -abstract public class Shape extends NonCopyable { +abstract public class Shape extends NonCopyable implements ConstShape { // ************************************************************************* // constructors @@ -50,6 +50,50 @@ protected Shape(long virtualAddress) { // ************************************************************************* // new methods exposed + /** + * Instantiate a Shape from its virtual address. + * + * @param shapeVa the virtual address of the native object, or zero + * @return a new instance, or {@code null} if the argument was zero + */ + static Shape newShape(long shapeVa) { + if (shapeVa == 0L) { + return null; + } + + int ordinal = getSubType(shapeVa); + EShapeSubType subType = EShapeSubType.values()[ordinal]; + Shape result; + switch (subType) { + case Box: + result = new BoxShape(shapeVa); + break; + case Capsule: + result = new CapsuleShape(shapeVa); + break; + case ConvexHull: + result = new ConvexHullShape(shapeVa); + break; + case Cylinder: + result = new CylinderShape(shapeVa); + break; + case HeightField: + result = new HeightFieldShape(shapeVa); + break; + case Mesh: + result = new MeshShape(shapeVa); + break; + case Sphere: + result = new SphereShape(shapeVa); + break; + default: + throw new IllegalArgumentException("type = " + subType); + } + return result; + } + // ************************************************************************* + // ConstShape methods + /** * Copy the vertex coordinates of the shape's debug mesh to the specified * buffer. @@ -57,6 +101,7 @@ protected Shape(long virtualAddress) { * @param storeBuffer the buffer to fill with vertex coordinates (not null, * modified) */ + @Override public void copyDebugTriangles(FloatBuffer storeBuffer) { long shapeVa = va(); int numTriangles = storeBuffer.capacity() / 9; @@ -68,6 +113,7 @@ public void copyDebugTriangles(FloatBuffer storeBuffer) { * * @return the count (>0) */ + @Override public int countDebugTriangles() { long shapeVa = va(); int result = countDebugTriangles(shapeVa); @@ -77,10 +123,11 @@ public int countDebugTriangles() { } /** - * Return the shape's mass properties. + * Copy the shape's mass properties. * * @return a new instance */ + @Override public MassProperties getMassProperties() { long shapeVa = va(); long propertiesVa = getMassProperties(shapeVa); @@ -94,6 +141,7 @@ public MassProperties getMassProperties() { * * @return an enum value */ + @Override public EShapeSubType getSubType() { long shapeVa = va(); int ordinal = getSubType(shapeVa); @@ -107,6 +155,7 @@ public EShapeSubType getSubType() { * * @return an enum value */ + @Override public EShapeType getType() { long shapeVa = va(); int ordinal = getType(shapeVa); @@ -120,53 +169,12 @@ public EShapeType getType() { * * @return true if it can be only be static, otherwise false */ + @Override public boolean mustBeStatic() { long shapeVa = va(); boolean result = mustBeStatic(shapeVa); return result; } - - /** - * Instantiate a Shape from its virtual address. - * - * @param shapeVa the virtual address of the native object, or zero - * @return a new instance, or {@code null} if the argument was zero - */ - static Shape newShape(long shapeVa) { - if (shapeVa == 0L) { - return null; - } - - int ordinal = getSubType(shapeVa); - EShapeSubType subType = EShapeSubType.values()[ordinal]; - Shape result; - switch (subType) { - case Box: - result = new BoxShape(shapeVa); - break; - case Capsule: - result = new CapsuleShape(shapeVa); - break; - case ConvexHull: - result = new ConvexHullShape(shapeVa); - break; - case Cylinder: - result = new CylinderShape(shapeVa); - break; - case HeightField: - result = new HeightFieldShape(shapeVa); - break; - case Mesh: - result = new MeshShape(shapeVa); - break; - case Sphere: - result = new SphereShape(shapeVa); - break; - default: - throw new IllegalArgumentException("type = " + subType); - } - return result; - } // ************************************************************************* // native private methods