~ubuntu-branches/ubuntu/raring/mricron/raring

« back to all changes in this revision

Viewing changes to landmarks.pas

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-07-29 22:07:43 UTC
  • Revision ID: james.westby@ubuntu.com-20100729220743-q621ts2zj806gu0n
Tags: upstream-0.20100725.1~dfsg.1
ImportĀ upstreamĀ versionĀ 0.20100725.1~dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
unit landmarks;
 
2
 
 
3
interface
 
4
{$H+}
 
5
 
 
6
 
 
7
uses
 
8
    {$IFDEF Win32}
 
9
  Windows,  Messages,
 
10
{$ELSE}
 
11
  LMessages, LCLType,
 
12
{$ENDIF}
 
13
   {$IFDEF FPC}LResources, {$ENDIF}
 
14
  SysUtils, Variants, Classes, Graphics, Controls, Forms,
 
15
  Dialogs, StdCtrls, Buttons, ToolWin, ComCtrls;
 
16
 
 
17
type
 
18
  TAnatForm = class(TForm)
 
19
    ToolBar1: TToolBar;
 
20
    SaveBtn: TSpeedButton;
 
21
    AddBtn: TSpeedButton;
 
22
    ComboBox1: TComboBox;
 
23
    UpdateBtn: TSpeedButton;
 
24
    OpenBtn: TSpeedButton;
 
25
    DeleteBtn: TSpeedButton;
 
26
    procedure SaveBtnClick(Sender: TObject);
 
27
    procedure AddBtnClick(Sender: TObject);
 
28
    procedure ComboBox1Change(Sender: TObject);
 
29
    procedure UpdateCombo;
 
30
    procedure OpenBtnClick(Sender: TObject);
 
31
    procedure Update(lIndex: integer);
 
32
    procedure UpdateBtnClick(Sender: TObject);
 
33
    procedure DeleteBtnClick(Sender: TObject);
 
34
    procedure OpenAnat(lFilename: string);
 
35
    procedure CloseAnat;
 
36
  private
 
37
    { Private declarations }
 
38
  public
 
39
    { Public declarations }
 
40
  end;
 
41
 
 
42
var
 
43
  AnatForm: TAnatForm;
 
44
 
 
45
implementation
 
46
 
 
47
uses nifti_img_view, nifti_img, nifti_hdr_view, define_types;
 
48
 {$IFNDEF FPC} //Delphi
 
49
{$R *.dfm}
 
50
{$ENDIF}
 
51
type
 
52
  TLandmark = record 
 
53
    Name: string;
 
54
    X,Y,Z: single;
 
55
  end;
 
56
  TLandmarkRA = array of TLandmark;
 
57
const
 
58
kAnatFilter = 'AnatomyFile|*.anat';
 
59
var
 
60
  gLandmarks: TLandmarkRA;
 
61
procedure TAnatForm.CloseAnat;
 
62
begin
 
63
  if length(gLandmarks) < 1 then
 
64
     exit;
 
65
  SetLength(gLandmarks,0);
 
66
     UpdateCombo;
 
67
end;
 
68
  
 
69
procedure TAnatForm.SaveBtnClick(Sender: TObject);
 
70
const
 
71
  kSep = chr(9);
 
72
var
 
73
  i: integer;
 
74
  lF: TextFile;
 
75
begin
 
76
  if length(gLandmarks) < 1 then begin
 
77
    showmessage('No landmarks open - either open a file or create new landmarks');
 
78
    exit;
 
79
  end;
 
80
  ImgForm.SaveDialog1.Filter := kAnatFilter;
 
81
     ImgForm.SaveDialog1.DefaultExt := '.anat';
 
82
  ImgForm.SaveDialog1.Filename := ChangeFileExt(ImgForm.SaveDialog1.Filename, ImgForm.SaveDialog1.DefaultExt); //10102006
 
83
  if not ImgForm.SaveDialog1.Execute then exit;
 
84
  Filemode := 0;
 
85
   AssignFile(lF, ImgForm.SaveDialog1.Filename);
 
86
   rewrite(lF);
 
87
   for i := 0 to length(gLandmarks)-1 do
 
88
    Writeln(lF, gLandmarks[i].Name+kSep+floattostr(gLandmarks[i].X)+kSep+floattostr(gLandmarks[i].Y)+kSep+floattostr(gLandmarks[i].Z)  );
 
89
  CloseFile(lF);
 
90
 
 
91
end;
 
92
 
 
93
procedure TAnatForm.UpdateCombo;
 
94
var
 
95
  i: integer;
 
96
begin
 
97
//xxx
 
98
  ComboBox1.Items.Clear;
 
99
  if length(gLandmarks) < 1 then
 
100
    exit;
 
101
  for i := 0 to length(gLandmarks)-1 do
 
102
    ComboBox1.Items.Add(gLandmarks[i].Name);
 
103
  ComboBox1.ItemIndex := length(gLandmarks)-1;
 
104
  ComboBox1Change(nil);
 
105
end;
 
106
 
 
107
 
 
108
procedure TAnatForm.AddBtnClick(Sender: TObject);
 
109
var
 
110
  s: string;
 
111
  i: integer;
 
112
  lOK: boolean;
 
113
begin
 
114
  i := length(gLandmarks)+1;
 
115
  s := 'A'+inttostr(i);
 
116
  lOK := InputQuery('Enter a name', 'region name', s);
 
117
  if not lOK then
 
118
    exit;
 
119
  setlength(gLandmarks,i);
 
120
  gLandmarks[i-1].Name := s;
 
121
  Update(i-1);
 
122
  UpdateCombo;
 
123
end;
 
