def get_path_instructions(self, gc, polygons, closed=False, rgbFace=None):
     '''With a graphics context and a set of polygons it returns a list
        of InstructionGroups required to render the path.
     '''
     instructions_list = []
     points_line = []
     for polygon in polygons:
         for x, y in polygon:
             x = x + self.widget.x
             y = y + self.widget.y
             points_line += [float(x), float(y), ]
         tess = Tesselator()
         tess.add_contour(points_line)
         if not tess.tesselate():
             Logger.warning("Tesselator didn't work :(")
             return
         newclip = self.handle_clip_rectangle(gc, x, y)
         if newclip > -1:
             instructions_list.append((self.clip_rectangles[newclip],
                     self.get_graphics(gc, tess, points_line, rgbFace,
                                       closed=closed)))
         else:
             instructions_list.append((self.widget,
                     self.get_graphics(gc, tess, points_line, rgbFace,
                                       closed=closed)))
     return instructions_list
Exemple #2
0
    def _geojson_part_geometry(self, geometry, properties):
        from kivy.graphics import Mesh, Line, Color
        from kivy.graphics.tesselator import Tesselator, WINDING_ODD, TYPE_POLYGONS
        from kivy.utils import get_color_from_hex
        from kivy.metrics import dp
        tp = geometry["type"]
        graphics = []
        if tp == "Polygon":
            tess = Tesselator()
            for c in geometry["coordinates"]:
                xy = list(self._lonlat_to_xy(c))
                xy = flatten(xy)
                tess.add_contour(xy)

            tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

            color = self._get_color_from(properties.get("color", "FF000088"))
            graphics.append(Color(*color))
            for vertices, indices in tess.meshes:
                graphics.append(Mesh(vertices=vertices,
                                     indices=indices,
                                     mode="triangle_fan"))

        elif tp == "LineString":
            stroke = get_color_from_hex(properties.get("stroke", "#ff000088"))
            stroke_width = dp(properties.get("stroke-width", 2))
            xy = list(self._lonlat_to_xy(geometry["coordinates"]))
            xy = flatten(xy)
            graphics.append(Color(*stroke))
            graphics.append(Line(points=xy, width=stroke_width))

        return graphics
 def get_path_instructions(self, gc, polygons, closed=False, rgbFace=None):
     '''With a graphics context and a set of polygons it returns a list
        of InstructionGroups required to render the path.
     '''
     instructions_list = []
     points_line = []
     for polygon in polygons:
         for x, y in polygon:
             x = x + self.widget.x
             y = y + self.widget.y
             points_line += [
                 float(x),
                 float(y),
             ]
         tess = Tesselator()
         tess.add_contour(points_line)
         if not tess.tesselate():
             Logger.warning("Tesselator didn't work :(")
             return
         newclip = self.handle_clip_rectangle(gc, x, y)
         if newclip > -1:
             instructions_list.append((self.clip_rectangles[newclip],
                                       self.get_graphics(gc,
                                                         tess,
                                                         points_line,
                                                         rgbFace,
                                                         closed=closed)))
         else:
             instructions_list.append((self.widget,
                                       self.get_graphics(gc,
                                                         tess,
                                                         points_line,
                                                         rgbFace,
                                                         closed=closed)))
     return instructions_list
Exemple #4
0
class HexTile(Widget):
    polygon = ListProperty([])
    tesselator = ObjectProperty(Tesselator())
    vertices = ListProperty([])
    indices = ListProperty([])
    is_hovered = BooleanProperty(False)

    def __init__(self, radius, **kwargs):
        super().__init__(**kwargs)
        self._radius = radius
        for alpha in (0, 60, 120, 180, 240, 300):
            self.polygon.append(cos(radians(alpha)) * radius + self.x)
            self.polygon.append(sin(radians(alpha)) * radius + self.y)

        self.tesselator.add_contour(self.polygon)
        if not self.tesselator.tesselate():
            raise ValueError('Unable to tesselate.')

        self.vertices, self.indices = self.tesselator.meshes[0]

        #with self.canvas:
        #    Color(.1, .1, .1)
        #    for vertices, indices in tess.meshes:
        #        Mesh(vertices=vertices, indices=indices, mode='triangle_fan')

        #Color(.46, .46, .6)
        #Line(points=self._polygon + self._polygon[:2], width=1.2)

    def collide_point(self, x, y):
        x, y = self.to_local(x, y)
        return _point_inside_polygon(x, y, self.polygon)
