forked from neurolabusc/surf-ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.pas
2082 lines (2030 loc) · 74 KB
/
track.pas
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
unit track;
{$mode objfpc}{$H+}
{$Include opts.inc}
interface
uses
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl, {$ENDIF} {$ENDIF DGL}
{$ifndef isTerminalApp}
ClipBrd,dialogs,colorTable,
{$endif}
Classes, SysUtils, math, define_types, matmath, track_simplify, zstream;
Type
// const
// kMaxScalars = 3; //maximum number of properties/scalars from TRK file, e.g. FA, pval, pval_corr
TScalar = record
mn, mx: single; //full range of scalar
mnView, mxView: single; //selected window for displaying scalar, e.g. if 0..1 with a grayscale color table than values <0 will be black and >1 will be white
scalar: array of float;
name: string;
end;
TTrack = class
//utime: QWord;
scale, minFiberLength, ditherColorFrac, maxObservedFiberLength : single;
origin, mxV, mnV : TPoint3f;
isBusy, isRebuildList, isTubes, isWorldSpaceMM: boolean;
TrackTubeSlices, //e.g. if 5 then cross section of fiber is pentagon
n_count, n_faces, n_vertices, n_indices, minFiberLinks, LineWidth: integer;
{$IFDEF COREGL}
vao, vbo : GLuint;
{$ELSE}
displayList : GLuint;
{$ENDIF}
tracks: array of single;
scalarLUT: TLUT;
scalarSelected: integer;
scalars: array of TScalar;
private
function LoadBfloat(const FileName: string): boolean;
function LoadDat(const FileName: string): boolean;
function LoadPdb(const FileName: string): boolean;
function LoadTck(const FileName: string): boolean;
function LoadTrk(const FileName: string): boolean;
function LoadVtkASCII(const FileName: string): boolean;
function LoadVtk(const FileName: string): boolean;
procedure SetDescriptives;
procedure SetScalarDescriptives;
{$ifndef isTerminalApp}
procedure BuildListStrip;
procedure BuildListTubes ;
{$endif}
public
constructor Create;
function SimplifyMM(Tol, minLength: float): boolean;
function LoadFromFile(const FileName: string): boolean;
procedure SaveBfloat(const FileName: string);
procedure SaveVtk(const FileName: string);
procedure SaveTrk(const FileName: string);
procedure Save(FileName: string);
function Smooth: boolean;
function SimplifyRemoveRedundant(Tol: float): boolean;
procedure Close;
{$ifndef isTerminalApp}
procedure DrawGL;
{$endif}
procedure CenterX;
destructor Destroy; override;
end;
implementation
{$ifndef isTerminalApp}
uses mainunit, shaderu, {$IFDEF COREGL} gl_core_3d {$ELSE} gl_legacy_3d {$ENDIF};
{$endif}
procedure TTrack.CenterX;
var
offset: single;
i, m, mi: integer;
mn, mx, pt : TPoint3f;
begin
maxObservedFiberLength := 0;
if (n_count < 1) or (length(tracks) < 4) then exit;
mn := ptf(Infinity,Infinity,Infinity);
mx := ptf(-Infinity, -Infinity, -Infinity);
i := 0;
while i < length(tracks) do begin
m := asInt( tracks[i]); inc(i);
for mi := 1 to m do begin
pt.X := tracks[i];
i := i + 3;
minmax(pt, mn,mx);
end;
end;
if (mn.X >= mx.X) then exit;
offset := (mn.X + mx.X)/ 2;
showmessage(floattostr(mn.X)+'..'+floattostr(mx.X)+' : '+floattostr(offset));
i := 0;
while i < length(tracks) do begin
m := asInt( tracks[i]); inc(i);
for mi := 1 to m do begin
tracks[i] := tracks[i] - offset;
i := i + 3;
end;
end;
setDescriptives;
end; // CenterX()
procedure TTrack.Close;
var i: integer;
begin
i := 1;
{$ifndef isTerminalApp}
scalarLUT := UpdateTransferFunction(i, false); //red-yellow
{$endif}
scalarSelected := -1; //none: color based on direction
n_count := 0;
n_vertices := 0;
n_faces := 0;
n_indices := 0;
setlength(tracks, 0);
if scalars <> nil then begin //close all open properties/scalars
for i := 0 to (length(scalars)-1) do begin
scalars[i].scalar := nil;
scalars[i].name := '';
end;
scalars := nil;
end;
end;
function vector2RGB(pt1, pt2: TPoint3F; var len: single): TPoint3f;
begin
len := sqrt (sqr(pt1.X - pt2.X) + sqr(pt1.Y - pt2.Y) + sqr(pt1.Z - pt2.Z) );
if (len = 0) then begin
result := ptf(0,0,0);
exit;
end;
result.X := (abs(pt1.X - pt2.X)/len);
result.Y := (abs(pt1.Y - pt2.Y)/len);
result.Z := (abs(pt1.Z - pt2.Z)/len);
end;
{$ifndef isTerminalApp}
function RGBA2pt3f(RGBA: TRGBA): TPoint3f;
begin
result.X := RGBA.R/255;
result.Y := RGBA.G/255;
result.Z := RGBA.B/255;
end;
function mixRandRGBA(RGBin: TPoint3f; randAmount: single): TRGBA;
//randAmount 0..1 mix this proportion of random (grayscale) intensity
var
s: single;
v: TPoint3f;
begin
v := RGBin;
if (randAmount < 0) or (randAmount > 1.0) then exit;
s := random();
v.X := (randAmount * s) + ((1-randAmount) * v.X);
v.Y := (randAmount * s) + ((1-randAmount) * v.Y);
v.Z := (randAmount * s) + ((1-randAmount) * v.Z);
result.R := round(UnitBound(v.X) * 255);
result.G := round(UnitBound(v.Y) * 255);
result.B := round(UnitBound(v.Z) * 255);
result.A := 255;
end;
procedure TTrack.BuildListStrip;
// create displaylist where tracks are drawn as connected line segments
var
vRGBA: TVertexRGBA;
Indices,vType: TInts;
Verts, vNorms: TVertices;
normRGB: TPoint3f;
normRGBA : TRGBA;
pts, norms: array of TPoint3f;
len: single;
maxLinks, m, mi, i,j, ntracks, nfiber, nvertex: integer;
trackLinks : array of integer;
startPt, endPt:TPoint3f;
isScalarPerFiberColor : boolean = false;
isScalarPerVertexColor: boolean = false;
begin
randomize;
//GLForm1.Caption := inttostr(random(666));
if (ScalarSelected >= 0) and (length(Scalars) > ScalarSelected) then begin
// GLForm1.Caption := format('%d %d %d %d %d',[ScalarSelected, length(Scalars), length(Scalars[ScalarSelected].scalar), n_count, random(666)]);
if length(Scalars[ScalarSelected].scalar) = n_count then
isScalarPerFiberColor := true //one color per fiber
else
isScalarPerVertexColor := true;
end;
//if isScalarPerVertexColor then
// GLForm1.Caption := format('%d %d %d %d %d',[ScalarSelected, length(Scalars), length(Scalars[ScalarSelected].scalar), n_count, random(666)]);
maxLinks := 0;
n_faces := 0;
n_vertices := 0;
n_indices := 0;
TrackTubeSlices := 5;
if (length(tracks) < 4) then exit;
//if minFiberLinks < 3 then minFiberLinks := 3; //minimum to compute normal;
ntracks := length(tracks);
//uTime := GetTickCount64();
{$DEFINE TWOPASS_STRIP} //two passes is ~2 times quicker as we do not waste time re-allocating memory
{$IFDEF TWOPASS_STRIP}
//first pass: find mesh size
setlength(trackLinks, ntracks);
i := 0;
while i < ntracks do begin
trackLinks[i] := 0;
m := asInt( tracks[i]);
if m >= minFiberLinks then begin
startPt.X := tracks[i+1];
startPt.Y := tracks[i+2];
startPt.Z := tracks[i+3];
j := (3 * (m-1));
endPt.X := tracks[j+i+1];
endPt.Y := tracks[j+i+2];
endPt.Z := tracks[j+i+3];
normRGB := vector2RGB(startPt, endPt, len);
if len >= minFiberLength then begin
trackLinks[i] := m;
n_vertices := n_vertices + 2*m+8; //Duplicate vertices + 8 for the Begin and End Imposter
n_indices := n_indices + 2*m+11; //Same as Above + 3 Primitive Restart
{$IFDEF COREGL}
n_faces := n_faces + 2*m + 3;//adjacent start + adjacent end + primitive restart!
{$ELSE}
n_faces := n_faces + m + 1;//primitive restart!
{$ENDIF}
if (m > maxLinks) then
maxLinks := m;
end;
end; //len >= minFiberLength
i := i + 1 + (3 * m);
end;
if (maxLinks < 1) then exit;
//allocate memory
setlength(pts, maxLinks);
setlength(norms, maxLinks);
setlength(vRGBA, n_vertices);
setlength(vType, n_vertices);
setlength(Verts, n_vertices);
setlength(vNorms, n_vertices);
setlength(Indices, n_indices);
//second pass: fill arrays
i := 0;
n_vertices := 0;
n_faces := 0;
n_indices := 0;
nfiber := 0;
nvertex := 0;
while i < ntracks do begin
m := asInt( tracks[i]);
if trackLinks[i] >= minFiberLinks then begin
inc(i);
for mi := 0 to (m-1) do begin
pts[mi].X := tracks[i]; inc(i);
pts[mi].Y := tracks[i]; inc(i);
pts[mi].Z := tracks[i]; inc(i);
end;
if isScalarPerFiberColor then begin
normRGBA := inten2rgb1(Scalars[ScalarSelected].scalar[nfiber], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
normRGB := RGBA2pt3f(normRGBA);
end else
normRGB := vector2RGB(pts[0], pts[m-1], len);
normRGBA := mixRandRGBA(normRGB, ditherColorFrac);
if isScalarPerVertexColor then
normRGBA := inten2rgb1(Scalars[ScalarSelected].scalar[nvertex], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
for mi := 0 to (m-2) do begin
norms[mi] := normalDirection(pts[mi], pts[mi+1]); //line does not have a surface normal, but a direction
end;
//Add the first end imposter
for mi:=0 to 3 do begin
Verts[n_vertices] := pts[0];
vNorms[n_vertices].x := -norms[0].x;
vNorms[n_vertices].y := -norms[0].y;
vNorms[n_vertices].z := -norms[0].z;
if mi>1 then
vType[n_vertices] := 1
else
vType[n_vertices] := 2;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
end;
Indices[n_indices] := kPrimitiveRestart;inc(n_indices);
//Duplicate every vertice
for mi := 0 to (m-2) do begin
if isScalarPerVertexColor then
normRGBA := inten2rgb1(Scalars[ScalarSelected].scalar[nvertex+mi], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
Verts[n_vertices] := pts[mi];
vNorms[n_vertices] := norms[mi];
vType[n_vertices] := 0;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
Verts[n_vertices] := pts[mi];
vNorms[n_vertices] := norms[mi];
vType[n_vertices] := 0;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
end;
if isScalarPerVertexColor then
normRGBA := inten2rgb1(Scalars[ScalarSelected].scalar[nvertex+m-1], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
//The normal for the last vestice is different
Verts[n_vertices] := pts[m-1];
vNorms[n_vertices] := norms[m-2];
vType[n_vertices] := 0;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
Verts[n_vertices] := pts[m-1];
vNorms[n_vertices] := norms[m-2];
vType[n_vertices] := 0;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
Indices[n_indices] := kPrimitiveRestart;inc(n_indices);
//Add the Last end imposter
for mi:=0 to 3 do begin
Verts[n_vertices] := pts[m-1];
vNorms[n_vertices] := norms[m-2];
if mi>1 then begin vType[n_vertices] := 1;
end else vType[n_vertices] := 2;
vRGBA[n_vertices] := normRGBA;
Indices[n_indices] := n_vertices;inc(n_indices);
inc(n_vertices);
end;
Indices[n_indices] := kPrimitiveRestart;inc(n_indices);
end else
i := i + 1 + (3 * m);
nfiber := nfiber + 1;
nvertex := nvertex + m;
end;
{$ELSE}
{$IFDEF COREGL} Use two pass or change code below for GL_LINE_STRIP_ADJACENCY {$ENDIF}
i := 0;
while i < ntracks do begin
m := asInt( tracks[i]); inc(i);
if m >= minFiberLinks then begin
setlength(pts, m);
setlength(norms, m);
for mi := 0 to (m-1) do begin
pts[mi].X := tracks[i]; inc(i);
pts[mi].Y := tracks[i]; inc(i);
pts[mi].Z := tracks[i]; inc(i);
end;
normRGB := vector2RGB(pts[0], pts[m-1], len);
if len >= minFiberLength then begin
normRGBA := mixRandRGBA(normRGB, ditherColorFrac);
for mi := 1 to (m-2) do
norms[mi] := normalDirection(pts[mi-1], pts[mi+1]); //line does not have a surface normal, but a direction
setlength(vRGBA, n_vertices + m);
setlength(Verts, n_vertices + m);
setlength(vNorms, n_vertices + m);
setlength(Indices, n_faces + m + 1);
//create as line strip - repeat start/end to end primitive
for mi := 0 to (m-1) do begin
vRGBA[mi+n_vertices] := normRGBA;
Verts[mi+n_vertices] := pts[mi];
vNorms[mi+n_vertices] := norms[mi];
Indices[mi+n_faces] := mi+n_vertices;
end;
Indices[m+n_faces] := kPrimitiveRestart;
n_vertices := n_vertices + m ;
n_faces := n_faces + m + 1;
end;
end else
i := i + (3 * m);
end;
{$ENDIF}
{$IFDEF COREGL}
BuildDisplayListStrip(Indices, Verts, vNorms, vRGBA, vType, LineWidth, vao, vbo);
{$ELSE}
displayList := BuildDisplayListStrip(Indices, Verts, vNorms, vRGBA, LineWidth);
{$ENDIF}
n_indices := length(Indices);
n_vertices := 0;
n_faces := 0;
end; // BuildList()
procedure TTrack.BuildListTubes ;
// create displaylist where tracks are drawn as connected cylinders
//const
// kSlices = 5; //the cylinder is a pie cut into this many slices: fewer = simpler, more = less boxy
var
vRGBA: TVertexRGBA;
normRGB: TPoint3f;
normRGBA: TRGBA;
pts: array of TPoint3f;
len, radius: single;
numCylVert, numVert, mprev, m, mi, i, j, ntracks, nfiber, nvtx: integer;
vertices: TVertices;
faces, cylFace: TFaces;
numFaces, numCylFace, maxLinks: integer;
cylVert: TVertices;
B, startPt, endPt: TPoint3f;
trackLinks : array of integer;
perVertexScalars: array of single;
isScalarPerFiberColor : boolean = false;
isScalarPerVertexColor: boolean = false;
begin
if (ScalarSelected >= 0) and (length(Scalars) > ScalarSelected) then begin
if length(Scalars[ScalarSelected].scalar) = n_count then
isScalarPerFiberColor := true //one color per fiber
else
isScalarPerVertexColor := true;
end;
//tm := gettickcount64();
n_indices := 0;
n_faces := 0;
n_vertices := 0;
maxLinks := 0;
if (length(tracks) < 4) then exit;
//if minFiberLinks < 3 then minFiberLinks := 3; //minimum to compute normal;
MakeCylinder( 1, 71, cylFace, cylVert, TrackTubeSlices);
numCylFace := length(cylFace);
numCylVert := TrackTubeSlices;//numCylVert div 2; //number of faces for half of cylinder (top or bottom disk)
ntracks := length(tracks);
radius := LineWidth * 0.25;
{$DEFINE TWOPASS} //two passes is ~4 times quicker as we do not waste time re-allocating memory
{$IFDEF TWOPASS}
//first pass: find mesh size
setlength(trackLinks, ntracks);
i := 0;
while i < ntracks do begin
trackLinks[i] := 0;
m := asInt( tracks[i]);
if m >= minFiberLinks then begin
startPt.X := tracks[i+1];
startPt.Y := tracks[i+2];
startPt.Z := tracks[i+3];
j := (3 * (m-1));
endPt.X := tracks[j+i+1];
endPt.Y := tracks[j+i+2];
endPt.Z := tracks[j+i+3];
normRGB := vector2RGB(startPt, endPt, len); //here only used for length
if len >= minFiberLength then begin
trackLinks[i] := m;
n_vertices := n_vertices + (m * numCylVert);
n_faces := n_faces + ((m - 1) * numCylFace); //fence post problem
if (m > maxLinks) then
maxLinks := m;
end;
end; //len >= minFiberLength
i := i + 1 + (3 * m);
end;
//GLForm1.Caption := inttostr(n_faces);
if (n_faces < 1) then exit;
//allocate memory
setlength(vertices, n_vertices); //each node as 1 disk (bottom of cylinder)
setlength(vRGBA, n_vertices); //each node as 1 disk (bottom of cylinder)
setlength(faces, n_faces);
setlength(pts, maxLinks);
setlength(perVertexScalars, maxLinks);
//second pass - load geometry for links that are long enough
n_vertices := 0;
n_faces := 0;
nfiber := 0;
nvtx := 0;
i := 0;
while i < ntracks do begin
m := asInt( tracks[i]);
if (trackLinks[i] >= minFiberLinks) then begin
inc(i);
for mi := 0 to (m-1) do begin
pts[mi].X := tracks[i]; inc(i);
pts[mi].Y := tracks[i]; inc(i);
pts[mi].Z := tracks[i]; inc(i);
if isScalarPerVertexColor then
perVertexScalars[mi] := Scalars[ScalarSelected].scalar[nvtx+mi];
end;
normRGB := vector2RGB(pts[0], pts[m-1], len);
numFaces := 0;
numVert := 0;
mprev := 0; //location of previous vertex
B := ptf(0,0,0); //need to generate random binormal
for mi := 0 to (m-2) do begin
makeCylinderEnd(radius, pts[mprev], pts[mi], pts[mi+1], cylVert, B, TrackTubeSlices);
for j := 0 to (numCylFace - 1) do //add this cylinder
faces[j+numFaces+n_faces] := vectorAdd(cylFace[j], numVert+n_vertices);
numFaces := numFaces + numCylFace;
for j := 0 to (numCylVert - 1) do //add bottom of this cylinder
vertices[j+numVert+n_vertices] := cylVert[j];
if isScalarPerVertexColor then begin
normRGBA := inten2rgb1(perVertexScalars[mi], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
for j := 0 to (numCylVert - 1) do
vRGBA[j+numVert+n_vertices] := normRGBA;
end;
numVert := numVert + numCylVert;
mprev := mi;
end;
//faces[numFaces+n_faces-1] := vectorAdd(cylFace[j], 0);
makeCylinderEnd(radius, pts[m-2], pts[m-1], pts[m-1], cylVert, B, TrackTubeSlices);
for j := 0 to (numCylVert - 1) do //add top of last cylinder
vertices[j+numVert+n_vertices] := cylVert[j];
if isScalarPerVertexColor then begin
normRGBA := inten2rgb1(perVertexScalars[m-1], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
for j := 0 to (numCylVert - 1) do
vRGBA[j+numVert+n_vertices] := normRGBA;
end;
numVert := numVert + numCylVert;
if not isScalarPerVertexColor then begin
if isScalarPerFiberColor then begin
normRGBA := inten2rgb1(Scalars[ScalarSelected].scalar[nfiber], Scalars[ScalarSelected].mnView, Scalars[ScalarSelected].mxView, scalarLUT );
normRGB := RGBA2pt3f(normRGBA);
end;
normRGBA := mixRandRGBA(normRGB, ditherColorFrac);
for j := 0 to ((m * numCylVert) -1) do
vRGBA[j+n_vertices] := normRGBA;
end;
n_vertices := n_vertices + (m * numCylVert); //each node as 1 disk (bottom of cylinder)
n_faces := n_faces + (m - 1) * numCylFace; //-1: fencepost error
end else //len >= minFiberLength
i := i + 1 + (3 * m);
nfiber := nfiber + 1;
nvtx := nvtx + m;
end;
(*AssignFile(f, '~/Test.txt');
ReWrite(f);
writeln(f, floattostr(radius),' -> ',inttostr(TrackTubeSlices));
i := 0;
writeln(f, floattostr(pts[i].X),kTab, floattostr(pts[i].Y),kTab, floattostr(pts[i].Z) );
i :=1;
writeln(f, floattostr(pts[i].X),kTab, floattostr(pts[i].Y),kTab, floattostr(pts[i].Z) );
writeln(f,'xxx');
for i := 0 to (length(vertices)-1) do
writeln(f, floattostr(vertices[i].X),kTab, floattostr(vertices[i].Y),kTab, floattostr(vertices[i].Z) );
CloseFile(f); *)
setlength(trackLinks,0);
setlength(pts, 0);
{$ELSE}
i := 0;
while i < ntracks do begin
m := asInt( tracks[i]); inc(i);
if m >= minFiberLinks then begin
setlength(pts, m);
for mi := 0 to (m-1) do begin
pts[mi].X := tracks[i]; inc(i);
pts[mi].Y := tracks[i]; inc(i);
pts[mi].Z := tracks[i]; inc(i);
end;
normRGB := vector2RGB(pts[0], pts[m-1], len);
if len >= minFiberLength then begin
setlength(vertices, n_vertices + (m * numCylVert) ); //each node as 1 disk (bottom of cylinder)
setlength(faces, n_faces + ((m - 1) * numCylFace) ); //-1: fencepost error
numFaces := 0;
numVert := 0;
mprev := 0; //location of previous vertex
for mi := 0 to (m-2) do begin
makeCylinderEnd(radius, pts[mprev], pts[mi], pts[mi+1], cylVert, kSlices);
for j := 0 to (numCylFace - 1) do //add this cylinder
//faces[j+numFaces+n_faces] := pti(cylFace[j].X+numVert, cylFace[j].Y+numVert, cylFace[j].Z+numVert) ;
faces[j+numFaces+n_faces] := vectorAdd(cylFace[j], numVert+n_vertices);
numFaces := numFaces + numCylFace;
for j := 0 to (numCylVert - 1) do //add bottom of this cylinder
vertices[j+numVert+n_vertices] := cylVert[j];
numVert := numVert + numCylVert;
mprev := mi;
end;
makeCylinderEnd(radius, pts[m-2], pts[m-1], pts[m-1], cylVert, kSlices);
for j := 0 to (numCylVert - 1) do //add top of last cylinder
vertices[j+numVert+n_vertices] := cylVert[j];
numVert := numVert + numCylVert;
normRGBA := mixRandRGBA(normRGB, ditherColorFrac);
setlength(vRGBA, n_vertices + (m * numCylVert) ); //each node as 1 disk (bottom of cylinder)
for j := 0 to ((m * numCylVert) -1) do
vRGBA[j+n_vertices] := normRGBA;
n_vertices := n_vertices + (m * numCylVert); //each node as 1 disk (bottom of cylinder)
n_faces := n_faces + (m - 1) * numCylFace; //-1: fencepost error
end; //len >= minFiberLength
end else
i := i + (3 * m);
end;
{$ENDIF}
//utime := gettickcount64() - tm;
{$IFDEF COREGL}
BuildDisplayList(faces, vertices, vRGBA, vao, vbo, normRGBA);
{$ELSE}
displayList := BuildDisplayList(faces, vertices, vRGBA);
{$ENDIF}
end;
procedure TTrack.DrawGL;
begin
if (length(tracks) < 4) then exit;
if isBusy then exit;
isBusy := true;
if isRebuildList then begin
{$IFDEF COREGL}
if vao <> 0 then
glDeleteVertexArrays(1,@vao);
if (vbo <> 0) then
glDeleteBuffers(1, @vbo);
vao := 0; vbo := 0;
{$ELSE}
if displayList <> 0 then
glDeleteLists(displayList, 1);
displayList := 0;
{$ENDIF}
randseed := 123; //so that dither colors do not flicker when width is adjusted
if isTubes then //this is slow, so 'isRebuildList' ensures this is done only if the model has been changed
BuildListTubes //upload geometry as a cylinder-based display list: http://www.songho.ca/opengl/gl_displaylist.html
else
BuildListStrip; //upload geometry as a line-based display list: http://www.songho.ca/opengl/gl_displaylist.html
isRebuildList := false;
end;
{$IFDEF COREGL}
//RunMeshGLSL (2,0,0,0); //disable clip plane
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo);
if isTubes then begin
//666 RunMeshGLSL (2,0,0,0);
glDrawElements(GL_TRIANGLES, n_faces* 3, GL_UNSIGNED_INT, nil)
end else if n_indices > 0 then begin
//RunTrackGLSL(lineWidth, lPrefs);
//GLForm1.Caption := inttostr(n_indices);
glPrimitiveRestartIndex(kPrimitiveRestart);
glDrawElements(GL_TRIANGLE_STRIP, n_indices, GL_UNSIGNED_INT, nil) ;
end;
glBindVertexArray(0);
{$ELSE}
glCallList(DisplayList);
{$ENDIF}
isBusy := false;
end; // DrawGL()
{$endif}
constructor TTrack.Create;
var
i : integer;
begin
i := 1;
{$ifndef isTerminalApp}
scalarLUT := UpdateTransferFunction(i, false); ; //red-yellow
{$endif}
scalarSelected := -1; //none: color based on direction
SetLength(tracks, 0);
n_count := 0;
n_faces := 0;
n_vertices := 0;
LineWidth := 2;
{$IFDEF COREGL}
vao := 0;
vbo := 0;
{$ELSE}
displayList := 0;
{$ENDIF}
scale := 0;
maxObservedFiberLength := 0;
ditherColorFrac := 0.3;
minFiberLinks := 1;//minFiberLinks := 2;
minFiberLength := 20;
isRebuildList := true;
isBusy := false;
isTubes := true;
isWorldSpaceMM := true; //assume image oriented in world space
scalars := nil;
end; // Create()
function TTrack.LoadVtkASCII(const FileName: string): boolean;
//Read ASCII VTK mesh
// ftp://ftp.tuwien.ac.at/visual/vtk/www/FileFormats.pdf
// http://www.vtk.org/wp-content/uploads/2015/04/file-formats.pdf
// http://people.sc.fsu.edu/~jburkardt/data/vtk/vtk.html
label
666;
var
f: TextFile;
strlst: TStringList;
str: string;
nVtx, inPos, outPos, vtx, i, v, num_v, n_items: integer;
vert: array of TPoint3f;
items: array of LongInt;
function ReadLnSkipBlank (var s: string): boolean;
begin
result := false;
s := '';
while (s = '') and (not eof(f)) do begin
ReadLn(f, s); //read any whitespace http://people.sc.fsu.edu/~jburkardt/data/vtk/vtk.html
s := trim(s);
end;
if s = '' then //eof(f)
showmessage('LoadVtkASCII: Unexpected end of file')
else
result := true;
end; //ReadLnSkipBlank
function readItem (var s: string): boolean; //'LINES 1 348 347' will return successively 'LINES','1','348','347'
var
ch: char;
s2: string;
begin
result := true;
s := '';
while (not eof(f)) do begin
Read(f, ch);
s2 := ch;
s2 := trim(s2);
if (s2 = '') and (s <> '') then
exit; //white space after item
s := s + s2;
end;
if s = '' then
result := false; //eof
end; //readItem()
function readInt: integer; //'1 348 347\n2' will return successively'1','348','347','2'
var
s: string;
begin
readItem(s);
result := strtointdef(s,0);
end; //readInt()
begin
isRebuildList := true;
result := false;
FileMode := fmOpenRead;
AssignFile(f, FileName);
Reset(f);
ReadLn(f, str); //signature: '# vtk DataFile'
if pos('VTK', UpperCase(str)) <> 3 then begin
goto 666;
end;
ReadLn(f, str); //comment: 'Comment: created with MRIcroS'
if not ReadLnSkipBlank(str) then goto 666; //kind: 'BINARY' or 'ASCII'
if (pos('ASCII', UpperCase(str)) <> 1) then begin // '# vtk DataFile'
showmessage('Only able to read ASCII or binary VTK files: '+str);
goto 666;
end;
if not ReadLnSkipBlank(str) then goto 666;// kind, e.g. "DATASET POLYDATA" or "DATASET STRUCTURED_ POINTS"
if pos('POLYDATA', UpperCase(str)) = 0 then begin
showmessage('Only able to read VTK images saved as POLYDATA, not '+ str);
goto 666;
end;
if not ReadLnSkipBlank(str) then goto 666; //type: 'POINTS 347 float'
if pos('POINTS', UpperCase(str)) <> 1 then begin
showmessage('Expected header to report "POINTS" not '+ str);
goto 666;
end;
strlst:=TStringList.Create;
num_v := 0;
strlst.DelimitedText := str;
num_v := StrToIntDef(strlst[1],0);
if (num_v < 1) or (pos('FLOAT', UpperCase(strlst[2])) <> 1) then begin
showmessage('Expected at least 1 point of type FLOAT, not '+ str);
goto 666;
end;
setlength(vert, num_v); //vert = zeros(num_f, 9);
for i := 0 to (num_v - 1) do begin
read(f,vert[i].X);
read(f,vert[i].Y);
read(f,vert[i].Z);
end;
if not readItem (str) then goto 666; //Read one item at a time, as FSU data does not use EOLN: "LINES 1 348 347" not "LINES 1 348\n347"
if pos('POLYGONS', UpperCase(str)) > 0 then begin // number of vert, e.g. "POLYGONS 1380 5520"
showmessage('This is a mesh file: rename with a ".vtk" extension and use File/Open to view: '+ str);
goto 666;
end;
if pos('LINES', UpperCase(str)) <> 1 then begin
showmessage('Expected header to report "LINES" not '+ str);
goto 666;
end;
n_count := readInt;
n_items := readInt;
if (n_count < 1) or (n_items < 1) then goto 666;
setlength(items, n_items);
for i := 0 to (n_items - 1) do
items[i] := readInt;
vtx := n_items - n_count;
setlength(tracks, vtx * 3 + n_count);
inPos := 0;
outPos := 0;
for i := 1 to n_count do begin
nVtx := items[inPos]; inc(inPos);
//showmessage(format('%d %d', [items[inPos], items[inpos]]));
tracks[outPos] := asSingle(nVtx); inc(outPos);
for v := 1 to nVtx do begin
vtx := items[inPos]; inc(inPos);
tracks[outPos] := vert[vtx].X; inc(outPos);
tracks[outPos] := vert[vtx].Y; inc(outPos);
tracks[outPos] := vert[vtx].Z; inc(outPos);
end; //for each vertex in fiber
end; //for each fiber
result := true;
exit;
666:
closefile(f);
end; //LoadVtkASCII()
function MemoryStreamAsString(vms: TMemoryStream): string;
//binary contents as ASCII string: http://forum.lazarus.freepascal.org/index.php?topic=15622.5;wap2
begin
SetString(Result, vms.Memory, vms.Size)
end; //MemoryStreamAsString()
procedure TTrack.SaveVtk(const FileName: string);
var
f : TextFile;
m, mi, i,j,num_v, k, nk, n_items: integer;
items: array of integer;
vert: array of single;
outStream : TMemoryStream;
begin
num_v := (length(tracks) - n_count) div 3;
if (num_v < 1) or (n_count < 2) then begin
showmessage('You need to open a mesh before you can save it');
exit;
end;
FileMode := fmOpenWrite;
AssignFile(f, FileName);
ReWrite(f);
WriteLn(f, '# vtk DataFile Version 3.0');
WriteLn(f, 'vtk output');
WriteLn(f, 'BINARY');
WriteLn(f, 'DATASET POLYDATA');
//serialize data
setlength(vert, num_v * 3);
n_items := n_count+num_v;
setlength(items, n_items);
j := 0;
i := 0;
k := 0;
nk := 0;
while i < length(tracks) do begin
m := asInt( tracks[i]); inc(i);
//showmessage(format('%d -> %d',[i, m]));
items[k] := m; inc(k);
for mi := 0 to (m-1) do begin
items[k] := nk; inc(k); inc(nk);
vert[j] := tracks[i]; inc(i); inc(j);
vert[j] := tracks[i]; inc(i); inc(j);
vert[j] := tracks[i]; inc(i); inc(j);
end;
end;
{$IFDEF ENDIAN_LITTLE}
for i := 0 to (n_items -1) do
SwapLongInt(items[i]);
for i := 0 to ((num_v*3) -1) do begin
SwapSingle(vert[i]);
end;
{$ENDIF}
//showmessage(format('yyyy %d %d',[n_items, num_v]));
//write points
WriteLn(f, 'POINTS '+inttostr(num_v) +' float'); //POINTS 7361202 float
outStream := TMemoryStream.Create;
outStream.Write(pointer(vert)^, num_v * 3 * sizeOf(single));
WriteLn(f, MemoryStreamAsString(outStream));
outStream.Free;
//write lines
WriteLn(f, 'LINES '+inttostr(n_count) +' '+inttostr(n_items)); //LINES 50076 7411278
outStream := TMemoryStream.Create;
outStream.Write(pointer(items)^, (n_items) * sizeOf(longint));
WriteLn(f, MemoryStreamAsString(outStream));
outStream.Free;
CloseFile(f);
FileMode := fmOpenRead;
end; //SaveVtk()
function TTrack.LoadVtk(const FileName: string): boolean;
//Read BINARY VTK mesh
// https://github.com/bonilhamusclab/MRIcroS/blob/master/%2BfileUtils/%2Bvtk/readVtk.m
// ftp://ftp.tuwien.ac.at/visual/vtk/www/FileFormats.pdf
// "The VTK data files described here are written in big endian form"
label
666;
var
f: TFByte;
strlst: TStringList;
str: string;
nVtx, inPos, outPos, vtx, i, v, num_v, n_items, fsz: integer;
vert: array of TPoint3f;
items: array of LongInt;
begin
isRebuildList := true;
result := false;
FileMode := fmOpenRead;
AssignFile(f, FileName);
Reset(f,1);
fsz := filesize(f);
if fsz < 64 then goto 666;
ReadLnBin(f, str); //signature: '# vtk DataFile'
if pos('VTK', UpperCase(str)) <> 3 then begin
goto 666;
end;
ReadLnBin(f, str); //comment: 'Comment: created with MRIcroS'
ReadLnBin(f, str); //kind: 'BINARY' or 'ASCII'
if (pos('BINARY', UpperCase(str)) <> 1) then begin // '# vtk DataFile'
closefile(f);
result := LoadVtkASCII(FileName);
exit;
end;
ReadLnBin(f, str); // kind, e.g. "DATASET POLYDATA" or "DATASET STRUCTURED_ POINTS"
if pos('POLYDATA', UpperCase(str)) = 0 then begin
showmessage('Only able to read VTK images saved as POLYDATA, not '+ str);
goto 666;
end;
ReadLnBin(f, str); // number of vert, e.g. "POINTS 685462 float"
if pos('POINTS', UpperCase(str)) <> 1 then begin
showmessage('Expected header to report "POINTS" not '+ str);
goto 666;
end;
strlst:=TStringList.Create;
num_v := 0;
strlst.DelimitedText := str;
num_v := StrToIntDef(strlst[1],0);
if (num_v < 1) or (pos('FLOAT', UpperCase(strlst[2])) <> 1) then begin
showmessage('Expected at least 1 point of type FLOAT, not '+ str);
goto 666;
end;
if fsz < (filepos(f)+ (3 * 4 * num_v)) then begin
showmessage('File too small to contain this many vertices!');
goto 666;
end;
setlength(vert, num_v); //vert = zeros(num_f, 9);
blockread(f, vert[0], 3 * 4 * num_v);
ReadLnBin(f, str); // number of vert, e.g. "POLYGONS 1380 5520"
if str = '' then ReadLnBin(f, str);
if pos('POLYGONS', UpperCase(str)) > 0 then begin
showmessage('This is a mesh file: rename with a ".vtk" extension and use File/Open to view: '+ str);
goto 666;
end;
if pos('LINES', UpperCase(str)) <> 1 then begin
showmessage('Expected header to report "LINES" not '+ str);
goto 666;
end;
strlst.DelimitedText := str;
n_count := StrToIntDef(strlst[1],0);
n_items := StrToIntDef(strlst[2],0);
strlst.free;
if fsz < (filepos(f)+ (n_items * 4)) then begin
showmessage('File too small to contain this many lines!');
goto 666;
end;
setlength(items, n_items);
blockread(f, items[0], n_items * 4);
closefile(f);
{$IFDEF ENDIAN_LITTLE}
for i := 0 to (n_items -1) do
SwapLongInt(items[i]);
for i := 0 to (num_v -1) do begin
SwapSingle(vert[i].X);
SwapSingle(vert[i].Y);
SwapSingle(vert[i].Z);
end;
{$ENDIF}
vtx := n_items - n_count;
setlength(tracks, vtx * 3 + n_count);
inPos := 0;
outPos := 0;
for i := 1 to n_count do begin
nVtx := items[inPos]; inc(inPos);
tracks[outPos] := asSingle(nVtx); inc(outPos);
for v := 1 to nVtx do begin
vtx := items[inPos]; inc(inPos);
tracks[outPos] := vert[vtx].X; inc(outPos);
tracks[outPos] := vert[vtx].Y; inc(outPos);
tracks[outPos] := vert[vtx].Z; inc(outPos);
//if v < 4 then
// showmessage(format('%g %g %g',[vert[vtx].X, vert[vtx].Y, vert[vtx].Z]));
end; //for each vertex in fiber
end; //for each fiber
result := true;
exit;
666:
closefile(f);
end; //LoadVtk()
procedure TTrack.SaveBfloat(const FileName: string);
var
flt: array of single;
i, o, m, mi, nflt: integer;
mStream : TMemoryStream;
zStream: TGZFileStream;
FileNameBf: string;
begin
if (n_count < 1) or (length(tracks) < 4) then exit;
nflt := length(tracks) + n_count;
setlength(flt, nflt);
o := 0; //output position
i := 0; //input position
while i < length(tracks) do begin
m := asInt( tracks[i]); inc(i);
flt[o] := m; inc(o); // "N"
flt[o] := m; inc(o); // "SeedIndex"
for mi := 0 to (m-1) do begin
flt[o] := tracks[i]; inc(i); inc(o);
flt[o] := tracks[i]; inc(i); inc(o);