def gen(seed, num):
     random.seed(seed)
     
     base_level = GridLevel(0, b0, radius)
     levels = [base_level] + [GridLevel(i, base_level.size / (2**i), radius) for i in range(1, max_levels)]
     epsilon = levels[-1].weight * 0.5
     
     with progress.ProgressContext("Activate Cells", 0, GridLevel.num_cells_in_range(imin, imax, jmin, jmax, 0, 1)):
         base_level.set_active_cells(imin, imax, jmin, jmax, 0, 1)
     
     with progress.ProgressContext("Init Spatial Grid", 0, PointGrid.num_cells(radius, gridmin, gridmax)):
         pgrid = PointGrid(radius, gridmin, gridmax)
     
     with progress.ProgressContext("Generate Samples", 0, num):
         for i in range(num):
             progress.progress_add(1)
             
             if not any(level.cells for level in levels):
                 break
             
             level, cell = pop_cell(levels)
             if level:
                 x0, x1, y0, y1, z0, z1 = level.cell_corners(cell)
                 
                 # test coverage
                 if not is_covered(radius2, b0, pgrid, level, cell.i, cell.j, x0, x1, y0, y1):
                     point = level.sample(x0, x1, y0, y1, z0, z1)
                     if test_disk(radius2, pgrid, point, level, cell.i, cell.j):
                         yield point
                         pgrid.insert(point)
                     else:
                         if level.index < max_levels - 1:
                             split_cell(radius2, b0, pgrid, levels[level.index+1], cell, x0, x1, y0, y1, z0, z1)
             else:
                 break
Exemplo n.º 2
0
def make_blob_visualizer(context, groundob, blobs, display_radius, hide, hide_render=True):
    slope_factor = 1.0

    # common parent empty for blobs
    blob_parent = get_blob_parent(context, groundob.matrix_world, _sampleviz_parent_name)
    blob_parent.hide = hide
    blob_parent.hide_render = hide_render

    # preliminary display object
    # XXX this could be removed eventually, but it's helpful as visual feedback to the user
    # before creating the actual duplicator blob meshes
    with progress.ProgressContext("Sample Visualization", 0, len(blobs)):
        for index, blob in enumerate(blobs):
            progress.progress_add(1)
            if blob:
                # generator for duplimesh, yielding (loc, rot) pairs
                def mesh_samples():
                    up = Vector((0,0,1))
                    for loc, nor, _, _, _ in blob.samples:
                        mat = (slope_factor * up.rotation_difference(nor)).to_matrix()
                        mat.resize_4x4()
                        yield loc, mat
                ob = make_blob_object(context, index, blob.loc, mesh_samples(), display_radius)
                # put it in the blob group
                blob_group_assign(context, ob)
                # use parent to keep the outliner clean
                set_object_parent(ob, blob_parent)
                # apply layers
                if groundob.meadow.use_layers:
                    ob.layers = groundob.meadow.layers
                
                ob.hide = hide
                ob.hide_render = hide_render
                # make children unselectable by default
                ob.hide_select = True
Exemplo n.º 3
0
 def set_active_cells(self, imin, imax, jmin, jmax, kmin, kmax):
     tot = (imax - imin) * (jmax - jmin) * (kmax - kmin)
     self.cells = [None] * tot
     c = 0
     for k in range(kmin, kmax):
         for j in range(jmin, jmax):
             for i in range(imin, imax):
                 progress.progress_add(1)
                 self.cells[c] = GridCell(i, j, k)
                 c += 1
 def set_active_cells(self, imin, imax, jmin, jmax, kmin, kmax):
     tot = (imax - imin) * (jmax - jmin) * (kmax - kmin)
     self.cells = [None] * tot
     c = 0
     for k in range(kmin, kmax):
         for j in range(jmin, jmax):
             for i in range(imin, imax):
                 progress.progress_add(1)
                 self.cells[c] = GridCell(i, j, k)
                 c += 1
Exemplo n.º 5
0
def make_blobs(context, gridob, groundob, samples2D, display_radius):
    blob_group_clear(context)
    blobs = []

    imat = groundob.matrix_world.inverted()

    blobtree = KDTree(len(gridob.data.vertices))
    for i, v in enumerate(gridob.data.vertices):
        co = gridob.matrix_world * v.co
        # note: only using 2D coordinates, otherwise weights get distorted by z offset
        blobtree.insert((co[0], co[1], 0.0), i)
    blobtree.balance()

    for v in gridob.data.vertices:
        co = gridob.matrix_world * v.co
        ok, loc, nor, poly_index = project_on_ground(groundob, co)
        blobs.append(Blob(loc, nor, poly_index) if ok else None)

    with progress.ProgressContext("Grouping Samples", 0, len(samples2D)):
        mpolys = groundob.data.polygons
        mverts = groundob.data.vertices
        for xy in samples2D:
            progress.progress_add(1)

            # note: use only 2D coordinates for weighting, z component should be 0
            index = assign_blob(blobtree, (xy[0], xy[1], 0.0), nor)
            if index < 0:
                continue
            blob = blobs[index]
            if blob is None:
                continue

            # project samples onto the ground object
            ok, sloc, snor, spoly = project_on_ground(groundob,
                                                      xy[0:2] + (0, ))
            if not ok:
                continue

            # calculate barycentric vertex weights on the poly
            poly = mpolys[spoly]
            sverts = list(poly.vertices)
            # note: coordinate space has to be consistent, use sloc in object space
            sweights = poly_3d_calc(tuple(mverts[i].co for i in sverts),
                                    imat * sloc)

            blob.add_sample(sloc, snor, spoly, sverts, sweights)

    blobs_to_customprops(groundob.meadow, blobs)

    make_blob_visualizer(context, groundob, blobs, display_radius, hide=True)