Exemple #5
0
    def build(self, *args):
        self.canvas.clear()
        if not self.points:
            return

        with self.canvas:
            if self.fill:
                Color(rgba=self.fill_color)
                t = Tesselator()
                t.add_contour(self.points)
                if t.tesselate:
                    for vertices, indices in t.meshes:
                        Mesh(vertices=vertices,
                             indices=indices,
                             mode='triangle fan')
                    else:
                        print("Mesh didn't tesselate!")
            if self.stroke:
                Color(rgba=self.color)
                Line(points=self.points, width=self.line_width)
Exemple #6
0
    def _geojson_part_geometry(self, geometry):
        from kivy.graphics import Mesh, Line, Color
        from kivy.graphics.tesselator import Tesselator, WINDING_ODD, TYPE_POLYGONS
        tp = geometry["type"]
        graphics = []
        if tp == "Polygon":
            tess = Tesselator()
            for c in geometry["coordinates"]:
                xy = list(self._lonlat_to_xy(c))
                xy = flatten(xy)
                tess.add_contour(xy)

            tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

            graphics.append(Color(1, 0, 0, .5))
            for vertices, indices in tess.meshes:
                graphics.append(Mesh(
                    vertices=vertices, indices=indices,
                    mode="triangle_fan"))

        return graphics
Exemple #7
0
    def _geojson_part_geometry(self, geometry, properties):
        from kivy.graphics import Mesh, Line, Color
        from kivy.graphics.tesselator import Tesselator, WINDING_ODD, TYPE_POLYGONS
        from kivy.utils import get_color_from_hex
        from kivy.metrics import dp
        tp = geometry["type"]
        graphics = []
        if tp == "Polygon":
            tess = Tesselator()
            for c in geometry["coordinates"]:
                xy = list(self._lonlat_to_xy(c))
                xy = flatten(xy)
                tess.add_contour(xy)

            tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

            color = self._get_color_from(properties.get("color", "FF000088"))
            graphics.append(Color(*color))
            for vertices, indices in tess.meshes:
                graphics.append(
                    Mesh(vertices=vertices,
                         indices=indices,
                         mode="triangle_fan"))

        elif tp == "LineString":
            stroke = get_color_from_hex(properties.get("stroke", "#ffffff"))
            stroke_width = dp(properties.get("stroke-width"))
            xy = list(self._lonlat_to_xy(geometry["coordinates"]))
            xy = flatten(xy)
            graphics.append(Color(*stroke))
            graphics.append(Line(points=xy, width=stroke_width))

        return graphics
Exemple #8
0
    def show_precincts(self):
        precinct_graphics = self.precinct_graphics = {}
        with self.canvas:
            PushMatrix()
            Translate(self.focus_region_width, 0)
            for precinct in self.voronoi_mapping.precincts:
                assert len(precinct.boundary) >= 6
                tess = Tesselator()
                tess.add_contour(precinct.boundary)
                tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

                graphics = [
                    Color(rgba=(0, 0, 0, 1))]
                for vertices, indices in tess.meshes:
                    graphics.append(
                        Mesh(
                            vertices=vertices, indices=indices,
                            mode="triangle_fan"))

                graphics.append(
                    Color(rgba=(0, 1, 0, 1)))
                graphics.append(
                    Line(points=precinct.boundary, width=1))
                precinct_graphics[precinct] = graphics
            PopMatrix()
Exemple #9
0
    def _geojson_part_geometry(self, geometry, properties):
        tp = geometry["type"]
        graphics = []
        if tp == "Polygon":
            tess = Tesselator()
            for c in geometry["coordinates"]:
                xy = list(self._lonlat_to_xy(c))
                xy = flatten(xy)
                tess.add_contour(xy)

            tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

            color = self._get_color_from(properties.get("style").get("stroke", "FF000088"))
            graphics.append(Color(*color))
            for vertices, indices in tess.meshes:
                graphics.append(
                    Mesh(
                        vertices=vertices,
                        indices=indices,
                        mode="triangle_fan"))

        elif tp == "LineString":
            stroke = get_color_from_hex(properties.get("style").get("stroke", "#ffffff"))
            print "stroke: " + `stroke`
            #storke =  [0.0, 0.0, 0.0, 1]

            print 'properties.get("width") :' + `properties.get("style").get("width")`
            stroke_width = dp(properties.get("style").get("width"))
            print "stroke_width: " + `stroke_width`
            xy = list(self._lonlat_to_xy(geometry["coordinates"]))
            xy = flatten(xy)
            graphics.append(Color(*stroke))
            graphics.append(Line(points=xy, width=stroke_width))

        return graphics
