Beispiel #1
0
    def Atest_isect1(self):
        factory = renmas2.Factory()
        ren = renmas2.Renderer()
        runtime = Runtime()

        ply = renmas2.core.Ply()
        ply.load("I:/Ply_files/cube.ply")
        #ply.load("I:/Ply_files/dragon_vrip.ply")
        #ply.load("I:/Ply_files/xyzrgb_dragon.ply")
        #ply.load("I:/Ply_files/lucy.ply")
        vb = ply._vertex_buffer
        tb = ply._triangle_buffer
        mesh = factory.create_mesh(vb, tb)
        triangles = self.create_triangle_list(vb, tb)

        #self.intersection_tests(10, triangles, mesh)

        mesh._isect_triangles_asm([runtime], "ray_triangles_idx",
                                  ren.assembler, ren.structures)
        mc = ren.assembler.assemble(self.asm_code1(ren))
        ds = runtime.load("test", mc)

        ray = self.random_ray()
        self.ray_ds(ds, ray, "ray1")
        self.flat_mesh_ds(ds, mesh, "mesh1")
        addr = mesh._grid._get_addr_in_array(0, 1, 2)
        print(addr)
        ds["ptr_triangles_arr"] = addr

        ntri = x86.GetUInt32(addr, 0, 0)
        triangles = x86.GetUInt32(addr + 4, 0, ntri)
        hp = mesh.isect_triangles(ray, triangles)
        print(hp)
        if hp:
            print("t=", hp.t)
            print("hit=", hp.hit_point)
            print("normala=", hp.normal)
        print(ntri)
        print(triangles)
        runtime.run("test")
        print("ret", ds["ret"])
        print("t= ", ds["t"])
        print("hit=", ds["hit"])
        print("normala", ds["normal"])

        start = time.clock()
        #self.speed_test(20, triangles, mesh)
        end = time.clock()
Beispiel #2
0
 def get(self, index):
     if index < self._size:
         addr = self._address.ptr() + index * self._item_size
         p = x86.GetUInt32(addr, 0, 3)
         n = x86.GetFloat(addr + 16, 0, 3)
         return (p, n)
     return None
Beispiel #3
0
 def _get_addr_in_array(self, i, j, k):
     idx = i + self.nx * j + self.nx * self.ny * k
     addr = self.asm_cells.ptr() + idx * 4
     offset_in_array = x86.GetUInt32(addr, 0, 0)
     if offset_in_array != 0:
         return self.lin_array.ptr() + offset_in_array
     return None
Beispiel #4
0
 def _compare_cells(self, cells, grid3d, arr):
     nx = self.nx
     ny = self.ny
     nz = self.nz
     for k in range(nz):
         for j in range(ny):
             for i in range(nx):
                 idx = i + nx * j + nx * ny * k
                 cell = cells[idx]
                 addr = grid3d.ptr() + idx * 4
                 idx_in_array = x86.GetUInt32(addr, 0, 0)
                 if idx_in_array != 0:
                     addr = arr.ptr() + idx_in_array
                     ntri = x86.GetUInt32(addr, 0, 0)
                     triangles = x86.GetUInt32(addr + 4, 0, ntri)
                     self._compare_seq(cell, triangles)
Beispiel #5
0
 def get_pixel(self, x, y):
     if x < 0 or x >= self.width: return None
     if y < 0 or y >= self.height: return None
     adr = y * self.width * 4 + x * 4
     # return b, g, r, a
     pix = x86.GetUInt32(self.pixels.ptr()+adr, 0, 0)
     return pix
Beispiel #6
0
 def _compare_cells(self, cells, grid3d, arr):
     nx = self.nx
     ny = self.ny
     nz = self.nz
     for k in range(nz):
         for j in range(ny):
             for i in range(nx):
                 idx = i + nx * j + nx * ny * k
                 cell = cells[idx]
                 addr = grid3d.ptr() + idx * 4
                 idx_in_array = x86.GetUInt32(addr, 0, 0)
                 if idx_in_array != 0:
                     addr = arr.ptr() + idx_in_array
                     ntri = x86.GetUInt32(addr, 0, 0)
                     triangles = x86.GetUInt32(addr + 4, 0, ntri)
                     for c_idx in range(len(cell)):
                         if cell[c_idx] != triangles[c_idx]:
                             raise ValueError("Error in Grid array!!!")
Beispiel #7
0
 def get_pixel(self, x, y):
     if x < 0 or x >= self.width:
         return None
     if y < 0 or y >= self.height:
         return None
     adr = y * self.pitch + x * 4
     pix = x86.GetUInt32(self._pixels.ptr() + adr, 0, 0)
     b = pix & 0xFF
     g = (pix >> 8) & 0xFF
     r = (pix >> 16) & 0xFF
     a = pix >> 24
     return (r, g, b, a)
Beispiel #8
0
 def get_pixel(self, x, y):
     """
         Get color of pixel at cooridantes x, y. (r, g, b, a) tuple is returned.
         If x, y cooridantes are out of range None is return.
     """
     if x < 0 or x >= self.width:
         return None
     if y < 0 or y >= self.height:
         return None
     adr = y * self.pitch + x * 4
     pix = x86.GetUInt32(self._pixels.ptr() + adr, 0, 0)
     b = pix & 0xFF
     g = (pix >> 8) & 0xFF
     r = (pix >> 16) & 0xFF
     a = pix >> 24
     return (r, g, b, a)
