Example #1
0
def quat_to_euler(q):
    name = _ek.detail.array_name('Array', q.Type, [3], q.IsScalar)
    module = _modules.get(q.__module__)
    Array3f = getattr(module, name)

    sinp = 2 * _ek.fmsub(q.w, q.y, q.z * q.x)
    gimbal_lock = _ek.abs(sinp) > (1.0 - 5e-8)

    # roll (x-axis rotation)
    q_y_2 = _ek.sqr(q.y)
    sinr_cosp = 2 * _ek.fmadd(q.w, q.x, q.y * q.z)
    cosr_cosp = _ek.fnmadd(2, _ek.fmadd(q.x, q.x, q_y_2), 1)
    roll = _ek.select(gimbal_lock, 2 * _ek.atan2(q.x, q.w),
                      _ek.atan2(sinr_cosp, cosr_cosp))

    # pitch (y-axis rotation)
    pitch = _ek.select(gimbal_lock, _ek.copysign(0.5 * _ek.Pi, sinp),
                       _ek.asin(sinp))

    # yaw (z-axis rotation)
    siny_cosp = 2 * _ek.fmadd(q.w, q.z, q.x * q.y)
    cosy_cosp = _ek.fnmadd(2, _ek.fmadd(q.z, q.z, q_y_2), 1)
    yaw = _ek.select(gimbal_lock, 0, _ek.atan2(siny_cosp, cosy_cosp))

    return Array3f(roll, pitch, yaw)
Example #2
0
def sph_convert(v):
    x2 = ek.pow(v.x, 2)
    y2 = ek.pow(v.y, 2)
    z2 = ek.pow(v.z, 2)

    r = ek.sqrt(x2 + y2 + z2)
    phi = ek.atan2(v.y, v.x)
    theta = ek.atan2(ek.sqrt(x2 + y2), v.z)

    return r, theta, phi
Example #3
0
 def forward(ctx, arg1, arg2):
     ctx.in1 = ek.FloatD(arg1)
     ctx.in2 = ek.FloatD(arg2)
     ek.set_requires_gradient(ctx.in1, arg1.requires_grad)
     ek.set_requires_gradient(ctx.in2, arg2.requires_grad)
     ctx.out = ek.atan2(ctx.in1, ctx.in2)
     out_torch = ctx.out.torch()
     ek.cuda_malloc_trim()
     return out_torch
Example #4
0
def atan2_(a0, a1):
    if not a0.IsFloat:
        raise Exception("atan2(): requires floating point operands!")
    ar, sr = _check2(a0, a1)
    if not a0.IsSpecial:
        for i in range(sr):
            ar[i] = _ek.atan2(a0[i], a1[i])
    else:
        raise Exception("atan2(): unsupported array type!")
    return ar
Example #5
0
def quat_to_euler(q):
    q_y_2 = _ek.sqr(q.y)

    sinr_cosp = 2 * _ek.fmadd(q.w, q.x, q.y * q.z)
    cosr_cosp = _ek.fnmadd(2, _ek.fmadd(q.x, q.x, q_y_2), 1)
    roll = _ek.atan2(sinr_cosp, cosr_cosp)

    # pitch (y-axis rotation)
    sinp = 2 * _ek.fmsub(q.w, q.y, q.z * q.x)
    if (_ek.abs(sinp) >= 1.0):
        pitch = _ek.copysign(0.5 * _ek.Pi, sinp)
    else:
        pitch = _ek.asin(sinp)

    # yaw (z-axis rotation)
    siny_cosp = 2 * _ek.fmadd(q.w, q.z, q.x * q.y)
    cosy_cosp = _ek.fnmadd(2, _ek.fmadd(q.z, q.z, q_y_2), 1)
    yaw = _ek.atan2(siny_cosp, cosy_cosp)

    name = _ek.detail.array_name('Array', q.Type, [3], q.IsScalar)
    module = _modules.get(q.__module__)
    Array3f = getattr(module, name)

    return Array3f(roll, pitch, yaw)
Example #6
0
def test32_atan2(m):
    x = ek.linspace(m.Float, -.8, .8, 10)
    y = m.Float(ek.arange(m.Int, 10) & 1) * 1 - .5
    ek.enable_grad(x, y)
    z = ek.atan2(y, x)
    ek.backward(z)
    assert ek.allclose(
        z,
        m.Float(-2.58299, 2.46468, -2.29744, 2.06075, -1.74674, 1.39486,
                -1.08084, 0.844154, -0.676915, 0.558599))
    assert ek.allclose(
        ek.grad(x),
        m.Float(0.561798, -0.784732, 1.11724, -1.55709, 1.93873, -1.93873,
                1.55709, -1.11724, 0.784732, -0.561798))
    assert ek.allclose(
        ek.grad(y),
        m.Float(-0.898876, -0.976555, -0.993103, -0.83045, -0.344663, 0.344663,
                0.83045, 0.993103, 0.976555, 0.898876))
Example #7
0
 def map_backward(self, p):
     from mitsuba.core import Vector2f
     return Vector2f(ek.atan2(y=p.y, x=p.x), -p.z)
Example #8
0
def cartesian2spherical(w):
    # Convert: Cartesian coordinates -> Spherical
    theta = elevation(w)  #ek.acos(w.z)
    phi = ek.atan2(w.y, w.x)
    return theta, phi