001/**
002 * Copyright (c) 2008-2014 Ardor Labs, Inc.
003 *
004 * This file is part of Ardor3D.
005 *
006 * Ardor3D is free software: you can redistribute it and/or modify it 
007 * under the terms of its license which may be found in the accompanying
008 * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
009 */
010
011package com.ardor3d.math;
012
013import static org.junit.Assert.*;
014
015import org.junit.Test;
016
017import com.ardor3d.math.type.ReadOnlyPlane.Side;
018
019public class TestPlane {
020
021    @Test
022    public void testGetSet() {
023        final Plane plane = new Plane();
024        assertEquals(Vector3.UNIT_Y, plane.getNormal());
025        assertTrue(plane.getConstant() == 0.0);
026
027        plane.setNormal(Vector3.UNIT_X);
028        plane.setConstant(1.0);
029        assertEquals(Vector3.UNIT_X, plane.getNormal());
030        assertTrue(plane.getConstant() == 1.0);
031
032        final Plane plane2 = new Plane(plane);
033        assertEquals(Vector3.UNIT_X, plane2.getNormal());
034        assertTrue(plane.getConstant() == 1.0);
035
036        final Plane plane3 = new Plane(Vector3.NEG_UNIT_Z, 2.5);
037        assertEquals(Vector3.NEG_UNIT_Z, plane3.getNormal());
038        assertTrue(plane3.getConstant() == 2.5);
039
040        final Plane plane4 = new Plane().setPlanePoints(new Vector3(1, 1, 1), new Vector3(2, 1, 1),
041                new Vector3(2, 2, 1));
042        assertEquals(Vector3.UNIT_Z, plane4.getNormal());
043        assertTrue(plane4.getConstant() == 1.0);
044    }
045
046    @Test
047    public void testEquals() {
048        // couple of equals validity tests
049        final Plane plane1 = new Plane();
050        assertEquals(plane1, plane1);
051        assertFalse(plane1.equals(null));
052        assertFalse(plane1.equals(new Vector2()));
053
054        // throw in a couple pool accesses for coverage
055        final Plane plane2 = Plane.fetchTempInstance();
056        plane2.set(plane1);
057        assertEquals(plane1, plane2);
058        assertNotSame(plane1, plane2);
059        Plane.releaseTempInstance(plane2);
060
061        // cover more of equals
062        assertFalse(plane1.equals(new Plane(Vector3.UNIT_X, 0)));
063    }
064
065    @Test
066    public void testSimpleHash() {
067        // Just a simple sanity check.
068        final Plane plane1 = new Plane(Vector3.UNIT_Y, 2);
069        final Plane plane2 = new Plane(Vector3.UNIT_Y, 2);
070        final Plane plane3 = new Plane(Vector3.UNIT_Z, 2);
071
072        assertTrue(plane1.hashCode() == plane2.hashCode());
073        assertTrue(plane1.hashCode() != plane3.hashCode());
074    }
075
076    @Test
077    public void testClone() {
078        final Plane plane1 = new Plane();
079        final Plane plane2 = plane1.clone();
080        assertEquals(plane1, plane2);
081        assertNotSame(plane1, plane2);
082    }
083
084    @Test
085    public void testValid() {
086        final Plane plane1 = new Plane();
087        final Plane plane2 = new Plane(new Vector3(Double.NaN, 0, 0), 0.5);
088        final Plane plane3 = new Plane(Vector3.UNIT_X, Double.NaN);
089        final Plane plane4 = new Plane(Vector3.UNIT_X, Double.POSITIVE_INFINITY);
090
091        assertTrue(Plane.isValid(plane1));
092        assertFalse(Plane.isValid(plane2));
093        assertFalse(Plane.isValid(plane3));
094        assertFalse(Plane.isValid(plane4));
095
096        plane4.setConstant(1);
097        assertTrue(Plane.isValid(plane4));
098
099        assertFalse(Plane.isValid(null));
100    }
101
102    @Test
103    public void testDistance() {
104        final Plane plane1 = new Plane(Vector3.UNIT_Y, 1.0);
105        final Vector3 point = new Vector3(0, 5, 0);
106        assertTrue(4.0 == plane1.pseudoDistance(point));
107        assertEquals(Side.Outside, plane1.whichSide(point));
108
109        point.set(0, -4, 0);
110        assertTrue(-5.0 == plane1.pseudoDistance(point));
111        assertEquals(Side.Inside, plane1.whichSide(point));
112
113        point.set(1, 1, 1);
114        assertTrue(0.0 == plane1.pseudoDistance(point));
115        assertEquals(Side.Neither, plane1.whichSide(point));
116    }
117
118    @Test
119    public void testReflect() {
120        final Plane plane1 = new Plane(Vector3.UNIT_X, 5.0);
121        assertEquals(new Vector3(), plane1.reflectVector(new Vector3(), new Vector3()));
122        assertEquals(new Vector3(-1, 0, 0), plane1.reflectVector(new Vector3(1, 0, 0), null));
123        assertEquals(new Vector3(-1, 1, 1).normalizeLocal(),
124                plane1.reflectVector(new Vector3(1, 1, 1).normalizeLocal(), null));
125        assertEquals(new Vector3(-3, 2, -1).normalizeLocal(),
126                plane1.reflectVector(new Vector3(3, 2, -1).normalizeLocal(), null));
127
128        final Plane plane2 = new Plane(Vector3.UNIT_Z, 1.0);
129        assertEquals(new Vector3(), plane2.reflectVector(new Vector3(), new Vector3()));
130        assertEquals(new Vector3(0, 0, -1), plane2.reflectVector(new Vector3(0, 0, 1), null));
131        assertEquals(new Vector3(1, 1, -1).normalizeLocal(),
132                plane2.reflectVector(new Vector3(1, 1, 1).normalizeLocal(), null));
133        assertEquals(new Vector3(3, 2, 1).normalizeLocal(),
134                plane2.reflectVector(new Vector3(3, 2, -1).normalizeLocal(), null));
135    }
136}