www.pudn.com > Ì廿֯Âë.rar > slicer.cpp, change:2001-12-19,size:4546b


#include "volume.h" 
#include "slicer.h" 
 
Slicer::Slicer(Volume *vol, TransferFunction *tf) 
{ 
   m_volume = vol; 
   m_tf = tf; 
   m_slice_data = NULL; 
   m_slice_pos = 0; 
   m_slice_zoom = 1.0f; 
   m_threshold = 2048; 
   m_threshold_enabled = false; 
} 
  
Slicer::~Slicer() 
{ 
   if (m_slice_data != NULL) free(m_slice_data); 
} 
 
void  
Slicer::SetSliceOrientation(SliceOrientation so) 
{ 
   m_slice_orientation = so; 
 
   switch(m_slice_orientation) 
   { 
      case SO_XY: 
      { 
         m_slice_num = m_volume->GetDimZ(); 
         m_slice_width = m_volume->GetDimX(); 
         m_slice_height = m_volume->GetDimY(); 
         break; 
      } 
      case SO_XZ: 
      { 
         m_slice_num = m_volume->GetDimY(); 
         m_slice_width = m_volume->GetDimX(); 
         m_slice_height = m_volume->GetDimZ(); 
         break; 
      } 
      case SO_YZ: 
      { 
         m_slice_num = m_volume->GetDimX(); 
         m_slice_width = m_volume->GetDimY(); 
         m_slice_height = m_volume->GetDimZ(); 
         break; 
      } 
   } 
   m_slice_pos = 0; 
   m_slice_size = m_slice_width * m_slice_height * sizeof(rgba); 
   m_slice_data = (rgba*)realloc(m_slice_data, m_slice_size); 
   SetSliceDrawSize(m_slice_draw_width, m_slice_draw_height); 
} 
 
void  
Slicer::SetSlicePixel(int x, int y, data_t data) 
{ 
   unsigned char i; 
   rgba color; 
 
   if (m_usetf) 
   { 
      m_tf->GetBlendedColor(data, color); 
      if (m_threshold_enabled)  
      { 
         if (m_threshold > data) 
         { 
            anycolor(color, 0, 0, 0); 
         } 
      } 
   } 
   else 
   { 
      i = (char)(data >> 4); 
      if (m_threshold_enabled)  
      { 
         if (m_threshold = data) 
         { 
            anycolor(color, 255, 255, 255); 
         } 
         else 
         { 
            anycolor(color, 0, 0, 0); 
         } 
      } 
      else 
      { 
         anycolor(color, i, i, i); 
      } 
   } 
   m_slice_data[y * m_slice_width + x] = color; 
} 
 
void  
Slicer::SetSlicePos(int pos) 
{   
   if (pos  0) pos = m_slice_pos; 
   if ((pos  0) || (pos >= m_slice_num)) return; 
   m_slice_pos = pos; 
 
   switch(m_slice_orientation) 
   { 
      case SO_XY: 
      { 
         for (int y=0; y<m_slice_height; y++) 
            for (int x=0; x<m_slice_width; x++) 
            { 
               SetSlicePixel(x, y, m_volume->GetData(x,y,m_slice_pos)); 
            } 
         break; 
      } 
      case SO_XZ: 
      { 
         for (int y=0; y<m_slice_height; y++) 
            for (int x=0; x<m_slice_width; x++) 
            { 
               SetSlicePixel(x, y, m_volume->GetData(x,m_slice_pos,y)); 
            } 
         break; 
      } 
      case SO_YZ: 
      { 
         for (int y=0; y<m_slice_height; y++) 
            for (int x=0; x<m_slice_width; x++) 
            { 
               SetSlicePixel(x, y, m_volume->GetData(m_slice_pos,x,y)); 
            } 
         break; 
      } 
   } 
} 
 
void  
Slicer::SetSliceDrawSize(int width, int height) 
{ 
   m_slice_draw_width = width; 
   m_slice_draw_height = height; 
   float pr1 = ((float)width)/((float)height); 
   float pr2 = ((float)m_slice_width)/((float)m_slice_height); 
   if (pr1  pr2) 
   { 
      m_slice_zoom = ((float)width)/((float)m_slice_width); 
   } 
   else 
   { 
      m_slice_zoom = ((float)height)/((float)m_slice_height); 
   } 
} 
 
int  
Slicer::GetSliceNum() 
{ 
   return m_slice_num; 
} 
 
int  
Slicer::GetSliceWidth() 
{ 
   return m_slice_width; 
} 
 
int  
Slicer::GetSliceHeight() 
{ 
   return m_slice_height; 
} 
 
//------------------------------------------------------------------- 
 
void  
Slicer::SetThreshold(int threshold) 
{ 
   m_threshold = threshold; 
} 
 
void  
Slicer::UseThreshold(bool use) 
{ 
   m_threshold_enabled = use; 
} 
 
void  
Slicer::UseTransFunc(bool use) 
{ 
   m_usetf = use; 
} 
 
//------------------------------------------------------------------- 
 
void  
Slicer::glDrawSlice() 
{ 
   if (m_slice_data != NULL) 
   {     
   	glMatrixMode(GL_PROJECTION); 
   	glLoadIdentity();				 
      glOrtho(0, m_slice_draw_width, 0, m_slice_draw_height, -1, 1); 
   	glMatrixMode(GL_MODELVIEW);		 
	   glLoadIdentity();							 
    
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer 
      glRasterPos3i(0,0,0); 
      glPixelZoom(m_slice_zoom, m_slice_zoom); 
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
      glDrawPixels(m_slice_width, m_slice_height, GL_RGBA, GL_UNSIGNED_BYTE, m_slice_data); 
   } 
}