00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00023 #include "mandelbrot3d.h"
00024 #include "imagecalculator.h"
00025 #include <cmath>
00026
00030 MandelBrot3d::MandelBrot3d(QVector<QVector<int> >&vector, QVector<QVector<triplex> >&normalMap, QImage *d, QRectF window, int reso, int iterations):reso(reso), iterations(iterations), destination(d), projectedImage(vector), normalMap(normalMap), window(window){
00031 step.a = window.width()/ reso;
00032 step.b = window.height()/ reso;
00033 step.c = step.a;
00034
00035 projectedImage.clear();
00036 normalMap.clear();
00037
00038 for (int i=0; i<reso; i++){
00039 QVector<int> v;
00040 v.fill(-1, reso);
00041 projectedImage.push_back(v);
00042
00043 QVector<triplex> v2;
00044 v2.fill(triplex(), reso);
00045 normalMap.push_back(v2);
00046 }
00047 }
00048
00052 bool MandelBrot3d::processPoint(triplex& t){
00053 triplex start = t;
00054 triplex tmp = start;
00055
00056 for (int i=0; i<iterations; i++){
00057 if (tmp.norm2() > 4)
00058 return false;
00059
00060 tmp = tmp.square() + start;
00061 }
00062
00063 return true;
00064 }
00065
00072 int MandelBrot3d::process(){
00073 int max = 0;
00074 for (int i=0; i<reso; i++){
00075 float x = window.left() + i*step.a;
00076 for (int j=0; j<reso; j++){
00077 float y = window.top() + j * step.b;
00078 for (int k=0; k<reso; k++){
00079 float z = window.left() + k * step.c;
00080 if (processPoint(triplex(x,y,z))){
00081
00082 projectedImage[i][j] = k;
00083
00084 if (k > max)
00085 max = k;
00086
00087 break;
00088 }
00089 }
00090 }
00091
00092 emit progress(100*i/reso * 4/5);
00093 }
00094
00095
00096
00097
00098 for (int i=0; i<reso; i++){
00099 for (int j=0; j<reso; j++){
00100 int value = projectedImage[i][j];
00101 if (value == -1)
00102 value = 0;
00103 else
00104 value = max-value;
00105
00106 destination->setPixel(i,j, ImageCalculator::intToRGBA(value));
00107
00108
00109 float beforex = (i==0)?0: (projectedImage[i-1][j] - value);
00110 float afterx = (i==reso-1)?0: (value - projectedImage[i+1][j]);
00111 float beforey = (j==0)?0: (projectedImage[i][j-1] - value);
00112 float aftery = (j==reso-1)?0 : (value - projectedImage[i][j+1]);
00113
00114 QPointF p(afterx - beforex, aftery - beforey);
00115
00116 triplex n(p.x(), p.y(),10);
00117
00118 normalMap[i][j] = n.normalize();
00119 }
00120
00121 emit progress(80 +100*i/reso * 1/5);
00122 }
00123 return max;
00124 }