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