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