00001 #include "ThreadedMetaTracer.h"
00002 #include <boost/thread/thread.hpp>
00003 #include <boost/bind.hpp>
00004 #include <boost/ref.hpp>
00005
00006 namespace rcrt
00007 {
00008
00009 ThreadedMetaTracer::ThreadedMetaTracer(TracingStrategy* trac):
00010 TracingStrategy(trac->getScene()), tracer(trac)
00011 {
00012 }
00013
00014 ThreadedMetaTracer::~ThreadedMetaTracer()
00015 {
00016 }
00017
00018 RGBColor ThreadedMetaTracer::trace(Ray& r) const
00019 {
00020 return tracer->trace(r);
00021 }
00022
00023 Image ThreadedMetaTracer::trace(Camera* cam, const int& x0, const int& y0,
00024 const int& x1, const int& y1) const
00025 {
00026 int resX = x1-x0+1;
00027 int resY = y1-y0+1;
00028 int resXLeft = resX/2;
00029 int resXRight = resX-resXLeft;
00030 Image leftImg(resXLeft,resY);
00031 Image rightImg(resXRight,resY);
00032
00033
00034
00035 boost::thread left(boost::bind(boost::mem_fn(&ThreadedMetaTracer::traceJob),
00036 this, boost::ref(leftImg), cam, x0, y0, resXLeft-1, resY-1));
00037 boost::thread right(boost::bind(boost::mem_fn(&ThreadedMetaTracer::traceJob),
00038 this, boost::ref(rightImg), cam, resXLeft, y0, resX-1, resY-1));
00039
00040
00041 left.join();
00042 right.join();
00043
00044 Image finalImg(resX, resY);
00045
00046 for (int i = 0; i < resXLeft; i++) {
00047 for (int j = 0; j < resY; j++) {
00048 const RGBAColor& pixel = leftImg.getPixel(i,j);
00049 finalImg.setPixel(pixel,i,j);
00050 }
00051 }
00052 for (int i = 0; i < resXRight; i++) {
00053 for (int j = 0; j < resY; j++) {
00054 const RGBAColor& pixel = rightImg.getPixel(i,j);
00055 finalImg.setPixel(pixel,i+resXLeft,j);
00056 }
00057 }
00058
00059 return finalImg;
00060 }
00061
00062 void ThreadedMetaTracer::traceJob(Image& img, Camera* cam, const int& x0, const int& y0,
00063 const int& x1, const int& y1) const
00064 {
00065 Image retimg = tracer->trace(cam, x0, y0, x1, y1);
00066 for (int i = 0; i < (x1-x0+1); i++) {
00067 for (int j = 0; j < (y1-y0+1); j++) {
00068 const RGBAColor& pixel = retimg.getPixel(i,j);
00069 img.setPixel(pixel,i,j);
00070 }
00071 }
00072 }
00073
00074 }