-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor: BoundingSphereDebug optimization + javadoc #2465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
capdevon
wants to merge
3
commits into
jMonkeyEngine:master
Choose a base branch
from
capdevon:capdevon-BoundingSphereDebug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
337 changes: 173 additions & 164 deletions
337
jme3-core/src/main/java/com/jme3/environment/util/BoundingSphereDebug.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,165 +1,174 @@ | ||
| /* | ||
| * Copyright (c) 2009-2024 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.environment.util; | ||
|
|
||
| import com.jme3.asset.AssetManager; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.math.ColorRGBA; | ||
| import com.jme3.math.FastMath; | ||
| import com.jme3.scene.Geometry; | ||
| import com.jme3.scene.Mesh; | ||
| import com.jme3.scene.VertexBuffer.Type; | ||
| import com.jme3.util.BufferUtils; | ||
| import java.nio.FloatBuffer; | ||
| import java.nio.ShortBuffer; | ||
|
|
||
| /** | ||
| * | ||
| * A debugging shape for a BoundingSphere | ||
| * Consists of 3 axis aligned circles. | ||
| * | ||
| * @author nehon | ||
| */ | ||
| public class BoundingSphereDebug extends Mesh { | ||
|
|
||
| protected int vertCount; | ||
| protected int triCount; | ||
| protected int radialSamples = 32; | ||
| protected boolean useEvenSlices; | ||
| protected boolean interior; | ||
|
|
||
| public BoundingSphereDebug() { | ||
| setGeometryData(); | ||
| setIndexData(); | ||
| } | ||
|
|
||
| /** | ||
| * builds the vertices based on the radius | ||
| */ | ||
| private void setGeometryData() { | ||
| setMode(Mode.Lines); | ||
|
|
||
| FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 3); | ||
| FloatBuffer colBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 4); | ||
|
|
||
| setBuffer(Type.Position, 3, posBuf); | ||
| setBuffer(Type.Color, 4, colBuf); | ||
|
|
||
| // generate geometry | ||
| float fInvRS = 1.0f / radialSamples; | ||
|
|
||
| // Generate points on the unit circle to be used in computing the mesh | ||
| // points on a sphere slice. | ||
| float[] afSin = new float[(radialSamples + 1)]; | ||
| float[] afCos = new float[(radialSamples + 1)]; | ||
| for (int iR = 0; iR < radialSamples; iR++) { | ||
| float fAngle = FastMath.TWO_PI * fInvRS * iR; | ||
| afCos[iR] = FastMath.cos(fAngle); | ||
| afSin[iR] = FastMath.sin(fAngle); | ||
| } | ||
| afSin[radialSamples] = afSin[0]; | ||
| afCos[radialSamples] = afCos[0]; | ||
|
|
||
| for (int iR = 0; iR <= radialSamples; iR++) { | ||
| posBuf.put(afCos[iR]) | ||
| .put(afSin[iR]) | ||
| .put(0); | ||
| colBuf.put(ColorRGBA.Blue.r) | ||
| .put(ColorRGBA.Blue.g) | ||
| .put(ColorRGBA.Blue.b) | ||
| .put(ColorRGBA.Blue.a); | ||
|
|
||
| } | ||
| for (int iR = 0; iR <= radialSamples; iR++) { | ||
| posBuf.put(afCos[iR]) | ||
| .put(0) | ||
| .put(afSin[iR]); | ||
| colBuf.put(ColorRGBA.Green.r) | ||
| .put(ColorRGBA.Green.g) | ||
| .put(ColorRGBA.Green.b) | ||
| .put(ColorRGBA.Green.a); | ||
| } | ||
| for (int iR = 0; iR <= radialSamples; iR++) { | ||
| posBuf.put(0) | ||
| .put(afCos[iR]) | ||
| .put(afSin[iR]); | ||
| colBuf.put(ColorRGBA.Yellow.r) | ||
| .put(ColorRGBA.Yellow.g) | ||
| .put(ColorRGBA.Yellow.b) | ||
| .put(ColorRGBA.Yellow.a); | ||
| } | ||
|
|
||
| updateBound(); | ||
| setStatic(); | ||
| } | ||
|
|
||
| /** | ||
| * sets the indices for rendering the sphere. | ||
| */ | ||
| private void setIndexData() { | ||
|
|
||
| // allocate connectivity | ||
| int nbSegments = (radialSamples) * 3; | ||
|
|
||
| ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments); | ||
| setBuffer(Type.Index, 2, idxBuf); | ||
|
|
||
| int idx = 0; | ||
| int segDone = 0; | ||
| while (segDone < nbSegments) { | ||
| idxBuf.put((short) idx); | ||
| idxBuf.put((short) (idx + 1)); | ||
| idx++; | ||
| segDone++; | ||
| if (segDone == radialSamples || segDone == radialSamples * 2) { | ||
| idx++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convenience factory method that creates a debug bounding-sphere geometry | ||
| * | ||
| * @param assetManager the assetManager | ||
| * @return the bounding sphere debug geometry. | ||
| */ | ||
| public static Geometry createDebugSphere(AssetManager assetManager) { | ||
| BoundingSphereDebug mesh = new BoundingSphereDebug(); | ||
| Geometry geom = new Geometry("BoundingDebug", mesh); | ||
| Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); | ||
| mat.setBoolean("VertexColor", true); | ||
| mat.getAdditionalRenderState().setWireframe(true); | ||
| geom.setMaterial(mat); | ||
| return geom; | ||
| } | ||
|
|
||
| } | ||
| * Copyright (c) 2009-2025 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.environment.util; | ||
|
|
||
| import com.jme3.asset.AssetManager; | ||
| import com.jme3.export.InputCapsule; | ||
| import com.jme3.export.JmeExporter; | ||
| import com.jme3.export.JmeImporter; | ||
| import com.jme3.export.OutputCapsule; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.math.ColorRGBA; | ||
| import com.jme3.math.FastMath; | ||
| import com.jme3.scene.Geometry; | ||
| import com.jme3.scene.Mesh; | ||
| import com.jme3.scene.VertexBuffer.Type; | ||
| import com.jme3.util.BufferUtils; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.FloatBuffer; | ||
| import java.nio.ShortBuffer; | ||
|
|
||
| /** | ||
| * A debugging shape for a BoundingSphere. | ||
| * This mesh consists of three axis-aligned circles (XY, XZ, YZ planes), | ||
| * providing a visual representation of a bounding sphere. | ||
| * | ||
| * @author nehon | ||
| */ | ||
| public class BoundingSphereDebug extends Mesh { | ||
|
|
||
| protected int radialSamples = 32; | ||
|
|
||
| /** | ||
| * Constructs a new BoundingSphereDebug mesh. | ||
| */ | ||
| public BoundingSphereDebug() { | ||
| setGeometryData(); | ||
| setIndexData(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates and sets the position and color data for the three circles. | ||
| * The circles are drawn in the XY (blue), XZ (green), and YZ (yellow) planes. | ||
| */ | ||
| private void setGeometryData() { | ||
|
|
||
| int numVertices = radialSamples + 1; | ||
|
|
||
| // Each circle has radialSamples + 1 vertices (to close the loop) | ||
| // We have 3 circles, so (radialSamples + 1) * 3 vertices in total. | ||
| FloatBuffer posBuf = BufferUtils.createVector3Buffer(numVertices * 3); | ||
| FloatBuffer colBuf = BufferUtils.createVector3Buffer(numVertices * 4); | ||
|
|
||
| // --- Generate Geometry Data --- | ||
| float angleStep = FastMath.TWO_PI / radialSamples; | ||
|
|
||
| // Generate points for a unit circle | ||
| float[] sin = new float[numVertices]; | ||
| float[] cos = new float[numVertices]; | ||
|
|
||
| for (int i = 0; i <= radialSamples; i++) { | ||
| float angle = angleStep * i; | ||
| cos[i] = FastMath.cos(angle); | ||
| sin[i] = FastMath.sin(angle); | ||
| } | ||
|
|
||
| // XY Plane Circle (Blue) | ||
| for (int i = 0; i < numVertices; i++) { | ||
| addCircleData(posBuf, colBuf, cos[i], sin[i], 0, ColorRGBA.Blue); | ||
| } | ||
| // XZ Plane Circle (Green) | ||
| for (int i = 0; i < numVertices; i++) { | ||
| addCircleData(posBuf, colBuf, cos[i], 0, sin[i], ColorRGBA.Green); | ||
| } | ||
| // YZ Plane Circle (Yellow) | ||
| for (int i = 0; i < numVertices; i++) { | ||
| addCircleData(posBuf, colBuf, 0, cos[i], sin[i], ColorRGBA.Yellow); | ||
| } | ||
|
|
||
| setBuffer(Type.Position, 3, posBuf); | ||
| setBuffer(Type.Color, 4, colBuf); | ||
|
|
||
| setMode(Mode.Lines); | ||
| updateBound(); | ||
| setStatic(); | ||
| } | ||
|
|
||
| private void addCircleData(FloatBuffer posBuf, FloatBuffer colBuf, | ||
| float x, float y, float z, ColorRGBA c) { | ||
| posBuf.put(x).put(y).put(z); | ||
| colBuf.put(c.r).put(c.g).put(c.b).put(c.a); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the index data for rendering the circles as lines. | ||
| * Each circle is made of `radialSamples` line segments. | ||
| */ | ||
| private void setIndexData() { | ||
|
|
||
| // allocate connectivity | ||
| int nbSegments = (radialSamples) * 3; // 3 circles | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable can be inlined. |
||
|
|
||
| ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments); | ||
| setBuffer(Type.Index, 2, idxBuf); | ||
|
|
||
| for (int c = 0; c < 3; c++) { // For each of the 3 circles | ||
| int baseIndex = c * (radialSamples + 1); | ||
| for (int i = 0; i < radialSamples; i++) { | ||
| idxBuf.put((short) (baseIndex + i)); | ||
| idxBuf.put((short) (baseIndex + i + 1)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convenience factory method that creates a debug bounding-sphere geometry | ||
| * | ||
| * @param assetManager the assetManager | ||
| * @return the bounding sphere debug geometry. | ||
| */ | ||
| public static Geometry createDebugSphere(AssetManager assetManager) { | ||
| BoundingSphereDebug mesh = new BoundingSphereDebug(); | ||
| Geometry geom = new Geometry("BoundingDebug", mesh); | ||
| Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); | ||
| mat.setBoolean("VertexColor", true); | ||
| mat.getAdditionalRenderState().setWireframe(true); | ||
| geom.setMaterial(mat); | ||
| return geom; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JmeExporter ex) throws IOException { | ||
| super.write(ex); | ||
| OutputCapsule oc = ex.getCapsule(this); | ||
| oc.write(radialSamples, "radialSamples", 32); | ||
| } | ||
|
|
||
| @Override | ||
| public void read(JmeImporter im) throws IOException { | ||
| super.read(im); | ||
| InputCapsule ic = im.getCapsule(this); | ||
| radialSamples = ic.readInt("radialSamples", 32); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically correct, but is confusing since
colBufis for storing colors with 4 components. I suggest changing this toBufferUtils.createFloatBuffer(numVertices * 4 * 3).