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
017public class TestValidatingTransform {
018
019    @Test
020    public void testConstructor() {
021        final ValidatingTransform vt1 = new ValidatingTransform();
022        assertEquals(Transform.IDENTITY, vt1);
023
024        vt1.translate(0, 1, 2);
025        vt1.setRotation(new Matrix3().fromAngleAxis(Math.PI, Vector3.UNIT_X));
026
027        final ValidatingTransform vt2 = new ValidatingTransform(vt1);
028        assertEquals(vt1, vt2);
029    }
030
031    @Test(expected = InvalidTransformException.class)
032    public void failConstructor() {
033        final Transform bad = new Transform();
034        bad.translate(Double.NaN, 1, 2);
035        new ValidatingTransform(Transform.IDENTITY); // good
036        new ValidatingTransform(bad); // bad
037    }
038
039    @Test(expected = InvalidTransformException.class)
040    public void testSetRotationReadOnlyMatrix3() {
041        final ValidatingTransform vt1 = new ValidatingTransform();
042        final Matrix3 rotation = new Matrix3();
043        vt1.setRotation(rotation); // good
044        rotation.setM00(Double.NaN);
045        vt1.setRotation(rotation); // bad
046    }
047
048    @Test(expected = InvalidTransformException.class)
049    public void testSetRotationReadOnlyQuaternion() {
050        final ValidatingTransform vt1 = new ValidatingTransform();
051        final Quaternion rotation = new Quaternion();
052        vt1.setRotation(rotation); // good
053        rotation.setX(Double.NaN);
054        vt1.setRotation(rotation); // bad
055    }
056
057    @Test(expected = InvalidTransformException.class)
058    public void testSetTranslationReadOnlyVector3() {
059        final ValidatingTransform vt1 = new ValidatingTransform();
060        vt1.setTranslation(new Vector3(0, 0, 1)); // good
061        vt1.setTranslation(new Vector3(0, 0, Double.NaN)); // bad
062    }
063
064    @Test(expected = InvalidTransformException.class)
065    public void testSetTranslationDoubleDoubleDouble() {
066        final ValidatingTransform vt1 = new ValidatingTransform();
067        vt1.setTranslation(0, 0, 1); // good
068        vt1.setTranslation(0, 0, Double.NaN); // bad
069    }
070
071    @Test(expected = InvalidTransformException.class)
072    public void testSetScaleReadOnlyVector3() {
073        final ValidatingTransform vt1 = new ValidatingTransform();
074        vt1.setScale(new Vector3(1, 1, 1)); // good
075        vt1.setScale(new Vector3(1, 1, Double.NaN)); // bad
076    }
077
078    @Test(expected = InvalidTransformException.class)
079    public void testSetScaleDoubleDoubleDouble() {
080        final ValidatingTransform vt1 = new ValidatingTransform();
081        vt1.setScale(0, 0, 1); // good
082        vt1.setScale(0, 0, Double.NaN); // bad
083    }
084
085    @Test(expected = InvalidTransformException.class)
086    public void testSetScaleDouble() {
087        final ValidatingTransform vt1 = new ValidatingTransform();
088        vt1.setScale(1); // good
089        vt1.setScale(Double.NaN); // bad
090    }
091
092    @Test(expected = InvalidTransformException.class)
093    public void testSet() {
094        final Transform bad = new Transform();
095        bad.translate(Double.NaN, 1, 2);
096        final ValidatingTransform vt1 = new ValidatingTransform();
097        vt1.set(Transform.IDENTITY); // good
098        vt1.set(bad); // bad
099    }
100
101    @Test(expected = InvalidTransformException.class)
102    public void testTranslateDoubleDoubleDouble() {
103        final ValidatingTransform vt1 = new ValidatingTransform();
104        vt1.translate(1, 2, 3); // good
105        vt1.translate(0, 0, Double.NaN); // bad
106    }
107
108    @Test(expected = InvalidTransformException.class)
109    public void testTranslateReadOnlyVector3() {
110        final ValidatingTransform vt1 = new ValidatingTransform();
111        vt1.translate(new Vector3(1, 2, 3)); // good
112        vt1.translate(new Vector3(0, 0, Double.NaN)); // bad
113    }
114
115    @Test(expected = InvalidTransformException.class)
116    public void testMultiply() {
117        final ValidatingTransform vt1 = new ValidatingTransform();
118        vt1.multiply(Transform.IDENTITY, null); // good
119        final Transform bad = new Transform();
120        bad.translate(Double.NaN, 1, 2);
121        vt1.multiply(bad, null); // bad
122    }
123
124    @Test(expected = InvalidTransformException.class)
125    public void testInvert() {
126        final ValidatingTransform vt1 = new ValidatingTransform();
127        vt1.setScale(2);
128        vt1.invert(null); // good
129        // a little chicanery to get around other checks.
130        ((Vector3) vt1.getScale()).setX(0);
131        vt1.invert(null); // bad
132    }
133
134    @Test(expected = InvalidTransformException.class)
135    public void testFromHomogeneousMatrix() {
136        final ValidatingTransform vt1 = new ValidatingTransform();
137        final Matrix4 matrix = new Matrix4();
138        vt1.fromHomogeneousMatrix(matrix); // good
139        matrix.setM00(Double.NaN);
140        vt1.fromHomogeneousMatrix(matrix); // bad
141    }
142
143    @Test
144    public void testClone() {
145        final ValidatingTransform trans1 = new ValidatingTransform();
146        final ValidatingTransform trans2 = trans1.clone();
147        assertEquals(trans1, trans2);
148        assertNotSame(trans1, trans2);
149    }
150
151}