<commit_before>from __future__ import division

from itertools import chain
from math import cos, pi, sin, sqrt
from random import randint

from ..geometry.matrix import Matrix
from ..geometry.orientation import Orientation
from ..geometry.vec3 import Origin, Vec3, XAxis, YAxis, ZAxis


white=(255, 255, 255, 255)


class Geometry(object):
    '''
    Defines a 3d object as a list of vertices, and a list of faces.
    Each face is a list of indices into the vertex array, forming a
    coplanar convex ring defining the face's boundary.
    '''
    def __init__(self, vertices, faces):
        if len(vertices) > 0 and not isinstance(vertices[0], Vec3):
            vertices = [Vec3(*v) for v in vertices]
        self.vertices = vertices
        self.faces = faces


class Shape(object):

    def __init__(self, geometry, color=white, position=None, orientation=None):
        self.geometry = geometry
        self.color = color
        if type(position) is tuple:
            position = Vec3(*position)
        self.position = position
        if type(orientation) is tuple:
            orientation = Orientation(orientation)
        self.orientation = orientation

        self._vertices = None

    @property
    def vertices(self):
        if self._vertices is None:
            matrix = Matrix(self.position, self.orientation)
            self._vertices = [
                matrix.transform(vert)
                for vert in self.geometry.vertices]
        return self._vertices

    @property
    def faces(self):
        return self.geometry.faces

    @property
    def colors(self):
        return [self.color for _ in xrange(len(self.vertices))]


class MultiShape(object):

    def __init__(self, *args, **kwargs):
        self.children = list(args)
        self.position = kwargs.pop('position', None)
        self.orientation = kwargs.pop('orientation', None)
        assert kwargs == {}, 'unrecognized kwargs, %s' % (kwargs,)
        self._vertices = None
        self._colors = None
        self._faces = None

    def add(self, child):
        self.children.append(child)

    @property
    def vertices(self):
        if self._vertices is None:
            matrix = Matrix(self.position, self.orientation)
            self._vertices = [
                matrix.transform(vert)
                for shape in self.children
                for vert in shape.vertices]
        return self._vertices

    @property
    def faces(self):
        if self._faces is None:
            newfaces = []
            index_offset = 0
            for shape in self.children:
                for face in shape.faces:
                    newface = []
                    for index in face:
                        newface.append(index + index_offset)
                    newfaces.append(newface)
                index_offset += len(shape.vertices)
            self._faces = newfaces
        return self._faces

    @property
    def colors(self):
        if self._colors is None:
            self._colors = list(
                chain.from_iterable(shape.colors for shape in self.children))
        return self._colors


def Rectangle(width, height):
    vertices = [
        (-width/2, -height/2),
        (+width/2, -height/2),
        (+width/2, +height/2),
        (-width/2, +height/2),
    ]
    face = [0, 1, 2, 3]
    return Geometry(vertices, [face])


def Circle(radius):
    NUM_POINTS = 32
    verts = []
    for n in xrange(0, NUM_POINTS):
        a = n * 2 * pi / NUM_POINTS
        verts.append( (radius * cos(a), radius * sin(a)) )
    face = [n for n in xrange(0, NUM_POINTS)]
    return Geometry(verts, [face])


def Cube(edge):
    e2 = edge/2
    verts = [
        (-e2, -e2, -e2),
        (-e2, -e2, +e2),
        (-e2, +e2, -e2),
        (-e2, +e2, +e2),
        (+e2, -e2, -e2),
        (+e2, -e2, +e2),
        (+e2, +e2, -e2),
        (+e2, +e2, +e2),
    ]
    faces = [
        [0, 1, 3, 2], # left
        [4, 6, 7, 5], # right
        [7, 3, 1, 5], # front
        [0, 2, 6, 4], # back
        [3, 7, 6, 2], # top
        [1, 0, 4, 5], # bottom
    ]
    return Geometry(verts, faces)


def Tetrahedron(edge):
    size = edge / sqrt(2)/2
    vertices = [
        (+size, +size, +size),
        (-size, -size, +size),
        (-size, +size, -size),
        (+size, -size, -size), 
    ]
    faces = [ [0, 2, 1], [1, 3, 0], [2, 3, 1], [0, 3, 2] ]
    return Geometry(vertices, faces)


def Tetrahedron2(edge):
    '''
    same geometry as Tetrahedron, but at a differetn orientation
    '''
    # equilateral triangle centroid to mid-point of edge
    c2e = edge * sqrt(3) / 6
    # regular tetrahedron centroid to mid-point of face
    c2f = edge / sqrt(24)
    vertices = [
        (      0, 3*c2f,          0),
        (-edge/2,  -c2f,       -c2e),
        (+edge/2,  -c2f,       -c2e),
        (      0,  -c2f, edge*c2e*2),
    ]
    faces = [ [0, 1, 2], [0, 2, 3], [0, 1, 3] ]
    return Geometry(vertices, faces)


def RgbCubeCluster(edge, cluster_edge, cube_count):
    shape = MultiShape()
    for i in xrange(cube_count):
        r = randint(1, cluster_edge-1)
        g = randint(1, cluster_edge-1)
        b = randint(1, cluster_edge-1)
        color = (
            int(r / cluster_edge * 255),
            int(g / cluster_edge * 255),
            int(b / cluster_edge * 255),
            255)
        pos = [
            r - cluster_edge / 2,
            g - cluster_edge / 2,
            b - cluster_edge / 2,
        ]<commit_msg>no rgb cubes near the origin, to leave a space for other 'showcase' objects<commit_after>from __future__ import division

from itertools import chain
from math import cos, pi, sin, sqrt
from random import randint

