Пример #1
0
 def __getattr__(self, name):
     try:
         return self[name]
     except KeyError as e:
         if not ti.inside_kernel():
             raise AttributeError(name) from None
         return self.FakeAssign(self, name)
Пример #2
0
def _vector_getattr(self, key):
    ret = []
    stk = []
    for k in key:
        sgn = 0
        i = 0
        if k != '_':
            sgn = 1
            i = 'xyzw'.find(k)
            if i == -1:
                sgn = -1
                i = 'XYZW'.find(k)
                if i == -1:
                    break

        stk.append((i, sgn))

    else:
        for i, sgn in stk:
            if ti.inside_kernel():
                t = self.subscript(i) * sgn
                ret.append(ti.expr_init(t))
            else:
                t = self[i] * sgn
                ret.append(t)

        return ti.Vector(ret)

    _taichi_skip_traceback = 1
    raise AttributeError(f"'Matrix' object has no attribute {key}")
Пример #3
0
 def iMouse(self):
     '''
     (TS, 2D float32 vector, RO) Current mouse position from 0 to 1.
     '''
     if not self.has_input:
         raise Exception(
             'Add ``self.define_input()`` to ``on_init`` if you '
             'wish to use inputs')
     if ti.inside_kernel():
         return self._iMouse.subscript(None)
     else:
         return self._iMouse[None]
Пример #4
0
 def iFrame(self):
     '''
     (TS, int32, RO) Current frame number start from 0.
     '''
     if not self.has_input:
         raise Exception(
             'Add ``self.define_input()`` to ``on_init`` if you '
             'wish to use inputs')
     if ti.inside_kernel():
         return ti.subscript(self._iFrame, None)
     else:
         return self._iFrame[None]
Пример #5
0
 def iTime(self):
     '''
     (TS, float32, RO) Current time in seconds.
     '''
     if not self.has_input:
         raise Exception(
             'Add ``self.define_input()`` to ``on_init`` if you '
             'wish to use inputs')
     if ti.inside_kernel():
         return ti.subscript(self._iTime, None)
     else:
         return self._iTime[None]
Пример #6
0
def clamp_unsigned(x):
    def _clamp_unsigned_to_range(npty, val):
        iif = np.iinfo(npty)
        if iif.min <= val <= iif.max:
            return val
        cap = (1 << iif.bits)
        if not (0 <= val < cap):
            return val
        new_val = val - cap
        return new_val

    if ti.inside_kernel():
        if ti.impl.get_runtime().default_ip in {ti.i32, ti.u32}:
            return _clamp_unsigned_to_range(np.int32, x)
        elif ti.impl.get_runtime().default_ip in {ti.i64, ti.u64}:
            return _clamp_unsigned_to_range(np.int64, x)
    return x
Пример #7
0
    def iKeyDirection(self):
        '''
        (TS, 2D float32 vector, RO) Direction according to ASWD / arrow keys.

        If A or left arrow is pressed, then ``self.iKeyDirection`` is ``vec(-1.0, 0.0)``.
        If D or right arrow is pressed, then ``self.iKeyDirection`` is ``vec(1.0, 0.0)``.
        If W or up arrow is pressed, then ``self.iKeyDirection`` is ``vec(0.0, 1.0)``.
        If S or down arrow is pressed, then ``self.iKeyDirection`` is ``vec(0.0, -1.0)``.
        '''
        if not self.has_input:
            raise Exception(
                'Add ``self.define_input()`` to ``on_init`` if you '
                'wish to use inputs')
        if ti.inside_kernel():
            return self._iKeyDirection.subscript(None)
        else:
            return self._iKeyDirection[None]
Пример #8
0
    def iMouseButton(self):
        '''
        (TS, 3D int32 vector, RO) Current mouse button status.

        ``self.iMouseButton[0]`` is ``1`` if LMB is pressed.
        ``self.iMouseButton[1]`` is ``1`` if MMB is pressed.
        ``self.iMouseButton[2]`` is ``1`` if RMB is pressed.
        Otherwise, ``0``.
        '''
        if not self.has_input:
            raise Exception(
                'Add ``self.define_input()`` to ``on_init`` if you '
                'wish to use inputs')
        if ti.inside_kernel():
            return self._iMouseButton.subscript(None)
        else:
            return self._iMouseButton[None]
Пример #9
0
def materialize(self, *args):
    if not ti.inside_kernel():
        return
    #print(time.time(), 'Func call:', self.func)