Browse Source

bump spatialindex2d

main
forest 3 months ago
parent
commit
5cbac1a58a
4 changed files with 59 additions and 16 deletions
  1. +1
    -1
      go.mod
  2. +2
    -0
      go.sum
  3. +42
    -15
      main.go
  4. +14
    -0
      static/app.css

+ 1
- 1
go.mod View File

@ -3,7 +3,7 @@ module git.sequentialread.com/forest/graffiti
go 1.16
require (
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624191456-0f858ffdca6c
git.sequentialread.com/forest/pkg-errors v0.9.2
github.com/boltdb/bolt v1.3.1
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646


+ 2
- 0
go.sum View File

@ -2,6 +2,8 @@ git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210620162508-4bef09
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210620162508-4bef09fabeb3/go.mod h1:qQXFuJiQIgQIWQ8/viqhsFeoeeFOttqGAF/O5HD/lo4=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390 h1:KjnRL7U1LwnCW9H5WkVukMivibkRG24ZXze3rS9/VpA=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390/go.mod h1:qQXFuJiQIgQIWQ8/viqhsFeoeeFOttqGAF/O5HD/lo4=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624191456-0f858ffdca6c h1:WO/vmOABFwTEfyBm9zQNDjyNvq2l0FWxKyOcgG7O1XI=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624191456-0f858ffdca6c/go.mod h1:qQXFuJiQIgQIWQ8/viqhsFeoeeFOttqGAF/O5HD/lo4=
git.sequentialread.com/forest/pkg-errors v0.9.2 h1:j6pwbL6E+TmE7TD0tqRtGwuoCbCfO6ZR26Nv5nest9g=
git.sequentialread.com/forest/pkg-errors v0.9.2/go.mod h1:8TkJ/f8xLWFIAid20aoqgDZcCj9QQt+FU+rk415XO1w=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=


+ 42
- 15
main.go View File

@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
@ -19,7 +20,7 @@ import (
"text/template"
"time"
spatialIndex "git.sequentialread.com/forest/modular-spatial-index"
spatial "git.sequentialread.com/forest/modular-spatial-index"
errors "git.sequentialread.com/forest/pkg-errors"
"github.com/boltdb/bolt"
base58 "github.com/shengdoushi/base58"
@ -94,9 +95,16 @@ var listenPort = 8080
var indexTemplate *template.Template
var indexCSSBytes []byte
var spatialIndex *spatial.SpatialIndex2D
// the spatial index has a worst-case (about 3x slower) performance near 0,0.
// so we simply shift the universe over a bit so the home page lands in a more performant area.
var sillyHilbertOptimizationOffset = 300000
// so we simply shift the universe over by 25 units so the home page query lands in a more performant area.
var sillyHilbertOptimizationOffset = 50
// we scale the pixel-based world down by this much before storing in the spatial index,
// so that we don't run out of space on our 32 bit spatial index which is only 32766 units wide
var spatialIndexUnitSizeInPixels = float64(64)
var pixelsToSpatialIndexUnits = float64(1) / spatialIndexUnitSizeInPixels
// TODO set these to default to false for prod
// create xyz.bpg.png files (decoded from bpg) so it's easier to view them and compare the quality, etc.
@ -108,6 +116,14 @@ var debugLog = true
func main() {
// sane defaults for golang runtime 🤮
debug.SetPanicOnFault(true)
var err error
// Force 32 bit index so it will always be compatible no matter what CPU arch.
// normally you would do spatial.NewSpatialIndex2D(bits.UintSize))
spatialIndex, err = spatial.NewSpatialIndex2D(32)
if err != nil {
log.Fatalf("can't start because can't create spatial index: %+v", err)
}
envPath := os.Getenv("BPGENC_PATH")
if envPath != "" {
@ -120,7 +136,6 @@ func main() {
debugLog = true
}
var err error
db, err = bolt.Open("graffiti-bolt.db", 0600, nil)
if err != nil {
log.Fatalf("can't start because can't open/create database file graffiti-bolt.db: %s", err)
@ -488,7 +503,9 @@ func setupRoutes(mux *http.ServeMux) {
func createPost(post *Post) error {
return db.Update(func(tx *bolt.Tx) error {
spatialKey, err := spatialIndex.GetIndexedPoint(post.X+sillyHilbertOptimizationOffset, post.Y+sillyHilbertOptimizationOffset)
spatialX := int(math.Round(float64(post.X)*pixelsToSpatialIndexUnits)) + sillyHilbertOptimizationOffset
spatialY := int(math.Round(float64(post.Y)*pixelsToSpatialIndexUnits)) + sillyHilbertOptimizationOffset
spatialKey, err := spatialIndex.GetIndexedPoint(spatialX, spatialY)
if err != nil {
return err
}
@ -525,17 +542,27 @@ func createPost(post *Post) error {
}
func getPosts(x, y, radius int) ([][]byte, error) {
xMin := x - radius
yMin := y - radius
xMax := x + (radius * 2)
yMax := y + (radius * 2)
ranges, err := spatialIndex.RectangleToIndexedRanges(xMin+sillyHilbertOptimizationOffset, yMin+sillyHilbertOptimizationOffset, radius*2, radius*2, 1)
spatialXMin := int(math.Floor(float64(x-radius)*pixelsToSpatialIndexUnits)) + sillyHilbertOptimizationOffset
spatialYMin := int(math.Floor(float64(x-radius)*pixelsToSpatialIndexUnits)) + sillyHilbertOptimizationOffset
spatialWidth := int(math.Ceil(float64(radius*2) * pixelsToSpatialIndexUnits))
spatialHeight := int(math.Ceil(float64(radius*2) * pixelsToSpatialIndexUnits))
spatialXMax := spatialXMin + spatialWidth
spatialYMax := spatialYMin + spatialHeight
if debugLog {
log.Printf("getPosts(): RectangleToIndexedRanges(%d, %d, %d, %d, 1)\n", spatialXMin, spatialYMin, spatialWidth, spatialHeight)
}
ranges, err := spatialIndex.RectangleToIndexedRanges(spatialXMin, spatialYMin, spatialWidth, spatialHeight, 1)
if err != nil {
return nil, err
}
// for _, rng := range ranges {
// log.Printf("\n\n%x\n%x\n\n", rng.Start[0:8], rng.End[0:8])
// }
if debugLog {
log.Printf("getPosts(): spatialIndex range count: %d\n---\n", len(ranges))
for _, rng := range ranges {
fmt.Printf("%x\n%x\n---\n", rng.Start[0:8], rng.End[0:8])
}
}
toReturn := [][]byte{}
@ -546,11 +573,11 @@ func getPosts(x, y, radius int) ([][]byte, error) {
cursor := bucket.Cursor()
key, value := cursor.Seek(rng.Start)
for key != nil && bytes.Compare(key, rng.End) < 1 {
postX, postY, err := spatialIndex.GetPositionFromIndexedPoint(key)
spatialX, spatialY, err := spatialIndex.GetPositionFromIndexedPoint(key)
if err != nil {
return err
}
if postX > xMin && postX < xMax && postY > yMin && postY < yMax {
if spatialX > spatialXMin && spatialX < spatialXMax && spatialY > spatialYMin && spatialY < spatialYMax {
toReturn = append(toReturn, value)
}
key, value = cursor.Next()


+ 14
- 0
static/app.css View File

@ -373,6 +373,20 @@ textarea {
pointer-events: all;
}
/* Webkit sucks at rendering border-images with stretched sides */
/* you get 1px glitches around the corners when css transform is used. */
/* So these solid color borders stretch out underneath the border-image and mostly hide the blemishes */
.polaroid-container canvas,
.polaroid-container img {
border-top: 23px solid white;
box-sizing: content-box;
margin-top: -23px;
border-bottom: 100px solid #c9ceda;
border-left: 23px solid #d5d8e5;
margin-left: -23px;
border-right: 19px solid #d5d8e5;
}
.post-container.no-polaroid.legal {
background-color: #00ff00;
border-radius: 1rem;


Loading…
Cancel
Save