author | Radek Brich <radek.brich@devl.cz> |
Mon, 05 May 2008 15:31:14 +0200 | |
branch | pyrit |
changeset 92 | 9af5c039b678 |
parent 90 | f6a72eb99631 |
child 97 | 2a853d284a6a |
permissions | -rw-r--r-- |
58 | 1 |
# LightWave .lwo file loader |
2 |
||
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
3 |
from math import * |
58 | 4 |
from struct import * |
90
f6a72eb99631
rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
5 |
from pyrit import Triangle, NormalVertex, Material |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
6 |
from vector import dot |
58 | 7 |
|
8 |
def read_int4(f): |
|
9 |
return unpack('>i', f.read(4))[0] |
|
10 |
||
11 |
def read_int2(f): |
|
12 |
return unpack('>h', f.read(2))[0] |
|
13 |
||
14 |
def read_float4(f): |
|
15 |
return unpack('>f', f.read(4))[0] |
|
16 |
||
17 |
def read_string(f): |
|
18 |
res = '' |
|
19 |
b = f.read(1) |
|
20 |
l = 1 |
|
21 |
while ( b != '\0' ): |
|
22 |
res += b |
|
23 |
b = f.read(1) |
|
24 |
l += 1 |
|
25 |
if (l % 2 != 0): |
|
26 |
f.read(1) |
|
27 |
l += 1 |
|
28 |
return (res,l) |
|
29 |
||
30 |
def read_chunk(f): |
|
31 |
ID = f.read(4) |
|
32 |
if (ID == ''): |
|
33 |
return ('',0) |
|
34 |
size = read_int4(f) |
|
35 |
return (ID, size) |
|
36 |
||
37 |
def read_subchunk(f): |
|
38 |
ID = f.read(4) |
|
39 |
size = read_int2(f) |
|
40 |
return (ID, size) |
|
41 |
||
42 |
def read_lwo(filename): |
|
43 |
points = [] |
|
44 |
faces = [] |
|
45 |
tags = [] |
|
46 |
surfaces = [] |
|
65
242839c6d27d
basic detection of compiler (GCC or ICC) and CPU capabilities
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
47 |
f = file(filename, 'rb') |
58 | 48 |
(ID,size) = read_chunk(f) |
49 |
form = f.read(4) |
|
50 |
if (ID != 'FORM' or form != 'LWOB'): |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
51 |
print 'unknown format' |
58 | 52 |
return |
53 |
(ID,size) = read_chunk(f) |
|
54 |
while (ID != ''): |
|
55 |
#print ID,size |
|
56 |
if (ID == 'PNTS'): |
|
57 |
while (size > 0): |
|
58 |
p1 = read_float4(f) |
|
59 |
p2 = read_float4(f) |
|
60 |
p3 = read_float4(f) |
|
61 |
points.append((p1,p2,p3)) |
|
62 |
size -= 12 |
|
63 |
elif (ID == 'SRFS'): |
|
64 |
while (size > 0): |
|
65 |
(s,l) = read_string(f) |
|
66 |
size -= l |
|
67 |
tags.append(s) |
|
68 |
elif (ID == 'POLS'): |
|
69 |
while (size > 0): |
|
70 |
vertex_count = read_int2(f) |
|
71 |
size -= 2 |
|
72 |
inds = [] |
|
73 |
for i in range(vertex_count): |
|
74 |
index = read_int2(f) |
|
75 |
inds.append(index) |
|
76 |
size -= 2 |
|
77 |
surf = read_int2(f) |
|
78 |
size -= 2 |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
79 |
if surf < 0: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
80 |
# detail polygons |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
81 |
surf = abs(surf) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
82 |
count = read_int2(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
83 |
size -= 2 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
84 |
# ... FIXME |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
85 |
#print size, vertex_count |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
86 |
if vertex_count >= 3: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
87 |
faces.append([inds[0], inds[1], inds[2], surf]) |
58 | 88 |
i = 0 |
89 |
while (vertex_count > 3): |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
90 |
faces.append([inds[0], inds[2+i], inds[3+i], surf]) |
58 | 91 |
vertex_count -= 1 |
92 |
i += 1 |
|
93 |
elif (ID == 'SURF'): |
|
94 |
(name,l) = read_string(f) |
|
95 |
size -= l |
|
96 |
surf = {} |
|
97 |
while (size > 0): |
|
98 |
(subID,subsize) = read_subchunk(f) |
|
99 |
size -= 6 |
|
100 |
if (subID == 'COLR'): |
|
101 |
col = f.read(subsize) |
|
102 |
surf['color'] = (unpack('BBB',col[:3])) |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
103 |
elif (subID == 'FLAG'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
104 |
flags = read_int2(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
105 |
surf['luminous'] = (flags >> 0) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
106 |
surf['outline'] = (flags >> 1) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
107 |
surf['smooth'] = (flags >> 2) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
108 |
surf['color-highlights'] = (flags >> 3) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
109 |
surf['color-filter'] = (flags >> 4) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
110 |
surf['opaque-edge'] = (flags >> 5) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
111 |
surf['transparent-edge'] = (flags >> 6) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
112 |
surf['sharp-terminator'] = (flags >> 7) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
113 |
surf['double-sided'] = (flags >> 8) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
114 |
surf['additive'] = (flags >> 9) & 1; |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
115 |
|
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
116 |
# Base Shading (float) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
117 |
elif (subID == 'VDIF'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
118 |
surf['diffuse'] = read_float4(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
119 |
elif (subID == 'VSPC'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
120 |
surf['specular'] = read_float4(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
121 |
elif (subID == 'VRFL'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
122 |
surf['reflection'] = read_float4(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
123 |
elif (subID == 'VTRN'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
124 |
surf['transparency'] = read_float4(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
125 |
|
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
126 |
# Base Shading (short) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
127 |
elif (subID == 'DIFF'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
128 |
if not surf.has_key('diffuse'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
129 |
surf['diffuse'] = read_int2(f)/255. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
130 |
elif (subID == 'SPEC'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
131 |
if not surf.has_key('specular'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
132 |
surf['specular'] = read_int2(f)/255. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
133 |
elif (subID == 'REFL'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
134 |
if not surf.has_key('reflection'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
135 |
surf['reflection'] = read_int2(f)/255. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
136 |
elif (subID == 'TRAN'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
137 |
if not surf.has_key('transparency'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
138 |
surf['transparency'] = read_int2(f)/255. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
139 |
|
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
140 |
elif (subID == 'RIND'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
141 |
surf['refractive-index'] = read_float4(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
142 |
elif (subID == 'GLOS'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
143 |
surf['glossiness'] = read_int2(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
144 |
elif (subID == 'SMAN'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
145 |
surf['smoothing-max-angle'] = read_float4(f) |
58 | 146 |
else: |
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
147 |
print "Warning: SURF sub chunk", subID,"("+str(subsize),"B) ignored" |
58 | 148 |
f.read(subsize) |
149 |
size -= subsize |
|
150 |
surfaces.append(surf) |
|
151 |
else: |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
152 |
print "Warning: chunk", ID,"("+str(size),"B) ignored" |
58 | 153 |
f.read(size) |
154 |
(ID,size) = read_chunk(f) |
|
155 |
return (points, faces, tags, surfaces) |
|
156 |
||
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
157 |
def LoadLightwaveLwoFile(rt, filename, scale=(1,1,1), trans=(0,0,0)): |
58 | 158 |
if (type(scale) == float or type(scale) == int): |
159 |
scale = (scale,)*3 |
|
160 |
(points, faces, tags, surfaces) = read_lwo(filename) |
|
161 |
||
162 |
vertices = [] |
|
163 |
normals = [] |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
164 |
vertex_faces = [] |
59
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
165 |
materials = [] |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
166 |
|
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
167 |
for surf in surfaces: |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
168 |
mat = Material(colour=tuple(float(x)/255. for x in surf['color'])) |
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
169 |
if surf.has_key('smooth'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
170 |
mat.setSmooth(surf['smooth']) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
171 |
diff = 1. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
172 |
if surf.has_key('diffuse'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
173 |
diff = surf['diffuse'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
174 |
spec = 0. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
175 |
if surf.has_key('specular'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
176 |
spec = surf['specular'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
177 |
gloss = 1.0 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
178 |
if surf.has_key('glossiness'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
179 |
gloss = surf['glossiness'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
180 |
mat.setPhong(0.1, diff, spec, gloss) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
181 |
refl = 0. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
182 |
if surf.has_key('reflection'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
183 |
refl = surf['reflection'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
184 |
mat.setReflectivity(refl) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
185 |
transp = 0. |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
186 |
if surf.has_key('transparency'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
187 |
transp = surf['transparency'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
188 |
rindex = 1.0 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
189 |
if surf.has_key('refractive-index'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
190 |
rindex = surf['refractive-index'] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
191 |
mat.setTransmissivity(transp, rindex) |
59
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
192 |
materials.append(mat) |
58 | 193 |
|
194 |
for point in points: |
|
195 |
v0 = scale[0]*point[0] + trans[0] |
|
196 |
v1 = scale[1]*point[1] + trans[1] |
|
197 |
v2 = scale[2]*point[2] + trans[2] |
|
198 |
vertices.append(NormalVertex((v2,v1,v0))) |
|
199 |
normals.append([0.,0.,0.]) |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
200 |
vertex_faces.append([]) |
58 | 201 |
|
202 |
for f in faces: |
|
203 |
for x in f[0:3]: |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
204 |
vertex_faces[x].append(f) |
58 | 205 |
|
206 |
# interpolate normals at vertices |
|
207 |
num = 0 |
|
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
208 |
for vfaces in vertex_faces: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
209 |
vert = vertices[num] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
210 |
edges = {} |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
211 |
N = [0,0,0] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
212 |
for f in vfaces: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
213 |
for fvert in f[0:3]: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
214 |
if edges.has_key(str(fvert)): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
215 |
edges[str(fvert)].append(f) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
216 |
else: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
217 |
edges[str(fvert)] = [f] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
218 |
for f in vfaces: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
219 |
vv = [vertices[x] for x in f[0:3]] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
220 |
fN = Triangle(vv[0], vv[1], vv[2], materials[f[3]-1]).getNormal() |
58 | 221 |
for i in range(3): |
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
222 |
N[i] += fN[i] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
223 |
surf = surfaces[f[3]-1] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
224 |
if not surf.has_key('smoothing-max-angle'): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
225 |
continue |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
226 |
fNvert = list(fN) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
227 |
Ncount = 1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
228 |
copy_vertex = False |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
229 |
for fvert in f[0:3]: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
230 |
for ef in edges[str(fvert)]: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
231 |
if ef == f: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
232 |
continue |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
233 |
# f - ref. face; ef - other face |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
234 |
vv = [vertices[x] for x in ef[0:3]] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
235 |
efN = Triangle(vv[0], vv[1], vv[2], materials[ef[3]-1]).getNormal() |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
236 |
d = dot(fN, efN) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
237 |
if d > 1: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
238 |
d = 1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
239 |
if d < -1: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
240 |
d = -1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
241 |
if acos(d) < surf['smoothing-max-angle']: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
242 |
for i in range(3): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
243 |
fNvert[i] += efN[i] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
244 |
Ncount += 1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
245 |
else: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
246 |
copy_vertex = True |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
247 |
# here fNvert is normal for num'th vertex in face f |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
248 |
if copy_vertex: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
249 |
for i in range(3): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
250 |
fNvert[i] /= Ncount |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
251 |
new_vert = NormalVertex(vert) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
252 |
new_vert.setNormal(tuple(fNvert)) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
253 |
f.append(f[0]) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
254 |
f.append(f[1]) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
255 |
f.append(f[2]) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
256 |
vertices.append(new_vert) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
257 |
for i in range(3): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
258 |
if f[i] == num: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
259 |
f[i+4] = len(vertices)-1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
260 |
if (len(vfaces) > 0): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
261 |
for i in range(3): |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
262 |
N[i] /= len(vfaces) |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
263 |
vertices[num].setNormal(tuple(N)) |
58 | 264 |
num += 1 |
69
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
265 |
|
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
266 |
for f in faces: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
267 |
if len(f) > 4: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
268 |
v = [vertices[x] for x in f[4:7]] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
269 |
else: |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
270 |
v = [vertices[x] for x in f[0:3]] |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
271 |
matidx = f[3]-1 |
303583d2fb97
move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents:
65
diff
changeset
|
272 |
face = Triangle(v[0], v[1], v[2], materials[matidx]) |
75
20dee9819b17
unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
273 |
rt.addShape(face) |