Skip to content

Commit

Permalink
add the ConstShape interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jul 11, 2024
1 parent 388664d commit 1a37168
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 44 deletions.
78 changes: 78 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/ConstShape.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/
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();
}
96 changes: 52 additions & 44 deletions src/main/java/com/github/stephengold/joltjni/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
*
* @author Stephen Gold [email protected]
*/
abstract public class Shape extends NonCopyable {
abstract public class Shape extends NonCopyable implements ConstShape {
// *************************************************************************
// constructors

Expand All @@ -50,13 +50,58 @@ 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.
*
* @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;
Expand All @@ -68,6 +113,7 @@ public void copyDebugTriangles(FloatBuffer storeBuffer) {
*
* @return the count (>0)
*/
@Override
public int countDebugTriangles() {
long shapeVa = va();
int result = countDebugTriangles(shapeVa);
Expand All @@ -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);
Expand All @@ -94,6 +141,7 @@ public MassProperties getMassProperties() {
*
* @return an enum value
*/
@Override
public EShapeSubType getSubType() {
long shapeVa = va();
int ordinal = getSubType(shapeVa);
Expand All @@ -107,6 +155,7 @@ public EShapeSubType getSubType() {
*
* @return an enum value
*/
@Override
public EShapeType getType() {
long shapeVa = va();
int ordinal = getType(shapeVa);
Expand All @@ -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

Expand Down

0 comments on commit 1a37168

Please sign in to comment.