Beispiel #1
0
def rasterise(vertices, callback):
    if len(vertices) != 3:
        print("Error, incorrect number of vertices")
        return False

    v0 = vertices[0]
    v1 = vertices[1]
    v2 = vertices[2]

    d0 = m2d.sub(v1, v0)
    d1 = m2d.sub(v2, v0)

    mnmx = minmax(v0, v1, v2)

    k = kross(d0, d1)

    for x in range(mnmx[0][0], mnmx[0][1]+1):
        for y in range(mnmx[1][0], mnmx[1][1]+1):
            q = m2d.sub([x, y], v0)
            s = float(kross(q, d1) / k)
            t = float(kross(d0, q) / k)
            if s >= 0 and t >= 0 and (s + t <= 1):
                callback((x, y))
Beispiel #2
0
    def build_grid(self):
        scale = self.obj.scale
        dims = self.obj.dims

        centre = m2d.mul(compute_centre(self.bound), dims[0])

        print("Map Dims:", scale, dims, centre)

        prim_count = self.obj.get_prim_count()

        min_dims = m2d.mul(m2d.sub(self.bound[PB][:-1], self.bound[PA][:-1]),
                           dims[0])
        print(min_dims)

        tile_dims = []

        ss_offset = [dims[0] / 2, dims[1] / 2]  # Screen space offset
        ss_scale = [1, -1]  # Flip y axis on screen
        # Find minimum dimensions
        for floor in range(prim_count):
            if floor % 2 == 1:
                continue  # Skip odd numbered primitives (the other tri in the quad)

            prim = self.obj.get_prim(floor)
            if len(prim) != 3:
                continue

            A = self.obj.get_position(
                prim[0])[:-1]  # Left Bottom corner, truncate z coord
            B = self.obj.get_position(prim[1])[:-1]  # Right Bottom corner
            C = self.obj.get_position(prim[2])[:-1]  # Left Top corner

            lb = m2d.sub(m2d.cp_mul([A[X], A[Y]], dims), centre)
            rt = m2d.sub(m2d.cp_mul([B[X], C[Y]], dims), centre)

            # Move the polygon into screen-space for direct display by the Display Window
            slb = m2d.add(m2d.cp_mul(lb, ss_scale), ss_offset)
            srt = m2d.add(m2d.cp_mul(rt, ss_scale), ss_offset)

            self.poly_arr.append([slb, srt])

            tile_delta = m2d.sub(rt, lb)
            print(floor, A, B, tile_delta)

            if tile_delta[X] < min_dims[X]:
                min_dims[X] = tile_delta[X]
            if tile_delta[Y] < min_dims[Y]:
                min_dims[Y] = tile_delta[Y]

            tile_dims.append(tile_delta)

        print(min_dims)
        self.min_dims = min_dims

        # Compute the greatest common divisor for each axis
        x_dims = list(map(lambda x: x[0], tile_dims))
        y_dims = list(map(lambda x: x[1], tile_dims))
        xmin = functools.reduce(lambda x, y: gcd(int(x), int(y)), x_dims)
        ymin = functools.reduce(lambda x, y: gcd(int(x), int(y)), y_dims)

        print(
            "X Axis GCD:", xmin,
            x_dims)  # Seems to be 1 in most cases... will have to be by pixel
        print("Y Axis GCD:", ymin, y_dims)
        print("Polygons:", self.poly_arr)