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

transferfuncform.cpp

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

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