Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | 428x 428x 428x 428x 650x 650x 650x 650x 650x 650x 650x 650x 208x 134982x 650x 650x 650x 650x 650x 428x 650x 650x 7408644x 7408644x 7408644x 7408644x 7408644x 7408644x 135084x 7273560x 7273560x 7273560x 19901790x 21024490x 21024490x 1057690x 18844100x 650x 13008896x 13008896x 650x 12898296x 12898296x 12898296x 12898296x 12898296x 270066x 270066x 12628230x 12628230x 12628230x 12628230x 12578842x 14470x 14470x 12564372x 12564372x 49388x 49388x 49388x 49388x 49388x 49388x 49388x 49388x 38252x 38252x 8244x 8244x 30008x 38252x 11136x 2258x 2258x 2258x 8878x 8878x 8878x 8878x 8878x | import type Point3 from '../types/Point3'; import type BoundsIJK from '../types/BoundsIJK'; import type { PixelDataTypedArray } from '../types'; /** * The RLERun specifies a contigous run of values for a row, * where all indices (i only) from `[start,end)` have the specified * value. */ export type RLERun<T> = { value: T; start: number; end: number; }; /** * Performs adjacent flood fill in all directions, for a true flood fill */ const ADJACENT_ALL = [ [0, -1, 0], [0, 1, 0], [0, 0, -1], [0, 0, 1], ]; const ADJACENT_SINGLE_PLANE = [ [0, -1, 0], [0, 1, 0], ]; /** * Adjacent in and out do a flood fill in only one of depth (in or out) directions. * That improves the performance, as well as looks much nicer for many flood operations. */ const ADJACENT_IN = [ [0, -1, 0], [0, 1, 0], [0, 0, -1], ]; const ADJACENT_OUT = [ [0, -1, 0], [0, 1, 0], [0, 0, 1], ]; /** * A type that has converts to and from an integer plane representation. */ export type PlaneNormalizer = { toIJK: (ijkPrime: Point3) => Point3; fromIJK: (ijk: Point3) => Point3; boundsIJKPrime: BoundsIJK; }; /** * RLE based implementation of a voxel map. * This can be used as single or multi-plane, as the underlying indexes are * mapped to rows and hte rows are indexed started at 0 and continuing * incrementing for all rows in the multi-plane voxel. */ export default class RLEVoxelMap<T> { public normalizer: PlaneNormalizer; /** * The rows for the voxel map is a map from the j index location (or for * volumes, `j + k*height`) to a list of RLE runs. That is, each entry in * the rows specifies the voxel data for a given row in the image. * Then, the RLE runs themselves specify the pixel values for given rows as * a pair of start/end indices, plus the value to apply. */ protected rows = new Map<number, RLERun<T>[]>(); /** The height of the images stored in the voxel map (eg the height of each plane) */ public height = 1; /** The width of the image planes */ public width = 1; /** * The number of image planes stored (the depth of the indices), with the k * index going from 0...depth. */ public depth = 1; /** * A multiplier value to go from j values to overall index values. */ protected jMultiple = 1; /** * A multiplier value to go from k values to overall index values. */ protected kMultiple = 1; /** Number of components in the value */ protected numComps = 1; /** * The default value returned for get. * This allows treating the voxel map more like scalar data, returning the right * default value for unset values. * Set to 0 by default, but any maps where 0 not in T should update this value. */ public defaultValue: T; /** * The constructor for creating pixel data. */ public pixelDataConstructor = Uint8Array; /** * Copies the data in source into the map. */ public static copyMap<T>( destination: RLEVoxelMap<T>, source: RLEVoxelMap<T> ) { for (const [index, row] of source.rows) { destination.rows.set(index, structuredClone(row)); } } constructor(width: number, height: number, depth = 1) { this.width = width; this.height = height; this.depth = depth; this.jMultiple = width; this.kMultiple = this.jMultiple * height; } /** * This is a function on the voxel manager, to get the RLE scalar data. * @returns an array of the given type for the data. */ public static getScalarData = function (ArrayType = Uint8ClampedArray) { const scalarData = new ArrayType(this.frameSize); this.map.updateScalarData(scalarData); return scalarData; }; /** * Update the scalar data with the current RLE state * @param scalarData - old scalar data to update */ public updateScalarData = function (scalarData: PixelDataTypedArray) { scalarData.fill(0); const callback = (index, rle, row) => { const { start, end, value } = rle; for (let i = start; i < end; i++) { scalarData[index + i] = value; } }; this.forEach(callback); }; /** * Gets the value encoded in the map at the given index, which is * an integer `[i,j,k]` voxel index, equal to `index=i+(j+k*height)*width` * value (eg a standard ScalarData index for stack/volume single component * indices.) * * Returns defaultValue if the RLE value is not found. */ public get = (index: number): T => { const i = index % this.jMultiple; const j = (index - i) / this.jMultiple; const rle = this.getRLE(i, j); return rle?.value ?? this.defaultValue; }; public toIJK(index: number): Point3 { const i = index % this.jMultiple; const j = ((index - i) / this.jMultiple) % this.height; const k = Math.floor(index / this.kMultiple); return [i, j, k]; } public toIndex([i, j, k]: Point3) { return i + k * this.kMultiple + j * this.jMultiple; } /** * Gets a list of RLERun values which specify the data on the row j * This allows applying or modifying the run directly. See CanvasActor * for an example in the RLE rendering. */ protected getRLE(i: number, j: number, k = 0): RLERun<T> { const row = this.rows.get(j + k * this.height); if (!row) { return; } const index = this.findIndex(row, i); const rle = row[index]; return i >= rle?.start ? rle : undefined; } /** * Indicate if the map has the given value */ public has(index: number): boolean { const i = index % this.jMultiple; const j = (index - i) / this.jMultiple; const rle = this.getRLE(i, j); return rle?.value !== undefined; } /** * Delete any value at the given index; */ public delete(index: number) { const i = index % this.width; const j = (index - i) / this.width; const row = this.rows.get(j); if (!row) { return; } const rleIndex = this.findIndex(row, i); const rle = row[rleIndex]; if (!rle || rle.start > i) { // Value not in RLE, so no need to delete return; } if (rle.end === i + 1) { // Value at end, so decrease the length. // This also handles hte case of the value at the beginning and deleting // the final value in the RLE rle.end--; if (rle.start >= rle.end) { // Last value in the RLE row.splice(rleIndex, 1); if (!row.length) { this.rows.delete(j); } } return; } if (rle.start === i) { // Not the only value, otherwise this is checked by the previous code rle.start++; return; } // Need to split the rle since the value occurs in the middle. const newRle = { value: rle.value, start: i + 1, end: rle.end, }; rle.end = i; row.splice(rleIndex + 1, 0, newRle); } /** * Finds the index in the row that i is contained in, OR that i would be * before. That is, the rle value for the returned index in that row * has `i ε [start,end)` if a direct RLE is found, or `i ε [end_-1,start)` if * in the prefix. If no RLE is found with that index, then * `i ε [end_final,length)` */ protected findIndex(row: RLERun<T>[], i: number) { for (let index = 0; index < row.length; index++) { const { end: iEnd } = row[index]; if (i < iEnd) { return index; } } return row.length; } /** * For each RLE element, call the given callback */ public forEach(callback, options?: { rowModified?: boolean }) { const rowModified = options?.rowModified; for (const [baseIndex, row] of this.rows) { const rowToUse = rowModified ? [...row] : row; for (const rle of rowToUse) { callback(baseIndex * this.width, rle, row); } } } /** * For each row, call the callback with the base index and the row data */ public forEachRow(callback) { for (const [baseIndex, row] of this.rows) { callback(baseIndex * this.width, row); } } /** * Gets the run for the given j,k indices. This is used to allow fast access * to runs for data for things like rendering entire rows of data. */ public getRun = (j: number, k: number) => { const runIndex = j + k * this.height; return this.rows.get(runIndex); }; /** * Adds to the RLE at the given position. This is unfortunately fairly * complex since it is desirable to minimize the number of runs, but to still * allow it to be efficient. */ public set = (index: number, value: T) => { Iif (value === undefined) { // Don't store undefined values return; } const i = index % this.width; const j = (index - i) / this.width; const row = this.rows.get(j); if (!row) { this.rows.set(j, [{ start: i, end: i + 1, value }]); return; } const rleIndex = this.findIndex(row, i); const rle1 = row[rleIndex]; const rle0 = row[rleIndex - 1]; // Adding to the end of the row if (!rle1) { // We are at the end, check if the previous rle can be extended if (!rle0 || rle0.value !== value || rle0.end !== i) { row[rleIndex] = { start: i, end: i + 1, value }; // validateRow(row, i, rleIndex, value); return; } // Just add it to the previous element. rle0.end++; return; } const { start, end, value: oldValue } = rle1; // Handle the already in place case Iif (value === oldValue && i >= start) { // validateRow(row, i, rleIndex, value, start); return; } const rleInsert = { start: i, end: i + 1, value }; const isAfter = i > start; const insertIndex = isAfter ? rleIndex + 1 : rleIndex; const rlePrev = isAfter ? rle1 : rle0; let rleNext = isAfter ? row[rleIndex + 1] : rle1; // Can merge with previous value, so no insert if (rlePrev?.value === value && rlePrev?.end === i) { rlePrev.end++; if (rleNext?.value === value && rleNext.start === i + 1) { rlePrev.end = rleNext.end; row.splice(rleIndex, 1); // validateRow(row, i, rleIndex, value); } else Iif (rleNext?.start === i) { rleNext.start++; if (rleNext.start === rleNext.end) { row.splice(rleIndex, 1); rleNext = row[rleIndex]; // Check if we can merge twice if (rleNext?.start === i + 1 && rleNext.value === value) { rlePrev.end = rleNext.end; row.splice(rleIndex, 1); } } // validateRow(row, i, rleIndex, value); } return; } // Can merge with next, so no insert if (rleNext?.value === value && rleNext.start === i + 1) { rleNext.start--; Iif (rlePrev?.end > i) { rlePrev.end = i; if (rlePrev.end === rlePrev.start) { row.splice(rleIndex, 1); } } // validateRow(row, i, rleIndex, value); return; } // Can't merge, need to see if we can replace Iif (rleNext?.start === i && rleNext.end === i + 1) { rleNext.value = value; const nextnext = row[rleIndex + 1]; if (nextnext?.start == i + 1 && nextnext.value === value) { row.splice(rleIndex + 1, 1); rleNext.end = nextnext.end; } // validateRow(row, i, rleIndex, value); return; } // Need to fix the next start value Iif (i === rleNext?.start) { rleNext.start++; } Iif (isAfter && end > i + 1) { // Insert two items, to split the existing into three row.splice(insertIndex, 0, rleInsert, { start: i + 1, end: rlePrev.end, value: rlePrev.value, }); } else { row.splice(insertIndex, 0, rleInsert); } Iif (rlePrev?.end > i) { rlePrev.end = i; } // validateRow(row, i, rleIndex, value, insertIndex); }; /** * Clears all entries. */ public clear() { this.rows.clear(); } /** * Gets the set of key entries - that is j values. This may include * `j>=height`, where `j = key % height`, and `k = Math.floor(j / height)` */ public keys(): number[] { return [...this.rows.keys()]; } /** * Gets the pixel data into the provided pixel data array, or creates one * according to the assigned type. */ public getPixelData( k = 0, pixelData?: PixelDataTypedArray ): PixelDataTypedArray { if (!pixelData) { pixelData = new this.pixelDataConstructor( this.width * this.height * this.numComps ); } else { pixelData.fill(0); } const { width, height, numComps } = this; for (let j = 0; j < height; j++) { const row = this.getRun(j, k); if (!row) { continue; } if (numComps === 1) { for (const rle of row) { const rowOffset = j * width; const { start, end, value } = rle; for (let i = start; i < end; i++) { pixelData[rowOffset + i] = value as unknown as number; } } } else { for (const rle of row) { const rowOffset = j * width * numComps; const { start, end, value } = rle; for (let i = start; i < end; i += numComps) { for (let comp = 0; comp < numComps; comp++) { pixelData[rowOffset + i + comp] = value[comp]; } } } } } return pixelData; } /** * Performs a flood fill on the RLE values at the given position, replacing * the current value with the new value (which must be different) * Note that this is, by default, a planar fill, which will fill each plane * given the starting point, in a true flood fill fashion, but then not * re-fill the given plane. * * @param i,j,k - starting point to fill from, as integer indices into * the voxel volume. These are converted internally to RLE indices * @param value - to replace the existing value with. Must be different from * the starting value. * @param options - to control the flood. * * planar means to flood the current k plane entirely, and then use the * points from the current plane as seed points in the k+1 and k-1 planes, * but not returning to the current plane * * singlePlane is just a single k plane, not filling any other planes * * diagonals means to use the diagonally adjacent points. */ public floodFill( i: number, j: number, k: number, value: T, options?: { planar?: boolean; diagonals?: boolean; singlePlane?: boolean } ): number { const rle = this.getRLE(i, j, k); if (!rle) { throw new Error(`Initial point ${i},${j},${k} isn't in the RLE`); } const stack = [[rle, j, k]]; const replaceValue = rle.value; if (replaceValue === value) { throw new Error( `source (${replaceValue}) and destination (${value}) are identical` ); } return this.flood(stack, replaceValue, value, options); } /** * Performs a flood fill on the stack. * * @param stack - list of points/rle runs to try filling * @param sourceValue - the value that is being replaced in the flood * @param value - the destination value for the flood * @param options - see floodFill */ private flood(stack, sourceValue, value, options) { let sum = 0; const { planar = true, diagonals = true, singlePlane = false, } = options || {}; const childOptions = { planar, diagonals, singlePlane }; while (stack.length) { const top = stack.pop(); const [current] = top; if (current.value !== sourceValue) { continue; } current.value = value; sum += current.end - current.start; const adjacents = this.findAdjacents(top, childOptions).filter( (adjacent) => adjacent && adjacent[0].value === sourceValue ); stack.push(...adjacents); } return sum; } /** * Fills an RLE from a given getter result, skipping undefined values only. * @param getter - a function taking i,j,k values (indices) and returning the new * value at the given point. * @param boundsIJK - a set of boundary values to flood up to and including both values. */ public fillFrom( getter: (i: number, j: number, k: number) => T, boundsIJK: BoundsIJK ) { for (let k = boundsIJK[2][0]; k <= boundsIJK[2][1]; k++) { for (let j = boundsIJK[1][0]; j <= boundsIJK[1][1]; j++) { let rle; let row; for (let i = boundsIJK[0][0]; i <= boundsIJK[0][1]; i++) { const value = getter(i, j, k); if (value === undefined) { rle = undefined; continue; } if (!row) { row = []; this.rows.set(j + k * this.height, row); } if (rle && rle.value !== value) { rle = undefined; } if (!rle) { rle = { start: i, end: i, value }; row.push(rle); } rle.end++; } } } } /** * Finds adjacent RLE runs, in all directions. * The planar value (true by default) does plane at a time fills. * @param item - an RLE being sepecified to find adjacent values for * @param options - see floodFill */ public findAdjacents( item: [RLERun<T>, number, number, Point3[]?], { diagonals = true, planar = true, singlePlane = false } ) { const [rle, j, k, adjacentsDelta] = item; const { start, end } = rle; const leftRle = start > 0 && this.getRLE(start - 1, j, k); const rightRle = end < this.width && this.getRLE(end, j, k); const range = diagonals ? [start > 0 ? start - 1 : start, end < this.width ? end + 1 : end] : [start, end]; const adjacents = []; if (leftRle) { adjacents.push([leftRle, j, k]); } if (rightRle) { adjacents.push([rightRle, j, k]); } for (const delta of adjacentsDelta || (singlePlane ? ADJACENT_SINGLE_PLANE : ADJACENT_ALL)) { const [, delta1, delta2] = delta; const testJ = delta1 + j; const testK = delta2 + k; if (testJ < 0 || testJ >= this.height) { continue; } if (testK < 0 || testK >= this.depth) { continue; } const row = this.getRun(testJ, testK); if (!row) { continue; } for (const testRle of row) { const newAdjacentDelta = adjacentsDelta || (singlePlane && ADJACENT_SINGLE_PLANE) || (planar && delta2 > 0 && ADJACENT_OUT) || (planar && delta2 < 0 && ADJACENT_IN) || ADJACENT_ALL; if (!(testRle.end <= range[0] || testRle.start >= range[1])) { adjacents.push([testRle, testJ, testK, newAdjacentDelta]); } } } return adjacents; } } // This is some code to allow debugging RLE maps // To be deleted along with references once RLE is better tested. // Might move to testing code at that point // function validateRow(row, ...inputs) { // if (!row) { // return; // } // let lastRle; // for (const rle of row) { // const { start, end, value } = rle; // if (start < 0 || end > 1920 || start >= end) { // console.log('Wrong order', ...inputs); // } // if (!lastRle) { // lastRle = rle; // continue; // } // const { start: lastStart, end: lastEnd, value: lastValue } = lastRle; // lastRle = rle; // if (start < lastEnd) { // console.log('inputs for wrong overlap', ...inputs); // } // if (start === lastEnd && value === lastValue) { // console.log('inputs for two in a row same', ...inputs); // } // } // } |