def arr_vulkan_layout_to_field_normal_layout(vk_arr: ndarray_type.ndarray(), normal_field: template()): static_assert(len(normal_field.shape) == 2) w = normal_field.shape[0] h = normal_field.shape[1] for i, j in ndrange(w, h): normal_field[i, j] = vk_arr[(h - 1 - j) * w + i]
def outer_product(self, other): impl.static( impl.static_assert(self.m == 1, "lhs for outer_product is not a vector")) impl.static( impl.static_assert(other.m == 1, "rhs for outer_product is not a vector")) ret = Matrix([[self[i] * other[j] for j in range(other.n)] for i in range(self.n)]) return ret
def dot(self, other): """Perform the dot product with the input Vector (1-D Matrix). Args: other (:class:`~taichi.lang.matrix.Matrix`): The input Vector (1-D Matrix) to perform the dot product. Returns: DataType: The dot product result (scalar) of the two Vectors. """ impl.static( impl.static_assert(self.m == 1, "lhs for dot is not a vector")) impl.static( impl.static_assert(other.m == 1, "rhs for dot is not a vector")) return (self * other).sum()
def _matrix_outer_product(self, other): """Perform the outer product with the input Vector (1-D Matrix). Args: other (:class:`~taichi.lang.matrix.Matrix`): The input Vector (1-D Matrix) to perform the outer product. Returns: :class:`~taichi.lang.matrix.Matrix`: The outer product result (Matrix) of the two Vectors. """ impl.static( impl.static_assert(self.m == 1, "lhs for outer_product is not a vector")) impl.static( impl.static_assert(other.m == 1, "rhs for outer_product is not a vector")) return matrix.Matrix([[self[i] * other[j] for j in range(other.n)] for i in range(self.n)])
def vector_to_fast_image(img: template(), out: ndarray_type.ndarray()): # FIXME: Why is ``for i, j in img:`` slower than: for i, j in ndrange(*img.shape): r, g, b = 0, 0, 0 color = img[i, img.shape[1] - 1 - j] if static(img.dtype in [f16, f32, f64]): r, g, b = min(255, max(0, int(color * 255))) else: static_assert(img.dtype == u8) r, g, b = color idx = j * img.shape[0] + i # We use i32 for |out| since OpenGL and Metal doesn't support u8 types if static(get_os_name() != 'osx'): out[idx] = (r << 16) + (g << 8) + b else: # What's -16777216? # # On Mac, we need to set the alpha channel to 0xff. Since Mac's GUI # is big-endian, the color is stored in ABGR order, and we need to # add 0xff000000, which is -16777216 in I32's legit range. (Albeit # the clarity, adding 0xff000000 doesn't work.) alpha = -16777216 out[idx] = (b << 16) + (g << 8) + r + alpha
def normalized(self, eps=0): """Normalize a vector. Args: eps (Number): a safe-guard value for sqrt, usually 0. Examples:: a = ti.Vector([3, 4]) a.normalized() # [3 / 5, 4 / 5] # `a.normalized()` is equivalent to `a / a.norm()`. Note: Only vector normalization is supported. """ impl.static( impl.static_assert(self.m == 1, "normalized() only works on vector")) invlen = 1 / (self.norm() + eps) return invlen * self
def dot(self, other): impl.static( impl.static_assert(self.m == 1, "lhs for dot is not a vector")) impl.static( impl.static_assert(other.m == 1, "rhs for dot is not a vector")) return (self * other).sum()
def normalized(self, eps=0): impl.static( impl.static_assert(self.m == 1, "normalized() only works on vector")) invlen = 1 / (self.norm() + eps) return invlen * self