-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add overlaps to SpatialID and SpatialIDDetector
- Loading branch information
Showing
3 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package spatialID | ||
|
||
import ( | ||
radixtree "github.com/trajectoryjp/multidimensional-radix-tree/src/tree" | ||
) | ||
|
||
type SpatialIDDetector interface { | ||
IsOverlap(ids SpatialIDs) bool | ||
} | ||
|
||
type SpatialIDGreedyDetector struct { | ||
ids SpatialIDs | ||
} | ||
|
||
func NewSpatialIDGreedyDetector(ids SpatialIDs) SpatialIDDetector { | ||
return &SpatialIDGreedyDetector{ids} | ||
} | ||
|
||
func (detector *SpatialIDGreedyDetector) IsOverlap(targetIDs SpatialIDs) bool { | ||
return detector.ids.Overlaps(targetIDs) | ||
} | ||
|
||
type SpatialIDTreeDetector struct { | ||
positiveTree radixtree.TreeInterface | ||
negativeTree radixtree.TreeInterface | ||
} | ||
|
||
func NewSpatialIDTreeDetector(ids SpatialIDs) SpatialIDDetector { | ||
var positiveTree radixtree.TreeInterface | ||
var negativeTree radixtree.TreeInterface | ||
for _, id := range ids { | ||
if id.GetF() >= 0 { | ||
if positiveTree == nil { | ||
positiveTree = radixtree.CreateTree(radixtree.Create3DTable()) | ||
} | ||
treeIndex := radixtree.Indexs{id.GetF(), id.GetX(), id.GetY()} | ||
positiveTree.Append(treeIndex, radixtree.ZoomSetLevel(id.GetZ()), struct{}{}) | ||
} else { | ||
if negativeTree == nil { | ||
negativeTree = radixtree.CreateTree(radixtree.Create3DTable()) | ||
} | ||
treeIndex := radixtree.Indexs{^id.GetF(), id.GetX(), id.GetY()} | ||
negativeTree.Append(treeIndex, radixtree.ZoomSetLevel(id.GetZ()), struct{}{}) | ||
} | ||
} | ||
return &SpatialIDTreeDetector{positiveTree, negativeTree} | ||
} | ||
|
||
func (tree *SpatialIDTreeDetector) IsOverlap(targetIds SpatialIDs) bool { | ||
for _, id := range targetIds { | ||
var isOverlap bool | ||
if id.GetF() >= 0 { | ||
if tree.positiveTree != nil { | ||
treeIndex := radixtree.Indexs{id.GetF(), id.GetX(), id.GetY()} | ||
isOverlap = tree.positiveTree.IsOverlap(treeIndex, radixtree.ZoomSetLevel(id.GetZ())) | ||
} | ||
} else { | ||
if tree.negativeTree != nil { | ||
treeIndex := radixtree.Indexs{^id.GetF(), id.GetX(), id.GetY()} | ||
isOverlap = tree.negativeTree.IsOverlap(treeIndex, radixtree.ZoomSetLevel(id.GetZ())) | ||
} | ||
} | ||
if isOverlap { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package spatialID | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSpatialIDDetector(t *testing.T) { | ||
testCases := []struct { | ||
spatialIDs SpatialIDs | ||
targetSpatialIDs SpatialIDs | ||
expectedResult bool | ||
}{ | ||
// ズームレベル最小、インデックス最小値/最大値 | ||
{ | ||
spatialIDs: SpatialIDs{{0, 0, 0, 0}}, | ||
targetSpatialIDs: SpatialIDs{{0, 0, 0, 0}}, | ||
expectedResult: true, | ||
}, | ||
// ズームレベル最大、インデックス最小値 | ||
{ | ||
spatialIDs: SpatialIDs{{MaxZ, 0, 0, 0}}, | ||
targetSpatialIDs: SpatialIDs{{MaxZ, 0, 0, 0}}, | ||
expectedResult: true, | ||
}, | ||
// ズームレベル最大、インデックス最大値 | ||
{ | ||
spatialIDs: SpatialIDs{{MaxZ, int64(1)<<MaxZ - 1, int64(1)<<MaxZ - 1, int64(1)<<MaxZ - 1}}, | ||
targetSpatialIDs: SpatialIDs{{MaxZ, int64(1)<<MaxZ - 1, int64(1)<<MaxZ - 1, int64(1)<<MaxZ - 1}}, | ||
expectedResult: true, | ||
}, | ||
// ズームレベル違い | ||
{ | ||
spatialIDs: SpatialIDs{{22, 0, 0, 0}}, | ||
targetSpatialIDs: SpatialIDs{{23, 1, 1, 1}}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
spatialIDs: SpatialIDs{{24, 2, 2, 2}}, | ||
targetSpatialIDs: SpatialIDs{{23, 1, 1, 1}}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
spatialIDs: SpatialIDs{{24, 3, 3, 3}}, | ||
targetSpatialIDs: SpatialIDs{{23, 1, 1, 1}}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
spatialIDs: SpatialIDs{{22, 1, 1, 1}, {23, 0, 0, 0}, {23, 2, 2, 2}, {24, 0, 0, 0}, {24, 1, 1, 1}}, | ||
targetSpatialIDs: SpatialIDs{{23, 1, 1, 1}}, | ||
expectedResult: false, | ||
}, | ||
} | ||
|
||
for testCaseIndex, testCase := range testCases { | ||
for swapIndex := range 2 { | ||
for detectorIndex, newSpatialIDDetector := range []func(SpatialIDs) SpatialIDDetector{ | ||
NewSpatialIDGreedyDetector, | ||
NewSpatialIDTreeDetector, | ||
} { | ||
spatialIDDetector := newSpatialIDDetector(testCase.spatialIDs) | ||
actualResult := spatialIDDetector.IsOverlap(testCase.targetSpatialIDs) | ||
assert.Equal( | ||
t, | ||
testCase.expectedResult, | ||
actualResult, | ||
fmt.Sprintf("testCaseIndex: %d, swapIndex: %d, detectorIndex: %d", testCaseIndex, swapIndex, detectorIndex), | ||
) | ||
} | ||
testCase.spatialIDs, testCase.targetSpatialIDs = testCase.targetSpatialIDs, testCase.spatialIDs | ||
} | ||
} | ||
} |