vertices = [
  22,    0, -119,  -21,    0, -119,   48,   42, -103,   26,   78,
 -89,  -25,   78,  -89,   90,   26,  -77,  120,   22,    0,   90,
 -25,  -77,  104,  -47,  -41,   26,  -77,  -89,   48,  -41, -103,
-103,  -47,  -41,  -89,  -25,  -77,  -41, -103,  -47,  -25,  -77,
 -89,  -47,  -41, -103,  -89,   26,  -77,  -77,   90,  -25,  -47,
  42, -103,    0,  120,   22,  -41,  104,   48,  -41,  104,  -47,
 -77,   90,   26,    0,  120,  -21,   42,  104,  -47,   78,   90,
  26,  -77,  -89,  -25,   42, -103,  -47,   78,  -89,  -25,    0,
-119,  -21,    0, -119,   22,   78,  -89,   26,   22,    0,  120,
 -47,   42,  104,   26,   78,   90,   48,   42,  104,   42,  104,
  48,   90,   26,   78,  104,   48,   42,   90,  -25,   78,  120,
 -21,    0,  104,  -47,   42,   48,  -41,  104,   42, -103,   48,
 -25,  -77,   90,  -47,  -41,  104,   26,  -77,   90,  -21,    0,
 120,  -89,  -25,   78,  -41, -103,   48,  -77,  -89,   26,  -89,
  26,   78, -119,   22,    0, -103,  -47,   42, -119,  -21,    0,
-103,   48,   42,  -25,   78,   90,  104,   48,  -41,   78,   90,
 -25,   90,   26,  -77,   48,   42, -103,   90,  -25,  -77,   48,
 -41, -103,  -25,   78,  -89,  -41,  104,  -47,    0,  120,  -21,
  26,   78,  -89,   42,  104,  -47,  -47,  -41, -103,  -89,  -25,
 -77,  -89,   26,  -77,  -41, -103,  -47,  -25,  -77,  -89,   26,
 -77,  -89,   42, -103,  -47,    0, -119,  -21,   90,  -25,   78,
  90,   26,   78,   48,   42,  104,   48,  -41,  104,  -25,  -77,
  90,  -41, -103,   48,    0, -119,   22,   42, -103,   48,   26,
 -77,   90,  -47,   42,  104,  -89,   26,   78,  -21,    0,  120,
 -89,  -25,   78,  -47,  -41,  104,   42,  104,   48,   26,   78,
  90,    0,  120,   22,  -41,  104,   48,  104,  -47,   42,   78,
 -89,   26,   78,  -89,  -25,  104,  -47,  -41,   78,   90,  -25,
  78,   90,   26,  104,   48,  -41,  104,   48,   42,  -77,   90,
  26, -103,   48,  -41, -103,   48,   42, -119,   22,    0, -103,
 -47,   42, -119,  -21,    0,  -77,  -89,   26,  -77,  -89,  -25]
        
