示例#1
0
    def draw_geometry(self):
        """Draw the pattern's geometry mesh in the geometry layer.

		Parameters
		----------

		Returns
		-------

		"""

        clear_layer('geometry')
        draw_mesh(self.geometry_mesh, 'geometry')
示例#2
0
    def draw_topology(self):
        """Draw the pattern's topology mesh in the topology layer.

		Parameters
		----------

		Returns
		-------

		"""

        clear_layer('topology')
        draw_mesh(self.topology_mesh, 'topology')
示例#3
0
    def draw_density(self):
        """Draw the pattern's density in the density layer.

		Parameters
		----------

		Returns
		-------

		"""

        clear_layer('density')
        draw_mesh(self.density_mesh, 'density')
示例#4
0
    def draw_singularity(self):
        """Draw the pattern's singularity in the singularity layer.

		Parameters
		----------

		Returns
		-------

		"""

        clear_layer('singularity')
        draw_mesh(self.singularity_mesh, 'singularity')
示例#5
0
import rhinoscriptsyntax as rs

from compas.datastructures.mesh import Mesh
import compas_rhino as rhino

from compas_pattern.topology.pattern_operators import conway_ambo

from compas_pattern.cad.rhino.utilities import draw_mesh

mesh = rs.GetObject('mesh', filter=32)
mesh = rhino.mesh_from_guid(Mesh, mesh)

mesh = conway_ambo(mesh)

rs.EnableRedraw(False)
draw_mesh(mesh)
rs.EnableRedraw(True)
示例#6
0
import compas_rhino as rhino
from compas_rhino.geometry import RhinoGeometry

from compas.datastructures.mesh import Mesh
from compas_pattern.datastructures.pseudo_quad_mesh import PseudoQuadMesh
from compas_pattern.datastructures.pseudo_quad_mesh import pqm_from_mesh
from compas_pattern.cad.rhino.utilities import draw_mesh

from compas_pattern.algorithms.editing import editing

# mesh selection
guid = rs.GetObject('get mesh')
mesh = RhinoGeometry.from_guid(guid)
vertices, faces = mesh.get_vertices_and_faces()
mesh = PseudoQuadMesh.from_vertices_and_faces(vertices, faces)

poles = rs.GetObjects('pole points', filter=1)
if poles is None:
    poles = []
poles = [rs.PointCoordinates(pole) for pole in poles]

vertices, face_vertices = pqm_from_mesh(mesh, poles)

mesh = PseudoQuadMesh.from_vertices_and_faces(vertices, face_vertices)

editing(mesh)

mesh = mesh.to_mesh()

mesh_guid = draw_mesh(mesh)
示例#7
0
def start():
    from compas.geometry.algorithms.smoothing import mesh_smooth_centroid
    from compas.geometry.algorithms.smoothing import mesh_smooth_area

    from compas_pattern.algorithms.smoothing import define_constraints
    from compas_pattern.algorithms.smoothing import apply_constraints

    guid = rs.GetObject('get mesh')
    dense_mesh = RhinoGeometry.from_guid(guid)
    vertices, faces = dense_mesh.get_vertices_and_faces()
    dense_mesh = Mesh.from_vertices_and_faces(vertices, faces)

    #lines = rs.GetObjects('lines', filter = 4)
    #edges = [[rs.CurveStartPoint(line), rs.CurveEndPoint(line)] for line in lines]
    #dense_mesh = Mesh.from_lines(edges)

    #faces = list(dense_mesh.faces())
    #for fkey in faces:
    #    if len(dense_mesh.face_vertices(fkey)) > 10:
    #        delete_face(dense_mesh, fkey)
    #        print '-1 face'

    #dense_mesh = conway_ambo(dense_mesh)
    #dense_mesh = conway_dual(dense_mesh)
    #dense_mesh = conway_gyro(dense_mesh)
    #dense_mesh = conway_dual(dense_mesh)
    #dense_mesh = conway_gyro(dense_mesh)
    #dense_mesh = conway_kis(dense_mesh)

    #rs.EnableRedraw(False)
    #draw_mesh(dense_mesh)
    #rs.EnableRedraw(True)
    #return

    surface_guid = rs.GetSurfaceObject('surface constraint')[0]

    smooth_mesh = dense_mesh.copy()

    smoothing_iterations = rs.GetInteger('number of iterations for smoothing',
                                         number=20)
    damping_value = rs.GetReal('damping value for smoothing', number=.5)

    rs.EnableRedraw(False)

    #constraints, surface_boundaries = custom_constraints(smooth_mesh, surface_guid)
    constraints, surface_boundaries = define_constraints(
        smooth_mesh, surface_guid)

    rs.EnableRedraw(False)
    fixed_vertices = [
        vkey for vkey, constraint in constraints.items()
        if constraint[0] == 'fixed'
    ]
    mesh_smooth_area(smooth_mesh,
                     fixed=fixed_vertices,
                     kmax=smoothing_iterations,
                     damping=damping_value,
                     callback=apply_constraints,
                     callback_args=[smooth_mesh, constraints])

    smooth_mesh_guid = draw_mesh(smooth_mesh)

    #layer = 'smooth_mesh'
    #rs.AddLayer(layer)
    #rs.ObjectLayer(smooth_mesh_guid, layer = layer)

    rs.EnableRedraw(True)
            x, y, z = centroid_points(positions[index])
            vertex[0] = x
            vertex[1] = y
            vertex[2] = z

        if callback:
            callback(k, callback_args)
    
    return vertices



