Esempio n. 1
0
def kagome_polyedge_colouring(kagome):

    polyedges = kagome.polyedge_data

    edge_to_polyedge_index = {vkey: {} for vkey in kagome.vertices()}
    for i, polyedge in enumerate(polyedges):
        for u, v in pairwise(polyedge):
            edge_to_polyedge_index[u][v] = i
            edge_to_polyedge_index[v][u] = i

    vertices = [
        centroid_points([kagome.vertex_coordinates(vkey) for vkey in polyedge])
        for polyedge in polyedges
    ]

    edges = []
    for idx, polyedge in enumerate(polyedges):
        for vkey in polyedge:
            for vkey_2 in kagome.vertex_neighbors(vkey):
                idx_2 = edge_to_polyedge_index[vkey][vkey_2]
                if idx_2 != idx and idx < idx_2 and (idx, idx_2) not in edges:
                    edges.append((idx, idx_2))

    polyedge_network = Network.from_nodes_and_edges(vertices, edges)

    key_to_colour = vertex_coloring(polyedge_network.adjacency)

    return [key_to_colour[key] for key in sorted(key_to_colour.keys())]
Esempio n. 2
0
def quad_mesh_strip_n_coloring(quad_mesh):
    """Color the strips of a quad mesh with a minimum number of colors without overlapping strips with the same color.

    Parameters
    ----------
    quad_mesh : QuadMesh
        A quad mesh.

    Returns
    -------
    dict
        A dictionary with strip keys pointing to colors.
    """

    vertices, edges = quad_mesh.strip_graph()
    return vertex_coloring(adjacency_from_edges(edges))
Esempio n. 3
0
def graph_colourability(graph):
    """Compute vertex colourability of a graph.

	Parameters
	----------
	graph : Network
		A graph.

	Returns
	-------
	int
		Colourability.

	"""

    return max(list(vertex_coloring(graph.adjacency).values())) + 1
Esempio n. 4
0
def mesh_vertex_n_coloring(mesh):
    """Color the vertices of a mesh with a minimum number of colors without adjacent vertices with the same color.

	Parameters
	----------
	mesh : Mesh
		A mesh.

	Returns
	-------
	dict
		A dictionary with vertex keys pointing to colors.
		
	"""

    return vertex_coloring(mesh.adjacency)
Esempio n. 5
0
def quad_mesh_polyedge_n_coloring(quad_mesh):
    """Color the polyedges of a quad mesh with a minimum number of colors without overlapping polyedges with the same color.
    Polyedges connected by their extremities, which are singularities, do not count as overlapping.

    Parameters
    ----------
    quad_mesh : QuadMesh
        A quad mesh.

    Returns
    -------
    dict
        A dictionary with polyedge keys pointing to colors.
    """

    vertices, edges = quad_mesh.polyedge_graph()
    return vertex_coloring(adjacency_from_edges(edges))
Esempio n. 6
0
def mesh_face_n_coloring(mesh):
    """Color the faces of a mesh with a minimum number of colors without adjacent faces with the same color.

	Parameters
	----------
	mesh : Mesh
		A mesh.

	Returns
	-------
	dict
		A dictionary with face keys pointing to colors.
		
	"""

    edges = [(mesh.halfedge[u][v], mesh.halfedge[v][u])
             for u, v in mesh.edges() if not mesh.is_edge_on_boundary(u, v)]
    return vertex_coloring(adjacency_from_edges(edges))
# mesh representation of an icosahedron
# and its dual
mesh = Mesh.from_polyhedron(20)
dual = mesh.dual()

# mesh transformation for drawing various representations
# of the mesh in non-overlapping way
# the mesh will be transformed in-place
# therefore the same transformation can be applied consecutively
bbox = mesh.bounding_box_xy()
dx = bbox[1][0] - bbox[0][0]
X = Translation([1.5 * dx, 0, 0])

# vertex coloring
key_color = vertex_coloring(mesh.adjacency)
c = len(set(key_color.values()))
colors = Colormap(list(range(c)), 'rgb')
vertexcolor = {key: colors(key_color[key]) for key in mesh.vertices()}

# face coloring
# which is the same as a vertex coloring of the dual
key_color = vertex_coloring(dual.adjacency)
c = len(set(key_color.values()))
colors = Colormap(list(range(c)), 'rgb')
facecolor = {key: colors(key_color[key]) for key in dual.vertices()}

# the artist for drawing various versions of the mesh
artist = MeshArtist(mesh)

# mesh
import compas
from compas.datastructures import Network
from compas.plotters import NetworkPlotter
from compas.topology import vertex_coloring

network = Network.from_obj(compas.get('grid_irregular.obj'))

key_color = vertex_coloring(network.adjacency)
colors = ['#ff0000', '#00ff00', '#0000ff']

plotter = NetworkPlotter(network)

plotter.draw_vertices(facecolor={key: colors[key_color[key]] for key in network.vertices()})
plotter.draw_edges()

plotter.show()
Esempio n. 9
0
                    if mesh.is_vertex_on_boundary(
                            polyedge_1[0]) and mesh.is_vertex_on_boundary(
                                polyedge_1[1]) and mesh.is_vertex_on_boundary(
                                    polyedge_2[0]
                                ) and mesh.is_vertex_on_boundary(
                                    polyedge_2[1]):
                        continue
                    idx_0 = init_polyedges.index(polyedge_1)
                    idx_1 = init_polyedges.index(polyedge_2)
                    edges.append([idx_0, idx_1])

graph = Network.from_vertices_and_edges(vertices, edges)

adjacency = graph.adjacency

key_to_colour = vertex_coloring(adjacency)

colours = []
for key, colour in key_to_colour.items():
    if colour not in colours:
        colours.append(colour)

for colour in colours:
    rs.AddLayer(name=str(colour))

if rs.IsLayer(str(0)):
    rs.LayerColor(str(0), [255, 0, 0])
if rs.IsLayer(str(1)):
    rs.LayerColor(str(1), [0, 0, 255])
if rs.IsLayer(str(2)):
    rs.LayerColor(str(2), [0, 255, 0])
Esempio n. 10
0
	# 	(1, 2),
	# 	(2, 3),
	# 	(3, 4),
	# 	(4, 5),
	# 	(5, 0),
	# 	(0, 3),
	# 	(2, 5)
	# ]

	# adjacency = adjacency_from_edges(edges)
	# print(adjacency)
	
	# t0 = time.time()
	# print(is_adjacency_two_colorable(adjacency))
	# t1 = time.time()
	# print(t1 - t0)

	mesh = Mesh.from_obj(compas.get('faces.obj'))

	t0 = time.time()
	print(is_adjacency_two_colorable(mesh.adjacency))
	t1 = time.time()
	print(t1 - t0)

	t0 = time.time()
	print(vertex_coloring(mesh.adjacency))
	t1 = time.time()
	print(t1 - t0)