def _create_grid(self, cells): # we must alocate memory for 3d grid and array nx = self.nx ny = self.ny nz = self.nz num_cells = int(nx * ny * nz) self.asm_cells = x86.MemData(num_cells * 4) self.asm_cells.fill() self.lin_array = x86.MemData( self.num_arrays * 4 + self.num_objects * 4 + 4) #we start of index[1] that why extra four bytes x86.SetUInt32(self.lin_array.ptr(), 0, 0) offset = 4 # offset is in bytes addr_cells = self.asm_cells.ptr() addr_arr = self.lin_array.ptr() 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] if len(cell) == 0: pass else: adr = addr_cells + idx * 4 x86.SetUInt32(adr, offset, 0) adr = addr_arr + offset num = len(cell) x86.SetUInt32(adr, num, 0) offset += 4 x86.SetUInt32(adr + 4, tuple(cell), 0) offset = offset + len(cell) * 4
def _create_grid(self, runtimes, intersector, visibility=False): # we must alocate memory for 3d grid and array cells = self.cells nx = self.nx ny = self.ny nz = self.nz num_cells = int(nx * ny * nz) if visibility: self.asm_cells_b = x86.MemData(num_cells * 4) self.asm_cells_b.fill() self.lin_arrays_b = {} else: self.asm_cells = x86.MemData(num_cells * 4) self.asm_cells.fill() self.lin_arrays = {} for r in runtimes: if visibility: self.lin_arrays_b[r] = x86.MemData( self.num_arrays * 4 + self.num_objects * 8 + 4) #we start of index[1] that why extra four bytes x86.SetUInt32(self.lin_arrays_b[r].ptr(), 0, 0) else: self.lin_arrays_b = {} self.lin_arrays[r] = x86.MemData( self.num_arrays * 4 + self.num_objects * 8 + 4) #we start of index[1] that why extra four bytes x86.SetUInt32(self.lin_arrays[r].ptr(), 0, 0) offset = 4 # offset is in bytes if visibility: addr_cells = self.asm_cells_b.ptr() else: addr_cells = self.asm_cells.ptr() 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] if len(cell) == 0: pass else: adr = addr_cells + idx * 4 x86.SetUInt32(adr, offset, 0) for r in runtimes: if visibility: addr_arr = self.lin_arrays_b[r].ptr() else: addr_arr = self.lin_arrays[r].ptr() adr = addr_arr + offset num = len(cell) x86.SetUInt32(adr, num, 0) x86.SetUInt32( adr + 4, self._get_ptrs_obj_func( cell, r, intersector, visibility), 0) offset = offset + len(cell) * 8 + 4
def add(self, v0, v1, v2): if self._reserve == self._size: self._resize() offset = self._item_size * self._size x86.SetUInt32(self._address.ptr() + offset, (v0, v1, v2), 0) self._size += 1
def set_pixel(self, x, y, r, g, b, a=255): #y = self.height - y - 1 # for flipping image #clipping FIXME throw exception if x < 0 or x >= self.width: return if y < 0 or y >= self.height: return adr = y * self.pitch + x * 4 pix = (a<<24) | (r<<16) | (g<<8) | b x86.SetUInt32(self.pixels.ptr()+adr, pix, 0)
def add(self, v0, v1, v2, nx, ny, nz): if self._reserve == self._size: self._resize() offset = self._item_size * self._size addr = self._address.ptr() + offset x86.SetUInt32(addr, (v0, v1, v2), 0) x86.SetFloat(addr + 16, (nx, ny, nz, 0.0), 0) self._size += 1
def set_pixel(self, x, y, r, g, b, a=255): if x < 0 or x >= self.width: return False if y < 0 or y >= self.height: return False adr = y * self.pitch + x * 4 pix = (a << 24) | (r << 16) | (g << 8) | b x86.SetUInt32(self._pixels.ptr() + adr, pix, 0) return True
def set_pixel(self, x, y, r, g, b, a=255): """ Set color of pixel at cooridantes x, y. r, g, b, a are red, green blue and alpha color components. Each component must be in range 0-255. Default value for alpha component is 255. Function return False if x, y coordinates are out of range otherwise True """ if x < 0 or x >= self.width: return False if y < 0 or y >= self.height: return False adr = y * self.pitch + x * 4 pix = (a<<24) | (r<<16) | (g<<8) | b x86.SetUInt32(self._pixels.ptr() + adr, pix, 0) return True
def edit(self, index, v0, v1, v2, nx, ny, nz): if index < self._size: addr = self._address.ptr() + index * self._item_size x86.SetUInt32(addr, (v0, v1, v2), 0) x86.SetFloat(addr + 16, (nx, ny, nz, 0.0), 0)
def edit(self, index, v0, v1, v2): if index < self._size: addr = self._address.ptr() + index * self._item_size x86.SetUInt32(addr, (v0, v1, v2), 0)
def setup(self, mesh): self.mesh = mesh p0 = Vector3(9999999.0, 9999999.0, 9999999.0) p1 = Vector3(-9999999.0, -9999999.0, -9999999.0) bb_min = BBox(p0, p1, None) ntriangles = mesh.ntriangles() #get number of triangles self.bbox = mesh.bbox() #get bounding box around mesh bb_min = self.bbox num_shapes = ntriangles wx = bb_min.x1 - bb_min.x0 wy = bb_min.y1 - bb_min.y0 wz = bb_min.z1 - bb_min.z0 multiplier = 1.3 # about 8 times more cells than objects ako stavimo faktor 2 TODO test this! s = math.pow(wx * wy * wz / float(num_shapes), 0.333333) nx = int(multiplier * wx / s + 1) ny = int(multiplier * wy / s + 1) nz = int(multiplier * wz / s + 1) self.nx = nx self.ny = ny self.nz = nz num_cells = int(nx * ny * nz) print("wx=", wx, " wy=", wy, " wz=", wz) print("nx=", nx, " ny=", ny, " nz=", nz) # every cell have referencs to objects that are in that cell self.cells = [] # we need to initialize empty lists for c in range(num_cells): self.cells.append([]) max_len = 0 num_arrays = 0 num_objects = 0 for idx_triangle in range(ntriangles): bbox = mesh.bbox_tri(idx_triangle) ixmin = int( clamp((bbox.x0 - bb_min.x0) * nx / (bb_min.x1 - bb_min.x0), 0, nx - 1)) iymin = int( clamp((bbox.y0 - bb_min.y0) * ny / (bb_min.y1 - bb_min.y0), 0, ny - 1)) izmin = int( clamp((bbox.z0 - bb_min.z0) * nz / (bb_min.z1 - bb_min.z0), 0, nz - 1)) ixmax = int( clamp((bbox.x1 - bb_min.x0) * nx / (bb_min.x1 - bb_min.x0), 0, nx - 1)) iymax = int( clamp((bbox.y1 - bb_min.y0) * ny / (bb_min.y1 - bb_min.y0), 0, ny - 1)) izmax = int( clamp((bbox.z1 - bb_min.z0) * nz / (bb_min.z1 - bb_min.z0), 0, nz - 1)) #print("x = ", ixmin, ixmax) #print("y = ", iymin, iymax) #print("z = ", izmin, izmax) for k in range(izmin, izmax + 1): for j in range(iymin, iymax + 1): for i in range(ixmin, ixmax + 1): idx = i + nx * j + nx * ny * k self.cells[idx].append(idx_triangle) duzina = len(self.cells[idx]) num_objects += 1 if duzina == 1: num_arrays += 1 if duzina > max_len: max_len = duzina nx_1 = float(self.nx - 1) ny_1 = float(self.ny - 1) nz_1 = float(self.nz - 1) self.n_1 = Vector3(nx_1, ny_1, nz_1) ovx = 1.0 / self.nx ovy = 1.0 / self.ny ovz = 1.0 / self.nz self.one_overn = Vector3(ovx, ovy, ovz) nboxx = float(self.nx / (self.bbox.x1 - self.bbox.x0)) nboxy = float(self.ny / (self.bbox.y1 - self.bbox.y0)) nboxz = float(self.nz / (self.bbox.z1 - self.bbox.z0)) self.nbox_width = Vector3(nboxx, nboxy, nboxz) # we must alocate memory 3d grid and arrays self.asm_cells = x86.MemData(num_cells * 4) self.lin_array = x86.MemData( num_arrays * 4 + num_objects * 4 + 4) #we start of index[1] that why extra four bytes x86.SetUInt32(self.lin_array.ptr(), 0, 0) offset = 4 # offset is in bytes for k in range(nz): for j in range(ny): for i in range(nx): idx = i + nx * j + nx * ny * k cell = self.cells[idx] if len(cell) == 0: adr = self.asm_cells.ptr() adr = adr + idx * 4 x86.SetUInt32(adr, 0, 0) else: adr = self.asm_cells.ptr() adr = adr + idx * 4 x86.SetUInt32(adr, offset, 0) adr = self.lin_array.ptr() adr += offset num = len(cell) x86.SetUInt32(adr, num, 0) offset += 4 x86.SetUInt32(adr + 4, tuple(cell), 0) offset = offset + len(cell) * 4 print("Najduzi niz je", duzina) return None
def setup(self, shapes, ren=None): p0 = Vector3(9999999.0, 9999999.0, 9999999.0) p1 = Vector3(-9999999.0, -9999999.0, -9999999.0) bb_min = BBox(p0, p1, None) for shape in shapes: bbox = shape.bbox() if bbox.x0 < bb_min.x0: bb_min.x0 = bbox.x0 if bbox.y0 < bb_min.y0: bb_min.y0 = bbox.y0 if bbox.z0 < bb_min.z0: bb_min.z0 = bbox.z0 if bbox.x1 > bb_min.x1: bb_min.x1 = bbox.x1 if bbox.y1 > bb_min.y1: bb_min.y1 = bbox.y1 if bbox.z1 > bb_min.z1: bb_min.z1 = bbox.z1 self.bbox = bb_min num_shapes = len(shapes) #FIXME when we incoorporate mesh wx = bb_min.x1 - bb_min.x0 wy = bb_min.y1 - bb_min.y0 wz = bb_min.z1 - bb_min.z0 multiplier = 1.3 # about 8 times more cells than objects TODO test this! s = math.pow(wx * wy * wz / float(num_shapes), 0.333333) nx = int(multiplier * wx / s + 1) ny = int(multiplier * wy / s + 1) nz = int(multiplier * wz / s + 1) self.nx = nx self.ny = ny self.nz = nz num_cells = int(nx * ny * nz) print("wx=", wx, " wy=", wy, " wz=", wz) print("nx=", nx, " ny=", ny, " nz=", nz) # every cell have referencs to objects that are in that cell self.cells = [] # we need to initialize list for c in range(num_cells): self.cells.append([]) max_len = 0 num_arrays = 0 num_objects = 0 for shape in shapes: bbox = shape.bbox() ixmin = int( clamp((bbox.x0 - bb_min.x0) * nx / (bb_min.x1 - bb_min.x0), 0, nx - 1)) iymin = int( clamp((bbox.y0 - bb_min.y0) * ny / (bb_min.y1 - bb_min.y0), 0, ny - 1)) izmin = int( clamp((bbox.z0 - bb_min.z0) * nz / (bb_min.z1 - bb_min.z0), 0, nz - 1)) ixmax = int( clamp((bbox.x1 - bb_min.x0) * nx / (bb_min.x1 - bb_min.x0), 0, nx - 1)) iymax = int( clamp((bbox.y1 - bb_min.y0) * ny / (bb_min.y1 - bb_min.y0), 0, ny - 1)) izmax = int( clamp((bbox.z1 - bb_min.z0) * nz / (bb_min.z1 - bb_min.z0), 0, nz - 1)) #print("x = ", ixmin, ixmax) #print("y = ", iymin, iymax) #print("z = ", izmin, izmax) for k in range(izmin, izmax + 1): for j in range(iymin, iymax + 1): for i in range(ixmin, ixmax + 1): idx = i + nx * j + nx * ny * k self.cells[idx].append(shape) duzina = len(self.cells[idx]) num_objects += 1 if duzina == 1: num_arrays += 1 if duzina > max_len: max_len = duzina nx_1 = float(self.nx - 1) ny_1 = float(self.ny - 1) nz_1 = float(self.nz - 1) self.n_1 = Vector3(nx_1, ny_1, nz_1) ovx = 1.0 / self.nx ovy = 1.0 / self.ny ovz = 1.0 / self.nz self.one_overn = Vector3(ovx, ovy, ovz) nboxx = float(self.nx / (self.bbox.x1 - self.bbox.x0)) nboxy = float(self.ny / (self.bbox.y1 - self.bbox.y0)) nboxz = float(self.nz / (self.bbox.z1 - self.bbox.z0)) self.nbox_width = Vector3(nboxx, nboxy, nboxz) # we must alocate memory 3d grid and arrays self.asm_cells = x86.MemData(num_cells * 4) self.lin_array = x86.MemData( num_arrays * 4 + num_objects * 8 + 4) #we start of index[1] that why extra four bytes x86.SetUInt32(self.lin_array.ptr(), 0, 0) offset = 4 # offset is in bytes self.runtime = runtime = Runtime() for k in range(nz): for j in range(ny): for i in range(nx): idx = i + nx * j + nx * ny * k cell = self.cells[idx] if len(cell) == 0: adr = self.asm_cells.ptr() adr = adr + idx * 4 x86.SetUInt32(adr, 0, 0) else: adr = self.asm_cells.ptr() adr = adr + idx * 4 x86.SetUInt32(adr, offset, 0) adr = self.lin_array.ptr() adr += offset num = len(cell) x86.SetUInt32(adr, num, 0) offset += 4 if ren is None: lst_address = renmas.interface.objfunc_array( cell, runtime) else: lst_address = self.objfunc_array( cell, runtime, ren) n = len(lst_address) x86.SetUInt32(adr + 4, lst_address, 0) offset = offset + n * 4 #br = x86.GetUInt32(self.lin_array.ptr(), 0, 30) #print("br", br) #for k in range(nz): # for j in range(ny): # for i in range(nx): # idx = i + nx * j + nx * ny * k # adr = self.asm_cells.ptr() # adr = adr + idx * 4 # br = x86.GetUInt32(adr, 0, 0) # print(k, j, i, idx, br) print("duzina", max_len) print("num objects =", num_objects, " num arrays", num_arrays) return None