From c707785a34983ee5e1f3775376e52060d6437840 Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Wed, 21 Nov 2018 14:14:21 -0800 Subject: [PATCH] test and fix for JME issue #957 --- .../src/main/java/com/jme3/math/Triangle.java | 88 +++++++++++++++--- .../test/java/com/jme3/math/TestIssue957.java | 92 +++++++++++++++++++ 2 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 jme3-core/src/test/java/com/jme3/math/TestIssue957.java diff --git a/jme3-core/src/main/java/com/jme3/math/Triangle.java b/jme3-core/src/main/java/com/jme3/math/Triangle.java index 527d6e7b4..590268213 100644 --- a/jme3-core/src/main/java/com/jme3/math/Triangle.java +++ b/jme3-core/src/main/java/com/jme3/math/Triangle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2012 jMonkeyEngine + * Copyright (c) 2009-2018 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,6 +56,9 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja private float projection; private int index; + /** + * Instantiate a zero-size Triangle at the origin. + */ public Triangle() { } @@ -75,11 +78,11 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja } /** - * * get retrieves a point on the triangle denoted by the index * supplied. - * @param i the index of the point. - * @return the point. + * + * @param i the index of the point (0, 1, or 2) + * @return a pre-existing location vector */ public Vector3f get(int i) { switch (i) { @@ -94,26 +97,47 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja } } + /** + * Access the location of the 1st point (A). + * + * @return the pre-existing vector (not null) + */ + @Override public Vector3f get1() { return pointa; } + /** + * Access the location of the 2nd point (B). + * + * @return the pre-existing vector (not null) + */ + @Override public Vector3f get2() { return pointb; } + /** + * Access the location of the 3rd point (C). + * + * @return the pre-existing vector (not null) + */ + @Override public Vector3f get3() { return pointc; } /** + * set sets one of the triangle's points to that specified as a + * parameter. * - * set sets one of the triangle's points to that specified as - * a parameter. - * @param i the index to place the point. - * @param point the point to set. + * @param i the index to place the point (0, 1, or 2) + * @param point the desired location of the point (not null, unaffected) */ public void set(int i, Vector3f point) { + center = null; + normal = null; + switch (i) { case 0: pointa.set(point); @@ -128,12 +152,17 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja } /** + * set alters the location of one of the triangle's points. * - * set sets one of the triangle's points to that specified as - * a parameter. - * @param i the index to place the point. + * @param i the index to place the point (0, 1, or 2) + * @param x the desired X-component of the point's location + * @param y the desired Y-component of the point's location + * @param z the desired Z-component of the point's location */ public void set(int i, float x, float y, float z) { + center = null; + normal = null; + switch (i) { case 0: pointa.set(x, y, z); @@ -147,19 +176,54 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja } } + /** + * set1 alters the location of the triangle's 1st point. + * + * @param v the desired location (not null, unaffected) + */ public void set1(Vector3f v) { + center = null; + normal = null; + pointa.set(v); } + /** + * set2 alters the location of the triangle's 2nd point. + * + * @param v the desired location (not null, unaffected) + */ public void set2(Vector3f v) { + center = null; + normal = null; + pointb.set(v); } + /** + * set3 alters the location of the triangle's 3rd point. + * + * @param v the desired location (not null, unaffected) + */ public void set3(Vector3f v) { + center = null; + normal = null; + pointc.set(v); } + /** + * set alters the locations of all 3 points. + * + * @param v1 the desired location of the 1st point (not null, unaffected) + * @param v2 the desired location of the 2nd point (not null, unaffected) + * @param v3 the desired location of the 3rd point (not null, unaffected) + */ + @Override public void set(Vector3f v1, Vector3f v2, Vector3f v3) { + center = null; + normal = null; + pointa.set(v1); pointb.set(v2); pointc.set(v3); @@ -275,12 +339,14 @@ public class Triangle extends AbstractTriangle implements Savable, Cloneable, ja return store.normalizeLocal(); } + @Override public void write(JmeExporter e) throws IOException { e.getCapsule(this).write(pointa, "pointa", Vector3f.ZERO); e.getCapsule(this).write(pointb, "pointb", Vector3f.ZERO); e.getCapsule(this).write(pointc, "pointc", Vector3f.ZERO); } + @Override public void read(JmeImporter e) throws IOException { pointa = (Vector3f) e.getCapsule(this).readSavable("pointa", Vector3f.ZERO.clone()); pointb = (Vector3f) e.getCapsule(this).readSavable("pointb", Vector3f.ZERO.clone()); diff --git a/jme3-core/src/test/java/com/jme3/math/TestIssue957.java b/jme3-core/src/test/java/com/jme3/math/TestIssue957.java new file mode 100644 index 000000000..16cca641e --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/TestIssue957.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018 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.math; + +import org.junit.Test; + +/** + * Verify that a Triangle center and normal get recomputed after a change. This + * was issue #957 at GitHub. + * + * @author Stephen Gold + */ +public class TestIssue957 { + + final private Vector3f v0 = new Vector3f(0f, 0f, 0f); + final private Vector3f v1 = new Vector3f(3f, 0f, 0f); + final private Vector3f v2 = new Vector3f(0f, 3f, 0f); + + @Test + public void testIssue957() { + Vector3f v3 = new Vector3f(0f, 0f, 3f); + + Triangle t1 = makeTriangle(); + t1.set(2, v3); + checkTriangle(t1); + + Triangle t2 = makeTriangle(); + t2.set(v3, v0, v1); + checkTriangle(t2); + + Triangle t3 = makeTriangle(); + t3.set(2, v3.x, v3.y, v3.z); + checkTriangle(t3); + + Triangle t4 = makeTriangle(); + t4.set3(v3); + checkTriangle(t4); + } + + private Triangle makeTriangle() { + + Triangle triangle = new Triangle(v0, v1, v2); + /* + * Check center and normal before modification. + */ + Vector3f center = triangle.getCenter(); + Vector3f normal = triangle.getNormal(); + assert center.equals(new Vector3f(1f, 1f, 0f)); + assert normal.equals(new Vector3f(0f, 0f, 1f)); + + return triangle; + } + + /** + * Check center and normal after modification. + */ + private void checkTriangle(Triangle triangle) { + Vector3f center = triangle.getCenter(); + Vector3f normal = triangle.getNormal(); + assert center.equals(new Vector3f(1f, 0f, 1f)); + assert normal.equals(new Vector3f(0f, -1f, 0f)); + } +}