guid = rs.GetObject('get mesh')
mesh = rhino.mesh_from_guid(PseudoQuadMesh, guid)

vertices = []
key_to_index = {}
for i, vkey in enumerate(mesh.vertices()):
    vertices.append(mesh.vertex_coordinates(vkey))
    key_to_index[vkey] = i
faces = [[key_to_index[vkey] for vkey in mesh.face_vertices(fkey)] for fkey in fkeys]

fixed_vertices = [key_to_index[vkey] for vkey in mesh.vertices_on_boundary()]



new_vertices = planarize_faces(vertices, faces, fixed = fixed_vertices, kmax=100)


draw_mesh(Mesh.from_vertices_and_faces(new_vertices, faces))
示例#9
0
def automatic_constraints(mesh, surface_constraint, curve_constraints = [], point_constraints = []):
    """Defines the constraints on the vertices of the mesh on a point, a curve or a surface.
    
    Parameters
    ----------
    mesh : Mesh
        A mesh.
    surface_constraint : Rhino surface guid
        A surface to project vertices.
    curve_constraints : Rhino curve guids
        Curve features on surface to constrain vertices.
    point_constraints : Rhino point guids
        Point features on surface to constrain vertices.

    Returns
    -------
    constraints: dict
        Dictionary of constraints {vertex_key: (constraint_type, constraint_information)}.

    Raises
    ------
    -

    """

    constraints = {}

    surface_boundaries = surface_borders(surface_constraint, border_type = 0)

    # set point constraints at point feature, curve feature extremities and boundary curve corners
    constrained_points = []
    for curve_guid in surface_boundaries:
        start_tgt = rs.CurveTangent(curve_guid, rs.CurveParameter(curve_guid, 0))
        end_tgt = rs.CurveTangent(curve_guid, rs.CurveParameter(curve_guid, 1))
        # add only if not closed or closed with a kink
        if not rs.IsCurveClosed(curve_guid) or not rs.IsVectorParallelTo(start_tgt, end_tgt):
            start = geometric_key(rs.CurveStartPoint(curve_guid))
            end = geometric_key(rs.CurveEndPoint(curve_guid))
            if start not in constrained_points:
                constrained_points.append(start)
            if end not in constrained_points:
                constrained_points.append(end)

    for vkey in mesh.vertices():
        xyz = mesh.vertex_coordinates(vkey)
        geom_key = geometric_key(xyz)
        if geom_key in constrained_points:
            constraints[vkey] = ['point', xyz]

    # set boundary curve constraints
    split_vertices = [vkey for vkey, constraint in constraints.items() if constraint[0] == 'point']
    split_mesh_boundaries = mesh_boundaries(mesh, vertex_splits = split_vertices)

    # constrain a mesh boundary to a surface boundary if the two extremities of the mesh boundary are on the surface boundary
    for mesh_bdry in split_mesh_boundaries:

        if mesh_bdry[0] == mesh_bdry[-1]:
            crv_cstr = None
            for vkey in mesh_bdry:
                xyz = mesh.vertex_coordinates(vkey)
                for srf_bdry in surface_boundaries:
                    if is_point_on_curve(srf_bdry, xyz):
                        crv_cstr = srf_bdry
                        break
                if crv_cstr is not None:
                    break
            if crv_cstr is not None:
                for vkey in mesh_bdry:
                    xyz = mesh.vertex_coordinates(vkey)
                    if is_point_on_curve(crv_cstr, xyz) and vkey not in constraints:
                        constraints[vkey] = ['curve', crv_cstr]
                for i, vkey in enumerate(mesh_bdry):
                    if vkey not in constraints:
                        # find next contrained point
                        n_plus = 1
                        norm_t_plus = None
                        count = len(mesh_bdry)
                        while count > 0:
                            count -= 1
                            vkey_plus = mesh_bdry[i + n_plus - len(mesh_bdry)]
                            if vkey_plus in constraints:
                                norm_t_plus = rs.CurveNormalizedParameter(crv_cstr, rs.CurveClosestPoint(crv_cstr, mesh.vertex_coordinates(vkey_plus)))
                            else:
                                n_plus += 1
                        # find previous contrained point
                        n_minus = 1
                        norm_t_minus = None
                        count = len(mesh_bdry)
                        while count > 0:
                            count -= 1
                            vkey_minus = mesh_bdry[i - n_minus]
                            if vkey_minus in constraints:
                                norm_t_minus = rs.CurveNormalizedParameter(crv_cstr, rs.CurveClosestPoint(crv_cstr, mesh.vertex_coordinates(vkey_minus)))
                            else:
                                n_minus += 1
                        # calculate barycentric parameter and move to it
                        # dichotomy required in case of curve seam being between the two parameters
                        #print n_minus, norm_t_minus, n_plus, norm_t_plus
                        if norm_t_minus == norm_t_plus:
                            norm_t = (norm_t_plus + .5) % 1
                        elif norm_t_minus < norm_t_plus:
                            norm_t = (n_minus * norm_t_plus + n_plus * norm_t_minus) / (n_minus + n_plus)
                        else:
                            norm_t_plus += 1
                            norm_t = (n_minus * norm_t_plus + n_plus * norm_t_minus) / (n_minus + n_plus)
                        # update coordiantes
                        t = rs.CurveParameter(crv_cstr, norm_t)
                        x, y, z = rs.EvaluateCurve(crv_cstr, t)
                        attr = mesh.vertex[vkey]
                        attr['x'] = x
                        attr['y'] = y
                        attr['z'] = z
                        # store constraint
                        constraints[vkey] = ['curve', crv_cstr]
        
        else:
            for srf_bdry in surface_boundaries:
                start_xyz = mesh.vertex_coordinates(mesh_bdry[0])
                end_xyz = mesh.vertex_coordinates(mesh_bdry[-1])
                # if the mesh boundary extremities match the ones of the curve boundary...
                if is_point_on_curve(srf_bdry, start_xyz) and is_point_on_curve(srf_bdry, end_xyz):
                    # ... and if there is an intermediary mesh boundary vertex on this curve boundary (needed for two-sided boundary elements)
                    to_constrain = False
                    for vkey in mesh_bdry[1 : -1]:
                        if is_point_on_curve(srf_bdry, mesh.vertex_coordinates(vkey)):
                            to_constrain = True
                    if to_constrain:
                        crv_cstr = srf_bdry
                        for vkey in mesh_bdry:
                            xyz = mesh.vertex_coordinates(vkey)
                            if is_point_on_curve(crv_cstr, xyz) and vkey not in constraints:
                                constraints[vkey] = ['curve', crv_cstr]
                        for i, vkey in enumerate(mesh_bdry):
                            if vkey not in constraints:
                                # find next contrained point
                                n_plus = 1
                                norm_t_plus = None
                                count = len(mesh_bdry)
                                while count > 0:
                                    count -= 1
                                    vkey_plus = mesh_bdry[i + n_plus - len(mesh_bdry)]
                                    if vkey_plus in constraints:
                                        norm_t_plus = rs.CurveNormalizedParameter(crv_cstr, rs.CurveClosestPoint(crv_cstr, mesh.vertex_coordinates(vkey_plus)))
                                    else:
                                        n_plus += 1
                                # find previous contrained point
                                n_minus = 1
                                norm_t_minus = None
                                count = len(mesh_bdry)
                                while count > 0:
                                    count -= 1
                                    vkey_minus = mesh_bdry[i - n_minus]
                                    if vkey_minus in constraints:
                                        norm_t_minus = rs.CurveNormalizedParameter(crv_cstr, rs.CurveClosestPoint(crv_cstr, mesh.vertex_coordinates(vkey_minus)))
                                    else:
                                        n_minus += 1
                                # calculate barycentric parameter and move to it
                                # dichotomy required in case of curve seam being between the two parameters
                                #print n_minus, norm_t_minus, n_plus, norm_t_plus
                                if norm_t_minus == norm_t_plus:
                                    norm_t = (norm_t_plus + .5) % 1
                                elif norm_t_minus < norm_t_plus:
                                    norm_t = (n_minus * norm_t_plus + n_plus * norm_t_minus) / (n_minus + n_plus)
                                else:
                                    norm_t_plus += 1
                                    norm_t = (n_minus * norm_t_plus + n_plus * norm_t_minus) / (n_minus + n_plus)
                                # update coordiantes
                                t = rs.CurveParameter(crv_cstr, norm_t)
                                x, y, z = rs.EvaluateCurve(crv_cstr, t)
                                attr = mesh.vertex[vkey]
                                attr['x'] = x
                                attr['y'] = y
                                attr['z'] = z
                                # store constraint
                                constraints[vkey] = ['curve', crv_cstr]

    # constrain to point features
    point_constraints_keys = [geometric_key(rs.PointCoordinates(pt)) for pt in point_constraints]
    for vkey in mesh.vertices():
        xyz = mesh.vertex_coordinates(vkey)
        geom_key = geometric_key(xyz)
        if geom_key in point_constraints_keys:
            constraints[vkey] = ['point', xyz]

    # constrain to curve features
    for crv in curve_constraints:
        # extremities
        start = rs.CurveStartPoint(crv)
        start_geom_key = geometric_key(start)
        end = rs.CurveEndPoint(crv)
        end_geom_key = geometric_key(end)
        for vkey in mesh.vertices():
            xyz = mesh.vertex_coordinates(vkey)
            geom_key = geometric_key(xyz)
            if geom_key == start_geom_key:
                constraints[vkey] = ['point', xyz]
                start_key = vkey
            if geom_key == end_geom_key:
                constraints[vkey] = ['point', xyz]
                end_key = vkey
        # regular nodes
        path = [start_key]
        for nbr in mesh.vertex_neighbors(start_key):
            completed = False
            if mesh.is_vertex_on_boundary(nbr):
                continue
            path.append(nbr)
            count = len(list(mesh.vertices()))
            while count > 0:
                count -= 1
                u, v = path[-2], path[-1]
                fkey = mesh.halfedge[u][v]
                x = mesh.face_vertex_descendant(fkey, v)
                fkey = mesh.halfedge[x][v]
                w = mesh.face_vertex_descendant(fkey, v)
                path.append(w)
                if w == end_key:
                    completed = True
                    break
                elif mesh.is_vertex_on_boundary(w) or len(mesh.vertex_neighbors(w)) != 4:
                    break
            if completed:
                break
            else:
                path = [start_key]
        for vkey in path[1 : -1]:
            constraints[vkey] = ['curve', crv]

        
    # set surface constraints by default for the others
    for vkey in mesh.vertices():
        if vkey not in constraints:
            constraints[vkey] = ['surface', surface_constraint]

    # udpdate drawn mesh
    layer = 'pattern_topology'
    mesh_guid = rs.ObjectsByLayer(layer)[0]
    rs.DeleteObject(mesh_guid)
    mesh_guid = draw_mesh(mesh)
    rs.ObjectLayer(mesh_guid, layer)

    return constraints, surface_boundaries