124
 
 
125
(*
 
126
 MMToImgCoord(lX,lY,lZ,lXmm,lYmm,lZmm);
 
127
 if lX <> ImgForm.XViewEdit.value then ImgForm.XViewEdit.value := lX;
 
128
 if lY <> ImgForm.YViewEdit.value then ImgForm.YViewEdit.value := lY;
 
129
 if lZ <> ImgForm.ZViewEdit.value then ImgForm.ZViewEdit.value := lZ;
 
130
   *)
 
131
procedure SetLandmark(index: integer);//indexed from 0
 
132
var
 
133
//lXmm,lYmm,lZmm: single;
 
134
lX,lY,lZ: integer;
 
135
begin
 
136
  if (index < 0) or (index >= length(gLandmarks)) then
 
137
    exit;
 
138
 MMToImgCoord(lX,lY,lZ,gLandmarks[index].X,gLandmarks[index].Y,gLandmarks[index].Z);
 
139
 if lX <> ImgForm.XViewEdit.value then ImgForm.XViewEdit.value := lX;
 
140
 if lY <> ImgForm.YViewEdit.value then ImgForm.YViewEdit.value := lY;
 
141
 if lZ <> ImgForm.ZViewEdit.value then ImgForm.ZViewEdit.value := lZ;
 
142
 ImgForm.XViewEditChange(nil);
 
143
end;
 
144
 
 
145
procedure TAnatForm.ComboBox1Change(Sender: TObject);
 
146
begin
 
147
  SetLandmark(ComboBox1.ItemIndex);
 
148
end;
 
149
 
 
150
function NextTab(lStr: string; var lP: integer): string;
 
151
//reports text prior to comma...
 
152
var
 
153
 len: integer;
 
154
begin
 
155
  result := '';
 
156
  len := length(lStr);
 
157
  if len < lP then exit;
 
158
  repeat
 
159
    if (lStr[lP] = chr(9){','})   then begin
 
160
      lP := lP + 1;
 
161
      exit;
 
162
    end;
 
163
    //if lStr[lP] <> ' ' then
 
164
      result := result + lStr[lP];
 
165
    lP := lP + 1;
 
166
  until (lP > len);
 
167
end;
 
168
 
 
169
procedure TAnatForm.OpenAnat(lFilename: string);
 
170
var
 
171
  st: string;
 
172
   sl: TStringList;
 
173
  n, line, col : integer;
 
174
begin
 
175
  if not Fileexists(lFilename) then begin
 
176
    CloseAnat;
 
177
    exit;
 
178
  end;
 
179
   //will load the TAB delimited TXT here
 
180
   sl := TStringList.Create;
 
181
   try
 
182
     //load the tab delimited txt file
 
183
     sl.LoadFromFile(lFilename) ;
 
184
     //for each tab delimited line
 
185
     n := 0;
 
186
     setlength(gLandmarks,sl.Count);
 
187
     for line := 0 to sl.Count-1 do begin
 
188
       st := sl[line];
 
189
       col := 1;
 
190
       if (NextTab(st,col) <> '') and  (NextTab(st,col) <> '') and(NextTab(st,col) <> '') and(NextTab(st,col) <> '')  then begin
 
191
          inc(n);
 
192
          col := 1;
 
193
          gLandmarks[line].Name := NextTab(st,col);
 
194
          gLandmarks[line].X := strtofloat(NextTab(st,col));
 
195
          gLandmarks[line].Y := strtofloat(NextTab(st,col));
 
196
          gLandmarks[line].Z := strtofloat(NextTab(st,col));
 
197
       end;
 
198
     end;
 
199
     setlength(gLandmarks,n);
 
200
   finally
 
201
     sl.Free;
 
202
   end;
 
203
   UpdateCombo;
 
204
    AnatForm.show;
 
205
end;
 
206
 
 
207
 
 
208
procedure TAnatForm.OpenBtnClick(Sender: TObject);
 
209
begin
 
210
     if not OpenDialogExecute(kAnatFilter,'Select background image',false) then exit;
 
211
   OpenAnat(HdrForm.OpenHdrDlg.Filename) ;
 
212
end;
 
213
 
 
214
procedure TAnatForm.Update(lIndex: integer);
 
215
var
 
216
  X,Y,Z: integer;
 
217
begin
 
218
  if lIndex >= Length(gLandmarks) then
 
219
    exit;
 
220
  X := round(ImgForm.XViewEdit.value);
 
221
  Y := round(ImgForm.YViewEdit.value);
 
222
  Z := round(ImgForm.ZViewEdit.value);
 
223
  ImgCoordToMM(X,Y,Z, gLandmarks[lIndex].X,gLandmarks[lIndex].Y,gLandmarks[lIndex].Z);
 
224
  ComboBox1Change(nil);
 
225
end;
 
226
 
 
227
procedure TAnatForm.UpdateBtnClick(Sender: TObject);
 
228
begin
 
229
  Update(ComboBox1.ItemIndex);
 
230
 
 
231
end;
 
232
 
 
233
procedure TAnatForm.DeleteBtnClick(Sender: TObject);
 
234
var
 
235
  p,i,l: integer;
 
236
begin
 
237
  l := Length(gLandmarks);
 
238
  i := ComboBox1.ItemIndex;
 
239
  if (l < 1) or (i >= l) or (i < 0) then
 
240
    exit;
 
241
  if i < (l-1) then
 
242
    for p := i+1 to l-1 do
 
243
      gLandmarks[p-1] := gLandmarks[p];
 
244
  SetLength(gLandmarks,l-1);
 
245
     UpdateCombo;
 
246
end;
 
247
 
 
248
initialization
 
249
{$IFDEF FPC}
 
250
{$I landmarks.lrs}
 
251
{$ENDIF}
 
252
 
 
253
end.
 
254