| 1 |
{ |
| 2 |
Copyright 2003-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Generate space-filling curves (TSwapScanCurve, THilbertCurve, TPeanoCurve). |
| 17 |
These are sequences of points that completely fill some 2D space. } |
| 18 |
unit CastleInternalSpaceFillingCurves; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses SysUtils, CastleVectors; |
| 25 |
|
| 26 |
type |
| 27 |
{ Angle for space-filling curves. |
| 28 |
|
| 29 |
Let's say 0 = 0 degrees, 1 = 90 degrees, 2 = 180, 3 = 270, |
| 30 |
but actually it doesn't matter much --- it all a matter of convention here. } |
| 31 |
TSFCAngle = 0..3; |
| 32 |
|
| 33 |
TSFCStepFunction = procedure (Angle: TSFCAngle; StepFuncData: Pointer); |
| 34 |
|
| 35 |
{ Low-level procedures to generate consecutive Peano and Hilbert curve points. |
| 36 |
See e.g. "Graphic Gems II" (gem I.8) or |
| 37 |
http://en.wikipedia.org/wiki/Space-filling_curve for nice description. |
| 38 |
|
| 39 |
Step is a function that goes to a neighbor 2D point, and "marks" it |
| 40 |
(whatever it means for your usage). If the starting point is "marked" |
| 41 |
at the beginning, using Peano or Hilbert curve guarantees that you |
| 42 |
will eventually "mark" the whole 2D space. |
| 43 |
|
| 44 |
Peano curve fills a plane size 3^Level, Hilbert curve fills size 2^Level. |
| 45 |
|
| 46 |
Angle is an initial orientation, determining in which direction |
| 47 |
the curve will be drawn. If you want to fill the space up and to the left |
| 48 |
from the initial point (according to conventions that |
| 49 |
Angle = 0 is right, Angle = 1 is up, and so on (CCW)), then use: |
| 50 |
|
| 51 |
@unorderedList( |
| 52 |
@itemSpacing Compact |
| 53 |
@item(InitialOrient = false and Angle = 0 for PeanoCurve.) |
| 54 |
@item(InitialOrient = true and Angle = 0 for HilbertCurve.) |
| 55 |
) |
| 56 |
|
| 57 |
@groupBegin |
| 58 |
} |
| 59 |
procedure PeanoCurve(InitialOrient: boolean; Angle: TSFCAngle; InitialLevel: Cardinal; |
| 60 |
Step: TSFCStepFunction; StepData: Pointer); |
| 61 |
procedure HilbertCurve(InitialOrient: boolean; Angle: TSFCAngle; InitialLevel: Cardinal; |
| 62 |
Step: TSFCStepFunction; StepData: Pointer); |
| 63 |
{ @groupEnd } |
| 64 |
|
| 65 |
type |
| 66 |
{ Base abstract space-filling curve class. |
| 67 |
|
| 68 |
Generates consecutive points by NextPixel, until EndOfPixels = @true, |
| 69 |
filling the 2D space in [0..SizeX-1, 0..SizeY-1]. |
| 70 |
To process (exactly once) each point of your 2D space, you can use |
| 71 |
something like |
| 72 |
|
| 73 |
@longCode(# while not SFCurve.EndOfPixels do DoSomethingOnPixel(SFCurve.NextPixel); #) |
| 74 |
|
| 75 |
We try to make NextPixel and EndOfPixels work instantly fast, |
| 76 |
preferably making some preprocessing at construction time. } |
| 77 |
TSpaceFillingCurve = class |
| 78 |
private |
| 79 |
FSizeX, FSizeY, FPixelsCount: Cardinal; |
| 80 |
public |
| 81 |
{ Size of the 2D space filled by space-filling curve. |
| 82 |
It's OK even if they are 0 (then EndOfPixels = @true instantly). |
| 83 |
@groupBegin } |
| 84 |
property SizeX: Cardinal read FSizeX; |
| 85 |
property SizeY: Cardinal read FSizeY; |
| 86 |
{ @groupEnd } |
| 87 |
|
| 88 |
{ Shortcut for SizeX * SizeY. } |
| 89 |
property PixelsCount: Cardinal read FPixelsCount; |
| 90 |
|
| 91 |
constructor Create(ASizeX, ASizeY: Cardinal); virtual; |
| 92 |
|
| 93 |
function EndOfPixels: boolean; virtual; abstract; |
| 94 |
|
| 95 |
{ Get next point. Do not ever call this when EndOfPixels = @true. } |
| 96 |
function NextPixel: TVector2Cardinal; virtual; abstract; |
| 97 |
|
| 98 |
{ Skip next SkipCount curve points. Just like you would call |
| 99 |
NextPixel SkipCount times, ignoring the result. Although may |
| 100 |
be implemented much faster, so don't worry about calling with |
| 101 |
large SkipCount values. |
| 102 |
|
| 103 |
Do not ever try to skip beyond the end of points. |
| 104 |
That is, do not use @code(SkipCount > PixelsCount - PixelsDone). |
| 105 |
For example, do not use SkipCount > 0 if currently EndOfPixels - @true. } |
| 106 |
procedure SkipPixels(SkipCount: Cardinal); virtual; abstract; |
| 107 |
|
| 108 |
{ Start generating points back from the beginning. } |
| 109 |
procedure Reset; virtual; abstract; |
| 110 |
|
| 111 |
{ How many curve points were generated. Number of generated |
| 112 |
points (by NextPixel or skipped by SkipPixels), |
| 113 |
since the last @link(Reset) or constructor. } |
| 114 |
function PixelsDone: Cardinal; virtual; abstract; |
| 115 |
|
| 116 |
{ Nice curve name, like 'swapscan', 'hilbert' or 'peano'. } |
| 117 |
class function SFCName: string; virtual; abstract; |
| 118 |
end; |
| 119 |
|
| 120 |
TSpaceFillingCurveClass = class of TSpaceFillingCurve; |
| 121 |
|
| 122 |
{ Abstract space-filling curve class, helping implementing |
| 123 |
curves that precalculate points at construction. |
| 124 |
In descendants, you only need to override GeneratePoints. } |
| 125 |
TPrecalcCurve = class(TSpaceFillingCurve) |
| 126 |
private |
| 127 |
Pixels: PVector2CardinalArray; |
| 128 |
NextPixelNum: Cardinal; |
| 129 |
protected |
| 130 |
{ Generate next PixelsCount points. You should generate next |
| 131 |
PixelsCount points to the Pixels table |
| 132 |
(it's guaranteed that PixelsCount > 0). } |
| 133 |
procedure GeneratePixels(APixels: PVector2CardinalArray); virtual; abstract; |
| 134 |
public |
| 135 |
constructor Create(ASizeX, ASizeY: Cardinal); override; |
| 136 |
destructor Destroy; override; |
| 137 |
function EndOfPixels: boolean; override; |
| 138 |
function NextPixel: TVector2Cardinal; override; |
| 139 |
procedure SkipPixels(SkipCount: Cardinal); override; |
| 140 |
procedure Reset; override; |
| 141 |
function PixelsDone: Cardinal; override; |
| 142 |
end; |
| 143 |
|
| 144 |
{ Simple space-filling curve that goes row by row, swapping direction. |
| 145 |
This is a trivial space-filling curve, at least it goes by neighboring |
| 146 |
points. We go from the bottom to the top, in even rows we go the right, |
| 147 |
in odd rows we go to the left. } |
| 148 |
TSwapScanCurve = class(TPrecalcCurve) |
| 149 |
protected |
| 150 |
procedure GeneratePixels(APixels: PVector2CardinalArray); override; |
| 151 |
public |
| 152 |
class function SFCName: string; override; |
| 153 |
end; |
| 154 |
|
| 155 |
{ Space-filling Hilbert curve. |
| 156 |
See e.g. [http://en.wikipedia.org/wiki/Space-filling_curve]. |
| 157 |
It's cut off to fill nicely the rectangular |
| 158 |
[0..SizeX-1, 0..SizeY-1] space. } |
| 159 |
THilbertCurve = class(TPrecalcCurve) |
| 160 |
protected |
| 161 |
procedure GeneratePixels(APixels: PVector2CardinalArray); override; |
| 162 |
public |
| 163 |
class function SFCName: string; override; |
| 164 |
end; |
| 165 |
|
| 166 |
{ Space-filling Peano curve. |
| 167 |
See e.g. [http://en.wikipedia.org/wiki/Space-filling_curve]. |
| 168 |
It's cut off to fill nicely the rectangular |
| 169 |
[0..SizeX-1, 0..SizeY-1] space. } |
| 170 |
TPeanoCurve = class(TPrecalcCurve) |
| 171 |
protected |
| 172 |
procedure GeneratePixels(APixels: PVector2CardinalArray); override; |
| 173 |
public |
| 174 |
class function SFCName: string; override; |
| 175 |
end; |
| 176 |
|
| 177 |
const |
| 178 |
{ Available space-filling curves (non-abstract TSpaceFillingCurve descendants). } |
| 179 |
AvailableSFCurveClasses: array[0..2]of TSpaceFillingCurveClass= |
| 180 |
(TSwapScanCurve, THilbertCurve, TPeanoCurve); |
| 181 |
|
| 182 |
type |
| 183 |
EInvalidSFCurveClassName = class(Exception); |
| 184 |
|
| 185 |
{ For curve name (matching some TSpaceFillingCurve.SFCName), |
| 186 |
return appropriate class. Not case-sensitive. |
| 187 |
@raises EInvalidSFCurveClassName For unknown curve names. } |
| 188 |
function StrToSFCurveClass(const s: string): TSpaceFillingCurveClass; |
| 189 |
|
| 190 |
{ All non-abstract space-filling curve names. |
| 191 |
Separated by commas, in apostrophes. } |
| 192 |
function AllSFCurveClassesNames: string; |
| 193 |
|
| 194 |
implementation |
| 195 |
|
| 196 |
uses Math, |
| 197 |
CastleUtils; |
| 198 |
|
| 199 |
const |
| 200 |
{ AngleTurn[Angle, Orient] = (definicja) |
| 201 |
if Orient then |
| 202 |
result := ChangeIntCycle(Angle, 1, 3) else |
| 203 |
result := ChangeIntCycle(Angle, -1, 3); |
| 204 |
Uzywanie tablicy juz przeliczonych wartosci da nam tutaj maly zysk czasowy. } |
| 205 |
AngleTurn: array[TSFCAngle, boolean]of TSFCAngle = |
| 206 |
( |
| 207 |
{Angle = 0} (3, 1), |
| 208 |
{Angle = 1} (0, 2), |
| 209 |
{Angle = 2} (1, 3), |
| 210 |
{Angle = 3} (2, 0) |
| 211 |
); |
| 212 |
|
| 213 |
procedure PeanoCurve(InitialOrient: boolean; Angle: TSFCAngle; InitialLevel: Cardinal; |
| 214 |
Step: TSFCStepFunction; StepData: Pointer); |
| 215 |
{ na podstawie "Graphic Gems II", gem I.8. |
| 216 |
Zmienna Angle jest globalna z punktu widzenia kolejnych rekurencyjnych |
| 217 |
wywolan Peano(), one wszystkie modyfikuja po prostu zadeklarowany powyzej |
| 218 |
parametr Angle. } |
| 219 |
|
| 220 |
procedure Peano(Orient: boolean; Level: Cardinal); |
| 221 |
begin |
| 222 |
if Level = 0 then Exit; |
| 223 |
Dec(Level); |
| 224 |
|
| 225 |
Peano(Orient, Level); |
| 226 |
Step(Angle, StepData); |
| 227 |
Peano(not Orient, Level); |
| 228 |
Step(Angle, StepData); |
| 229 |
Peano(Orient, Level); |
| 230 |
Angle := AngleTurn[Angle, not Orient]; |
| 231 |
Step(Angle, StepData); |
| 232 |
Angle := AngleTurn[Angle, not Orient]; |
| 233 |
Peano(not Orient, Level); |
| 234 |
Step(Angle, StepData); |
| 235 |
Peano(Orient, Level); |
| 236 |
Step(Angle, StepData); |
| 237 |
Peano(not Orient, Level); |
| 238 |
Angle := AngleTurn[Angle, Orient]; |
| 239 |
Step(Angle, StepData); |
| 240 |
Angle := AngleTurn[Angle, Orient]; |
| 241 |
Peano(Orient, Level); |
| 242 |
Step(Angle, StepData); |
| 243 |
Peano(not Orient, Level); |
| 244 |
Step(Angle, StepData); |
| 245 |
Peano(Orient, Level); |
| 246 |
end; |
| 247 |
|
| 248 |
begin |
| 249 |
Peano(InitialOrient, InitialLevel); |
| 250 |
end; |
| 251 |
|
| 252 |
procedure HilbertCurve(InitialOrient: boolean; Angle: TSFCAngle; InitialLevel: Cardinal; |
| 253 |
Step: TSFCStepFunction; StepData: Pointer); |
| 254 |
{ na podstawie "Graphic Gems II", gem I.8 } |
| 255 |
|
| 256 |
procedure Hilbert(Orient: boolean; Level: Cardinal); |
| 257 |
begin |
| 258 |
if Level = 0 then Exit; |
| 259 |
Dec(Level); |
| 260 |
|
| 261 |
Angle := AngleTurn[Angle, Orient]; |
| 262 |
Hilbert(not Orient, Level); |
| 263 |
Step(Angle, StepData); |
| 264 |
Angle := AngleTurn[Angle, not Orient]; |
| 265 |
Hilbert(Orient, Level); |
| 266 |
Step(Angle, StepData); |
| 267 |
Hilbert(Orient, Level); |
| 268 |
Angle := AngleTurn[Angle, not Orient]; |
| 269 |
Step(Angle, StepData); |
| 270 |
Hilbert(not Orient, Level); |
| 271 |
Angle := AngleTurn[Angle, Orient]; |
| 272 |
end; |
| 273 |
|
| 274 |
begin |
| 275 |
Hilbert(InitialOrient, InitialLevel); |
| 276 |
end; |
| 277 |
|
| 278 |
{ TSpaceFillingCurve ------------------------------------------------------------ } |
| 279 |
|
| 280 |
constructor TSpaceFillingCurve.Create(ASizeX, ASizeY: Cardinal); |
| 281 |
begin |
| 282 |
inherited Create; |
| 283 |
FSizeX := ASizeX; |
| 284 |
FSizeY := ASizeY; |
| 285 |
FPixelsCount := SizeX*SizeY; |
| 286 |
end; |
| 287 |
|
| 288 |
{ TPrecalcCurve ------------------------------------------------------------ } |
| 289 |
|
| 290 |
constructor TPrecalcCurve.Create(ASizeX, ASizeY: Cardinal); |
| 291 |
begin |
| 292 |
inherited; |
| 293 |
if PixelsCount <> 0 then |
| 294 |
begin |
| 295 |
Pixels := GetMem(SizeOf(TVector2Cardinal) * PixelsCount); |
| 296 |
GeneratePixels(Pixels); |
| 297 |
end; |
| 298 |
Reset; |
| 299 |
end; |
| 300 |
|
| 301 |
destructor TPrecalcCurve.Destroy; |
| 302 |
begin |
| 303 |
FreeMemNiling(Pointer(Pixels)); |
| 304 |
end; |
| 305 |
|
| 306 |
function TPrecalcCurve.EndOfPixels: boolean; |
| 307 |
begin |
| 308 |
result := NextPixelNum >= PixelsCount; |
| 309 |
end; |
| 310 |
|
| 311 |
function TPrecalcCurve.NextPixel: TVector2Cardinal; |
| 312 |
begin |
| 313 |
Assert(not EndOfPixels); |
| 314 |
result := Pixels^[NextPixelNum]; |
| 315 |
Inc(NextPixelNum); |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TPrecalcCurve.SkipPixels(SkipCount: Cardinal); |
| 319 |
begin |
| 320 |
Assert(SkipCount <= PixelsCount-PixelsDone); |
| 321 |
NextPixelNum := NextPixelNum + SkipCount; |
| 322 |
end; |
| 323 |
|
| 324 |
procedure TPrecalcCurve.Reset; |
| 325 |
begin |
| 326 |
NextPixelNum := 0; |
| 327 |
end; |
| 328 |
|
| 329 |
function TPrecalcCurve.PixelsDone: Cardinal; |
| 330 |
begin |
| 331 |
result := NextPixelNum; |
| 332 |
end; |
| 333 |
|
| 334 |
{ TSwapScanCurve ------------------------------------------------------------ } |
| 335 |
|
| 336 |
procedure TSwapScanCurve.GeneratePixels(APixels: PVector2CardinalArray); |
| 337 |
var |
| 338 |
NextPixelToWriteNum: Cardinal; |
| 339 |
|
| 340 |
procedure AppendPixel(x, y: Cardinal); |
| 341 |
begin |
| 342 |
APixels^[NextPixelToWriteNum].X := x; |
| 343 |
APixels^[NextPixelToWriteNum].Y := y; |
| 344 |
Inc(NextPixelToWriteNum); |
| 345 |
end; |
| 346 |
|
| 347 |
var |
| 348 |
x, y: Cardinal; |
| 349 |
begin |
| 350 |
NextPixelToWriteNum := 0; |
| 351 |
|
| 352 |
for y := 0 to SizeY-1 do |
| 353 |
begin |
| 354 |
if Odd(y) then |
| 355 |
for x := SizeX-1 downto 0 do AppendPixel(x, y) |
| 356 |
else |
| 357 |
for x := 0 to SizeX-1 do AppendPixel(x, y); |
| 358 |
end; |
| 359 |
end; |
| 360 |
|
| 361 |
class function TSwapScanCurve.SFCName: string; |
| 362 |
begin |
| 363 |
result := 'swapscan'; |
| 364 |
end; |
| 365 |
|
| 366 |
{ pomoce do THilbertCurve i TPeanoCurve ---------------------------------------- } |
| 367 |
|
| 368 |
type |
| 369 |
TStepData = record |
| 370 |
LastX, LastY, NextPixelToWriteNum, SizeX, SizeY: Cardinal; |
| 371 |
Pixels: PVector2CardinalArray; |
| 372 |
end; |
| 373 |
PStepData=^TStepData; |
| 374 |
|
| 375 |
procedure InitStepData(out StepData: TStepData; Pixels: PVector2CardinalArray; |
| 376 |
const SizeX, SizeY: Cardinal); |
| 377 |
begin |
| 378 |
FillChar(Pixels^[0], SizeOf(Pixels^[0]), 0); |
| 379 |
StepData.Pixels := Pixels; |
| 380 |
StepData.NextPixelToWriteNum := 1; |
| 381 |
StepData.LastX := 0; |
| 382 |
StepData.LastY := 0; |
| 383 |
StepData.SizeX := SizeX; |
| 384 |
StepData.SizeY := SizeY; |
| 385 |
end; |
| 386 |
|
| 387 |
procedure HilbertPeanoStep(Angle: TSFCAngle; RawData: Pointer); |
| 388 |
{ callback dla TSFCStepFunction dla Hilbert lub PeanoCurve. |
| 389 |
Zapisze Pixels^[NextPixelToWriteNum], uaktualniajac tez |
| 390 |
LastX, LastY, NextPixelToWriteNum. Bedzie obcinal zapisywane do Pixels |
| 391 |
elementy do 0..SizeX-1, 0..SizeY-1. } |
| 392 |
var Data: PStepData absolute RawData; |
| 393 |
begin |
| 394 |
with Data^ do |
| 395 |
begin |
| 396 |
case Angle of |
| 397 |
0: Inc(LastX); |
| 398 |
1: Inc(LastY); |
| 399 |
2: Dec(LastX); |
| 400 |
3: Dec(LastY); |
| 401 |
end; |
| 402 |
|
| 403 |
if Between(LastX, 0, SizeX-1) and Between(LastY, 0, SizeY-1) then |
| 404 |
begin |
| 405 |
Pixels^[NextPixelToWriteNum].X := LastX; |
| 406 |
Pixels^[NextPixelToWriteNum].Y := LastY; |
| 407 |
Inc(NextPixelToWriteNum); |
| 408 |
end; |
| 409 |
end; |
| 410 |
end; |
| 411 |
|
| 412 |
{ THilbertCurve ------------------------------------------------------------ } |
| 413 |
|
| 414 |
procedure THilbertCurve.GeneratePixels(APixels: PVector2CardinalArray); |
| 415 |
var StepData: TStepData; |
| 416 |
Level: Cardinal; |
| 417 |
begin |
| 418 |
Level := Smallest2Exponent(Max(SizeX, SizeY)); |
| 419 |
Assert((Level = 0) or (NatNatPower(2, Level-1) < Max(SizeX, SizeY))); |
| 420 |
|
| 421 |
InitStepData(StepData, APixels, SizeX, SizeY); |
| 422 |
HilbertCurve(true, 0, Level, |
| 423 |
{$ifdef FPC} @ {$endif} HilbertPeanoStep, @StepData); |
| 424 |
end; |
| 425 |
|
| 426 |
class function THilbertCurve.SFCName: string; |
| 427 |
begin |
| 428 |
result := 'hilbert'; |
| 429 |
end; |
| 430 |
|
| 431 |
{ TPeanoCurve ------------------------------------------------------------ } |
| 432 |
|
| 433 |
procedure TPeanoCurve.GeneratePixels(APixels: PVector2CardinalArray); |
| 434 |
var StepData: TStepData; |
| 435 |
MaxSize, Power3Level, Level: Cardinal; |
| 436 |
begin |
| 437 |
{ calculate Level, pomagajac sobie MaxSize i Power3Level } |
| 438 |
MaxSize := Max(SizeX, SizeY); |
| 439 |
Level := 0; |
| 440 |
Power3Level := 1; { = zawsze 3^Level } |
| 441 |
while Power3Level < MaxSize do |
| 442 |
begin |
| 443 |
Inc(Level); |
| 444 |
Power3Level := Power3Level * 3 |
| 445 |
end; |
| 446 |
Assert((Level = 0) or (NatNatPower(3, Level-1) < Max(SizeX, SizeY))); |
| 447 |
|
| 448 |
InitStepData(StepData, APixels, SizeX, SizeY); |
| 449 |
PeanoCurve(false, 0, Level, |
| 450 |
{$ifdef FPC} @ {$endif} HilbertPeanoStep, @StepData); |
| 451 |
end; |
| 452 |
|
| 453 |
class function TPeanoCurve.SFCName: string; |
| 454 |
begin |
| 455 |
result := 'peano'; |
| 456 |
end; |
| 457 |
|
| 458 |
{ operacje na SFCName -------------------------------------------------------- } |
| 459 |
|
| 460 |
function StrToSFCurveClass(const s: string): TSpaceFillingCurveClass; |
| 461 |
var i: Integer; |
| 462 |
begin |
| 463 |
for i := 0 to High(AvailableSFCurveClasses) do |
| 464 |
if AnsiSameText(s, AvailableSFCurveClasses[i].SFCName) then |
| 465 |
Exit(AvailableSFCurveClasses[i]); |
| 466 |
raise EInvalidSFCurveClassName.Create('Invalid space filling curve name : "'+ |
| 467 |
s+'", allowed names are '+AllSFCurveClassesNames+'.'); |
| 468 |
end; |
| 469 |
|
| 470 |
function AllSFCurveClassesNames: string; |
| 471 |
var |
| 472 |
i: Integer; |
| 473 |
begin |
| 474 |
result := '"'+AvailableSFCurveClasses[0].SFCName+'"'; |
| 475 |
for i := 1 to High(AvailableSFCurveClasses) do |
| 476 |
Result := Result + ', "'+AvailableSFCurveClasses[i].SFCName+'"'; |
| 477 |
end; |
| 478 |
|
| 479 |
end. |