示例#10
0
def customed_constraints(mesh, constraints, surface_boundaries, surface_constraint):

    count = 1000
    while count > 0:
        count -= 1


        all_curve_constraints = []
        for vkey, constraint in constraints.items():
            constraint_type, constraint_object = constraint
            if constraint_type == 'curve' and constraint_object not in all_curve_constraints:
                all_curve_constraints.append(constraint_object)
        n  = len(all_curve_constraints)
        rs.EnableRedraw(False)
        dots = []
        for i, crv in enumerate(all_curve_constraints):
            dot = rs.AddTextDot(i, rs.CurveMidPoint(crv))
            dots.append(dot)
            rs.ObjectColor(dot, [0, float(i) / n * 255, (n - float(i)) / n * 255])
        rs.EnableRedraw(True)

        # display  vertices to select with colour code
        vertex_colors = {}
        for vkey, constraint in constraints.items():
            contraint_type, constraint_object = constraint
            if contraint_type == 'point':
                rgb = [255, 0, 255]
            elif contraint_type == 'curve':
                i = all_curve_constraints.index(constraint_object)
                rgb = [0, float(i) / n * 255, (n - float(i)) / n * 255]
            elif contraint_type == 'surface':
                rgb = [255, 255, 255]
            else:
                rgb = [0, 0, 0]
            vertex_colors[vkey] = rgb

        artist = rhino.artists.MeshArtist(mesh, layer='mesh_artist')
        artist.clear_layer()
        
        artist.draw_vertices(color = vertex_colors)
        artist.redraw()
        vkeys = rhino.helpers.mesh_select_vertices(mesh, message = 'change vertex constraints?')
        artist.clear_layer()
        artist.redraw()
        
        rs.DeleteLayer('mesh_artist')

        if vkeys == []:
            rs.DeleteObjects(dots)
            return constraints

        rs.EnableRedraw(True)

        constraint_types = ['point', 'curve', 'surface', 'none', 'move']
        new_constraint_type = rs.GetString('constraint type?', strings = constraint_types)

        # set new point constraint
        if new_constraint_type == 'point':
            for vkey in vkeys:
                constraints[vkey] = ['point', mesh.vertex_coordinates(vkey)]

        # set new curve constraint
        elif new_constraint_type == 'curve':
            curve_constraint = rs.GetObject('curve constraint?', filter = 4)
            for vkey in vkeys:
                constraints[vkey] = ['curve', curve_constraint]

        # set new surface constraint
        elif new_constraint_type == 'surface':
            for vkey in vkeys:
                constraints[vkey] = ['surface', surface_constraint]

        # remove constraint
        elif new_constraint_type == 'none':
            for vkey in vkeys:
                constraints[vkey] = ['none', None]

        # move nodes
        elif new_constraint_type == 'move':
            x0, y0, z0 = rs.GetPoint('from...')
            x1, y1, z1 = rs.GetPoint('...to')
            for vkey in vkeys:
                attr = mesh.vertex[vkey]
                attr['x'] += x1 - x0
                attr['y'] += y1 - y0
                attr['z'] += z1 - z0
            # udpdate drawn mesh
            layer = 'pattern_topology'
            mesh_guid = rs.ObjectsByLayer(layer)[0]
            rs.DeleteObject(mesh_guid)
            mesh_guid = draw_mesh(mesh)
            rs.ObjectLayer(mesh_guid, layer)
            rs.EnableRedraw(True)
            rs.EnableRedraw(False)

        rs.DeleteObjects(dots)

        rs.EnableRedraw(False)

    return constraints