from ..geometry.matrix import Matrix
from ..geometry.orientation import Orientation
from ..geometry.vec3 import Origin, Vec3, XAxis, YAxis, ZAxis


white=(255, 255, 255, 255)


class Geometry(object):
    '''
    Defines a 3d object as a list of vertices, and a list of faces.
    Each face is a list of indices into the vertex array, forming a
    coplanar convex ring defining the face's boundary.
    '''
    def __init__(self, vertices, faces):
        if len(vertices) > 0 and not isinstance(vertices[0], Vec3):
            vertices = [Vec3(*v) for v in vertices]
        self.vertices = vertices
        self.faces = faces


class Shape(object):

    def __init__(self, geometry, color=white, position=None, orientation=None):
        self.geometry = geometry
        self.color = color
        if type(position) is tuple:
            position = Vec3(*position)
        self.position = position
        if type(orientation) is tuple:
            orientation = Orientation(orientation)
        self.orientation = orientation

        self._vertices = None

    @property
    def vertices(self):
        if self._vertices is None:
            matrix = Matrix(self.position, self.orientation)
            self._vertices = [
                matrix.transform(vert)
                for vert in self.geometry.vertices]
        return self._vertices

    @property
    def faces(self):
        return self.geometry.faces

    @property
    def colors(self):
        return [self.color for _ in xrange(len(self.vertices))]


class MultiShape(object):

    def __init__(self, *args, **kwargs):
        self.children = list(args)
        self.position = kwargs.pop('position', None)
        self.orientation = kwargs.pop('orientation', None)
        assert kwargs == {}, 'unrecognized kwargs, %s' % (kwargs,)
        self._vertices = None
        self._colors = None
        self._faces = None

    def add(self, child):
        self.children.append(child)

    @property
    def vertices(self):
        if self._vertices is None:
            matrix = Matrix(self.position, self.orientation)
            self._vertices = [
                matrix.transform(vert)
                for shape in self.children
                for vert in shape.vertices]
        return self._vertices

    @property
    def faces(self):
        if self._faces is None:
            newfaces = []
            index_offset = 0
            for shape in self.children:
                for face in shape.faces:
                    newface = []
                    for index in face:
                        newface.append(index + index_offset)
                    newfaces.append(newface)
                index_offset += len(shape.vertices)
            self._faces = newfaces
        return self._faces

    @property
    def colors(self):
        if self._colors is None:
            self._colors = list(
                chain.from_iterable(shape.colors for shape in self.children))
        return self._colors


def Rectangle(width, height):
    vertices = [
        (-width/2, -height/2),
        (+width/2, -height/2),
        (+width/2, +height/2),
        (-width/2, +height/2),
    ]
    face = [0, 1, 2, 3]
    return Geometry(vertices, [face])


def Circle(radius):
    NUM_POINTS = 32
    verts = []
    for n in xrange(0, NUM_POINTS):
        a = n * 2 * pi / NUM_POINTS
        verts.append( (radius * cos(a), radius * sin(a)) )
    face = [n for n in xrange(0, NUM_POINTS)]
    return Geometry(verts, [face])


def Cube(edge):
    e2 = edge/2
    verts = [
        (-e2, -e2, -e2),
        (-e2, -e2, +e2),
        (-e2, +e2, -e2),
        (-e2, +e2, +e2),
        (+e2, -e2, -e2),
        (+e2, -e2, +e2),
        (+e2, +e2, -e2),
        (+e2, +e2, +e2),
    ]
    faces = [
        [0, 1, 3, 2], # left
        [4, 6, 7, 5], # right
        [7, 3, 1, 5], # front
        [0, 2, 6, 4], # back
        [3, 7, 6, 2], # top
        [1, 0, 4, 5], # bottom
    ]
    return Geometry(verts, faces)


def Tetrahedron(edge):
    size = edge / sqrt(2)/2
    vertices = [
        (+size, +size, +size),
        (-size, -size, +size),
        (-size, +size, -size),
        (+size, -size, -size), 
    ]
    faces = [ [0, 2, 1], [1, 3, 0], [2, 3, 1], [0, 3, 2] ]
    return Geometry(vertices, faces)


def Tetrahedron2(edge):
    '''
    same geometry as Tetrahedron, but at a differetn orientation
    '''
    # equilateral triangle centroid to mid-point of edge
    c2e = edge * sqrt(3) / 6
    # regular tetrahedron centroid to mid-point of face
    c2f = edge / sqrt(24)
    vertices = [
        (      0, 3*c2f,          0),
        (-edge/2,  -c2f,       -c2e),
        (+edge/2,  -c2f,       -c2e),
        (      0,  -c2f, edge*c2e*2),
    ]
    faces = [ [0, 1, 2], [0, 2, 3], [0, 1, 3] ]
    return Geometry(vertices, faces)


def RgbCubeCluster(edge, cluster_edge, cube_count):
    shape = MultiShape()
    for i in xrange(cube_count):
        while True:
            r = randint(1, cluster_edge-1)
            g = randint(1, cluster_edge-1)
            b = randint(1, cluster_edge-1)
            color = (
                int(r / cluster_edge * 255),
                int(g / cluster_edge * 255),
                int(b / cluster_edge * 255),
                255)
            pos = Vec3(
                r - cluster_edge / 2,
                g - cluster_edge / 2,
                b - cluster_edge / 2,
            )
            if pos.length > 8:
                break
{'fraction_non_alphanumeric': 0.12240682532555007, 'fraction_numerical': 0.027660529860799282, 'mean_word_length': 1.7729083665338645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920091', 'n_tokens_mistral': 3749, 'n_tokens_neox': 3475, 'n_words': 1024}