-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-obj.rb
470 lines (424 loc) · 12.5 KB
/
ruby-obj.rb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#require 'pry'
#require 'free-image'
require 'numo/narray'
$errors=["file not found".freeze,"unsupported mtl options".freeze,
"unsupported texture options".freeze,"unsupported shapes/bezier/curves".freeze]
def transverts(v,rot,trans)
rottransm=Numo::SFloat.cast([[1,0,0,trans[0]],
[0,Math::cos(rot),-Math::sin(rot),trans[1]],
[0,Math::sin(rot),Math::cos(rot),trans[2]],
[0,0,0,1]])
v.shape[0].times {|x| v[x,0..-1] = Numo::SFloat[*v[x,0..-1],1].inner(rottransm)[0..2]}
end
def transcammat(cam)
e = cam[0,0..-1] #origin
d = cam[1,0..-1] #direction
u = Numo::SFloat[0,1,0] #up
r = cross(d,u)
u = cross(r,d)
d = norm(d)
r = norm(r)
u = norm(u)
rottransm = Numo::SFloat.cast([[*r,r.dot(e*-1)],[*u,u.dot(e*-1)],[*(d*-1),d.dot(e)],[0,0,0,1]])
end
def translateall(v,trans)
rottransm=Numo::SFloat.cast([[1,0,0,trans[0]],
[0,1,0,trans[1]],
[0,0,1,trans[2]],
[0,0,0,1]])
if v.class == Obj
v.v.shape[0].times {|x| v.v[x,0..-1] = Numo::SFloat[*v.v[x,0..-1],1].inner(rottransm)[0..2]}
GC.start
v.center = Numo::SFloat[*v.center[0..-1],1].inner(rottransm)[0..2]
calcminmax(v)
v.drawing.each do |x|
calcminmax(v.models[x])
bsphere(v.models[x])
v.models[x].facenormals
GC.start
end
end
GC.start
end
def rotateall(v,trans,r)
# r=rot.map {|x| x/57.29577951308232 if x!=0}
if r[0] != 0
r[0]=r[0]*0.01745
sina=Math::sin(r[0])
cosa=Math::cos(r[0])
else
sina=0.0
cosa=1.0
end
if r[1] != 0
r[1]=r[1]*0.01745
sinb=Math::sin(r[1])
cosb=Math::cos(r[1])
else
sinb=0.0
cosb=1.0
end
if r[2] != 0
r[2]=r[2]*0.01745
sinc=Math::sin(r[2])
cosc=Math::cos(r[2])
else
sinc=0.0
cosc=1.0
end
rottransm=Numo::SFloat.cast(
[[cosb*cosc,-cosb*sinc,sinb,trans[0]],
[cosa*sinc+sina*sinb*cosc,cosa*cosc-sina*sinb*sinc,-sina*cosb,trans[1]],
[sina*sinc-cosa*sinb*cosc,sina*cosc+cosa*sinb*sinc,cosa*cosb,trans[2]],
[0,0,0,1]])
GC.start
if v.class == Obj
v.v.shape[0].times {|x| v.v[x,0..-1] = Numo::SFloat[*v.v[x,0..-1],1].inner(rottransm)[0..2]}
GC.start
v.center = Numo::SFloat[*v.center[0..-1],1].inner(rottransm)[0..2]
calcminmax(v)
v.drawing.each do |x|
calcminmax(v.models[x])
bsphere(v.models[x])
v.models[x].facenormals
GC.start
end
else
return Numo::SFloat[*v,1].inner(rottransm)[0..2]
end
GC.start
end
def transcam(obj,cam)
e = cam[0,0..-1] #origin
d = cam[1,0..-1] #direction
u = Numo::SFloat[0,1,0] #up
r = cross(d,u)
u = cross(r,d)
d = norm(d)
r = norm(r)
u = norm(u)
rottransm = Numo::SFloat.cast([[*r,r.dot(e*-1)],[*u,u.dot(e*-1)],[*(d*-1),d.dot(e)],[0,0,0,1]])
trans = Numo::SFloat.cast(obj.collect {|x| x[2]})
obj[(zsort(Numo::SFloat.cast(trans.split(trans.shape[0]).map {|x| Numo::SFloat.cast([*x.flatten,1]).inner(rottransm)})))[0]]#[cam[1,2] > 0 ? 0 : -1]]
end
def transall(obj,cam)
e = cam[0,0..-1] #origin
d = cam[1,0..-1] #direction
u = Numo::SFloat[0,1,0] #up
r = cross(d,u)
u = cross(r,d)
d = norm(d)
r = norm(r)
u = norm(u)
rottransm = Numo::SFloat.cast([[*r,r.dot(e*-1)],[*u,u.dot(e*-1)],[*(d*-1),d.dot(e*-1)],[0,0,0,1]])
if obj.class == Obj
obj.v.shape[0].times {|x| obj.v[x,0..-1] = Numo::SFloat[*obj.v[x,0..-1],1].inner(rottransm)[0..2]}
end
end
def calcminmax(obj)
xmin=ymin=zmin=10000.0
xmax=ymax=zmax=-10000.0
if obj.class == Obj
v = obj.v
xmin,xmax = v[0..-1,0].minmax
ymin,ymax = v[0..-1,1].minmax
zmin,zmax = v[0..-1,2].minmax
else
v = obj.faces
obj.faces.each do |x|
xmin = obj.parent.v[x,0] if xmin > obj.parent.v[x,0]
xmax = obj.parent.v[x,0] if xmax < obj.parent.v[x,0]
ymin = obj.parent.v[x,1] if ymin > obj.parent.v[x,1]
ymax = obj.parent.v[x,1] if ymax < obj.parent.v[x,1]
zmin = obj.parent.v[x,2] if zmin > obj.parent.v[x,2]
zmax = obj.parent.v[x,2] if zmax < obj.parent.v[x,2]
end
end
obj.minv=Numo::SFloat[xmin,ymin,zmin]
obj.maxv=Numo::SFloat[xmax,ymax,zmax]
end
def bsphere(obj)
obj.center = (obj.minv + obj.maxv)*0.5
obj.radius = Numo::SFloat.cast((obj.maxv - obj.minv).max / 2.0)
obj.r2 = obj.radius.square
end
def zsort(obj)
if obj.class == Obj
obj.v[0..-1,2].sort_index
elsif obj.class == Model
Numo::SFloat.cast(obj.faces.split(obj.faces.shape[0]).map {|x| x[0,0..-1,2].mean}).sort_index
elsif obj.class == Numo::SFloat
# pp obj
obj[0..-1,2].map {|x| x.abs}.sort_index
end
end
def norm(u)
den = u.square.sum ** 0.5
u/den
end
def cross(u,v)
tmp = Numo::SFloat[u[1]*v[2]-u[2]*v[1],u[2]*v[0]-u[0]*v[2],u[0]*v[1]-u[1]*v[0]]
end
class Scene
attr_accessor :objs, :actors, :envs
def raytrace(ray)
arf=[]
tmp = @objs.keys.select {|x| @objs[x].class == Obj}
tmp.each do |x|
tmp2 = @objs[x].raytrace(ray)
if tmp2 != [] && tmp2 != nil
tmp2.each {|y| arf << y}
end
end
arf.sort_by! {|x| x[2].sum}
arf.reverse! if ray[1,0..-1].sum < 0
arf
end
def initialize
@objs={}
@envs=[]
@actors=[]
end
def actor(name,obj)
@objs[name]=obj
@actors << name
end
def env(name,obj)
@objs[name]=obj
@envs << name
end
def draw(func,verts=nil,indices=nil)
if $pos
rotm = transcammat(Numo::SFloat[$pos,$dir])
@actors.sort_by {|x| Numo::SFloat[*@objs[x].center,1].inner(rotm)[2]}.each {|x| @objs[x].draw(func,x,verts,indices)}
@envs.sort_by {|x| Numo::SFloat[*@objs[x].center,1].inner(rotm)[2]}.each {|x| @objs[x].draw(func,x,verts,indices)}
else
@actors.sort_by {|x| @objs[x].center[2]}.each {|x| @objs[x].draw(func,x,verts,indices)}
@envs.sort_by {|x| @objs[x].center[2]}.each {|x| @objs[x].draw(func,x,verts,indices)}
end
end
end
class Model
attr_accessor :mat, :faces, :fc, :parent, :radius, :center, :minv, :maxv, :fn, :name, :r2
def initialize(parent)
@parent = parent
end
def raytrace(ray)
isec = []
if @faces
ray[1,0..-1] = norm(ray[1,0..-1])
p = ray[0,0..-1] - @center
if (p_d = p.dot(ray[1,0..-1])) > 0 || (p.dot(p) < @r2) == 1
return nil
end
a = p - p_d * ray[1,0..-1]
if a.dot(a) > @r2 == 1
return nil
end
@fn.shape[0].times do |x|
if (b=@fn[x,0..-1].dot(ray[1,0..-1])).abs < 0.01
next
end
a=@fn[x,0..-1].dot(ray[0,0..-1][email protected][@faces[x,0],0..-1]) * -1
if (r = a/b) < 0.0
next
end
i = ray[0,0..-1] + r * ray[1,0..-1]
if @faces.shape[1] == 3
u = @parent.v[@faces[x,1],0..-1] - @parent.v[@faces[x,0],0..-1]
v = @parent.v[@faces[x,2],0..-1] - @parent.v[@faces[x,0],0..-1]
uu = u.dot(u)
uv = u.dot(v)
vv = v.dot(v)
w = i - @parent.v[@faces[x,0],0..-1]
wu = w.dot(u)
wv = w.dot(v)
d = uv * uv - uu * vv
s=(uv*wv - vv*wu)/d
if s < 0.0 || s > 1.0
next
end
t = (uv*wu - uu*wv)/d
if t < 0.0 || (s+t) > 1.0
next
end
else
next
end
isec << [@name,x,i]
end
end
isec.empty? ? nil : transcam(isec,ray)
end
def facenormals
@fn = Numo::SFloat.new(@faces.shape[0],3).allocate
@faces.shape[0].times do |p|
u = @parent.v[faces[p,1],0..-1] - @parent.v[faces[p,0],0..-1]
v = @parent.v[faces[p,2],0..-1] - @parent.v[faces[p,0],0..-1]
tmp = Numo::SFloat[u[1]*v[2]-u[2]*v[1],u[2]*v[0]-u[0]*v[2],u[0]*v[1]-u[1]*v[0]]
den = tmp.square.sum ** 0.5
@fn[p,0..-1] = (tmp / den)
end
end
end
class Material
attr_accessor :Ka, :Kd, :Ks, :d, :Ns, :illum,
:map_Ka, :map_Kd, :map_Ks, :map_d, :image
end
class Obj
attr_accessor :models, :materials, :v, :vt, :vn, :pos, :rot, :center, :radius, :minv, :maxv, :r2, :drawing
def initialize(filename)
begin
a=File.open(filename)
rescue
error(0)
end
@dirname=File.dirname(filename)
b=a.read
a.close
@pos=Numo::SFloat[0,0,0]
@rot=Numo::SFloat[0,[0,0,0]]
@models={}
readobj(b)
GC.start
calcminmax(self)
GC.start
bsphere(self)
GC.start
@drawing = @models.keys.select {|x| @models[x].faces}.sort_by {|x| 1- @models[x].mat.d}
@drawing.each {|x| calcminmax(@models[x]);bsphere(@models[x]);@models[x].facenormals;GC.start}
end
def move(translation=[0,0,0],rotation=[0,0,0])
if rotation != [0,0,0]
rotateall(self,translation,rotation)
elsif rotation == [0,0,0] && translation != [0,0,0]
translateall(self,translation)
end
end
def draw(func,name,verts,indices)
@drawing.each {|x| func.call(@models[x],name,verts,indices)}
end
def raytrace(ray)
ray[1,0..-1] = norm(ray[1,0..-1])
p = ray[0,0..-1] - @center
if (p_d = p.dot(ray[1,0..-1])) > 0 # || (p.dot(p) < @r2) == 1
return nil
end
a = p - p_d * ray[1,0..-1]
if a.dot(a) > @r2 == 1
return nil
end
arf = []
tmp = #@models.keys.select {|x| @models[x].faces}
@drawing.sort_by {|x| ((@models[x].center + @models[x].radius*(ray[1,0..-1]*-1)) - ray[0,0..-1]).sum}
tmp.reverse! if ray[1,0..-1].sum < 0
tmp.each do |x|
tmp2 = @models[x].raytrace(ray)
if tmp2 != nil
arf << tmp2
if (@models[x].mat && @models[x].mat.d == 1)
break
end
end
end
arf
end
def readobj(b)
materialfile = b.scan(/^mtllib ([^\r\n]*)/).join
puts materialfile
if materialfile
@materials={}
readmtl(File.expand_path(materialfile,@dirname))
else
@hasmaterials=false
end
if b.index(/^(vp|cstype|deg|curv2|parm|surf|curv|trim) /)
error(3)
end
@v=Numo::SFloat.parse(b.scan(/^v ([^\r\n]*)/).join("\n"))
if b.index(/^vt /)
@vt = Numo::SFloat.parse(b.scan(/^vt ([^\r\n]*)/).join("\n"))
end
if b.index(/^vn /)
@vn = Numo::SFloat.parse(b.scan(/^vn ([^\r\n]*)/).join("\n"))
end
if b.index(/^[go] /)
@hasgroups=true
parsegroups(b)
end
end
def parsegroups(b)
groups = b.split(/^[go] /)[1..-1]
groups.each do |tmp|
name = tmp.lines[0].chomp.freeze
cur = (@models[name]=Model.new(self))
cur.name = name
if tmp2 = tmp.scan(/usemtl ([^\r\n]*)/)
cur.mat = @materials[tmp2.flatten[0]]
end
if tmp.index(/^f /)
cur.faces = Numo::UInt32.cast(tmp.scan(/^f ([^\r\n]*)/).map{|y| y[0].split(" ").map{|z| z.split("/")[0].to_i()-1 }})
cur.fc = cur.faces.shape[0]
end
end
end
def readmtl(mtlfile)
begin
a=File.open(mtlfile)
rescue
error(0)
end
b=a.read.chomp.split("\n")
a.close
parsemtl(b)
end
def parsemtl(mtl)
mtls=mtl.each_index.select {|x| mtl[x].start_with?("newmtl ")} + [mtl.size]
(mtls.size() -1).times do |section|
tmp = mtl[mtls[section]...mtls[section+1]]
name = tmp[0].gsub("usemtl ",'').gsub("newmtl ",'').chomp
cur = (@materials[name] = Material.new)
tmp2=nil
cur.Ka = kakdks(tmp.find{|x| x.start_with?("Ka ")})
cur.Kd = kakdks(tmp.find{|x| x.start_with?("Kd ")})
cur.Ks = kakdks(tmp.find{|x| x.start_with?("Ks ")})
cur.illum = ((ttmp = tmp.find{|x| x.start_with?("illum ")}) != nil ? ttmp.split(" ")[1].to_f : nil)
cur.d = ((ttmp = tmp.find{|x| x.start_with?("d ")}) != nil ? ttmp.split(" ")[1].to_f : nil)
cur.Ns = ((ttmp = tmp.find{|x| x.start_with?("Ns ")}) != nil ? ttmp.split(" ")[1].to_f : nil)
cur.map_Ka = mapkakdks(tmp.find{|x| x.start_with?("map_Ka ")})
cur.map_Kd = mapkakdks(tmp.find{|x| x.start_with?("map_Kd ")})
cur.map_Ks = mapkakdks(tmp.find{|x| x.start_with?("map_Ks ")})
cur.map_d = ((ttmp = tmp.find{|x| x.start_with?("map_d ")}) != nil ? ttmp.to_f : nil)
# if cur.map_Kd
# cur.image = FreeImage::Bitmap.open(cur.map_Kd)
# end
end
end
def mapkakdks(line)
if line
args = line.split(" ")[1..-1].join
if args.match(/\-(blendu|blendv|clamp|imfchan|mm|o|s|t|texres) /)
error(2)
end
return File.expand_path(args,@dirname)
else
return nil
end
end
def kakdks(line)
if line
args = line.split(" ")[1..-1]
if args.include?("xyz") || args.include?(/\.rfl/)
error(1)
end
args+=[args[0],args[0]] if args.size == 1
return args.map(&:to_f)
else
return nil
end
end
def error(type)
puts $errors[type]
exit
end
end