2009-12-16 14:58:56 +01:00
|
|
|
/*
|
|
|
|
* MathUtil.h
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZOOM_MATHUTIL_H_
|
|
|
|
#define ZOOM_MATHUTIL_H_
|
|
|
|
|
2009-12-17 02:14:32 +01:00
|
|
|
#include "Triangle.h"
|
|
|
|
|
2009-12-16 14:58:56 +01:00
|
|
|
#include <vmmlib/matrix.hpp>
|
|
|
|
|
|
|
|
namespace Zoom {
|
|
|
|
|
|
|
|
class MathUtil {
|
|
|
|
public:
|
2009-12-17 02:14:32 +01:00
|
|
|
static const float EPSILON;
|
|
|
|
|
2009-12-26 03:19:49 +01:00
|
|
|
class Ray {
|
|
|
|
public:
|
|
|
|
Ray() : vertex(vmml::vec3f::ZERO), dir(vmml::vec3f::ZERO) {}
|
|
|
|
Ray(const vmml::vec3f &v, const vmml::vec3f &d) : vertex(v), dir(d) {}
|
|
|
|
|
|
|
|
const vmml::vec3f& getVertex() const {
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vmml::vec3f& getDirection() const {
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
vmml::vec3f vertex;
|
|
|
|
vmml::vec3f dir;
|
|
|
|
};
|
|
|
|
|
2009-12-17 02:14:32 +01:00
|
|
|
class Plane {
|
|
|
|
public:
|
|
|
|
Plane(const vmml::vec3f &n = vmml::vec3f::ZERO, float d0 = 0) : normal(n), d(d0) {}
|
2010-01-06 06:12:58 +01:00
|
|
|
Plane(const Triangle &t) : normal(t.getNormal()), d(t.getVertex(0).dot(normal)) {}
|
2009-12-17 02:14:32 +01:00
|
|
|
|
|
|
|
bool contains(const vmml::vec3f &v) const {
|
|
|
|
return (fabsf(normal.dot(v) - d) < EPSILON);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBehind(const vmml::vec3f &v) const {
|
|
|
|
return (normal.dot(v) - d) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInFront(const vmml::vec3f &v) const {
|
|
|
|
return (normal.dot(v) - d) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool contains(const Triangle &t) const {
|
|
|
|
for(int i = 0; i < 3; ++i) {
|
|
|
|
if(!contains(t.getVertex(i)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBehind(const Triangle &t) const {
|
|
|
|
for(int i = 0; i < 3; ++i) {
|
|
|
|
if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInFront(const Triangle &t) const {
|
|
|
|
for(int i = 0; i < 3; ++i) {
|
|
|
|
if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-26 03:19:49 +01:00
|
|
|
float distance(const vmml::vec3f &v) {
|
|
|
|
return (normal.dot(v) - d);
|
|
|
|
}
|
|
|
|
|
2009-12-17 02:14:32 +01:00
|
|
|
const vmml::vec3f& getNormal() const {
|
|
|
|
return normal;
|
|
|
|
}
|
|
|
|
|
2009-12-26 03:19:49 +01:00
|
|
|
vmml::vec3f intersection(const Ray &ray) const;
|
2009-12-17 02:14:32 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
vmml::vec3f normal;
|
|
|
|
float d;
|
|
|
|
};
|
|
|
|
|
2009-12-16 14:58:56 +01:00
|
|
|
static vmml::mat4f perspective(float fovy, float aspect, float zNear);
|
|
|
|
|
|
|
|
private:
|
|
|
|
MathUtil();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ZOOM_MATHUTIL_H_ */
|