Example #1
0
    def _make_ribbon_mesh(self, x, y, w, h):
        signal_power = np.square(self.data)
        frames_per_pixel = int(self.frames_per_beat / HORIZ_SCALE)
        scale_factor = frames_per_pixel * 2

        pad_size = math.ceil(float(signal_power.size)/scale_factor)*scale_factor - signal_power.size
        signal_power = np.append(signal_power, np.zeros(pad_size)*np.NaN)

        print signal_power.shape
        signal_power = signal_power.reshape(-1, scale_factor)
        print signal_power.shape

        signal_power = scipy.nanmean(signal_power, axis=1)
        print signal_power.shape

        signal_power /= np.max(signal_power)

        print 'signal power', len(signal_power)
        print signal_power[100:200]
        print np.max(signal_power)

        segments = self.blah_width

        mesh = Mesh()

        # create indices
        mesh.indices = range(segments * 2 + 2)

        # create vertices with evenly spaced texture coordinates
        span = np.linspace(0.0, 1.0, segments + 1)
        verts = []

        mid_y = y + h/2
        y_scale = h/2

        idx = 0
        for s in span:
            height = y_scale * signal_power[idx]
            verts += [x + s * w, mid_y - height, s, 0, x + s * w, mid_y + height, s, 1]
            idx += 1
        mesh.vertices = verts

        # # animate a sine wave by setting the vert positions every frame:
        # theta = 3.0 * self.time
        # y = 300 + 50 * np.sin(np.linspace(theta, theta + 2 * np.pi, self.segments + 1))
        # self.mesh.vertices[5::8] = y

        # seems that you have to reassign the entire verts list in order for the change
        # to take effect.
        mesh.vertices = mesh.vertices

        # # assign texture
        # if tex_file:
        #     mesh.texture = Image(tex_file).texture

        # standard triangle strip mode
        mesh.mode = 'triangle_strip'

        return mesh
Example #2
0
def DrawTris(vertices, triangles, DrawCallback):
	
	#Arrange data in kivy internal mesh format
	vertMod = []
	for i in range(len(vertices)/2):
		vertMod.extend((vertices[i*2], vertices[(i*2)+1], 0., 0.))
	ind = []
	for tri in triangles:
		ind.extend(tri)
	
	mesh = Mesh()
	mesh.vertices = vertMod
	mesh.indices = ind
	mesh.mode = "triangles"
	DrawCallback(mesh)
 def _set_ticks(self, *args):
     mesh = self._mesh
     s_max = self.max
     s_min = self.min
     if self.ticks_major and s_max > s_min:
         if not mesh:
             mesh = Mesh(mode='lines', group=str('TickMarker%d' % id(self)))
             self._mesh = mesh
             self.canvas.add(mesh)
         indices = mesh.indices
         vertices = mesh.vertices
         compute_ticks(indices, vertices, self.ticks_minor,
                       self.ticks_major, self.padding, self.max,
                       self.min, self.log, self.orientation == 'horizontal',
                       tuple(self.size), tuple(self.pos))
         mesh.vertices = vertices
         mesh.indices = indices
     else:
         if mesh:
             self.canvas.remove_group(str('TickMarker%d' % id(self)))
             self._mesh = None
