00001
00002
00003 #ifndef RAYCASTER_H_
00004 #define RAYCASTER_H_
00005
00006 #include "ARender.h"
00007 #include "Vector.h"
00008 #include "Data.h"
00009 #include "Size.h"
00010 #include "Plane.h"
00011 #include "Lighting.h"
00012 #include "Gradient.h"
00013
00019 enum RenderMode{
00020 RM_FrontToBack,
00021
00022 RM_BackToFront
00023 };
00024
00029 enum InterpolationMode{
00030 IM_NearestNeighbour,
00031 IM_Trilinear
00032
00033 };
00034
00035
00036
00058 class RayCaster : public ARender {
00059 public:
00060 RayCaster( Data& aData, Gradient& aGradient, Size s = Size(1,1), Color bgColor = Color(0,0,0,255),
00061 RenderMode renderMode = RM_FrontToBack, InterpolationMode interpolationMode = IM_NearestNeighbour);
00062 ~RayCaster();
00063
00064 void setViewerPosition(const Vector<>& A, const Vector<>& B,
00065 const Vector<>& C);
00066
00067 void setImageSize(const Size& s);
00068
00069
00070 virtual Color getPixelColor(unsigned int x, unsigned int y);
00071 virtual Size getImageSize() const;
00072 virtual void render(unsigned char* buffer);
00073
00074 void setRenderMode(RenderMode newRenderMode);
00075 RenderMode getRenderMode() const;
00076
00077 void setInterpolationMode(InterpolationMode newInterpolationMode);
00078 InterpolationMode getInterpolationMode() const;
00079
00080 void setBGColor(Color newBGColor);
00081 Color getBGColor() const;
00082
00083 void setSampleRate(float newRate);
00084 float getSampleRate() const;
00085
00086 void enableLighting();
00087 void disableLighting();
00088 bool isLightingEnabled() const;
00089
00090 Lighting* lighting();
00091
00092 protected:
00093 Color applyTransferFunctions(unsigned short aDataValue, const Vector<>& gradient) const;
00094 private:
00095 void calcVectors();
00100 std::auto_ptr< Vector<> > intersectRayWithNearestPlane (const Vector<>& startPos, const Vector<>& direction) const;
00101 std::auto_ptr< Vector<> > intersectRayWithFarthestPlane (const Vector<>& startPos, const Vector<>& direction) const;
00102
00106 unsigned short getDataAt(float x, float y, float z) const {
00107
00108 return mData.getData(int(x), int(y), int(z));
00109 }
00110
00111 unsigned short getDataTrilinearInterpolated(float x, float y, float z){
00112 return mData.getDataTrilinearInterpolated(x,y,z);
00113 }
00114
00115 Vector<> getGradientAt(float x, float y, float z) const{
00116 return mGradient.getData(int(x), int(y), int(z));
00117 }
00118
00119 Vector<> getGradientTrilinearInterpolated(float x, float y, float z){
00120 return mGradient.getDataTrilinearInterpolated(x,y,z);
00121 }
00122
00123
00124 Color getPixelColor_FrontToBack(unsigned int x, unsigned int y);
00125 Color getPixelColor_BackToFront(unsigned int x, unsigned int y);
00126
00127 void render_FrontToBack(unsigned char* buffer);
00128 void render_BackToFront(unsigned char* buffer);
00129
00133 void compositeBackToFront(Color& dest, const Color& src) {
00134 float fR = dest.R() / float(255);
00135 float fG = dest.G() / float(255);
00136 float fB = dest.B() / float(255);
00137
00138 float sR = src.R() / float(255);
00139 float sG = src.G() / float(255);
00140 float sB = src.B() / float(255);
00141 float sA = src.A() / float(255);
00142
00143 dest.setR((Color::ColorUnit) (255 * (fR * (1 - sA) + sR * sA)));
00144 dest.setG((Color::ColorUnit) (255 * (fG * (1 - sA) + sG * sA)));
00145 dest.setB((Color::ColorUnit) (255 * (fB * (1 - sA) + sB * sA)));
00146 }
00147
00148 void compositeFrontToBack(Color& dest, const Color& src) {
00149
00150
00151
00152 float fA = dest.A() / float(255);
00153
00154 float sR = src.R() / float(255);
00155 float sG = src.G() / float(255);
00156 float sB = src.B() / float(255);
00157 float sA = src.A() / float(255);
00158
00159 dest.setR((Color::ColorUnit) (dest.R() + 255 * (sR * ((1-fA) * sA))));
00160 dest.setG((Color::ColorUnit) (dest.G() + 255 * (sG * ((1-fA) * sA))));
00161 dest.setB((Color::ColorUnit) (dest.B() + 255 * (sB * ((1-fA) * sA))));
00162
00163
00164
00165
00166
00167
00168
00169
00170 dest.setA((Color::ColorUnit) (255 * (fA + (1-fA) * sA)));
00171 }
00172
00173 Data& mData;
00174 Gradient& mGradient;
00175 Size mImageSize;
00176 Vector<> mTopLeft, mTopRight, mLowerRight, mViewingDirection;
00177
00178
00179
00180 Vector<> mRightVec, mDownVec;
00181
00182 Lighting mLighting;
00183
00184 Color mBGColor;
00185
00186 RenderMode mRenderMode;
00187
00188 InterpolationMode mInterpolationMode;
00189 float mSampleRate;
00190 bool mEnableLighting;
00191
00192 const Vector<> mMaxDataVector;
00193
00194
00195 const Plane mFrontPlane;
00196 const Plane mBackPlane;
00197
00198 const Plane mTopPlane;
00199 const Plane mBottomPlane;
00200
00201 const Plane mLeftPlane;
00202 const Plane mRightPlane;
00203
00204 };
00205
00206 #endif