00001 #include "HistogramDraw.h" 00002 00003 #include "TransferFunctionGUI.h" 00004 #include "Volume.h" 00005 00006 HistogramDraw* HistogramDraw::_instance = NULL; 00007 bool HistogramDraw::initialized = false; 00008 00009 HistogramDraw::~HistogramDraw() 00010 { 00011 if(m_HistogramData) 00012 delete m_HistogramData; 00013 if(m_HistogramDataLogarithmic) 00014 delete m_HistogramData; 00015 } 00016 00017 HistogramDraw::HistogramDraw() 00018 { 00019 m_HistogramData = NULL; 00020 m_HistogramDataLogarithmic = NULL; 00021 m_HistogramDataSize = 0; 00022 m_HistogramDrawType = HISTOGRAM_DRAW_LOG; 00023 } 00024 00025 HistogramDraw *HistogramDraw::getInstance() 00026 { 00027 if(!initialized) 00028 { 00029 _instance = new HistogramDraw(); 00030 initialized = true; 00031 } 00032 00033 return _instance; 00034 } 00035 00036 00037 00038 00039 void HistogramDraw::refreshHistogramDrawData(Volume& volume) 00040 { 00041 if(m_HistogramData) 00042 delete m_HistogramData; 00043 if(m_HistogramDataLogarithmic) 00044 delete m_HistogramDataLogarithmic; 00045 00046 std::vector<double> normalizedHistogramData = volume.GetNormHistogram(); 00047 00048 m_HistogramDataSize = normalizedHistogramData.size(); 00049 m_HistogramData = new float[m_HistogramDataSize]; 00050 m_HistogramDataLogarithmic = new float[m_HistogramDataSize]; 00051 00052 rescaleHistogramDataToHighestValue(normalizedHistogramData); 00053 calculateLogarithmicHistogramData(); 00054 } 00055 00056 00057 void HistogramDraw::draw() 00058 { 00059 if(!m_HistogramDrawType == HISTOGRAM_DRAW_NONE) 00060 { 00061 int histogramDrawWidth = TransferFunctionGUI::g_iTransferFuncPickerWidth; 00062 00063 glLineWidth(1.0f); 00064 glEnable(GL_LINE_SMOOTH); 00065 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); 00066 00067 00068 glBegin(GL_LINES); 00069 glColor4f(0.3f,0.4f,0.4f,0.3f); 00070 00071 for(int i = 0; i < m_HistogramDataSize; ++i) 00072 { 00073 float yFactor; 00074 00075 if(m_HistogramDrawType == HISTOGRAM_DRAW_LINEAR) 00076 yFactor = m_HistogramData[i]; 00077 else if(m_HistogramDrawType == HISTOGRAM_DRAW_LOG) 00078 yFactor = m_HistogramDataLogarithmic[i]; 00079 00080 float y = TransferFunctionGUI::g_iTransferFuncPickerHeight * yFactor; 00081 float x = histogramDrawWidth * i / (float) m_HistogramDataSize; 00082 00083 glVertex2f(x, - y); 00084 glVertex2f(x, 0.f); 00085 } 00086 00087 00088 glEnd(); 00089 00090 glDisable(GL_LINE_SMOOTH); 00091 } 00092 } 00093 00094 00095 void HistogramDraw::setDrawType(HistogramDrawType drawType) 00096 { 00097 m_HistogramDrawType = drawType; 00098 } 00099 00100 void HistogramDraw::rescaleHistogramDataToHighestValue( std::vector<double> &normalizedHistogramData ) 00101 { 00102 float histoMax = 0.f; 00103 for(unsigned int i = 0; i < normalizedHistogramData.size(); ++i) 00104 { 00105 if(normalizedHistogramData.at(i) > histoMax) 00106 histoMax = (float)normalizedHistogramData.at(i); 00107 } 00108 00109 00110 for(unsigned int i = 0; i < normalizedHistogramData.size(); ++i) 00111 { 00112 m_HistogramData[i] = (float)normalizedHistogramData.at(i) / histoMax; 00113 } 00114 } 00115 00116 void HistogramDraw::calculateLogarithmicHistogramData() 00117 { 00118 for(int i = 0; i < m_HistogramDataSize; ++i) 00119 { 00120 m_HistogramDataLogarithmic[i] = 1.f - pow(10, ( - m_HistogramData[i] * 2.f)); 00121 } 00122 }