Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vanruesc committed Aug 30, 2021
1 parent d4b76b2 commit 9c0770e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/core/Octree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function cull(node: Node, region: Frustum | Box3, result: Node[]): void {
* @param result - A list to be filled with the identified octants.
*/

function findNodesByLevel(node: Node, level: number, depth: number, result: Node[]): void {
function findNodesByLevel(node: Node, level: number, depth: number,
result: Node[]): void {

const children = node.children;

Expand Down
10 changes: 5 additions & 5 deletions src/core/OctreeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ export class OctreeHelper extends Group {
});

// Create geometry in multiple runs to limit the amount of vertices.
for(let i = 0, length = 0, n = Math.ceil(octantCount / maxOctants); n > 0; --n) {
for(let i = 0, l = 0, n = Math.ceil(octantCount / maxOctants); n > 0; --n) {

length += (octantCount < maxOctants) ? octantCount : maxOctants;
l += (octantCount < maxOctants) ? octantCount : maxOctants;
octantCount -= maxOctants;

const vertexCount = length * 8;
const vertexCount = l * 8;
const indices = new Uint16Array(vertexCount * 3);
const positions = new Float32Array(vertexCount * 3);

// Continue where the previous run left off.
for(let c = 0, d = 0, result = iterator.next(); !result.done && i < length;) {
for(let c = 0, d = 0, result = iterator.next(); !result.done && i < l;) {

const octant = result.value as Node;
const min = octant.min;
Expand All @@ -92,7 +92,7 @@ export class OctreeHelper extends Group {

}

if(++i < length) {
if(++i < l) {

result = iterator.next();

Expand Down
3 changes: 2 additions & 1 deletion src/points/PointOctree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,8 @@ export class PointOctree<T> extends Octree {
* @return A list of points.
*/

findPoints(point: Vector3, radius: number, skipSelf = false): PointContainer<T>[] {
findPoints(point: Vector3, radius: number,
skipSelf = false): PointContainer<T>[] {

const result: PointContainer<T>[] = [];
findPoints(point, radius, skipSelf, this.root as PointOctant<T>, result);
Expand Down
26 changes: 13 additions & 13 deletions src/raycasting/OctreeRaycaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ const flags = new RaycastingFlags();
* @param tx1 - A ray projection parameter.
* @param ty1 - A ray projection parameter.
* @param tz1 - A ray projection parameter.
* @param intersects - An array to be filled with the intersecting octants.
* @param result - An array to be filled with the intersecting octants.
*/

function raycastOctant(node: Node, tx0: number, ty0: number, tz0: number,
tx1: number, ty1: number, tz1: number, intersects: Node[]): void {
tx1: number, ty1: number, tz1: number, result: Node[]): void {

if(tx1 >= 0.0 && ty1 >= 0.0 && tz1 >= 0.0) {

const children = node.children;
const c = node.children;

if(children === null) {
if(c === null) {

// Leaf.
intersects.push(node);
result.push(node);

} else {

Expand All @@ -56,42 +56,42 @@ function raycastOctant(node: Node, tx0: number, ty0: number, tz0: number,
switch(currentOctant) {

case 0:
raycastOctant(children[f], tx0, ty0, tz0, txm, tym, tzm, intersects);
raycastOctant(c[f], tx0, ty0, tz0, txm, tym, tzm, result);
currentOctant = findNextOctant(currentOctant, txm, tym, tzm);
break;

case 1:
raycastOctant(children[f ^ 1], tx0, ty0, tzm, txm, tym, tz1, intersects);
raycastOctant(c[f ^ 1], tx0, ty0, tzm, txm, tym, tz1, result);
currentOctant = findNextOctant(currentOctant, txm, tym, tz1);
break;

case 2:
raycastOctant(children[f ^ 2], tx0, tym, tz0, txm, ty1, tzm, intersects);
raycastOctant(c[f ^ 2], tx0, tym, tz0, txm, ty1, tzm, result);
currentOctant = findNextOctant(currentOctant, txm, ty1, tzm);
break;

case 3:
raycastOctant(children[f ^ 3], tx0, tym, tzm, txm, ty1, tz1, intersects);
raycastOctant(c[f ^ 3], tx0, tym, tzm, txm, ty1, tz1, result);
currentOctant = findNextOctant(currentOctant, txm, ty1, tz1);
break;

case 4:
raycastOctant(children[f ^ 4], txm, ty0, tz0, tx1, tym, tzm, intersects);
raycastOctant(c[f ^ 4], txm, ty0, tz0, tx1, tym, tzm, result);
currentOctant = findNextOctant(currentOctant, tx1, tym, tzm);
break;

case 5:
raycastOctant(children[f ^ 5], txm, ty0, tzm, tx1, tym, tz1, intersects);
raycastOctant(c[f ^ 5], txm, ty0, tzm, tx1, tym, tz1, result);
currentOctant = findNextOctant(currentOctant, tx1, tym, tz1);
break;

case 6:
raycastOctant(children[f ^ 6], txm, tym, tz0, tx1, ty1, tzm, intersects);
raycastOctant(c[f ^ 6], txm, tym, tz0, tx1, ty1, tzm, result);
currentOctant = findNextOctant(currentOctant, tx1, ty1, tzm);
break;

case 7:
raycastOctant(children[f ^ 7], txm, tym, tzm, tx1, ty1, tz1, intersects);
raycastOctant(c[f ^ 7], txm, tym, tzm, tx1, ty1, tz1, result);
// Far top right octant. No other octants can be reached from here.
currentOctant = 8;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/raycasting/findNextOctant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const octantTable = [
* @return The index of the next octant that the ray travels through.
*/

export function findNextOctant(currentOctant: number, tx1: number, ty1: number, tz1: number): number {
export function findNextOctant(currentOctant: number, tx1: number, ty1: number,
tz1: number): number {

let min: number;
let exit: number;
Expand Down
6 changes: 4 additions & 2 deletions src/raycasting/intersectOctree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export function intersectOctree(octree: Tree, ray: Ray,
const tz1 = (max.z - origin.z) * invDirZ;

// Check if the ray hits the octree.
return (Math.max(Math.max(tx0, ty0), tz0) < Math.min(Math.min(tx1, ty1), tz1)) ?
[tx0, ty0, tz0, tx1, ty1, tz1] : null;
const hit = (Math.max(Math.max(tx0, ty0), tz0) <
Math.min(Math.min(tx1, ty1), tz1));

return hit ? [tx0, ty0, tz0, tx1, ty1, tz1] : null;

}

0 comments on commit 9c0770e

Please sign in to comment.