-
Notifications
You must be signed in to change notification settings - Fork 23
/
meshlhrh.pas
262 lines (240 loc) · 7.56 KB
/
meshlhrh.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
unit meshlhrh;
{$Include opts.inc}
{$mode objfpc}{$H+}
interface
//{$DEFINE TIMER} //defined in opts.inc
{$DEFINE TREFOIL} //use Trefoil Knot as default object (instead of pyramid)
uses
//nifti_foreign,
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl, glext,{$ENDIF} {$ENDIF DGL}
{$IFDEF CTM} ctm_loader, {$ENDIF}
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, strutils, DateUtils,
base64, zstream, LcLIntf, nifti_loader, colorTable, matmath, math,
define_types, nifti_types, fileutil, mesh;
type TMeshLHRH = class(TMesh)
RH: TMesh;
private
procedure CreateDummyOverlays;
procedure SynchronizeOverlays;
public
isShowLH: boolean;
isShowRH: boolean;
function LoadFromFile(const FileName: string; LoadBilateralLHRH: boolean): boolean;
function LoadOverlay(const FileName: string; lLoadSmooth: boolean): boolean;
procedure DrawGL (Clr: TRGBA; clipPlane: TPoint4f; isFlipMeshOverlay: boolean; XRay: integer= kXRayNo);
procedure ReverseFaces;
procedure SwapYZ;
procedure SwapZY;
function Contours(OverlayIndex: integer): boolean;
function Mesh2Node(const FileName: string; isAppend: boolean = true):boolean;
function Atlas2Node(const FileName: string; isAppend: boolean = true):boolean;
procedure CloseOverlays;
procedure Close;
constructor Create;
destructor Destroy; override;
end;
procedure LHRHNameChange(FileName: string; out FileNameLH, FileNameRH: string; isIgnoreSpheres: boolean = false);
implementation
destructor TMeshLHRH.Destroy;
begin
RH.Destroy;
inherited;
end; // Destroy()
procedure TMeshLHRH.Close;
begin
RH.Close;
inherited;
end;
constructor TMeshLHRH.Create;
begin
RH := TMesh.Create;
RH.isBilateral := true;
isShowRH := true;
isShowLH := true;
inherited;
end;
procedure printf(s: string);
begin
{$IFDEF UNIX}writeln(s);{$ENDIF}
end;
procedure LHRHNameChange(FileName: string; out FileNameLH, FileNameRH: string; isIgnoreSpheres: boolean = false);
// ".LH", ".RH" std.60.rh.white -> std.60.lh.white
var
pos: integer;
pth, fnm: string;
ch: char;
begin
fnm := extractfilename(FileName);
pth := extractfilepath(Filename);
FileNameLH := '';
FileNameRH := '';
//if (isIgnoreSpheres) and (PosEx('sphere', upcase(fnm)) > 0) then
// exit;
pos := PosEx('LH.', upcase(fnm));
if pos < 1 then pos := PosEx('LH_', upcase(fnm));
if pos > 0 then begin //LH left is input
if (fnm[pos] = 'l') then
ch := 'r'
else
ch := 'R';
FileNameLH := FileName;
fnm[pos] := ch;
FileNameRH := pth+fnm;
end else begin
pos := PosEx('RH.', upcase(fnm));
if pos < 1 then pos := PosEx('RH_', upcase(fnm));
if pos > 0 then begin //RH left is input
if (fnm[pos] = 'r') then
ch := 'l'
else
ch := 'L';
FileNameRH := FileName;
fnm[pos] := ch;
FileNameLH := pth+fnm;
end;
end;
(*if (not fileexists(FileNameRH)) or (not fileexists(FileNameLH)) then begin
FileNameRH := '';
FileNameLH := '';
end;*)
end;
procedure TMeshLHRH.ReverseFaces;
begin
Inherited;
if length(rh.Faces) > 0 then
rh.ReverseFaces;
end;
procedure TMeshLHRH.SwapYZ;
begin
Inherited;
if length(rh.Faces) > 0 then
rh.SwapYZ;
end;
procedure TMeshLHRH.SwapZY;
begin
Inherited;
if length(rh.Faces) > 0 then
rh.SwapZY;
end;
procedure TMeshLHRH.CloseOverlays;
begin
Inherited;
if length(rh.Faces) > 0 then
rh.CloseOverlays;
end;
function TMeshLHRH.Mesh2Node(const FileName: string; isAppend: boolean = true):boolean;
begin
Inherited;
if length(rh.Faces) > 0 then
rh.Mesh2Node(FileName, isAppend);
end;
function TMeshLHRH.Atlas2Node(const FileName: string; isAppend: boolean = true):boolean;
begin
result := Inherited;
if length(rh.Faces) > 0 then
result := rh.Atlas2Node(FileName, isAppend);
end;
function TMeshLHRH.Contours(OverlayIndex: integer): boolean;
begin
Inherited;
SynchronizeOverlays();
if length(rh.Faces) > 0 then
rh.Contours(OverlayIndex);
CreateDummyOverlays();
end;
function TMeshLHRH.LoadFromFile(const FileName: string; LoadBilateralLHRH: boolean): boolean;
var
filenameLH, filenameRH: string;
begin
self.isBilateral := false;
RH.isBilateral := false;
RH.Close;
if (not LoadBilateralLHRH) then begin
result := Inherited LoadFromFile(FileName);
exit;
end;
LHRHNameChange(FileName, filenameLH, filenameRH, true);
printf(FileName+' LH '+filenameLH+' RH '+ filenameRH);
if (filenameLH = '') or (filenameRH = '') then begin
result := Inherited LoadFromFile(FileName);
exit;
end;
self.isBilateral := true;
result := Inherited LoadFromFile(filenameLH);
if not result then begin
result := Inherited LoadFromFile(FileName);
exit;
end;
RH.isBilateral := true;
RH.LoadFromFile( filenameRH);
//inherited;
end;
procedure TMeshLHRH.CreateDummyOverlays;
//Left and right hemisphere should have the same number of overlays
// if one hemisphere has more, clone
var
i: integer;
begin
if OpenOverlays = RH.OpenOverlays then exit;
if (RH.OpenOverlays < OpenOverlays) then begin
for i := (RH.OpenOverlays+1) to OpenOverlays do
RH.LoadDummyOverlay(i, overlay[i]);
RH.OpenOverlays := OpenOverlays
end;
if (RH.OpenOverlays > OpenOverlays) then begin
for i := (OpenOverlays+1) to RH.OpenOverlays do
LoadDummyOverlay(i, RH.overlay[i]);
OpenOverlays := RH.OpenOverlays
end;
end;
function TMeshLHRH.LoadOverlay(const FileName: string; lLoadSmooth: boolean): boolean;
var
filenameLH, filenameRH: string;
resultRH: boolean = false;
begin
if length(rh.faces) < 1 then begin
result := Inherited LoadOverlay(FileName, lLoadSmooth);
exit;
end;
LHRHNameChange(FileName, filenameLH, filenameRH);
//printf(format('%s -> LH %s RH %s', [FileName, filenameLH, filenameRH] ));
if (filenameLH = '') and (filenameRH = '') then begin
result := Inherited LoadOverlay(FileName, lLoadSmooth);
resultRH := RH.LoadOverlay(filename, lLoadSmooth);
result := result or resultRH;
exit;
end;
result := false;
if filenameLH <> '' then
result := Inherited LoadOverlay(filenameLH, lLoadSmooth);
if filenameRH <> '' then
resultRH := RH.LoadOverlay(filenameRH, lLoadSmooth);
CreateDummyOverlays();
result := result or resultRH;
end;
procedure TMeshLHRH.SynchronizeOverlays;
var
i: integer;
begin
if (length(RH.faces) < 1) or (not isRebuildList) then exit;
RH.vertexRgbaAlpha := self.vertexRgbaAlpha;
RH.vertexRgbaSaturation := self.vertexRgbaSaturation;
RH.isRebuildList:= true;
if (self.OpenOverlays >= kMinOverlayIndex) then
for i := kMinOverlayIndex to self.OpenOverlays do begin
RH.overlay[i].OpacityPercent := Self.overlay[i].OpacityPercent;
RH.overlay[i].WindowScaledMin := Self.overlay[i].WindowScaledMin;
RH.overlay[i].WindowScaledMax := Self.overlay[i].WindowScaledMax;
//printf(format('--->>>>> %g %g',[Self.overlay[i].WindowScaledMin, Self.overlay[i].WindowScaledMax]));
RH.overlay[i].LUT := Self.overlay[i].LUT;
RH.overlay[i].LUTinvert := Self.overlay[i].LUTinvert;
RH.overlay[i].LUTindex:=Self.overlay[i].LUTindex;
RH.overlay[i].aoMap := Self.overlay[i].aoMap;
end;
end;
procedure TMeshLHRH.DrawGL (Clr: TRGBA; clipPlane: TPoint4f; isFlipMeshOverlay: boolean; XRay: integer = kXRayNo);
begin
SynchronizeOverlays();
Inherited DrawGL(Clr, clipPlane, isFlipMeshOverlay, XRay);
end;
end.