octree.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Begin License:
  2. // Copyright (C) 2006-2014 Tobias Sargeant (tobias.sargeant@gmail.com).
  3. // All rights reserved.
  4. //
  5. // This file is part of the Carve CSG Library (http://carve-csg.com/)
  6. //
  7. // This file may be used under the terms of either the GNU General
  8. // Public License version 2 or 3 (at your option) as published by the
  9. // Free Software Foundation and appearing in the files LICENSE.GPL2
  10. // and LICENSE.GPL3 included in the packaging of this file.
  11. //
  12. // This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
  13. // INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
  14. // A PARTICULAR PURPOSE.
  15. // End:
  16. #if defined(HAVE_CONFIG_H)
  17. # include <carve_config.h>
  18. #endif
  19. #include <carve/octree_decl.hpp>
  20. #include <carve/octree_impl.hpp>
  21. #include <carve/poly_decl.hpp>
  22. namespace carve {
  23. namespace csg {
  24. Octree::Node::Node(const carve::geom3d::Vector &newMin, const carve::geom3d::Vector &newMax) :
  25. parent(NULL), is_leaf(true), min(newMin), max(newMax) {
  26. for (int i = 0; i < 8; ++i) children[i] = NULL;
  27. aabb = Octree::makeAABB(this);
  28. }
  29. Octree::Node::Node(Node *p, double x1, double y1, double z1, double x2, double y2, double z2) :
  30. parent(p), is_leaf(true), min(carve::geom::VECTOR(x1, y1, z1)), max(carve::geom::VECTOR(x2, y2, z2)) {
  31. for (int i = 0; i < 8; ++i) children[i] = NULL;
  32. aabb = Octree::makeAABB(this);
  33. }
  34. Octree::Node::~Node() {
  35. for (int i = 0; i < 8; ++i) {
  36. if (children[i] != NULL) {
  37. (*children[i]).~Node();
  38. }
  39. }
  40. if (children[0] != NULL) {
  41. char *ptr = (char*)children[0];
  42. delete[] ptr;
  43. }
  44. }
  45. bool Octree::Node::mightContain(const carve::poly::Face<3> &face) {
  46. if (face.nVertices() == 3) {
  47. return aabb.intersects(carve::geom::tri<3>(face.vertex(0)->v, face.vertex(1)->v, face.vertex(2)->v));
  48. } else {
  49. return aabb.intersects(face.aabb) && aabb.intersects(face.plane_eqn);
  50. }
  51. }
  52. bool Octree::Node::mightContain(const carve::poly::Edge<3> &edge) {
  53. return aabb.intersectsLineSegment(edge.v1->v, edge.v2->v);
  54. }
  55. bool Octree::Node::mightContain(const carve::poly::Vertex<3> &p) {
  56. return aabb.containsPoint(p.v);
  57. }
  58. bool Octree::Node::hasChildren() {
  59. return !is_leaf;
  60. }
  61. bool Octree::Node::split() {
  62. if (is_leaf && hasGeometry()) {
  63. carve::geom3d::Vector mid = 0.5 * (min + max);
  64. char *ptr = new char[sizeof(Node)*8];
  65. children[0] = new (ptr + sizeof(Node) * 0) Node(this, min.x, min.y, min.z, mid.x, mid.y, mid.z);
  66. children[1] = new (ptr + sizeof(Node) * 1) Node(this, mid.x, min.y, min.z, max.x, mid.y, mid.z);
  67. children[2] = new (ptr + sizeof(Node) * 2) Node(this, min.x, mid.y, min.z, mid.x, max.y, mid.z);
  68. children[3] = new (ptr + sizeof(Node) * 3) Node(this, mid.x, mid.y, min.z, max.x, max.y, mid.z);
  69. children[4] = new (ptr + sizeof(Node) * 4) Node(this, min.x, min.y, mid.z, mid.x, mid.y, max.z);
  70. children[5] = new (ptr + sizeof(Node) * 5) Node(this, mid.x, min.y, mid.z, max.x, mid.y, max.z);
  71. children[6] = new (ptr + sizeof(Node) * 6) Node(this, min.x, mid.y, mid.z, mid.x, max.y, max.z);
  72. children[7] = new (ptr + sizeof(Node) * 7) Node(this, mid.x, mid.y, mid.z, max.x, max.y, max.z);
  73. for (int i = 0; i < 8; ++i) {
  74. putInside(faces, children[i], children[i]->faces);
  75. putInside(edges, children[i], children[i]->edges);
  76. putInside(vertices, children[i], children[i]->vertices);
  77. }
  78. faces.clear();
  79. edges.clear();
  80. vertices.clear();
  81. is_leaf = false;
  82. }
  83. return is_leaf;
  84. }
  85. template <class T>
  86. void Octree::Node::putInside(const T &input, Node *child, T &output) {
  87. for (typename T::const_iterator it = input.begin(), e = input.end(); it != e; ++it) {
  88. if (child->mightContain(**it)) {
  89. output.push_back(*it);
  90. }
  91. }
  92. }
  93. bool Octree::Node::hasGeometry() {
  94. return faces.size() > 0 || edges.size() > 0 || vertices.size() > 0;
  95. }
  96. Octree::Octree() {
  97. root = NULL;
  98. }
  99. Octree::~Octree() {
  100. if (root) delete root;
  101. }
  102. void Octree::setBounds(const carve::geom3d::Vector &min, const carve::geom3d::Vector &max) {
  103. if (root) delete root;
  104. root = new Node(min, max);
  105. }
  106. void Octree::setBounds(carve::geom3d::AABB aabb) {
  107. if (root) delete root;
  108. aabb.extent = 1.1 * aabb.extent;
  109. root = new Node(aabb.min(), aabb.max());
  110. }
  111. void Octree::addEdges(const std::vector<carve::poly::Edge<3> > &e) {
  112. root->edges.reserve(root->edges.size() + e.size());
  113. for (size_t i = 0; i < e.size(); ++i) {
  114. root->edges.push_back(&e[i]);
  115. }
  116. }
  117. void Octree::addFaces(const std::vector<carve::poly::Face<3> > &f) {
  118. root->faces.reserve(root->faces.size() + f.size());
  119. for (size_t i = 0; i < f.size(); ++i) {
  120. root->faces.push_back(&f[i]);
  121. }
  122. }
  123. void Octree::addVertices(const std::vector<const carve::poly::Vertex<3> *> &p) {
  124. root->vertices.insert(root->vertices.end(), p.begin(), p.end());
  125. }
  126. carve::geom3d::AABB Octree::makeAABB(const Node *node) {
  127. carve::geom3d::Vector centre = 0.5 * (node->min + node->max);
  128. carve::geom3d::Vector size = SLACK_FACTOR * 0.5 * (node->max - node->min);
  129. return carve::geom3d::AABB(centre, size);
  130. }
  131. void Octree::doFindEdges(const carve::geom::aabb<3> &aabb,
  132. Node *node,
  133. std::vector<const carve::poly::Edge<3> *> &out,
  134. unsigned depth) const {
  135. if (node == NULL) {
  136. return;
  137. }
  138. if (node->aabb.intersects(aabb)) {
  139. if (node->hasChildren()) {
  140. for (int i = 0; i < 8; ++i) {
  141. doFindEdges(aabb, node->children[i], out, depth + 1);
  142. }
  143. } else {
  144. if (depth < MAX_SPLIT_DEPTH && node->edges.size() > EDGE_SPLIT_THRESHOLD) {
  145. if (!node->split()) {
  146. for (int i = 0; i < 8; ++i) {
  147. doFindEdges(aabb, node->children[i], out, depth + 1);
  148. }
  149. return;
  150. }
  151. }
  152. for (std::vector<const carve::poly::Edge<3>*>::const_iterator it = node->edges.begin(), e = node->edges.end(); it != e; ++it) {
  153. if ((*it)->tag_once()) {
  154. out.push_back(*it);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. void Octree::doFindEdges(const carve::geom3d::LineSegment &l,
  161. Node *node,
  162. std::vector<const carve::poly::Edge<3> *> &out,
  163. unsigned depth) const {
  164. if (node == NULL) {
  165. return;
  166. }
  167. if (node->aabb.intersectsLineSegment(l.v1, l.v2)) {
  168. if (node->hasChildren()) {
  169. for (int i = 0; i < 8; ++i) {
  170. doFindEdges(l, node->children[i], out, depth + 1);
  171. }
  172. } else {
  173. if (depth < MAX_SPLIT_DEPTH && node->edges.size() > EDGE_SPLIT_THRESHOLD) {
  174. if (!node->split()) {
  175. for (int i = 0; i < 8; ++i) {
  176. doFindEdges(l, node->children[i], out, depth + 1);
  177. }
  178. return;
  179. }
  180. }
  181. for (std::vector<const carve::poly::Edge<3>*>::const_iterator it = node->edges.begin(), e = node->edges.end(); it != e; ++it) {
  182. if ((*it)->tag_once()) {
  183. out.push_back(*it);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. void Octree::doFindEdges(const carve::geom3d::Vector &v,
  190. Node *node,
  191. std::vector<const carve::poly::Edge<3> *> &out,
  192. unsigned depth) const {
  193. if (node == NULL) {
  194. return;
  195. }
  196. if (node->aabb.containsPoint(v)) {
  197. if (node->hasChildren()) {
  198. for (int i = 0; i < 8; ++i) {
  199. doFindEdges(v, node->children[i], out, depth + 1);
  200. }
  201. } else {
  202. if (depth < MAX_SPLIT_DEPTH && node->edges.size() > EDGE_SPLIT_THRESHOLD) {
  203. if (!node->split()) {
  204. for (int i = 0; i < 8; ++i) {
  205. doFindEdges(v, node->children[i], out, depth + 1);
  206. }
  207. return;
  208. }
  209. }
  210. for (std::vector<const carve::poly::Edge<3>*>::const_iterator
  211. it = node->edges.begin(), e = node->edges.end(); it != e; ++it) {
  212. if ((*it)->tag_once()) {
  213. out.push_back(*it);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. void Octree::doFindFaces(const carve::geom::aabb<3> &aabb,
  220. Node *node,
  221. std::vector<const carve::poly::Face<3>*> &out,
  222. unsigned depth) const {
  223. if (node == NULL) {
  224. return;
  225. }
  226. if (node->aabb.intersects(aabb)) {
  227. if (node->hasChildren()) {
  228. for (int i = 0; i < 8; ++i) {
  229. doFindFaces(aabb, node->children[i], out, depth + 1);
  230. }
  231. } else {
  232. if (depth < MAX_SPLIT_DEPTH && node->faces.size() > FACE_SPLIT_THRESHOLD) {
  233. if (!node->split()) {
  234. for (int i = 0; i < 8; ++i) {
  235. doFindFaces(aabb, node->children[i], out, depth + 1);
  236. }
  237. return;
  238. }
  239. }
  240. for (std::vector<const carve::poly::Face<3>*>::const_iterator it = node->faces.begin(), e = node->faces.end(); it != e; ++it) {
  241. if ((*it)->tag_once()) {
  242. out.push_back(*it);
  243. }
  244. }
  245. }
  246. }
  247. }
  248. void Octree::doFindFaces(const carve::geom3d::LineSegment &l,
  249. Node *node,
  250. std::vector<const carve::poly::Face<3>*> &out,
  251. unsigned depth) const {
  252. if (node == NULL) {
  253. return;
  254. }
  255. if (node->aabb.intersectsLineSegment(l.v1, l.v2)) {
  256. if (node->hasChildren()) {
  257. for (int i = 0; i < 8; ++i) {
  258. doFindFaces(l, node->children[i], out, depth + 1);
  259. }
  260. } else {
  261. if (depth < MAX_SPLIT_DEPTH && node->faces.size() > FACE_SPLIT_THRESHOLD) {
  262. if (!node->split()) {
  263. for (int i = 0; i < 8; ++i) {
  264. doFindFaces(l, node->children[i], out, depth + 1);
  265. }
  266. return;
  267. }
  268. }
  269. for (std::vector<const carve::poly::Face<3>*>::const_iterator it = node->faces.begin(), e = node->faces.end(); it != e; ++it) {
  270. if ((*it)->tag_once()) {
  271. out.push_back(*it);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. void Octree::doFindVerticesAllowDupes(const carve::geom3d::Vector &v, Node *node, std::vector<const carve::poly::Vertex<3> *> &out, unsigned depth) const {
  278. if (node == NULL) {
  279. return;
  280. }
  281. if (node->aabb.containsPoint(v)) {
  282. if (node->hasChildren()) {
  283. for (int i = 0; i < 8; ++i) {
  284. doFindVerticesAllowDupes(v, node->children[i], out, depth + 1);
  285. }
  286. } else {
  287. if (depth < MAX_SPLIT_DEPTH && node->vertices.size() > POINT_SPLIT_THRESHOLD) {
  288. if (!node->split()) {
  289. for (int i = 0; i < 8; ++i) {
  290. doFindVerticesAllowDupes(v, node->children[i], out, depth + 1);
  291. }
  292. return;
  293. }
  294. }
  295. for (std::vector<const carve::poly::Vertex<3> *>::const_iterator it = node->vertices.begin(), e = node->vertices.end(); it != e; ++it) {
  296. out.push_back(*it);
  297. }
  298. }
  299. }
  300. }
  301. void Octree::findEdgesNear(const carve::geom::aabb<3> &aabb, std::vector<const carve::poly::Edge<3>*> &out) const {
  302. tagable::tag_begin();
  303. doFindEdges(aabb, root, out, 0);
  304. }
  305. void Octree::findEdgesNear(const carve::geom3d::LineSegment &l, std::vector<const carve::poly::Edge<3>*> &out) const {
  306. tagable::tag_begin();
  307. doFindEdges(l, root, out, 0);
  308. }
  309. void Octree::findEdgesNear(const carve::poly::Edge<3> &e, std::vector<const carve::poly::Edge<3>*> &out) const {
  310. tagable::tag_begin();
  311. doFindEdges(carve::geom3d::LineSegment(e.v1->v, e.v2->v), root, out, 0);
  312. }
  313. void Octree::findEdgesNear(const carve::geom3d::Vector &v, std::vector<const carve::poly::Edge<3>*> &out) const {
  314. tagable::tag_begin();
  315. doFindEdges(v, root, out, 0);
  316. }
  317. void Octree::findFacesNear(const carve::geom::aabb<3> &aabb, std::vector<const carve::poly::Face<3>*> &out) const {
  318. tagable::tag_begin();
  319. doFindFaces(aabb, root, out, 0);
  320. }
  321. void Octree::findFacesNear(const carve::geom3d::LineSegment &l, std::vector<const carve::poly::Face<3>*> &out) const {
  322. tagable::tag_begin();
  323. doFindFaces(l, root, out, 0);
  324. }
  325. void Octree::findFacesNear(const carve::poly::Edge<3> &e, std::vector<const carve::poly::Face<3>*> &out) const {
  326. tagable::tag_begin();
  327. doFindFaces(carve::geom3d::LineSegment(e.v1->v, e.v2->v), root, out, 0);
  328. }
  329. void Octree::findVerticesNearAllowDupes(const carve::geom3d::Vector &v, std::vector<const carve::poly::Vertex<3> *> &out) const {
  330. tagable::tag_begin();
  331. doFindVerticesAllowDupes(v, root, out, 0);
  332. }
  333. void Octree::doSplit(int maxSplit, Node *node) {
  334. // Don't split down any further than 4 levels.
  335. if (maxSplit <= 0 || (node->edges.size() < 5 && node->faces.size() < 5)) {
  336. return;
  337. }
  338. if (!node->split()) {
  339. for (int i = 0; i < 8; ++i) {
  340. doSplit(maxSplit - 1, node->children[i]);
  341. }
  342. }
  343. }
  344. void Octree::splitTree() {
  345. // initially split 4 levels
  346. doSplit(0, root);
  347. }
  348. }
  349. }