00001
00002
00003
00004
00005
00006 #include <vcl.h>
00007 #pragma hdrstop
00008
00009 #include "MDIFrame.h"
00010 #include "MDIFirstHit.h"
00011 #include "MDIEdit.h"
00012 #include "MDISlice.h"
00013 #include "MDIGradient.h"
00014
00015 #include "MDIMarchingCubes.h"
00016 #include "MDICuberille.h"
00017 #include "MDIIsovalue.h"
00018 #include "MDIRegionBoundary.h"
00019 #include "MDITransparent.h"
00020 #include "MDIAverage.h"
00021 #include "MDINPR.h"
00022 #include "MDIMIP.h"
00023 #include "about.h"
00024 #include "MDITransfer.h"
00025 #include "MDITwoLevel.h"
00026 #include <fastmath.h>
00027 #include <math.h>
00028
00029 #pragma resource "*.dfm"
00030
00031 #define MIN(x,y) ((x<y) ? (x) : (y))
00032 #define MAX(x,y) ((x>y) ? (x) : (y))
00033
00034 TFrameForm *FrameForm;
00035
00036
00037 __fastcall TFrameForm::TFrameForm(TComponent* Owner)
00038 : TForm(Owner)
00039 {
00040 }
00041
00042 void __fastcall TFrameForm::Exit1Click(TObject *Sender)
00043 {
00044 Close();
00045 }
00046
00047 void __fastcall TFrameForm::Tile1Click(TObject *Sender)
00048 {
00049 Tile();
00050 }
00051
00052 void __fastcall TFrameForm::Cascade1Click(TObject *Sender)
00053 {
00054 Cascade();
00055 }
00056
00057 void __fastcall TFrameForm::Arrangeicons1Click(TObject *Sender)
00058 {
00059 ArrangeIcons();
00060 }
00061
00062 void __fastcall TFrameForm::Open1Click(TObject *Sender)
00063 {
00064 if(OpenFileDialog->Execute()){ this->Repaint();
00065 if (loadFile(OpenFileDialog->FileName)){
00066 filename->Caption = OpenFileDialog->FileName;
00067 dimensions->Caption = IntToStr(sizeX) + " x " +IntToStr(sizeY) + " x " + IntToStr(sizeZ);
00068 CoolBar->Visible = true;
00069 Ansicht1->Enabled = true;
00070 Window1->Enabled = true;
00071 Close1->Enabled = true;
00072 }
00073 }
00074 }
00075
00076 void __fastcall TFrameForm::SlicesClick(TObject *Sender)
00077 {
00078 if (Slices->Checked)
00079 {
00080 SliceForm->Close();
00081 Slices->Checked = false;
00082 }
00083 else
00084 {
00085 SliceForm = new TSliceForm(this);
00086 Slices->Checked = true;
00087 }
00088 }
00089
00090
00091
00092
00093 void __fastcall TFrameForm::GradientSlicesClick(TObject *Sender)
00094 {
00095 if (GradientSlices->Checked)
00096 {
00097 GradientForm->Close();
00098 GradientSlices->Checked = false;
00099 }
00100 else
00101 {
00102 GradientForm = new TGradientForm(this);
00103 GradientSlices->Checked = true;
00104 }
00105
00106 }
00107
00108
00109 void __fastcall TFrameForm::AverageClick(TObject *Sender)
00110 {
00111 if (Average->Checked)
00112 {
00113 AverageForm->Close();
00114 Average->Checked = false;
00115 }
00116 else
00117 {
00118 AverageForm = new TAverageForm(this);
00119 Average->Checked = true;
00120 }
00121
00122 }
00123
00124
00125 void __fastcall TFrameForm::FirstHitClick(TObject *Sender)
00126 {
00127 if (FirstHit->Checked)
00128 {
00129 FirstHitForm->Close();
00130 FirstHit->Checked = false;
00131 }
00132 else
00133 {
00134 FirstHitForm = new TFirstHitForm(this);
00135 FirstHit->Checked = true;
00136 }
00137
00138 }
00139
00140
00141 void __fastcall TFrameForm::TransparentClick(TObject *Sender)
00142 {
00143 if (Transparent->Checked)
00144 {
00145 TransparentForm->Close();
00146 Transparent->Checked = false;
00147 }
00148 else
00149 {
00150 TransparentForm = new TTransparentForm(this);
00151 Transparent->Checked = true;
00152 }
00153
00154 }
00155
00156
00157 void __fastcall TFrameForm::CuberilleClick(TObject *Sender)
00158 {
00159 if (Cuberille->Checked)
00160 {
00161 CuberilleForm->Close();
00162 Cuberille->Checked = false;
00163 }
00164 else
00165 {
00166 CuberilleForm = new TCuberilleForm(this);
00167 Cuberille->Checked = true;
00168 }
00169
00170 }
00171
00172
00173
00174 void __fastcall TFrameForm::MIPClick(TObject *Sender)
00175 {
00176 if (MIP->Checked)
00177 {
00178 MIPForm->Close();
00179 MIP->Checked = false;
00180 }
00181 else
00182 {
00183 MIPForm = new TMIPForm(this);
00184 MIP->Checked = true;
00185 }
00186
00187 }
00188
00189
00190 void __fastcall TFrameForm::NPRClick(TObject *Sender)
00191 {
00192 if (NPR->Checked)
00193 {
00194 NPRForm->Close();
00195 NPR->Checked = false;
00196 }
00197 else
00198 {
00199 NPRForm = new TNPRForm(this);
00200 NPR->Checked = true;
00201 }
00202
00203 }
00204
00205
00206
00207 void __fastcall TFrameForm::Saveas1Click(TObject *Sender)
00208 {
00209 if(SaveFileDialog->Execute()){
00210
00211 }
00212 }
00213
00214
00215 void __fastcall TFrameForm::Print1Click(TObject *Sender)
00216 {
00217 if(PrintDialog->Execute()){
00218
00219 }
00220 }
00221
00222
00223 void __fastcall TFrameForm::Close1Click(TObject *Sender)
00224 {
00225 filename->Caption = "";
00226 dimensions->Caption = "0 x 0 x 0";
00227 CoolBar->Visible = false;
00228 Ansicht1->Enabled = false;
00229 Window1->Enabled = false;
00230 Close1->Enabled = false;
00231 Open1->Enabled = true;
00232 deallocate();
00233
00234 }
00235
00236 bool __fastcall TFrameForm::loadFile(AnsiString name)
00237 {
00238 int pagesize, i;
00239 double d;
00240
00241
00242 ifstream f(name.c_str(), ios::binary|ios::in);
00243
00244
00245 sizeX = _readInt16(f);
00246 sizeY = _readInt16(f);
00247 sizeZ = _readInt16(f);
00248
00249 pagesize = sizeX*sizeY;
00250
00251
00252 StatusProgress->Visible = true;
00253 size = sizeX*sizeY*sizeZ;
00254
00255 deallocate();
00256
00257 data = new __int16[size];
00258 histo = new THistogram(4096);
00259 histo->horizontal = false;
00260 contr = new TContrast(histo->size);
00261 transe = new TTransfer(histo);
00262 gradient = new vertex[size+pagesize];
00263
00264 absgrad = new __int16[size];
00265 color = new rgba[size];
00266
00267 memusage = memusage + size*2;
00268 memusage = memusage + sizeof(THistogram)+4096*sizeof(__int16);
00269 memusage = memusage + sizeof(TContrast);
00270 memusage = memusage + sizeof(TTransfer);
00271 memusage = memusage + size*4*3;
00272 memusage = memusage + size*2;
00273 memusage = memusage + size*4;
00274 memupdate();
00275
00276 StatusMain->Caption = "Reading File ...";
00277 StatusMain->Repaint();
00278
00279 char * buff = (char *)data;
00280 int __valsize = sizeof(__int16);
00281 for(int i = 0; i<size*__valsize; i+=(sizeX*__valsize)) {
00282 f.read(buff+i, sizeX*__valsize);
00283 StatusProgress->Position = 10*(float(i)/float(size*__valsize))+1;
00284 }
00285 for (i=0; i<size; i++)
00286 { if (data[i]<0) data[i]=0;
00287 if (data[i]>4095) data[i]=4095; }
00288
00289
00290
00291 StatusMain->Caption = "Calculating Gradient ...";
00292 StatusMain->Repaint();
00293
00294 __int16 dataMax = 0;
00295
00296
00297 for (i=0; i<size; i++)
00298 {
00299
00300 if (i<pagesize || i>size-pagesize) { gradient[i].x = gradient[i].y = gradient[i].z=0;absgrad[i]=0.0000001;}
00301 else
00302 {
00303 gradient[i].x = (data[i+1]-data[i-1])/2;
00304 gradient[i].y = (data[i+sizeX]-data[i-sizeX])/2;
00305 gradient[i].z = (data[i+pagesize]-data[i-pagesize])/2;
00306 absgrad[i] = _fm_sqrt(gradient[i].x*gradient[i].x + gradient[i].y*gradient[i].y + gradient[i].z * gradient[i].z);
00307 d = (double)sqrt(gradient[i].x*gradient[i].x + gradient[i].y*gradient[i].y + gradient[i].z * gradient[i].z);
00308 gradient[i].x = (float)(gradient[i].x / d);
00309 gradient[i].y = (float)(gradient[i].y / d);
00310 gradient[i].z = (float)(gradient[i].z / d);
00311 }
00312 if (!(i%(sizeX*sizeY)))
00313 StatusProgress->Position = 10+70*(float(i)/float(size))+1;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 StatusMain->Caption = "Mapping Colors ...";
00368 StatusMain->Repaint();
00369 dataMax = 4095;
00370
00371 float ratio = (float)0xFF / (float)dataMax;
00372 for (int i=0; i<size; ++i)
00373 {
00374 color[i].r = color[i].g = color[i].b =
00375 (unsigned char)((float)data[i]*ratio);
00376 if (data[i]>350) {
00377
00378 if (data[i]>1200) {
00379 color[i].a = 255;
00380 color[i].r = 255;
00381 color[i].g = 0;
00382 color[i].b = 0;
00383 } else {
00384 color[i].a = 10;
00385 color[i].r = 0;
00386 color[i].g = 0;
00387 color[i].b = 255;
00388 }
00389 } else {
00390 color[i].a = 0;
00391 }
00392
00393
00394 ++((*histo)[data[i]]);
00395 if (!(i%(sizeX*sizeY)))
00396 StatusProgress->Position = 80+20*(float(i)/float(size))+1;
00397 }
00398
00399 StatusMain->Caption = "";
00400 StatusProgress->Visible = false;
00401 return true;
00402 }
00403
00404 __int16 __fastcall TFrameForm::_readInt16(ifstream & ifs)
00405 {
00406 char tmp[2];
00407 tmp[0] = ifs.get();
00408 tmp[1] = ifs.get();
00409 __int16 *res = (__int16 *)tmp;
00410 return *res;
00411 }
00412
00413 void __fastcall TFrameForm::FormClose(TObject *Sender,
00414 TCloseAction &Action)
00415 {
00416 deallocate();
00417 }
00418
00419
00420 void __fastcall TFrameForm::About1Click(TObject *Sender)
00421 {
00422 AboutBox = new TAboutBox(this);
00423 AboutBox->ShowModal();
00424
00425 }
00426
00427
00428
00429 void __fastcall TFrameForm::deallocate()
00430 {
00431
00432 if (color) {delete [] color;memusage = memusage - size*4;color = NULL;}
00433 if (gradient) {delete [] gradient; memusage = memusage - size*4*3;gradient = NULL;}
00434 if (absgrad) {delete [] absgrad; memusage = memusage - size*2;absgrad = NULL;}
00435 if (data) {delete [] data;memusage = memusage - size*2;data = NULL;}
00436 if (histo) {delete histo;memusage = memusage - sizeof(THistogram)-4096*sizeof(__int16);histo = NULL;}
00437 if (contr) {delete contr;memusage = memusage - sizeof(TContrast);contr = NULL;}
00438 if (transe) {delete transe;memusage = memusage - sizeof(TTransfer);transe = NULL;}
00439 memupdate();
00440 }
00441
00442
00443 void __fastcall TFrameForm::FormResize(TObject *Sender)
00444 {
00445 StatusMain->Width = (Width-168)/2;
00446 StatusMain2->Width = StatusMain->Width;
00447 StatusMain2->Left = StatusMain->Width + 7;
00448 StatusProgress->Width = StatusMain->Width-10;
00449 }
00450
00451
00452 void __fastcall TFrameForm::FormCreate(TObject *Sender)
00453 {
00454 StatusDate->Caption = DateToStr(Date());
00455 memusage = 0;
00456 memupdate();
00457 }
00458
00459 void __fastcall TFrameForm::memupdate()
00460 {
00461 StatusMem->Caption = FloatToStrF(memusage/1000, ffNumber, 7,0) + " K";
00462 StatusMem->Repaint();
00463 }
00464
00465 void __fastcall TFrameForm::METransferfunktionClick(TObject *Sender)
00466 {
00467 if (METransferfunktion->Checked)
00468 {
00469 TransferForm->Close();
00470 METransferfunktion->Checked = false;
00471 }
00472 else
00473 {
00474 TransferForm = new TTransferForm(this, transe);
00475 METransferfunktion->Checked = true;
00476 }
00477 }
00478
00479
00480 void __fastcall TFrameForm::TwoLevelClick(TObject *Sender)
00481 {
00482 if (TwoLevel->Checked)
00483 {
00484 TwoLevelForm->Close();
00485 TwoLevel->Checked = false;
00486 }
00487 else
00488 {
00489 TwoLevelForm = new TTwoLevelForm(this);
00490 TwoLevel->Checked = true;
00491 }
00492 }
00493
00494