Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

3dvis.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 // 3dvis.cpp : Defines the class behaviors for the application.
00005 //
00006 
00007 #include "stdafx.h"
00008 #include "3dvis.h"
00009 
00010 #include "MainFrm.h"
00011 #include "3dvisDoc.h"
00012 #include "3dvisView.h"
00013 
00014 #include "transfuncfile.h"
00015 #include "FileInfoDialog.h"
00016 #include "VolumeDialog.h"
00017 #include "IniFile.h"
00018 
00019 
00020 #ifdef _DEBUG
00021 #define new DEBUG_NEW
00022 #undef THIS_FILE
00023 static char THIS_FILE[] = __FILE__;
00024 #endif
00025 
00027 // CMy3dvisApp
00028 
00029 BEGIN_MESSAGE_MAP(CMy3dvisApp, CWinApp)
00030         //{{AFX_MSG_MAP(CMy3dvisApp)
00031         ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
00032         ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
00033         ON_COMMAND(ID_F_CLOSE, OnFClose)
00034         ON_COMMAND(ID_RENDER_VOLUME, OnRenderVolume)
00035         ON_COMMAND(ID_OPTIONS_TRANSFER, OnOptionsTransfer)
00036         ON_COMMAND(ID_PRESET_0, OnPreset0)      
00037         ON_COMMAND(ID_PRESET_1, OnPreset1)      
00038         ON_COMMAND(ID_PRESET_2, OnPreset2)      
00039         ON_COMMAND(ID_PRESET_3, OnPreset3)      
00040         ON_COMMAND(ID_PRESET_4, OnPreset4)      
00041         ON_COMMAND(ID_PRESET_5, OnPreset5)      
00042         ON_COMMAND(ID_PRESET_6, OnPreset6)      
00043         ON_COMMAND(ID_PRESET_7, OnPreset7)      
00044         ON_COMMAND(ID_PRESET_8, OnPreset8)      
00045         ON_COMMAND(ID_PRESET_9, OnPreset9)      
00046         ON_COMMAND(ID_FILE_INFO, OnFileInfo)
00047         ON_COMMAND(ID_MENU_VOLUME, OnMenuVolume)
00048         ON_COMMAND(ID_FILE_INI, OnFileIni)
00049         //}}AFX_MSG_MAP
00050         // Standard file based document commands
00051         ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
00052 END_MESSAGE_MAP()
00053 
00054 
00055 // CMy3dvisApp construction
00056 
00057 CMy3dvisApp::CMy3dvisApp()
00058 {
00059         // TODO: add construction code here,
00060         // Place all significant initialization in InitInstance
00061         m_bRenderVolume = false;
00062         m_bClicked = false;
00063         transferFunction = NULL;
00064 
00065         m_dFileThres = 40;
00066         m_dPacketSize = 10;
00067 
00068         m_dOperation = 0;
00069 
00070         char path[_MAX_PATH + 1];
00071 
00072         GetModuleFileName(m_hInstance, path, sizeof(path));
00073 
00074         string temp = path;
00075         int pos = temp.find_last_of('\\', temp.size());
00076         programPath = temp.substr(0, pos + 1);
00077 }
00078 
00080 // The one and only CMy3dvisApp object
00081 
00082 CMy3dvisApp theApp;
00083 
00085 // CMy3dvisApp initialization
00086 
00087 BOOL CMy3dvisApp::InitInstance()
00088 {
00089 
00090         CTransfuncFile funcFile;
00091         presetTransformFunctions = funcFile.LoadPresets(m_dNumPresets);
00092 
00093 
00094         // Standard initialization
00095         // If you are not using these features and wish to reduce the size
00096         //  of your final executable, you should remove from the following
00097         //  the specific initialization routines you do not need.
00098 
00099 #ifdef _AFXDLL
00100         Enable3dControls();                     // Call this when using MFC in a shared DLL
00101 #else
00102         Enable3dControlsStatic();       // Call this when linking to MFC statically
00103 #endif
00104 
00105         // Change the registry key under which our settings are stored.
00106         // TODO: You should modify this string to be something appropriate
00107         // such as the name of your company or organization.
00108         SetRegistryKey(_T("Local AppWizard-Generated Applications"));
00109 
00110         LoadStdProfileSettings();  // Load standard INI file options (including MRU)
00111 
00112         // Register the application's document templates.  Document templates
00113         //  serve as the connection between documents, frame windows and views.
00114 
00115         CSingleDocTemplate* pDocTemplate;
00116         pDocTemplate = new CSingleDocTemplate(
00117                 IDR_MAINFRAME,
00118                 RUNTIME_CLASS(CMy3dvisDoc),
00119                 RUNTIME_CLASS(CMainFrame),       // main SDI frame window
00120                 RUNTIME_CLASS(CMy3dvisView));
00121         AddDocTemplate(pDocTemplate);
00122 
00123         // Parse command line for standard shell commands, DDE, file open
00124         CCommandLineInfo cmdInfo;
00125         ParseCommandLine(cmdInfo);
00126 
00127         // Dispatch commands specified on the command line
00128         if (!ProcessShellCommand(cmdInfo))
00129                 return FALSE;
00130 
00131         
00132         m_ProcessIniFile();
00133 
00134 
00135         // The one and only window has been initialized, so show and update it.
00136         m_pMainWnd->ShowWindow(SW_SHOW);
00137         m_pMainWnd->UpdateWindow();
00138         return TRUE;
00139 }
00140 
00141 
00143 // CAboutDlg dialog used for App About
00144 
00145 class CAboutDlg : public CDialog
00146 {
00147 public:
00148         CAboutDlg();
00149 
00150 // Dialog Data
00151         //{{AFX_DATA(CAboutDlg)
00152         enum { IDD = IDD_ABOUTBOX };
00153         //}}AFX_DATA
00154 
00155         // ClassWizard generated virtual function overrides
00156         //{{AFX_VIRTUAL(CAboutDlg)
00157         protected:
00158         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
00159         //}}AFX_VIRTUAL
00160 
00161 // Implementation
00162 protected:
00163         //{{AFX_MSG(CAboutDlg)
00164                 // No message handlers
00165         //}}AFX_MSG
00166         DECLARE_MESSAGE_MAP()
00167 };
00168 
00169 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
00170 {
00171         //{{AFX_DATA_INIT(CAboutDlg)
00172         //}}AFX_DATA_INIT
00173 }
00174 
00175 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
00176 {
00177         CDialog::DoDataExchange(pDX);
00178         //{{AFX_DATA_MAP(CAboutDlg)
00179         //}}AFX_DATA_MAP
00180 }
00181 
00182 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
00183         //{{AFX_MSG_MAP(CAboutDlg)
00184                 // No message handlers
00185         //}}AFX_MSG_MAP
00186 END_MESSAGE_MAP()
00187 
00188 // App command to run the dialog
00189 void CMy3dvisApp::OnAppAbout()
00190 {
00191         CAboutDlg aboutDlg;
00192         aboutDlg.DoModal();
00193 }
00194 
00196 // CMy3dvisApp message handlers
00197 
00198 
00199 int CMy3dvisApp::ExitInstance() 
00200 {
00201         // TODO: Add your specialized code here and/or call the base class
00202         if (data3D != NULL) {
00203                 delete data3D;
00204                 data3D = NULL;
00205         }
00206 
00207         if (slice != NULL) {
00208                 delete slice;
00209                 slice = NULL;
00210         }
00211 
00212         if (raycaster != NULL) {
00213                 delete raycaster;
00214                 raycaster = NULL;
00215         }
00216 
00217         if (transferFunction != NULL) {
00218                 delete transferFunction;
00219                 transferFunction = NULL;
00220         }
00221         
00222         return CWinApp::ExitInstance();
00223 }
00224 
00229 void CMy3dvisApp::OnFileOpen() 
00230 {
00231         // TODO: Add your command handler code here
00232         CFileDialog fd(TRUE, NULL, NULL, OFN_FILEMUSTEXIST,
00233                                         "Dat Files (*.dat)|*.dat| AllFiles (*.*)|* *|", m_pMainWnd);
00234         
00235 
00236         int returnVal = fd.DoModal();
00237 
00238         if(returnVal == IDOK) {
00239 
00240                 m_bOpen = true;
00241                 this->OnFClose();
00242                 m_bOpen = false;
00243                         
00244                 if (data3D != NULL) {
00245                         if (MessageBox(m_pMainWnd->m_hWnd, "Already data loaded! Unload currend data?",
00246                                                                                                         "load new data", MB_YESNO) == IDYES) {
00247                                 delete data3D;
00248                                 data3D = NULL;
00249                         }
00250                         else
00251                                 return;
00252                 }
00253 
00254 
00255                 CString filename = fd.GetFileName();
00256                 char *fn = new char[filename.GetLength() + 1];
00257                 for (int i = 0; i < filename.GetLength(); i++) {
00258                         fn[i] = filename.GetAt(i);
00259                 }
00260                 fn[i] = '\0';
00261 
00262                 
00263                 data3D = new Data();
00264                 m_ProcessIniFile();
00265 
00266 
00267                 SetWindowText(m_pMainWnd->m_hWnd, "3D Visualisation - loading visualisation data, please wait,...");
00268 
00269                 data3D->LoadData(fn);
00270                 delete[] fn;
00271 
00272         
00273                 m_bRenderVolume = false;
00274 
00275 
00276                 if (slice == NULL)
00277                         slice = new Slice();
00278 
00279                 slice->SetPixels(data3D, 0);
00280                 int num = slice->GetSliceNumber(data3D);
00281 
00282 
00283                 
00284                 if (!m_bRenderVolume)
00285                         AfxGetMainWnd()->SendMessage(MYWM_SHOW_BAR, (WPARAM) 0, (LPARAM) 0);
00286                 else
00287                         AfxGetMainWnd()->SendMessage(MYWM_SHOW_BAR, (WPARAM) 1, (LPARAM) 0);
00288                 
00289 
00290                 SetWindowText(m_pMainWnd->m_hWnd, CString("3D Visualisation - loaded \"" + filename + "\""));
00291    }
00292 }
00293 
00296 void CMy3dvisApp::OnFClose() 
00297 {
00298         // TODO: Add your command handler code here
00299 
00300         if (data3D && !m_bOpen && MessageBox(m_pMainWnd->m_hWnd, "do you really want to close the\ncurrent data-set?",
00301                                         "close data-set", MB_YESNO) == IDNO)
00302                 return;
00303 
00304         
00305         AfxGetMainWnd()->SendMessage(MYWM_HIDE_BARS);
00306 
00307         
00308         if (slice != NULL) {
00309                 delete slice;
00310                 slice = NULL;
00311         }
00312 
00313         if (data3D != NULL) {
00314                 delete data3D;
00315                 data3D = NULL;
00316         }
00317 
00318         m_pMainWnd->SetWindowText("3D Visualisation");
00319 }
00320 
00323 void CMy3dvisApp::OnOptionsTransfer() 
00324 {
00325         // TODO: Add your command handler code here
00326         if (data3D == NULL)
00327                 return;
00328 
00329         int response = m_Transferdiag.DoModal();
00330 
00331 
00332         if (response == IDOK) {
00333                 int num = m_Transferdiag.m_vTransferIndices.size() - 1;
00334 
00335                 if (num <= 0)
00336                         return;
00337 
00338                 m_BuildTransferFunction(m_Transferdiag.m_vTransferIndices, num);
00339         }
00340 
00341 
00342         m_pMainWnd->Invalidate();
00343         m_pMainWnd->UpdateWindow();
00344 }
00345 
00346 
00347 void CMy3dvisApp::OnPreset0() {
00348         m_BuildTransferFunction(presetTransformFunctions[0],
00349                                                         presetTransformFunctions[0].size() - 1);
00350 }
00351 
00352 void CMy3dvisApp::OnPreset1() {
00353         m_BuildTransferFunction(presetTransformFunctions[1],
00354                                                         presetTransformFunctions[1].size() - 1);
00355 }
00356 
00357 void CMy3dvisApp::OnPreset2() {
00358         m_BuildTransferFunction(presetTransformFunctions[2],
00359                                                         presetTransformFunctions[2].size() - 1);
00360 }
00361 
00362 void CMy3dvisApp::OnPreset3() {
00363         m_BuildTransferFunction(presetTransformFunctions[3],
00364                                                         presetTransformFunctions[3].size() - 1);
00365 }
00366 
00367 void CMy3dvisApp::OnPreset4() {
00368         m_BuildTransferFunction(presetTransformFunctions[4],
00369                                                         presetTransformFunctions[4].size() - 1);
00370 }
00371 
00372 void CMy3dvisApp::OnPreset5() {
00373         m_BuildTransferFunction(presetTransformFunctions[5],
00374                                                         presetTransformFunctions[5].size() - 1);
00375 }
00376 
00377 void CMy3dvisApp::OnPreset6() {
00378         m_BuildTransferFunction(presetTransformFunctions[6],
00379                                                         presetTransformFunctions[6].size() - 1);
00380 }
00381 
00382 void CMy3dvisApp::OnPreset7() {
00383         m_BuildTransferFunction(presetTransformFunctions[7],
00384                                                         presetTransformFunctions[7].size() - 1);
00385 }
00386 
00387 void CMy3dvisApp::OnPreset8() {
00388         m_BuildTransferFunction(presetTransformFunctions[8],
00389                                                         presetTransformFunctions[8].size() - 1);
00390 }
00391 
00392 void CMy3dvisApp::OnPreset9() {
00393         m_BuildTransferFunction(presetTransformFunctions[9],
00394                                                         presetTransformFunctions[9].size() - 1);
00395 }
00396 
00399 void CMy3dvisApp::m_BuildTransferFunction(INDEXLIST &function, int num) {
00400         if (!data3D)
00401                 return;
00402         
00403         if (!transferFunction)
00404                 transferFunction = new Transfunc();
00405 
00406         rgb col;
00407 
00408         float alpha_begin = 0.0;
00409         float alpha_end = 1.0;
00410 
00411 
00412         for (int i = 1; i <= num; i++) {
00413                 COLORREF c = function[i].m_scColor;
00414                 col.r = (unsigned char)(function[i].m_scColor & 0x000000FF);
00415                 col.g = (unsigned char)((function[i].m_scColor & 0x0000FF00) >> 8);
00416                 col.b = (unsigned char)((function[i].m_scColor & 0x00FF0000) >> 16);
00417 
00418                 float thres = function[i].m_sdEnd - function[i].m_sdBegin;
00419                 float size = thres * 0.1;
00420                 
00421                 if (i > 1)
00422                         alpha_begin = (float)function[i - 1].m_ucAlpha / 255;
00423                 else
00424                         alpha_begin = 0.0;
00425 
00426 
00427                 if (i == num)
00428                         alpha_end = 0.0;
00429                 else
00430                         alpha_end = (float)function[i].m_ucAlpha / 255;
00431 
00432                 float alpha = alpha_end - alpha_begin;
00433                 float step = alpha / size;
00434                 if (i > 1)
00435                         alpha = (float)function[i].m_ucAlpha / 255;
00436 
00437 
00438                 for (int j = function[i].m_sdBegin;     j < function[i].m_sdEnd; j++) {
00439                         transferFunction->SetDensityColor(j, col);
00440                         if ((alpha > 0.0) && ((alpha + step) <= alpha_end))
00441                                 alpha += step;
00442 //                      transferFunction->SetOpacity(j, ((float)function[i].m_ucAlpha / 255));
00443                         transferFunction->SetOpacity(j, alpha);
00444                 }
00445         }
00446 
00447         if (slice == NULL)
00448                 slice = new Slice();
00449 
00450         slice->SetTransferfunction(transferFunction);
00451         slice->SetPixels(data3D, -1);
00452 }
00453 
00454 
00455 void CMy3dvisApp::OnFileInfo() 
00456 {
00457         // TODO: Add your command handler code here
00458         if (!data3D)
00459                 return;
00460         
00461         CFileInfoDialog fid;
00462         fid.SetInfo(data3D->filename, data3D->GetXDim(), data3D->GetYDim(), data3D->GetZDim());
00463         fid.DoModal();
00464 }
00465 
00466 void CMy3dvisApp::OnMenuVolume() 
00467 {
00468         // TODO: Add your command handler code here
00469         AfxGetMainWnd()->SendMessage(MYWM_CHANGE_RENDERING, (WPARAM) 1, 1);
00470 }
00471 
00472 
00476 void CMy3dvisApp::m_ProcessIniFile(bool store) {
00477         FILE *file;
00478 
00479         string filename = programPath + "config.ini";
00480 
00481         if (!store && (file = fopen(filename.c_str(), "r")) != NULL) {
00482                 if (!data3D)
00483                         return;
00484                 
00485                 fscanf(file, "%d\n%d", &m_dFileThres, &m_dPacketSize);
00486                 data3D->SetFileThreshold(m_dFileThres);
00487                 data3D->SetDataPackets(m_dPacketSize);
00488 
00489                 fclose(file);
00490 
00491                 return;
00492         }
00493 
00494 
00495         if ((file = fopen(filename.c_str(), "w")) != NULL) {
00496                 m_dFileThres = 40;
00497                 m_dPacketSize = 10;
00498 
00499                 fprintf(file, "%d\n%d", m_dFileThres, m_dPacketSize);
00500                 fclose(file);
00501         }
00502 
00503 
00504 }
00505 
00506 void CMy3dvisApp::OnFileIni() 
00507 {
00508         // TODO: Add your command handler code here
00509         IniFile inifile;
00510         inifile.SetupData(m_dFileThres, m_dPacketSize);
00511         int answer = inifile.DoModal();
00512 
00513         if (answer == IDCANCEL)
00514                 return;
00515         
00516         
00517         inifile.GetData(m_dFileThres, m_dPacketSize);   
00518         m_ProcessIniFile(true);
00519 }
00520 
00521 
00522 
00525 void CMy3dvisApp::OnRenderVolume() 
00526 {
00527         // TODO: Add your command handler code here
00528         if (!data3D || !slice)
00529                 return;
00530 
00531         if (!((CMy3dvisApp *)AfxGetApp())->transferFunction) {
00532                 MessageBox(m_pMainWnd->m_hWnd,
00533                                         "please select or create a transferfunction",
00534                                         "transferfunction missing",
00535                                         MB_OK);
00536                 return;
00537         }
00538                 
00539 
00540         if (((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume) {
00541                 ((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume = false;
00542 
00543                 AfxGetMainWnd()->SendMessage(MYWM_CHANGE_RENDERING, (WPARAM) 0, (LPARAM) 0);
00544         }
00545         else {
00546                 ((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume = true;
00547 
00548                 AfxGetMainWnd()->SendMessage(MYWM_CHANGE_RENDERING, (WPARAM) 1, (LPARAM) 0);
00549 
00550                 raycaster = new Raycaster();
00551                 raycaster->SetViewingCondition(VECTOR(300,300,300),Color(0.0f,1.0f,0.0f),0.3f,0.4f,0.2f,8);
00552 
00553                 raycaster->Initialize(/*VECTOR(92,-20,85),*/400,400,1.0f,data3D,((CMy3dvisApp *)AfxGetApp())->transferFunction);
00554                 raycaster->Zoom(1.1f); // werte > 1.0 hinein zoomen <1.0 wegzoomen
00555 
00556                 raycaster->SetTreshold(1500);
00557                 raycaster->SetRaytype(NN);
00558                 raycaster->SetRendermode(FH);
00559                 raycaster->SetPreCalcGradients(true);
00560         }
00561 }

Generated on Thu Jan 30 21:35:43 2003 for 3DVis by doxygen1.3-rc2