[4541] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
| 17 | |
---|
| 18 | #include "obb_tree_node.h" |
---|
[4542] | 19 | #include "list.h" |
---|
| 20 | #include "obb.h" |
---|
[4544] | 21 | #include "vector.h" |
---|
[4541] | 22 | |
---|
[4543] | 23 | #include <math.h> |
---|
| 24 | |
---|
[4541] | 25 | using namespace std; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | \brief standard constructor |
---|
| 30 | */ |
---|
| 31 | OBBTreeNode::OBBTreeNode () |
---|
| 32 | { |
---|
| 33 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
| 34 | |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | \brief standard deconstructor |
---|
| 40 | |
---|
| 41 | */ |
---|
| 42 | OBBTreeNode::~OBBTreeNode () |
---|
| 43 | { |
---|
| 44 | // delete what has to be deleted here |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
[4542] | 48 | |
---|
| 49 | /** |
---|
| 50 | \brief creates a new BVTree or BVTree partition |
---|
| 51 | \param depth: the depth of the tree |
---|
| 52 | \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle |
---|
| 53 | */ |
---|
[4544] | 54 | void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length) |
---|
[4542] | 55 | { |
---|
[4543] | 56 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
| 57 | float face; //!< surface area of the entire convex hull |
---|
[4545] | 58 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
| 59 | Vector centre; //!< the centre of the entire hull |
---|
[4542] | 60 | OBB* obb = new OBB(); //!< the new obb to add |
---|
[4544] | 61 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
[4545] | 62 | Vector t1, t2; //!< temporary values |
---|
| 63 | float covariance[3][3]; //!< the covariance matrix |
---|
| 64 | |
---|
| 65 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
| 66 | for(int i = 0; i < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4542] | 67 | { |
---|
[4544] | 68 | p = *(verticesList + i); |
---|
| 69 | q = *(verticesList + i + 1); |
---|
| 70 | r = *(verticesList + i + 2); |
---|
| 71 | |
---|
| 72 | t1 = p - q; t2 = p - r; |
---|
| 73 | |
---|
| 74 | /* finding the facelet surface via cross-product */ |
---|
| 75 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
| 76 | /* update the entire convex hull surface */ |
---|
| 77 | face += facelet[i]; |
---|
[4545] | 78 | |
---|
| 79 | |
---|
| 80 | /* calculate the cetroid of the hull triangles */ |
---|
| 81 | centroid[i] = (p + q + r) * 1/3; |
---|
| 82 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
| 83 | centre += centroid[i] * facelet[i]; |
---|
[4542] | 84 | } |
---|
[4545] | 85 | /* take the average of the centroid sum */ |
---|
| 86 | centre /= face; |
---|
| 87 | |
---|
| 88 | /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */ |
---|
| 89 | for(int j = 0; j < 3; ++j) |
---|
| 90 | { |
---|
| 91 | for(int k = 0; k < 3; ++k) |
---|
| 92 | { |
---|
| 93 | for(int i = 0; i < length; i+=3) |
---|
| 94 | { |
---|
| 95 | p = *(verticesList + i); |
---|
| 96 | q = *(verticesList + i + 1); |
---|
| 97 | r = *(verticesList + i + 2); |
---|
[4544] | 98 | |
---|
[4545] | 99 | covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] + |
---|
| 100 | q[j] * q[k] + r[j]*r[k]) - centre[j] * centre[k]; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
[4542] | 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | void OBBTreeNode::collideWith(const BVTree &tree) |
---|
| 108 | {} |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | void OBBTreeNode::drawBV(int currentDepth, const int depth) const |
---|
| 112 | {} |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const |
---|
| 116 | {} |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const |
---|
| 120 | {} |
---|