KDTree.cpp 11 KB

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