Example #4
0
    def _set_ticks(self, *args):
        mesh = self._mesh
        s_max = self.max
        s_min = self.min
        if self.ticks_major and s_max > s_min:
            if not mesh:
                mesh = Mesh(mode='lines', group=str('TickMarker%d' % id(self)))
                self._mesh = mesh
                self.canvas.add(mesh)
            indices = mesh.indices
            vertices = mesh.vertices
            ticks_minor = self.ticks_minor
            ticks_major = self.ticks_major
            padding = self.padding
            log = self.log

            if log:
                # count the decades in min - max. This is in actual decades,
                # not logs.
                n_decades = floor(s_max - s_min)
                # for the fractional part of the last decade, we need to
                # convert the log value, x, to 10**x but need to handle
                # differently if the last incomplete decade has a decade
                # boundary in it
                if floor(s_min + n_decades) != floor(s_max):
                    n_decades += 1 - (10 ** (s_min + n_decades + 1) - 10 **
                                      s_max) / 10 ** floor(s_max + 1)
                else:
                    n_decades += ((10 ** s_max - 10 ** (s_min + n_decades)) /
                                  10 ** floor(s_max + 1))
                # this might be larger than what is needed, but we delete
                # excess later
                n_ticks = n_decades / float(ticks_major)
                n_ticks = int(floor(n_ticks * (ticks_minor if ticks_minor >=
                                               1. else 1.0))) + 2
                # in decade multiples, e.g. 0.1 of the decade, the distance
                # between ticks
                decade_dist = ticks_major / float(ticks_minor if
                                                  ticks_minor else 1.0)

                points = [0] * n_ticks
                points[0] = (0., True)  # first element is always a major tick
                k = 1  # position in points
                # because each decade is missing 0.1 of the decade, if a tick
                # falls in < min_pos skip it
                min_pos = 0.1 - 0.00001 * decade_dist
                s_min_low = floor(s_min)
                # first real tick location. value is in fractions of decades
                # from the start we have to use decimals here, otherwise
                # floating point inaccuracies results in bad values
                start_dec = ceil((10 ** Decimal(s_min - s_min_low - 1)) /
                                 Decimal(decade_dist)) * decade_dist
                count_min = (0 if not ticks_minor else
                             floor(start_dec / decade_dist) % ticks_minor)
                start_dec += s_min_low
                count = 0  # number of ticks we currently have passed start
                while True:
                    # this is the current position in decade that we are.
                    # e.g. -0.9 means that we're at 0.1 of the 10**ceil(-0.9)
                    # decade
                    pos_dec = start_dec + decade_dist * count
                    pos_dec_low = floor(pos_dec)
                    diff = pos_dec - pos_dec_low
                    zero = abs(diff) < 0.001 * decade_dist
                    if zero:
                        # the same value as pos_dec but in log scale
                        pos_log = pos_dec_low
                    else:
                        pos_log = log10((pos_dec - pos_dec_low
                                         ) * 10 ** ceil(pos_dec))
                    if pos_log > s_max:
                        break
                    count += 1
                    if zero or diff >= min_pos:
                        points[k] = (pos_log - s_min, ticks_minor
                                     and not count_min % ticks_minor)
                        k += 1
                    count_min += 1
                del points[k:]
                n_ticks = len(points)
            else:
                # distance between each tick
                tick_dist = ticks_major / float(ticks_minor if
                                                ticks_minor else 1.0)
                n_ticks = int(floor((s_max - s_min) / tick_dist) + 1)

            count = len(indices) // 2
            # adjust mesh size
            if count > n_ticks:
                del vertices[n_ticks * 8:]
                del indices[n_ticks * 2:]
            elif count < n_ticks:
                    vertices.extend([0] * (8 * (n_ticks - count)))
                    indices.extend(range(2 * count, 2 * n_ticks))

            if self.orientation == 'horizontal':
                center = self.center_y
                axis_dist = self.width
                start = self.x + padding
                above, below, dist1, dist2 = 1, 5, 0, 4
            else:
                center = self.center_x
                axis_dist = self.height
                start = self.y + padding
                above, below, dist1, dist2 = 0, 4, 1, 5
            # now, the physical distance between ticks
            tick_dist = (axis_dist - 2 * padding
                         ) / float(s_max - s_min) * (tick_dist if not log
                                                     else 1.0)
            # amounts that ticks extend above/below axis
            maj_plus = center + metrics.dp(12)
            maj_minus = center - metrics.dp(12)
            min_plus = center + metrics.dp(6)
            min_minus = center - metrics.dp(6)

            if log:
                for k in range(0, n_ticks):
                    m = k * 8
                    vertices[m + dist1] = start + points[k][0] * tick_dist
                    vertices[m + dist2] = vertices[m + dist1]
                    if not points[k][1]:
                        vertices[m + above] = min_plus
                        vertices[m + below] = min_minus
                    else:
                        vertices[m + above] = maj_plus
                        vertices[m + below] = maj_minus
            else:
                for k in range(0, n_ticks):
                    m = k * 8
                    vertices[m + dist1] = start + k * tick_dist
                    vertices[m + dist2] = vertices[m + dist1]
                    if ticks_minor and k % ticks_minor:
                        vertices[m + above] = min_plus
                        vertices[m + below] = min_minus
                    else:
                        vertices[m + above] = maj_plus
                        vertices[m + below] = maj_minus
            mesh.vertices = vertices
            mesh.indices = indices
        else:
            if mesh:
                self.canvas.remove_group(str('TickMarker%d' % id(self)))
                self._mesh = None