def add(u: Vector = ZEROS, v: Vector = ZEROS) -> Vertices: u, v = make_compatible(u, v) max_len = max(u.shape[0], v.shape[0]) out = np.zeros((max_len, 4)) out[:, :3] = u[:, :3] + v[:, :3] out[:, 3] = max(u[0, 3], v[0, 3]) return out
def distance(u: Vector = ZEROS, v: Vector = ZEROS) -> Float: # speed!? http://stackoverflow.com/a/9184560/1243487 # return length(sub(u, v)) u, v = make_compatible(u, v) x = u[:, :3] - v[:, :3] return np.sqrt((x * x).sum(axis=1))
def sub(u: Vector = ZEROS, v: Vector = ZEROS) -> Vertices: # return add(u, opposite(v)) u, v = make_compatible(u, v) max_len = max(u.shape[0], v.shape[0]) out = np.zeros((max_len, 4)) out[:, :3] = u[:, :3] - v[:, :3] out[:, 3] = max(u[0, 3], v[0, 3]) return out
def merge(a: Vertices = None, b: Vertices = None, mix: BoolP(description="interleave") = False) -> Vertices: if a is None: return b if b is None: return a if mix: a, b = make_compatible(a, b, broadcast=False) new_l = len(a) + len(b) out = np.empty((new_l, 4)) out[::2] = a out[1::2] = b return out else: return np.concatenate((a, b))
def dot(u: Vector = ZEROS, v: Vector = ZEROS) -> Float: u, v = make_compatible(u, v) return np.sum(u[:, :3] * v[:, :3], axis=1) # .. not sure
def scale(u: Vector = ZEROS, s: Float = 1.0) -> Vertices: u, s = make_compatible(u, s[:, np.newaxis]) out = u.copy() out[:, :3] = u[:, :3] * s[:, :3] return out
def cross(u: Vector = X_AXIS, v: Vector = Y_AXIS) -> Vertices: u, v = make_compatible(u, v) max_len = max(u.shape[0], v.shape[0]) out = np.zeros((max_len, 4)) out[:, :3] = np.cross(u[:, :3], v[:, :3]) return out
def component_mul(u: Vector = ONES, v: Vector = ONES) -> Vertices: u, v = make_compatible(u, v) return u * v