2019-11-11 10:03:52 +00:00
|
|
|
""" Misc utilities. """
|
|
|
|
|
|
|
|
from mathutils import Vector
|
2020-10-20 18:28:53 +00:00
|
|
|
from typing import List
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-11-29 23:32:17 +00:00
|
|
|
|
|
|
|
def vec_to_str(vec):
|
|
|
|
return "({:.4},{:.4},{:.4})".format(vec.x,vec.y,vec.z)
|
|
|
|
|
|
|
|
def quat_to_str(quat):
|
2020-12-06 05:19:58 +00:00
|
|
|
return "({:.4},{:.4},{:.4},{:.4})".format(quat.w, quat.x, quat.y, quat.z)
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
def add_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(v0 + v1 for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def sub_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(v0 - v1 for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def mul_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(v0 * v1 for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def div_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(v0 / v1 for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def max_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(max(v0, v1) for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def min_vec(l: Vector, r: Vector) -> Vector:
|
|
|
|
return Vector(min(v0, v1) for v0, v1 in zip(l, r))
|
|
|
|
|
|
|
|
def pack_color(color) -> int:
|
|
|
|
packed = 0
|
|
|
|
|
2020-02-04 01:28:34 +00:00
|
|
|
packed |= (int(color[0] * 255.0 + 0.5) << 16)
|
|
|
|
packed |= (int(color[1] * 255.0 + 0.5) << 8)
|
2019-11-11 10:03:52 +00:00
|
|
|
packed |= (int(color[2] * 255.0 + 0.5))
|
|
|
|
packed |= (int(color[3] * 255.0 + 0.5) << 24)
|
|
|
|
|
|
|
|
return packed
|
2020-10-20 18:28:53 +00:00
|
|
|
|
|
|
|
def unpack_color(color: int) -> List[float]:
|
|
|
|
|
|
|
|
mask = int(0x000000ff)
|
|
|
|
|
|
|
|
r = (color & (mask << 16)) / 255.0
|
|
|
|
g = (color & (mask << 8)) / 255.0
|
|
|
|
b = (color & mask) / 255.0
|
|
|
|
a = (color & (mask << 24)) / 255.0
|
|
|
|
|
|
|
|
return [r,g,b,a]
|