00001
00002 #include "stdafx.h"
00003 #include "slice.h"
00004
00005 Slice::Slice()
00006 {
00007 m_depth=0;
00008 m_height=0;
00009 m_width=0;
00010 m_type=0;
00011 slice_data = NULL;
00012 m_dSliceNumber = -1;
00013 m_bZoom = false;
00014 m_bUseTransfer = false;
00015 m_tTransferfunction = NULL;
00016 }
00017
00018
00019 Slice::~Slice()
00020 {
00021 if(slice_data != NULL) delete slice_data;
00022 }
00023
00024 bool
00025 Slice::SetType(int tp)
00026 {
00027 if ((tp < 0)||(tp > 2)) return false;
00028 m_type = tp;
00029 return true;
00030 }
00031
00032 int
00033 Slice::GetType()
00034 {
00035 return m_type;
00036 }
00037
00038
00039 int Slice::GetSliceNumber(Data *dat) {
00040 if (dat == NULL)
00041 return m_dSliceNumber;
00042
00043 switch (m_type) {
00044 case XY:
00045 m_dSliceNumber = dat->GetZDim() - 1;
00046 break;
00047 case XZ:
00048 m_dSliceNumber = dat->GetYDim() - 1;
00049 break;
00050 case YZ:
00051 m_dSliceNumber = dat->GetXDim() - 1;
00052 break;
00053 }
00054 return m_dSliceNumber;
00055 }
00056
00057
00058 bool
00059 Slice::SetPixels(Transfunc *tf, Data *dat, int dp)
00060 {
00061 if (m_bUseTransfer && tf == NULL)
00062 m_bUseTransfer = false;
00063
00064 int size = 0;
00065 if(slice_data != NULL) delete slice_data;
00066 m_depth = dp;
00067 int density;
00068
00069 if(m_type == XY)
00070 {
00071 m_width = (int)dat->GetXDim();
00072 m_height = (int)dat->GetYDim();
00073 size = m_width*m_height;
00074 slice_data = new rgb[size];
00075 for(int i = 0; i < m_height; i++)
00076 {
00077 for(int j = 0; j < m_width; j++)
00078 {
00079 if (m_bUseTransfer) {
00080 density = dat->GetDensity(j,i,m_depth);
00081 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00082 }
00083 else {
00084 density = dat->GetDensity(j, i, m_depth) / 16;
00085 density = CLAMP0255(density);
00086 rgb col;
00087 col.r = density;
00088 col.g = density;
00089 col.b = density;
00090 slice_data[i * m_width + j] = col;
00091 }
00092 }
00093 }
00094 }
00095
00096 if(m_type == XZ)
00097 {
00098 m_width = dat->GetXDim();
00099 m_height = dat->GetZDim();
00100 size = m_width*m_height;
00101 slice_data = new rgb[size];
00102 for(int i = 0; i < m_height; i++)
00103 {
00104 for(int j = 0; j < m_width; j++)
00105 {
00106 if (m_bUseTransfer) {
00107 density = dat->GetDensity(j,m_depth,i);
00108 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00109 }
00110 else {
00111 density = dat->GetDensity(j, m_depth, i) / 16;
00112 density = CLAMP0255(density);
00113 rgb col;
00114 col.r = density;
00115 col.g = density;
00116 col.b = density;
00117 slice_data[i * m_width + j] = col;
00118 }
00119 }
00120 }
00121 }
00122
00123 if(m_type == YZ)
00124 {
00125 m_width = dat->GetYDim();
00126 m_height = dat->GetZDim();
00127 size = m_width*m_height;
00128 slice_data = new rgb[size];
00129 for(int i = 0; i < m_height; i++)
00130 {
00131 for(int j = 0; j < m_width; j++)
00132 {
00133 if (m_bUseTransfer) {
00134 density = dat->GetDensity(m_depth,j,i);
00135 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00136 }
00137 else {
00138 density = dat->GetDensity(m_depth, j, i) / 16;
00139 density = CLAMP0255(density);
00140 rgb col;
00141 col.r = density;
00142 col.g = density;
00143 col.b = density;
00144 slice_data[i * m_width + j] = col;
00145 }
00146 }
00147 }
00148 }
00149
00150 return true;
00151 }
00152
00153
00154 #pragma warning(push)
00155 #pragma warning(disable : 4244)
00156
00157 bool
00158 Slice::DrawSlice(int width, int height)
00159 {
00160 float zoomx = (float)width / (float)m_width;
00161 float zoomy = (float)height / (float)m_height;
00162 float zoom;
00163
00164
00165 if (zoomx < zoomy) {
00166 zoom = zoomx;
00167 m_x = 0;
00168 m_y = height / 2 - (m_height * zoom) / 2;
00169 }
00170 else {
00171 zoom = zoomy;
00172 m_y = 0;
00173 m_x = width / 2 - (m_width * zoom) / 2;
00174 }
00175
00176
00177 if (!m_bZoom) {
00178 zoom = 1.0f;
00179 m_x = width / 2 - m_width / 2;
00180 m_y = height / 2 - m_height / 2;
00181 }
00182
00183
00184 if(slice_data == NULL) return false;
00185 glRasterPos2i(m_x, m_y);
00186 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
00187 glPixelZoom(zoom, zoom);
00188 glDrawPixels(m_width, m_height, GL_RGB, GL_UNSIGNED_BYTE, slice_data);
00189 return true;
00190 }
00191
00192 #pragma warning(pop)
00193
00194
00195 void Slice::GetImageInfo(int &width, int &height, int &x, int &y, int &plane, int &depth) {
00196 width = m_width;
00197 height = m_height;
00198 x = m_x;
00199 y = m_y;
00200 plane = m_type;
00201 depth = m_depth;
00202 }