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

MDIAverage.cpp

00001 //---------------------------------------------------------------------------
00002 
00003 #include <vcl.h>
00004 #pragma hdrstop
00005 
00006 #include "MDIAverage.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 TAverageForm *AverageForm;
00015 rgb *averageview;
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 TAverageForm::TAverageForm(TComponent* Owner)
00023         : TForm(Owner)
00024 {
00025 }
00026 //---------------------------------------------------------------------------
00027 void __fastcall TAverageForm::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 TAverageForm::FormClose(TObject *Sender, TCloseAction &Action)
00051 {
00052 delete [] averageview;
00053 Action=caFree;
00054 FrameForm->Average->Checked = false;
00055 memusage = memusage - 3*1024*1024;
00056 FrameForm->memupdate();
00057 wglDeleteContext(hrc);
00058 ReleaseDC(Handle,hdc);
00059 }
00060 //---------------------------------------------------------------------
00061 void __fastcall TAverageForm::FormCreate(TObject *Sender)
00062 {
00063     int i,a;
00064 
00065     averageview = 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 TAverageForm::FormResize(TObject *Sender)
00093 {
00094         PrepareGLWindow();
00095         if (autores->Checked) {RayCast();}
00096         PaintGL();
00097 }
00098 //---------------------------------------------------------------------------
00099 void __fastcall TAverageForm::FormPaint(TObject *Sender)
00100 {
00101         PaintGL();
00102 }
00103 //---------------------------------------------------------------------------
00104 void __fastcall TAverageForm::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,averageview);
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 TAverageForm::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 TAverageForm::FormActivate(TObject *Sender)
00155 {
00156         PaintGL();        
00157 }
00158 //---------------------------------------------------------------------------
00159 
00160 void __fastcall TAverageForm::Button1Click(TObject *Sender)
00161 {
00162 RayCast();
00163 PaintGL();
00164 }
00165 //---------------------------------------------------------------------------
00166 
00167 
00168 
00169 void __fastcall TAverageForm::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 TAverageForm::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 TAverageForm::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 TAverageForm::RayCast()
00210 {
00211         int a,i,j,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;
00218         long averg, anzavg;
00219         int detval;
00220 
00221         detval = StrToInt(detail->Caption);
00222         vertex pppos;
00223         vertex dlookup[1024];
00224 
00225         if (autores->Checked)
00226         { if (w>h) {xres=h;yres=h;} else {xres=w;yres=w;} }
00227         else
00228         {
00229         xres = StrToInt(textx->Caption);
00230         yres = StrToInt(texty->Caption);
00231         }
00232 
00233         xrot = (float)StrToFloat(txrot->Text)*pirad;
00234 //        yrot = (float)StrToFloat(tyrot->Text)*pirad;
00235         zrot = (float)StrToFloat(tzrot->Text)*pirad;
00236 
00237         xrcos = _fm_cos(xrot);
00238 //        yrcos = _fm_cos(yrot);
00239         zrcos = _fm_cos(zrot);
00240         xrsin = _fm_sin(xrot);
00241 //        yrsin = _fm_sin(yrot);
00242         zrsin = _fm_sin(zrot);
00243 
00244         FrameForm->StatusProgress->Position = 0;
00245         FrameForm->StatusProgress->Visible = true;
00246         maxres = MAX(MAX(sizeX,sizeY),sizeZ);
00247 
00248         dx.x = (float)maxres/(float)xres;
00249         dx.y = 0;
00250         dx.z = 0;
00251 
00252         temp = dx.y;
00253 
00254         dx.x = dx.x;
00255         dx.y = temp * xrcos - dx.z*xrsin;
00256         dx.z = temp * xrsin + dx.z*xrcos;
00257 
00258         temp = dx.x;
00259 
00260         dx.x = temp * zrcos - dx.y * zrsin;
00261         dx.y = temp * zrsin + dx.y * zrcos;
00262         dx.z = dx.z;
00263 
00264         dy.x = 0;
00265         dy.y = 0;
00266         dy.z = (float)maxres/(float)yres;
00267 
00268         temp = dy.y;
00269 
00270         dy.x = dy.x;
00271         dy.y = temp * xrcos - dy.z*xrsin;
00272         dy.z = temp * xrsin + dy.z*xrcos;
00273 
00274         temp = dy.x;
00275 
00276         dy.x = temp * zrcos - dy.y * zrsin;
00277         dy.y = temp * zrsin + dy.y * zrcos;
00278         dy.z = dy.z;
00279 
00280 
00281         dz.x = 0;
00282         dz.y = 1;
00283         dz.z = 0;
00284 
00285         temp = dz.y;
00286 
00287         dz.x = dz.x;
00288         dz.y = temp * xrcos - dz.z*xrsin;
00289         dz.z = temp * xrsin + dz.z*xrcos;
00290 
00291         temp = dz.x;
00292 
00293         dz.x = temp * zrcos - dz.y * zrsin;
00294         dz.y = temp * zrsin + dz.y * zrcos;
00295         dz.z = dz.z;
00296 
00297         o.x = -(maxres/2);
00298         o.y = -(sizeY/2);
00299         o.z = -(maxres/2);
00300 
00301         temp = o.y;
00302 
00303         o.x = o.x;
00304         o.y = temp * xrcos - o.z*xrsin;
00305         o.z = temp * xrsin + o.z*xrcos;
00306 
00307         temp = o.x;
00308 
00309         o.x = temp * zrcos - o.y * zrsin;
00310         o.y = temp * zrsin + o.y * zrcos;
00311         o.z = o.z;
00312 
00313         o.x = o.x + sizeX/2;
00314         o.y = o.y + sizeY/2;
00315         o.z = o.z + sizeZ/2;
00316 
00317         pagesize = sizeX*sizeY;
00318         rowsize = sizeX;
00319 
00320         for (i=0; i<1024; i++)
00321         {
00322         dlookup[i].x = (i-512) * dz.x;        //Lookup-Values für RayCast berechnen
00323         dlookup[i].y = (i-512) * dz.y;
00324         dlookup[i].z = (i-512) * dz.z;
00325         }
00326 
00327         k = 0;
00328         for (a=0; a<yres; a++)
00329         {
00330                 pppos.x = o.x + a * dy.x;
00331                 pppos.y = o.y + a * dy.y;
00332                 pppos.z = o.z + a * dy.z;
00333 
00334                 if (arepaint->Checked == false) {FrameForm->StatusProgress->Position = (int)(100 * ((float)a / (float)yres));}
00335                 for (i=0; i<xres; i++)
00336                 {
00337                         ppos.x = pppos.x + i * dx.x;     // Rotierten Startpunkt für parametrisierte
00338                         ppos.y = pppos.y + i * dx.y;     // Gerade berechnen ...
00339                         ppos.z = pppos.z + i * dx.z;
00340 
00341                         ta = 9999;
00342                         te = 9999;
00343 
00344                         if (dz.x!=0)
00345                         {
00346                           t = -ppos.x/dz.x;                      // Schneide Ray mit x=0
00347                           ty = ppos.y + t*dz.y;
00348                           tz = ppos.z + t*dz.z;
00349                           if (ty>=0 && tz>=2 && ty<sizeY-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00350 
00351                           t = (sizeX-1-ppos.x)/dz.x;               // Schneide Ray mit x=sizeX
00352                           ty = ppos.y + t*dz.y;
00353                           tz = ppos.z + t*dz.z;
00354                           if (ty>=0 && tz>=2 && ty<sizeY-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00355                         }
00356 
00357                         if (dz.y!=0)
00358                         {
00359                           t = -ppos.y/dz.y;                      // Schneide Ray mit y=0
00360                           tx = ppos.x + t*dz.x;
00361                           tz = ppos.z + t*dz.z;
00362                           if (tx>=0 && tz>=2 && tx<sizeX-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00363 
00364                           t = (sizeY-1-ppos.y)/dz.y;               // Schneide Ray mit y=sizeY
00365                           tx = ppos.x + t*dz.x;
00366                           tz = ppos.z + t*dz.z;
00367                           if (tx>=0 && tz>=2 && tx<sizeX-1 && tz<sizeZ-3) { if (ta==9999) ta=t; else te=t; }
00368                         }
00369 
00370                         if (dz.z!=0)
00371                         {
00372                           t = (2-ppos.z)/dz.z;                      // Schneide Ray mit z=0
00373                           tx = ppos.x + t*dz.x;
00374                           ty = ppos.y + t*dz.y;
00375                           if (tx>=0 && ty>=0 && tx<sizeX-1 && ty<sizeY-1) { if (ta==9999) ta=t; else te=t; }
00376 
00377                           t = (sizeZ-3-ppos.z)/dz.z;               // Schneide Ray mit z=sizeZ
00378                           tx = ppos.x + t*dz.x;
00379                           ty = ppos.y + t*dz.y;
00380                           if (tx>=0 && ty>=0 && tx<sizeX-1 && ty<sizeY-1) { if (ta==9999) ta=t; else te=t; }
00381                         }
00382 
00383                         if (ta>te) {t=ta; ta=te; te=t;}
00384 
00385                         averg = 0;anzavg = 0;
00386 
00387                         if (te==9999)
00388                         {
00389                         averageview[k].r=averageview[k].b=averageview[k].g = 0;
00390                         }
00391                         else
00392                         {
00393                         value = 0; ita = (int)ta+512; ite = (int)te+512;
00394                         for (j=ita; j<ite; j += detval)
00395                         {
00396 /*
00397                           pfx = (ppos.x) + j * dz.x;
00398                           pfy = (ppos.y) + j * dz.y;
00399                           pfz = (ppos.z) + j * dz.z;
00400 */
00401                           anzavg++;
00402                           pfx = (ppos.x) + dlookup[j].x;
00403                           pfy = (ppos.y) + dlookup[j].y;
00404                           pfz = (ppos.z) + dlookup[j].z;
00405 
00406                           px = (int)pfx;
00407                           py = (int)pfy;
00408                           pz = (int)pfz;
00409                           aktpos = pagesize*pz + rowsize*py + px;
00410                           if (trilinear->Checked)
00411                           {
00412                                 sx = pfx-px;
00413                                 sy = pfy-py;
00414                                 sz = pfz-pz;
00415                                 s1x = 1-sx;
00416                                 s1y = 1-sy;
00417                                 value1 = (float)data[aktpos]*s1x                  + (float)data[aktpos+1]*sx;
00418                                 value2 = (float)data[aktpos+rowsize]*s1x          + (float)data[aktpos+rowsize+1]*sx;
00419                                 value3 = (float)data[aktpos+pagesize+rowsize]*s1x + (float)data[aktpos+pagesize+rowsize+1]*sx;
00420                                 value4 = (float)data[aktpos+pagesize]*s1x         + (float)data[aktpos+pagesize+1]*sx;
00421                                 value11 = value1*(s1y) + value2*(sy);
00422                                 value22 = value4*(s1y) + value3*(sy);
00423                                 value = (int)(value11*(1-sz) + value22*(sz));
00424                                 averg = averg + value;
00425                           }
00426                           else
00427                           {
00428                                 value = data[pagesize*pz + rowsize*py + px];
00429                                 averg = averg + value;
00430                           }
00431 
00432                          }
00433                          if (anzavg<5) { averageview[k].r = averageview[k].g = averageview[k].b = 0; }
00434                          else {
00435                          if ((averg / (anzavg*8))>255) {averageview[k].r = averageview[k].g = averageview[k].b = 255;}
00436                          else {averageview[k].r = averageview[k].g = averageview[k].b = (averg) / (anzavg*8);}}
00437                          }
00438                 k++;
00439                 }
00440         }
00441         FrameForm->StatusProgress->Visible = false;
00442 }
00443 
00444 
00445 void __fastcall TAverageForm::ResTrackBarChange(TObject *Sender)
00446 {
00447 switch (ResTrackBar->Position)
00448         {
00449         case 1: textx->Caption = "32"; texty->Caption = "32"; break;
00450         case 2: textx->Caption = "64"; texty->Caption = "64"; break;
00451         case 3: textx->Caption = "128"; texty->Caption = "128"; break;
00452         case 4: textx->Caption = "256"; texty->Caption = "256"; break;
00453         case 5: textx->Caption = "512"; texty->Caption = "512"; break;
00454         case 6: textx->Caption = "1024"; texty->Caption = "1024";
00455         }
00456 if (arepaint->Checked) {RayCast();PaintGL();}
00457         
00458 }
00459 //---------------------------------------------------------------------------
00460 
00461 void __fastcall TAverageForm::DetTrackBarChange(TObject *Sender)
00462 {
00463 detail->Caption = IntToStr(DetTrackBar->Position);
00464 if (arepaint->Checked) {RayCast();PaintGL();}
00465         
00466 }
00467 //---------------------------------------------------------------------------
00468 
00469 void __fastcall TAverageForm::autoresClick(TObject *Sender)
00470 {
00471 if (autores->Checked==true)
00472         {
00473         ResTrackBar->Enabled=false;
00474         textx->Enabled=false;
00475         Label2->Enabled=false;
00476         texty->Enabled=false;
00477         }
00478         else
00479         {
00480         ResTrackBar->Enabled=true;
00481         textx->Enabled=true;
00482         Label2->Enabled=true;
00483         texty->Enabled=true;
00484         }
00485 if (arepaint->Checked) {RayCast();PaintGL();}
00486 
00487 }
00488 //---------------------------------------------------------------------------
00489 
00490 void __fastcall TAverageForm::trilinearClick(TObject *Sender)
00491 {
00492 if (arepaint->Checked) {RayCast();PaintGL();}
00493         
00494 }
00495 //---------------------------------------------------------------------------
00496 

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