2017-02-26 13:13:19 +01:00
|
|
|
// Copyright 2014, 2015, 2017 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
2017-02-26 13:13:19 +01:00
|
|
|
|
2024-01-07 03:51:27 +01:00
|
|
|
type area struct {
|
2017-02-26 13:13:19 +01:00
|
|
|
X1, Z1 int16
|
|
|
|
X2, Z2 int16
|
|
|
|
}
|
|
|
|
|
2024-01-07 03:51:27 +01:00
|
|
|
func (a area) contains(x, z int16) bool {
|
2017-02-26 13:13:19 +01:00
|
|
|
return x >= a.X1 && x <= a.X2 && z >= a.Z1 && z <= a.Z2
|
|
|
|
}
|
|
|
|
|
2024-01-07 03:51:27 +01:00
|
|
|
func (a area) higher() bool {
|
2017-02-26 13:13:19 +01:00
|
|
|
return a.Z2-a.Z1 > a.X2-a.X1
|
|
|
|
}
|
|
|
|
|
2024-01-07 03:51:27 +01:00
|
|
|
func areasContain(areas []area, x, z int16) bool {
|
2017-02-26 13:13:19 +01:00
|
|
|
for _, r := range areas {
|
|
|
|
if r.contains(x, z) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
// recalculate implements a greedy algorithm to figure out
|
2017-02-26 13:13:19 +01:00
|
|
|
// a list of disjunct areas of free regions in the domain
|
|
|
|
// to the (x, z) block plane.
|
|
|
|
// oldAreas are searched and found free areas are appended
|
|
|
|
// to newAreas which ist return.
|
|
|
|
// This is useful to spatial query only blocks from db
|
|
|
|
// that are not below already rendered blocks.
|
2024-01-07 03:51:27 +01:00
|
|
|
func (a area) recalculate(r *Renderer, nareas []area) []area {
|
2017-02-26 13:13:19 +01:00
|
|
|
yM := r.yMin
|
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
const ex = 1
|
|
|
|
const ez = 2
|
|
|
|
|
|
|
|
nas := len(nareas)
|
|
|
|
|
2024-01-06 16:12:19 +01:00
|
|
|
for z := a.Z1; z <= a.Z2; z++ {
|
2017-02-26 17:01:15 +01:00
|
|
|
row := z * int16(r.width)
|
2024-01-06 16:12:19 +01:00
|
|
|
for x := a.X1; x <= a.X2; x++ {
|
2017-02-26 17:01:15 +01:00
|
|
|
// Uncovered and not in list of new areas?
|
|
|
|
if yM[row+x] > math.MinInt32 || areasContain(nareas[nas:], x, z) {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-07 03:51:27 +01:00
|
|
|
ar := area{X1: x, Z1: z, X2: x, Z2: z}
|
2017-02-26 17:01:15 +01:00
|
|
|
// Try to extend the area in x and/or z till no further extension is possible.
|
|
|
|
ext:
|
|
|
|
for extend := ex | ez; extend != 0; {
|
|
|
|
// If we extending in both directions a the current area
|
|
|
|
// is higher than wide we gain more block if extend
|
|
|
|
// in the x direction first.
|
2024-01-06 16:12:19 +01:00
|
|
|
if (extend == ex|ez && ar.higher()) || extend&ex == ex { // check x
|
|
|
|
nx := ar.X2 + 1
|
|
|
|
if nx > a.X2 { // reached border of area
|
2017-02-26 17:01:15 +01:00
|
|
|
extend &= ^ex
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check column right of the current area if its fully free.
|
2024-01-06 16:12:19 +01:00
|
|
|
for nz := ar.Z1; nz <= ar.Z2; nz++ {
|
2017-02-26 17:01:15 +01:00
|
|
|
if yM[nz*int16(r.width)+nx] > math.MinInt32 ||
|
|
|
|
areasContain(nareas[nas:], nx, nz) {
|
|
|
|
extend &= ^ex
|
|
|
|
continue ext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// free -> extend
|
2024-01-06 16:12:19 +01:00
|
|
|
ar.X2 = nx
|
2017-02-26 17:01:15 +01:00
|
|
|
} else if extend&ez == ez { // check z
|
2024-01-06 16:12:19 +01:00
|
|
|
nz := ar.Z2 + 1
|
|
|
|
if nz > a.Z2 {
|
2017-02-26 17:01:15 +01:00
|
|
|
extend &= ^ez
|
|
|
|
continue
|
2017-02-26 13:13:19 +01:00
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
// Check line right below the current area if its free.
|
|
|
|
row2 := nz * int16(r.width)
|
2024-01-06 16:12:19 +01:00
|
|
|
for nx := ar.X1; nx <= ar.X2; nx++ {
|
2017-02-26 17:01:15 +01:00
|
|
|
if yM[row2+nx] > math.MinInt32 ||
|
|
|
|
areasContain(nareas[nas:], nx, nz) {
|
|
|
|
extend &= ^ez
|
|
|
|
continue ext
|
2017-02-26 13:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
// free -> extend
|
2024-01-06 16:12:19 +01:00
|
|
|
ar.Z2 = nz
|
2017-02-26 13:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
// At this point the area is extended to max.
|
2024-01-06 16:12:19 +01:00
|
|
|
nareas = append(nareas, ar)
|
2017-02-26 13:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
return nareas
|
2017-02-26 13:13:19 +01:00
|
|
|
}
|