forked from neurolabusc/surf-ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.pas
executable file
·6370 lines (6237 loc) · 223 KB
/
mesh.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 mesh;
{$Include opts.inc}
{$mode objfpc}{$H+}
interface
{$DEFINE TREFOIL} //use Trefoil Knot as default object (instead of pyramid)
uses
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl, {$ENDIF} {$ENDIF DGL}
{$IFDEF CTM} ctm_loader, {$ENDIF}
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, strutils,
base64, zstream, LcLIntf, nifti_loader, colorTable, matmath, math,
define_types, nifti_types, fileutil;
const
kMinOverlayIndex = 1;
kMaxOverlays = 256;
kLUTinvisible = 0;
kLUTtranslucent = 1;
kLUTopaque = 2;
type
TSphere = packed record
X: single;
Y: single;
Z: single;
Clr: single;
Radius: single;
end;
TOverlay = record
LUTinvert: boolean;
LUTvisible: integer; //0=invisible, 1=translucent, 2=opaque
LUTindex,atlasMaxIndex : integer;
LUT: TLUT;
minIntensity, maxIntensity, windowScaledMin, windowScaledMax: single;
filename: string;
intensity: TFloats;
//next: if loaded as mesh...
faces : TFaces;
vertices: TVertices;
vertexRGBA : array of TRGBA;
vertexAtlas: TInts; //what atlss regions does this vertex belong to, e.g. [7, 8, 17...] the 3rd vertex belongs to region 17
atlasTransparentFilter: TBools;
atlasHideFilter: TInts; //atlas show these atlas regions, e.g. if [7, 8, 22] then only regions 7,8 and 22 will be visible
end;
TNodePrefs = record //preferences for nodes
minEdge, maxEdge, maxEdgeAbs, minEdgeThresh, maxEdgeThresh, minNodeColor, maxNodeColor, minNodeSize, maxNodeSize,
minNodeThresh, maxNodeThresh, scaleNodeSize, scaleEdgeSize, thresholdNodeFrac: single;
nodeLUTindex , edgeLUTindex: integer;
isNodeThresholdBySize, isNoNegEdge, isNoPosEdge, isNoLeftNodes,isNoRightNodes,isNoNodeWithoutEdge, isNodeColorVaries, isEdgeColorVaries, isEdgeSizeVaries, isEdgeShowNeg, isEdgeShowPos : boolean;
end;
type
TMesh = class
scale, vertexRgbaSaturation, vertexRgbaAlpha : single;
origin, mxV, mnV : TPoint3f;
{$IFDEF COREGL}
vao, vbo, vaoOverlay, vboOverlay: GLuint;
nFacesOverlay: integer;
{$ELSE}
displayList, displayListOverlay : GLuint;
{$ENDIF}
isZDimIsUp, isRebuildList, isBusy, isNode, isFreeSurferMesh, isVisible, isAdditiveOverlay : boolean;
nodePrefs: TNodePrefs;
OpenOverlays, OverlayTransparency, AtlasMaxIndex : integer;
overlay: array [kMinOverlayIndex..kMaxOverlays] of TOverlay;
nodes: array of TSphere;
edges: array of array of Single;
faces : array of TPoint3i;
vertices: array of TPoint3f;
vertexRGBA : array of TRGBA;
vertexAtlas: TInts; //what atlss regions does this vertex belong to, e.g. [7, 8, 17...] the 3rd vertex belongs to region 17
atlasTransparentFilter: TBools;
atlasHideFilter: TInts; //atlas show these atlas regions, e.g. if [7, 8, 22] then only regions 7,8 and 22 will be visible
tempIntensityLUT : TFloats; //only used to load without external files - flushed after LoadOverlay
errorString: string;
//overlay: array of single;
private
function CheckMesh: boolean;
procedure SetOverlayDescriptives(lOverlayIndex: integer);
procedure MinMaxPct(lOverlayIndex, num_v: integer; var mx, mn: single; isExcludeZero: boolean);
procedure SetDescriptives;
{$IFDEF TREFOIL} procedure MakeTrefoil; {$ENDIF}
procedure MakeSphere;
procedure BuildListCore(Clr: TRGBA; var f: TFaces; var v: TVertices; var vtxRGBA: TVertexRGBA);
procedure BuildListPartialAtlas(Clr: TRGBA; vtxRGBA: TVertexRGBA);
procedure BuildList(Clr: TRGBA);
procedure FilterOverlay(c: integer; var f: TFaces; var v: TVertices; var vRGBA: TVertexRGBA);
procedure BuildListOverlay(Clr: TRGBA);
function Load3Do(const FileName: string): boolean;
function Load3ds(const FileName: string): boolean;
function LoadAc(const FileName: string): boolean;
function LoadAnnot(const FileName: string): boolean;
function loadCifti(fnm: string; lOverlayIndex, lSeriesIndex: integer; isLoadCortexLeft: boolean): integer;
function LoadDae(const FileName: string): boolean; //only subset!
function LoadDfs(const FileName: string): boolean;
function LoadDxf(const FileName: string): boolean;
function LoadGcs(const FileName: string): boolean;
function LoadGii(const FileName: string; lOverlayIndex, lOverlayItem: integer): integer;
function LoadGts(const FileName: string): boolean;
function LoadJson(const FileName: string): boolean;
function LoadLwo(const FileName: string): boolean;
function LoadMesh(const FileName: string): boolean;
function LoadMeshAscii(const FileName: string): boolean;
function LoadMs3d(const FileName: string): boolean;
//function LoadVbo(const FileName: string): boolean;
function LoadMz3(const FileName: string; lOverlayIndex : integer): boolean;
function LoadPrwm(const FileName: string): boolean;
function LoadObjMni(const FileName: string): boolean;
function LoadOff(const FileName: string): boolean;
function LoadPly2(const FileName: string): boolean;
function LoadSrf(const FileName: string): boolean;
function LoadSurf(const FileName: string): boolean;
function LoadTri(const FileName: string): boolean;
function LoadWfr(const FileName: string): boolean; //EMSE wireframe
procedure LoadAsc_Srf(const FileName: string);
procedure LoadCtm(const FileName: string);
function LoadDpv(const FileName: string; lOverlayIndex: integer): boolean;
function LoadAtlasMapCore(lOverlayIndex: integer; intensityLUT: TFloats): string;
function LoadAtlasMap(const FileName: string; lOverlayIndex: integer): boolean;
procedure LoadCurv(const FileName: string; lOverlayIndex: integer);
procedure LoadMeshAsOverlay(const FileName: string; lOverlayIndex: integer);
procedure LoadNii(const FileName: string; lOverlayIndex: integer; lLoadSmooth: boolean);
function LoadCluster(const FileName: string): boolean;
procedure LoadNode(const FileName: string; out isEmbeddedEdge: boolean);
procedure LoadNv(const FileName: string);
procedure LoadObj(const FileName: string);
procedure LoadPial(const FileName: string);
procedure LoadPly(const FileName: string);
procedure LoadStl(const FileName: string);
procedure LoadStlAscii(const FileName: string);
procedure LoadVtk(const FileName: string);
procedure LoadW(const FileName: string; lOverlayIndex: integer);
public
procedure MakePyramid;
//function AtlasStatMapCore(AtlasName, StatName: string; Indices: TInts; Intensities: TFloats): string;
//function AtlasMaxIndex: integer;
procedure DrawGL (Clr: TRGBA; clipPlane: TPoint4f; isFlipMeshOverlay: boolean);
procedure Node2Mesh;
procedure ReverseFaces;
procedure CenterOrigin;
procedure SwapYZ;
procedure SwapZY;
function LoadFromFile(const FileName: string): boolean;
function LoadEdge(const FileName: string; isEmbeddedEdge: boolean): boolean;
function LoadOverlay(const FileName: string; lLoadSmooth: boolean): boolean;
procedure CloseOverlaysCore;
procedure CloseOverlays;
procedure Close;
constructor Create;
procedure SaveMz3(const FileName: string);
procedure SaveGii(const FileName: string);
procedure SaveObj(const FileName: string);
procedure SavePly(const FileName: string);
procedure SaveMesh(const FileName: string);
procedure SaveOverlay(const FileName: string; OverlayIndex: integer);
destructor Destroy; override;
end;
implementation
uses
mainunit,
meshify_simplify,shaderu, {$IFDEF COREGL} gl_core_3d {$ELSE} gl_legacy_3d {$ENDIF};
//{$IFDEF COREGL}
function mixRGBA(c1, c2: TRGBA; frac2: single): TRGBA;
var
frac1: single;
begin
frac2 := ((c2.a * frac2 )/255.0);
frac1 := 1 - frac2;
result.R := round(c1.R*frac1 + c2.R*frac2) ;
result.G := round(c1.G*frac1 + c2.G*frac2);
result.B := round(c1.B*frac1 + c2.B*frac2);
end;
//{$ENDIF}
procedure AddPt4f(var v: TPoint4f; c1,c2,c3: TRGBA); //create float vector
begin
v.X := v.X + c1.r+ c2.r+ c3.r;
v.Y := v.Y + c1.g+ c2.g+ c3.g;
v.Z := v.Z + c1.b+ c2.b+ c3.b;
v.W := v.W + c1.a+ c2.a+ c3.a;
end;
procedure TMesh.BuildListCore(Clr: TRGBA; var f: TFaces; var v: TVertices; var vtxRGBA: TVertexRGBA);
var
i,c, translucent: integer;
mn, mx: single;
rgb, rgb0: TRGBA;
vRGBA, vRGBAmx :TVertexRGBA;
vNumNeighbor: array of integer;
vSumRGBBA: array of TPoint4f;
isOverlayPainting : boolean = false;
begin
if (length(f) < 1) or (length(v) < 3) then exit;
isOverlayPainting := false;
if (OpenOverlays > 0) then //ignore overlays if they are all meshes rather than vertex colors
for c := OpenOverlays downto 1 do
if (overlay[c].LUTvisible <> kLUTinvisible) and (length(overlay[c].intensity) = length(v)) then
isOverlayPainting := true;
if (isOverlayPainting) or (length(vtxRGBA) = length(v)) then begin
rgb := RGBA(Clr.R, Clr.G, Clr.B, 0);
setlength(vRGBA, length(v));
if (length(vtxRGBA) = length(v)) then begin
c := round(vertexRgbaAlpha * 255);
if vertexRgbaSaturation >= 1 then begin
for i := 0 to (length(v)-1) do begin
vRGBA[i].r := vtxRGBA[i].r; vRGBA[i].g := vtxRGBA[i].g; vRGBA[i].b := vtxRGBA[i].b; vRGBA[i].a := c;
end;
end else begin
for i := 0 to (length(v)-1) do
vRGBA[i] := desaturateRGBA ( vtxRGBA[i], vertexRgbaSaturation, C);
end;
{$IFDEF COREGL}
if (vertexRGBAAlpha < 1.0) then
for i := 0 to (length(v)-1) do
vRGBA[i] := mixRGBA( Clr, vRGBA[i], vertexRGBAAlpha);
{$ENDIF}
end else begin
for i := 0 to (length(v)-1) do
vRGBA[i] := rgb;
end;
if (OpenOverlays > 0) then begin
if isAdditiveOverlay then begin
setlength(vRGBAmx, length(v));
rgb0 := RGBA(0,0,0,0);
for i := 0 to (length(v)-1) do
vRGBAmx[i] := rgb0;
for c := OpenOverlays downto 1 do begin
if (overlay[c].LUTvisible <> kLUTinvisible) and (length(overlay[c].intensity) = length(v)) then begin
if overlay[c].LUTvisible <> kLUTopaque then
translucent := 2 //if translucent, halve alpha
else
translucent := 1;
if overlay[c].windowScaledMax > overlay[c].windowScaledMin then begin
mn := overlay[c].windowScaledMin;
mx := overlay[c].windowScaledMax;
end else begin
mx := overlay[c].windowScaledMin;
mn := overlay[c].windowScaledMax;
end;
for i := 0 to (length(v)-1) do begin
rgb := inten2rgb(overlay[c].intensity[i], mn, mx, overlay[c].LUT);
rgb.A := rgb.A div translucent;
vRGBAmx[i] := maxRGBA(vRGBAmx[i], rgb);
end; //for i
end; //if visible
end; //for c
for i := 0 to (length(v)-1) do
vRGBA[i] := blendRGBA(vRGBA[i],vRGBAmx[i]);
end else begin
//GLForm1.caption := 'xxxxx'+(inttostr(vRGBA[i].A));
for c := OpenOverlays downto 1 do begin
if (overlay[c].LUTvisible <> kLUTinvisible) and (length(overlay[c].intensity) = length(v)) then begin
if overlay[c].LUTvisible <> kLUTopaque then
translucent := 2 //if translucent, halve alpha
else
translucent := 1;
if overlay[c].windowScaledMax > overlay[c].windowScaledMin then begin
mn := overlay[c].windowScaledMin;
mx := overlay[c].windowScaledMax;
end else begin
mx := overlay[c].windowScaledMin;
mn := overlay[c].windowScaledMax;
end;
for i := 0 to (length(v)-1) do begin
rgb := inten2rgb(overlay[c].intensity[i], mn, mx, overlay[c].LUT);
rgb.A := rgb.A div translucent;
vRGBA[i] := blendRGBA(vRGBA[i], rgb);
end; //for i
end; //if visible
end; //for c
end;
if (length(vtxRGBA) < 1) then begin //feather edges of overlay
setlength(vNumNeighbor, length(v));
setlength(vSumRGBBA, length(v));
for i := 0 to (length(v)-1) do begin
vNumNeighbor[i] := 0;
vSumRGBBA[i] := pt4f(0,0,0,0);
end;
for i := 0 to (length(f)-1) do begin
AddPt4f(vSumRGBBA[f[i].X], vRGBA[f[i].X], vRGBA[f[i].Y], vRGBA[f[i].Z]);
AddPt4f(vSumRGBBA[f[i].Y], vRGBA[f[i].X], vRGBA[f[i].Y], vRGBA[f[i].Z]);
AddPt4f(vSumRGBBA[f[i].Z], vRGBA[f[i].X], vRGBA[f[i].Y], vRGBA[f[i].Z]);
inc(vNumNeighbor[f[i].X],3);
inc(vNumNeighbor[f[i].Y],3);
inc(vNumNeighbor[f[i].Z],3);
end;
for i := 0 to (length(v)-1) do begin
if (vNumNeighbor[i] > 0) then begin //vertex at edge: neighbors both colored and uncolored v
vRGBA[i].a := round(vSumRGBBA[i].W / vNumNeighbor[i]);
if {(vRGBA[i].a < 255) and} (vRGBA[i].a > 0) then begin
vRGBA[i].r := round( vSumRGBBA[i].X / vNumNeighbor[i]);
vRGBA[i].g := round( vSumRGBBA[i].Y / vNumNeighbor[i]);
vRGBA[i].b := round( vSumRGBBA[i].Z / vNumNeighbor[i]);
end;
end;
end;
vNumNeighbor := nil;
//vNumColorNeighbor := nil;
vSumRGBBA := nil;
end; //end feather edges
(*if (length(vtxRGBA) < 1) then begin //feather edges if regions without overlay have alpha = 0
setlength(vZeroNeighbor, length(v));
for i := 0 to (length(v)-1) do
vZeroNeighbor[i] := false;
for i := 0 to (length(f)-1) do begin
if (vRGBA[f[i].X].A = 0) or (vRGBA[f[i].Y].A = 0) or (vRGBA[f[i].Z].A = 0) then begin
vZeroNeighbor[f[i].X] := true;
vZeroNeighbor[f[i].Y] := true;
vZeroNeighbor[f[i].Z] := true;
end;
end;
for i := 0 to (length(v)-1) do
if(vZeroNeighbor[i]) then
vRGBA[i].a := vRGBA[i].a shr 1; //make edges more transparent
end; //end feather edges
*)
{$IFDEF COREGL} //with new openGL we mix here
mx := 1.0 - OverlayTransparency/100;
if mx < 0 then mx := 0;
if mx > 1 then mx := 1;
//if (OverlayTransparency > 0 ) and (OverlayTransparency <= 100) then
for i := 0 to (length(v)-1) do
vRGBA[i] := mixRGBA( Clr, vRGBA[i], mx);
{$ELSE} //with old GLSL we mix in the shader
if (OverlayTransparency > 0 ) and (OverlayTransparency <= 100) then begin
for i := 0 to (length(v)-1) do
vRGBA[i].a := round( vRGBA[i].a * (1 - (OverlayTransparency /100)) );
end;
{$ENDIF}
end; // if OpenOverlays > 0
{$IFDEF COREGL}
//the purpose of the next loop is to allow us to hide curvature
for c := OpenOverlays downto 1 do begin
if isFreeSurferLUT(overlay[c].LUTindex) then begin
for i := 0 to (length(v)-1) do begin
mn := ((overlay[c].intensity[i] + 1.0) * 0.5); //convert curvature to range 0..1
if (mn < 0) then mn := 0;
if (mn > 1) then mn := 1;
mn := mn * 255.0;
vRGBA[i].A := round(mn);
end;
end;
end; //for c
{$ENDIF}
{$IFDEF COREGL}
BuildDisplayList(f, v, vRGBA, vao, vbo, Clr);
{$ELSE}
displayList:= BuildDisplayList(f, v, vRGBA);
{$ENDIF}
end else begin
Clr.A := 128; //just a bit less than 50% - only for hiding curvature
setLength(vRGBA,0);
{$IFDEF COREGL}
BuildDisplayList(f, v, vRGBA,vao, vbo, Clr);
{$ELSE}
displayList:= BuildDisplayList(f, v, vRGBA);
{$ENDIF}
end;
end; // BuildListCore()
(*procedure FilterAtlas(Clr: TRGBA; maxROI: integer; faces: TFaces; vertices: TVertices; vertexAtlas: TInts; vtxRGBA: TVertexRGBA; atlasHideFilter: TInts; atlasTransparentFilter: TBools; var f: TFaces; var v: TVertices; var vRGBA: TVertexRGBA);
label
123;
var
i, j, nfOK, nvOK, nFace, nVert: integer;
vOK: array of integer;
filterLUT: array of boolean;
fOK: array of boolean;
begin
setlength(v,0);
setlength(f,0);
setlength(vRGBA,0);
nVert := length(vertices);
nFace := length(faces);
if (nVert < 3) or (nFace < 1) or (maxROI < 1) or (length(vertexAtlas) <> nVert) then exit;
if (length(atlasHideFilter) < 1) then begin //everything survives
//atlasTransparentFilter
goto 123;
end;
//set up lookup table - list of regions that survive
setlength(filterLUT, maxROI+1);
for i := 0 to maxROI do
filterLUT[i] := false; //assume we will not filter these regions
for i := 0 to (length(atlasHideFilter) - 1) do
if (atlasHideFilter[i] > 0) and (atlasHideFilter[i] <= maxROI) then
filterLUT[atlasHideFilter[i]] := true; //filter the specified region
//mark surviving vertices
setlength(vOK, nVert);
nvOK := 0;
for i := 0 to (nVert -1) do
if (vertexAtlas[i] > 0) and (not filterLUT[vertexAtlas[i]]) then begin
vOK[i] := nvOK;
nvOK := nvOK + 1;
end else
vOK[i] := -1; //does not survive
setlength(filterLUT, 0);
//conditionals for unusual situations
if nvOK = 0 then begin //nothing survives
setlength(vOK,0);
exit;
end;
if (nvOK = nVert) then begin //everything survives
//BuildListCore(Clr, faces, vertices, vtxRGBA); //show complete array
setlength(vOK,0);
goto 123;
end;
//see how many faces survive
setlength(fOK,nFace);
nfOK := 0;
for i := 0 to (nFace -1) do begin
fOK[i] := (vOK[faces[i].X] >= 0) and (vOK[faces[i].Y] >= 0) and (vOK[faces[i].Z] >= 0);
if fOK[i] then nfOK := nfOK + 1;
end;
//no faces survive
if nvOK = 0 then begin //nothing survives
setlength(vOK,0);
setlength(fOK,0);
exit;
end;
//copy surviving vertices
setlength(v,nvOK);
for i := 0 to (nVert -1) do
if (vOK[i] >= 0) then
v[vOK[i]] := vertices[i];
//copy surviving vertices
setlength(f,nfOK);
j := 0;
for i := 0 to (nFace -1) do
if fOK[i] then begin
f[j].X := vOK[faces[i].X];
f[j].Y := vOK[faces[i].Y];
f[j].Z := vOK[faces[i].Z];
j := j + 1;
end;
//set vertex colors (if loaded)
if length(vtxRGBA) = nVert then begin
setlength(vRGBA, nvOK);
j := 0;
for i := 0 to (nVert -1) do begin
if vOK[i] >= 0 then begin
vRGBA[j] := vtxRGBA[i];
j := j + 1;
end; //if vertex survives
end; //for each vertex
end; //if vertices are colored (vtxRGBA)
setlength(vOK,0);
setlength(fOK,0);
//>> BuildListCore(Clr, f, v, vRGBA);
//release filtered faces, vertices and colors
123:
if (length(v) < 1) then begin //nothing hidden
nVert := length(vertices);
nFace := length(faces);
setlength(f,nFace);
setlength(v,nVert);
setlength(vRGBA,nVert);
f := copy(faces, 0, maxint);
v := copy(vertices, 0, maxint);
vRGBA := copy(vtxRGBA, 0, maxint);
end;
if (length(atlasTransparentFilter)) < 1 then exit;
for i := 0 to (nVert-1) do begin
j := vertexAtlas[i];
if (j < 1) or (j >= length(atlasTransparentFilter)) then continue;
if atlasTransparentFilter[j] then
vtxRGBA[i] := Clr;
end;
end; *)
procedure FilterAtlas(Clr: TRGBA; maxROI: integer; faces: TFaces; vertices: TVertices; vertexAtlas: TInts; vtxRGBA: TVertexRGBA; atlasHideFilter: TInts; atlasTransparentFilter: TBools; var f: TFaces; var v: TVertices; var vRGBA: TVertexRGBA);
var
i, j, nfOK, nvOK, nFace, nVert: integer;
vOK: array of integer;
filterLUT: array of boolean;
fOK: array of boolean;
begin
nVert := length(vertices);
nFace := length(faces);
if (nVert < 3) or (nFace < 1) or (maxROI < 1) or (length(vertexAtlas) <> nVert) then exit;
setlength(f,nFace);
setlength(v,nVert);
setlength(vRGBA,nVert);
f := copy(faces, 0, maxint);
v := copy(vertices, 0, maxint);
vRGBA := copy(vtxRGBA, 0, maxint);
if (length(atlasTransparentFilter) > 0) and (length(vtxRGBA) = nVert) then begin
for i := 0 to (nVert-1) do begin
j := vertexAtlas[i];
if (j < 1) or (j >= length(atlasTransparentFilter)) then continue;
if atlasTransparentFilter[j] then
vRGBA[i] := Clr;
end;
end;
if (length(atlasHideFilter) < 1) then exit;
//set up lookup table - list of regions that survive
setlength(filterLUT, maxROI+1);
for i := 0 to maxROI do
filterLUT[i] := false; //assume we will not filter these regions
for i := 0 to (length(atlasHideFilter) - 1) do
if (atlasHideFilter[i] > 0) and (atlasHideFilter[i] <= maxROI) then
filterLUT[atlasHideFilter[i]] := true; //filter the specified region
//mark surviving vertices
setlength(vOK, nVert);
nvOK := 0;
for i := 0 to (nVert -1) do
if (vertexAtlas[i] > 0) and (not filterLUT[vertexAtlas[i]]) then begin
vOK[i] := nvOK;
nvOK := nvOK + 1;
end else
vOK[i] := -1; //does not survive
setlength(filterLUT, 0);
//conditionals for unusual situations
if nvOK = 0 then begin //nothing survives
setlength(vOK,0);
setlength(f,0);
setlength(v,0);
setlength(vRGBA,0);
exit;
end;
if (nvOK = nVert) then begin //everything survives
//BuildListCore(Clr, faces, vertices, vtxRGBA); //show complete array
setlength(vOK,0);
exit;
end;
//see how many faces survive
setlength(fOK,nFace);
nfOK := 0;
for i := 0 to (nFace -1) do begin
fOK[i] := (vOK[faces[i].X] >= 0) and (vOK[faces[i].Y] >= 0) and (vOK[faces[i].Z] >= 0);
if fOK[i] then nfOK := nfOK + 1;
end;
//no faces survive
if nvOK = 0 then begin //nothing survives
setlength(vOK,0);
setlength(fOK,0);
exit;
end;
//copy surviving vertices
setlength(v,nvOK);
for i := 0 to (nVert -1) do
if (vOK[i] >= 0) then
v[vOK[i]] := vertices[i];
//copy surviving vertices
setlength(f,nfOK);
j := 0;
for i := 0 to (nFace -1) do
if fOK[i] then begin
f[j].X := vOK[faces[i].X];
f[j].Y := vOK[faces[i].Y];
f[j].Z := vOK[faces[i].Z];
j := j + 1;
end;
//set vertex colors (if loaded)
if length(vtxRGBA) = nVert then begin
j := 0;
for i := 0 to (nVert -1) do begin
if vOK[i] >= 0 then begin
vRGBA[j] := vRGBA[i]; //note we read from vRGBA not vtxRGBA to preserve gray vertices
j := j + 1;
end; //if vertex survives
end; //for each vertex
setlength(vRGBA, nvOK);
end; //if vertices are colored (vtxRGBA)
setlength(vOK,0);
setlength(fOK,0);
//>> BuildListCore(Clr, f, v, vRGBA);
//release filtered faces, vertices and colors
end;
procedure TMesh.BuildListPartialAtlas(Clr: TRGBA; vtxRGBA: TVertexRGBA);
var
f: TFaces;
v: TVertices;
vRGBA: TVertexRGBA;
begin
FilterAtlas(Clr, AtlasMaxIndex, faces, vertices, vertexAtlas, vtxRGBA, atlasHideFilter, atlasTransparentFilter, f, v, vRGBA);
if length(v) > 0 then
BuildListCore(Clr, f, v, vRGBA);
setlength(f,0);
setlength(v,0);
setlength(vRGBA,0);
end;
procedure TMesh.BuildList (Clr: TRGBA);
begin
if (length(faces) < 1) or (length(vertices) < 3) then exit;
if ((length(atlasTransparentFilter) > 0) or (length(atlasHideFilter) > 0)) and (length(vertexAtlas) = length(vertices)) then
BuildListPartialAtlas(Clr, vertexRGBA) //hide parts of an overlay atlas
else
BuildListCore(Clr, faces, vertices, vertexRGBA)
end;
procedure TMesh.FilterOverlay(c: integer; var f: TFaces; var v: TVertices; var vRGBA: TVertexRGBA);
//given an overlay where each vertex is a scalar intensity, only preserve vertices that exceed threshold
var
mn, mx: single;
i, j, nfOK, nvOK, nFace, nVert: integer;
vOK: array of integer;
fOK: array of boolean;
rgb, rgba192: TRGBA;
begin
setlength(v,0);
setlength(f,0);
nVert := length(overlay[c].vertices);
nFace := length(overlay[c].faces);
if (overlay[c].atlasMaxIndex > 0) or (overlay[c].LUTvisible = kLUTinvisible) or (nVert < 3) or (nFace < 1) then exit;
if length(overlay[c].intensity) <> nVert then exit; //requires intensity values
if overlay[c].windowScaledMax > overlay[c].windowScaledMin then begin
mn := overlay[c].windowScaledMin;
mx := overlay[c].windowScaledMax;
end else begin
mx := overlay[c].windowScaledMin;
mn := overlay[c].windowScaledMax;
end;
setlength(vOK, nVert);
for i := 0 to (nVert -1) do
vOK[i] := -1; //does not survive
nvOK := 0;
if (mn >= 0) then begin
for i := 0 to (nVert -1) do
if (overlay[c].intensity[i] >= mn) then begin
vOK[i] := nvOK;
nvOK := nvOK + 1;
end;
end else if (mx < 0) then begin
for i := 0 to (nVert -1) do
if (overlay[c].intensity[i] <= mx) then begin
vOK[i] := nvOK;
nvOK := nvOK + 1;
end;
end else //e.g. range -1..+3
nvOK := nVert;
if nvOK = 0 then begin //nothing survives
setlength(vOK,0);
exit;
end;
if (nvOK = nVert) then begin //everything survives
setlength(vOK,0);
setlength(v,nVert);
setlength(f,nFace);
v := overlay[c].vertices;
f := overlay[c].faces;
setlength(vRGBA, nvOK);
rgba192 := overlay[c].LUT[192];
for i := 0 to (nvOK -1) do begin
rgb := inten2rgb(overlay[c].intensity[i], mn, mx, overlay[c].LUT);
vRGBA[i].r := rgb.r;
vRGBA[i].g := rgb.g;
vRGBA[i].b := rgb.b;
vRGBA[i].a := rgba192.a;
end;
exit;
end;
//see how many faces survive
setlength(fOK,nFace);
for i := 0 to (nFace -1) do
fOK[i] := (vOK[overlay[c].faces[i].X] >= 0) and (vOK[overlay[c].faces[i].Y] >= 0) and (vOK[overlay[c].faces[i].Z] >= 0);
nfOK := 0;
for i := 0 to (nFace -1) do
if fOK[i] then nfOK := nfOK + 1;
//copy surviving vertices
setlength(v,nvOK);
for i := 0 to (nVert -1) do
if (vOK[i] >= 0) then
v[vOK[i]] := overlay[c].vertices[i];
//copy surviving vertices
setlength(f,nfOK);
j := 0;
for i := 0 to (nFace -1) do
if fOK[i] then begin
f[j].X := vOK[overlay[c].faces[i].X];
f[j].Y := vOK[overlay[c].faces[i].Y];
f[j].Z := vOK[overlay[c].faces[i].Z];
j := j + 1;
end;
//set RGBA
setlength(vRGBA, nvOK);
rgba192 := overlay[c].LUT[192];
//for i := 0 to (nvOK -1) do
// vRGBA[i] := rgba192;
j := 0;
for i := 0 to (nVert -1) do begin
if vOK[i] >= 0 then begin
rgb := inten2rgb(overlay[c].intensity[i], mn, mx, overlay[c].LUT);
vRGBA[j].r := rgb.r;
vRGBA[j].g := rgb.g;
vRGBA[j].b := rgb.b;
vRGBA[j].a := rgba192.a;
j := j + 1;
end;
end;
setlength(vOK,0);
setlength(fOK,0);
end;
procedure TMesh.BuildListOverlay(Clr: TRGBA);
var
c, i, nVert, nFace, sumFace, sumVert, nMeshOverlay: integer;
isIntensityColored : boolean;
//mn, mx: single;
oFaces, sFaces, fFaces: TFaces;
oVerts, sVerts, fVerts: TVertices;
vRGBA, sRGBA, fRGBA: TVertexRGBA;
//sFaces, sVerts, sRGBA
//rgb: TRGBA;
begin
if (length(faces) < 1) or (length(vertices) < 3) or (OpenOverlays < 1) then
exit;
nMeshOverlay := 0;
for c := 1 to OpenOverlays do
if (overlay[c].LUTvisible <> kLUTinvisible) and (length(overlay[c].vertices) > 2) then
nMeshOverlay := nMeshOverlay + 1;
if nMeshOverlay < 1 then exit;
nMeshOverlay := 0;
sumVert := 0;
sumFace := 0;
for c := 1 to OpenOverlays do begin
nVert := length(overlay[c].vertices);
nFace := length(overlay[c].faces);
if (overlay[c].LUTvisible = kLUTinvisible) or (nVert < 3) or (nFace < 1) then continue;
isIntensityColored := length(overlay[c].intensity) = nVert;
if (not isIntensityColored) then begin
if (length(overlay[c].atlasHideFilter) > 0) or (length(overlay[c].atlasTransparentFilter) > 0) then begin
(*setlength(fFaces,nFace);
setlength(fVerts,nVert);
setlength(fRGBA,nVert);
fFaces := copy(overlay[c].faces, 0, maxint);
fVerts := copy(overlay[c].vertices, 0, maxint);
fRGBA := copy(overlay[c].vertexRGBA, 0, maxint);*)
FilterAtlas(overlay[c].LUT[192], overlay[c].AtlasMaxIndex, overlay[c].faces, overlay[c].vertices, overlay[c].vertexAtlas, overlay[c].vertexRGBA, overlay[c].atlasHideFilter, overlay[c].atlasTransparentFilter, fFaces, fVerts, fRGBA);
nVert := length(fVerts);
nFace := length(fFaces);
if (nVert < 3) or (nFace < 1) then continue;
setlength(oFaces, sumFace + nFace);
for i := 0 to (nFace -1) do
oFaces[i+sumFace] := vectorAdd(fFaces[i], sumVert);
setlength(oVerts, sumVert + nVert);
for i := 0 to (nVert -1) do
oVerts[i+sumVert] := fVerts[i];//fVerts[i]; *)
setlength(vRGBA, sumVert + nVert);
for i := 0 to (nVert -1) do
vRGBA[i+sumVert] := fRGBA[i];
setlength(fFaces,0);
setlength(fVerts,0);
setlength(fRGBA,0);
sumVert := sumVert + nVert;
sumFace := sumFace + nFace;
continue;
end;
setlength(oFaces, sumFace + nFace);
for i := 0 to (nFace -1) do
oFaces[i+sumFace] := vectorAdd(overlay[c].faces[i], sumVert);
setlength(oVerts, sumVert + nVert);
for i := 0 to (nVert -1) do
oVerts[i+sumVert] := overlay[c].vertices[i];
setlength(vRGBA, sumVert + nVert);
for i := 0 to (nVert -1) do
vRGBA[i+sumVert] := overlay[c].LUT[192];
if (length(overlay[c].vertexRGBA) = nVert) then
for i := 0 to (nVert -1) do
vRGBA[i+sumVert] := overlay[c].vertexRGBA[i];
(*if isIntensityColored then begin
if overlay[c].windowScaledMax > overlay[c].windowScaledMin then begin
mn := overlay[c].windowScaledMin;
mx := overlay[c].windowScaledMax;
end else begin
mx := overlay[c].windowScaledMin;
mn := overlay[c].windowScaledMax;
end;
for i := 0 to (nVert -1) do begin
rgb := inten2rgb(overlay[c].intensity[i], mn, mx, overlay[c].LUT);
vRGBA[i+sumVert].r := rgb.r;
vRGBA[i+sumVert].g := rgb.g;
vRGBA[i+sumVert].b := rgb.b;
end;
end;*) //isIntensityColored > 0
sumVert := sumVert + nVert;
sumFace := sumFace + nFace;
continue;
end; // not isIntensityColored
//intensity colored follows
// we will hide vertices below threshold....
FilterOverlay(c, sFaces, sVerts, sRGBA);
nFace := length(sFaces);
nVert := length(sVerts);
//GLForm1.Caption := 'x'+inttostr(nVert)+'y'+inttostr(nFace)+'z'+inttostr(length(sRGBA));
if (nFace < 3) or (nVert < 1) or (length(sRGBA) <> nVert) then begin
setlength(sFaces, 0);
setlength(sVerts, 0);
setlength(sRGBA, 0);
continue;
end;
//GLForm1.Caption := 'xx'+inttostr(nVert)+'y'+inttostr(nFace)+'z'+inttostr(length(sRGBA));
setlength(oFaces, sumFace + nFace);
for i := 0 to (nFace -1) do
oFaces[i+sumFace] := vectorAdd(sFaces[i], sumVert);
setlength(oVerts, sumVert + nVert);
for i := 0 to (nVert -1) do
oVerts[i+sumVert] := sVerts[i];
setlength(vRGBA, sumVert + nVert);
for i := 0 to (nVert -1) do
vRGBA[i+sumVert] := sRGBA[i];
sumVert := sumVert + nVert;
sumFace := sumFace + nFace;
end; //for each overlay
{$IFDEF COREGL}
nFacesOverlay := length(oFaces);
BuildDisplayList(oFaces, oVerts, vRGBA, vaoOverlay, vboOverlay, Clr);
{$ELSE}
displayListOverlay := BuildDisplayList(oFaces, oVerts, vRGBA);
{$ENDIF}
end; // BuildListOverlay()
procedure TMesh.DrawGL (Clr: TRGBA; clipPlane: TPoint4f; isFlipMeshOverlay: boolean);
begin
if (isNode) and (isRebuildList) then
//do NOT exit: we might generate new faces and vertices - e.g nodescale increased from zero
else if (length(faces) < 1) or (length(vertices) < 3) then
exit;
isBusy := true;
if isRebuildList then begin//only if the model has been changed
isRebuildList := false;
if isNode then
Node2Mesh;
{$IFDEF COREGL}
if vao <> 0 then
glDeleteVertexArrays(1,@vao);
if (vbo <> 0) then
glDeleteBuffers(1, @vbo);
if vaoOverlay <> 0 then
glDeleteVertexArrays(1,@vaoOverlay);
if (vboOverlay <> 0) then
glDeleteBuffers(1, @vboOverlay);
vao := 0; vbo := 0; vaoOverlay := 0; vboOverlay := 0;
{$ELSE}
glDeleteLists(displayList, 1);
displayList := 0;
glDeleteLists(displayListOverlay, 1);
displayListOverlay := 0;
{$ENDIF}
BuildList(Clr); //upload geometry as a display list: http://www.songho.ca/opengl/gl_displaylist.html
BuildListOverlay(Clr);
end;
{$IFDEF COREGL}
if (isFlipMeshOverlay) and true then begin
if isVisible then begin
glBindVertexArray(vaoOverlay);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vboOverlay);
glDrawElements(GL_TRIANGLES, nFacesOverlay* 3, GL_UNSIGNED_INT, nil);
glBindVertexArray(0);
end;
if (vaoOverlay <> 0) and (vboOverlay <> 0) and (nFacesOverlay > 0) then begin
RunOverlayGLSL(clipPlane);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo);
glDrawElements(GL_TRIANGLES, Length(faces)* 3, GL_UNSIGNED_INT, nil);
glBindVertexArray(0);
end;
end else begin
if isVisible then begin
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo);
glDrawElements(GL_TRIANGLES, Length(faces)* 3, GL_UNSIGNED_INT, nil);
glBindVertexArray(0);
end;
if (vaoOverlay <> 0) and (vboOverlay <> 0) and (nFacesOverlay > 0) then begin
RunOverlayGLSL(clipPlane);
glBindVertexArray(vaoOverlay);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vboOverlay);
glDrawElements(GL_TRIANGLES, nFacesOverlay * 3, GL_UNSIGNED_INT, nil);
glBindVertexArray(0);
end;
end;
{$ELSE}
if (isFlipMeshOverlay) and (displayListOverlay <> 0) then begin
if isVisible then
glCallList(displayListOverlay);
if (displayListOverlay <> 0) then begin
RunOverlayGLSL(clipPlane);
glCallList(displayList);
end;
end else begin
if isVisible then
glCallList(displayList);
if (displayListOverlay <> 0) then begin
RunOverlayGLSL(clipPlane);
glCallList(displayListOverlay);
end;
end;
{$ENDIF}
isBusy := false;
end; // DrawGL()
procedure TMesh.SwapYZ;
var
swap: TPoint3f;
i : integer;
begin
if (length(vertices) < 1) or (length(faces) < 1) then exit;
for i := 0 to (length(vertices) - 1) do begin
swap.X := vertices[i].X;
swap.Y := -vertices[i].Z;
swap.Z := vertices[i].Y;
vertices[i] := swap;
end;
SetDescriptives;
isRebuildList := true;
end; // SwapYZ()
procedure TMesh.SwapZY;
var
swap: TPoint3f;
i : integer;
begin
if (length(vertices) < 1) or (length(faces) < 1) then exit;
for i := 0 to (length(vertices) - 1) do begin
swap.X := vertices[i].X;
swap.Y := vertices[i].Z;
swap.Z := -vertices[i].Y;
vertices[i] := swap;
end;
SetDescriptives;
isRebuildList := true;
end; // SwapZY()
procedure TMesh.CenterOrigin;
var
i: integer;
begin
if length(vertices) < 1 then begin
showmessage('No mesh is open: unable to center origin');
exit;
end;
vectorNegate(origin);
for i := 0 to (length(vertices) - 1) do
vectorAdd(vertices[i], origin);
setDescriptives;
isRebuildList := true;
isBusy := false;
end; // CenterOrigin()
procedure TMesh.ReverseFaces; //reverse face winding to reverse front and back faces
var
oldFaces : array of TPoint3i;
i: integer;
begin
if (length(faces) < 1) then begin
showmessage('No mesh is open: unable to reverse faces');
exit;
end;
if isBusy then exit;
isBusy := true;
oldFaces := Copy(faces, Low(faces), Length(faces));
for i := 0 to (length(faces)-1) do begin
faces[i].X := oldFaces[i].Z;
faces[i].Z := oldFaces[i].X;
end;
isRebuildList := true;
isBusy := false;
end; // ReverseFaces()
procedure TMesh.SetDescriptives;
var
mn, mx: TPoint3f;
i: integer;
begin
if length(vertices) < 1 then exit;
mx := vertices[0];
mn := mx;
for i := 0 to (length(vertices) - 1) do
minMax(vertices[i], mn, mx);
origin.X := (0.5 * (mx.X - mn.X)) + mn.X;
origin.Y := (0.5 * (mx.Y - mn.Y)) + mn.Y;
origin.Z := (0.5 * (mx.Z - mn.Z)) + mn.Z;
Scale := abs(mx.X - origin.X);
if abs(mx.Y - origin.Y) > Scale then
Scale := abs(mx.Y - origin.Y);
if abs(mx.Z - origin.Z) > Scale then
Scale := abs(mx.Z - origin.Z);
mnV := mn;
mxV := mx;
end; // SetDescriptives()
{$DEFINE SPHERE_BY_SUBDIVIDE} //we can make a sphere via Polyhedra Subdivision or Parametric Surfaces http://prideout.net/blog/?p=44
//with 2 subdivisions we get a sphere with 162 vertices, 320 faces similar to the 15-step sphere that is 286 vertices and 528 faces
//see also http://richardssoftware.net/Home/Post?id=60
{$IFDEF SPHERE_BY_SUBDIVIDE}
// 0,1,2,3 subdivision faces 20, 80, 320, 1280; vertices: 12, 42, 162, 642
function NormMidPoint(a,b: TPoint3f): TPoint3f;
begin
result := a;
vectorAdd(result,b);
vectorNormalize(result);
end; // NormMidPoint()