C:/sources/c++/buddhabrot/buddhabrot/mandelbrot3d.cpp

00001 /*
00002 Copyright 2010 Pierre SCHWARTZ
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
00005 documentation files (the "Software"), to deal in the Software without restriction, including without 
00006 limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
00007 of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
00008 conditions: The above copyright notice and this permission notice shall be included in all copies or 
00009 substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00010 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00011 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00012 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 
00013 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
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;   // le point n'est pas dans l'ensemble de Mandelbrot
00059                 
00060                 tmp = tmp.square() + start;             // z^2+c        
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                                         // on a trouvé un point de l'ensemble de Mandelbrot
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         // à ce stade on a une heightmap vue depuis la caméra projetée le long de l'axe z vers les négatifs
00096 
00097         // on rebalance tout dans l'image
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                         // calcul de la normale au point considéré
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 }

Generated on Fri Feb 26 21:07:52 2010 for BuddhaBrot by  doxygen 1.4.6-NO