KDTree.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <algorithm>
  2. #include <iostream>
  3. #include "common/utils/KDTree.h"
  4. KDTree::Triangle::Triangle(const Vector3& a, const Vector3& b, const Vector3& c) {
  5. v[0] = a;
  6. v[1] = b;
  7. v[2] = c;
  8. mid = (a + b + c) * (1.0f / 3.0f);
  9. }
  10. const Array<Vector3, 3>& KDTree::Triangle::data() const {
  11. return v;
  12. }
  13. const Vector3& KDTree::Triangle::operator[](int index) const {
  14. return v[index];
  15. }
  16. const Vector3& KDTree::Triangle::getMid() const {
  17. return mid;
  18. }
  19. KDTree::Node::Node() : splitDim(0), splitValue(0.0f), color(0xFFFFFF) {
  20. childs[0] = nullptr;
  21. childs[1] = nullptr;
  22. }
  23. KDTree::~KDTree() {
  24. clean(&root);
  25. }
  26. void KDTree::clean(Node* n) {
  27. if(n->childs[0] != nullptr) {
  28. clean(n->childs[0]);
  29. }
  30. if(n->childs[1] != nullptr) {
  31. clean(n->childs[1]);
  32. }
  33. delete n->childs[0];
  34. delete n->childs[1];
  35. }
  36. void KDTree::build(std::vector<KDTree::Triangle>& data) {
  37. if(data.size() == 0) {
  38. return;
  39. }
  40. min = data[0][0];
  41. max = data[0][0];
  42. for(const Triangle& t : data) {
  43. for(const Vector3& v : t.data()) {
  44. min.set(std::min(min[0], v[0]), std::min(min[1], v[1]), std::min(min[2], v[2]));
  45. max.set(std::max(max[0], v[0]), std::max(max[1], v[1]), std::max(max[2], v[2]));
  46. }
  47. }
  48. build(&root, data);
  49. }
  50. float KDTree::median(std::vector<KDTree::Triangle>& data, int dim) const {
  51. auto compare = [dim](const Triangle& a, const Triangle & b) {
  52. return a.getMid()[dim] < b.getMid()[dim];
  53. };
  54. size_t length = data.size();
  55. if((length & 1) == 0) {
  56. std::nth_element(data.begin(), data.begin() + (length / 2 - 1), data.end(), compare);
  57. float tmp = data[length / 2 - 1].getMid()[dim];
  58. std::nth_element(data.begin(), data.begin() + (length / 2), data.end(), compare);
  59. return (tmp + data[length / 2].getMid()[dim]) / 2;
  60. }
  61. std::nth_element(data.begin(), data.begin() + (length / 2), data.end(), compare);
  62. return data[length / 2].getMid()[dim];
  63. }
  64. void KDTree::build(Node* n, std::vector<KDTree::Triangle>& data) {
  65. if(data.size() == 0) {
  66. return;
  67. } else if(data.size() == 1) {
  68. n->data.push_back(data[0]);
  69. return;
  70. }
  71. // find min and max coordinates
  72. Vector3 min = data[0][0];
  73. Vector3 max = data[0][0];
  74. for(const Triangle& t : data) {
  75. for(const Vector3& v : t.data()) {
  76. min.set(std::min(min[0], v[0]), std::min(min[1], v[1]), std::min(min[2], v[2]));
  77. max.set(std::max(max[0], v[0]), std::max(max[1], v[1]), std::max(max[2], v[2]));
  78. }
  79. }
  80. // find biggest span and its dimension
  81. int splitDim = 0;
  82. float maxSpan = max[0] - min[0];
  83. for(int i = 1; i < 3; i++) {
  84. float span = max[i] - min[i];
  85. if(span > maxSpan) {
  86. splitDim = i;
  87. maxSpan = span;
  88. }
  89. }
  90. // assign data to node
  91. n->splitDim = splitDim;
  92. n->splitValue = median(data, splitDim);
  93. // storage for split data
  94. std::vector<KDTree::Triangle> lessEqualData;
  95. std::vector<KDTree::Triangle> greaterData;
  96. // actually split the data
  97. for(const Triangle& t : data) {
  98. // count points on each split side
  99. int lessEqualCounter = 0;
  100. int greaterCount = 0;
  101. for(const Vector3& v : t.data()) {
  102. if(v[n->splitDim] <= n->splitValue) {
  103. lessEqualCounter++;
  104. } else {
  105. greaterCount++;
  106. }
  107. }
  108. // put the data in the correct container
  109. if(lessEqualCounter == 3) {
  110. lessEqualData.push_back(t);
  111. } else if(greaterCount == 3) {
  112. greaterData.push_back(t);
  113. } else {
  114. n->data.push_back(t);
  115. }
  116. }
  117. if(lessEqualData.size() == 0 || greaterData.size() == 0) {
  118. for(KDTree::Triangle& t : lessEqualData) {
  119. n->data.push_back(t);
  120. }
  121. for(KDTree::Triangle& t : greaterData) {
  122. n->data.push_back(t);
  123. }
  124. return;
  125. }
  126. // recursive calls
  127. if(lessEqualData.size() > 0) {
  128. n->childs[0] = new Node();
  129. build(n->childs[0], lessEqualData);
  130. }
  131. if(greaterData.size() > 0) {
  132. n->childs[1] = new Node();
  133. build(n->childs[1], greaterData);
  134. }
  135. }
  136. void KDTree::fillLines(Lines& lines) const {
  137. lines.clear();
  138. lines.add(Vector3(min[0], min[1], min[2]), Vector3(max[0], min[1], min[2]), root.color);
  139. lines.add(Vector3(min[0], min[1], min[2]), Vector3(min[0], min[1], max[2]), root.color);
  140. lines.add(Vector3(max[0], min[1], min[2]), Vector3(max[0], min[1], max[2]), root.color);
  141. lines.add(Vector3(min[0], min[1], max[2]), Vector3(max[0], min[1], max[2]), root.color);
  142. lines.add(Vector3(min[0], min[1], min[2]), Vector3(min[0], max[1], min[2]), root.color);
  143. lines.add(Vector3(max[0], min[1], min[2]), Vector3(max[0], max[1], min[2]), root.color);
  144. lines.add(Vector3(min[0], min[1], max[2]), Vector3(min[0], max[1], max[2]), root.color);
  145. lines.add(Vector3(max[0], min[1], max[2]), Vector3(max[0], max[1], max[2]), root.color);
  146. lines.add(Vector3(min[0], max[1], min[2]), Vector3(max[0], max[1], min[2]), root.color);
  147. lines.add(Vector3(min[0], max[1], min[2]), Vector3(min[0], max[1], max[2]), root.color);
  148. lines.add(Vector3(max[0], max[1], min[2]), Vector3(max[0], max[1], max[2]), root.color);
  149. lines.add(Vector3(min[0], max[1], max[2]), Vector3(max[0], max[1], max[2]), root.color);
  150. fillLines(lines, &root, min, max);
  151. lines.build();
  152. }
  153. void KDTree::fillLines(Lines& lines, const Node* n, const Vector3& min, const Vector3& max) const {
  154. if(n->childs[0] == nullptr && n->childs[1] == nullptr) {
  155. return;
  156. }
  157. switch(n->splitDim) {
  158. case 0:
  159. lines.add(Vector3(n->splitValue, min[1], min[2]), Vector3(n->splitValue, max[1], min[2]), n->color);
  160. lines.add(Vector3(n->splitValue, max[1], min[2]), Vector3(n->splitValue, max[1], max[2]), n->color);
  161. lines.add(Vector3(n->splitValue, max[1], max[2]), Vector3(n->splitValue, min[1], max[2]), n->color);
  162. lines.add(Vector3(n->splitValue, min[1], max[2]), Vector3(n->splitValue, min[1], min[2]), n->color);
  163. if(n->childs[0] != nullptr) {
  164. fillLines(lines, n->childs[0], min, Vector3(n->splitValue, max[1], max[2]));
  165. }
  166. if(n->childs[1] != nullptr) {
  167. fillLines(lines, n->childs[1], Vector3(n->splitValue, min[1], min[2]), max);
  168. }
  169. break;
  170. case 1:
  171. lines.add(Vector3(min[0], n->splitValue, min[2]), Vector3(max[0], n->splitValue, min[2]), n->color);
  172. lines.add(Vector3(min[0], n->splitValue, min[2]), Vector3(min[0], n->splitValue, max[2]), n->color);
  173. lines.add(Vector3(max[0], n->splitValue, min[2]), Vector3(max[0], n->splitValue, max[2]), n->color);
  174. lines.add(Vector3(min[0], n->splitValue, max[2]), Vector3(max[0], n->splitValue, max[2]), n->color);
  175. if(n->childs[0] != nullptr) {
  176. fillLines(lines, n->childs[0], min, Vector3(max[0], n->splitValue, max[2]));
  177. }
  178. if(n->childs[1] != nullptr) {
  179. fillLines(lines, n->childs[1], Vector3(min[0], n->splitValue, min[2]), max);
  180. }
  181. break;
  182. case 2:
  183. lines.add(Vector3(min[0], min[1], n->splitValue), Vector3(min[0], max[1], n->splitValue), n->color);
  184. lines.add(Vector3(min[0], max[1], n->splitValue), Vector3(max[0], max[1], n->splitValue), n->color);
  185. lines.add(Vector3(max[0], max[1], n->splitValue), Vector3(max[0], min[1], n->splitValue), n->color);
  186. lines.add(Vector3(max[0], min[1], n->splitValue), Vector3(min[0], min[1], n->splitValue), n->color);
  187. if(n->childs[0] != nullptr) {
  188. fillLines(lines, n->childs[0], min, Vector3(max[0], max[1], n->splitValue));
  189. }
  190. if(n->childs[1] != nullptr) {
  191. fillLines(lines, n->childs[1], Vector3(min[0], min[1], n->splitValue), max);
  192. }
  193. break;
  194. }
  195. }
  196. void KDTree::cleanVisited(Node* n) {
  197. if(n->childs[0] != nullptr) {
  198. cleanVisited(n->childs[0]);
  199. }
  200. if(n->childs[1] != nullptr) {
  201. cleanVisited(n->childs[1]);
  202. }
  203. n->color = 0xFFFFFF;
  204. }
  205. bool KDTree::findIntersection(const Vector3& pos, const Vector3& direction) {
  206. minDistance = 9999999.0f;
  207. original = pos;
  208. cleanVisited(&root);
  209. float t = 0.0f;
  210. for(int i = 0; i < 3; i++) {
  211. if(direction[i] == 0.0f) {
  212. continue;
  213. }
  214. t = std::max(t, std::abs((min[i] - pos[i]) / direction[i]));
  215. t = std::max(t, std::abs((max[i] - pos[i]) / direction[i]));
  216. }
  217. return findIntersection(&root, pos, direction, t);
  218. }
  219. Vector3 KDTree::getIntersection() const {
  220. return intersection;
  221. }
  222. KDTree::Triangle KDTree::getIntersectedTriangle() const {
  223. return intersectionTriangle;
  224. }
  225. bool KDTree::findIntersection(Node* n, const Vector3& pos, const Vector3& direction, float tMax) {
  226. if(n == nullptr) {
  227. return false;
  228. }
  229. n->color = 0x00FFFF;
  230. int dim = n->splitDim;
  231. // lessEqual = 0
  232. // greater = 1
  233. bool r = false;
  234. bool first = pos[dim] > n->splitValue;
  235. if(direction[dim] == 0.0f) {
  236. r = findIntersection(n->childs[first], pos, direction, tMax);
  237. } else {
  238. float t = (n->splitValue - pos[dim]) / direction[dim];
  239. if(t >= 0.0f && t < tMax) {
  240. r = findIntersection(n->childs[first], pos, direction, t);
  241. r = findIntersection(n->childs[!first], pos + t * direction, direction, tMax - t) || r;
  242. } else {
  243. r = findIntersection(n->childs[first], pos, direction, tMax);
  244. }
  245. }
  246. for(KDTree::Triangle& tri : n->data) {
  247. float t = testIntersection(pos, direction, tri);
  248. if(t < 0.0f) {
  249. continue;
  250. }
  251. Vector3 i = pos + t * direction;
  252. float distance = static_cast<Vector3> (original - i).squareLength();
  253. if(distance > minDistance) {
  254. continue;
  255. }
  256. intersectionTriangle = tri;
  257. intersection = i;
  258. minDistance = distance;
  259. r = true;
  260. }
  261. return r;
  262. }
  263. float KDTree::orient(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d) const {
  264. return static_cast<Vector3> (a - d).dot(static_cast<Vector3> (b - d).cross(c - d));
  265. }
  266. float KDTree::testIntersection(const Vector3& pos, const Vector3& direction, const KDTree::Triangle& t) const {
  267. const float eps = 0.0001f;
  268. Vector3 h1 = t[1] - t[0];
  269. Vector3 h2 = t[2] - t[0];
  270. Vector3 abc = h1.cross(h2);
  271. if(abc.squareLength() < eps) {
  272. return -1.0f;
  273. }
  274. abc.normalize();
  275. float d = -abc.dot(t[0]);
  276. float check = abc.dot(direction);
  277. if(std::abs(check) < eps) {
  278. return -1.0f;
  279. }
  280. float factor = -(abc.dot(pos) + d) / check;
  281. Vector3 inter = pos + factor * direction;
  282. int sideA = 0;
  283. int sideB = 0;
  284. for(int i = 0; i < 3; i++) {
  285. const Vector3& a = t[i];
  286. const Vector3& b = t[(i + 1) % 3];
  287. const Vector3 c = t[i] + abc;
  288. float o = orient(a, b, c, inter);
  289. if(o < -eps) {
  290. sideA++;
  291. } else if(o > eps) {
  292. sideB++;
  293. } else {
  294. sideA++;
  295. sideB++;
  296. }
  297. }
  298. if(sideA >= 3 || sideB >= 3) {
  299. return factor;
  300. }
  301. return -1.0f;
  302. }