示例#11
0
for fkey in faces:
    if len(mesh.face_vertices(fkey)) > 10:
        mesh.delete_face(fkey)
print mesh.number_of_vertices() - mesh.number_of_edges(
) + mesh.number_of_faces()

smooth_mesh = mesh.copy()
smooth_mesh.cull_vertices()
constraints, surface_boundaries = define_constraints(
    smooth_mesh,
    surface_guid,
    curve_constraints=curve_features_guids,
    point_constraints=point_features_guids)
fixed_vertices = [
    vkey for vkey, constraint in constraints.items()
    if constraint[0] == 'point'
]
rs.EnableRedraw(True)
smoothing_iterations = rs.GetInteger('number of iterations for smoothing',
                                     number=20)
damping_value = rs.GetReal('damping value for smoothing', number=.5)
rs.EnableRedraw(False)
mesh_smooth_area(smooth_mesh,
                 fixed=fixed_vertices,
                 kmax=smoothing_iterations,
                 damping=damping_value,
                 callback=apply_constraints,
                 callback_args=[smooth_mesh, constraints])
rs.DeleteObjects(surface_boundaries)
smooth_mesh_guid = draw_mesh(smooth_mesh)
def editing_primal_dual(primal, default_settings, curve_constraints,
                        point_constraints):

    dx = default_settings['dx']
    dy = default_settings['dy']
    density = default_settings['density']
    pattern = default_settings['pattern']
    smoothing_iterations = default_settings['smoothing iterations']
    smoothing_damping = default_settings['smoothing damping']

    # grammar rules + propagation scheme
    rules = [
        'settings', 'face_opening', 'flat_corner_2', 'flat_corner_3',
        'flat_corner_33', 'split_35', 'split_35_diag', 'split_26',
        'simple_split', 'double_split', 'singular_boundary_1',
        'singular_boundary_2', 'clear_faces', 'move_vertices', 'stop'
    ]

    primal_guid = draw_mesh(primal)

    dense_primal = densification(primal, 1, custom=False)

    # smooth
    rs.EnableRedraw(False)
    constraints = {}
    for pt in point_constraints:
        vertex = None
        min_dist = -1
        for vkey in dense_primal.vertices_on_boundary():
            dist = distance_point_point(rs.PointCoordinates(pt),
                                        dense_primal.vertex_coordinates(vkey))
            if min_dist < 0 or min_dist > dist:
                vertex = vkey
                min_dist = dist
        constraints[vertex] = ('point', rs.PointCoordinates(pt))
    for vkey in dense_primal.vertices_on_boundary():
        if vkey not in constraints:
            curve = None
            min_dist = -1
            for crv in curve_constraints:
                vkey_guid = rs.AddPoint(dense_primal.vertex_coordinates(vkey))
                t = rs.CurveClosestPoint(crv, vkey_guid)
                pt = rs.EvaluateCurve(crv, t)
                dist = distance_point_point(
                    pt, dense_primal.vertex_coordinates(vkey))
                rs.DeleteObject(vkey_guid)
                if min_dist < 0 or min_dist > dist:
                    curve = crv
                    min_dist = dist
            constraints[vkey] = ('curve', curve)
    rs.EnableRedraw(True)

    mesh_smooth_area(dense_primal,
                     kmax=smoothing_iterations,
                     damping=smoothing_damping,
                     callback=apply_constraints,
                     callback_args=[dense_primal, constraints])

    dense_primal_guid = draw_mesh(dense_primal)
    rs.MoveObject(dense_primal_guid, [dx, 0, 0])

    dense_dual = mesh_dual(dense_primal, Mesh)
    rotate_mesh(dense_dual, pi / 2)
    rs.EnableRedraw(False)
    dense_dual_guid = draw_mesh(dense_dual)
    rs.MoveObject(dense_dual_guid, [2 * dx, 0, 0])
    rs.EnableRedraw(True)

    # start editing
    count = 1000
    while count > 0:
        count -= 1

        primal.update_default_edge_attributes()

        #ask for rule
        rule = rs.GetString('rule?', strings=rules)

        # exit
        if rule == 'stop':
            break

        elif rule == 'settings':
            density, pattern, dx, dy, smoothing_iterations, smoothing_damping = rs.PropertyListBox(
                [
                    'density', 'pattern', 'dx', 'dy', 'smoothing iterations',
                    'smoothing damping'
                ], [
                    density, pattern, dx, dy, smoothing_iterations,
                    smoothing_damping
                ],
                title='settings')
            density = float(density)
            dx = float(dx)
            dy = float(dy)
            smoothing_iterations = int(smoothing_iterations)
            smoothing_damping = float(smoothing_damping)

        # apply editing rule
        elif rule in rules:
            regular_vertices = list(primal.vertices())
            apply_rule(primal, rule)
            #if rule != 'clear_faces':
            mesh_propagation(primal, regular_vertices)

        rs.DeleteObjects(primal_guid)
        rs.DeleteObjects(dense_primal_guid)
        rs.DeleteObjects(dense_dual_guid)

        primal_guid = draw_mesh(primal)

        for fkey in primal.faces():
            if len(primal.face_vertices(fkey)) != 4:
                rs.AddPoint(primal.face_centroid(fkey))

        dense_primal = densification(primal, density, custom=False)
        dense_primal = dense_primal.to_mesh()
        dense_primal = patterning(dense_primal, pattern)
        to_del = [
            vkey for vkey in dense_primal.vertices()
            if len(dense_primal.vertex_neighbours(vkey)) == 0
        ]
        for vkey in to_del:
            del dense_primal.vertex[vkey]

        # smooth
        rs.EnableRedraw(False)
        constraints = {}
        for pt in point_constraints:
            vertex = None
            min_dist = -1
            for vkey in dense_primal.vertices_on_boundary():
                dist = distance_point_point(
                    rs.PointCoordinates(pt),
                    dense_primal.vertex_coordinates(vkey))
                if min_dist < 0 or min_dist > dist:
                    vertex = vkey
                    min_dist = dist
            constraints[vertex] = ('point', rs.PointCoordinates(pt))
        for vkey in dense_primal.vertices_on_boundary():
            if vkey not in constraints:
                curve = None
                min_dist = -1
                for crv in curve_constraints:
                    vkey_guid = rs.AddPoint(
                        dense_primal.vertex_coordinates(vkey))
                    t = rs.CurveClosestPoint(crv, vkey_guid)
                    pt = rs.EvaluateCurve(crv, t)
                    dist = distance_point_point(
                        pt, dense_primal.vertex_coordinates(vkey))
                    rs.DeleteObject(vkey_guid)
                    if min_dist < 0 or min_dist > dist:
                        curve = crv
                        min_dist = dist
                constraints[vkey] = ('curve', curve)
        rs.EnableRedraw(True)

        mesh_smooth_area(dense_primal,
                         kmax=smoothing_iterations,
                         damping=smoothing_damping,
                         callback=apply_constraints,
                         callback_args=[dense_primal, constraints])

        rs.EnableRedraw(False)
        dense_primal_guid = draw_mesh(dense_primal)
        rs.MoveObject(dense_primal_guid, [dx, 0, 0])
        rs.EnableRedraw(True)

        dense_dual = mesh_dual(dense_primal, Mesh)
        rotate_mesh(dense_dual, pi / 2)
        rs.EnableRedraw(False)
        dense_dual_guid = draw_mesh(dense_dual)
        rs.MoveObject(dense_dual_guid, [2 * dx, 0, 0])
        rs.EnableRedraw(True)

    return primal, dense_primal, dense_dual
