//MAIN LOOP int main(){ matType matA,matB,matResult; addMatrices(matA,matB,&matResult); getch(); return 0; }
//Generates a full patch of value noise. Parameters can be set in constructor. vector<float> PerlinPatchGenerator::generatePatch(int x, int y) { vector<float> tempPatch; vector<float> heightMapPatch; int frequency; int gradientPoints; float amplitude; //Initiate a start heightMapPatch.assign(gridSize*gridSize,0); //Position based seed int n=x+y*57; n=(n<<13)^n; int seed=(n*(n*n*60493+19990303)+1376312589)&0x7fffffff; srand(seed); //Creates a height map for each frequency, and adds them together for(int n = 0; n <= NoF; n = n+1){ //Each frequency should be a multiple of the previous one frequency = pow(2,n); gradientPoints = frequency + 1; //Amplitude decreases as frequency increases. Higher frequencies should have smaller inpact on the heightmap amplitude = 1.0/(float)frequency; // Biotope = 2 gives the patch desert like shape if(biotope == 2){ amplitude *= amplitude; } amplitude *= amplitudeScale; //Calculate patch for that frequency tempPatch = createPatch(frequency,gradientPoints, amplitude); //Add the temporary patch together with previous patches heightMapPatch = addMatrices(heightMapPatch, tempPatch); } return heightMapPatch; }
vector<float> PerlinPatchGenerator::generatePatch(int x, int y, int size) { cout << "generatePatch begin\n"; vector<float> tempPatch; vector<float> heightMapPatch; int frequency; int gradientPoints; float amplitude; heightMapPatch.assign(size*size,0); for(int n = 0; n <= 7; n = n+1){ //max value on n: 2^n <= size frequency = pow(2,n); gradientPoints = frequency + 1; amplitude = 1.0/(float)frequency; tempPatch = createPatch(size,frequency,gradientPoints, amplitude); heightMapPatch = addMatrices(heightMapPatch, tempPatch, size); } //printMatrix(heightMapPatch,size); cout << "generatePatch end\n"; return heightMapPatch; }
void strassenMultMatrix(double **a,double **b,double **c,int size){ //Performs a Strassen matrix multiply operation //This does miracles, and is recursive //To perform a miracle, it first performs a miracle double **a11, **a22, **a12, **a21; double **b11, **b22, **b12, **b21; double **m1, **m2, **m3, **m4, **m5, **m6, **m7; double **t1, **t2, **t3, **t4, **t5, **t6, **t7, **t8, **t9, **t10; int newsize = (int)size/2; int i; if (size > threshold) { //Allocate memory....this could get expensive pretty quickly a11 = (double**) malloc(sizeof(double)*newsize); a12 = (double**) malloc(sizeof(double)*newsize); a21 = (double**) malloc(sizeof(double)*newsize); a22 = (double**) malloc(sizeof(double)*newsize); b11 = (double**) malloc(sizeof(double)*newsize); b12 = (double**) malloc(sizeof(double)*newsize); b21 = (double**) malloc(sizeof(double)*newsize); b22 = (double**) malloc(sizeof(double)*newsize); m1 = (double**) malloc(sizeof(double)*newsize); m2 = (double**) malloc(sizeof(double)*newsize); m3 = (double**) malloc(sizeof(double)*newsize); m4 = (double**) malloc(sizeof(double)*newsize); m5 = (double**) malloc(sizeof(double)*newsize); m6 = (double**) malloc(sizeof(double)*newsize); m7 = (double**) malloc(sizeof(double)*newsize); t1 = (double**) malloc(sizeof(double)*newsize); t2 = (double**) malloc(sizeof(double)*newsize); t3 = (double**) malloc(sizeof(double)*newsize); t4 = (double**) malloc(sizeof(double)*newsize); t5 = (double**) malloc(sizeof(double)*newsize); t6 = (double**) malloc(sizeof(double)*newsize); t7 = (double**) malloc(sizeof(double)*newsize); t8 = (double**) malloc(sizeof(double)*newsize); t9 = (double**) malloc(sizeof(double)*newsize); t10 = (double**) malloc(sizeof(double)*newsize); for (i=0; i < newsize; i++){ a11[i] = (double*) malloc(sizeof(double)*newsize); a12[i] = (double*) malloc(sizeof(double)*newsize); a21[i] = (double*) malloc(sizeof(double)*newsize); a22[i] = (double*) malloc(sizeof(double)*newsize); b11[i] = (double*) malloc(sizeof(double)*newsize); b12[i] = (double*) malloc(sizeof(double)*newsize); b21[i] = (double*) malloc(sizeof(double)*newsize); b22[i] = (double*) malloc(sizeof(double)*newsize); m1[i] = (double*) malloc(sizeof(double)*newsize); m2[i] = (double*) malloc(sizeof(double)*newsize); m3[i] = (double*) malloc(sizeof(double)*newsize); m4[i] = (double*) malloc(sizeof(double)*newsize); m5[i] = (double*) malloc(sizeof(double)*newsize); m6[i] = (double*) malloc(sizeof(double)*newsize); m7[i] = (double*) malloc(sizeof(double)*newsize); t1[i] = (double*) malloc(sizeof(double)*newsize); t2[i] = (double*) malloc(sizeof(double)*newsize); t3[i] = (double*) malloc(sizeof(double)*newsize); t4[i] = (double*) malloc(sizeof(double)*newsize); t5[i] = (double*) malloc(sizeof(double)*newsize); t6[i] = (double*) malloc(sizeof(double)*newsize); t7[i] = (double*) malloc(sizeof(double)*newsize); t8[i] = (double*) malloc(sizeof(double)*newsize); t9[i] = (double*) malloc(sizeof(double)*newsize); t10[i] = (double*) malloc(sizeof(double)*newsize); } splitMatrix(a,a11,a12,a21,a22,size); splitMatrix(b,b11,b12,b21,b22,size); addMatrices(a11,a22,t1,newsize); addMatrices(a21,a22,t2,newsize); addMatrices(a11,a12,t3,newsize); subMatrices(a21,a11,t4,newsize); subMatrices(a12,a22,t5,newsize); addMatrices(b11,b22,t6,newsize); subMatrices(b12,b22,t7,newsize); subMatrices(b21,b11,t8,newsize); addMatrices(b11,b12,t9,newsize); addMatrices(b21,b22,t10,newsize); strassenMultMatrix(t1,t6,m1,newsize); strassenMultMatrix(t2,b11,m2,newsize); strassenMultMatrix(a11,t7,m3,newsize); strassenMultMatrix(a22,t8,m4,newsize); strassenMultMatrix(t3,b22,m5,newsize); strassenMultMatrix(t4,t9,m6,newsize); strassenMultMatrix(t5,t10,m7,newsize); addMatrices(m1,m4,a11,newsize); subMatrices(m5,m7,a12,newsize); addMatrices(m3,m1,a21,newsize); subMatrices(m2,m6,a22,newsize); subMatrices(a11,a12,b11,newsize); addMatrices(m3,m5,b12,newsize); addMatrices(m2,m4,b21,newsize); subMatrices(a21,a22,b22,newsize); catMatrix(c,b11,b12,b21,b22,size); free(a11);free(a12);free(a21);free(a22); free(b11);free(b12);free(b21);free(b22); free(t1);free(t2);free(t3);free(t4);free(t5);free(t6);free(t7);free(t8);free(t9);free(t10); free(m1);free(m2);free(m3);free(m4);free(m5);free(m6);free(m7); } else { normalMultMatrix(b,a,c,size); } }