Pyrr icon indicating copy to clipboard operation
Pyrr copied to clipboard

Possibly a bug in sig of the distance

Open ipcoder opened this issue 9 months ago • 0 comments

It seems to me that the minus sign is by mistake applied twice when passing the d into create().

Considering that create does not changes values, only packs into array [*normal, distance], than distance there is equivalent to the d in the comment defining the plane equation -d = ax+bx+xz. So that [nx, ny, nx, distance ] equivalent to [a, b, c, d] up to normalization. It is calculated correctly on the next line with required minus d = -np.sum(n * position). However the sign is changed again when passed to create(n, -d, dtype)

@parameters_as_numpy_arrays('position', 'normal')
def create_from_position(position, normal, dtype=None):
    dtype = dtype or position.dtype
    # -d = a * x  + b * y + c * z
    n = vector.normalize(normal)
    d = -np.sum(n * position)
    return create(n, -d, dtype)

def create(normal=None, distance=0.0, dtype=None):
    if normal is None:
        normal = [0.0, 0.0, 1.0]
    return np.array([normal[0], normal[1], normal[2], distance], dtype=dtype)

As a result, when creating a plane in this simple case:

pl = plane.create_from_position([1,1,1], [1,1,1], dtype=float)
print(f"plane: {pl}")

zero = plane.normal(pl).dot(plane.position(pl)) + plane.distance(pl)
print(f"p*n + d = {zero} != 0!"

the result does not satisfy the plane equation p*n + d = 0 for position:

plane: [0.57735027 0.57735027 0.57735027 1.73205081]
p*n + d = 3.4641016151377553 != 0!

Unless I am missing something, I suggest to remove the minus in the call to create:

return create(n, d, dtype)

ipcoder avatar Feb 04 '25 20:02 ipcoder