Beispiel #9
0
 def get(self, index):
     if index < self._size:
         addr = self._address.ptr() + index * self._item_size
         return x86.GetUInt32(addr, 0, 3)
     return None
Beispiel #10
0
    def isect(self, ray, min_dist=999999.0):
        ox = ray.origin.x
        oy = ray.origin.y
        oz = ray.origin.z
        dx = ray.dir.x
        dy = ray.dir.y
        dz = ray.dir.z

        x0 = self.bbox.x0
        y0 = self.bbox.y0
        z0 = self.bbox.z0
        x1 = self.bbox.x1
        y1 = self.bbox.y1
        z1 = self.bbox.z1

        if dx == 0.0: dx = 0.00001
        a = 1.0 / dx
        if a >= 0:
            tx_min = (x0 - ox) * a
            tx_max = (x1 - ox) * a
        else:
            tx_min = (x1 - ox) * a
            tx_max = (x0 - ox) * a

        if dy == 0.0: dy = 0.00001
        b = 1.0 / dy
        if b >= 0:
            ty_min = (y0 - oy) * b
            ty_max = (y1 - oy) * b
        else:
            ty_min = (y1 - oy) * b
            ty_max = (y0 - oy) * b

        if dz == 0.0: dz = 0.00001
        c = 1.0 / dz
        if c >= 0:
            tz_min = (z0 - oz) * c
            tz_max = (z1 - oz) * c
        else:
            tz_min = (z1 - oz) * c
            tz_max = (z0 - oz) * c

        if tx_min > ty_min: t0 = tx_min
        else: t0 = ty_min

        if tz_min > t0: t0 = tz_min

        if tx_max < ty_max: t1 = tx_max
        else: t1 = ty_max

        if tz_max < t1: t1 = tz_max

        if t0 > t1:
            return False  #no intersection ocur

        if self.bbox.inside(ray.origin):
            ix = int(clamp((ox - x0) * self.nx / (x1 - x0), 0, self.nx - 1))
            iy = int(clamp((oy - y0) * self.ny / (y1 - y0), 0, self.ny - 1))
            iz = int(clamp((oz - z0) * self.nz / (z1 - z0), 0, self.nz - 1))
        else:
            p = ray.origin + ray.dir * t0
            ix = int(clamp((p.x - x0) * self.nx / (x1 - x0), 0, self.nx - 1))
            iy = int(clamp((p.y - y0) * self.ny / (y1 - y0), 0, self.ny - 1))
            iz = int(clamp((p.z - z0) * self.nz / (z1 - z0), 0, self.nz - 1))

        dtx = (tx_max - tx_min) / self.nx
        dty = (ty_max - ty_min) / self.ny
        dtz = (tz_max - tz_min) / self.nz

        if dx > 0.0:
            tx_next = tx_min + (ix + 1) * dtx
            ix_step = 1
            ix_stop = self.nx
        else:
            tx_next = tx_min + (self.nx - ix) * dtx
            ix_step = -1
            ix_stop = -1
        if dx == 0.0:
            tx_next = 9999999.9999
            ix_step = -1
            ix_stop = -1

        if dy > 0.0:
            ty_next = ty_min + (iy + 1) * dty
            iy_step = 1
            iy_stop = self.ny
        else:
            ty_next = ty_min + (self.ny - iy) * dty
            iy_step = -1
            iy_stop = -1
        if dy == 0.0:
            ty_next = 9999999.9999
            iy_step = -1
            iy_stop = -1

        if dz > 0.0:
            tz_next = tz_min + (iz + 1) * dtz
            iz_step = 1
            iz_stop = self.nz
        else:
            tz_next = tz_min + (self.nz - iz) * dtz
            iz_step = -1
            iz_stop = -1
        if dz == 0.0:
            tz_next = 9999999.9999
            iz_step = -1
            iz_stop = -1

        while True:
            #TODO both ways and test if garbage collector release memory!!!
            idx = ix + self.nx * iy + self.nx * self.ny * iz
            #cell = self.cells[ix + self.nx * iy + self.nx * self.ny * iz]
            addr = self.asm_cells.ptr() + idx * 4
            idx_in_array = x86.GetUInt32(addr, 0, 0)
            if idx_in_array != 0:
                addr = self.lin_array.ptr() + idx_in_array
                ntri = x86.GetUInt32(addr, 0, 0)
                triangles = x86.GetUInt32(addr + 4, 0, ntri)
            else:
                triangles = []

            if tx_next < ty_next and tx_next < tz_next:
                if len(triangles) > 0:
                    hp = self.mesh.isect_triangles(ray, triangles, min_dist)
                else:
                    hp = False
                if hp and hp.t < tx_next:
                    return hp

                tx_next += dtx
                ix += ix_step
                if ix == ix_stop: return False

            else:
                if ty_next < tz_next:
                    if len(triangles) > 0:
                        hp = self.mesh.isect_triangles(ray, triangles,
                                                       min_dist)
                    else:
                        hp = False
                    if hp and hp.t < ty_next:
                        return hp

                    ty_next += dty
                    iy += iy_step
                    if iy == iy_stop: return False
                else:
                    if len(triangles) > 0:
                        hp = self.mesh.isect_triangles(ray, triangles,
                                                       min_dist)
                    else:
                        hp = False
                    if hp and hp.t < tz_next:
                        return hp

                    tz_next += dtz
                    iz += iz_step
                    if iz == iz_stop: return False
        return False