-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbminit.lua
More file actions
941 lines (738 loc) · 22.9 KB
/
bminit.lua
File metadata and controls
941 lines (738 loc) · 22.9 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
-- Libraries
local image = require("image")
local gl = require("gl")
local glu = require("glu")
local glut = require("glut")
local mesh = require("mesh")
local bm = require("bm")
local pairs = pairs
local string = string
local io = io
local math = math
local os = os
-- Functions
local abs = math.abs
local cos = math.cos
local exp = math.exp
local floor = math.floor
local glBegin = gl.glBegin
local glCallList = gl.glCallList
local glClear = gl.glClear
local glClearColor = gl.glClearColor
local glColord = gl.glColord
local glDepthMask = gl.glDepthMask
local glDisable = gl.glDisable
local glEnable = gl.glEnable
local glEnd = gl.glEnd
local glEndList = gl.glEndList
local glFlush = gl.glFlush
local glGenLists = gl.glGenLists
local glGetBooleanv = gl.glGetBooleanv
local glGetDoublev = gl.glGetDoublev
local glLoadIdentity = gl.glLoadIdentity
local glMaterial = gl.glMaterial
local glMatrixMode = gl.glMatrixMode
local glNewList = gl.glNewList
local glOrtho = gl.glOrtho
local glPixelZoom = gl.glPixelZoom
local glPointSize = gl.glPointSize
local glPopMatrix = gl.glPopMatrix
local glPushMatrix = gl.glPushMatrix
local glRasterPosd = gl.glRasterPosd
local glReadPixels = gl.glReadPixels
local glRotated = gl.glRotated
local glScaled = gl.glScaled
local glTranslated = gl.glTranslated
local glVertexd = gl.glVertexd
local glViewport = gl.glViewport
local gluPerspective = glu.gluPerspective
local gluUnProject = glu.gluUnProject
local glutBitmapCharacter = glut.glutBitmapCharacter
local glutFullScreen = glut.glutFullScreen
local glutGet = glut.glutGet
local glutGetModifiers = glut.glutGetModifiers
local glutPostRedisplay = glut.glutPostRedisplay
local glutReshapeWindow = glut.glutReshapeWindow
local glutSwapBuffers = glut.glutSwapBuffers
local glutWireCube = glut.glutWireCube
local max = math.max
local min = math.min
local pi = math.pi
local sin = math.sin
local sqrt = math.sqrt
local unpack = table.unpack
-- Boolean values
local GL_FALSE = 0x0000
local GL_TRUE = 0x0001
-- Data types
local GL_FLOAT = 0x1406
-- Primitives
local GL_POINTS = 0x0000
local GL_LINES = 0x0001
local GL_QUADS = 0x0007
-- Matrix Mode
local GL_MODELVIEW = 0x1700
local GL_PROJECTION = 0x1701
-- Lines
local GL_LINE_SMOOTH = 0x0B20
-- Polygons
local GL_POINT = 0x1B00
local GL_LINE = 0x1B01
local GL_FILL = 0x1B02
-- Depth buffer
local GL_DEPTH_TEST = 0x0B71
local GL_DEPTH_COMPONENT = 0x1902
-- Lighting
local GL_LIGHTING = 0x0B50
local GL_DIFFUSE = 0x1201
local GL_FRONT_AND_BACK = 0x0408
-- Feedback
local GL_2D = 0x0600
-- Fog
local GL_LINEAR = 0x2601
-- Buffers, Pixel Drawing/Reading
local GL_LEFT = 0x0406
local GL_RIGHT = 0x0407
local GL_RGBA = 0x1908
-- Pixel Mode / Transfer
local GL_UNPACK_ALIGNMENT = 0x0CF5
-- Texture mapping
local GL_TEXTURE_ENV = 0x2300
local GL_TEXTURE_ENV_MODE = 0x2200
local GL_TEXTURE_1D = 0x0DE0
local GL_TEXTURE_WRAP_S = 0x2802
local GL_TEXTURE_MAG_FILTER = 0x2800
local GL_TEXTURE_MIN_FILTER = 0x2801
local GL_TEXTURE_GEN_S = 0x0C60
local GL_TEXTURE_GEN_MODE = 0x2500
local GL_TEXTURE_COMPONENTS = 0x1003
local GL_LINEAR_MIPMAP_LINEAR = 0x2703
local GL_OBJECT_LINEAR = 0x2401
local GL_OBJECT_PLANE = 0x2501
local GL_MODULATE = 0x2100
local GL_REPEAT = 0x2901
local GL_S = 0x2000
-- glPush/PopAttrib bits
local GL_DEPTH_BUFFER_BIT = 0x00000100
local GL_COLOR_BUFFER_BIT = 0x00004000
-- Display Lists
local GL_COMPILE = 0x1300
local GLUT_DOWN = 0
local GLUT_UP = 1
local GLUT_LEFT = 0
local GLUT_WINDOW_X = 100
local GLUT_WINDOW_Y = 101
local GLUT_WINDOW_WIDTH = 102
local GLUT_WINDOW_HEIGHT = 103
local GLUT_SCREEN_WIDTH = 200
local GLUT_SCREEN_HEIGHT = 201
local GLUT_WINDOW_RGBA = 116
local GLUT_WINDOW_CURSOR = 122
local GLUT_ACTIVE_SHIFT = 1
local GLUT_ACTIVE_CTRL = 2
local GLUT_ACTIVE_ALT = 4
local browser_path, pager_path
= "firefox", "firefox"
local mesh_obj = mesh.load("data/cortex.mesh")
local labels = {}
local fovy_deg, camera_distance, znear, zfar, x_angle_deg, y_angle_deg, label_point_size
= 45.0, 2.0, 1.0, 1e5, 0.0, 0.0, 5.0
local camera_pivot, camera_offset
= {0, 0, 0}, {0, 0, 0}
local mouse_xi, mouse_yi
= 0, 0
local axes_color, stripe_color, mesh_color, horizontal_plane_color, coronal_plane_color, sagittal_plane_color, label_color
= {0, 0, 0, 1}, {1, 0, 0, 1}, {0.9, 0.9, 0.9, 1.0}, {0.0, 0.0, 0.8, 0.3}, {0.7, 0.0, 0.0, 0.3}, {0.7, 0.4, 0.0, 0.3}, {1.0, 6.0, 0.0, 1.0}
local draw_mode = GL_FILL
local locked_position = nil
local scene_box = {mesh_obj:get_bounds()}
local kb_cmd_mode = false
local command = "" -- command being entered by the user
local candy_striping, doing_transparency, drawing_thumbnails, ctrl_is_pressed, shift_is_pressed
= false, false, true, false, false
local warn = function(...)
io.stderr:write(string.format(...) .. "\n")
end
local reverse = function(t)
local len_t = #t
for i = 1, len_t / 2 do
t[i], t[len_t - i + 1] = t[len_t - i + 1], t[i]
end
end
local load_thumbnails = function(orient)
local bm_thumbnails, bm_names
= {}, {}
local bm_max_width, bm_max_height, i
= 1, 1, 1
local file = io.open("thumbnails/" .. orient .. "-labelled/names")
for filename in file:lines() do
bm_names[i] = filename:sub(1, -5)
bm_thumbnails[i] = image.load("thumbnails/" .. orient .. "-labelled/" .. filename)
local width, height = bm_thumbnails[i]:get_size()
bm_max_width = max(bm_max_width, width)
bm_max_height = max(bm_max_height, height)
i = i + 1
end
bm[orient .. "_thumbnails"] = bm_thumbnails
bm[orient .. "_names"] = bm_names
bm[orient .. "_max_width"] = bm_max_width
bm[orient .. "_max_height"] = bm_max_height
end
-- Load coronal thumbnails
load_thumbnails("coronal")
reverse(bm.coronal_names)
reverse(bm.coronal_thumbnails)
-- Load horizontal thumbnails
load_thumbnails("horizontal")
-- Load sagittal thumbnails
load_thumbnails("sagittal")
-- wx, wy, wz are the returned world coordinates
-- Reference: redbook/unproject.c & one of the NeHe demos
-- The name is a bit misleading, since the result can be
-- different if the position is locked.
local get_mouse_location = function()
if locked_position ~= nil then
return unpack(locked_position)
end
local real_x, real_y
= mouse_xi, glutGet(GLUT_WINDOW_HEIGHT) - mouse_yi - 1
return gluUnProject(real_x, real_y, glReadPixels(real_x, real_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT))
end
local center = { (scene_box[1] + scene_box[4]) / 2, (scene_box[2] + scene_box[5]) / 2, (scene_box[3] + scene_box[6]) / 2 }
local edges = { scene_box[4] - scene_box[1], scene_box[5] - scene_box[2], scene_box[6] - scene_box[3] }
local get_indexes_from_mouse = function()
local x, y, z = get_mouse_location()
return floor((#bm.coronal_names) * (z - scene_box[3]) / edges[3]),
floor((#bm.horizontal_names) * (y - scene_box[2]) / edges[2]),
floor((#bm.sagittal_names) * (x - scene_box[1]) / edges[1])
end
-------
-- Etc.
-------
local draw_bitmap_string = function(font, s, x, y, z)
if x ~= nil then
if z ~= nil then
glRasterPosd(x, y, z)
else
glDepthMask(false)
glRasterPosd(x, y)
glDepthMask(true)
end
end
for i = 1, s:len() do
glutBitmapCharacter(font, s:byte(i))
end
end
local show_pos = function(x, y, z)
draw_bitmap_string("h12", ("(%3.1f, %3.1f, %3.1f) [mm]"):format(x, y, z), x, y, z)
end
local draw_box_list = glGenLists(1)
do
glNewList(draw_box_list, GL_COMPILE)
glPushMatrix()
glTranslated(center[1], center[2], center[3], 1)
glScaled(edges[1], edges[2], edges[3])
glutWireCube(1.0)
glPopMatrix()
show_pos(scene_box[1], scene_box[2], scene_box[3])
show_pos(scene_box[4], scene_box[5], scene_box[6])
glEndList()
end
local set_up_3D_viewport_and_matrices = function()
local w, h = max(1, glutGet(GLUT_WINDOW_WIDTH)), max(1, glutGet(GLUT_WINDOW_HEIGHT))
local width_of_3d_area = ({ [true] = w - bm.coronal_max_width, [false] = w })[drawing_thumbnails]
glViewport(0, 0, width_of_3d_area - 1, h - 1)
-- Do transformations to implement the camera
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(fovy_deg, width_of_3d_area / h, znear, zfar)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
local x, y, z
= unpack(camera_offset)
z = z - camera_distance
glTranslated(x, y, z)
glRotated(x_angle_deg, -1.0, 0.0, 0.0)
glRotated(y_angle_deg, 0.0, 1.0, 0.0)
x, y, z = unpack(camera_pivot)
glTranslated(-x, -y, -z)
end
on_display = function()
if doing_transparency == false then
glEnable(GL_DEPTH_TEST)
end
glClearColor(1.0, 1.0, 1.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
set_up_3D_viewport_and_matrices()
glMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE, unpack(mesh_color))
glEnable(GL_LIGHTING)
glPushMatrix()
if candy_striping == true then
glEnable(GL_TEXTURE_1D)
glEnable(GL_TEXTURE_GEN_S)
end
mesh_obj:draw()
if candy_striping == true then
glDisable(GL_TEXTURE_1D)
glDisable(GL_TEXTURE_GEN_S)
end
glPopMatrix()
glDisable(GL_LIGHTING)
glColord(unpack(axes_color))
glCallList(draw_box_list)
local x, y, z = get_mouse_location()
if locked_position ~= nil then
glDepthMask(false)
glBegin(GL_QUADS)
-- coronal
glColord(unpack(coronal_plane_color))
glVertexd(scene_box[1], scene_box[2], z)
glVertexd(scene_box[1], scene_box[5], z)
glVertexd(scene_box[4], scene_box[5], z)
glVertexd(scene_box[4], scene_box[2], z)
-- horizontal
glColord(unpack(horizontal_plane_color))
glVertexd(scene_box[1], y, scene_box[3])
glVertexd(scene_box[1], y, scene_box[6])
glVertexd(scene_box[4], y, scene_box[6])
glVertexd(scene_box[4], y, scene_box[3])
-- sagittal
glColord(unpack(sagittal_plane_color))
glVertexd(x, scene_box[2], scene_box[3])
glVertexd(x, scene_box[2], scene_box[6])
glVertexd(x, scene_box[5], scene_box[6])
glVertexd(x, scene_box[5], scene_box[3])
glEnd()
-- Draw lines where the planes of section intersect
glBegin(GL_LINES)
glColord(0.0, 0.0, 0.0, 1.0) -- back to black
glVertexd(x, y, scene_box[3])
glVertexd(x, y, scene_box[6])
glVertexd(scene_box[1], y, z)
glVertexd(scene_box[4], y, z)
glVertexd(x, scene_box[2], z)
glVertexd(x, scene_box[5], z)
glEnd()
glDepthMask(true)
end
glDisable(GL_DEPTH_TEST)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
local w, h
= max(1, glutGet(GLUT_WINDOW_WIDTH)), max(1, glutGet(GLUT_WINDOW_HEIGHT))
glOrtho(1, w, 1, h, -1, 1)
local str = nil
-- Only draw the 3D mouse coords if the mouse is probably over the
-- surface. This hack on the next line should be replaced with something
-- smarter.
if x ^ 2 + y ^ 2 + z ^ 2 < (0.75 * zfar) ^ 2 then
str = ("Mouse location: (%4.3f, %4.3f, %4.3f)"):format(x, y, z)
draw_bitmap_string("h12", str, 10, h - 20)
end
-- More here: print out other info of interest: frame rate, etc.
if kb_cmd_mode == true then
draw_bitmap_string("h12", "lua> " .. command .. "|", 10, 10)
end
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
if glGetBooleanv(GL_DEPTH_TEST) == GL_TRUE then
glEnable(GL_DEPTH_TEST)
end
if #labels > 0 then
glColord(unpack(label_color))
for _, L in pairs(labels) do
x, y, z, s = unpack(L)
draw_bitmap_string("h12", s, x, y, z)
end
glPointSize(label_point_size)
glBegin(GL_POINTS)
for _, L in pairs(labels) do
x, y, z, s = unpack(L)
glVertexd(x, y, z)
end
glEnd()
glPointSize(1)
glColord(0.0, 0.0, 0.0, 1.0)
end
if drawing_thumbnails == true then
-- These have to be done while we still have the GL
-- matrices and viewport set up from the 3D drawing.
local icor, ihor, isag
= get_indexes_from_mouse()
local wt = bm.coronal_max_width
glViewport(w - wt - 10, 0, wt + 10, h)
glDisable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(1, wt, 1, h, 0, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
-- Figure out which section to draw, based on where the mouse is
-- pointing in 3D.
glPixelZoom(1.0, -1.0) -- GL draws images upside down by default.
-- Draw a white square behind all the images
glColord(1, 1, 1)
local x0, y0, x1, y1
= 1, 1, bm.coronal_max_width, h
glBegin(GL_QUADS)
glVertexd(x0, y0)
glVertexd(x1, y0)
glVertexd(x1, y1)
glVertexd(x0, y1)
glEnd()
glColord(0, 0, 0)
local cor_img = bm.coronal_thumbnails[icor]
h = h - 1
if cor_img ~= nil then
glRasterPosd(1, h)
cor_img:draw_pixels()
end
h = h - bm.coronal_max_height - 1
local sag_img = bm.sagittal_thumbnails[isag]
if sag_img ~= nil then
glRasterPosd(1, h)
sag_img:draw_pixels()
end
h = h - bm.sagittal_max_height - 1
local hor_img = bm.horizontal_thumbnails[ihor]
if hor_img ~= nil then
glRasterPosd(1, h)
hor_img:draw_pixels()
end
set_up_3D_viewport_and_matrices() -- put it back so we can calc mouse coords
end
glFlush()
glutSwapBuffers()
end
-- It's not really zooming but moving the camera toward or away from the
-- object being viewed.
local zoom_to_fit = function()
camera_distance = sqrt(edges[1] ^ 2 + edges[2] ^ 2 + edges[3] ^ 2) / sin(fovy_deg * pi / 360.0) / 2.0
camera_pivot = center
end
bm.set_idle_callback(nil)
zoom_to_fit()
-- Run the given Lua file
local run = function(filename)
local cmd = loadfile(filename)
if cmd ~= nil then
cmd()
else
warn("Could not run " .. filename)
end
end
local restart = function()
run("bminit.lua")
end
local run_user_selected_lua_script = function()
run(bm.get_filename())
end
local browse_coronal = function()
local icor, ihor, isag = get_indexes_from_mouse()
local name = bm.coronal_names[icor]
if name ~= nil then
bm.run_process_in_background(browser_path .. " " .. "\"http://brainmaps.org/index.php?dirname=HBP/m.mulatta/RH04/RH04a/&file=HBP/m.mulatta/RH04/RH04a/" .. name .. "/&win=max\"")
end
end
local browse_horizontal = function()
local icor, ihor, isag = get_indexes_from_mouse()
local name = bm.horizontal_names[ihor]
if name ~= nil then
bm.run_process_in_background(browser_path .. " " .. "\"http://brainmaps.org/index.php?dirname=HBP/m.mulatta/RH10/RH10a/&file=HBP/m.mulatta/RH10/RH10a/" .. name .. "/&win=max\"")
end
end
local browse_sagittal = function()
local icor, ihor, isag = get_indexes_from_mouse()
local name = bm.sagittal_names[isag]
if name ~= nil then
bm.run_process_in_background(browser_path .. " " .. "\"http://brainmaps.org/index.php?dirname=HBP/m.mulatta/RH12/RH12a/&file=HBP/m.mulatta/RH12/RH12a/" .. name .. "/&win=max\"")
end
end
-- Launch web browsers on brainmaps.org for the three sections specified by
-- the mouse cursor's projection onto the brain surface.
local browse_all_three = function()
browse_coronal()
browse_horizontal()
browse_sagittal()
end
local toggle_transparency = function()
doing_transparency = not(doing_transparency)
mesh_color[4] = (doing_transparency and 0.5) or 1.0
end
-- This sets up a temporary idle callback that gradually changes the
-- camera focus to the point in 3D space pointed to by the mouse,
-- while reducing the distance between the camera and that point.
local zoom_in = function()
local dist, src
= camera_distance / 4.0, camera_distance
local x1, y1, z1 = get_mouse_location()
if max(abs(x1), abs(y1), abs(z1)) < 1e4 then
local x0, y0, z0 = unpack(camera_pivot)
local cam_off0 = camera_offset
local cnt = 0
bm.set_idle_callback(function()
cnt = cnt + 1
if cnt > 60 then
bm.set_idle_callback(nil)
end
local t = cnt / 60.0
-- This polynomial u(t) satisfies u(0)=0, u(1)=1,
-- u'(0)=0, u'(1)=0, so we get a smoother transition than
-- we would get by using a linear ramp.
local u = (3.0 - 2.0 * t) * (t ^ 2)
local v = 1.0 - u
for i = 1, #camera_offset do
camera_offset[i] = v * cam_off0[i]
end
camera_pivot = {v * x0 + u * x1, v * y0 + u * y1, v * z0 + u * z1}
camera_distance = v * src + u * dist
end)
else
print("Mouse location is suspiciously large. Is it off the surface?")
end
end
local toggle_thumbnails = function()
drawing_thumbnails = not(drawing_thumbnails)
end
local toggle_position_lock = function()
locked_position = ((locked_position == nil) and {get_mouse_location()}) or nil
end
-- Parts of this function were borrowed from the OpenGL texgen demo.
local make_stripe_fun = function(x, y, z, a)
return function()
local stripe_texture = {}
for i = 0, 3 do
for j = 1, 4 do
stripe_texture[i * 2 + j] = stripe_color[j]
end
end
for i = 4, 127 do
for j = 1, 3 do
stripe_texture[i * 4 + j] = mesh_color[j]
end
stripe_texture[i * 4 + 4] = 1.0 -- opaque
end
gl.glBindTexture(GL_TEXTURE_1D, gl.glGenTextures(1))
gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT)
gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
gl.glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
gl.gluBuild1DMipmaps(GL_TEXTURE_1D, GL_RGBA, 128, GL_RGBA, GL_FLOAT, stripe_texture)
gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
gl.glTexGen(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
gl.glTexGen(GL_S, GL_OBJECT_PLANE, x, y, z, a)
candy_striping = true
end
end
local cycle_through_draw_styles = (function()
local draw_mode_switch = {
[GL_FILL] = GL_LINE,
[GL_LINE] = GL_POINT,
[GL_POINT] = GL_FILL,
}
return function()
draw_mode = draw_mode_switch[draw_mode]
if draw_mode == GL_LINE then
glDisable(GL_LINE_SMOOTH)
else
glEnable(GL_LINE_SMOOTH)
end
gl.glPolygonMode(GL_FRONT_AND_BACK, draw_mode)
end
end)()
local help = function()
bm.run_process_in_background(pager_path .. " help.txt")
end
local toggle_full_screen = (function()
local fullscreen = false
local last_width, last_height
return function()
fullscreen = not(fullscreen)
if fullscreen == true then
last_width, last_height = max(1, glutGet(GLUT_WINDOW_WIDTH)), max(1, glutGet(GLUT_WINDOW_HEIGHT))
glutFullScreen()
else
glutReshapeWindow(last_width, last_height)
end
end
end)()
local quit = function()
os.exit(true)
end
local no_stripes = function()
candy_striping = false
end
-- Global function add_label() for executing labels.txt
add_label = function(x, y, z, text)
print(("adding label: %3.3f %3.3f %3.3f %s"):format(x, y, z, text))
table.insert(labels, {x, y, z, text})
end
local begin_command_mode = function()
kb_cmd_mode = true
end
local load_labels = function()
print("load_labels")
local file = io.open("labels.txt")
if file == nil then
print("== Result of wget request ==")
os.execute("wget http://brainmaps.org/labels.txt")
else
file:close()
end
dofile("labels.txt")
end
local calc_camera_coords = function()
local theta, phi
= -y_angle_deg / 180.0 * pi, -x_angle_deg / 180.0 * pi
local cx, cy, cz = unpack(camera_pivot)
return cx + camera_distance * cos(phi) * sin(theta), cy + camera_distance * sin(phi), cz + camera_distance * cos(phi) * cos(theta)
end
local add_mouse_label = function()
local x, y, z = get_mouse_location()
add_label(x, y, z, "*")
end
---------------
-- Menu
---------------
do
local menu_items = {
{ text="Zoom in (i)", func=zoom_in },
{ text="Zoom to fit (f)", func=zoom_to_fit },
{ text="Toggle full screen (F)", func=toggle_full_screen },
{ text="Toggle transparency (T)", func=toggle_transparency },
{ text="Toggle thumbnails (t)", func=toggle_thumbnails },
{ text="Toggle position lock (l)", func=toggle_position_lock },
{ text="Browse coronal (c)", func=browse_coronal },
{ text="Browse horizontal (h)", func=browse_horizontal },
{ text="Browse sagittal (s)", func=browse_sagittal },
{ text="Browse all three (b)", func=browse_all_three },
{ text="X stripes (x)", func=make_stripe_fun(2, 0, 0, 0) },
{ text="Y stripes (y)", func=make_stripe_fun(0, 2, 0, 0) },
{ text="Z stripes (z)", func=make_stripe_fun(0, 0, 2, 0) },
{ text="Cycle through draw styles (m)", func=cycle_through_draw_styles },
{ text="No stripes (n)", func=no_stripes },
{ text="Load labels (L)", func=load_labels },
{ text="Add label (p)", func=add_mouse_label },
{ text="Lua prompt (:)", func=begin_command_mode },
{ text="Run Lua script", func=run_user_selected_lua_script },
{ text="Help (?)", func=help },
{ text="Restart (r)", func=restart },
{ text="Quit (q)", func=quit },
}
bm.reset_menu()
for _, v in pairs(menu_items) do
bm.add_menu_item(v.text, v.func)
end
end
on_mouse = function(button, state, xi, yi)
shift_is_pressed = (glutGetModifiers() & GLUT_ACTIVE_SHIFT) ~= 0
ctrl_is_pressed = (glutGetModifiers() & GLUT_ACTIVE_CTRL ) ~= 0
if state == GLUT_DOWN then
if button == GLUT_LEFT then
mouse_xi, mouse_yi = xi, yi
end
end
end
on_motion = function(xi, yi)
if shift_is_pressed == true then
-- move camera closer in or further out
camera_distance = camera_distance / exp(0.01 * (yi - mouse_yi))
elseif ctrl_is_pressed == true then
local h = max(1, glutGet(GLUT_WINDOW_HEIGHT))
-- z=0 at near clipping plane (http://www.opengl.org/resources/faq/technical/glu.htm)
local z = camera_distance / (zfar - znear)
glPushMatrix()
glLoadIdentity()
local pprev = {gluUnProject(mouse_xi, h - mouse_yi - 1, z)}
local p = {gluUnProject(xi, h - yi - 1, z)}
glPopMatrix()
for i = 1, #camera_offset do
camera_offset[i] = camera_offset[i] + edges[i] * (p[i] - pprev[i])
end
else
y_angle_deg = y_angle_deg + 0.5 * (xi - mouse_xi)
x_angle_deg = x_angle_deg - 0.5 * (yi - mouse_yi)
end
mouse_xi, mouse_yi = xi, yi
glutPostRedisplay()
end
on_passive_motion = function(xi, yi)
mouse_xi, mouse_yi = xi, yi;
glutPostRedisplay()
end
---------------
-- Key bindings
---------------
local key_bindings = {
['C'] = function()
print(("camera: %.3f %.3f %.3f"):format(calc_camera_coords()))
end,
['L'] = load_labels,
['m'] = cycle_through_draw_styles,
['P'] = function()
print(("%4.3f %4.3f %4.3f"):format(get_mouse_location()))
end,
['p'] = add_mouse_label,
['?'] = help,
['r'] = restart,
['h'] = browse_horizontal,
['s'] = browse_sagittal,
['c'] = browse_coronal,
['b'] = browse_all_three,
['f'] = zoom_to_fit,
['i'] = zoom_in,
['t'] = toggle_thumbnails,
['F'] = toggle_full_screen,
['T'] = toggle_transparency,
['x'] = make_stripe_fun(2, 0, 0, 0),
['y'] = make_stripe_fun(0, 2, 0, 0),
['z'] = make_stripe_fun(0, 0, 2, 0),
['n'] = no_stripes,
['l'] = toggle_position_lock,
['q'] = quit,
-- Bring up a lua command prompt, for power users
[':'] = begin_command_mode,
}
on_keyboard = function(key, xi, yi)
if kb_cmd_mode == true then
-- If it's Enter, then try to execute the command.
if key == 13 then
local loaded_cmd = loadstring(command)
if loaded_cmd ~= nil then
local status, result = pcall(loaded_cmd)
if status ~= false then
if result ~= nil then
print(result)
end
else
warn("Lua error. ", result)
end
else
warn("Lua syntax error.")
end
km_cmd_mode = 0
command = ""
-- If it's backspace or delete, then try to delete the last
-- character in the command.
elseif key == 8 or key == 127 then
command = command:sub(1, -2)
-- If it's Esc or ctrl-c, then abort the command
elseif key == 27 or key == 3 then
kb_cmd_mode = false
command = ""
else
-- Otherwise, add the character to the command.
command = command .. string.char(key)
end
else
local f = key_bindings[string.char(key)]
if f ~= nil then
f(xi, yi)
end
end
glutPostRedisplay()
end