// // triangle3d.cc // // An array of three point3d. // // Copyright (C) J. Belson 1999.02.07 // #include "triangle3d.h" /** * Constructor */ triangle3d::triangle3d(int p1, int p2, int p3) { point[0] = p1; point[1] = p2; point[2] = p3; // Default to all flags off state = 0; } /// Get vertices of this triangle void triangle3d::get(int* p1, int* p2, int* p3) { *p1 = point[0]; *p2 = point[1]; *p3 = point[2]; } /// Set vertices of this triangle void triangle3d::set(int p1, int p2, int p3) { point[0] = p1; point[1] = p2; point[2] = p3; // visible = true; } /// Set colour of this triangle void triangle3d::set_colour(const fcolour& c) { col = c; } // Get colour of this triangle void triangle3d::get_colour(fcolour* c) { *c = col; } /// Set an attribute flag for this triangle void triangle3d::set_flag(flags f) { state |= f; } /// Clear an attribute flag for this triangle void triangle3d::clear_flag(flags f) { state &= ~f; } /// Get an attribute flag for this triangle bool triangle3d::get_flag(flags f) const { return state&f; } /** * Finds approximation to centre of triangle * @todo should reference points via point[] rather than * assuming they start at zero? * @param points Array of points to retrieve point data from */ point3d triangle3d::find_centre(const point3d* points) { float smallest_x = 1000, largest_x = -1000; float smallest_y = 1000, largest_y = -1000; float smallest_z = 1000, largest_z = -1000; for (int i=0; i<3; i++) { const point3d& p = points[ /*tri.*/ point[i]]; if (p.x < smallest_x) { smallest_x = p.x; } if (p.x > largest_x) { largest_x = p.x; } if (p.y < smallest_y) { smallest_y = p.y; } if (p.y > largest_y) { largest_y = p.y; } if (p.z < smallest_z) { smallest_z = p.z; } if (p.z > largest_z) { largest_z = p.z; } } float centre_x = (largest_x + smallest_x)/2; float centre_y = (largest_y + smallest_y)/2; float centre_z = (largest_z + smallest_z)/2; return point3d(centre_x, centre_y, centre_z); } /** * Calculate the normal vector for this triangle * @param pnt3d Array of points to retrieve point data from */ void triangle3d::calculate_normal(const point3d* pnt3d) { vector3d v1(pnt3d[point[1]].x - pnt3d[point[0]].x, pnt3d[point[1]].y - pnt3d[point[0]].y, pnt3d[point[1]].z - pnt3d[point[0]].z); vector3d v2(pnt3d[point[1]].x - pnt3d[point[2]].x, pnt3d[point[1]].y - pnt3d[point[2]].y, pnt3d[point[1]].z - pnt3d[point[2]].z); // Surface nomal given by vector product of the two // edge vectors normal = v1.vector_product(v2); normal.normalise(); }