PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cinder/AxisAlignedBox.cpp

https://github.com/mikeedwards/Cinder
C++ | 123 lines | 83 code | 19 blank | 21 comment | 34 complexity | 6cfc57f500d16f27295dc40496d8a9d4 MD5 | raw file
  1/*
  2 Copyright (c) 2010, The Cinder Project: http://libcinder.org
  3 All rights reserved.
  4
  5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  6 the following conditions are met:
  7
  8    * Redistributions of source code must retain the above copyright notice, this list of conditions and
  9	the following disclaimer.
 10    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 11	the following disclaimer in the documentation and/or other materials provided with the distribution.
 12
 13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 15 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 17 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 18 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 20 POSSIBILITY OF SUCH DAMAGE.
 21*/
 22
 23#include "cinder/AxisAlignedBox.h"
 24
 25#include <limits>
 26
 27namespace cinder {
 28
 29AxisAlignedBox3f::AxisAlignedBox3f( const Vec3f &aMin, const Vec3f &aMax )
 30{
 31	mExtents[0] = Vec3f( aMin.x, aMin.y, aMin.z );
 32	mExtents[1] = Vec3f( aMax.x, aMax.y, aMax.z );
 33
 34	Vec3f extent( aMax.x - aMin.x, aMax.y - aMin.y, aMax.z - aMin.z );
 35	Vec3f mid( ( aMin.x + aMax.x ) / 2.0f, ( aMin.y + aMax.y ) / 2.0f, ( aMin.z + aMax.z ) / 2.0f );
 36	mVerts[0] = Vec3f( -0.5f, -0.5f, 0.5f ) * extent +  mid;
 37	mVerts[1] = Vec3f( 0.5f, -0.5f, 0.5f ) * extent + mid;
 38	mVerts[2] = Vec3f( -0.5f, 0.5f, 0.5f ) * extent + mid;
 39	mVerts[3] = Vec3f( 0.5f, 0.5f, 0.5f ) * extent + mid;
 40	mVerts[4] = Vec3f( -0.5f, 0.5f, -0.5f ) * extent + mid;
 41	mVerts[5] = Vec3f( 0.5f, 0.5f, -0.5f ) * extent + mid;
 42	mVerts[6] = Vec3f( -0.5f, -0.5f, -0.5f ) * extent + mid;
 43	mVerts[7] = Vec3f( 0.5f, -0.5f, -0.5f ) * extent + mid;
 44}
 45
 46bool AxisAlignedBox3f::intersects( const Ray &ray )
 47{
 48	float tmin, tmax, tymin, tymax, tzmin, tzmax;
 49
 50	tmin = (mExtents[ray.getSignX()].x - ray.getOrigin().x) * ray.getInverseDirection().x;
 51	tmax = (mExtents[1-ray.getSignX()].x - ray.getOrigin().x) * ray.getInverseDirection().x;
 52	tymin = (mExtents[ray.getSignY()].y - ray.getOrigin().y) * ray.getInverseDirection().y;
 53	tymax = (mExtents[1-ray.getSignY()].y - ray.getOrigin().y) * ray.getInverseDirection().y;
 54	if( ( tmin > tymax ) || ( tymin > tmax) ) 
 55		return false;
 56	if( tymin > tmin )
 57		tmin = tymin;
 58	if( tymax < tmax )
 59	   tmax = tymax;
 60	tzmin = (mExtents[ray.getSignZ()].z - ray.getOrigin().z) * ray.getInverseDirection().z;
 61	tzmax = (mExtents[1 - ray.getSignZ()].z - ray.getOrigin().z) * ray.getInverseDirection().z;
 62	if( ( tmin > tzmax ) || ( tzmin > tmax ) ) 
 63		return false;
 64	if( tzmin > tmin )
 65		tmin = tzmin;
 66	if( tzmax < tmax )
 67		tmax = tzmax;
 68
 69	return tmin > 0;
 70}
 71
 72int AxisAlignedBox3f::intersect( const Ray &ray, float intersections[2] )
 73{
 74	int i = 0;
 75	
 76	if( ray.calcTriangleIntersection( mVerts[2], mVerts[0], mVerts[1], &intersections[i] ) ) { // +Z
 77		i++;
 78	}
 79	else if( ray.calcTriangleIntersection( mVerts[1], mVerts[3], mVerts[2], &intersections[i] ) ) { // +Z
 80		i++;
 81	}
 82
 83	if( ray.calcTriangleIntersection( mVerts[7], mVerts[5], mVerts[3], &intersections[i] ) ) { // +X
 84		i++; if( i > 2 ) return i;
 85	}
 86	else if( ray.calcTriangleIntersection( mVerts[1], mVerts[7], mVerts[3], &intersections[i] ) ) { // +X
 87		i++; if( i > 2 ) return i;
 88	}
 89
 90	if( ray.calcTriangleIntersection( mVerts[3], mVerts[5], mVerts[4], &intersections[i] ) ) { // +Y
 91		i++; if( i > 2 ) return i;
 92	}
 93	else if( ray.calcTriangleIntersection( mVerts[2], mVerts[3], mVerts[4], &intersections[i] ) ) { // +Y
 94		i++; if( i > 2 ) return i;
 95	}
 96
 97	if( ray.calcTriangleIntersection( mVerts[1], mVerts[0], mVerts[7], &intersections[i] ) ) { // -Y
 98		i++; if( i > 2 ) return i;
 99	}
100	else if( ray.calcTriangleIntersection( mVerts[0], mVerts[6], mVerts[7], &intersections[i] ) ) { // -Y
101		i++; if( i > 2 ) return i;
102	}
103
104	if( ray.calcTriangleIntersection( mVerts[4], mVerts[0], mVerts[2], &intersections[i] ) ) { // -X
105		i++; if( i > 2 ) return i;
106	}
107	else if( ray.calcTriangleIntersection( mVerts[4], mVerts[6], mVerts[0], &intersections[i] ) ) { // -X
108		i++; if( i > 2 ) return i;
109	}
110
111	if( ray.calcTriangleIntersection( mVerts[6], mVerts[4], mVerts[5], &intersections[i] ) ) { // -Z
112		i++;
113	}
114	else if( ray.calcTriangleIntersection( mVerts[7], mVerts[6], mVerts[5], &intersections[i] ) ) { // -Z
115		i++;
116	}
117
118	return i;
119}
120
121
122
123} // namespace cinder