def CalNormal(): for w, h in ti.ndrange(WIDGHT, HEIGTH): # 基础法向量 base = face_base_normal[deep_index[w, h]] radius = ti.acos(-base[2]) up = ti.Vector([base[1], -base[0], 0]) q = QuaternionRotationByRadis(res_normal[w, h], up, radius) res_normal[w, h][0], res_normal[w, h][1], res_normal[w, h][2] = q[0], q[1], q[2]
def ggx_sample(n, wo, roughness): u = ti.Vector([1.0, 0.0, 0.0]) if abs(n[1]) < 1.0 - eps: u = n.cross(ti.Vector([0.0, 1.0, 0.0])).normalized() v = n.cross(u) r0 = ti.random() r1 = ti.random() a = roughness ** 2.0 a2 = a * a theta = ti.acos(ti.sqrt((1.0 - r0) / ((a2-1.0)*r0 + 1.0))) phi = 2.0 * math.pi * r1 wm = ti.cos(theta) * n + ti.sin(theta) * ti.cos(phi) * \ u + ti.sin(theta) * ti.sin(phi) * v wi = reflect(-wo, wm) return wi
def rotmat2rotvec(rotmat): m00, m01, m02 = rotmat[0, 0], rotmat[0, 1], rotmat[0, 2] m10, m11, m12 = rotmat[1, 0], rotmat[1, 1], rotmat[1, 2] m20, m21, m22 = rotmat[2, 0], rotmat[2, 1], rotmat[2, 2] tr = m00 + m11 + m22 qw = ti.sqrt(1.0 + tr) / 2.0 qx = (m21 - m12) / (4.0 * qw) qy = (m02 - m20) / (4.0 * qw) qz = (m10 - m01) / (4.0 * qw) angle = ti.acos((tr - 1.0) / 2.0) axis = (ti.Vector([qx, qy, qz]) * (ti.sin(angle) / abs(ti.sin(angle)) + 1e-9)).normalized() return angle * axis
def func(): xi[0] = -yi[None] xi[1] = ~yi[None] xi[2] = ti.logical_not(yi[None]) xi[3] = ti.abs(yi[None]) xf[0] = -yf[None] xf[1] = ti.abs(yf[None]) xf[2] = ti.sqrt(yf[None]) xf[3] = ti.sin(yf[None]) xf[4] = ti.cos(yf[None]) xf[5] = ti.tan(yf[None]) xf[6] = ti.asin(yf[None]) xf[7] = ti.acos(yf[None]) xf[8] = ti.tanh(yf[None]) xf[9] = ti.floor(yf[None]) xf[10] = ti.ceil(yf[None]) xf[11] = ti.exp(yf[None]) xf[12] = ti.log(yf[None])
def intramolecular_forces(): for i in range(0, N): for j in range(i + 1, N): kb = intra[i, j][0] b0 = intra[i, j][1] if kb != 0: r = pos[j] - pos[i] r_norm = r.norm() U[None] += kb * ((r_norm - b0)**2) # angle potential for i in range(0, N_angles): ri = pos[int(angles[i][0])] rj = pos[int(angles[i][1])] rk = pos[int(angles[i][2])] rij = ri - rj rkj = rk - rj theta = ti.acos(rij.dot(rkj) / (rij.norm() * rkj.norm())) kthetha = angles[i][3] theta0 = pi * angles[i][4] / 180 U[None] += kthetha * ((theta - theta0)**2)
def test_trigonometric(): grad_test(lambda x: ti.tanh(x), lambda x: np.tanh(x)) grad_test(lambda x: ti.sin(x), lambda x: np.sin(x)) grad_test(lambda x: ti.cos(x), lambda x: np.cos(x)) grad_test(lambda x: ti.acos(x), lambda x: np.arccos(x)) grad_test(lambda x: ti.asin(x), lambda x: np.arcsin(x))
lambda x: x * x * x * x, lambda x: 0.4 * x * x - 3, lambda x: (x - 3) * (x - 1), lambda x: (x - 3) * (x - 1) + x * x, ]) @if_has_autograd @test_utils.test() def test_poly(tifunc): grad_test(tifunc) @pytest.mark.parametrize('tifunc,npfunc', [ (lambda x: ti.tanh(x), lambda x: np.tanh(x)), (lambda x: ti.sin(x), lambda x: np.sin(x)), (lambda x: ti.cos(x), lambda x: np.cos(x)), (lambda x: ti.acos(x), lambda x: np.arccos(x)), (lambda x: ti.asin(x), lambda x: np.arcsin(x)), ]) @if_has_autograd @test_utils.test(exclude=[ti.vulkan]) def test_trigonometric(tifunc, npfunc): grad_test(tifunc, npfunc) @pytest.mark.parametrize('tifunc', [ lambda x: 1 / x, lambda x: (x + 1) / (x - 1), lambda x: (x + 1) * (x + 2) / ((x - 1) * (x + 3)), ]) @if_has_autograd @test_utils.test()
def derivative(self, cosx, args): k, theta0 = args[0], args[1] return - k * (ti.acos(cosx) - theta0) / ti.sqrt(1 - cosx ** 2)
def __call__(self, cosx, args): k, theta0 = args[0], args[1] return 1/2. * k * (ti.acos(cosx) - theta0) ** 2