summaryrefslogtreecommitdiff
path: root/bin/chroma-bvh
blob: fcd20a89168af023eb8f40beadd24d18fdacd9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
#--*-python-*-
import optparse
import sys
import time

from chroma.cache import Cache
from chroma.loader import load_geometry_from_string
from chroma.log import logger, logging

def parse_bvh_id(id_str):
    '''Parse a BVH identifier given on the command line into
    a name of geometry and an optional BVH name, assuming the form:

          "geo_name:bvh_name"
          
    If no bvh_name is given, it is assumed to be "default."
    '''
    bvh_name = 'default'
    parts = id_str.split(':')
    if len(parts) > 1 and len(parts[1]) > 0:
        bvh_name = parts[1]
    geo_name = parts[0]
    return geo_name, bvh_name
        

def node_swap(cache, args):
    geo_name, bvh_name = parse_bvh_id(args[0])
    mesh_hash = cache.get_geometry_hash(geo_name)
    bvh = cache.load_bvh(mesh_hash, bvh_name)
    

def list_cmd(cache, args):
    geo_name = args[0]
    mesh_hash = cache.get_geometry_hash(geo_name)
    bvh_list = cache.list_bvh(mesh_hash)
    print 'BVHs for %s (MD5=%s):' % (geo_name, mesh_hash)
    print '\n'.join(bvh_list)

def copy(cache, args):
    geo_name, bvh_name = parse_bvh_id(args[0])
    target_bvh_name = args[1]
    mesh_hash = cache.get_geometry_hash(geo_name)
    bvh = cache.load_bvh(mesh_hash, bvh_name)

    cache.save_bvh(bvh, mesh_hash, target_bvh_name)

def remove(cache, args):
    geo_name, bvh_name = parse_bvh_id(args[0])
    mesh_hash = cache.get_geometry_hash(geo_name)
    cache.remove_bvh(mesh_hash, bvh_name)

def stat(cache, args):
    geo_name, bvh_name = parse_bvh_id(args[0])
    mesh_hash = cache.get_geometry_hash(geo_name)
    bvh = cache.load_bvh(mesh_hash, bvh_name)
    print_stat(geo_name, bvh_name, mesh_hash, bvh)

def print_stat(geo_name, bvh_name, mesh_hash, bvh):
    num_layers = bvh.layer_count()
    print '[BVH] %s:%s (MD5=%s)' % (geo_name, bvh_name, mesh_hash)
    print '-' * 72
    print 'World origin: (%f,%f,%f)' % tuple(bvh.world_coords.world_origin)
    print 'World scale factor: %f' % bvh.world_coords.world_scale
    print 'Tree degree: %d' % bvh.degree
    print 'Nodes: %d' % len(bvh)
    print 'Layers:'

    for i in xrange(num_layers):
        layer = bvh.get_layer(i)
        if len(layer) == 1:
            node_str = 'node, '
        else:
            node_str = 'nodes,'
        print '  % 3d) % 10s %s    area = % 9e' % \
            (i, len(layer), node_str, layer.area())

commands = {
    'stat' : stat,
    'list' : list_cmd,
    'copy' : copy,
    'remove' : remove,
    }
             

def main():
    logger.setLevel(logging.INFO)

    parser = optparse.OptionParser('%prog <cmd> <options>')
    parser.add_option('-c', '--cache', dest='cache', 
                      default=None, help='Chroma cache directory')
    options, args = parser.parse_args()

    if len(args) < 1:
        sys.exit(parser.format_help())

    if options.cache is None:
        cache = Cache()
    else:
        cache = Cache(options.cache)

    cmd = args[0]
    cmd_args = args[1:]

    if cmd in commands:
        commands[cmd](cache, cmd_args)
    else:
        print 'error: unknown cmd %s' % cmd
        sys.exit(1)

if __name__ == '__main__':
    main()