Exemple #10
0
def _(obj, world):
    tess = Tesselator()
    vertices = []
    for x, y in obj.vertices:
        vertices.append(x)
        vertices.append(y)
    tess.add_contour(vertices)
    if not tess.tesselate():
        raise Exception('Tesselator didn\'t work')
    with world.widget.canvas:
        Color(*obj.color.rgbf)
        PushMatrix()
        translation = Translate(0, 0)
        rotation = Rotate(axis=(0, 0, 1), origin=(obj.pos.x, obj.pos.y, 0))
        for vertices, indices in tess.meshes:
            Mesh(vertices=vertices, indices=indices, mode="triangle_fan")
        PopMatrix()
        world.objects.append(
            KivyObjectWrapper(
                obj,
                tess,
                translation,
                rotation))
Exemple #11
0
 def build_mesh(self):
     tess = Tesselator()
     tess.add_contour(self.points)
     tess.tesselate()
     with self.canvas:
         for vertices, indices in tess.meshes:
             self.canvas.add(Mesh(vertices=vertices,
                                  indices=indices,
                                  mode="triangle_fan"))
Exemple #12
0
    def build(self):
        tess = Tesselator()
        count = 0
        for shape in self.shapes:
            if len(shape) >= 3:
                tess.add_contour(shape)
                count += 1
        if self.shape and len(self.shape) >= 3:
            tess.add_contour(self.shape)
            count += 1
        if not count:
            return
        print("Tesselate {} shapes".format(count))
        ret = tess.tesselate(WINDING_ODD, TYPE_POLYGONS)
        print("Result: {}".format(ret))
        print("Vertex count: {}".format(tess.vertex_count))
        print("Element count: {}".format(tess.element_count))

        self.canvas.after.clear()

        debug = self.ids.debug.state == "down"
        if debug:
            from random import random
            with self.canvas.after:
                c = 0
                for vertices, indices in tess.meshes:
                    Color(c, 1, 1, mode="hsv")
                    c += 0.3
                    indices = [0]
                    for i in range(1, len(vertices) / 4):
                        if i > 0:
                            indices.append(i)
                        indices.append(i)
                        indices.append(0)
                        indices.append(i)
                    indices.pop(-1)
                    Mesh(vertices=vertices, indices=indices, mode="lines")
        else:
            with self.canvas.after:
                Color(1, 1, 1, 1)
                for vertices, indices in tess.meshes:
                    Mesh(vertices=vertices,
                         indices=indices,
                         mode="triangle_fan")

        self.ids.status.text = "Vertex: {} - Elements: {}".format(
            tess.vertex_count, tess.element_count)
Exemple #13
0
    def build(self):
        tess = Tesselator()
        count = 0
        for shape in self.shapes:
            if len(shape) >= 3:
                tess.add_contour(shape)
                count += 1
        if self.shape and len(self.shape) >= 3:
            tess.add_contour(self.shape)
            count += 1
        if not count:
            return
        print("Tesselate {} shapes".format(count))
        ret = tess.tesselate(WINDING_ODD, TYPE_POLYGONS)
        print("Result: {}".format(ret))
        print("Vertex count: {}".format(tess.vertex_count))
        print("Element count: {}".format(tess.element_count))

        self.canvas.after.clear()

        debug = self.ids.debug.state == "down"
        if debug:
            from random import random
            with self.canvas.after:
                c = 0
                for vertices, indices in tess.meshes:
                    Color(c, 1, 1, mode="hsv")
                    c += 0.3
                    indices = [0]
                    for i in range(1, len(vertices) / 4):
                        if i > 0:
                            indices.append(i)
                        indices.append(i)
                        indices.append(0)
                        indices.append(i)
                    indices.pop(-1)
                    Mesh(vertices=vertices, indices=indices, mode="lines")
        else:
            with self.canvas.after:
                Color(1, 1, 1, 1)
                for vertices, indices in tess.meshes:
                    Mesh(vertices=vertices, indices=indices, mode="triangle_fan")

        self.ids.status.text = "Vertex: {} - Elements: {}".format(
            tess.vertex_count, tess.element_count)
Exemple #14
0
def make_mesh_list(polygon):
    tess = Tesselator()
    tess.add_contour(polygon)
    tess.tesselate()
    return tess