triangles = [
1,    2,  19,   
 1,  19,   3,    
3,   19,   4,   
19,   5,   4,   

9,    8,   6,   
 9,   6,  41,   
41,   6,   7,   
 6,  58,   7,   
 
8,    9,  29,   
 8,  29,  11,   
11,  29,  10,   
29,  28,  10,    

1,   11,  10,   
1,  10,   2,     
2,   10,  16,   
10,  15,  16,   

27,  12,  13,   
27,  13,  14,   
14,  13,  15,   
13,  16,  15,   

13,  12,  55,   
13,  55,  17,   
17,  55, 104,   
55,  53, 104,  

104, 18,  22,  
104, 22,  17,     
17,  22,  19,  
22,   5,  19,  

20,  24,  22,  
20,  22,  21,   
21,  22,  23,  
22,  18,  23,  

24,  20,  37,  
24,  37,  25,   
25,  37,  59,  
37,  26,  59,   

14,  30,  31,  
14,  31,  27,   
27,  31,  51, 
31,  50,  51,  

30,  28,  29,  
30,  29,  31,   
31,  29,  44,  
29,  32,  44,   

34,  48,  33,  
34,  33,  57,   
57,  33,  35,  
33,  36,  35,   

35,  36,  38,  
35,  38,  37,   
37,  38,  26,  
38,  39,  26, 

39,  38,   40,  
39,  40,    7,    
 7,   40,  41, 
40,   42,  41,   

42,  40,  43, 
42,  43,  32,  
32,  43,  44,   
43,  47,  44,   

46,  45,  47,   
46,  47,  48,   
48,  47,  33,   
47,  43,  33,   

45,  46,  49, 
45,  49,  50,   
50,  49,  51,   
49,  54,  51,  

52,  56,  53,   
52,  53,  49,   
49,  53,  54,   
53,  55,  54,   

23,  56,  52,   
23,  52,  21,   
21,  52,  57,   
52,  34,  57,  

59,  58,   6,   
59,   6,  25, 
25,   6,   4,    
6,   3,   4,    

61,  60,  62,   
61,  62,   1,   
62,  63,   1,   

64,  65,  66,   
64,  66,  67,   
66,  68,  67,   

19,   2,  69,   
19,  69,  71,   
69,  70,  71,   

72,  73,  74,   
72,  74,  76, 
74,  75,  76,   

77,  78,  79,   
77,  79,  80,   
79,  33,  80,   

81,  82,  83,   
81,  83,  85,   
83,  84,  85,   

88,  86,  87,   
88,  87,  90,   
87,  89,  90,   

92,  91,  93,   
92,  93,  57,   
93,  94,  57, 

41,  95,  96,   
41,  96,  98,   
96,  97,  98,  

101, 99, 100,  
101, 100,   7,  
100, 102,   7,  

103,  18, 104,  
103, 104, 105,  
104, 106, 105,  

107, 108,  12,  
107,  12, 109,   
12, 110, 109]

triangles = int32(triangles)-1

# Extract model data
# For every face: unwrap (infer) boundary
# Add point in center
# Regenerate new triangle mesh using center point.

newpoint_index = len(vertices)//3
newpoints = list(vertices)
newtriangles = []

MAGNITUDE = 14691.545454545454

X = float32(vertices[0::3])
Y = float32(vertices[1::3])
Z = float32(vertices[2::3])

M = array([X,Y,Z])
S = sqrt(MAGNITUDE/sum(M*M,0))
X *= S
Y *= S
Z *= S
M = array([X,Y,Z])
vertices = ravel(M.T)

ix = 0;
for ihexagon in range(20): 
    # Hexagons are represented at 4 triangles
    # The hexagon points are in a stereotyped order in the model
    # So, just hard-code extract the points
    # 0 1  2 3  4 5 6  7 8  9 A B
    # 1,2,19,1,19,3,3,19,4,19,5,4,
    p1 = triangles[ix*3+10]
    p2 = triangles[ix*3+8]
    p3 = triangles[ix*3+5]
    p4 = triangles[ix*3+0]
    p5 = triangles[ix*3+1]
    p6 = triangles[ix*3+2]
    pp = [p1,p2,p3,p4,p5,p6]
    data = array([vertices[p*3:][:3] for p in pp])
    
    center = np.mean(data,0)
    center = center*sqrt(MAGNITUDE/dot(center,center))
    newpoints.extend(center)
    for i in range(len(pp)):
        j = (i+1)%len(pp)
        t = [pp[i], pp[j], newpoint_index]
        newtriangles.extend(t)
    
    #for i in range(4): 
    #    newtriangles.extend(triangles[(ix+i)*3:][:3])
    newpoint_index+=1
    print data,center
    print pp
    ix += 4

