Allink
v0.1
|
00001 #include "../include/Matematica.h" 00002 00003 int Matematica::PermuteRandomAll(PERMUTE *Perm,int NMass){ 00004 int *Sequence = (int *)calloc(NMass,sizeof(int)); 00005 for(int n=0;n<NMass;n++){ 00006 int nSquare = n; 00007 int Ran = (int)(NMass*Casuale()); 00008 int IfGood=0; 00009 int IfAlready=1; 00010 for(int gg= nSquare-1;gg>=0;gg--){ 00011 //printf("%d %d) %d\n",nSquare,gg,Sequence[gg]); 00012 if(nSquare == Sequence[gg]){ 00013 IfAlready = 0; 00014 break; 00015 } 00016 } 00017 if(!IfAlready) continue; 00018 while(Ran==nSquare || !IfGood){// Ranw != Squarew && Ranh != Squareh){ 00019 IfGood = 1; 00020 Ran = (int)(NMass*Casuale()); 00021 for(int gg= nSquare-1;gg>=0;gg--){ 00022 //printf("%d %d) %d %d\n",nSquare,gg,Ran,Sequence[gg]); 00023 if(Ran == Sequence[gg]){ 00024 IfGood = 0; 00025 break; 00026 } 00027 } 00028 } 00029 Sequence[nSquare] = Ran; 00030 Sequence[Ran] = nSquare; 00031 // for(int g=0;g<NGrid*NGrid;g++)printf("%d %d\n",g,Sequence[g]); 00032 } 00033 for(int n=0;n<NMass;n++){ 00034 Perm[n].n = n; 00035 Perm[n].m = Sequence[n]; 00036 } 00037 int nTemp = 0; 00038 for(int n=0;n<NMass;n++){ 00039 if(Perm[n-nTemp].m < n ){ 00040 for(int nn=n-nTemp;nn<NMass-1-nTemp;nn++){ 00041 Perm[nn].n = nn+1+nTemp; 00042 Perm[nn].m = Sequence[nn+1+nTemp]; 00043 } 00044 nTemp++; 00045 } 00046 } 00047 //for(int n=0;n<NMass;n++)printf("%d %d - %d %d \n",Perm[n].n,Perm[n].m,n,Sequence[n]); 00048 return 1; 00049 } 00050 int Matematica::PermuteRandomAll(int *Sequence,int NMass){ 00051 for(int n=0;n<NMass;n++){ 00052 int nSquare = n; 00053 int Ran = (int)(NMass*Casuale()); 00054 int IfGood=0; 00055 int IfAlready=1; 00056 for(int gg= nSquare-1;gg>=0;gg--){ 00057 //printf("%d %d) %d\n",nSquare,gg,Sequence[gg]); 00058 if(nSquare == Sequence[gg]){ 00059 IfAlready = 0; 00060 break; 00061 } 00062 } 00063 if(!IfAlready) continue; 00064 while(Ran==nSquare || !IfGood){// Ranw != Squarew && Ranh != Squareh){ 00065 IfGood = 1; 00066 Ran = (int)(NMass*Casuale()); 00067 for(int gg= nSquare-1;gg>=0;gg--){ 00068 //printf("%d %d) %d %d\n",nSquare,gg,Ran,Sequence[gg]); 00069 if(Ran == Sequence[gg]){ 00070 IfGood = 0; 00071 break; 00072 } 00073 } 00074 } 00075 Sequence[nSquare] = Ran; 00076 Sequence[Ran] = nSquare; 00077 //if(Sequence[nSquare] < nSquare) Sequence[nSquare] = -1; 00078 // for(int g=0;g<NGrid*NGrid;g++)printf("%d %d\n",g,Sequence[g]); 00079 } 00080 return 1; 00081 } 00082 int Matematica::ApplyFilter(Matrice *Point,Matrice *Res,Matrice *Mask){ 00083 if(Point->Size() != Res->Size()){ 00084 printf("Matrices differ! %d %d \n",Point->Size(),Res->Size()); 00085 return 0; 00086 } 00087 int NMaskh = Mask->Size(); 00088 int NMaskw = Mask->Size(); 00089 int height = Point->Size(); 00090 int width = Point->Size(); 00091 int NHalf = Mask->Size()/2; 00092 for(int h=0;h<height; h++) { 00093 for(int w=0;w<width; w++) { 00094 //Res->setvalue(h,w,0.); 00095 for(int lh=0;lh<NMaskh;lh++){ 00096 for(int lw=0;lw<NMaskw;lw++){ 00097 if(h+lh-NHalf <0 ) continue; 00098 if(w+lw-NHalf <0 ) continue; 00099 if(h+lh-NHalf > height-1) continue; 00100 if(w+lw-NHalf > width-1) continue; 00101 Res->Add(h,w,Mask->Val(lh,lw)*Point->Val(h+lh-NHalf,w+lw-NHalf)); 00102 } 00103 } 00104 } 00105 } 00106 return 1; 00107 } 00108 int Matematica::ApplyFilter(Matrice *Res,Matrice *Mask){ 00109 int NMaskh = Mask->pNRow(); 00110 int NMaskw = Mask->pNCol(); 00111 // double Temp[NMaskh][NMaskw]; 00112 int width = Res->pNRow(); 00113 int height = Res->pNCol(); 00114 Matrice *Temp = new Matrice(width,height); 00115 Res->CopyOn(Temp); 00116 //int NHalf = Mask->Size()/2; 00117 int NHalf = (int)Mask->pNCol()/2; 00118 for(int h=0;h<height; h++) { 00119 for(int w=0;w<width; w++) { 00120 //Res->setvalue(h,w,0.); 00121 double dTemp=0.; 00122 for(int lh=0;lh<NMaskh;lh++){ 00123 int l1h = h + lh - NHalf; 00124 if(l1h >= height) continue;//g1x -= NGrid; 00125 if(l1h < 0) continue;//g1x + NGrid; 00126 for(int lw=0;lw<NMaskw;lw++){ 00127 int l1w = w + lw - NHalf; 00128 if(l1w >= width) continue;//g1x -= NGrid; 00129 if(l1w < 0) continue;//g1x + NGrid; 00130 dTemp += Mask->Val(lw,lh)*Temp->Val(l1w,l1h); 00131 } 00132 } 00133 Res->Set(w,h,dTemp); 00134 } 00135 } 00136 delete Temp; 00137 return 0; 00138 } 00139 int Matematica::Transform(int *Out,int *In,int NEdge,int operation){ 00140 int NMin = NEdge-1; 00141 if( OP_IF(operation,OP_INVERT) ){ 00142 for(int r=0;r<NEdge;r++) 00143 for(int c=0;c<NEdge;c++) 00144 Out[r*NEdge+c] = In[r*NEdge+c] == 1 ? 0 : 1; 00145 } 00146 if( OP_IF(operation,OP_ROT_90) ){ 00147 for(int r=0;r<NEdge;r++) 00148 for(int c=0;c<NEdge;c++) 00149 Out[r*NEdge+c] = In[(NMin-c)*NEdge+r]; 00150 } 00151 if( OP_IF(operation,OP_ROT_180) ){ 00152 for(int r=0;r<NEdge;r++) 00153 for(int c=0;c<NEdge;c++) 00154 Out[r*NEdge+c] = In[(NMin-r)*NEdge+(NMin-c)]; 00155 } 00156 if( OP_IF(operation,OP_ROT_270) ){ 00157 for(int r=0;r<NEdge;r++) 00158 for(int c=0;c<NEdge;c++) 00159 Out[r*NEdge+c] = In[c*NEdge+(NMin-r)]; 00160 } 00161 if( OP_IF(operation,OP_MIRROR) ){ 00162 for(int r=0;r<NEdge;r++) 00163 for(int c=0;c<NEdge;c++) 00164 Out[r*NEdge+c] = In[r*NEdge+(NMin-c)]; 00165 } 00166 if( OP_IF(operation,OP_TRANSPOSE) ){ 00167 for(int r=0;r<NEdge;r++) 00168 for(int c=0;c<NEdge;c++) 00169 Out[r*NEdge+c] = In[c*NEdge+r]; 00170 } 00171 return 1; 00172 } 00173 void Matematica::BackFold(Matrice *In,Matrice *Out,int NShift){ 00174 for(int r=0;r<In->pNRow();r++){ 00175 int r1 = r + NShift; 00176 if(r1 >= In->pNRow()) r1 -= In->pNRow(); 00177 if(r1 < 0) r1 += In->pNRow(); 00178 for(int c=0;c<In->pNCol();c++){ 00179 Out->Set(r1,c,In->Val(r,c)); 00180 } 00181 } 00182 }