# Guide on octree traversal for LOD

I am building an octree LOD for point cloud data visualization. I have octree and every time a camera is moved, I traverse a tree to find nodes and levels ( with throttle; I mean it checks with the throttle of 3 seconds).

Right now tree traversal code is:

``````function traverseTree(root, center_x, center_y, center_z, width) {
let [level, x, y, z] = root;
let newLevel = level + 1;
let key = level + "-" + x + "-" + y + "-" + z;
let distance = Math.sqrt(
Math.pow(Math.abs(cameraPosition[0] - center_x), 2) +
Math.pow(Math.abs(cameraPosition[1] - center_y), 2) +
Math.pow(Math.abs(cameraPosition[2] - center_z), 2)
);
if (distance > 5) {
return [];
}
// let cameraPosition = controls.object.position;
// let myDistanceFromCamera = cameraPosition.distanceTo(
//   new THREE.Vector3(center_x, center_y, center_z)
// );

let x_left = center_x - width[0] / 2;
let x_right = center_x + width[0] / 2;
let y_top = center_y + width[1] / 2;
let y_bottom = center_y - width[1] / 2;
let z_near = center_z - width[2] / 2;
let z_far = center_z + width[2] / 2;

let result = [key, nodePages[key].pointCount];
direction.forEach((element, index) => {
let [dx, dy, dz] = element;
let key = `\${newLevel}-\${2 * x + dx}-\${2 * y + dy}-\${2 * z + dz}`;
if (!(key in nodePages && nodePages[key].pointCount > 0)) {
return [];
}
center_x = x_left;
center_y = y_bottom;
center_z = z_far;
if (dx == 1) {
center_x = x_right;
}
if (dy == 1) {
center_y = y_top;
}
if (dz == 1) {
center_z = z_near;
}
let result1 = traverseTree(
[newLevel, 2 * x + dx, 2 * y + dy, 2 * z + dz],
center_x,
center_y,
center_z,
[width[0] / 2, width[1] / 2, width[2] / 2]
);
result.push(...result1);
});
return result;
}
``````

each node of the tree is labeled as

` level_of_tree + "-" + x + "-" + y + "-" + z`

and the tree is stored in a javascript object which I believe both of this information is not useful here.

For a point cloud data covering the width of width_x, width_y, width_z, and camera with the position of vec3 (x, y, z) what is my threshold value to check if the tree traversal should go deeper or not?

Right now I am just giving the random value of 5 which is causing an issue because in some places it is traversing so deep causing the system very slow and crash something.

I want to make it more like how it is supposed to be.