for ipentagon in range(12):
    # Pentagons are represented by 3 triangles
    #  0  1  2    3  4 5    6  7 8
    # 61,60,62,  61,62,1,  62,63,1,   
    p1 = triangles[ix*3+1]
    p2 = triangles[ix*3+0]
    p3 = triangles[ix*3+5]
    p4 = triangles[ix*3+7]
    p5 = triangles[ix*3+2]
    pp = [p1,p2,p3,p4,p5]
    data = array([vertices[p*3:][:3] for p in pp])
    
    center = np.mean(data,0)
    center = center*sqrt(MAGNITUDE/dot(center,center))
    newpoints.extend(center)
    for i in range(len(pp)):
        j = (i+1)%len(pp)
        t = [pp[i], newpoint_index, pp[j]]
        newtriangles.extend(t)
    
    #for i in range(3): 
    #    newtriangles.extend(triangles[(ix+i)*3:][:3])
    newpoint_index+=1
    print data,center
    print pp
    ix += 3

newpoints = array(newpoints)
newtriangles = ravel(array(newtriangles))

print newpoints
print newtriangles

def dense_matrix_print_format(x,WIDTH=100):
    lines = ''
    s = ''
    for e in x:
        s += str(e)+','
        if len(s)>WIDTH:
            lines+=s+'\n'
            s=''
    lines+=s+'\n'
    return lines

newpoints = int8(newpoints+0.5)

NTRIANGLES = len(newtriangles)//3
NVERTICES = len(newpoints)//3
print '#define NTRIANGLES %d'%NTRIANGLES
print '#define NVERTICES  %d'%NVERTICES
print 'PROGMEM const int8_t vertices[NVERTICES*3]={'
print dense_matrix_print_format(newpoints)+'};'
print 'PROGMEM const uint8_t triangles[NTRIANGLES*3]={'
print dense_matrix_print_format(newtriangles)+'};'




'''
Given a list of vertices and triangles, generate the following additional
data structures. 
-- A list of normalized vertex normals
-- A list of normalized face normals
-- A map from edges to triangle indecies
'''

triangles = uint8(newtriangles)
vertices  = float64(int8(newpoints))

UNIT_LENGTH=126

facenormals = []

# Generate a list of face normals
for i in range(NTRIANGLES):
    t1,t2,t3 = triangles[i*3:][:3]
    p ,q ,r  = [vertices[t*3:][:3] for t in [t1,t2,t3]]
    # Normal (unnormalized) is the cross product of edges
    nx = (q[1]-p[1])*(r[2]-p[2])-(r[1]-p[1])*(q[2]-p[2])
    ny = (q[2]-p[2])*(r[0]-p[0])-(r[2]-p[2])*(q[0]-p[0])
    nz = (q[0]-p[0])*(r[1]-p[1])-(r[0]-p[0])*(q[1]-p[1])
    n = array([nx,ny,nz])
    m = n*UNIT_LENGTH/sqrt(dot(n,n))
    facenormals.extend(m)
facenormals = float32(facenormals)


# To get vertex normals, average all face normals
# Just doing this by brute force
vertexnormals = []
for i in range(NVERTICES):
    '''
    normals=[]
    for j in find(triangles==i)//3:
        normals.append(facenormals[j*3:][:3])
    vnorm = mean(normals,0)
    vnorm *= UNIT_LENGTH/sqrt(dot(vnorm,vnorm))
    vertexnormals.append(vnorm)
    '''
    vnorm = vertices[i*3:][:3]
    vnorm *= UNIT_LENGTH/sqrt(dot(vnorm,vnorm))
    vertexnormals.append(vnorm)
        
vertexnormals = ravel(int8(vertexnormals))
facenormals = int8(facenormals)

print 'PROGMEM const int8_t facenormals[NTRIANGLES*3]={'
print dense_matrix_print_format(facenormals)+'};'
print 'PROGMEM const int8_t vertexnormals[NVERTICES*3]={'
print dense_matrix_print_format(vertexnormals)+'};'




