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.bounding;
012
013import static org.junit.Assert.assertEquals;
014import static org.junit.Assert.assertFalse;
015import static org.junit.Assert.assertTrue;
016
017import org.junit.Test;
018
019import com.ardor3d.intersection.IntersectionRecord;
020import com.ardor3d.math.MathUtils;
021import com.ardor3d.math.Quaternion;
022import com.ardor3d.math.Ray3;
023import com.ardor3d.math.Transform;
024import com.ardor3d.math.Vector3;
025
026public class TestRayBounding {
027    @Test
028    public void testRayAABBIntersection() throws Exception {
029        final BoundingBox obb = new BoundingBox();
030        obb.setCenter(Vector3.ZERO);
031        obb.setXExtent(1);
032        obb.setYExtent(1);
033        obb.setZExtent(1);
034
035        Ray3 ray = new Ray3(new Vector3(2, -10, 0), Vector3.UNIT_Y);
036        assertFalse(obb.intersects(ray));
037        IntersectionRecord record = obb.intersectsWhere(ray);
038        assertEquals(null, record);
039
040        final Quaternion rotation = new Quaternion();
041        rotation.fromAngleAxis(MathUtils.QUARTER_PI, Vector3.UNIT_Z);
042        final Transform transform = new Transform();
043        transform.setRotation(rotation);
044        obb.transform(transform, obb);
045
046        ray = new Ray3(new Vector3(1, -10, 0), Vector3.UNIT_Y);
047        assertTrue(obb.intersects(ray));
048        record = obb.intersectsWhere(ray);
049        assertEquals(2, record.getNumberOfIntersections());
050    }
051
052    @Test
053    public void testRayOBBIntersection() throws Exception {
054        final OrientedBoundingBox obb = new OrientedBoundingBox();
055        obb.setCenter(Vector3.ZERO);
056        obb.setExtent(Vector3.ONE);
057
058        Ray3 ray = new Ray3(new Vector3(1.2, -10, 0), Vector3.UNIT_Y);
059        assertFalse(obb.intersects(ray));
060        IntersectionRecord record = obb.intersectsWhere(ray);
061        assertEquals(null, record);
062
063        final Quaternion rotation = new Quaternion();
064        rotation.fromAngleAxis(MathUtils.QUARTER_PI, Vector3.UNIT_Z);
065        final Transform transform = new Transform();
066        transform.setRotation(rotation);
067        obb.transform(transform, obb);
068
069        ray = new Ray3(new Vector3(1.2, -10, 0), Vector3.UNIT_Y);
070        assertTrue(obb.intersects(ray));
071        record = obb.intersectsWhere(ray);
072        assertEquals(2, record.getNumberOfIntersections());
073    }
074
075    @Test
076    public void testRaySphereIntersection() throws Exception {
077        final BoundingSphere bs = new BoundingSphere();
078        bs.setCenter(Vector3.ZERO);
079        bs.setRadius(1);
080
081        final Ray3 ray = new Ray3(new Vector3(2, -3, 0), Vector3.UNIT_Y);
082        assertFalse(bs.intersects(ray));
083        IntersectionRecord record = bs.intersectsWhere(ray);
084        assertEquals(null, record);
085
086        final Transform transform = new Transform();
087        transform.setTranslation(2, 0, .5);
088        bs.transform(transform, bs);
089
090        assertTrue(bs.intersects(ray));
091        record = bs.intersectsWhere(ray);
092        assertEquals(2, record.getNumberOfIntersections());
093    }
094}