1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Copyright ©2024 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package curve
import (
"errors"
"fmt"
)
// ErrUnderflow is returned by Hilbert curve constructors when the power is less
// than 1.
var ErrUnderflow = errors.New("order is less than 1")
// ErrOverflow is returned (wrapped) by Hilbert curve constructors when the
// power would cause Len and Pos to overflow.
var ErrOverflow = errors.New("overflow int")
// The size of an int. Taken from src/math/const.go.
const intSize = 32 << (^uint(0) >> 63) // 32 or 64
// Hilbert2D is a 2-dimensional Hilbert curve.
type Hilbert2D struct{ order int }
// NewHilbert2D constructs a [Hilbert2D] of the given order. NewHilbert2D
// returns [ErrOverflow] (wrapped) if the order would cause Len and Pos to
// overflow.
func NewHilbert2D(order int) (Hilbert2D, error) {
v := Hilbert2D{order: order}
// The order must be greater than zero.
if order < 1 {
return v, ErrUnderflow
}
// The product of the order and number of dimensions must not exceed or
// equal the size of an int.
if order*2 >= intSize {
return v, fmt.Errorf("a 2-dimensional, %d-order Hilbert curve will %w", order, ErrOverflow)
}
return v, nil
}
// Dims returns the spatial dimensions of the curve, which is {2ᵏ, 2ᵏ}, where k
// is the order.
func (h Hilbert2D) Dims() []int { return []int{1 << h.order, 1 << h.order} }
// Len returns the length of the curve, which is 2ⁿᵏ, where n is the dimension
// (2) and k is the order.
//
// Len will overflow if order is ≥ 16 on architectures where [int] is 32-bits,
// or ≥ 32 on architectures where [int] is 64-bits.
func (h Hilbert2D) Len() int { return 1 << (2 * h.order) }
func (h Hilbert2D) rot(n int, v []int, d int) {
switch d {
case 0:
swap{0, 1}.do(n, v)
case 3:
flip{0, 1}.do(n, v)
}
}
// Pos returns the linear position of the 3-spatial coordinate v along the
// curve. Pos modifies v.
//
// Pos will overflow if order is ≥ 16 on architectures where [int] is 32-bits,
// or ≥ 32 on architectures where [int] is 64-bits.
func (h Hilbert2D) Pos(v []int) int {
var d int
const dims = 2
for n := h.order - 1; n >= 0; n-- {
rx := (v[0] >> n) & 1
ry := (v[1] >> n) & 1
rd := ry<<1 | (ry ^ rx)
d += rd << (dims * n)
h.rot(h.order, v, rd)
}
return d
}
// Coord writes the spatial coordinates of pos to dst and returns it. If dst is
// nil, Coord allocates a new slice of length 2; otherwise Coord is
// allocation-free.
//
// Coord panics if dst is not nil and len(dst) is not equal to 2.
func (h Hilbert2D) Coord(dst []int, pos int) []int {
if dst == nil {
dst = make([]int, 2)
} else if len(dst) != 2 {
panic("len(dst) must equal 2")
}
for n := 0; n < h.order; n++ {
e := pos & 3
h.rot(n, dst[:], e)
ry := e >> 1
rx := (e>>0 ^ e>>1) & 1
dst[0] += rx << n
dst[1] += ry << n
pos >>= 2
}
return dst
}
// Hilbert3D is a 3-dimensional Hilbert curve.
type Hilbert3D struct{ order int }
// NewHilbert3D constructs a [Hilbert3D] of the given order. NewHilbert3D
// returns [ErrOverflow] (wrapped) if the order would cause Len and Pos to
// overflow.
func NewHilbert3D(order int) (Hilbert3D, error) {
v := Hilbert3D{order: order}
// The order must be greater than zero.
if order < 1 {
return v, ErrUnderflow
}
// The product of the order and number of dimensions must not exceed or
// equal the size of an int.
if order*3 >= intSize {
return v, fmt.Errorf("a 3-dimensional, %d-order Hilbert curve will %w", order, ErrOverflow)
}
return v, nil
}
// Dims returns the spatial dimensions of the curve, which is {2ᵏ, 2ᵏ, 2ᵏ}, where
// k is the order.
func (h Hilbert3D) Dims() []int { return []int{1 << h.order, 1 << h.order, 1 << h.order} }
// Len returns the length of the curve, which is 2ⁿᵏ, where n is the dimension
// (3) and k is the order.
//
// Len will overflow if order is ≥ 11 on architectures where [int] is 32-bits,
// or ≥ 21 on architectures where [int] is 64-bits.
func (h Hilbert3D) Len() int { return 1 << (3 * h.order) }
func (h Hilbert3D) rot(reverse bool, n int, v []int, d int) {
switch d {
case 0:
do2(reverse, n, v, swap{1, 2}, swap{0, 2})
case 1, 2:
do2(reverse, n, v, swap{0, 2}, swap{1, 2})
case 3, 4:
invert{0, 1}.do(n, v)
case 5, 6:
do2(reverse, n, v, flip{0, 2}, flip{1, 2})
case 7:
do2(reverse, n, v, flip{1, 2}, flip{0, 2})
}
}
// Pos returns the linear position of the 4-spatial coordinate v along the
// curve. Pos modifies v.
//
// Pos will overflow if order is ≥ 11 on architectures where [int] is 32-bits,
// or ≥ 21 on architectures where [int] is 64-bits.
func (h Hilbert3D) Pos(v []int) int {
var d int
const dims = 3
for n := h.order - 1; n >= 0; n-- {
rx := (v[0] >> n) & 1
ry := (v[1] >> n) & 1
rz := (v[2] >> n) & 1
rd := rz<<2 | (rz^ry)<<1 | (rz ^ ry ^ rx)
d += rd << (dims * n)
h.rot(false, h.order, v, rd)
}
return d
}
// Coord writes the spatial coordinates of pos to dst and returns it. If dst is
// nil, Coord allocates a new slice of length 3; otherwise Coord is
// allocation-free.
//
// Coord panics if dst is not nil and len(dst) is not equal to 3.
func (h Hilbert3D) Coord(dst []int, pos int) []int {
if dst == nil {
dst = make([]int, 3)
} else if len(dst) != 3 {
panic("len(dst) must equal 3")
}
for n := 0; n < h.order; n++ {
e := pos & 7
h.rot(true, n, dst[:], e)
rz := e >> 2
ry := (e>>1 ^ e>>2) & 1
rx := (e>>0 ^ e>>1) & 1
dst[0] += rx << n
dst[1] += ry << n
dst[2] += rz << n
pos >>= 3
}
return dst
}
// Hilbert4D is a 4-dimensional Hilbert curve.
type Hilbert4D struct{ order int }
// NewHilbert4D constructs a [Hilbert4D] of the given order. NewHilbert4D
// returns [ErrOverflow] (wrapped) if the order would cause Len and Pos to
// overflow.
func NewHilbert4D(order int) (Hilbert4D, error) {
v := Hilbert4D{order: order}
// The order must be greater than zero.
if order < 1 {
return v, ErrUnderflow
}
// The product of the order and number of dimensions must not exceed or
// equal the size of an int.
if order*4 >= intSize {
return v, fmt.Errorf("a 4-dimensional, %d-order Hilbert curve will %w", order, ErrOverflow)
}
return v, nil
}
// Dims returns the spatial dimensions of the curve, which is {2ᵏ, 2ᵏ, 2ᵏ, 2ᵏ},
// where k is the order.
func (h Hilbert4D) Dims() []int { return []int{1 << h.order, 1 << h.order, 1 << h.order, 1 << h.order} }
// Len returns the length of the curve, which is 2ⁿᵏ, where n is the dimension
// (4) and k is the order.
//
// Len will overflow if order is ≥ 8 on architectures where [int] is 32-bits, or
// ≥ 16 on architectures where [int] is 64-bits.
func (h Hilbert4D) Len() int { return 1 << (4 * h.order) }
func (h Hilbert4D) rot(reverse bool, n int, v []int, d int) {
switch d {
case 0:
do2(reverse, n, v, swap{1, 3}, swap{0, 3})
case 1, 2:
do2(reverse, n, v, swap{0, 3}, swap{1, 3})
case 3, 4:
do2(reverse, n, v, flip{0, 1}, swap{2, 3})
case 5, 6:
do2(reverse, n, v, flip{1, 2}, swap{2, 3})
case 7, 8:
invert{0, 2}.do(n, v)
case 9, 10:
do2(reverse, n, v, flip{1, 2}, flip{2, 3})
case 11, 12:
do2(reverse, n, v, flip{0, 1}, flip{2, 3})
case 13, 14:
do2(reverse, n, v, flip{0, 3}, flip{1, 3})
case 15:
do2(reverse, n, v, flip{1, 3}, flip{0, 3})
}
}
// Pos returns the linear position of the 2-spatial coordinate v along the
// curve. Pos modifies v.
//
// Pos will overflow if order is ≥ 8 on architectures where [int] is 32-bits, or
// ≥ 16 on architectures where [int] is 64-bits.
func (h Hilbert4D) Pos(v []int) int {
var d int
const dims = 4
for n := h.order - 1; n >= 0; n-- {
var e int
for i := dims - 1; i >= 0; i-- {
v := v[i] >> n & 1
e = e<<1 | (e^v)&1
}
d += e << (dims * n)
h.rot(false, h.order, v, e)
}
return d
}
// Coord writes the spatial coordinates of pos to dst and returns it. If dst is
// nil, Coord allocates a new slice of length 4; otherwise Coord is
// allocation-free.
//
// Coord panics if dst is not nil and len(dst) is not equal to 4.
func (h Hilbert4D) Coord(dst []int, pos int) []int {
if dst == nil {
dst = make([]int, 4)
} else if len(dst) != 4 {
panic("len(dst) must equal 4")
}
N := 4
for n := 0; n < h.order; n++ {
e := pos & (1<<N - 1)
h.rot(true, n, dst[:], e)
for i, e := 0, e; i < N; i++ {
dst[i] += (e ^ e>>1) & 1 << n
e >>= 1
}
pos >>= N
}
return dst
}
type op interface{ do(int, []int) }
// invert I and J
type invert struct{ i, j int }
func (c invert) do(n int, v []int) { v[c.i], v[c.j] = v[c.i]^(1<<n-1), v[c.j]^(1<<n-1) }
// swap I and J
type swap struct{ i, j int }
func (c swap) do(n int, v []int) { v[c.i], v[c.j] = v[c.j], v[c.i] }
// swap and invert I and J
type flip struct{ i, j int }
func (c flip) do(n int, v []int) { v[c.i], v[c.j] = v[c.j]^(1<<n-1), v[c.i]^(1<<n-1) }
// do2 executes the given operations, optionally in reverse.
//
// Generic specialization reduces allocation (because it can eliminate interface
// value boxing) and improves performance
func do2[A, B op](reverse bool, n int, v []int, a A, b B) {
if reverse {
b.do(n, v)
a.do(n, v)
} else {
a.do(n, v)
b.do(n, v)
}
}
|