4 @maintainer Morgan McGuire, graphics3d.com
9 Copyright 2000-2006, Morgan McGuire.
13 #include "G3D/platform.h"
14 #include "G3D/Triangle.h"
15 #include "G3D/Plane.h"
16 #include "G3D/AABox.h"
21 void Triangle::init(const Vector3
& v0
, const Vector3
& v1
, const Vector3
& v2
) {
23 _plane
= Plane(v0
, v1
, v2
);
28 static int next
[] = {1,2,0};
30 for (int i
= 0; i
< 3; ++i
) {
31 const Vector3 e
= _vertex
[next
[i
]] - _vertex
[i
];
32 edgeMagnitude
[i
] = e
.magnitude();
34 if (edgeMagnitude
[i
] == 0) {
35 edgeDirection
[i
] = Vector3::zero();
37 edgeDirection
[i
] = e
/ (float)edgeMagnitude
[i
];
41 edge01
= _vertex
[1] - _vertex
[0];
42 edge02
= _vertex
[2] - _vertex
[0];
44 _primaryAxis
= _plane
.normal().primaryAxis();
45 _area
= (float)edgeDirection
[0].cross(edgeDirection
[2]).magnitude() * (edgeMagnitude
[0] * edgeMagnitude
[2]);
50 Triangle::Triangle() {
51 init(Vector3::zero(), Vector3::zero(), Vector3::zero());
55 Triangle::Triangle(const Vector3
& v0
, const Vector3
& v1
, const Vector3
& v2
) {
60 Triangle::~Triangle() {
64 double Triangle::area() const {
69 const Vector3
& Triangle::normal() const {
70 return _plane
.normal();
74 const Plane
& Triangle::plane() const {
79 Vector3
Triangle::center() const {
80 return (_vertex
[0] + _vertex
[1] + _vertex
[2]) / 3.0;
83 Vector3
Triangle::randomPoint() const {
84 // Choose a random point in the parallelogram
86 float s
= uniformRandom();
87 float t
= uniformRandom();
90 // Outside the triangle; reflect about the
91 // diagonal of the parallelogram
96 return edge01
* s
+ edge02
* t
+ _vertex
[0];
100 void Triangle::getBounds(AABox
& out
) const {
101 Vector3 lo
= _vertex
[0];
104 for (int i
= 1; i
< 3; ++i
) {
105 lo
= lo
.min(_vertex
[i
]);
106 hi
= hi
.max(_vertex
[i
]);