00001
00002
00003
00004 #include "stdafx.h"
00005 #include "3dvis.h"
00006 #include "transferfuncform.h"
00007 #include "transfuncfile.h"
00008
00009
00010 #include <string>
00011
00012
00013 #ifdef _DEBUG
00014 #define new DEBUG_NEW
00015 #undef THIS_FILE
00016 static char THIS_FILE[] = __FILE__;
00017 #endif
00018
00019 #include "data.h"
00020 extern Data *data3D;
00021
00022
00024
00025
00026
00027 transferfuncform::transferfuncform(CWnd* pParent )
00028 : CDialog(transferfuncform::IDD, pParent)
00029 {
00030
00031 m_dTransferTo = 0;
00032 m_sStaticRight = _T("4095");
00033 m_sStaticLeft = _T("0");
00034 m_sStaticFrom = _T("0");
00035 m_dTransferAlpha = 255;
00036 m_dSliderAlpha = 255;
00037 m_dStaticMaxDenstity = _T("");
00038
00039
00040
00041 INDICES i = { 0, 4095, 255, 0x00FFFFFF };
00042 m_vTransferIndices.push_back(i);
00043 m_cTemp = 0x00FFFFFF;
00044 }
00045
00046
00047 void transferfuncform::DoDataExchange(CDataExchange* pDX)
00048 {
00049 CDialog::DoDataExchange(pDX);
00050
00051 DDX_Text(pDX, IDC_EDIT_TRANSFER_TO, m_dTransferTo);
00052 DDV_MinMaxInt(pDX, m_dTransferTo, 0, 4095);
00053 DDX_Text(pDX, IDC_STATIC_RIGHT, m_sStaticRight);
00054 DDV_MaxChars(pDX, m_sStaticRight, 4);
00055 DDX_Text(pDX, IDC_STATIC_LEFT, m_sStaticLeft);
00056 DDV_MaxChars(pDX, m_sStaticLeft, 4);
00057 DDX_Text(pDX, IDC_STATIC_TRANSFER_FROM, m_sStaticFrom);
00058 DDV_MaxChars(pDX, m_sStaticFrom, 4);
00059 DDX_Text(pDX, IDC_EDIT_TRANSFER_ALPHA, m_dTransferAlpha);
00060 DDV_MinMaxInt(pDX, m_dTransferAlpha, 0, 255);
00061 DDX_Slider(pDX, IDC_SLIDER_TRANSFER_ALPHA, m_dSliderAlpha);
00062 DDX_Text(pDX, IDC_STATIC_TRANSFER_MAXDENS, m_dStaticMaxDenstity);
00063
00064 }
00065
00066
00067 void transferfuncform::m_ResetComboBox(bool clear) {
00068 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00069
00070 CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER);
00071 comboBox->ResetContent();
00072 comboBox->AddString("HISTOGRAM");
00073 comboBox->SetCurSel(0);
00074
00075 if (clear)
00076 return;
00077
00078
00079 comboBox->ResetContent();
00080 comboBox->AddString("HISTOGRAM");
00081
00082 std::string text = "TRANSFORM_";
00083 for (int i = 0; i < m_vTransferIndices.size() - 1; i++) {
00084 char s[5];
00085 _itoa(i, s, 10);
00086 text += s;
00087 comboBox->AddString(text.c_str());
00088 text.erase(10, strlen(s));
00089 }
00090
00091 comboBox->SetCurSel(0);
00092 }
00093
00094
00095 void transferfuncform::m_DrawHistogram(CDC *dc,
00096 int densityBegin,
00097 int densityEnd) {
00098
00099
00100
00101 dc->Rectangle(9, 30, 418, 280);
00102 int spanX = 418 - 9;
00103 int spanY = 280 - 30;
00104 int beginX = 9;
00105 int beginY = 280 - 1;
00106 int beginY2 = 30;
00107
00108
00109 int dLineWidth = 1;
00110 int dXRatio;
00111 int *hist = new int[spanX];
00112
00113
00114 if ((densityEnd - densityBegin) >= spanX)
00115 dXRatio = (densityEnd - densityBegin) / spanX;
00116 else {
00117 if ((densityEnd - densityBegin) == 0)
00118 dLineWidth = spanX;
00119 else {
00120 dXRatio = spanX / (densityEnd - densityBegin);
00121 dLineWidth = dXRatio;
00122 }
00123 }
00124
00125
00126 float dLocalMax = 0;
00127
00128 for (int i = 0; i < spanX; i++) {
00129 hist[i] = data3D->GetHistogram(densityBegin + i * dXRatio);
00130 if (hist[i] > dLocalMax)
00131 dLocalMax = hist[i];
00132 }
00133
00134 for (i = 0; i < spanX; i++)
00135 hist[i] = beginY2 + (spanY - (int)(((float)hist[i] / dLocalMax) * (float)spanY));
00136
00137
00138 CPen *pPen = (CPen *)dc->SelectStockObject(BLACK_PEN);
00139
00140 for (i = 0; i < spanX; i++) {
00141 dc->MoveTo(beginX + i, beginY);
00142 dc->LineTo(beginX + i, hist[i]);
00143 }
00144
00145 char temp[10];
00146 _itoa(dLocalMax, temp, 10);
00147 m_dStaticMaxDenstity = temp;
00148
00149
00150 if (hist)
00151 delete[] hist;
00152
00153 UpdateData(FALSE);
00154 }
00155
00156
00157 BEGIN_MESSAGE_MAP(transferfuncform, CDialog)
00158
00159 ON_WM_SHOWWINDOW()
00160 ON_WM_PAINT()
00161 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_COLOR, OnButtonTransferColor)
00162 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_SAVE, OnButtonTransferSave)
00163 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_CLEAR, OnButtonTransferClear)
00164 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_NEW, OnButtonTransferNew)
00165 ON_CBN_SELCHANGE(IDC_COMBO_TRANSFER, OnSelchangeComboTransfer)
00166 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_DELETE, OnButtonTransferDelete)
00167 ON_EN_CHANGE(IDC_EDIT_TRANSFER_ALPHA, OnChangeEditTransferAlpha)
00168 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_EXPORT, OnButtonTransferExport)
00169 ON_BN_CLICKED(IDC_BUTTON_TRANSFER_IMPORT, OnButtonTransferImport)
00170 ON_WM_HSCROLL()
00171 ON_EN_CHANGE(IDC_EDIT_TRANSFER_TO, OnChangeEditTransferTo)
00172
00173 END_MESSAGE_MAP()
00174
00175
00176
00177 void transferfuncform::OnOK()
00178 {
00179
00180
00181
00182 INDICES i;
00183
00184
00185
00186
00187
00188
00189
00190
00191 if ((i = m_vTransferIndices.back()).m_sdEnd != 4095) {
00192 INDICES j = { i.m_sdEnd + 1, 4095, 255, 0x00000000 };
00193 m_vTransferIndices.push_back(j);
00194 }
00195
00196 CDialog::OnOK();
00197 }
00198
00199 void transferfuncform::OnShowWindow(BOOL bShow, UINT nStatus)
00200 {
00201 CDialog::OnShowWindow(bShow, nStatus);
00202
00203
00204 if (!bShow)
00205 return;
00206
00207 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00208 ((CSliderCtrl *)GetDlgItem(IDC_SLIDER_TRANSFER_ALPHA))->SetRange(0, 255, TRUE);
00209
00210
00211
00212 CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER);
00213
00214 std::string text = "TRANSFORM_";
00215 for (int i = 0; i < m_vTransferIndices.size() - 1; i++) {
00216 char s[5];
00217 _itoa(i, s, 10);
00218 text += s;
00219 comboBox->AddString(text.c_str());
00220 text.erase(10, strlen(s));
00221 }
00222
00223 comboBox->SetCurSel(0);
00224
00225 UpdateData(FALSE);
00226 }
00227
00228
00229 void transferfuncform::OnPaint()
00230 {
00231 CPaintDC dc(this);
00232
00233 CComboBox *comboBox = (CComboBox *)this->GetDlgItem(IDC_COMBO_TRANSFER);
00234 int sel = comboBox->GetCurSel();
00235 dc.FillSolidRect(560, 159, 61, 23, m_vTransferIndices[sel].m_scColor);
00236
00237
00238 m_DrawHistogram(&dc,
00239 m_vTransferIndices[sel].m_sdBegin,
00240 m_vTransferIndices[sel].m_sdEnd);
00241
00242
00243
00244
00245
00246
00247
00248 }
00249
00250
00251 void transferfuncform::OnButtonTransferColor()
00252 {
00253 CComboBox *comboBox = (CComboBox *)this->GetDlgItem(IDC_COMBO_TRANSFER);
00254 int sel = comboBox->GetCurSel();
00255
00256 if (sel == CB_ERR)
00257 return;
00258
00259
00260 CColorDialog colDiag;
00261 if (colDiag.DoModal() != IDOK)
00262 return;
00263
00264 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(TRUE);
00265 COLORREF col = colDiag.GetColor();
00266
00267 m_cTemp = col;
00268
00269 CDC *cdc = (CDC *)this->GetDC();
00270 cdc->FillSolidRect(560, 159, 61, 23, col);
00271 }
00272
00273
00274 void transferfuncform::OnButtonTransferSave()
00275 {
00276
00277 if (((CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER))->GetCurSel() == 0)
00278 return;
00279
00280
00281 UpdateData(TRUE);
00282 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00283
00284 if (m_dTransferTo < atoi(m_sStaticFrom)) {
00285 MessageBox("to must be greater or equal than from");
00286 ((CEdit *)GetDlgItem(IDC_EDIT_TRANSFER_TO))->SetFocus();
00287 }
00288
00289 char text[5];
00290 m_sStaticLeft = m_sStaticFrom;
00291 _itoa(m_dTransferTo, text, 10);
00292 m_sStaticRight = text;
00293
00294 int index;
00295
00296 if ((index = ((CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER))->GetCurSel()) < m_vTransferIndices.size()) {
00297 m_vTransferIndices[index].m_scColor = m_cTemp;
00298 m_vTransferIndices[index].m_sdBegin = atoi(m_sStaticFrom);
00299 m_vTransferIndices[index].m_sdEnd = m_dTransferTo;
00300 m_vTransferIndices[index].m_ucAlpha = m_dTransferAlpha;
00301 }
00302 else {
00303 INDICES i = { atoi(m_sStaticFrom), m_dTransferTo, m_dTransferAlpha, m_cTemp };
00304 m_vTransferIndices.push_back(i);
00305 }
00306
00307
00308 if (index != m_vTransferIndices.size() - 1) {
00309 if (m_vTransferIndices[index].m_sdEnd != m_vTransferIndices[index + 1].m_sdBegin - 1)
00310 m_vTransferIndices[index + 1].m_sdBegin = m_vTransferIndices[index].m_sdEnd + 1;
00311 }
00312
00313
00314 m_bActive = false;
00315
00316
00317 m_DrawHistogram((CDC *)this->GetDC(),
00318 m_vTransferIndices[index].m_sdBegin,
00319 m_vTransferIndices[index].m_sdEnd);
00320
00321 ((CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER))->SetFocus();
00322 UpdateData(FALSE);
00323 }
00324
00325 void transferfuncform::OnButtonTransferClear()
00326 {
00327
00328 if (MessageBox("Clear the whole transfer-function?", "clear trans-func", MB_YESNO) == IDNO)
00329 return;
00330
00331 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00332
00333 m_ResetComboBox(TRUE);
00334
00335
00336 m_vTransferIndices.erase(m_vTransferIndices.begin() + 1, m_vTransferIndices.end());
00337 m_sStaticLeft = "0";
00338 m_sStaticRight = "4095";
00339 m_vTransferIndices[0].m_sdBegin = 0;
00340 m_vTransferIndices[0].m_sdEnd = 4095;
00341 m_vTransferIndices[0].m_scColor = 0x00FFFFFFFF;
00342 m_sStaticFrom = "0";
00343 m_dTransferTo = 4095;
00344
00345 CDC *cdc = (CDC *)this->GetDC();
00346 cdc->FillSolidRect(560, 159, 61, 23, 0x00FFFFFF);
00347
00348 UpdateData(FALSE);
00349 }
00350
00351
00352 void transferfuncform::OnButtonTransferNew()
00353 {
00354
00355 if (m_bActive)
00356 return;
00357
00358 m_bActive = true;
00359
00360
00361 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(TRUE);
00362
00363 CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER);
00364 int size = m_vTransferIndices.size() - 1;
00365 std::string text = "TRANSFORM_";
00366 char s[5];
00367 _itoa(size, s, 10);
00368 text += s;
00369
00370 comboBox->SetCurSel(comboBox->AddString(text.c_str()));
00371 INDICES i;
00372 if (!size) {
00373 _itoa(0, s, 10);
00374 m_sStaticFrom = s;
00375 }
00376 else {
00377 i = m_vTransferIndices.back();
00378 _itoa(i.m_sdEnd + 1, s, 10);
00379 m_sStaticFrom = s;
00380 }
00381
00382 m_dTransferTo = 4095;
00383
00384 UpdateData(FALSE);
00385 }
00386
00387 void transferfuncform::OnSelchangeComboTransfer()
00388 {
00389
00390 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00391
00392 CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER);
00393 int i = comboBox->GetCount();
00394 if ((m_vTransferIndices.size() != i) && (comboBox->GetCurSel() != (i - 1)))
00395 comboBox->DeleteString(i - 1);
00396
00397 i = comboBox->GetCurSel();
00398 char s[5];
00399 _itoa(m_vTransferIndices[i].m_sdBegin, s, 10);
00400 m_sStaticFrom = s;
00401 m_sStaticLeft = s;
00402 _itoa(m_vTransferIndices[i].m_sdEnd, s, 10);
00403 m_sStaticRight = s;
00404 m_dTransferTo = m_vTransferIndices[i].m_sdEnd;
00405
00406 m_dSliderAlpha = m_vTransferIndices[i].m_ucAlpha;
00407 m_dTransferAlpha = m_vTransferIndices[i].m_ucAlpha;
00408
00409 CDC *cdc = (CDC *)this->GetDC();
00410 cdc->FillSolidRect(560, 159, 61, 23, m_vTransferIndices[i].m_scColor);
00411
00412 m_DrawHistogram(cdc,
00413 m_vTransferIndices[i].m_sdBegin,
00414 m_vTransferIndices[i].m_sdEnd);
00415
00416 UpdateData(FALSE);
00417
00418 }
00419
00420 void transferfuncform::OnButtonTransferDelete()
00421 {
00422
00423 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(FALSE);
00424
00425
00426
00427 CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO_TRANSFER);
00428 int index;
00429 if ((index = comboBox->GetCurSel()) == 0)
00430 return;
00431
00432
00433 if (m_vTransferIndices.size() == 2) {
00434 m_ResetComboBox(TRUE);
00435 return;
00436 }
00437
00438
00439 INDEXLIST::iterator it = m_vTransferIndices.erase(m_vTransferIndices.begin() + index);
00440 INDEXLIST::iterator it2 = m_vTransferIndices.end();
00441
00442 if ((it - 1) == m_vTransferIndices.begin())
00443 it->m_sdBegin = 0;
00444 else if (it != m_vTransferIndices.end())
00445 it->m_sdBegin = (it - 1)->m_sdEnd + 1;
00446
00447
00448 m_sStaticFrom = "0";
00449 m_sStaticLeft = "0";
00450 m_sStaticRight = "4095";
00451 m_dTransferTo = 4095;
00452
00453
00454 m_ResetComboBox();
00455
00456
00457 CDC *cdc = (CDC *)this->GetDC();
00458 cdc->FillSolidRect(560, 159, 61, 23, 0x00FFFFFF);
00459
00460 UpdateData(FALSE);
00461 }
00462
00463 void transferfuncform::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
00464 {
00465
00466 UpdateData(TRUE);
00467 m_dTransferAlpha = m_dSliderAlpha;
00468 UpdateData(FALSE);
00469 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(TRUE);
00470
00471 CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
00472 }
00473
00474 void transferfuncform::OnChangeEditTransferAlpha()
00475 {
00476
00477
00478
00479
00480
00481
00482 UpdateData(TRUE);
00483 m_dSliderAlpha = m_dTransferAlpha;
00484 UpdateData(FALSE);
00485 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(TRUE);
00486 }
00487
00488
00489 void transferfuncform::OnButtonTransferExport()
00490 {
00491
00492 CFileDialog fd(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
00493 "Dat Files (*.tff)|*.tff| AllFiles (*.*)|* *|", this);
00494
00495
00496 int returnVal = fd.DoModal();
00497
00498 if (returnVal != IDOK)
00499 return;
00500
00501
00502 CString filename = fd.GetFileName();
00503 char *fn = new char[filename.GetLength() + 1];
00504 for (int i = 0; i < filename.GetLength(); i++) {
00505 fn[i] = filename.GetAt(i);
00506 }
00507 fn[i] = '\0';
00508
00509 CTransfuncFile funcFile;
00510 funcFile.SaveTransferFunc(m_vTransferIndices, fn);
00511 }
00512
00513 void transferfuncform::OnButtonTransferImport()
00514 {
00515
00516 CFileDialog fd(TRUE, NULL, NULL, OFN_FILEMUSTEXIST,
00517 "Dat Files (*.tff)|*.tff| AllFiles (*.*)|* *|", this);
00518
00519
00520 int returnVal = fd.DoModal();
00521
00522 if (returnVal != IDOK)
00523 return;
00524
00525
00526 CString filename = fd.GetFileName();
00527 char *fn = new char[filename.GetLength() + 1];
00528 for (int i = 0; i < filename.GetLength(); i++) {
00529 fn[i] = filename.GetAt(i);
00530 }
00531 fn[i] = '\0';
00532
00533 CTransfuncFile funcFile;
00534 m_vTransferIndices.clear();
00535 bool success;
00536 INDEXLIST temp = funcFile.LoadTransferFunc(fn, success);
00537 if (success)
00538 m_vTransferIndices = temp;
00539 else
00540 return;
00541
00542 m_cTemp = 0x00FFFFFF;
00543 m_sStaticFrom = "0";
00544 m_sStaticLeft = "0";
00545 m_sStaticRight = "4095";
00546 m_dTransferAlpha = 255;
00547 m_dSliderAlpha = 255;
00548 m_dTransferTo = 4095;
00549
00550 m_ResetComboBox();
00551
00552
00553 UpdateData(FALSE);
00554 }
00555
00556 void transferfuncform::OnChangeEditTransferTo()
00557 {
00558
00559
00560
00561
00562
00563
00564 UpdateData(TRUE);
00565 char s[5];
00566 _itoa(m_dTransferTo, s, 10);
00567 m_sStaticRight = s;
00568 UpdateData(FALSE);
00569 ((CButton *)GetDlgItem(IDC_BUTTON_TRANSFER_SAVE))->EnableWindow(TRUE);
00570 }