ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

ray.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "math/ray.h"
00024 
00025 #include "common/util.h"
00026 
00027 namespace Math {
00028 
00029 Ray::Ray() {
00030 }
00031 
00032 Ray::Ray(const Vector3d &origin, const Vector3d &direction) :
00033         _origin(origin),
00034         _direction(direction) {
00035 }
00036 
00037 void Ray::transform(const Matrix4 &matrix) {
00038     matrix.transform(&_origin, true);
00039     matrix.transform(&_direction, false);
00040     _direction.normalize();
00041 }
00042 
00043 void Ray::rotate(const Quaternion &rot) {
00044     rot.transform(_origin);
00045     rot.transform(_direction);
00046     _direction.normalize();
00047 }
00048 
00049 void Ray::translate(const Vector3d &v) {
00050     _origin += v;
00051 }
00052 
00053 
00054 bool Ray::intersectAABB(const AABB &aabb) const {
00055     Vector3d dirFrac;
00056     dirFrac.x() = 1.0f / _direction.x();
00057     dirFrac.y() = 1.0f / _direction.y();
00058     dirFrac.z() = 1.0f / _direction.z();
00059 
00060     float t1 = (aabb.getMin().x() - _origin.x()) * dirFrac.x();
00061     float t2 = (aabb.getMax().x() - _origin.x()) * dirFrac.x();
00062     float t3 = (aabb.getMin().y() - _origin.y()) * dirFrac.y();
00063     float t4 = (aabb.getMax().y() - _origin.y()) * dirFrac.y();
00064     float t5 = (aabb.getMin().z() - _origin.z()) * dirFrac.z();
00065     float t6 = (aabb.getMax().z() - _origin.z()) * dirFrac.z();
00066 
00067     float tMin = MAX(MAX(MIN(t1, t2), MIN(t3, t4)), MIN(t5, t6));
00068     float tMax = MIN(MIN(MAX(t1, t2), MAX(t3, t4)), MAX(t5, t6));
00069 
00070     // If tMax < 0, the ray is intersecting the AABB, but the whole AABB is in the opposite direction
00071     if (tMax < 0) {
00072         return false;
00073     }
00074 
00075     // If tMin > tMax, the ray doesn't intersect the AABB
00076     if (tMin > tMax) {
00077         return false;
00078     }
00079 
00080     return true;
00081 }
00082 
00083 }


Generated on Sat May 18 2019 05:01:17 for ResidualVM by doxygen 1.7.1
curved edge   curved edge