| 1 |
// Copyright 2006-2015 Tobias Sargeant ([email protected]). |
| 2 |
// |
| 3 |
// This file is part of the Carve CSG Library (http://carve-csg.com/) |
| 4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person |
| 6 |
// obtaining a copy of this software and associated documentation |
| 7 |
// files (the "Software"), to deal in the Software without |
| 8 |
// restriction, including without limitation the rights to use, copy, |
| 9 |
// modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 |
// of the Software, and to permit persons to whom the Software is |
| 11 |
// furnished to do so, subject to the following conditions: |
| 12 |
// |
| 13 |
// The above copyright notice and this permission notice shall be |
| 14 |
// included in all copies or substantial portions of the Software. |
| 15 |
// |
| 16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
// SOFTWARE. |
| 24 |
|
| 25 |
#pragma once |
| 26 |
|
| 27 |
#include <carve/carve.hpp> |
| 28 |
|
| 29 |
#include <carve/aabb.hpp> |
| 30 |
#include <carve/geom.hpp> |
| 31 |
|
| 32 |
#include <iostream> |
| 33 |
|
| 34 |
#include <cmath> |
| •35 |
#include <cstdint> |
| 36 |
#include <limits> |
| 37 |
|
| 38 |
namespace carve { |
| 39 |
namespace geom { |
| 40 |
|
| 41 |
template <unsigned ndim, typename data_t, |
| 42 |
typename aabb_calc_t = carve::geom::get_aabb<ndim, data_t> > |
| 43 |
struct RTreeNode { |
| 44 |
typedef aabb<ndim> aabb_t; |
| 45 |
typedef vector<ndim> vector_t; |
| 46 |
typedef RTreeNode<ndim, data_t, aabb_calc_t> node_t; |
| 47 |
|
| 48 |
aabb_t bbox; |
| 49 |
node_t* child; |
| 50 |
node_t* sibling; |
| 51 |
std::vector<data_t> data; |
| 52 |
|
| 53 |
aabb_t getAABB() const { return bbox; } |
| 54 |
|
| 55 |
struct data_aabb_t { |
| 56 |
aabb_t bbox; |
| 57 |
data_t data; |
| 58 |
|
| 59 |
data_aabb_t() {} |
| 60 |
data_aabb_t(const data_t& _data) |
| 61 |
: bbox(aabb_calc_t()(_data)), data(_data) {} |
| 62 |
|
| 63 |
aabb_t getAABB() const { return bbox; } |
| 64 |
|
| 65 |
struct cmp { |
| 66 |
size_t dim; |
| 67 |
cmp(size_t _dim) : dim(_dim) {} |
| 68 |
bool operator()(const data_aabb_t& a, const data_aabb_t& b) { |
| 69 |
return a.bbox.pos.v[dim] < b.bbox.pos.v[dim]; |
| 70 |
} |
| 71 |
}; |
| 72 |
}; |
| 73 |
|
| 74 |
// Fill an rtree node with a set of (data, aabb) pairs. |
| 75 |
template <typename iter_t> |
| 76 |
void _fill(iter_t begin, iter_t end, data_aabb_t) { |
| 77 |
data.reserve(std::distance(begin, end)); |
| 78 |
for (iter_t i = begin; i != end; ++i) { |
| 79 |
data.push_back((*i).data); |
| 80 |
} |
| 81 |
bbox.fit(begin, end); |
| 82 |
} |
| 83 |
|
| 84 |
// Fill an rtree node with a set of data. |
| 85 |
template <typename iter_t> |
| 86 |
void _fill(iter_t begin, iter_t end, data_t) { |
| 87 |
data.reserve(std::distance(begin, end)); |
| 88 |
std::copy(begin, end, std::back_inserter(data)); |
| 89 |
bbox.fit(begin, end, aabb_calc_t()); |
| 90 |
} |
| 91 |
|
| 92 |
// Fill an rtree node with a set of child nodes. |
| 93 |
template <typename iter_t> |
| 94 |
void _fill(iter_t begin, iter_t end, node_t*) { |
| 95 |
iter_t i = begin; |
| 96 |
node_t* curr = child = *i; |
| 97 |
while (++i != end) { |
| 98 |
curr->sibling = *i; |
| 99 |
curr = curr->sibling; |
| 100 |
} |
| 101 |
bbox.fit(begin, end); |
| 102 |
} |
| 103 |
|
| 104 |
// Search the rtree for objects that intersect obj (generally an aabb). |
| 105 |
// The aabb class must provide a method intersects(obj_t). |
| 106 |
template <typename obj_t, typename out_iter_t> |
| 107 |
void search(const obj_t& obj, out_iter_t out) const { |
| 108 |
if (!bbox.intersects(obj)) { |
| 109 |
return; |
| 110 |
} |
| 111 |
if (child) { |
| 112 |
for (node_t* node = child; node; node = node->sibling) { |
| 113 |
node->search(obj, out); |
| 114 |
} |
| 115 |
} else { |
| 116 |
std::copy(data.begin(), data.end(), out); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// update the bounding box extents of nodes that intersect obj (generally an |
| 121 |
// aabb). |
| 122 |
// The aabb class must provide a method intersects(obj_t). |
| 123 |
template <typename obj_t> |
| 124 |
void updateExtents(const obj_t& obj) { |
| 125 |
if (!bbox.intersects(obj)) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (child) { |
| 130 |
node_t* node = child; |
| 131 |
node->updateExtents(obj); |
| 132 |
bbox = node->bbox; |
| 133 |
for (node = node->sibling; node; node = node->sibling) { |
| 134 |
node->updateExtents(obj); |
| 135 |
bbox.unionAABB(node->bbox); |
| 136 |
} |
| 137 |
} else { |
| 138 |
bbox.fit(data.begin(), data.end()); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// update the bounding box extents of nodes that intersect obj (generally an |
| 143 |
// aabb). |
| 144 |
// The aabb class must provide a method intersects(obj_t). |
| 145 |
bool remove(const data_t& val, const aabb_t& val_aabb) { |
| 146 |
if (!bbox.intersects(val_aabb)) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
if (child) { |
| 151 |
node_t* node = child; |
| 152 |
node->remove(val, val_aabb); |
| 153 |
bbox = node->bbox; |
| 154 |
bool removed = false; |
| 155 |
for (node = node->sibling; node; node = node->sibling) { |
| 156 |
if (!removed) { |
| 157 |
removed = node->remove(val, val_aabb); |
| 158 |
} |
| 159 |
bbox.unionAABB(node->bbox); |
| 160 |
} |
| 161 |
return removed; |
| 162 |
} else { |
| 163 |
typename std::vector<data_t>::iterator i = |
| 164 |
std::remove(data.begin(), data.end(), val); |
| 165 |
if (i == data.end()) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
data.erase(i, data.end()); |
| 169 |
bbox.fit(data.begin(), data.end()); |
| 170 |
return true; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
template <typename iter_t> |
| 175 |
RTreeNode(iter_t begin, iter_t end) |
| 176 |
: bbox(), child(nullptr), sibling(nullptr), data() { |
| 177 |
_fill(begin, end, typename std::iterator_traits<iter_t>::value_type()); |
| 178 |
} |
| 179 |
|
| 180 |
~RTreeNode() { |
| 181 |
if (child) { |
| 182 |
RTreeNode* next = child; |
| 183 |
while (next) { |
| 184 |
RTreeNode* curr = next; |
| 185 |
next = next->sibling; |
| 186 |
delete curr; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// functor for ordering nodes by increasing aabb midpoint, along a specified |
| 192 |
// axis. |
| 193 |
struct aabb_cmp_mid { |
| 194 |
size_t dim; |
| 195 |
aabb_cmp_mid(size_t _dim) : dim(_dim) {} |
| 196 |
|
| 197 |
bool operator()(const node_t* a, const node_t* b) { |
| 198 |
return a->bbox.mid(dim) < b->bbox.mid(dim); |
| 199 |
} |
| 200 |
bool operator()(const data_aabb_t& a, const data_aabb_t& b) { |
| 201 |
return a.bbox.mid(dim) < b.bbox.mid(dim); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
// functor for ordering nodes by increasing aabb minimum, along a specified |
| 206 |
// axis. |
| 207 |
struct aabb_cmp_min { |
| 208 |
size_t dim; |
| 209 |
aabb_cmp_min(size_t _dim) : dim(_dim) {} |
| 210 |
|
| 211 |
bool operator()(const node_t* a, const node_t* b) { |
| 212 |
return a->bbox.min(dim) < b->bbox.min(dim); |
| 213 |
} |
| 214 |
bool operator()(const data_aabb_t& a, const data_aabb_t& b) { |
| 215 |
return a.bbox.min(dim) < b.bbox.min(dim); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
// functor for ordering nodes by increasing aabb maximum, along a specified |
| 220 |
// axis. |
| 221 |
struct aabb_cmp_max { |
| 222 |
size_t dim; |
| 223 |
aabb_cmp_max(size_t _dim) : dim(_dim) {} |
| 224 |
|
| 225 |
bool operator()(const node_t* a, const node_t* b) { |
| 226 |
return a->bbox.max(dim) < b->bbox.max(dim); |
| 227 |
} |
| 228 |
bool operator()(const data_aabb_t& a, const data_aabb_t& b) { |
| 229 |
return a.bbox.max(dim) < b.bbox.max(dim); |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
// facade for projecting node bounding box onto an axis. |
| 234 |
struct aabb_extent { |
| 235 |
size_t dim; |
| 236 |
aabb_extent(size_t _dim) : dim(_dim) {} |
| 237 |
|
| 238 |
double min(const node_t* a) { |
| 239 |
return a->bbox.pos.v[dim] - a->bbox.extent.v[dim]; |
| 240 |
} |
| 241 |
double max(const node_t* a) { |
| 242 |
return a->bbox.pos.v[dim] + a->bbox.extent.v[dim]; |
| 243 |
} |
| 244 |
double len(const node_t* a) { return 2.0 * a->bbox.extent.v[dim]; } |
| 245 |
double min(const data_aabb_t& a) { |
| 246 |
return a.bbox.pos.v[dim] - a.bbox.extent.v[dim]; |
| 247 |
} |
| 248 |
double max(const data_aabb_t& a) { |
| 249 |
return a.bbox.pos.v[dim] + a.bbox.extent.v[dim]; |
| 250 |
} |
| 251 |
double len(const data_aabb_t& a) { return 2.0 * a.bbox.extent.v[dim]; } |
| 252 |
}; |
| 253 |
|
| 254 |
template <typename iter_t> |
| 255 |
static void makeNodes(const iter_t begin, const iter_t end, size_t dim_num, |
| 256 |
uint32_t dim_mask, size_t child_size, |
| 257 |
std::vector<node_t*>& out) { |
| 258 |
const size_t N = std::distance(begin, end); |
| 259 |
|
| 260 |
size_t dim = ndim; |
| 261 |
double r_best = N + 1; |
| 262 |
|
| 263 |
// find the sparsest remaining dimension to partition by. |
| 264 |
for (size_t i = 0; i < ndim; ++i) { |
| 265 |
if (dim_mask & (1U << i)) { |
| 266 |
continue; |
| 267 |
} |
| 268 |
aabb_extent extent(i); |
| 269 |
double dmin, dmax, dsum; |
| 270 |
|
| 271 |
dmin = extent.min(*begin); |
| 272 |
dmax = extent.max(*begin); |
| 273 |
dsum = 0.0; |
| 274 |
for (iter_t j = begin; j != end; ++j) { |
| 275 |
dmin = std::min(dmin, extent.min(*j)); |
| 276 |
dmax = std::max(dmax, extent.max(*j)); |
| 277 |
dsum += extent.len(*j); |
| 278 |
} |
| 279 |
double r = dsum ? dsum / (dmax - dmin) : 0.0; |
| 280 |
if (r_best > r) { |
| 281 |
dim = i; |
| 282 |
r_best = r; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
CARVE_ASSERT(dim < ndim); |
| 287 |
|
| 288 |
// dim = dim_num; |
| 289 |
|
| 290 |
const size_t P = (N + child_size - 1) / child_size; |
| 291 |
const size_t n_parts = |
| 292 |
(size_t)std::ceil(std::pow((double)P, 1.0 / (ndim - dim_num))); |
| 293 |
|
| 294 |
std::sort(begin, end, aabb_cmp_mid(dim)); |
| 295 |
|
| 296 |
if (dim_num == ndim - 1 || n_parts == 1) { |
| 297 |
for (size_t i = 0, s = 0, e = 0; i < P; ++i, s = e) { |
| 298 |
e = N * (i + 1) / P; |
| 299 |
CARVE_ASSERT(e - s <= child_size); |
| 300 |
out.push_back(new node_t(begin + s, begin + e)); |
| 301 |
} |
| 302 |
} else { |
| 303 |
for (size_t i = 0, s = 0, e = 0; i < n_parts; ++i, s = e) { |
| 304 |
e = N * (i + 1) / n_parts; |
| 305 |
makeNodes(begin + s, begin + e, dim_num + 1, dim_mask | (1U << dim), |
| 306 |
child_size, out); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
static node_t* construct_STR(std::vector<data_aabb_t>& data, size_t leaf_size, |
| 312 |
size_t internal_size) { |
| 313 |
std::vector<node_t*> out; |
| 314 |
makeNodes(data.begin(), data.end(), 0, 0, leaf_size, out); |
| 315 |
|
| 316 |
while (out.size() > 1) { |
| 317 |
std::vector<node_t*> next; |
| 318 |
makeNodes(out.begin(), out.end(), 0, 0, internal_size, next); |
| 319 |
std::swap(out, next); |
| 320 |
} |
| 321 |
|
| 322 |
CARVE_ASSERT(out.size() == 1); |
| 323 |
return out[0]; |
| 324 |
} |
| 325 |
|
| 326 |
template <typename iter_t> |
| 327 |
static node_t* construct_STR(const iter_t& begin, const iter_t& end, |
| 328 |
size_t leaf_size, size_t internal_size) { |
| 329 |
std::vector<data_aabb_t> data; |
| 330 |
data.reserve(std::distance(begin, end)); |
| 331 |
for (iter_t i = begin; i != end; ++i) { |
| 332 |
data.push_back(*i); |
| 333 |
} |
| 334 |
return construct_STR(data, leaf_size, internal_size); |
| 335 |
} |
| 336 |
|
| 337 |
template <typename iter_t> |
| 338 |
static node_t* construct_STR(const iter_t& begin1, const iter_t& end1, |
| 339 |
const iter_t& begin2, const iter_t& end2, |
| 340 |
size_t leaf_size, size_t internal_size) { |
| 341 |
std::vector<data_aabb_t> data; |
| 342 |
data.reserve(std::distance(begin1, end1) + std::distance(begin2, end2)); |
| 343 |
for (iter_t i = begin1; i != end1; ++i) { |
| 344 |
data.push_back(*i); |
| 345 |
} |
| 346 |
for (iter_t i = begin2; i != end2; ++i) { |
| 347 |
data.push_back(*i); |
| 348 |
} |
| 349 |
return construct_STR(data, leaf_size, internal_size); |
| 350 |
} |
| 351 |
|
| 352 |
struct partition_info { |
| 353 |
double score; |
| 354 |
size_t partition_pos; |
| 355 |
|
| 356 |
partition_info() |
| 357 |
: score(std::numeric_limits<double>::max()), partition_pos(0) {} |
| 358 |
partition_info(double _score, size_t _partition_pos) |
| 359 |
: score(_score), partition_pos(_partition_pos) {} |
| 360 |
}; |
| 361 |
|
| 362 |
static partition_info findPartition( |
| 363 |
typename std::vector<data_aabb_t>::iterator base, |
| 364 |
std::vector<size_t>::iterator begin, std::vector<size_t>::iterator end, |
| 365 |
size_t part_size) { |
| 366 |
CARVE_ASSERT(begin < end); |
| 367 |
|
| 368 |
partition_info best(std::numeric_limits<double>::max(), 0); |
| 369 |
const size_t N = (size_t)std::distance(begin, end); |
| 370 |
|
| 371 |
std::vector<double> rhs_vol(N, 0.0); |
| 372 |
|
| 373 |
aabb_t rhs = base[begin[N - 1]].aabb; |
| 374 |
rhs_vol[N - 1] = rhs.volume(); |
| 375 |
for (size_t i = N - 1; i > 0;) { |
| 376 |
rhs.unionAABB(base[begin[--i]].aabb); |
| 377 |
rhs_vol[i] = rhs.volume(); |
| 378 |
} |
| 379 |
|
| 380 |
aabb_t lhs = base[begin[0]].aabb; |
| 381 |
for (size_t i = 1; i < N; ++i) { |
| 382 |
lhs.unionAABB(base[begin[i]].aabb); |
| 383 |
if (i % part_size == 0 || (N - i) % part_size == 0) { |
| 384 |
partition_info curr(lhs.volume() + rhs_vol[i], i); |
| 385 |
if (best.score > curr.score) { |
| 386 |
best = curr; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
return best; |
| 391 |
} |
| 392 |
|
| 393 |
static void partition(typename std::vector<data_aabb_t>::iterator base, |
| 394 |
std::vector<size_t>::iterator begin, |
| 395 |
std::vector<size_t>::iterator end, size_t part_size, |
| 396 |
std::vector<size_t>& part_num, size_t& part_next) { |
| 397 |
CARVE_ASSERT(begin < end); |
| 398 |
|
| 399 |
const size_t N = (size_t)std::distance(begin, end); |
| 400 |
|
| 401 |
partition_info best; |
| 402 |
partition_info curr; |
| 403 |
size_t part_curr = part_num[*begin]; |
| 404 |
|
| 405 |
std::vector<size_t> tmp(begin, end); |
| 406 |
|
| 407 |
for (size_t dim = 0; dim < ndim; ++dim) { |
| 408 |
std::sort(tmp.begin(), tmp.end(), |
| 409 |
make_index_sort(base, aabb_cmp_min(dim))); |
| 410 |
curr = findPartition(base, tmp.begin(), tmp.end(), part_size); |
| 411 |
if (best.score > curr.score) { |
| 412 |
best = curr; |
| 413 |
std::copy(tmp.begin(), tmp.end(), begin); |
| 414 |
} |
| 415 |
|
| 416 |
std::sort(tmp.begin(), tmp.end(), |
| 417 |
make_index_sort(base, aabb_cmp_mid(dim))); |
| 418 |
curr = findPartition(base, tmp.begin(), tmp.end(), part_size); |
| 419 |
if (best.score > curr.score) { |
| 420 |
best = curr; |
| 421 |
std::copy(tmp.begin(), tmp.end(), begin); |
| 422 |
} |
| 423 |
|
| 424 |
std::sort(tmp.begin(), tmp.end(), |
| 425 |
make_index_sort(base, aabb_cmp_max(dim))); |
| 426 |
curr = findPartition(base, tmp.begin(), tmp.end(), part_size); |
| 427 |
if (best.score > curr.score) { |
| 428 |
best = curr; |
| 429 |
std::copy(tmp.begin(), tmp.end(), begin); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
for (size_t j = 0; j < best.partition_pos; ++j) { |
| 434 |
part_num[begin[(ssize_t)j]] = part_curr; |
| 435 |
} |
| 436 |
for (size_t j = best.partition_pos; j < N; ++j) { |
| 437 |
part_num[begin[(ssize_t)j]] = part_next; |
| 438 |
} |
| 439 |
++part_next; |
| 440 |
|
| 441 |
if (best.partition_pos > part_size) { |
| 442 |
partition(base, begin, begin + best.partition_pos, part_size, part_num, |
| 443 |
part_next); |
| 444 |
} |
| 445 |
if (N - best.partition_pos > part_size) { |
| 446 |
partition(base, begin + best.partition_pos, end, part_size, part_num, |
| 447 |
part_next); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
static size_t makePartitions( |
| 452 |
typename std::vector<data_aabb_t>::iterator begin, |
| 453 |
typename std::vector<data_aabb_t>::iterator end, size_t part_size, |
| 454 |
std::vector<size_t>& part_num) { |
| 455 |
const size_t N = std::distance(begin, end); |
| 456 |
std::vector<size_t> idx; |
| 457 |
idx.reserve(N); |
| 458 |
for (size_t i = 0; i < N; ++i) { |
| 459 |
idx.push_back(i); |
| 460 |
} |
| 461 |
size_t part_next = 1; |
| 462 |
|
| 463 |
partition(begin, idx.begin(), idx.end(), part_size, part_num, part_next); |
| 464 |
return part_next; |
| 465 |
} |
| 466 |
|
| 467 |
static node_t* construct_TGS( |
| 468 |
typename std::vector<data_aabb_t>::iterator begin, |
| 469 |
typename std::vector<data_aabb_t>::iterator end, size_t leaf_size, |
| 470 |
size_t internal_size) { |
| 471 |
size_t N = std::distance(begin, end); |
| 472 |
|
| 473 |
if (N <= leaf_size) { |
| 474 |
return new node_t(begin, end); |
| 475 |
} else { |
| 476 |
size_t P = (N + internal_size - 1) / internal_size; |
| 477 |
std::vector<size_t> part_num(N, 0); |
| 478 |
P = makePartitions(begin, end, P, part_num); |
| 479 |
|
| 480 |
size_t S = 0, E = 0; |
| 481 |
std::vector<node_t*> children; |
| 482 |
for (size_t i = 0; i < P; ++i) { |
| 483 |
size_t j = S, k = N; |
| 484 |
while (true) { |
| 485 |
while (true) { |
| 486 |
if (j == k) { |
| 487 |
goto done; |
| 488 |
} else if (part_num[j] == i) { |
| 489 |
++j; |
| 490 |
} else { |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
--k; |
| 495 |
while (true) { |
| 496 |
if (j == k) { |
| 497 |
goto done; |
| 498 |
} else if (part_num[k] != i) { |
| 499 |
--k; |
| 500 |
} else { |
| 501 |
break; |
| 502 |
} |
| 503 |
} |
| 504 |
std::swap(*(begin + j), *(begin + k)); |
| 505 |
std::swap(part_num[j], part_num[k]); |
| 506 |
++j; |
| 507 |
} |
| 508 |
done: |
| 509 |
E = j; |
| 510 |
children.push_back( |
| 511 |
construct_TGS(begin + S, begin + E, leaf_size, internal_size)); |
| 512 |
S = E; |
| 513 |
} |
| 514 |
return new node_t(children.begin(), children.end()); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
template <typename iter_t> |
| 519 |
static node_t* construct_TGS(const iter_t& begin, const iter_t& end, |
| 520 |
size_t leaf_size, size_t internal_size) { |
| 521 |
std::vector<data_aabb_t> data; |
| 522 |
data.reserve(std::distance(begin, end)); |
| 523 |
for (iter_t i = begin; i != end; ++i) { |
| 524 |
data.push_back(*i); |
| 525 |
} |
| 526 |
return construct_TGS(data.begin(), data.end(), leaf_size, internal_size); |
| 527 |
} |
| 528 |
|
| 529 |
template <typename iter_t> |
| 530 |
static node_t* construct_TGS(const iter_t& begin1, const iter_t& end1, |
| 531 |
const iter_t& begin2, const iter_t& end2, |
| 532 |
size_t leaf_size, size_t internal_size) { |
| 533 |
std::vector<data_aabb_t> data; |
| 534 |
data.reserve(std::distance(begin1, end1) + std::distance(begin2, end2)); |
| 535 |
for (iter_t i = begin1; i != end1; ++i) { |
| 536 |
data.push_back(*i); |
| 537 |
} |
| 538 |
for (iter_t i = begin2; i != end2; ++i) { |
| 539 |
data.push_back(*i); |
| 540 |
} |
| 541 |
return construct_TGS(data.begin(), data.end(), leaf_size, internal_size); |
| 542 |
} |
| 543 |
}; |
| 544 |
} // namespace geom |
| 545 |
} // namespace carve |