00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "Triangle.h"
00013
#include <OpenCAL/Vertex.h>
00014
using namespace OpenCAL;
00015
00016
using namespace std;
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 Triangle::Triangle(
Vertex *a,
Vertex *b,
Vertex *c)
00034 : m_normalValid(false), m_material(0)
00035 {
00036 m_vertices[0] = a;
00037 m_vertices[1] = b;
00038 m_vertices[2] = c;
00039 }
00040
00041 Triangle::~Triangle()
00042 {
00043 }
00044
00045
00046
00047
00048
00049
00050
const Vector3 &Triangle::getNormal()
00051 {
00052
if(!m_normalValid)
00053
calculateNormal();
00054
00055
return m_normal;
00056 }
00057
00058
00059
00060
00061
00062
00063 void Triangle::calculateNormal()
00064 {
00065 Vector3 leg1 = m_vertices[1]->
getPosition() - m_vertices[0]->getPosition();
00066 Vector3 leg2 = m_vertices[2]->getPosition() - m_vertices[0]->getPosition();
00067 m_normal = (leg1).crossProduct(leg2);
00068
00069 m_normal.normalize();
00070 m_normalValid =
true;
00071 }
00072
00080 float Triangle::getArea()
const
00081
{
00082
const Vector3 &v1 = m_vertices[0]->
getPosition();
00083
const Vector3 &v2 = m_vertices[1]->getPosition();
00084
const Vector3 &v3 = m_vertices[2]->getPosition();
00085
00086
return 0.5f * ((v1 - v3).crossProduct(v2 - v3)).length();
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 }
00097
00098
Vector3 Triangle::getMidpoint()
const
00099
{
00100
const Vector3 &p1 = m_vertices[0]->
getPosition();
00101
const Vector3 &p2 = m_vertices[1]->getPosition();
00102
const Vector3 &p3 = m_vertices[2]->getPosition();
00103
00104
return ((p1 + p2 + p3) / 3.0f);
00105 }
00106
00107
00108
00109
00110