Exemplo n.º 6
0
def make_blobs(context, gridob, groundob, samples2D, display_radius):
    blob_group_clear(context)
    blobs = []
    
    imat = groundob.matrix_world.inverted()

    blobtree = KDTree(len(gridob.data.vertices))
    for i, v in enumerate(gridob.data.vertices):
        co = gridob.matrix_world * v.co
        # note: only using 2D coordinates, otherwise weights get distorted by z offset
        blobtree.insert((co[0], co[1], 0.0), i)
    blobtree.balance()
    
    for v in gridob.data.vertices:
        co = gridob.matrix_world * v.co
        ok, loc, nor, poly_index = project_on_ground(groundob, co)
        blobs.append(Blob(loc, nor, poly_index) if ok else None)
    
    with progress.ProgressContext("Grouping Samples", 0, len(samples2D)):
        mpolys = groundob.data.polygons
        mverts = groundob.data.vertices
        for xy in samples2D:
            progress.progress_add(1)

            # note: use only 2D coordinates for weighting, z component should be 0
            index = assign_blob(blobtree, (xy[0], xy[1], 0.0), nor)
            if index < 0:
                continue
            blob = blobs[index]
            if blob is None:
                continue
            
            # project samples onto the ground object
            ok, sloc, snor, spoly = project_on_ground(groundob, xy[0:2]+(0,))
            if not ok:
                continue
            
            # calculate barycentric vertex weights on the poly
            poly = mpolys[spoly]
            sverts = list(poly.vertices)
            # note: coordinate space has to be consistent, use sloc in object space
            sweights = poly_3d_calc(tuple(mverts[i].co for i in sverts), imat * sloc)

            blob.add_sample(sloc, snor, spoly, sverts, sweights)
    
    blobs_to_customprops(groundob.meadow, blobs)

    make_blob_visualizer(context, groundob, blobs, display_radius, hide=True)
Exemplo n.º 7
0
    def gen(seed, num):
        random.seed(seed)

        base_level = GridLevel(0, b0, radius)
        levels = [base_level] + [
            GridLevel(i, base_level.size / (2**i), radius)
            for i in range(1, max_levels)
        ]
        epsilon = levels[-1].weight * 0.5

        with progress.ProgressContext(
                "Activate Cells", 0,
                GridLevel.num_cells_in_range(imin, imax, jmin, jmax, 0, 1)):
            base_level.set_active_cells(imin, imax, jmin, jmax, 0, 1)

        with progress.ProgressContext(
                "Init Spatial Grid", 0,
                PointGrid.num_cells(radius, gridmin, gridmax)):
            pgrid = PointGrid(radius, gridmin, gridmax)

        with progress.ProgressContext("Generate Samples", 0, num):
            for i in range(num):
                progress.progress_add(1)

                if not any(level.cells for level in levels):
                    break

                level, cell = pop_cell(levels)
                if level:
                    x0, x1, y0, y1, z0, z1 = level.cell_corners(cell)

                    # test coverage
                    if not is_covered(radius2, b0, pgrid, level, cell.i,
                                      cell.j, x0, x1, y0, y1):
                        point = level.sample(x0, x1, y0, y1, z0, z1)
                        if test_disk(radius2, pgrid, point, level, cell.i,
                                     cell.j):
                            yield point
                            pgrid.insert(point)
                        else:
                            if level.index < max_levels - 1:
                                split_cell(radius2, b0, pgrid,
                                           levels[level.index + 1], cell, x0,
                                           x1, y0, y1, z0, z1)
                else:
                    break
Exemplo n.º 8
0
def make_blob_visualizer(context,
                         groundob,
                         blobs,
                         display_radius,
                         hide,
                         hide_render=True):
    slope_factor = 1.0

    # common parent empty for blobs
    blob_parent = get_blob_parent(context, groundob.matrix_world,
                                  _sampleviz_parent_name)
    blob_parent.hide = hide
    blob_parent.hide_render = hide_render

    # preliminary display object
    # XXX this could be removed eventually, but it's helpful as visual feedback to the user
    # before creating the actual duplicator blob meshes
    with progress.ProgressContext("Sample Visualization", 0, len(blobs)):
        for index, blob in enumerate(blobs):
            progress.progress_add(1)
            if blob:
                # generator for duplimesh, yielding (loc, rot) pairs
                def mesh_samples():
                    up = Vector((0, 0, 1))
                    for loc, nor, _, _, _ in blob.samples:
                        mat = (slope_factor *
                               up.rotation_difference(nor)).to_matrix()
                        mat.resize_4x4()
                        yield loc, mat

                ob = make_blob_object(context, index, blob.loc, mesh_samples(),
                                      display_radius)
                # put it in the blob group
                blob_group_assign(context, ob)
                # use parent to keep the outliner clean
                set_object_parent(ob, blob_parent)
                # apply layers
                if groundob.meadow.use_layers:
                    ob.layers = groundob.meadow.layers

                ob.hide = hide
                ob.hide_render = hide_render
                # make children unselectable by default
                ob.hide_select = True
Exemplo n.º 9
0
 def range_progress(tot):
     for i in range(tot):
         progress.progress_add(1)
         yield i
 def range_progress(tot):
     for i in range(tot):
         progress.progress_add(1)
         yield i