示例#13
0
from compas_pattern.cad.rhino.utilities import draw_mesh

guids = rs.GetObjects('get meshes')
poles = rs.GetObjects('pole points', filter=1)
if poles is None:
    poles = []
poles = [rs.PointCoordinates(pole) for pole in poles]
target_length = rs.GetReal('target length for densification', number=1)

for guid in guids:
    mesh = RhinoGeometry.from_guid(guid)
    vertices, faces = mesh.get_vertices_and_faces()
    coarse_quad_mesh = Mesh.from_vertices_and_faces(vertices, faces)

    rs.EnableRedraw(False)

    vertices, face_vertices = pqm_from_mesh(coarse_quad_mesh, poles)

    coarse_quad_mesh = PseudoQuadMesh.from_vertices_and_faces(
        vertices, face_vertices)

    quad_mesh = densification(coarse_quad_mesh, target_length)

    quad_mesh_guid = draw_mesh(quad_mesh)

    #layer = 'quad_mesh'
    #rs.AddLayer(layer)
    #rs.ObjectLayer(quad_mesh_guid, layer = layer)

    rs.EnableRedraw(True)
示例#14
0
def start():
    # -2. layer structure
    layers = ['shape_and_features', 'delaunay_mesh', 'initial_coarse_quad_mesh', 'edited_coarse_quad_mesh', 'quad_mesh', 'pattern_topology', 'pattern_geometry']
    colours = [[255,0,0], [255, 255, 255], [0,0,0], [0,0,0], [200,200,200], [100,100,100], [0,0,0]]
    for layer, colour in zip(layers, colours):
        rs.AddLayer(layer)
        rs.LayerColor(layer, colour)
        objects = rs.ObjectsByLayer(layer)
        rs.DeleteObjects(objects)
    
    # -1. template
    coarse_quad_mesh = None
    if rs.GetString('use template?', defaultString = 'False', strings = ['True', 'False']) == 'True':
            vertices, faces = templating()
            coarse_quad_mesh = PseudoQuadMesh.from_vertices_and_faces(vertices, faces)
            if len(mesh_boundaries(coarse_quad_mesh)) == 0:
                coarse_quad_mesh_guid = draw_mesh(coarse_quad_mesh)
                rs.ObjectLayer(coarse_quad_mesh_guid, layer = 'initial_coarse_quad_mesh')
                rs.LayerVisible('initial_coarse_quad_mesh', visible = True)
                return
    
    # 0. input
    surface_guid = rs.GetObject('select surface', filter = 8)
    if surface_guid is None:
        box = bounding_box([coarse_quad_mesh.vertex_coordinates(vkey) for vkey in coarse_quad_mesh.vertices()])
        points = box[:4]
        surface_guid = rs.AddSrfPt(points)
    rs.ObjectColor(surface_guid, [255,0,0])
    surface_guid = rs.CopyObject(surface_guid)
    rs.ObjectLayer(surface_guid, 'shape_and_features')
    curve_features_guids = rs.GetObjects('select curve features', filter = 4)
    if curve_features_guids is None:
        curve_features_guids = []
    rs.ObjectColor(curve_features_guids, [255,0,0])
    curve_features_guids = rs.CopyObjects(curve_features_guids)
    rs.ObjectLayer(curve_features_guids, 'shape_and_features')
    point_features_guids = rs.GetObjects('select point features', filter = 1)
    if point_features_guids is None:
        point_features_guids = []
    rs.ObjectColor(point_features_guids, [255,0,0])
    point_features_guids = rs.CopyObjects(point_features_guids)
    rs.ObjectLayer(point_features_guids, 'shape_and_features')
    
    if coarse_quad_mesh is None:
        # 1. mapping
        discretisation = rs.GetReal('NURBS element discretisation', number = 1)
        rs.EnableRedraw(False)
        planar_boundary_polyline, planar_hole_polylines, planar_polyline_features, planar_point_features = mapping(discretisation, surface_guid, curve_features_guids = curve_features_guids, point_features_guids = point_features_guids)
        
        # 2. triangulation
        delaunay_mesh = triangulation(planar_boundary_polyline, holes = planar_hole_polylines, polyline_features = planar_polyline_features, point_features = planar_point_features)
        delaunay_mesh_remapped = delaunay_mesh.copy()
        remapping(delaunay_mesh_remapped, surface_guid)
        delaunay_mesh_guid = draw_mesh(delaunay_mesh_remapped)
        rs.ObjectLayer(delaunay_mesh_guid, layer = 'delaunay_mesh')
        rs.LayerVisible('delaunay_mesh', visible = True)
        
        # 3. decomposition
        medial_branches, boundary_polylines = decomposition(delaunay_mesh)
        
        # 4. extraction
        vertices, faces, edges_to_polyline = extraction(boundary_polylines, medial_branches)
        patch_decomposition = PseudoQuadMesh.from_vertices_and_faces(vertices, faces)
        
        # 5. conforming
        
        coarse_quad_mesh = conforming(patch_decomposition, delaunay_mesh, medial_branches, boundary_polylines, edges_to_polyline, planar_point_features, planar_polyline_features)
        
        # 6. remapping
        remapping(coarse_quad_mesh, surface_guid)
    
    rs.LayerVisible('delaunay_mesh', visible = False)
    coarse_quad_mesh_guid = draw_mesh(coarse_quad_mesh)
    rs.ObjectLayer(coarse_quad_mesh_guid, layer = 'initial_coarse_quad_mesh')
    rs.LayerVisible('initial_coarse_quad_mesh', visible = True)
    
    # 7. editing
    rs.EnableRedraw(False)
    rs.LayerVisible('initial_coarse_quad_mesh', visible = False)
    editing(coarse_quad_mesh)
    rs.EnableRedraw(True)
    thickening = rs.GetString('thicken?', defaultString = 'False', strings = ['True', 'False'])
    if thickening == 'True':
        thickness = rs.GetReal(message = 'thickness', number = 1, minimum = .0001, maximum = 1000)
        coarse_quad_mesh = mesh_thickening(coarse_quad_mesh, thickness = thickness)
        #closed_mesh_guid = draw_mesh(closed_mesh.to_mesh())
        #rs.ObjectLayer(closed_mesh_guid, layer = 'edited_coarse_quad_mesh')
        #rs.LayerVisible('edited_coarse_quad_mesh', visible = True)
        #return
    coarse_quad_mesh_guid = draw_mesh(coarse_quad_mesh.to_mesh())
    rs.ObjectLayer(coarse_quad_mesh_guid, layer = 'edited_coarse_quad_mesh')
    rs.LayerVisible('edited_coarse_quad_mesh', visible = True)
    
    
    # 8. densification
    rs.EnableRedraw(True)
    target_length = rs.GetReal('target length for densification', number = 1)
    quad_mesh = densification(coarse_quad_mesh, target_length)
    quad_mesh = quad_mesh.to_mesh()
    quad_mesh_guid = draw_mesh(quad_mesh)
    rs.ObjectLayer(quad_mesh_guid, layer = 'quad_mesh')
    rs.LayerVisible('edited_coarse_quad_mesh', visible = False)
    rs.LayerVisible('quad_mesh', visible = True)
    
    # 9. patterning
    rs.EnableRedraw(True)
    operators = ['dual', 'join', 'ambo', 'kis', 'needle', 'gyro']
    patterning_operator = rs.GetString('patterning operator', strings = operators)
    rs.EnableRedraw(False)    
    pattern_topology = patterning(quad_mesh, patterning_operator)
    pattern_topology_guid = draw_mesh(pattern_topology)
    rs.ObjectLayer(pattern_topology_guid, layer = 'pattern_topology')
    rs.LayerVisible('quad_mesh', visible = False)
    rs.LayerVisible('pattern_topology', visible = True)
    
    # 10. smoothing
    pattern_geometry = pattern_topology.copy()
    pattern_geometry.cull_vertices()
    rs.EnableRedraw(True)
    smoothing_iterations = rs.GetInteger('number of iterations for smoothing', number = 20)
    if smoothing_iterations == 0:
        pattern_geometry_guid = draw_mesh(pattern_geometry)
        rs.ObjectLayer(pattern_geometry_guid, layer = 'pattern_geometry')
        rs.LayerVisible('pattern_topology', visible = False)
        rs.LayerVisible('pattern_geometry', visible = True)
        return
    damping_value = rs.GetReal('damping value for smoothing', number = .5)
    rs.EnableRedraw(False)
    constraints, surface_boundaries = define_constraints(pattern_geometry, surface_guid, curve_constraints = curve_features_guids, point_constraints = point_features_guids)
    fixed_vertices = [vkey for vkey, constraint in constraints.items() if constraint[0] == 'point']
    mesh_smooth_area(pattern_geometry, fixed = fixed_vertices, kmax = smoothing_iterations, damping = damping_value, callback = apply_constraints, callback_args = [pattern_geometry, constraints])
    #vertex_keys = pattern_geometry.vertices()
    #vertices = [pattern_geometry.vertex_coordinates(vkey) for vkey in vertex_keys]
    #adjacency = [[vertex_keys.index(nbr) for nbr in pattern_geometry.vertex_neighbours(vkey)] for vkey in vertex_keys]
    #smooth_centroid_cpp(vertices, adjacency, fixed_vertices, kmax = smoothing_iterations, callback = apply_constraints, callback_args = [pattern_geometry, constraints])
    rs.DeleteObjects(surface_boundaries)
    pattern_geometry_guid = draw_mesh(pattern_geometry)
    rs.ObjectLayer(pattern_geometry_guid, layer = 'pattern_geometry')
    rs.LayerVisible('pattern_topology', visible = False)
    rs.LayerVisible('pattern_geometry', visible = True)
    
    print_metrics(pattern_geometry)