-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_js.go
1316 lines (1216 loc) · 37 KB
/
main_js.go
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"errors"
"fmt"
"math"
"runtime"
"syscall/js"
"time"
"github.com/seqsense/pcgol/mat"
"github.com/seqsense/pcgol/pc"
webgl "github.com/seqsense/webgl-go"
)
const (
vib3DXAmp = 0.002
maxDrawArraysPoints = 30000000 // Firefox's limit
)
var (
Version = "unknown"
BuildDate = "unknown"
)
func main() {
println("pcdeditor", Version, BuildDate)
cb := js.Global().Get("document").Get("onPCDEditorLoaded")
if !cb.IsNull() {
cb.Invoke(
map[string]interface{}{
"attach": js.FuncOf(newPCDEditor),
},
)
}
select {}
}
type promiseCommand struct {
data interface{}
resolved func(interface{})
rejected func(error)
}
type pcdeditor struct {
canvas js.Value
logPrint func(msg interface{})
chImportPCD chan promiseCommand
chImportSubPCD chan promiseCommand
chImport2D chan promiseCommand
chExportPCD chan promiseCommand
chExportSelectedPCD chan promiseCommand
chReset chan promiseCommand
chCommand chan promiseCommand
chWheel chan webgl.WheelEvent
chClick chan webgl.MouseEvent
chMouseDown chan webgl.MouseEvent
chMouseDrag chan webgl.MouseEvent
chMouseMove chan webgl.MouseEvent
chMouseUp chan webgl.MouseEvent
chKey chan webgl.KeyboardEvent
ch2D chan promiseCommand
chContextLost chan webgl.WebGLContextEvent
chContextRestored chan webgl.WebGLContextEvent
vi *viewImpl
cg *clickGuard
cmd *commandContext
cs *console
onKeyDownHook func(webgl.KeyboardEvent)
}
func newPCDEditor(this js.Value, args []js.Value) interface{} {
canvas := args[0]
pe := &pcdeditor{
canvas: canvas,
logPrint: func(msg interface{}) {
fmt.Println(msg)
},
chImportPCD: make(chan promiseCommand, 1),
chImportSubPCD: make(chan promiseCommand, 1),
chImport2D: make(chan promiseCommand, 1),
chExportPCD: make(chan promiseCommand, 1),
chExportSelectedPCD: make(chan promiseCommand, 1),
chReset: make(chan promiseCommand, 1),
chCommand: make(chan promiseCommand, 1),
chWheel: make(chan webgl.WheelEvent, 10),
chClick: make(chan webgl.MouseEvent, 10),
chMouseDown: make(chan webgl.MouseEvent, 10),
chMouseDrag: make(chan webgl.MouseEvent, 10),
chMouseMove: make(chan webgl.MouseEvent, 10),
chMouseUp: make(chan webgl.MouseEvent, 10),
chKey: make(chan webgl.KeyboardEvent, 10),
ch2D: make(chan promiseCommand, 1),
chContextLost: make(chan webgl.WebGLContextEvent, 1),
chContextRestored: make(chan webgl.WebGLContextEvent, 1),
vi: newView(),
cg: &clickGuard{},
cmd: newCommandContext(&pcdIOImpl{}, &mapIOImpl{}),
}
pe.cs = &console{cmd: pe.cmd, view: pe.vi}
if len(args) > 1 {
init := args[1]
if logger := init.Get("logger"); !logger.IsUndefined() {
pe.logPrint = func(msg interface{}) {
logger.Invoke(fmt.Sprintf("%v", msg))
}
}
if onKeyDownHook := init.Get("onKeyDownHook"); !onKeyDownHook.IsUndefined() {
pe.onKeyDownHook = func(e webgl.KeyboardEvent) {
onKeyDownHook.Invoke(e.JS())
}
}
}
ctx, cancel := context.WithCancel(context.Background())
go pe.Run(ctx)
return js.ValueOf(map[string]interface{}{
"importPCD": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chImportPCD, args[0])
}),
"importSubPCD": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chImportSubPCD, args[0])
}),
"import2D": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chImport2D, [2]js.Value{args[0], args[1]})
}),
"exportPCD": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chExportPCD, nil)
}),
"exportSelectedPCD": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chExportSelectedPCD, nil)
}),
"command": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chCommand, args[0].String())
}),
"show2D": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.ch2D, args[0].Bool())
}),
"reset": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return newCommandPromise(pe.chReset, nil)
}),
"exit": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
cancel()
return nil
}),
})
}
func newCommandPromise(ch chan promiseCommand, data interface{}) js.Value {
promise := js.Global().Get("Promise")
return promise.New(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve, reject := args[0], args[1]
cmd := promiseCommand{
data: data,
resolved: func(res interface{}) {
switch r := res.(type) {
case [][]float32:
jm := js.Global().Get("Array").New(len(r))
for i, vec := range r {
jv := js.Global().Get("Array").New(len(vec))
for j, val := range vec {
jv.SetIndex(j, js.ValueOf(val))
}
jm.SetIndex(i, jv)
}
resolve.Invoke(jm)
default:
resolve.Invoke(res)
}
},
rejected: func(err error) { reject.Invoke(errorToJS(err)) },
}
select {
case ch <- cmd:
return nil
default:
reject.Invoke()
return nil
}
}))
}
func (pe *pcdeditor) Run(ctx context.Context) {
canvas := webgl.Canvas(pe.canvas)
canvas.OnClick(func(e webgl.MouseEvent) {
e.PreventDefault()
e.StopPropagation()
select {
case pe.chClick <- e:
default:
}
})
canvas.OnContextMenu(func(e webgl.MouseEvent) {
e.PreventDefault()
e.StopPropagation()
})
wheelHandler := func(e webgl.WheelEvent) {
e.PreventDefault()
e.StopPropagation()
select {
case pe.chWheel <- e:
default:
}
}
canvas.OnWheel(wheelHandler)
gesture := &gesture{
canvas: canvas,
onClick: func(e webgl.MouseEvent) {
select {
case pe.chClick <- e:
default:
}
},
onMouseDown: func(e webgl.MouseEvent) {
select {
case pe.chMouseDown <- e:
default:
}
},
onMouseDrag: func(e webgl.MouseEvent) {
select {
case pe.chMouseDrag <- e:
default:
}
},
onMouseUp: func(e webgl.MouseEvent) {
select {
case pe.chMouseUp <- e:
default:
}
},
onWheel: wheelHandler,
}
canvas.OnTouchStart(gesture.touchStart)
canvas.OnTouchMove(gesture.touchMove)
canvas.OnTouchEnd(gesture.touchEnd)
canvas.OnTouchCancel(gesture.touchEnd)
mouseDragging := webgl.MouseButtonNull
canvas.OnMouseUp(func(e webgl.MouseEvent) {
e.PreventDefault()
e.StopPropagation()
select {
case pe.chMouseUp <- e:
if mouseDragging == e.Button {
mouseDragging = webgl.MouseButtonNull
}
default:
}
})
canvas.OnMouseDown(func(e webgl.MouseEvent) {
e.PreventDefault()
e.StopPropagation()
select {
case pe.chMouseDown <- e:
mouseDragging = e.Button
default:
}
})
var lastMoveEvent *webgl.MouseEvent
dispatchMoveEvent := func(e webgl.MouseEvent) {
if e.Button == mouseDragging {
select {
case pe.chMouseDrag <- e:
default:
}
} else {
select {
case pe.chMouseMove <- e:
default:
}
}
}
canvas.OnMouseMove(func(e webgl.MouseEvent) {
e.PreventDefault()
e.StopPropagation()
if mouseDragging != webgl.MouseButtonNull {
e.Button = mouseDragging
}
lastMoveEvent = &e
dispatchMoveEvent(e)
})
updateMove := func(e webgl.KeyboardEvent) {
if lastMoveEvent != nil &&
(e.ShiftKey != lastMoveEvent.ShiftKey ||
e.CtrlKey != lastMoveEvent.CtrlKey ||
e.AltKey != lastMoveEvent.AltKey) {
// State of shift/ctrl/alt key is changed during drag
lastMoveEvent.ShiftKey = e.ShiftKey
lastMoveEvent.CtrlKey = e.CtrlKey
lastMoveEvent.AltKey = e.AltKey
dispatchMoveEvent(*lastMoveEvent)
}
}
canvas.OnKeyDown(func(e webgl.KeyboardEvent) {
if pe.onKeyDownHook != nil {
pe.onKeyDownHook(e)
}
e.PreventDefault()
e.StopPropagation()
select {
case pe.chKey <- e:
default:
}
updateMove(e)
})
canvas.OnKeyUp(func(e webgl.KeyboardEvent) {
e.PreventDefault()
e.StopPropagation()
updateMove(e)
})
canvas.OnWebGLContextLost(func(e webgl.WebGLContextEvent) {
e.PreventDefault()
e.StopPropagation()
pe.chContextLost <- e
})
canvas.OnWebGLContextRestored(func(e webgl.WebGLContextEvent) {
pe.chContextRestored <- e
})
for {
err := pe.runImpl(ctx)
switch err {
case errContextLostEvent:
// Received context lost event from the browser.
pe.logPrint("Waiting WebGL context restore")
<-pe.chContextRestored
pe.logPrint("WebGL context restored")
case errContextLost:
// WebGL context is not available during initialization.
time.Sleep(time.Second)
pe.logPrint("Retrying")
case nil:
pe.logPrint("Exiting")
println("pcdeditor exiting")
return
default:
pe.logPrint("Fatal: " + err.Error())
return
}
}
}
func (pe *pcdeditor) runImpl(ctx context.Context) error {
gl, err := webgl.New(pe.canvas)
if err != nil {
return err
}
showDebugInfo(gl)
vs, err := initVertexShader(gl, vsSource)
if err != nil {
return err
}
vsSub, err := initVertexShader(gl, vsSubSource)
if err != nil {
return err
}
vsSel, err := initVertexShader(gl, vsSelectSource)
if err != nil {
return err
}
vsMap, err := initVertexShader(gl, vsMapSource)
if err != nil {
return err
}
csComputeSelect, err := initVertexShader(gl, csComputeSelectSource)
if err != nil {
return err
}
fs, err := initFragmentShader(gl, fsSource)
if err != nil {
return err
}
fsMap, err := initFragmentShader(gl, fsMapSource)
if err != nil {
return err
}
fsComputeSelect, err := initFragmentShader(gl, fsComputeSelectSource)
if err != nil {
return err
}
program, err := linkShaders(gl, nil, vs, fs)
if err != nil {
return err
}
programSub, err := linkShaders(gl, nil, vsSub, fs)
if err != nil {
return err
}
programSel, err := linkShaders(gl, nil, vsSel, fs)
if err != nil {
return err
}
programMap, err := linkShaders(gl, nil, vsMap, fsMap)
if err != nil {
return err
}
programComputeSelect, err := linkShaders(gl, []string{"oResult"}, csComputeSelect, fsComputeSelect)
if err != nil {
return err
}
tf := gl.CreateTransformFeedback()
gl.BindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf)
uProjectionMatrixLocation := gl.GetUniformLocation(program, "uProjectionMatrix")
uCropMatrixLocation := gl.GetUniformLocation(program, "uCropMatrix")
uModelViewMatrixLocation := gl.GetUniformLocation(program, "uModelViewMatrix")
uSelectMatrixLocation := gl.GetUniformLocation(program, "uSelectMatrix")
uZMinLocation := gl.GetUniformLocation(program, "uZMin")
uZRangeLocation := gl.GetUniformLocation(program, "uZRange")
uPointSizeBase := gl.GetUniformLocation(program, "uPointSizeBase")
uUseSelectMask := gl.GetUniformLocation(program, "uUseSelectMask")
uMinLabel := gl.GetUniformLocation(program, "uMinLabel")
uMaxLabel := gl.GetUniformLocation(program, "uMaxLabel")
uProjectionMatrixLocationSub := gl.GetUniformLocation(programSub, "uProjectionMatrix")
uModelViewMatrixLocationSub := gl.GetUniformLocation(programSub, "uModelViewMatrix")
uPointSizeBaseSub := gl.GetUniformLocation(programSub, "uPointSizeBase")
uProjectionMatrixLocationSel := gl.GetUniformLocation(programSel, "uProjectionMatrix")
uModelViewMatrixLocationSel := gl.GetUniformLocation(programSel, "uModelViewMatrix")
uPointSizeBaseSel := gl.GetUniformLocation(programSel, "uPointSizeBase")
uProjectionMatrixLocationMap := gl.GetUniformLocation(programMap, "uProjectionMatrix")
uModelViewMatrixLocationMap := gl.GetUniformLocation(programMap, "uModelViewMatrix")
uSamplerLocationMap := gl.GetUniformLocation(programMap, "uSampler")
uAlphaLocationMap := gl.GetUniformLocation(programMap, "uAlpha")
uCropMatrixLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uCropMatrix")
uSelectMatrixLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uSelectMatrix")
uProjectionMatrixLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uProjectionMatrix")
uModelViewMatrixLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uModelViewMatrix")
uOriginLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uOrigin")
uDirLocationComputeSelect := gl.GetUniformLocation(programComputeSelect, "uDir")
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LEQUAL)
posBuf := gl.CreateBuffer()
posSubBuf := gl.CreateBuffer()
mapBuf := gl.CreateBuffer()
selectResultBuf := gl.CreateBuffer()
selectMaskBuf := gl.CreateBuffer()
toolBuf := gl.CreateBuffer()
var selectResultJS js.Value
var selectResultGo []byte
tick := time.NewTicker(time.Second / 8)
defer tick.Stop()
var fov float32
var projectionMatrix, modelViewMatrix mat.Mat4
var width, height int
var distance float64
var projectionType ProjectionType
var vib3D bool
var vib3DX float32
var nRectPoints int
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.ClearDepth(1.0)
const (
aVertexPosition = 0
aVertexLabel = 1
aTextureCoordMap = 1
aSelectMask = 2
)
devicePixelRatioJS := js.Global().Get("window").Get("devicePixelRatio")
wheelNormalizer := &wheelNormalizer{}
texture := gl.CreateTexture()
mapRect := &pc.PointCloud{
PointCloudHeader: pc.PointCloudHeader{
Fields: []string{"x", "y", "z", "u", "v"},
Size: []int{4, 4, 4, 4, 4},
Count: []int{1, 1, 1, 1, 1},
},
Points: 5,
Data: make([]byte, 5*4*5),
}
var selectMaskData webgl.ByteArrayBuffer
var pcCursor *pc.PointCloud
var moveStart *mat.Vec3
var show2D bool = true
// Allow export after crash
defer func() {
if r := recover(); r != nil {
pe.logPrint("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
for d := 0; ; d++ {
ptr, file, line, ok := runtime.Caller(d)
if !ok {
break
}
f := runtime.FuncForPC(ptr)
fmt.Printf("%s:%d: %s\n", file, line, f.Name())
}
pe.logPrint(r)
if pp, _, ok := pe.cmd.PointCloud(); ok {
pe.logPrint("CRASHED (export command is available)")
pe.logPrint("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
for promise := range pe.chExportPCD {
blob, err := pe.cmd.pcdIO.exportPCD(pp)
if err != nil {
promise.rejected(err)
continue
}
promise.resolved(blob)
}
} else {
pe.logPrint("CRASHED")
pe.logPrint("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
}
}
}()
forceReload := true
pe.logPrint("WebGL context initialized")
L_MAIN:
for {
scale := float32(devicePixelRatioJS.Float())
scaled := func(v int) int {
return int(float32(v) * scale)
}
newWidth := scaled(gl.Canvas.ClientWidth())
newHeight := scaled(gl.Canvas.ClientHeight())
newProjectionType := pe.cmd.ProjectionType()
newDistance := pe.vi.distance
newFOV := pe.vi.fov
if forceReload || newWidth != width || newHeight != height || newFOV != fov || projectionType != newProjectionType || (newProjectionType == ProjectionOrthographic && newDistance != distance) {
width, height = newWidth, newHeight
projectionType = newProjectionType
distance = newDistance
fov = newFOV
gl.Canvas.SetWidth(width)
gl.Canvas.SetHeight(height)
switch projectionType {
case ProjectionPerspective:
projectionMatrix = mat.Perspective(
fov,
float32(width)/float32(height),
1.0, 1000.0,
)
case ProjectionOrthographic:
projectionMatrix = mat.Orthographic(
-float32(width/2)*float32(distance)/1000,
float32(width/2)*float32(distance)/1000,
float32(height/2)*float32(distance)/1000,
-float32(height/2)*float32(distance)/1000,
-1000, 1000.0,
)
}
gl.UseProgram(program)
gl.UniformMatrix4fv(uProjectionMatrixLocation, false, projectionMatrix)
gl.UseProgram(programSub)
gl.UniformMatrix4fv(uProjectionMatrixLocationSub, false, projectionMatrix)
gl.UseProgram(programSel)
gl.UniformMatrix4fv(uProjectionMatrixLocationSel, false, projectionMatrix)
gl.UseProgram(programMap)
gl.UniformMatrix4fv(uProjectionMatrixLocationMap, false, projectionMatrix)
gl.UseProgram(programComputeSelect)
gl.UniformMatrix4fv(uProjectionMatrixLocationComputeSelect, false, projectionMatrix)
gl.Viewport(0, 0, width, height)
}
modelViewMatrix = mat.Rotate(1, 0, 0, -float32(pe.vi.pitch)).
MulAffine(mat.Rotate(0, 0, 1, -float32(pe.vi.yaw))).
MulAffine(mat.Translate(float32(pe.vi.x), float32(pe.vi.y), -1.5))
if projectionType == ProjectionPerspective {
modelViewMatrix =
mat.Translate(vib3DX, 0, -float32(pe.vi.distance)).MulAffine(modelViewMatrix)
}
if rect, updated := pe.cmd.Rect(); updated || forceReload {
// Send select box vertices to GPU
buf := make([]float32, 0, len(rect)*3)
for _, p := range rect {
buf = append(buf, p[0], p[1], p[2])
}
nRectPoints = len(rect)
if len(rect) > 0 {
pcCursor = &pc.PointCloud{
PointCloudHeader: pc.PointCloudHeader{
Fields: []string{"x", "y", "z"},
Size: []int{4, 4, 4},
Type: []string{"F", "F", "F"},
Count: []int{1, 1, 1},
Width: nRectPoints,
Height: 1,
},
Points: nRectPoints,
}
pcCursor.Data = make([]byte, nRectPoints*pcCursor.Stride())
it, _ := pcCursor.Vec3Iterator()
for _, p := range rect {
it.SetVec3(p)
it.Incr()
}
gl.BindBuffer(gl.ARRAY_BUFFER, toolBuf)
gl.BufferData(gl.ARRAY_BUFFER, webgl.Float32ArrayBuffer(buf), gl.STATIC_DRAW)
}
}
mi, img, mapUpdated, has2D := pe.cmd.Map()
if has2D && (mapUpdated || forceReload) {
// Send 2D map texture to GPU
err0 := gl.GetError()
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img.Interface().(js.Value))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.BindTexture(gl.TEXTURE_2D, webgl.Texture(nil))
if err := gl.GetError(); err0 == nil && err != nil {
pe.logPrint(fmt.Sprintf("Failed to render 2D map image (%v): 2D map image size may be too large for your graphic card", err))
}
gl.UseProgram(programMap)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.Uniform1i(uSamplerLocationMap, 0)
w, h := img.Width(), img.Height()
xi, _ := mapRect.Float32Iterator("x")
yi, _ := mapRect.Float32Iterator("y")
ui, _ := mapRect.Float32Iterator("u")
vi, _ := mapRect.Float32Iterator("v")
push := func(x, y, u, v float32) {
xi.SetFloat32(x)
yi.SetFloat32(y)
ui.SetFloat32(u)
vi.SetFloat32(v)
xi.Incr()
yi.Incr()
ui.Incr()
vi.Incr()
}
push(mi.Origin[0], mi.Origin[1], 0, 1)
push(mi.Origin[0]+float32(w)*mi.Resolution, mi.Origin[1], 1, 1)
push(mi.Origin[0]+float32(w)*mi.Resolution, mi.Origin[1]+float32(h)*mi.Resolution, 1, 0)
push(mi.Origin[0], mi.Origin[1]+float32(h)*mi.Resolution, 0, 0)
push(mi.Origin[0], mi.Origin[1], 0, 1)
gl.BindBuffer(gl.ARRAY_BUFFER, mapBuf)
gl.BufferData(gl.ARRAY_BUFFER, webgl.ByteArrayBuffer(mapRect.Data), gl.STATIC_DRAW)
}
updateSelectMask := func() {
gl.UseProgram(program)
gl.BindBuffer(gl.ARRAY_BUFFER, selectMaskBuf)
gl.BufferData(gl.ARRAY_BUFFER, selectMaskData, gl.STATIC_DRAW)
}
pp, updatedPointCloud, hasPointCloud := pe.cmd.PointCloud()
if hasPointCloud && (updatedPointCloud || forceReload) && pp.Points > 0 {
// Send PointCloud vertices to GPU
gl.BindBuffer(gl.ARRAY_BUFFER, posBuf)
gl.BufferData(gl.ARRAY_BUFFER, webgl.ByteArrayBuffer(pp.Data), gl.STATIC_DRAW)
// Re-allocate buffer only when pointcloud size is changed
if nBuf := pp.Points * 4; nBuf != len(selectResultGo) {
// Register buffer to receive GPGPU processing result
selectResultJS = js.Global().Get("Uint8Array").New(nBuf)
if cap(selectResultGo) < nBuf {
selectResultGo = make([]byte, nBuf)
}
selectResultGo = selectResultGo[:nBuf:nBuf]
selectMaskData = webgl.ByteArrayBuffer(selectResultGo)
}
gl.BindBuffer(gl.ARRAY_BUFFER, selectResultBuf)
gl.BufferData_JS(gl.ARRAY_BUFFER, js.ValueOf(selectResultJS), gl.STREAM_READ)
updateSelectMask()
}
ppSub, updatedSubPointCloud, hasSubPointCloud := pe.cmd.SubPointCloud()
if hasSubPointCloud && (updatedSubPointCloud || forceReload) && ppSub.Points > 0 {
// Send PointCloud vertices to GPU
gl.BindBuffer(gl.ARRAY_BUFFER, posSubBuf)
gl.BufferData(gl.ARRAY_BUFFER, webgl.ByteArrayBuffer(ppSub.Data), gl.STATIC_DRAW)
}
render := func() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
pointSize := pe.cmd.PointSize()
selectMode := pe.cmd.SelectMode()
samplingRatio := 1
if pe.vi.dragging() {
totalPoints := 0
maxStride := 4
if hasPointCloud && pp.Points > 0 {
totalPoints += pp.Points
maxStride = max(pp.Stride(), maxStride)
}
if hasSubPointCloud && ppSub.Points > 0 && selectMode == selectModeInsert {
totalPoints += ppSub.Points
maxStride = max(ppSub.Stride(), maxStride)
}
samplingRatio = 1 + totalPoints/pe.cmd.NumFastRenderPoints()
// make sure the stride used in gl.VertexAttribIPointer is <= 255
if samplingRatio*maxStride > 255 {
samplingRatio = int(255.0 / maxStride)
}
}
if hasPointCloud && pp.Points > 0 {
// Render PointCloud
gl.UseProgram(program)
clean := enableVertexAttribs(gl, aVertexPosition, aVertexLabel, aSelectMask)
switch selectMode {
case selectModeRect, selectModeInsert:
gl.Uniform1i(uUseSelectMask, 0)
case selectModeMask:
gl.Uniform1i(uUseSelectMask, 1)
}
renderLabelMin, renderLabelMax := pe.cmd.RenderLabelRange()
gl.Uniform1ui(uMinLabel, renderLabelMin)
gl.Uniform1ui(uMaxLabel, renderLabelMax)
gl.BindBuffer(gl.ARRAY_BUFFER, posBuf)
sampledStride := pp.Stride() * samplingRatio
gl.VertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, sampledStride, 0)
gl.VertexAttribIPointer(aVertexLabel, 1, gl.UNSIGNED_INT, sampledStride, 3*4)
gl.UniformMatrix4fv(uModelViewMatrixLocation, false, modelViewMatrix)
gl.UniformMatrix4fv(uCropMatrixLocation, false, pe.cmd.CropMatrix())
gl.BindBuffer(gl.ARRAY_BUFFER, selectMaskBuf)
gl.VertexAttribIPointer(aSelectMask, 1, gl.UNSIGNED_INT, 4*samplingRatio, 0)
zMin, zMax := pe.cmd.ZRange()
gl.Uniform1f(uZMinLocation, zMin)
gl.Uniform1f(uZRangeLocation, zMax-zMin)
mSel, _ := pe.cmd.SelectMatrix()
gl.UniformMatrix4fv(uSelectMatrixLocation, false, mSel)
gl.Uniform1f(uPointSizeBase, pointSize)
n := pp.Points / samplingRatio
for i := 0; i < n; i += maxDrawArraysPoints {
gl.DrawArrays(gl.POINTS, i, min(n-i, maxDrawArraysPoints)-1)
}
clean()
}
if hasSubPointCloud && ppSub.Points > 0 && selectMode == selectModeInsert {
// Render sub PointCloud
cursors := pe.cmd.Cursors()
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.UseProgram(programSub)
clean := enableVertexAttribs(gl, aVertexPosition)
gl.BindBuffer(gl.ARRAY_BUFFER, posSubBuf)
gl.VertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, ppSub.Stride()*samplingRatio, 0)
trans := cursorsToTrans(cursors)
gl.UniformMatrix4fv(
uModelViewMatrixLocationSub, false,
modelViewMatrix.Mul(trans),
)
gl.Uniform1f(uPointSizeBaseSub, pointSize)
n := ppSub.Points / samplingRatio
for i := 0; i < n; i += maxDrawArraysPoints {
gl.DrawArrays(gl.POINTS, i, min(n-i, maxDrawArraysPoints)-1)
}
gl.Disable(gl.BLEND)
clean()
}
if nRectPoints > 0 && (selectMode == selectModeRect || selectMode == selectModeInsert) {
// Render select box
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.UseProgram(programSel)
clean := enableVertexAttribs(gl, aVertexPosition)
for i := 0; i < nRectPoints; i += 4 {
gl.BindBuffer(gl.ARRAY_BUFFER, toolBuf)
gl.VertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, 3*4, 3*4*i)
n := 4
if n > nRectPoints-i {
n = nRectPoints - i
}
gl.UniformMatrix4fv(uModelViewMatrixLocationSel, false, modelViewMatrix)
gl.Uniform1f(uPointSizeBaseSel, pointSize)
gl.DrawArrays(gl.LINE_LOOP, 0, n)
gl.DrawArrays(gl.POINTS, 0, n)
}
gl.Disable(gl.BLEND)
clean()
}
if show2D && has2D {
// Render 2D map
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.UseProgram(programMap)
clean := enableVertexAttribs(gl, aVertexPosition, aTextureCoordMap)
gl.BindBuffer(gl.ARRAY_BUFFER, mapBuf)
gl.VertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, mapRect.Stride(), 0)
gl.VertexAttribPointer(aTextureCoordMap, 2, gl.FLOAT, false, mapRect.Stride(), 4*3)
gl.UniformMatrix4fv(uModelViewMatrixLocationMap, false, modelViewMatrix)
gl.Uniform1f(uAlphaLocationMap, pe.cmd.MapAlpha())
gl.DrawArrays(gl.TRIANGLE_FAN, 0, 5)
gl.Disable(gl.BLEND)
clean()
}
}
render()
// Calculate condition of each point by GPU
// It checks that the point is
// - in the crop box
// - in the select box
// - close to the mouse cursor position given as (x, y)
scanSelection := func(x, y int) bool {
if hasPointCloud && pp.Points > 0 {
origin, dir := perspectiveOriginDir(x, y, width, height, &projectionMatrix, &modelViewMatrix)
// Run GPGPU shader
gl.UseProgram(programComputeSelect)
clean := enableVertexAttribs(gl, aVertexPosition, aSelectMask)
defer clean()
gl.BindBuffer(gl.ARRAY_BUFFER, posBuf)
gl.VertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, pp.Stride(), 0)
gl.BindBuffer(gl.ARRAY_BUFFER, selectMaskBuf)
gl.VertexAttribIPointer(aSelectMask, 1, gl.UNSIGNED_INT, 4, 0)
gl.UniformMatrix4fv(uCropMatrixLocationComputeSelect, false, pe.cmd.CropMatrix())
gl.UniformMatrix4fv(uModelViewMatrixLocationComputeSelect, false, modelViewMatrix)
mSel, _ := pe.cmd.SelectMatrix()
gl.UniformMatrix4fv(uSelectMatrixLocationComputeSelect, false, mSel)
gl.Uniform3fv(uOriginLocationComputeSelect, *origin)
gl.Uniform3fv(uDirLocationComputeSelect, *dir)
gl.BindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, selectResultBuf)
gl.Enable(gl.RASTERIZER_DISCARD)
gl.BeginTransformFeedback(gl.POINTS)
for i, j := 0, 0; i < pp.Points; i, j = i+maxDrawArraysPoints, j+1 {
gl.DrawArrays(gl.POINTS, i, min(pp.Points-i, maxDrawArraysPoints)-1)
}
gl.EndTransformFeedback()
gl.Disable(gl.RASTERIZER_DISCARD)
gl.BindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, webgl.Buffer(js.Null()))
fence := gl.FenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0)
defer func() {
gl.DeleteSync(fence)
}()
// Re-render to avoid blank screen on Firefox
render()
// Switch execution frame first to ensure state update
time.Sleep(time.Millisecond)
// Wait calculation on GPU
L_SYNC:
for failCnt := 0; ; {
switch gl.ClientWaitSync(fence, 0, 0) {
case gl.ALREADY_SIGNALED, gl.CONDITION_SATISFIED:
break L_SYNC
case gl.WAIT_FAILED:
if failCnt++; failCnt > 10 {
return false
}
}
time.Sleep(10 * time.Millisecond)
}
// Get result from GPU
gl.BindBuffer(gl.ARRAY_BUFFER, selectResultBuf)
gl.GetBufferSubData(gl.ARRAY_BUFFER, 0, selectResultJS, 0, 0)
js.CopyBytesToGo(selectResultGo, selectResultJS)
pe.cmd.SetSelectMask(webgl.ByteArrayBuffer(selectResultGo).UInt32Slice())
return true
}
return false
}
// Check the cursor is on select box vertices
cursorOnSelect := func(e webgl.MouseEvent) (*mat.Vec3, bool) {
if nRectPoints == 0 || pcCursor == nil {
return nil, false
}
return selectPoint(
pcCursor, nil, projectionType, &modelViewMatrix, &projectionMatrix,
scaled(e.OffsetX), scaled(e.OffsetY), width, height, rectSelectRange,
)
}
forceReload = false
// Handle inputs
L_INPUT:
for {
select {
case promise := <-pe.chImportPCD:
pe.logPrint("importing pcd")
if err := pe.cmd.ImportPCD(promise.data); err != nil {
promise.rejected(err)
break
}
pe.logPrint("pcd loaded")
promise.resolved("loaded")
case promise := <-pe.chImportSubPCD:
pe.logPrint("importing sub pcd")
if err := pe.cmd.ImportSubPCD(promise.data); err != nil {
promise.rejected(err)
break
}
pe.logPrint("sub pcd loaded")
promise.resolved("loaded")
case promise := <-pe.chImport2D:
pe.logPrint("loading 2D map")
data := promise.data.([2]js.Value)
if err := pe.cmd.Import2D(data[0], data[1]); err != nil {
promise.rejected(err)
break
}
pe.logPrint("2D map loaded")
promise.resolved("loaded")
case promise := <-pe.chExportPCD:
pe.logPrint("exporting pcd")
blob, err := pe.cmd.ExportPCD()
if err != nil {
promise.rejected(err)
break
}
pe.logPrint("pcd exported")
promise.resolved(blob)
case promise := <-pe.chExportSelectedPCD:
pe.logPrint("exporting selected points as pcd")
if !scanSelection(0, 0) {
promise.rejected(errors.New("failed to scan selected points"))
}
blob, err := pe.cmd.ExportSelectedPCD()
if err != nil {
promise.rejected(err)
break
}
pe.logPrint("pcd exported")
promise.resolved(blob)
case promise := <-pe.chReset:
pe.cmd.Reset()
promise.resolved("resetted")
case promise := <-pe.chCommand:
res, err := pe.cs.Run(promise.data.(string), func() error {
if scanSelection(0, 0) {
return nil
}
return errors.New("failed to scan selected points")
})
if err != nil {
promise.rejected(err)
break
}
promise.resolved(res)
case promise := <-pe.ch2D:
show2D = promise.data.(bool)
promise.resolved("changed")
case e := <-pe.chWheel:
var ok bool