-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj2adj.py
135 lines (103 loc) · 3.2 KB
/
obj2adj.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import math
import numpy as np
import operator
import sys
from termcolor import cprint, colored
from ThreeDObject import ThreeDObject
import pickle
sprint = lambda x : cprint('\t' + x, 'magenta')
rprint = lambda x : cprint('\t\t' + x, 'red')
cyan = lambda x: cprint(x, 'cyan')
def euclidianD(p1, p2):
"""
Returns the euclidian distance between p1 and p2 points
"""
x1, y1, z1 = p1
x2, y2, z2 = p2
return math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)
def nnsearch(a, b):
"""
Returns an matrix of shortest paths from a and b
"""
g = []
for index, ii in enumerate(a):
l = []
for number, jj in enumerate(b):
l.append(euclidianD(ii, jj))
(m, i) = min((v, i) for i, v in enumerate(l))
g.append((m, i))
if m < 50: minDistList.append((m, i))
show_progress(index, len(a), 20)
return g
def store(args):
"""
Returns an array of ThreeDObjects based on filepath contained in args
"""
objs = []
for path in args[1:]:
obj = ThreeDObject(path.rstrip('.obj'), path)
objs.append(obj)
#rprint("obj => %s" %obj)
return np.asarray(objs)
def show_progress(index, end_val, bar_length):
"""
Displays the progress of the process running
"""
percent = float(index) / end_val
hashes = '#' * int(round(percent * bar_length))
spaces = ' ' * (bar_length - len(hashes))
text = "\b\tProcess: [{0}] {1}%".format(hashes + spaces, int(round(percent * 100)))
sys.stdout.write("\r {:<70}".format(text))
sys.stdout.flush()
def radix_sort(arr):
max = -1
for row in arr[0:]:
num = int(math.log10(row)) + 1
if num > max:
max = num
buckets = [[] for i in range(0, 10)] #buckets for each digit
for digit in range(0, max):
for number in arr[0:]:
buckets[number / 10**digit % 10].append(number)
del arr[:]
for buckets in buckets:
array.extend(buckets)
del bucket[:]
return arr
global dict
dict = {}
global minDistList
minDistList = []
global nnSearchList
nnSearchList = []
def compareAll(objs):
"""
Compares all elements using nnsearch stored in objs & saves it's compared data file
"""
for i1, element in enumerate(objs):
if (i1 + 1) < len(objs):
for i2, element2 in enumerate(objs[i1 + 1:]):
sprint('Comparing %s vs %s' %(str(element), str(element2)))
answer = nnsearch(element.getList(), element2.getList())
print "\n"
nnSearchList.append(answer)
a = element
b = element2
text = "%s_vs_%s" %(str(a), str(b))
pickle.dump(answer, open(text, "w"))
dict[str(a) + '->' + str(b)] = (nnSearchList, minDistList)
"""******************************************************************"""
objs = store(sys.argv)
compareAll(objs)
cyan(dict.keys())
"""
#cyan(objs)
obj1 = objs[0].getList()
obj2 = objs[1].getList()
answer = nnsearch(obj1, obj2)
#np.save("data.npy", answer)
pickle.dump(answer, open("saved_data", "w"))
cyan(answer)
rprint(radix_sort(answer))
"""