tile38/index/rtree/rtreed.go

699 lines
22 KiB
Go
Raw Normal View History

2016-10-03 03:28:23 +03:00
package rtree
import "math"
type float float32
const d3roundValues = true // only set to true when using 32-bit floats
func d3fmin(a, b float) float {
2016-10-03 03:28:23 +03:00
if a < b {
return a
}
return b
}
func d3fmax(a, b float) float {
2016-10-03 03:28:23 +03:00
if a > b {
return a
}
return b
}
const (
2016-10-03 21:37:16 +03:00
d3numDims = 3
d3maxNodes = 13
2016-10-03 21:37:16 +03:00
d3minNodes = d3maxNodes / 2
d3useSphericalVolume = true // Better split classification, may be slower on some systems
2016-10-03 03:28:23 +03:00
)
2016-10-03 21:37:16 +03:00
var d3unitSphereVolume = []float64{
2016-10-03 03:28:23 +03:00
0.000000, 2.000000, 3.141593, // Dimension 0,1,2
4.188790, 4.934802, 5.263789, // Dimension 3,4,5
5.167713, 4.724766, 4.058712, // Dimension 6,7,8
3.298509, 2.550164, 1.884104, // Dimension 9,10,11
1.335263, 0.910629, 0.599265, // Dimension 12,13,14
0.381443, 0.235331, 0.140981, // Dimension 15,16,17
0.082146, 0.046622, 0.025807, // Dimension 18,19,20
2016-10-03 21:37:16 +03:00
}[d3numDims]
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
type d3RTree struct {
root *d3nodeT ///< Root of tree
2016-10-03 03:28:23 +03:00
}
/// Minimal bounding rectangle (n-dimensional)
2016-10-03 21:37:16 +03:00
type d3rectT struct {
min [d3numDims]float ///< Min dimensions of bounding box
max [d3numDims]float ///< Max dimensions of bounding box
2016-10-03 03:28:23 +03:00
}
/// May be data or may be another subtree
/// The parents level determines this.
/// If the parents level is 0, then this is data
2016-10-03 21:37:16 +03:00
type d3branchT struct {
rect d3rectT ///< Bounds
child *d3nodeT ///< Child node
2016-10-03 03:28:23 +03:00
data interface{} ///< Data Id or Ptr
}
2016-10-03 21:37:16 +03:00
/// d3nodeT for each branch level
type d3nodeT struct {
2016-10-03 03:28:23 +03:00
count int ///< Count
level int ///< Leaf is zero, others positive
2016-10-03 21:37:16 +03:00
branch [d3maxNodes]d3branchT ///< Branch
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
func (node *d3nodeT) isInternalNode() bool {
2016-10-03 03:28:23 +03:00
return (node.level > 0) // Not a leaf, but a internal node
}
2016-10-03 21:37:16 +03:00
func (node *d3nodeT) isLeaf() bool {
2016-10-03 03:28:23 +03:00
return (node.level == 0) // A leaf, contains data
}
// Rounding constants for float32 -> float64 conversion.
const d3RNDTOWARDS = (1.0 - 1.0/8388608.0) // Round towards zero
const d3RNDAWAY = (1.0 + 1.0/8388608.0) // Round away from zero
// Convert an sqlite3_value into an RtreeValue (presumably a float)
// while taking care to round toward negative or positive, respectively.
func d3rtreeValueDown(d float64) float {
if !d3roundValues {
return float(d)
}
f := float(d)
if float64(f) > d {
if d < 0 {
f = float(d * d3RNDAWAY)
} else {
f = float(d * d3RNDTOWARDS)
}
}
return f
}
func d3rtreeValueUp(d float64) float {
if !d3roundValues {
return float(d)
}
f := float(d)
if float64(f) < d {
if d < 0 {
f = float(d * d3RNDTOWARDS)
} else {
f = float(d * d3RNDAWAY)
}
}
return f
}
2016-10-03 03:28:23 +03:00
/// A link list of nodes for reinsertion after a delete operation
2016-10-03 21:37:16 +03:00
type d3listNodeT struct {
next *d3listNodeT ///< Next in list
node *d3nodeT ///< Node
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
const d3notTaken = -1 // indicates that position
2016-10-03 03:28:23 +03:00
/// Variables for finding a split partition
2016-10-03 21:37:16 +03:00
type d3partitionVarsT struct {
partition [d3maxNodes + 1]int
2016-10-03 03:28:23 +03:00
total int
minFill int
count [2]int
2016-10-03 21:37:16 +03:00
cover [2]d3rectT
area [2]float
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
branchBuf [d3maxNodes + 1]d3branchT
2016-10-03 03:28:23 +03:00
branchCount int
2016-10-03 21:37:16 +03:00
coverSplit d3rectT
coverSplitArea float
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
func d3New() *d3RTree {
2016-10-03 03:28:23 +03:00
// We only support machine word size simple data type eg. integer index or object pointer.
// Since we are storing as union with non data branch
2016-10-03 21:37:16 +03:00
return &d3RTree{
root: &d3nodeT{},
2016-10-03 03:28:23 +03:00
}
}
/// Insert entry
/// \param a_min Min of bounding rect
/// \param a_max Max of bounding rect
/// \param a_dataId Positive Id of data. Maybe zero, but negative numbers not allowed.
2016-10-03 21:37:16 +03:00
func (tr *d3RTree) Insert(min, max [d3numDims]float64, dataId interface{}) {
var branch d3branchT
2016-10-03 03:28:23 +03:00
branch.data = dataId
2016-10-03 21:37:16 +03:00
for axis := 0; axis < d3numDims; axis++ {
branch.rect.min[axis] = d3rtreeValueDown(min[axis])
branch.rect.max[axis] = d3rtreeValueUp(max[axis])
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
d3insertRect(&branch, &tr.root, 0)
2016-10-03 03:28:23 +03:00
}
/// Remove entry
/// \param a_min Min of bounding rect
/// \param a_max Max of bounding rect
/// \param a_dataId Positive Id of data. Maybe zero, but negative numbers not allowed.
2016-10-03 21:37:16 +03:00
func (tr *d3RTree) Remove(min, max [d3numDims]float64, dataId interface{}) {
var rect d3rectT
for axis := 0; axis < d3numDims; axis++ {
rect.min[axis] = d3rtreeValueDown(min[axis])
rect.max[axis] = d3rtreeValueUp(max[axis])
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
d3removeRect(&rect, dataId, &tr.root)
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
/// Find all within d3search rectangle
/// \param a_min Min of d3search bounding rect
/// \param a_max Max of d3search bounding rect
/// \param a_searchResult d3search result array. Caller should set grow size. Function will reset, not append to array.
2016-10-03 03:28:23 +03:00
/// \param a_resultCallback Callback function to return result. Callback should return 'true' to continue searching
/// \param a_context User context to pass as parameter to a_resultCallback
/// \return Returns the number of entries found
2016-10-03 21:37:16 +03:00
func (tr *d3RTree) Search(min, max [d3numDims]float64, resultCallback func(data interface{}) bool) int {
var rect d3rectT
for axis := 0; axis < d3numDims; axis++ {
rect.min[axis] = d3rtreeValueDown(min[axis])
rect.max[axis] = d3rtreeValueUp(max[axis])
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
foundCount, _ := d3search(tr.root, rect, 0, resultCallback)
2016-10-03 03:28:23 +03:00
return foundCount
}
/// Count the data elements in this container. This is slow as no internal counter is maintained.
2016-10-03 21:37:16 +03:00
func (tr *d3RTree) Count() int {
2016-10-03 03:28:23 +03:00
var count int
2016-10-03 21:37:16 +03:00
d3countRec(tr.root, &count)
2016-10-03 03:28:23 +03:00
return count
}
/// Remove all entries from tree
2016-10-03 21:37:16 +03:00
func (tr *d3RTree) RemoveAll() {
2016-10-03 03:28:23 +03:00
// Delete all existing nodes
2016-10-03 21:37:16 +03:00
tr.root = &d3nodeT{}
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
func d3countRec(node *d3nodeT, count *int) {
2016-10-03 03:28:23 +03:00
if node.isInternalNode() { // not a leaf node
for index := 0; index < node.count; index++ {
2016-10-03 21:37:16 +03:00
d3countRec(node.branch[index].child, count)
2016-10-03 03:28:23 +03:00
}
} else { // A leaf node
*count += node.count
}
}
// Inserts a new data rectangle into the index structure.
// Recursively descends tree, propagates splits back up.
// Returns 0 if node was not split. Old node updated.
// If node was split, returns 1 and sets the pointer pointed to by
// new_node to point to the new node. Old node updated to become one of two.
// The level argument specifies the number of steps up from the leaf
// level to insert; e.g. a data rectangle goes in at level = 0.
2016-10-03 21:37:16 +03:00
func d3insertRectRec(branch *d3branchT, node *d3nodeT, newNode **d3nodeT, level int) bool {
2016-10-03 03:28:23 +03:00
// recurse until we reach the correct level for the new record. data records
// will always be called with a_level == 0 (leaf)
if node.level > level {
// Still above level for insertion, go down tree recursively
2016-10-03 21:37:16 +03:00
var otherNode *d3nodeT
//var newBranch d3branchT
2016-10-03 03:28:23 +03:00
// find the optimal branch for this record
2016-10-03 21:37:16 +03:00
index := d3pickBranch(&branch.rect, node)
2016-10-03 03:28:23 +03:00
// recursively insert this record into the picked branch
2016-10-03 21:37:16 +03:00
childWasSplit := d3insertRectRec(branch, node.branch[index].child, &otherNode, level)
2016-10-03 03:28:23 +03:00
if !childWasSplit {
// Child was not split. Merge the bounding box of the new record with the
// existing bounding box
2016-10-03 21:37:16 +03:00
node.branch[index].rect = d3combineRect(&branch.rect, &(node.branch[index].rect))
2016-10-03 03:28:23 +03:00
return false
} else {
// Child was split. The old branches are now re-partitioned to two nodes
// so we have to re-calculate the bounding boxes of each node
2016-10-03 21:37:16 +03:00
node.branch[index].rect = d3nodeCover(node.branch[index].child)
var newBranch d3branchT
2016-10-03 03:28:23 +03:00
newBranch.child = otherNode
2016-10-03 21:37:16 +03:00
newBranch.rect = d3nodeCover(otherNode)
2016-10-03 03:28:23 +03:00
// The old node is already a child of a_node. Now add the newly-created
// node to a_node as well. a_node might be split because of that.
2016-10-03 21:37:16 +03:00
return d3addBranch(&newBranch, node, newNode)
2016-10-03 03:28:23 +03:00
}
} else if node.level == level {
// We have reached level for insertion. Add rect, split if necessary
2016-10-03 21:37:16 +03:00
return d3addBranch(branch, node, newNode)
2016-10-03 03:28:23 +03:00
} else {
// Should never occur
return false
}
}
// Insert a data rectangle into an index structure.
2016-10-03 21:37:16 +03:00
// d3insertRect provides for splitting the root;
2016-10-03 03:28:23 +03:00
// returns 1 if root was split, 0 if it was not.
// The level argument specifies the number of steps up from the leaf
// level to insert; e.g. a data rectangle goes in at level = 0.
// InsertRect2 does the recursion.
//
2016-10-03 21:37:16 +03:00
func d3insertRect(branch *d3branchT, root **d3nodeT, level int) bool {
var newNode *d3nodeT
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
if d3insertRectRec(branch, *root, &newNode, level) { // Root split
2016-10-03 03:28:23 +03:00
// Grow tree taller and new root
2016-10-03 21:37:16 +03:00
newRoot := &d3nodeT{}
2016-10-03 03:28:23 +03:00
newRoot.level = (*root).level + 1
2016-10-03 21:37:16 +03:00
var newBranch d3branchT
2016-10-03 03:28:23 +03:00
// add old root node as a child of the new root
2016-10-03 21:37:16 +03:00
newBranch.rect = d3nodeCover(*root)
2016-10-03 03:28:23 +03:00
newBranch.child = *root
2016-10-03 21:37:16 +03:00
d3addBranch(&newBranch, newRoot, nil)
2016-10-03 03:28:23 +03:00
// add the split node as a child of the new root
2016-10-03 21:37:16 +03:00
newBranch.rect = d3nodeCover(newNode)
2016-10-03 03:28:23 +03:00
newBranch.child = newNode
2016-10-03 21:37:16 +03:00
d3addBranch(&newBranch, newRoot, nil)
2016-10-03 03:28:23 +03:00
// set the new root as the root node
*root = newRoot
return true
}
return false
}
// Find the smallest rectangle that includes all rectangles in branches of a node.
2016-10-03 21:37:16 +03:00
func d3nodeCover(node *d3nodeT) d3rectT {
2016-10-03 03:28:23 +03:00
rect := node.branch[0].rect
for index := 1; index < node.count; index++ {
2016-10-03 21:37:16 +03:00
rect = d3combineRect(&rect, &(node.branch[index].rect))
2016-10-03 03:28:23 +03:00
}
return rect
}
// Add a branch to a node. Split the node if necessary.
// Returns 0 if node not split. Old node updated.
// Returns 1 if node split, sets *new_node to address of new node.
// Old node updated, becomes one of two.
2016-10-03 21:37:16 +03:00
func d3addBranch(branch *d3branchT, node *d3nodeT, newNode **d3nodeT) bool {
if node.count < d3maxNodes { // Split won't be necessary
2016-10-03 03:28:23 +03:00
node.branch[node.count] = *branch
node.count++
return false
} else {
2016-10-03 21:37:16 +03:00
d3splitNode(node, branch, newNode)
2016-10-03 03:28:23 +03:00
return true
}
}
// Disconnect a dependent node.
// Caller must return (or stop using iteration index) after this as count has changed
2016-10-03 21:37:16 +03:00
func d3disconnectBranch(node *d3nodeT, index int) {
2016-10-03 03:28:23 +03:00
// Remove element by swapping with the last element to prevent gaps in array
node.branch[index] = node.branch[node.count-1]
node.branch[node.count-1].data = nil
node.branch[node.count-1].child = nil
node.count--
}
// Pick a branch. Pick the one that will need the smallest increase
// in area to accomodate the new rectangle. This will result in the
// least total area for the covering rectangles in the current node.
// In case of a tie, pick the one which was smaller before, to get
// the best resolution when searching.
2016-10-03 21:37:16 +03:00
func d3pickBranch(rect *d3rectT, node *d3nodeT) int {
2016-10-03 03:28:23 +03:00
var firstTime bool = true
var increase float
var bestIncr float = -1
var area float
var bestArea float
2016-10-03 03:28:23 +03:00
var best int
2016-10-03 21:37:16 +03:00
var tempRect d3rectT
2016-10-03 03:28:23 +03:00
for index := 0; index < node.count; index++ {
curRect := &node.branch[index].rect
2016-10-03 21:37:16 +03:00
area = d3calcRectVolume(curRect)
tempRect = d3combineRect(rect, curRect)
increase = d3calcRectVolume(&tempRect) - area
2016-10-03 03:28:23 +03:00
if (increase < bestIncr) || firstTime {
best = index
bestArea = area
bestIncr = increase
firstTime = false
} else if (increase == bestIncr) && (area < bestArea) {
best = index
bestArea = area
bestIncr = increase
}
}
return best
}
// Combine two rectangles into larger one containing both
2016-10-03 21:37:16 +03:00
func d3combineRect(rectA, rectB *d3rectT) d3rectT {
var newRect d3rectT
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
for index := 0; index < d3numDims; index++ {
newRect.min[index] = d3fmin(rectA.min[index], rectB.min[index])
newRect.max[index] = d3fmax(rectA.max[index], rectB.max[index])
2016-10-03 03:28:23 +03:00
}
return newRect
}
// Split a node.
// Divides the nodes branches and the extra one between two nodes.
// Old node is one of the new ones, and one really new one is created.
// Tries more than one method for choosing a partition, uses best result.
2016-10-03 21:37:16 +03:00
func d3splitNode(node *d3nodeT, branch *d3branchT, newNode **d3nodeT) {
2016-10-03 03:28:23 +03:00
// Could just use local here, but member or external is faster since it is reused
2016-10-03 21:37:16 +03:00
var localVars d3partitionVarsT
2016-10-03 03:28:23 +03:00
parVars := &localVars
// Load all the branches into a buffer, initialize old node
2016-10-03 21:37:16 +03:00
d3getBranches(node, branch, parVars)
2016-10-03 03:28:23 +03:00
// Find partition
2016-10-03 21:37:16 +03:00
d3choosePartition(parVars, d3minNodes)
2016-10-03 03:28:23 +03:00
// Create a new node to hold (about) half of the branches
2016-10-03 21:37:16 +03:00
*newNode = &d3nodeT{}
2016-10-03 03:28:23 +03:00
(*newNode).level = node.level
// Put branches from buffer into 2 nodes according to the chosen partition
node.count = 0
2016-10-03 21:37:16 +03:00
d3loadNodes(node, *newNode, parVars)
2016-10-03 03:28:23 +03:00
}
// Calculate the n-dimensional volume of a rectangle
func d3rectVolume(rect *d3rectT) float {
var volume float = 1
2016-10-03 21:37:16 +03:00
for index := 0; index < d3numDims; index++ {
2016-10-03 03:28:23 +03:00
volume *= rect.max[index] - rect.min[index]
}
return volume
}
2016-10-03 21:37:16 +03:00
// The exact volume of the bounding sphere for the given d3rectT
func d3rectSphericalVolume(rect *d3rectT) float64 {
2016-10-03 03:28:23 +03:00
var sumOfSquares float64 = 0
var radius float64
2016-10-03 21:37:16 +03:00
for index := 0; index < d3numDims; index++ {
halfExtent := float64(rect.max[index]-rect.min[index]) * 0.5
2016-10-03 03:28:23 +03:00
sumOfSquares += halfExtent * halfExtent
}
radius = math.Sqrt(sumOfSquares)
// Pow maybe slow, so test for common dims just use x*x, x*x*x.
switch d3numDims {
default:
2016-10-03 21:37:16 +03:00
return (math.Pow(radius, d3numDims) * d3unitSphereVolume)
case 2:
return (radius * radius * d3unitSphereVolume)
case 3:
return (radius * radius * radius * d3unitSphereVolume)
case 4:
return (radius * radius * radius * radius * d3unitSphereVolume)
case 5:
return (radius * radius * radius * radius * radius * d3unitSphereVolume)
2016-10-03 03:28:23 +03:00
}
}
// Use one of the methods to calculate retangle volume
func d3calcRectVolume(rect *d3rectT) float {
2016-10-03 21:37:16 +03:00
if d3useSphericalVolume {
return float(d3rectSphericalVolume(rect)) // Slower but helps certain merge cases
2016-10-03 03:28:23 +03:00
} else { // RTREE_USE_SPHERICAL_VOLUME
2016-10-03 21:37:16 +03:00
return d3rectVolume(rect) // Faster but can cause poor merges
2016-10-03 03:28:23 +03:00
} // RTREE_USE_SPHERICAL_VOLUME
}
// Load branch buffer with branches from full node plus the extra branch.
2016-10-03 21:37:16 +03:00
func d3getBranches(node *d3nodeT, branch *d3branchT, parVars *d3partitionVarsT) {
2016-10-03 03:28:23 +03:00
// Load the branch buffer
2016-10-03 21:37:16 +03:00
for index := 0; index < d3maxNodes; index++ {
2016-10-03 03:28:23 +03:00
parVars.branchBuf[index] = node.branch[index]
}
2016-10-03 21:37:16 +03:00
parVars.branchBuf[d3maxNodes] = *branch
parVars.branchCount = d3maxNodes + 1
2016-10-03 03:28:23 +03:00
// Calculate rect containing all in the set
parVars.coverSplit = parVars.branchBuf[0].rect
2016-10-03 21:37:16 +03:00
for index := 1; index < d3maxNodes+1; index++ {
parVars.coverSplit = d3combineRect(&parVars.coverSplit, &parVars.branchBuf[index].rect)
2016-10-03 03:28:23 +03:00
}
2016-10-03 21:37:16 +03:00
parVars.coverSplitArea = d3calcRectVolume(&parVars.coverSplit)
2016-10-03 03:28:23 +03:00
}
// Method #0 for choosing a partition:
// As the seeds for the two groups, pick the two rects that would waste the
// most area if covered by a single rectangle, i.e. evidently the worst pair
// to have in the same group.
// Of the remaining, one at a time is chosen to be put in one of the two groups.
// The one chosen is the one with the greatest difference in area expansion
// depending on which group - the rect most strongly attracted to one group
// and repelled from the other.
// If one group gets too full (more would force other group to violate min
// fill requirement) then other group gets the rest.
// These last are the ones that can go in either group most easily.
2016-10-03 21:37:16 +03:00
func d3choosePartition(parVars *d3partitionVarsT, minFill int) {
var biggestDiff float
2016-10-03 03:28:23 +03:00
var group, chosen, betterGroup int
2016-10-03 21:37:16 +03:00
d3initParVars(parVars, parVars.branchCount, minFill)
d3pickSeeds(parVars)
2016-10-03 03:28:23 +03:00
for ((parVars.count[0] + parVars.count[1]) < parVars.total) &&
(parVars.count[0] < (parVars.total - parVars.minFill)) &&
(parVars.count[1] < (parVars.total - parVars.minFill)) {
biggestDiff = -1
for index := 0; index < parVars.total; index++ {
2016-10-03 21:37:16 +03:00
if d3notTaken == parVars.partition[index] {
2016-10-03 03:28:23 +03:00
curRect := &parVars.branchBuf[index].rect
2016-10-03 21:37:16 +03:00
rect0 := d3combineRect(curRect, &parVars.cover[0])
rect1 := d3combineRect(curRect, &parVars.cover[1])
growth0 := d3calcRectVolume(&rect0) - parVars.area[0]
growth1 := d3calcRectVolume(&rect1) - parVars.area[1]
2016-10-03 03:28:23 +03:00
diff := growth1 - growth0
if diff >= 0 {
group = 0
} else {
group = 1
diff = -diff
}
if diff > biggestDiff {
biggestDiff = diff
chosen = index
betterGroup = group
} else if (diff == biggestDiff) && (parVars.count[group] < parVars.count[betterGroup]) {
chosen = index
betterGroup = group
}
}
}
2016-10-03 21:37:16 +03:00
d3classify(chosen, betterGroup, parVars)
2016-10-03 03:28:23 +03:00
}
// If one group too full, put remaining rects in the other
if (parVars.count[0] + parVars.count[1]) < parVars.total {
if parVars.count[0] >= parVars.total-parVars.minFill {
group = 1
} else {
group = 0
}
for index := 0; index < parVars.total; index++ {
2016-10-03 21:37:16 +03:00
if d3notTaken == parVars.partition[index] {
d3classify(index, group, parVars)
2016-10-03 03:28:23 +03:00
}
}
}
}
// Copy branches from the buffer into two nodes according to the partition.
2016-10-03 21:37:16 +03:00
func d3loadNodes(nodeA, nodeB *d3nodeT, parVars *d3partitionVarsT) {
2016-10-03 03:28:23 +03:00
for index := 0; index < parVars.total; index++ {
targetNodeIndex := parVars.partition[index]
2016-10-03 21:37:16 +03:00
targetNodes := []*d3nodeT{nodeA, nodeB}
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
// It is assured that d3addBranch here will not cause a node split.
d3addBranch(&parVars.branchBuf[index], targetNodes[targetNodeIndex], nil)
2016-10-03 03:28:23 +03:00
}
}
2016-10-03 21:37:16 +03:00
// Initialize a d3partitionVarsT structure.
func d3initParVars(parVars *d3partitionVarsT, maxRects, minFill int) {
2016-10-03 03:28:23 +03:00
parVars.count[0] = 0
parVars.count[1] = 0
parVars.area[0] = 0
parVars.area[1] = 0
parVars.total = maxRects
parVars.minFill = minFill
for index := 0; index < maxRects; index++ {
2016-10-03 21:37:16 +03:00
parVars.partition[index] = d3notTaken
2016-10-03 03:28:23 +03:00
}
}
2016-10-03 21:37:16 +03:00
func d3pickSeeds(parVars *d3partitionVarsT) {
2016-10-03 03:28:23 +03:00
var seed0, seed1 int
var worst, waste float
var area [d3maxNodes + 1]float
2016-10-03 03:28:23 +03:00
for index := 0; index < parVars.total; index++ {
2016-10-03 21:37:16 +03:00
area[index] = d3calcRectVolume(&parVars.branchBuf[index].rect)
2016-10-03 03:28:23 +03:00
}
worst = -parVars.coverSplitArea - 1
for indexA := 0; indexA < parVars.total-1; indexA++ {
for indexB := indexA + 1; indexB < parVars.total; indexB++ {
2016-10-03 21:37:16 +03:00
oneRect := d3combineRect(&parVars.branchBuf[indexA].rect, &parVars.branchBuf[indexB].rect)
waste = d3calcRectVolume(&oneRect) - area[indexA] - area[indexB]
2016-10-03 03:28:23 +03:00
if waste > worst {
worst = waste
seed0 = indexA
seed1 = indexB
}
}
}
2016-10-03 21:37:16 +03:00
d3classify(seed0, 0, parVars)
d3classify(seed1, 1, parVars)
2016-10-03 03:28:23 +03:00
}
// Put a branch in one of the groups.
2016-10-03 21:37:16 +03:00
func d3classify(index, group int, parVars *d3partitionVarsT) {
2016-10-03 03:28:23 +03:00
parVars.partition[index] = group
// Calculate combined rect
if parVars.count[group] == 0 {
parVars.cover[group] = parVars.branchBuf[index].rect
} else {
2016-10-03 21:37:16 +03:00
parVars.cover[group] = d3combineRect(&parVars.branchBuf[index].rect, &parVars.cover[group])
2016-10-03 03:28:23 +03:00
}
// Calculate volume of combined rect
2016-10-03 21:37:16 +03:00
parVars.area[group] = d3calcRectVolume(&parVars.cover[group])
2016-10-03 03:28:23 +03:00
parVars.count[group]++
}
// Delete a data rectangle from an index structure.
2016-10-03 21:37:16 +03:00
// Pass in a pointer to a d3rectT, the tid of the record, ptr to ptr to root node.
2016-10-03 03:28:23 +03:00
// Returns 1 if record not found, 0 if success.
2016-10-03 21:37:16 +03:00
// d3removeRect provides for eliminating the root.
func d3removeRect(rect *d3rectT, id interface{}, root **d3nodeT) bool {
var reInsertList *d3listNodeT
2016-10-03 03:28:23 +03:00
2016-10-03 21:37:16 +03:00
if !d3removeRectRec(rect, id, *root, &reInsertList) {
2016-10-03 03:28:23 +03:00
// Found and deleted a data item
// Reinsert any branches from eliminated nodes
for reInsertList != nil {
tempNode := reInsertList.node
for index := 0; index < tempNode.count; index++ {
// TODO go over this code. should I use (tempNode->m_level - 1)?
2016-10-03 21:37:16 +03:00
d3insertRect(&tempNode.branch[index], root, tempNode.level)
2016-10-03 03:28:23 +03:00
}
reInsertList = reInsertList.next
}
// Check for redundant root (not leaf, 1 child) and eliminate TODO replace
// if with while? In case there is a whole branch of redundant roots...
if (*root).count == 1 && (*root).isInternalNode() {
tempNode := (*root).branch[0].child
*root = tempNode
}
return false
} else {
return true
}
}
// Delete a rectangle from non-root part of an index structure.
2016-10-03 21:37:16 +03:00
// Called by d3removeRect. Descends tree recursively,
2016-10-03 03:28:23 +03:00
// merges branches on the way back up.
// Returns 1 if record not found, 0 if success.
2016-10-03 21:37:16 +03:00
func d3removeRectRec(rect *d3rectT, id interface{}, node *d3nodeT, listNode **d3listNodeT) bool {
2016-10-03 03:28:23 +03:00
if node.isInternalNode() { // not a leaf node
for index := 0; index < node.count; index++ {
2016-10-03 21:37:16 +03:00
if d3overlap(*rect, node.branch[index].rect) {
if !d3removeRectRec(rect, id, node.branch[index].child, listNode) {
if node.branch[index].child.count >= d3minNodes {
2016-10-03 03:28:23 +03:00
// child removed, just resize parent rect
2016-10-03 21:37:16 +03:00
node.branch[index].rect = d3nodeCover(node.branch[index].child)
2016-10-03 03:28:23 +03:00
} else {
// child removed, not enough entries in node, eliminate node
2016-10-03 21:37:16 +03:00
d3reInsert(node.branch[index].child, listNode)
d3disconnectBranch(node, index) // Must return after this call as count has changed
2016-10-03 03:28:23 +03:00
}
return false
}
}
}
return true
} else { // A leaf node
for index := 0; index < node.count; index++ {
if node.branch[index].data == id {
2016-10-03 21:37:16 +03:00
d3disconnectBranch(node, index) // Must return after this call as count has changed
2016-10-03 03:28:23 +03:00
return false
}
}
return true
}
}
2016-10-03 21:37:16 +03:00
// Decide whether two rectangles d3overlap.
func d3overlap(rectA, rectB d3rectT) bool {
for index := 0; index < d3numDims; index++ {
2016-10-03 03:28:23 +03:00
if rectA.min[index] > rectB.max[index] ||
rectB.min[index] > rectA.max[index] {
return false
}
}
return true
}
// Add a node to the reinsertion list. All its branches will later
// be reinserted into the index structure.
2016-10-03 21:37:16 +03:00
func d3reInsert(node *d3nodeT, listNode **d3listNodeT) {
newListNode := &d3listNodeT{}
2016-10-03 03:28:23 +03:00
newListNode.node = node
newListNode.next = *listNode
*listNode = newListNode
}
2016-10-03 21:37:16 +03:00
// d3search in an index tree or subtree for all data retangles that d3overlap the argument rectangle.
func d3search(node *d3nodeT, rect d3rectT, foundCount int, resultCallback func(data interface{}) bool) (int, bool) {
2016-10-03 03:28:23 +03:00
if node.isInternalNode() {
// This is an internal node in the tree
for index := 0; index < node.count; index++ {
2016-10-03 21:37:16 +03:00
if d3overlap(rect, node.branch[index].rect) {
2016-10-03 03:28:23 +03:00
var ok bool
2016-10-03 21:37:16 +03:00
foundCount, ok = d3search(node.branch[index].child, rect, foundCount, resultCallback)
2016-10-03 03:28:23 +03:00
if !ok {
// The callback indicated to stop searching
return foundCount, false
}
}
}
} else {
// This is a leaf node
for index := 0; index < node.count; index++ {
2016-10-03 21:37:16 +03:00
if d3overlap(rect, node.branch[index].rect) {
2016-10-03 03:28:23 +03:00
id := node.branch[index].data
foundCount++
if !resultCallback(id) {
return foundCount, false // Don't continue searching
}
}
}
}
return foundCount, true // Continue searching
}