def test_solve(self): s = solve([ Point3D(15, 5, 2), Point3D(10, 2, 2), Point3D(5, 7, 2), Point3D(0, 0, 2) ], 0.75) expected = Point3D(15/4, 53/16, 2) self.assertAlmostEqual(s.x, expected.x, delta=1E-5) self.assertAlmostEqual(s.y, expected.y, delta=1E-5) self.assertAlmostEqual(s.z, expected.z, delta=1E-5)
def body_3d_coords(body_array): coords = [] skeleton = body_array.reshape(-1, 4).transpose() for i in skeleton: print(i) coords.append(Point3D(skeleton[i][0], skeleton[i][1], skeleton[i][2])) return coords
def solve(pnts, time_ratio): """ :type pnts: list of Point3D :type time_ratio: float """ return sum( [pnts[i].scale(binom(3, i) * ((1 - time_ratio) ** (3 - i)) * (time_ratio ** i)) for i in range(4)], Point3D(0, 0, 0) )
def generate(): problem = {} while True: vertex_pos = Point3D(random.randint(-100, 100), random.randint(-100, 100), random.randint(-100, 100)) vertex_normal = Vector3D(random.randint(-10, 10), random.randint(-10, 10), random.randint(-10, 10)) cam = Point3D(random.randint(-100, 100), random.randint(-100, 100), random.randint(-100, 100)) light = Point3D(random.randint(-100, 100), random.randint(-100, 100), random.randint(-100, 100)) if vertex_pos != cam and vertex_pos != light and cam != light: v = vertex_pos.vector_to(cam) s = vertex_pos.vector_to(light) h = v + s if vertex_normal.dot_self() == 0 or s.dot_self() == 0 or h.dot_self() == 0: continue problem['v_pos'] = vertex_pos problem['v_norm'] = vertex_normal problem['c_pos'] = cam problem['l_pos'] = light break while True: g_a = Color.random_color() l_a = Color.random_color() if g_a.r + l_a.r > 1.0 or g_a.b + l_a.b > 1.0 or g_a.g + l_a.g > 1.0: continue l_d = Color.random_color() l_s = Color.random_color() m_a = Color.random_color() m_d = Color.random_color() m_s = Color.random_color() shine = random.randint(10, 250) s = solve(problem['v_pos'], problem['v_norm'], problem['c_pos'], problem['l_pos'], g_a, l_a, l_d, l_s, m_a, m_d, m_s, shine) if s.r > 1.0 or s.g > 1.0 or s.b > 1.0: continue problem['g_a'] = g_a problem['l_a'] = l_a problem['l_d'] = l_d problem['l_s'] = l_s problem['m_a'] = m_a problem['m_d'] = m_d problem['m_s'] = m_s problem['shine'] = shine return problem
def test_solve3(self): s = solve(v_pos=Point3D(0, 1, 1), v_normal=Vector3D(1, -1, 3), c_pos=Point3D(-3, 3, 4), l_pos=Point3D(3, -2, 2), l_a=Color(0, 0, 0), l_d=Color(0.5, 0.3, 0.7), l_s=Color(0.3, 0.8, 0.7), g_a=Color(0.3, 0.2, 0.4), m_a=Color(0.4, 0.7, 0.3), m_d=Color(0.4, 0.7, 0.2), m_s=Color(0.6, 0.6, 0.6), shine=21) expected = Color(r=0.3067022520099555, g=0.4365837630415202, b=0.3522745356466035) self.assertAlmostEqual(s.r, expected.r, delta=1E-5, msg='r') self.assertAlmostEqual(s.g, expected.g, delta=1E-5, msg='g') self.assertAlmostEqual(s.b, expected.b, delta=1E-5, msg='b')
def test_solve2(self): s = solve(v_pos=Point3D(4, 4, 3), v_normal=Vector3D(0, 1, 0), c_pos=Point3D(4, 6, 5), l_pos=Point3D(5, 8, -1), g_a=Color(0.3, 0.2, 0.4), l_a=Color(0, 0, 0), l_d=Color(0.5, 0.3, 0.7), l_s=Color(0.3, 0.8, 0.7), m_a=Color(0.4, 0.2, 0.3), m_d=Color(0.4, 0.7, 0.2), m_s=Color(0.6, 0.6, 0.6), shine=13) expected = Color(r=0.336555752545358, g=0.392341571751585, b=0.397835285490389) self.assertAlmostEqual(s.r, expected.r, delta=1E-5, msg='r') self.assertAlmostEqual(s.g, expected.g, delta=1E-5, msg='g') self.assertAlmostEqual(s.b, expected.b, delta=1E-5, msg='b')
def test_solve1(self): s = solve(v_pos=Point3D(1, 4, 3), v_normal=Vector3D(0, 0, -1), c_pos=Point3D(-1, -1, 5), l_pos=Point3D(3, 8, -2), g_a=Color(0, 0, 0), l_a=Color(0.3, 0.7, 0.6), l_d=Color(0.3, 0.7, 0.6), l_s=Color(0.3, 0.7, 0.6), m_a=Color(0.3, 0.1, 0.2), m_d=Color(0.6, 0.2, 0.4), m_s=Color(1, 1, 1), shine=10) expected = Color(r=0.401311078649987, g=0.587692838949990, b=0.653179438199983) self.assertAlmostEqual(s.r, expected.r, delta=1E-5, msg='r') self.assertAlmostEqual(s.g, expected.g, delta=1E-5, msg='g') self.assertAlmostEqual(s.b, expected.b, delta=1E-5, msg='b')
def generate(): t_start = random.randint(0, 15) t_end = random.randint(t_start + 2, 100) t_mid = random.randint(t_start + 1, t_end - 1) while True: pnts = [ Point3D(random.randint(-150, 150), random.randint(-150, 150), random.randint(-150, 150)), Point3D(random.randint(-150, 150), random.randint(-150, 150), random.randint(-150, 150)), Point3D(random.randint(-150, 150), random.randint(-150, 150), random.randint(-150, 150)), Point3D(random.randint(-150, 150), random.randint(-150, 150), random.randint(-150, 150)) ] continue_outer = False for pnt in pnts: if len(list(filter(lambda z: z == pnt, pnts))) > 1: continue_outer = True break if continue_outer: continue break return {'t_start': t_start, 't_mid': t_mid, 't_end': t_end, 'pnts': pnts}
def target_map(bodies, edges, camera, imsize): rows, cols = imsize bodies_2D = [] # pixel points for the edges bodies_3D = [] for body in bodies: bodies_2D.append([ camera.reproject_point(Point3D(i[0], i[1], i[2])) for i in body.reshape(-1, 4) ]) bodies_3D.append([ camera.point_in_cam(Point3D(i[0], i[1], i[2])) for i in body.reshape(-1, 4) ]) # Creating a array on format: # array: [ body0: [ edge0: [im], edge1: [im], ...], # body1: [ edge0: [im], edge1: [im], ...], # ... ] edge_array = create_edge_map(bodies_2D, bodies_3D, imsize, edges, camera) joint_array = create_joint_map() return edge_array, joint_array
def generate(): while True: problem = { 'eye': Point3D(random.randint(-100, 100), random.randint(-100, 100), random.randint(-100, 100)) } while True: look = Point3D(random.randint(-100, 100), random.randint(-100, 100), random.randint(-100, 100)) if look != problem['eye']: problem['look'] = look break up = rand_normal(problem['look'].vector_to(problem['eye']), 100) if up is None: continue problem['up'] = up problem['fov'] = random.randint(50, 150) problem['aspect_ratio'] = Fraction(random.randint(1, 25), random.randint(1, 25)) problem['near'] = random.randint(1, 25) problem['far'] = random.randint(problem['near'] + 1, 100) return problem
def test_solve3(self): d = 1E-5 s = solve( eye=Point3D(-1, -2, 4), look=Point3D(3, 1, -2), up=Vector3D(0, 1, 0), fov=30, aspect_ratio=16 / 9, near=1, far=11 ) n = s['n'] u = s['u'] v = s['v'] vm = s['view_mat'] pm = s['projection_mat'] expected = ( #u ( 3/sqrt(13), 0, 2/sqrt(13) ), #v ( -6/sqrt(793), 26/sqrt(793), 9/sqrt(793) ), #n ( -4/sqrt(61), -3/sqrt(61), 6/sqrt(61) ), #vm { (0, 0): 3/sqrt(13), (0, 1): 0, (0, 2): 2/sqrt(13), (0, 3): -5/sqrt(13), (1, 0): -6/sqrt(793), (1, 1): 26/sqrt(793), (1, 2): 9/sqrt(793), (1, 3): 10/sqrt(793), (2, 0): -4/sqrt(61), (2, 1): -3/sqrt(61), (2, 2): 6/sqrt(61), (2, 3): -34/sqrt(61), (3, 0): 0, (3, 1): 0, (3, 2): 0, (3, 3): 1, }, #pm { # T = tan(30 * pi / 360) # B = -tan(30 * pi / 360) # R = 16 * tan(30 * pi / 360) / 9 # L = -16 * tan(30 * pi / 360) / 9 (0, 0): 1 / (16 * tan(30 * pi / 360) / 9), (0, 1): 0, (0, 2): 0, (0, 3): 0, (1, 0): 0, (1, 1): 1 / (tan(30 * pi / 360)), (1, 2): 0, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): -1.2, (2, 3): -2.2, (3, 0): 0, (3, 1): 0, (3, 2): -1, (3, 3): 0, } ) # U self.assertAlmostEqual(u.x, expected[0][0], delta=d) self.assertAlmostEqual(u.y, expected[0][1], delta=d) self.assertAlmostEqual(u.z, expected[0][2], delta=d) # V self.assertAlmostEqual(v.x, expected[1][0], delta=d) self.assertAlmostEqual(v.y, expected[1][1], delta=d) self.assertAlmostEqual(v.z, expected[1][2], delta=d) # N self.assertAlmostEqual(n.x, expected[2][0], delta=d) self.assertAlmostEqual(n.y, expected[2][1], delta=d) self.assertAlmostEqual(n.z, expected[2][2], delta=d) # View matrix for vm_kv in expected[3].items(): self.assertAlmostEqual(vm.get_element(*vm_kv[0]), vm_kv[1], delta=d) # Projection matrix for pm_kv in expected[4].items(): self.assertAlmostEqual(pm.get_element(*pm_kv[0]), pm_kv[1], delta=d)
def test_solve1(self): d = 1E-5 s = solve( eye=Point3D(0, 8, 4), look=Point3D(0, 3, -1), up=Vector3D(0, 0, -1), fov=75, aspect_ratio=16 / 9, near=3, far=25 ) n = s['n'] u = s['u'] v = s['v'] vm = s['view_mat'] pm = s['projection_mat'] expected = ( #u ( 1.0, 0, 0 ), #v ( 0, sqrt(2) / 2, -sqrt(2) / 2 ), #n ( 0, sqrt(2) / 2, sqrt(2) / 2 ), #vm { (0, 0): 1, (0, 1): 0, (0, 2): 0, (0, 3): 0, (1, 0): 0, (1, 1): sqrt(2)/2, (1, 2): -sqrt(2)/2, (1, 3): -2*sqrt(2), (2, 0): 0, (2, 1): sqrt(2)/2, (2, 2): sqrt(2)/2, (2, 3): -6*sqrt(2), (3, 0): 0, (3, 1): 0, (3, 2): 0, (3, 3): 1, }, #pm { (0, 0): 9 / (16 * (sqrt(6) + sqrt(3) - sqrt(2) - 2)), (0, 1): 0, (0, 2): 0, (0, 3): 0, (1, 0): 0, (1, 1): 1 / (sqrt(6) + sqrt(3) - sqrt(2) - 2), (1, 2): 0, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): -14 / 11, (2, 3): -75 / 11, (3, 0): 0, (3, 1): 0, (3, 2): -1, (3, 3): 0, } ) # U self.assertAlmostEqual(u.x, expected[0][0], delta=d) self.assertAlmostEqual(u.y, expected[0][1], delta=d) self.assertAlmostEqual(u.z, expected[0][2], delta=d) # V self.assertAlmostEqual(v.x, expected[1][0], delta=d) self.assertAlmostEqual(v.y, expected[1][1], delta=d) self.assertAlmostEqual(v.z, expected[1][2], delta=d) # N self.assertAlmostEqual(n.x, expected[2][0], delta=d) self.assertAlmostEqual(n.y, expected[2][1], delta=d) self.assertAlmostEqual(n.z, expected[2][2], delta=d) # View matrix for vm_kv in expected[3].items(): self.assertAlmostEqual(vm.get_element(*vm_kv[0]), vm_kv[1], delta=d) # Projection matrix for pm_kv in expected[4].items(): self.assertAlmostEqual(pm.get_element(*pm_kv[0]), pm_kv[1], delta=d)
def test_solve2(self): d = 1E-5 s = solve( eye=Point3D(-29, -18, -93), look=Point3D(70, -76, -10), up=Vector3D(26, -10, -38), fov=74, aspect_ratio=7/4, near=8, far=23 ) n = s['n'] u = s['u'] v = s['v'] vm = s['view_mat'] pm = s['projection_mat'] expected = ( #u ( 0.45471427033982176, 0.8872473567606278, 0.07763414371655494 ), #v ( 0.5518192679714115, -0.21223817998900443, -0.8065050839582169 ), #n ( -0.6990925745885297, 0.409569387132674, -0.5861079160691713 ), #vm { (0, 0): 0.45471427033982176, (0, 1): 0.8872473567606278, (0, 2): 0.07763414371655494, (0, 3): 36.37714162718574, (1, 0): 0.5518192679714115, (1, 1): -0.21223817998900443, (1, 2): -0.806505083958216, (1, 3): -62.822501276745314, (2, 0): -0.6990925745885297, (2, 1): 0.409569387132674, (2, 2): -0.5861079160691713, (2, 3): -67.40947188911215, (3, 0): 0, (3, 1): 0, (3, 2): 0, (3, 3): 1, }, #pm { (0, 0): 0.7583113266402343, (0, 1): 0, (0, 2): 0, (0, 3): 0, (1, 0): 0, (1, 1): 1.32704482162041, (1, 2): 0, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): -2.066666666666667, (2, 3): -24.533333333333335, (3, 0): 0, (3, 1): 0, (3, 2): -1, (3, 3): 0, } ) # U self.assertAlmostEqual(u.x, expected[0][0], delta=d) self.assertAlmostEqual(u.y, expected[0][1], delta=d) self.assertAlmostEqual(u.z, expected[0][2], delta=d) # V self.assertAlmostEqual(v.x, expected[1][0], delta=d) self.assertAlmostEqual(v.y, expected[1][1], delta=d) self.assertAlmostEqual(v.z, expected[1][2], delta=d) # N self.assertAlmostEqual(n.x, expected[2][0], delta=d) self.assertAlmostEqual(n.y, expected[2][1], delta=d) self.assertAlmostEqual(n.z, expected[2][2], delta=d) # View matrix for vm_kv in expected[3].items(): self.assertAlmostEqual(vm.get_element(*vm_kv[0]), vm_kv[1], delta=d) # Projection matrix for pm_kv in expected[4].items(): self.assertAlmostEqual(pm.get_element(*pm_kv[0]), pm_kv[1], delta=d)
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # This is implicitly used from mpl_toolkits.mplot3d.art3d import Poly3DCollection from base_centered_reciprocal_lattice import BaseCenteredReciprocalLattice from body_centered_reciprocal_lattice import BodyCenteredReciprocalLattice from face_centered_reciprocal_lattice import FaceCenteredReciprocalLattice from geometry import GeometryUtils, Plane, Point3D, Segment3D, Vector3D from hexagonal_close_packed_reciprocal_lattice import \ HexagonalClosePackedReciprocalLattice from primitive_reciprocal_lattice import PrimitiveReciprocalLattice WIDTH = 0.05 # lattice period LATTICE_SIZE = 3 # count of atoms in one direction MIN_ZONES_COUNT = 2 # consider minimum N zones CENTER = Point3D(0, 0, 0) def __get_bragg_planes(zone_points): """Return the Bragg planes.""" for points in zone_points: middle_points = map(lambda point: point * 0.5, points) for middle_point in middle_points: yield Plane(middle_point, Vector3D(tuple(middle_point))) def get_intersections(planes): """Return lines that are intersections of the Bragg planes.""" first_it = iter(planes) try: while True:
def point_in_cam(self, point: Point3D) -> Point3D: coord = np.array([[point.x], [point.y], [point.z]]) extrinsic = np.matmul(self.m, np.append(coord, 1).transpose()) extrinsic = np.matmul(np.eye(3, 4), extrinsic.transpose()) return Point3D(extrinsic[0], extrinsic[1], extrinsic[2])
def normalized_image_point(self, point: Point2D) -> Point3D: """ Project a point into the normalized image plane """ x = (point.x - self.c_x) / self.f_x y = (point.y - self.c_y) / self.f_y z = 1 return Point3D(x, y, z)