Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Triangle.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "Triangle.h" 00013 #include <OpenCAL/Vertex.h> 00014 using namespace OpenCAL; 00015 00016 using namespace std; 00017 00018 00019 /****************************** 00020 * Constructors and destructor * 00021 ******************************/ 00022 00023 /* 00024 Triangle::Triangle() 00025 : m_normalValid(false) 00026 { 00027 m_vertices[0] = 0; 00028 m_vertices[1] = 0; 00029 m_vertices[2] = 0; 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 * Get and set functions * 00048 ************************/ 00049 00050 const Vector3 &Triangle::getNormal() 00051 { 00052 if(!m_normalValid) 00053 calculateNormal(); 00054 00055 return m_normal; 00056 } 00057 00058 00059 /****************** 00060 * Other functions * 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 //return 0.5f * (v1.crossProduct(v2) + v2.crossProduct(v3) + v3.crossProduct(v1)).length(); 00088 /* 00089 float a = m_vertices[0]->getPosition().distance(m_vertices[1]->getPosition()); 00090 float b = m_vertices[1]->getPosition().distance(m_vertices[2]->getPosition()); 00091 float c = m_vertices[2]->getPosition().distance(m_vertices[0]->getPosition()); 00092 00093 float s = (a + b + c) / 2.0f; 00094 return sqrtf(s * (s - a) * (s - b) * (s - c)); 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 * Protected functions * 00110 **********************/

Generated on Sun Aug 15 19:19:23 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8