[8901] Implement rage save part of talent 29723 buff and ranks.
[getmangos.git] / dep / src / g3dlite / Triangle.cpp
blobadb815e1602293a7e5979b863f9f379009eb0349
1 /**
2 @file Triangle.cpp
4 @maintainer Morgan McGuire, graphics3d.com
6 @created 2001-04-06
7 @edited 2006-01-20
9 Copyright 2000-2006, Morgan McGuire.
10 All rights reserved.
13 #include "G3D/platform.h"
14 #include "G3D/Triangle.h"
15 #include "G3D/Plane.h"
16 #include "G3D/AABox.h"
18 namespace G3D {
21 void Triangle::init(const Vector3& v0, const Vector3& v1, const Vector3& v2) {
23 _plane = Plane(v0, v1, v2);
24 _vertex[0] = v0;
25 _vertex[1] = v1;
26 _vertex[2] = 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();
36 } else {
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) {
56 init(v0, v1, v2);
60 Triangle::~Triangle() {
64 double Triangle::area() const {
65 return _area;
69 const Vector3& Triangle::normal() const {
70 return _plane.normal();
74 const Plane& Triangle::plane() const {
75 return _plane;
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();
89 if (t > 1.0f - s) {
90 // Outside the triangle; reflect about the
91 // diagonal of the parallelogram
92 t = 1.0f - t;
93 s = 1.0f - s;
96 return edge01 * s + edge02 * t + _vertex[0];
100 void Triangle::getBounds(AABox& out) const {
101 Vector3 lo = _vertex[0];
102 Vector3 hi = lo;
104 for (int i = 1; i < 3; ++i) {
105 lo = lo.min(_vertex[i]);
106 hi = hi.max(_vertex[i]);
109 out = AABox(lo, hi);
112 } // G3D