Main Page   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

MDIMIP.cpp

00001 //---------------------------------------------------------------------------
00002 
00003 #include <vcl.h>
00004 #pragma hdrstop
00005 
00006 #include "MDIMIP.h"
00007 #include "MDIEdit.h"
00008 #include "MDIFrame.h"
00009 #define MIN(x,y)        ((x<y) ? (x) : (y))
00010 #define MAX(x,y)        ((x>y) ? (x) : (y))
00011 //---------------------------------------------------------------------------
00012 #pragma package(smart_init)
00013 #pragma resource "*.dfm"
00014 TMIPForm *MIPForm;
00015 rgb *MIPview;
00016 
00017 const float pi=3.141592;
00018 const float pid2=2*pi;
00019 const float pih=pi/2;
00020 const float pirad=pi/180;
00021 //---------------------------------------------------------------------------
00022 __fastcall TMIPForm::TMIPForm(TComponent* Owner)
00023         : TForm(Owner)
00024 {
00025 }
00026 //---------------------------------------------------------------------------
00027 void __fastcall TMIPForm::SetPixelFormatDescriptor()
00028 {
00029     PIXELFORMATDESCRIPTOR pfd = {
00030         sizeof(PIXELFORMATDESCRIPTOR),
00031         1,
00032         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
00033         PFD_TYPE_RGBA,
00034         24,
00035         0,0,0,0,0,0,
00036         0,0,
00037         0,0,0,0,0,
00038         32,
00039         0,
00040         0,
00041         PFD_MAIN_PLANE,
00042         0,
00043         0,0,
00044     };
00045     PixelFormat = ChoosePixelFormat(hdc, &pfd);
00046     SetPixelFormat(hdc, PixelFormat, &pfd);
00047 }
00048 
00049 //---------------------------------------------------------------------
00050 void __fastcall TMIPForm::FormClose(TObject *Sender, TCloseAction &Action)
00051 {
00052 delete [] MIPview;
00053 Action=caFree;
00054 FrameForm->MIP->Checked = false;
00055 memusage = memusage - 3*1024*1024;
00056 FrameForm->memupdate();
00057 wglDeleteContext(hrc);
00058 ReleaseDC(Handle,hdc);
00059 }
00060 //---------------------------------------------------------------------
00061 void __fastcall TMIPForm::FormCreate(TObject *Sender)
00062 {
00063     int i,a;
00064 
00065     MIPview = new rgb[1024*1024];
00066     memusage = memusage + 3*1024*1024;
00067     FrameForm->memupdate();
00068 
00069     mrotx=0; mroty=0;xres=128;yres=128;thresh = 400;
00070     hdc = GetDC(Handle);
00071     SetPixelFormatDescriptor();
00072     hrc = wglCreateContext(hdc);
00073     if(hrc == NULL) ShowMessage("Could not create DC!");
00074     wglMakeCurrent(hdc, hrc);
00075 
00076     w = ClientWidth;
00077     h = ClientHeight;
00078 
00079     glEnable(GL_DEPTH_TEST);
00080     glEnable(GL_CULL_FACE);
00081 
00082     glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
00083     glOrtho (-50.0,50.0,-50.0,50.0,-50.0,50.0);
00084 
00085     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00086     glFlush();
00087     SwapBuffers(hdc);
00088     FormResize(Sender);
00089     RayCast();
00090 }
00091 //---------------------------------------------------------------------
00092 void __fastcall TMIPForm::FormResize(TObject *Sender)
00093 {
00094         PrepareGLWindow();
00095         if (autores->Checked) {RayCast();}
00096         PaintGL();
00097 }
00098 //---------------------------------------------------------------------------
00099 void __fastcall TMIPForm::FormPaint(TObject *Sender)
00100 {
00101         PaintGL();
00102 }
00103 //---------------------------------------------------------------------------
00104 void __fastcall TMIPForm::PaintGL()
00105 {
00106         float i;
00107 
00108         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00109         PrepareGLWindow();
00110         glColor3f(0.6f,0.6f,0.6f);
00111         glLineWidth(3.0f);
00112         glBegin(GL_LINE_STRIP);
00113         glVertex3f(-50,-50,0);
00114         glVertex3f(50,-50,0);
00115         glVertex3f(50,50,0);
00116         glVertex3f(-50,50,0);
00117         glVertex3f(-50,-50,0);
00118         glEnd();
00119 
00120         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00121         glDrawPixels(xres, yres, GL_RGB, GL_UNSIGNED_BYTE,MIPview);
00122                         // we use unsigned byte (char) to meet speed issues.
00123                         // 16 bit data (__int16) slows down drawing to much.
00124 
00125         glFlush();
00126         SwapBuffers(hdc);
00127 }
00128 
00129 void __fastcall TMIPForm::PrepareGLWindow()
00130 {
00131         wglMakeCurrent(hdc, hrc);
00132 
00133         GLfloat nRange = 50.0;
00134         w = ClientWidth-200;
00135         h = ClientHeight;
00136 
00137         if(h == 0) h = 1;
00138         if(w == 0) w = 1;
00139 
00140         glViewport(200, 0, w, h);
00141         glLoadIdentity();
00142 
00143         if (w <= h)
00144         {        glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
00145                 glPixelZoom((float)w/(float)xres, (float)w/(float)yres);}
00146          else
00147         {        glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
00148                 glPixelZoom((float)h/(float)xres, (float)h/(float)yres);}
00149 
00150         glRasterPos2d(-50,-50);
00151 }
00152 //---------------------------------------------------------------------------
00153 
00154 void __fastcall TMIPForm::FormActivate(TObject *Sender)
00155 {
00156         PaintGL();        
00157 }
00158 //---------------------------------------------------------------------------
00159 
00160 void __fastcall TMIPForm::Button1Click(TObject *Sender)
00161 {
00162 RayCast();
00163 PaintGL();
00164 }
00165 //---------------------------------------------------------------------------
00166 
00167 
00168 
00169 void __fastcall TMIPForm::FormMouseDown(TObject *Sender,
00170       TMouseButton Button, TShiftState Shift, int X, int Y)
00171 {
00172 mdown=true;
00173 ox = X;
00174 oy = Y;
00175 }
00176 //---------------------------------------------------------------------------
00177 
00178 
00179 
00180 void __fastcall TMIPForm::FormMouseUp(TObject *Sender,
00181       TMouseButton Button, TShiftState Shift, int X, int Y)
00182 {
00183 mdown=false;
00184 RayCast();
00185 PaintGL();
00186 }
00187 //---------------------------------------------------------------------------
00188 
00189 void __fastcall TMIPForm::FormMouseMove(TObject *Sender,
00190       TShiftState Shift, int X, int Y)
00191 {
00192 if (mdown)
00193 {
00194 mrotx -= (Y-oy);
00195 mroty -= (X-ox);
00196 if (mrotx<0) mrotx += 360;
00197 if (mrotx>360) mrotx -= 360;
00198 if (mroty<0) mroty += 360;
00199 if (mroty>360) mroty -= 360;
00200 
00201 if (arepaint->Checked) {RayCast(); PaintGL(); }
00202 txrot->Text = IntToStr(mrotx);
00203 tzrot->Text = IntToStr(mroty);
00204 ox = X;
00205 oy = Y;
00206 }
00207 }
00208 //---------------------------------------------------------------------------
00209 void __fastcall TMIPForm::RayCast()
00210 {
00211         int a,i,k,maxres;
00212         __int16 value;
00213         int aktpos, rowsize,pagesize, px, py, pz;
00214         int ita, ite;
00215         float t,ta,te,tx,ty,tz,xrcos,yrcos,zrcos,xrsin,yrsin,zrsin,xrot,yrot,zrot,ac;
00216         float value1,value2,value3, value4, value11, value22,pfx,pfy,pfz,sx,sy,sz,s1x,s1y;
00217         float temp,j, tmax, tmin;
00218         long averg, anzavg;
00219         int detval, maxdim;
00220 
00221         if (sizeX>sizeY) {maxdim=sizeX;} else {maxdim=sizeY;}
00222         if (maxdim<sizeZ) {maxdim=sizeZ;}
00223 
00224         //detval = StrToInt(detail->Caption);
00225         vertex pppos;
00226         vertex dlookup[1024];
00227 
00228         if (autores->Checked)
00229         { if (w>h) {xres=h;yres=h;} else {xres=w;yres=w;} }
00230         else
00231         {
00232         xres = StrToInt(textx->Caption);
00233         yres = StrToInt(texty->Caption);
00234         }
00235 
00236         xrot = (float)StrToFloat(txrot->Text)*pirad;
00237 //        yrot = (float)StrToFloat(tyrot->Text)*pirad;
00238         zrot = (float)StrToFloat(tzrot->Text)*pirad;
00239 
00240         xrcos = _fm_cos(xrot);
00241 //        yrcos = _fm_cos(yrot);
00242         zrcos = _fm_cos(zrot);
00243         xrsin = _fm_sin(xrot);
00244 //        yrsin = _fm_sin(yrot);
00245         zrsin = _fm_sin(zrot);
00246 
00247         FrameForm->StatusProgress->Position = 0;
00248         FrameForm->StatusProgress->Visible = true;
00249         maxres = MAX(MAX(sizeX,sizeY),sizeZ);
00250 
00251         dx.x = (float)maxres/(float)xres;
00252         dx.y = 0;
00253         dx.z = 0;
00254 
00255         temp = dx.y;
00256 
00257         dx.x = dx.x;
00258         dx.y = temp * xrcos - dx.z*xrsin;
00259         dx.z = temp * xrsin + dx.z*xrcos;
00260 
00261         temp = dx.x;
00262 
00263         dx.x = temp * zrcos - dx.y * zrsin;
00264         dx.y = temp * zrsin + dx.y * zrcos;
00265         dx.z = dx.z;
00266 
00267         dy.x = 0;
00268         dy.y = 0;
00269         dy.z = (float)maxres/(float)yres;
00270 
00271         temp = dy.y;
00272 
00273         dy.x = dy.x;
00274         dy.y = temp * xrcos - dy.z*xrsin;
00275         dy.z = temp * xrsin + dy.z*xrcos;
00276 
00277         temp = dy.x;
00278 
00279         dy.x = temp * zrcos - dy.y * zrsin;
00280         dy.y = temp * zrsin + dy.y * zrcos;
00281         dy.z = dy.z;
00282 
00283 
00284         dz.x = 0;
00285         dz.y = 1;
00286         dz.z = 0;
00287 
00288         temp = dz.y;
00289 
00290         dz.x = dz.x;
00291         dz.y = temp * xrcos - dz.z*xrsin;
00292         dz.z = temp * xrsin + dz.z*xrcos;
00293 
00294         temp = dz.x;
00295 
00296         dz.x = temp * zrcos - dz.y * zrsin;
00297         dz.y = temp * zrsin + dz.y * zrcos;
00298         dz.z = dz.z;
00299 
00300         o.x = -(maxres/2);
00301         o.y = -(sizeY/2);
00302         o.z = -(maxres/2);
00303 
00304         temp = o.y;
00305 
00306         o.x = o.x;
00307         o.y = temp * xrcos - o.z*xrsin;
00308         o.z = temp * xrsin + o.z*xrcos;
00309 
00310         temp = o.x;
00311 
00312         o.x = temp * zrcos - o.y * zrsin;
00313         o.y = temp * zrsin + o.y * zrcos;
00314         o.z = o.z;
00315 
00316         o.x = o.x + sizeX/2;
00317         o.y = o.y + sizeY/2;
00318         o.z = o.z + sizeZ/2;
00319 
00320         pagesize = sizeX*sizeY;
00321         rowsize = sizeX;
00322 
00323         for (i=0; i<1024; i++)
00324         {
00325         dlookup[i].x = (i-512) * dz.x;        //Lookup-Values für RayCast berechnen
00326         dlookup[i].y = (i-512) * dz.y;
00327         dlookup[i].z = (i-512) * dz.z;
00328         }
00329 
00330         k = 0;
00331         for (a=0; a<yres; a++)
00332         {
00333                 pppos.x = o.x + a * dy.x;
00334                 pppos.y = o.y + a * dy.y;
00335                 pppos.z = o.z + a * dy.z;
00336 
00337                 if (arepaint->Checked == false) {FrameForm->StatusProgress->Position = (int)(100 * ((float)a / (float)yres));}
00338                 for (i=0; i<xres; i++)
00339                 {
00340                         ppos.x = pppos.x + i * dx.x;     // Rotierten Startpunkt für parametrisierte
00341                         ppos.y = pppos.y + i * dx.y;     // Gerade berechnen ...
00342                         ppos.z = pppos.z + i * dx.z;
00343 
00344                         ta = 9999;
00345                         te = 9999;
00346 
00347                         if (dz.x!=0)
00348                         {
00349                           t = -ppos.x/dz.x;                      // Schneide Ray mit x=0
00350                           ty = ppos.y + t*dz.y;
00351                           tz = ppos.z + t*dz.z;
00352                           if (ty>=0 && tz>=2 && ty<sizeY-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00353 
00354                           t = (sizeX-1-ppos.x)/dz.x;               // Schneide Ray mit x=sizeX
00355                           ty = ppos.y + t*dz.y;
00356                           tz = ppos.z + t*dz.z;
00357                           if (ty>=0 && tz>=2 && ty<sizeY-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00358                         }
00359 
00360                         if (dz.y!=0)
00361                         {
00362                           t = -ppos.y/dz.y;                      // Schneide Ray mit y=0
00363                           tx = ppos.x + t*dz.x;
00364                           tz = ppos.z + t*dz.z;
00365                           if (tx>=0 && tz>=2 && tx<sizeX-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00366 
00367                           t = (sizeY-1-ppos.y)/dz.y;               // Schneide Ray mit y=sizeY
00368                           tx = ppos.x + t*dz.x;
00369                           tz = ppos.z + t*dz.z;
00370                           if (tx>=0 && tz>=2 && tx<sizeX-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00371                         }
00372 
00373                         if (dz.z!=0)
00374                         {
00375                           t = (2-ppos.z)/dz.z;                      // Schneide Ray mit z=0
00376                           tx = ppos.x + t*dz.x;
00377                           ty = ppos.y + t*dz.y;
00378                           if (tx>=0 && ty>=0 && tx<sizeX-1 && ty<sizeY-1) { if (ta==9999) ta=t; else te=t; }
00379 
00380                           t = (sizeZ-3-ppos.z)/dz.z;               // Schneide Ray mit z=sizeZ
00381                           tx = ppos.x + t*dz.x;
00382                           ty = ppos.y + t*dz.y;
00383                           if (tx>=0 && ty>=0 && tx<sizeX-1 && ty<sizeY-1) { if (ta==9999) ta=t; else te=t; }
00384                         }
00385 
00386                         if (ta>te) {t=ta; ta=te; te=t;}
00387 
00388                         averg = 0;
00389 
00390                         if (te==9999)
00391                         {
00392                         MIPview[k].r=MIPview[k].b=MIPview[k].g = 0;
00393                         }
00394                         else
00395                         {
00396                         value = 0; 
00397 
00398                         for (j=ta; j<te; j += 1.0)
00399                         {
00400                           pfx = (ppos.x) + j*dz.x;
00401                           pfy = (ppos.y) + j*dz.y;
00402                           pfz = (ppos.z) + j*dz.z;
00403 
00404                           px = (int)pfx;
00405                           py = (int)pfy;
00406                           pz = (int)pfz;
00407                           aktpos = pagesize*pz + rowsize*py + px;
00408                           if (trilinear->Checked)
00409                           {
00410                                 sx = pfx-px;
00411                                 sy = pfy-py;
00412                                 sz = pfz-pz;
00413                                 s1x = 1-sx;
00414                                 s1y = 1-sy;
00415                                 value1 = (float)data[aktpos]*s1x                  + (float)data[aktpos+1]*sx;
00416                                 value2 = (float)data[aktpos+rowsize]*s1x          + (float)data[aktpos+rowsize+1]*sx;
00417                                 value3 = (float)data[aktpos+pagesize+rowsize]*s1x + (float)data[aktpos+pagesize+rowsize+1]*sx;
00418                                 value4 = (float)data[aktpos+pagesize]*s1x         + (float)data[aktpos+pagesize+1]*sx;
00419                                 value11 = value1*(s1y) + value2*(sy);
00420                                 value22 = value4*(s1y) + value3*(sy);
00421                                 value = (int)(value11*(1-sz) + value22*(sz));
00422                                 if (depth->Checked){
00423                                         tmax = j;
00424                                         if (tmax<0) tmax=0;
00425                                         if (tmax>maxdim) tmax=maxdim;
00426                                         if ((value*(maxdim-tmax))>averg) {averg = (value*(maxdim-tmax));}
00427                                         }
00428                                         else {
00429                                         if (value>averg) {averg = value;}
00430                                         }
00431                           }
00432                           else
00433                           {
00434                                 value = data[pagesize*pz + rowsize*py + px];
00435                                 if (depth->Checked){
00436                                         tmax = j;
00437                                         if (tmax<0) tmax=0;
00438                                         if (tmax>maxdim) tmax=maxdim;
00439                                         if ((value*(maxdim-tmax))>averg) {averg = (value*(maxdim-tmax));}
00440 
00441                                         }
00442                                         else {
00443                                         if (value>averg) {averg = value;}
00444                                         }
00445                           }
00446 
00447                          }
00448                          if (depth->Checked){
00449                                 MIPview[k].r = MIPview[k].g = MIPview[k].b = averg/(16*maxdim);
00450                                 }
00451                                 else {
00452                                 MIPview[k].r = MIPview[k].g = MIPview[k].b = averg/16;
00453                                 }
00454                          }
00455                 k++;
00456                 }
00457         }
00458         FrameForm->StatusProgress->Visible = false;
00459 }
00460 
00461 
00462 void __fastcall TMIPForm::ResTrackBarChange(TObject *Sender)
00463 {
00464 switch (ResTrackBar->Position)
00465         {
00466         case 1: textx->Caption = "32"; texty->Caption = "32"; break;
00467         case 2: textx->Caption = "64"; texty->Caption = "64"; break;
00468         case 3: textx->Caption = "128"; texty->Caption = "128"; break;
00469         case 4: textx->Caption = "256"; texty->Caption = "256"; break;
00470         case 5: textx->Caption = "512"; texty->Caption = "512"; break;
00471         case 6: textx->Caption = "1024"; texty->Caption = "1024";
00472         }
00473 if (arepaint->Checked) {RayCast();PaintGL();}
00474         
00475 }
00476 //---------------------------------------------------------------------------
00477 
00478 
00479 void __fastcall TMIPForm::autoresClick(TObject *Sender)
00480 {
00481 if (autores->Checked==true)
00482         {
00483         ResTrackBar->Enabled=false;
00484         textx->Enabled=false;
00485         Label2->Enabled=false;
00486         texty->Enabled=false;
00487         }
00488         else
00489         {
00490         ResTrackBar->Enabled=true;
00491         textx->Enabled=true;
00492         Label2->Enabled=true;
00493         texty->Enabled=true;
00494         }
00495 if (arepaint->Checked) {RayCast();PaintGL();}
00496 
00497 }
00498 //---------------------------------------------------------------------------
00499 
00500 void __fastcall TMIPForm::trilinearClick(TObject *Sender)
00501 {
00502 if (arepaint->Checked) {RayCast();PaintGL();}
00503         
00504 }
00505 //---------------------------------------------------------------------------
00506 

Generated on Thu Jan 23 06:17:38 2003 for Vol by doxygen1.2.18