<commit_before>#!/usr/bin/python
# File:        dst-objtobytes.py
# Description: dst-objtobytes
# Created:     2017-08-20


import sys
import argparse

from distance.bytes import DstBytes
from distance.level import WedgeGS, Group


def parse_floats(s):
    return [float(f) for f in s.split(' ') if f]


class ObjFile(object):

    def __init__(self, **kw):
        self.__dict__.update(kw)


def read_obj(file):
    verts = []
    # texs = []
    # normals = []
    faces = []
    for line in file:
        left, _, vals = line.strip().partition(' ')
        if left == 'v':
            verts.append(parse_floats(vals))
        # elif left == 'vt':
        #     texs.append(parse_floats(vals))
        # elif left == 'vn':
        #     normals.append(parse_floats(vals))
        elif left == 'f':
            face = []
            for i in vals.split(' '):
                parts = i.split('/')
                if parts[0]:
                    face.append(int(parts[0]))
            faces.append(face)
    return ObjFile(vertices=verts, faces=faces)


def obj_to_simples(obj, dest):
    from distance.transform import create_triangle_simples
    import numpy as np, quaternion

    scale = 16

    def mkwedge(**kw):
        return WedgeGS(
            mat_color=(.8, .8, .8, 1),
            mat_emit=(0, 0, 0, 0),
            mat_reflect=(0, 0, 0, 0),
            image_index=17,
            emit_index=17,
            world_mapped=True,
            disable_diffuse=True,
            disable_bump=True,
            disable_collision=True,
            **kw)

    vertices = np.array(obj.vertices) * scale
    slen = len(dest)
    n_tris = 0
    for i, face in enumerate(obj.faces):
        it = iter(face)
        first = vertices[next(it) - 1]
        prev = vertices[next(it) - 1]
        for index in it:
            n_tris += 1
            vert = vertices[index - 1]
            create_triangle_simples(np.array([first, prev, vert]), dest,
                                    cls=mkwedge)
            prev = vert
            sys.stdout.write(f"\rgenerating... created {len(dest) - slen} "
                             f"simples for {n_tris} triangles")
    print()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("OBJIN", nargs=1, help=".obj filename to read")
    parser.add_argument("BYTESOUT", nargs=1, help=".bytes filename to write")
    args = parser.parse_args()

    with open(args.OBJIN[0]) as f:
        obj = read_obj(f)
    print(f"loaded {len(obj.vertices)} vertices and {len(obj.faces)} faces")

    objs = []

    print()
    obj_to_simples(obj, objs)

    group = Group(subobjects=objs)
    with open(args.BYTESOUT[0], 'wb') as f:
        print(f"Writing {args.BYTESOUT[0]}...")
        dbytes = DstBytes(f)
        group.write(dbytes)
        print(f"{dbytes.pos} bytes written")

    return 0


if __name__ == '__main__':
    exit(main())


# vim:set sw=4 ts=8 sts=4 et sr ft=python fdm=marker tw=0:
<commit_msg>add object scale,name args<commit_after>#!/usr/bin/python
# File:        dst-objtobytes.py
# Description: dst-objtobytes
# Created:     2017-08-20


import sys
import argparse

from distance.bytes import DstBytes
from distance.level import WedgeGS, Group


def parse_floats(s):
    return [float(f) for f in s.split(' ') if f]


class ObjFile(object):

    def __init__(self, **kw):
        self.__dict__.update(kw)


def read_obj(file):
    verts = []
    # texs = []
    # normals = []
    faces = []
    for line in file:
        left, _, vals = line.strip().partition(' ')
        if left == 'v':
            verts.append(parse_floats(vals))
        # elif left == 'vt':
        #     texs.append(parse_floats(vals))
        # elif left == 'vn':
        #     normals.append(parse_floats(vals))
        elif left == 'f':
            face = []
            for i in vals.split(' '):
                parts = i.split('/')
                if parts[0]:
                    face.append(int(parts[0]))
            faces.append(face)
    return ObjFile(vertices=verts, faces=faces)


def obj_to_simples(obj, dest, scale=1):
    from distance.transform import create_triangle_simples
    import numpy as np, quaternion

    def mkwedge(**kw):
        return WedgeGS(
            mat_color=(.8, .8, .8, 1),
            mat_emit=(0, 0, 0, 0),
            mat_reflect=(0, 0, 0, 0),
            image_index=17,
            emit_index=17,
            world_mapped=True,
            disable_diffuse=True,
            disable_bump=True,
            **kw)

    vertices = np.array(obj.vertices) * scale
    slen = len(dest)
    n_tris = 0
    for i, face in enumerate(obj.faces):
        it = iter(face)
        first = vertices[next(it) - 1]
        prev = vertices[next(it) - 1]
        for index in it:
            n_tris += 1
            vert = vertices[index - 1]
            create_triangle_simples(np.array([first, prev, vert]), dest,
                                    cls=mkwedge)
            prev = vert
            sys.stdout.write(f"\rgenerating... created {len(dest) - slen} "
                             f"simples for {n_tris} triangles")
    print()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--name", help="Custom object name")
    parser.add_argument("--scale", type=int, help="Set object scale")
    parser.add_argument("OBJIN", nargs=1, help=".obj filename to read")
    parser.add_argument("BYTESOUT", nargs=1, help=".bytes filename to write")
    parser.set_defaults(scale=16)
    args = parser.parse_args()

    with open(args.OBJIN[0]) as f:
        obj = read_obj(f)
    print(f"loaded {len(obj.vertices)} vertices and {len(obj.faces)} faces")

    objs = []

    print()
    obj_to_simples(obj, objs, scale=args.scale)

    group = Group(subobjects=objs, group_name=args.name)
    with open(args.BYTESOUT[0], 'wb') as f:
        print(f"Writing {args.BYTESOUT[0]}...")
        dbytes = DstBytes(f)
        group.write(dbytes)
        print(f"{dbytes.pos} bytes written")

    return 0


if __name__ == '__main__':
    exit(main())


# vim:set sw=4 ts=8 sts=4 et sr ft=python fdm=marker tw=0:

{'fraction_non_alphanumeric': 0.13464373464373464, 'fraction_numerical': 0.01425061425061425, 'mean_word_length': 2.0606516290726815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '461494', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1989, 'n_words': 461}