vanruesc / sparse-octree

A sparse octree data structure.
zlib License
117 stars 20 forks source link

I cant create point in same position #39

Closed gurkansahinn closed 3 years ago

gurkansahinn commented 3 years ago

An alternative should be developed to create more than one point in the same position.

vanruesc commented 3 years ago

It's possible to store multiple data for a single point by using an array or a set:

import { Vector3 } from "three";
import { PointOctree } from "sparse-octree";

const min = new Vector3(-1, -1, -1);
const max = new Vector3(1, 1, 1);
const octree = new PointOctree(min, max);
const point = new Vector3(0, 0, 0);

octree.set(point, ["my data"]);
octree.get(point).push("more data");
octree.get(point); // => ["my data", "more data"]

Related to #37

gurkansahinn commented 3 years ago

yes, but that's not what I really want. The points should be used directly as data. Adding additional data to these may not be a good solution. I don't think it would be harmful to create multiple points in the same position.

vanruesc commented 3 years ago

If I understand you correctly, then you want the following to work:

const p1 = new Vector3(0, 0, 0);
const p2 = new Vector3(0, 0, 0);

octree.set(p1, "some data");
octree.set(p2, "some other data");

octree.get(p1); // => "some data"
octree.get(p2); // => "some other data"

But instead you'd get "some other data" for both p1 and p2 with the current implementation and culling and raycasting would also only return a single point instead of the two original instances.

The problem with maintaining multiple instances of the same point is that it compromises the integrity of the point octree. The current implementation does not maintain the original vector instance you provide; it only cares about the x, y and z values. If it did use the original instance, the user could later modify this vector and it would no longer be in the correct place in the octree.

The points should be used directly as data.

I think the right way to deal with this use case is to store the point instances as data:

const points = [
  new Vector3(0, 0, 0),
  new Vector3(0, 0, 0),
  new Vector3(0, 0, 0),
  ...
];

if(points.length > 0) {

  const key = points[0];
  octree.set(key , points);
  octree.get(key); // => [p1, p2, p3, ...]

}

If this doesn't address your use case, please provide more information.