Files
TCPlayerbotCore/dep/include/g3dlite/G3D/Triangle.h
T

118 lines
2.6 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/**
@file Triangle.h
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
@maintainer Morgan McGuire, [email protected]
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
@created 2003-04-05
@edited 2004-03-14
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
@cite Random point method by Greg Turk, Generating random points in triangles. In A. S. Glassner, ed., Graphics Gems, pp. 24-28. Academic Press, 1990
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Copyright 2000-2006, Morgan McGuire.
All rights reserved.
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#ifndef G3D_TRIANGLE_H
#define G3D_TRIANGLE_H
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "G3D/platform.h"
#include "G3D/g3dmath.h"
#include "G3D/Vector3.h"
#include "G3D/Plane.h"
#include <string>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
namespace G3D {
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/**
A generic triangle representation. This should not be used
as the underlying triangle for creating models; it is intended
for providing fast property queries but requires a lot of
storage and is mostly immutable.
*/
class Triangle {
private:
friend class CollisionDetection;
friend class Ray;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Vector3 _vertex[3];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** edgeDirection[i] is the normalized vector v[i+1] - v[i] */
Vector3 edgeDirection[3];
double edgeMagnitude[3];
Plane _plane;
Vector3::Axis _primaryAxis;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** vertex[1] - vertex[0] */
Vector3 edge01;
/** vertex[2] - vertex[0] */
Vector3 edge02;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
float _area;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void init(const Vector3& v0, const Vector3& v1, const Vector3& v2);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
public:
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Triangle();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Triangle(const Vector3& v0, const Vector3& v1, const Vector3& v2);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
~Triangle();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** 0, 1, or 2 */
inline const Vector3& vertex(int n) const {
debugAssert((n >= 0) && (n < 3));
return _vertex[n];
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
double area() const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Vector3::Axis primaryAxis() const {
return _primaryAxis;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
const Vector3& normal() const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** Barycenter */
Vector3 center() const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
const Plane& plane() const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** Returns a random point in the triangle. */
Vector3 randomPoint() const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/**
For two triangles to be equal they must have
the same vertices <I>in the same order</I>.
That is, vertex[0] == vertex[0], etc.
*/
inline bool operator==(const Triangle& other) const {
for (int i = 0; i < 3; ++i) {
if (_vertex[i] != other._vertex[i]) {
return false;
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline unsigned int hashCode() const {
return
_vertex[0].hashCode() +
(_vertex[1].hashCode() >> 2) +
_vertex[2].hashCode();
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void getBounds(class AABox&) const;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
} // namespace
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline unsigned int hashCode(const G3D::Triangle& t) {
2009-02-09 08:16:34 -05:00
return t.hashCode();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#endif
2009-02-17 20:07:49 -06:00