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

line3d.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/line3d.h"
00024 
00025 namespace Math {
00026 
00027 Line3d::Line3d() {
00028 
00029 }
00030 
00031 Line3d::Line3d(const Vector3d &b, const Vector3d &e) :
00032     _begin(b), _end(e) {
00033 
00034 }
00035 
00036 Line3d::Line3d(const Line3d &other) {
00037     *this = other;
00038 }
00039 
00040 Math::Vector3d Line3d::begin() const {
00041     return _begin;
00042 }
00043 
00044 Math::Vector3d Line3d::end() const {
00045     return _end;
00046 }
00047 
00048 Math::Vector3d Line3d::middle() const {
00049     return (_begin + _end) / 2.f;
00050 }
00051 
00052 bool Line3d::intersectLine2d(const Line3d &other, Math::Vector3d *pos, bool useXZ) {
00053 
00054     float denom, nume_a, nume_b;
00055     if (useXZ) {
00056         denom = ((other._end.z() - other._begin.z()) * (_end.x() - _begin.x())) -
00057         ((other._end.x() - other._begin.x()) * (_end.z() - _begin.z()));
00058 
00059         nume_a = ((other._end.x() - other._begin.x()) * (_begin.z() - other._begin.z())) -
00060         ((other._end.z() - other._begin.z()) * (_begin.x() - other._begin.x()));
00061 
00062         nume_b = ((_end.x() - _begin.x()) * (_begin.z() - other._begin.z())) -
00063         ((_end.z() - _begin.z()) * (_begin.x() - other._begin.x()));
00064     } else {
00065         denom = ((other._end.y() - other._begin.y()) * (_end.x() - _begin.x())) -
00066         ((other._end.x() - other._begin.x()) * (_end.y() - _begin.y()));
00067 
00068         nume_a = ((other._end.x() - other._begin.x()) * (_begin.y() - other._begin.y())) -
00069         ((other._end.y() - other._begin.y()) * (_begin.x() - other._begin.x()));
00070 
00071         nume_b = ((_end.x() - _begin.x()) * (_begin.y() - other._begin.y())) -
00072         ((_end.y() - _begin.y()) * (_begin.x() - other._begin.x()));
00073     }
00074 
00075     if (denom == 0.0f) {
00076         return false;
00077     }
00078 
00079     float ua = nume_a / denom;
00080     float ub = nume_b / denom;
00081 
00082     if (ua < 0 || ua > 1 || ub < 0 || ub > 1)
00083         return false;
00084 
00085     // Get the intersection point.
00086     if (pos)
00087         *pos = _begin + ua * (_end - _begin);
00088 
00089     return true;
00090 }
00091 
00092 Line3d& Line3d::operator=(const Line3d &other) {
00093     _begin = other._begin;
00094     _end = other._end;
00095     return *this;
00096 }
00097 
00098 }


Generated on Sat Sep 14 2019 05:01:35 for ResidualVM by doxygen 1.7.1
curved edge   curved edge