------------------------------------------------------------------------------
| 1408 | |
| 1409 | //------------------------------------------------------------------------------ |
| 1410 | int vtkKdTree::Select(int dim, float* c1, int* ids, int nvals, double& coord) |
| 1411 | { |
| 1412 | int left = 0; |
| 1413 | int mid = nvals / 2; |
| 1414 | int right = nvals - 1; |
| 1415 | |
| 1416 | vtkKdTree::Select_(dim, c1, ids, left, right, mid); |
| 1417 | |
| 1418 | // We need to be careful in the case where the "mid" |
| 1419 | // value is repeated several times in the array. We |
| 1420 | // want to roll the dividing index (mid) back to the |
| 1421 | // first occurrence in the array, so that there is no |
| 1422 | // ambiguity about which spatial region a given point |
| 1423 | // belongs in. |
| 1424 | // |
| 1425 | // The array has been rearranged (in Select_) like this: |
| 1426 | // |
| 1427 | // All values c1[n], left <= n < mid, satisfy c1[n] <= c1[mid] |
| 1428 | // All values c1[n], mid < n <= right, satisfy c1[n] >= c1[mid] |
| 1429 | // |
| 1430 | // In addition, by careful construction, there is a J <= mid |
| 1431 | // such that |
| 1432 | // |
| 1433 | // All values c1[n], n < J, satisfy c1[n] < c1[mid] STRICTLY |
| 1434 | // All values c1[n], J <= n <= mid, satisfy c1[n] = c1[mid] |
| 1435 | // All values c1[n], mid < n <= right , satisfy c1[n] >= c1[mid] |
| 1436 | // |
| 1437 | // We need to roll back the "mid" value to the "J". This |
| 1438 | // means our spatial regions are less balanced, but there |
| 1439 | // is no ambiguity regarding which region a point belongs in. |
| 1440 | |
| 1441 | int midValIndex = mid * 3 + dim; |
| 1442 | |
| 1443 | while ((mid > left) && (c1[midValIndex - 3] == c1[midValIndex])) |
| 1444 | { |
| 1445 | mid--; |
| 1446 | midValIndex -= 3; |
| 1447 | } |
| 1448 | |
| 1449 | if (mid == left) |
| 1450 | { |
| 1451 | return mid; // failed to divide region |
| 1452 | } |
| 1453 | |
| 1454 | float leftMax = vtkKdTree::FindMaxLeftHalf(dim, c1, mid); |
| 1455 | |
| 1456 | coord = (static_cast<double>(c1[midValIndex]) + static_cast<double>(leftMax)) / 2.0; |
| 1457 | |
| 1458 | return mid; |
| 1459 | } |
| 1460 | |
| 1461 | //------------------------------------------------------------------------------ |
| 1462 | float vtkKdTree::FindMaxLeftHalf(int dim, float* c1, int K) |
no outgoing calls