void camellia128_init(const void* key, camellia128_ctx_t* s){ uint8_t i; s->kll = 0; /* ((uint64_t*)key)[0]; */ /* load the key, endian-adjusted, to kll,klr */ for(i=0; i<8; ++i){ s->kll <<= 8; s->kll |= *((uint8_t*)key); key = (uint8_t*)key+1; } for(i=0; i<8; ++i){ s->klr <<= 8; s->klr |= *((uint8_t*)key); key = (uint8_t*)key+1; } s->kal = s->kll; s->kar = s->klr; s->kar ^= camellia_f(s->kal, SIGMA(0)); s->kal ^= camellia_f(s->kar, SIGMA(1)); s->kal ^= s->kll; s->kar ^= s->klr; s->kar ^= camellia_f(s->kal, SIGMA(2)); s->kal ^= camellia_f(s->kar, SIGMA(3)); }
void setOrdinaryMaterial(int m){ CA(m) = (1 - SIGMA(m)*deltaT/(2*EPSR(m)*EPSNOT )) / ( 1 + SIGMA(m)*deltaT /(2*EPSR(m)*EPSNOT)); CB(m) = (deltaT/(EPSR(m)*EPSNOT*DELTA)) / ( 1 + SIGMA(m) * deltaT /(2*EPSR(m)*EPSNOT)); DA(m) = (1 - SIGMA(m)*deltaT/(2*MUR(m)*MUNOT)) / ( 1 + SIGMA(m) * deltaT /(2*MUR(m)*MUNOT)); DB(m) = (deltaT/(MUR(m)*MUNOT*DELTA)) / ( 1 + SIGMA(m)*deltaT /(2*MUR(m)*MUNOT)); } // end setOrdinaryMaterial
int main() { // LOAD DATA arma::mat X; // A) Toy Data // char inputFile[] = "../data_files/toyclusters/toyclusters.dat"; // B) X4.dat //char inputFile[] = "./X4.dat"; // C) fisher data //char inputFile[] = "./fisher.dat"; // D) MNIST data //char inputFile[] = "../data_files/MNIST/MNIST.dat"; // E) Reduced MNIST (5000x400) //char inputFile[] = "../data_files/MNIST/MNISTreduced.dat"; // F) Reduced MNIST (0 and 1) (1000x400) //char inputFile[] = "../data_files/MNIST/MNISTlittle.dat"; // G) Girl.png (512x768, RGB, already unrolled) //char inputFile[] = "girl.dat"; // H) Pool.png (383x512, RGB, already unrolled) //char inputFile[] = "pool.dat"; // I) Cat.png (733x490, RGB, unrolled) //char inputFile[] = "cat.dat"; // J) Airplane.png (512x512, RGB, unrolled) //char inputFile[] = "airplane.dat"; // K) Monarch.png (512x768, RGB, unrolled) //char inputFile[] = "monarch.dat"; // L) tulips.png (512x768 ,RGB, unrolled) //char inputFile[] = "tulips.dat"; // M) demo.dat (2d data) char inputFile[] = "demo.dat"; // INITIALIZE PARAMETERS X.load(inputFile); const arma::uword N = X.n_rows; const arma::uword D = X.n_cols; arma::umat ids(N,1); // needed to shuffle indices later arma::umat shuffled_ids(N,1); for (arma::uword i = 0; i < N; ++i) { ids(i,0) = i; } arma::arma_rng::set_seed_random(); // set arma rng // int seed = time(NULL); // set RNG seed to current time // srand(seed); arma::uword initial_K = 32; // initial number of clusters arma::uword K = initial_K; arma::umat clusters(N,1); // contains cluster assignments for each data point for (arma::uword i = 0; i < N; ++i) { clusters(i, 0) = i%K; // initialize as [0,1,...,K-1,0,1,...,K-1,0,...] } arma::umat cluster_sizes(N,1,arma::fill::zeros); // contains num data points in cluster k for (arma::uword i = 0; i < N; ++i) { cluster_sizes(clusters(i,0), 0) += 1; } arma::mat mu(N, D, arma::fill::zeros); // contains cluster mean parameters arma::mat filler(D,D,arma::fill::eye); std::vector<arma::mat> sigma(N,filler); // contains cluster covariance parameters if (K < N) { // set parameters not belonging to any cluster to -999 mu.rows(K,N-1).fill(-999); for (arma::uword k = K; k < N; ++k) { sigma[k].fill(-999); } } arma::umat uword_dummy(1,1); // dummy 1x1 matrix; // for (arma::uword i = 0; i <N; ++i) { // std::cout << sigma[i] << std::endl; // } // std::cout << X << std::endl // << N << std::endl // << D << std::endl // << K << std::endl // << clusters << std::endl // << cluster_sizes << std::endl // << ids << std::endl; // INITIALIZE HYPER PARAMETERS // Dirichlet Process concentration parameter is alpha: double alpha = 1; // Dirichlet Process base distribution (i.e. prior) is // H(mu,sigma) = NIW(mu,Sigma|m_0,k_0,S_0,nu_0) = N(mu|m_0,Sigma/k_0)IW(Sigma|S_0,nu_0) arma::mat perturbation(D,D,arma::fill::eye); perturbation *= 0.000001; //const arma::mat S_0 = arma::cov(X,X,1) + perturbation; // S_xbar / N const arma::mat S_0(D,D,arma::fill::eye); const double nu_0 = D + 2; const arma::mat m_0 = mean(X).t(); const double k_0 = 0.01; // std::cout << "S_0" << S_0 << std::endl; // std::cout << S_0 << std::endl // << nu_0 << std::endl // << m_0 << std::endl // << k_0 << std::endl; // INITIALIZE SAMPLING PARAMETERS arma::uword NUM_SWEEPS = 250; // number of Gibbs iterations bool SAVE_CHAIN = false; // save output of each Gibbs iteration? // arma::uword BURN_IN = NUM_SWEEPS - 10; // arma::uword CHAINSIZE = NUM_SWEEPS - BURN_IN; // std::vector<arma::uword> chain_K(CHAINSIZE, K); // Initialize chain variable to initial parameters for convinience // std::vector<arma::umat> chain_clusters(CHAINSIZE, clusters); // std::vector<arma::umat> chain_clusterSizes(CHAINSIZE, cluster_sizes); // std::vector<arma::mat> chain_mu(CHAINSIZE, mu); // std::vector<std::vector<arma::mat> > chain_sigma(CHAINSIZE, sigma); // for (arma::uword sweep = 0; sweep < CHAINSIZE; ++sweep) { // std::cout << sweep << " K\n" << chain_K[sweep] << std::endl // << sweep << " clusters\n" << chain_clusters[sweep] << std::endl // << sweep << " sizes\n" << chain_clusterSizes[sweep] << std::endl // << sweep << " mu\n" << chain_mu[sweep] << std::endl; // for (arma::uword i = 0; i < N; ++i) { // std::cout << sweep << " " << i << " sigma\n" << chain_sigma[sweep][i] << std::endl; // } // } // START CHAIN std::cout << "Starting Algorithm with K = " << K << std::endl; for (arma::uword sweep = 0; sweep < NUM_SWEEPS; ++sweep) { // shuffle indices shuffled_ids = shuffle(ids); // std::cout << shuffled_ids << std::endl; // SAMPLE CLUSTERS for (arma::uword j = 0; j < N; ++j){ // std::cout << "j = " << j << std::endl; arma::uword i = shuffled_ids(j); arma::mat x = X.row(i).t(); // current data point // Remove i's statistics and any empty clusters arma::uword c = clusters(i,0); // current cluster cluster_sizes(c,0) -= 1; //std::cout << "old c = " << c << std::endl; if (cluster_sizes(c,0) == 0) { // remove empty cluster cluster_sizes(c,0) = cluster_sizes(K-1,0); // move entries for K onto position c mu.row(c) = mu.row(K-1); sigma[c] = sigma[K-1]; uword_dummy(0,0) = c; arma::uvec idx = find(clusters == K - 1); clusters.each_row(idx) = uword_dummy; cluster_sizes(K-1,0) = 0; mu.row(K-1).fill(-999); sigma[K-1].fill(-999); --K; } // quick test of logMvnPdf: // arma::mat m_(2,1); // arma::mat s_(2,2); // arma::mat t_(2,1); // m_ << 1 << arma::endr << 2; // s_ << 3 << -0.2 << arma::endr << -0.2 << 1; // t_ << -3 << arma::endr << -3; // double lpdf = logMvnPdf(t_, m_, s_); // std::cout << lpdf << std::endl; // śhould be -19.1034 (works) // Find categorical distribution over clusters (tested) arma::mat logP(K+1, 1, arma::fill::zeros); // quick test of logInvWishPdf // arma::mat si_(2,2), s_(2,2); // double nu_ = 4; // si_ << 1 << 0.5 << arma::endr << 0.5 << 4; // s_ << 3 << -0.2 << arma::endr << -0.2 << 1; // double lpdf = logInvWishPdf(si_, s_, nu_); // std::cout << lpdf << std::endl; // should be -7.4399 (it is) // quick test for logNormInvWishPdf // arma::mat si_(2,2), s_(2,2); // arma :: mat mu_(2,1), m_(2,1); // double k_ = 0.5, nu_ = 4; // si_ << 1 << 0.5 << arma::endr << 0.5 << 4; // s_ << 3 << -0.2 << arma::endr << -0.2 << 1; // mu_ << -3 << arma::endr << -3; // m_ << 1 << arma::endr << 2; // double lp = logNormInvWishPdf(mu_,si_,m_,k_,s_,nu_); // std::cout << lp << std::endl; // should equal -15.2318 (it is) // p(existing clusters) (tested) for (arma::uword k = 0; k < K; ++k) { arma::mat m_ = mu.row(k).t(); arma::mat s_ = sigma[k]; logP(k,0) = log(cluster_sizes(k,0)) - log(N-1+alpha) + logMvnPdf(x,m_,s_); } // p(new cluster): find partition function (tested) // arma::mat dummy_mu(D, 1, arma::fill::zeros); // arma::mat dummy_sigma(D, D, arma::fill::eye); // double logPrior, logLklihd, logPstr, logPartition; // posterior hyperparameters (tested) arma::mat m_1(D,1), S_1(D,D); double k_1, nu_1; k_1 = k_0 + 1; nu_1 = nu_0 + 1; m_1 = (k_0*m_0 + x) / k_1; S_1 = S_0 + x * x.t() + k_0 * (m_0 * m_0.t()) - k_1 * (m_1 * m_1.t()); // std::cout << k_1 << std::endl // << nu_1 << std::endl // << m_1 << std::endl // << S_1 << std::endl; // // partition = likelihood*prior/posterior (tested) // // (perhaps a direct computation of the partition function would be better) // logPrior = logNormInvWishPdf(dummy_mu, dummy_sigma, m_0, k_0, S_0, nu_0); // logLklihd = logMvnPdf(x, dummy_mu, dummy_sigma); // logPstr = logNormInvWishPdf(dummy_mu, dummy_sigma, m_1, k_1, S_1, nu_1); // logPartition = logPrior + logLklihd - logPstr; // std::cout << "log Prior = " << logPrior << std::endl // << "log Likelihood = " << logLklihd << std::endl // << "log Posterior = " << logPstr << std::endl // << "log Partition = " << logPartition << std::endl; // Computing partition directly double logS0,signS0,logS1,signS1; arma::log_det(logS0,signS0,S_0); arma::log_det(logS1,signS1,S_1); /*std::cout << "log(det(S_0)) = " << logS0 << std::endl << "log(det(S_1)) = " << logS1 << std::endl;*/ double term1 = 0.5*D*(log(k_0)-log(k_1)); double term2 = -0.5*D*log(arma::datum::pi); double term3 = 0.5*(nu_0*logS0 - nu_1*logS1); double term4 = lgamma(0.5*nu_1); double term5 = -lgamma(0.5*(nu_1-D)); double logPartition = term1+term2+term3+term4+term5; /*double logPartition = 0.5*D*(log(k_0)-log(k_1)-log(arma::datum::pi)) \ /+0.5*(nu_0*logS0 - nu_1*logS1) + lgamma(0.5*nu_1) - lgamma(0.5*(nu_1-D));*/ /* std::cout << "term1 = " << term1 << std::endl << "term2 = " << term2 << std::endl << "term3 = " << term3 << std::endl << "term4 = " << term4 << std::endl << "term5 = " << term5 << std::endl;*/ //std::cout << "logP = " << logPartition << std::endl; // p(new cluster): (tested) logP(K,0) = log(alpha) - log(N - 1 + alpha) + logPartition; // std::cout << "logP(new cluster) = " << logP(K,0) << std::endl; //if(i == 49) //assert(false); // sample cluster for i arma::uword c_ = logCatRnd(logP); clusters(i,0) = c_; //if (j % 10 == 0){ //std::cout << "New c = " << c_ << std::endl; //std::cout << "logP = \n" << logP << std::endl; //} // quick test for mvnRnd // arma::mat mu, si; // mu << 1 << arma::endr << 2; // si << 1 << 0.9 << arma::endr << 0.9 << 1; // arma::mat m = mvnRnd(mu, si); // std::cout << m << std::endl; // quick test for invWishRnd // double df = 4; // arma::mat si(2,2); // si << 1 << 1 << arma::endr << 1 << 1; // arma::mat S = invWishRnd(si,df); // std::cout << S << std::endl; if (c_ == K) { // Sample parameters for any new-born clusters from posterior cluster_sizes(K, 0) = 1; arma::mat si_ = invWishRnd(S_1, nu_1); //arma::mat si_ = S_1; arma::mat mu_ = mvnRnd(m_1, si_/k_1); //arma::mat mu_ = m_1; mu.row(K) = mu_.t(); sigma[K] = si_; K += 1; } else { cluster_sizes(c_,0) += 1; } // if (sweep == 0) // std::cout << " K = " << K << std::endl; // // if (j == N-1) { // // std::cout << logP << std::endl; // // std::cout << K << std::endl; // // assert(false); // // } // std::cout << "K = " << K << "\n" << std::endl; } // sample CLUSTER PARAMETERS FROM POSTERIOR for (arma::uword k = 0; k < K; ++k) { // std::cout << "k = " << k << std::endl; // cluster data arma::mat Xk = X.rows(find(clusters == k)); arma::uword Nk = cluster_sizes(k,0); // posterior hyperparameters arma::mat m_Nk(D,1), S_Nk(D,D); double k_Nk, nu_Nk; arma::mat sum_k = sum(Xk,0).t(); arma::mat cov_k(D, D, arma::fill::zeros); for (arma::uword l = 0; l < Nk; ++l) { cov_k += Xk.row(l).t() * Xk.row(l); } k_Nk = k_0 + Nk; nu_Nk = nu_0 + Nk; m_Nk = (k_0 * m_0 + sum_k) / k_Nk; S_Nk = S_0 + cov_k + k_0 * (m_0 * m_0.t()) - k_Nk * (m_Nk * m_Nk.t()); // sample fresh parameters arma::mat si_ = invWishRnd(S_Nk, nu_Nk); //arma::mat si_ = S_Nk; arma::mat mu_ = mvnRnd(m_Nk, si_/k_Nk); //arma::mat mu_ = m_Nk; mu.row(k) = mu_.t(); sigma[k] = si_; } std::cout << "Iteration " << sweep + 1 << "/" << NUM_SWEEPS<< " done. K = " << K << std::endl; // // STORE CHAIN // if (SAVE_CHAIN) { // if (sweep >= BURN_IN) { // chain_K[sweep - BURN_IN] = K; // chain_clusters[sweep - BURN_IN] = clusters; // chain_clusterSizes[sweep - BURN_IN] = cluster_sizes; // chain_mu[sweep - BURN_IN] = mu; // chain_sigma[sweep - BURN_IN] = sigma; // } // } } std::cout << "Final cluster sizes: " << std::endl << cluster_sizes.rows(0, K-1) << std::endl; // WRITE OUPUT DATA TO FILE arma::mat MU = mu.rows(0,K-1); arma::mat SIGMA(D*K,D); for (arma::uword k = 0; k < K; ++k) { SIGMA.rows(k*D,(k+1)*D-1) = sigma[k]; } arma::umat IDX = clusters; // A) toycluster data // char MuFile[] = "../data_files/toyclusters/dpmMU.out"; // char SigmaFile[] = "../data_files/toyclusters/dpmSIGMA.out"; // char IdxFile[] = "../data_files/toyclusters/dpmIDX.out"; // B) X4.dat char MuFile[] = "dpmMU.out"; char SigmaFile[] = "dpmSIGMA.out"; char IdxFile[] = "dpmIDX.out"; std::ofstream KFile("K.out"); MU.save(MuFile, arma::raw_ascii); SIGMA.save(SigmaFile, arma::raw_ascii); IDX.save(IdxFile, arma::raw_ascii); KFile << "K = " << K << std::endl; if (SAVE_CHAIN) {} // std::ofstream chainKFile("chainK.out"); // std::ofstream chainClustersFile("chainClusters.out"); // std::ofstream chainClusterSizesFile("chainClusterSizes.out"); // std::ofstream chainMuFile("chainMu.out"); // std::ofstream chainSigmaFile("chainSigma.out"); // chainKFile << "Dirichlet Process Mixture Model.\nInput: " << inputFile << std::endl // << "Number of iterations of Gibbs Sampler: " << NUM_SWEEPS << std::endl // << "Burn-In: " << BURN_IN << std::endl // << "Initial number of clusters: " << initial_K << std::endl // << "Output: Number of cluster (K)\n" << std::endl; // chainClustersFile << "Dirichlet Process Mixture Model.\nInput: " << inputFile << std::endl // << "Number of iterations of Gibbs Sampler: " << NUM_SWEEPS << std::endl // << "Burn-In: " << BURN_IN << std::endl // << "Initial number of clusters: " << initial_K << std::endl // << "Output: Cluster identities (clusters)\n" << std::endl; // chainClusterSizesFile << "Dirichlet Process Mixture Model.\nInput: " << inputFile << std::endl // << "Number of iterations of Gibbs Sampler: " << NUM_SWEEPS << std::endl // << "Burn-In: " << BURN_IN << std::endl // << "Initial number of clusters: " << initial_K << std::endl // << "Output: Size of clusters (cluster_sizes)\n" << std::endl; // chainMuFile << "Dirichlet Process Mixture Model.\nInput: " << inputFile << std::endl // << "Number of iterations of Gibbs Sampler: " << NUM_SWEEPS << std::endl // << "Burn-In: " << BURN_IN << std::endl // << "Initial number of clusters: " << initial_K << std::endl // << "Output: Samples for cluster mean parameters (mu. Note: means stored in rows)\n" << std::endl; // chainSigmaFile << "Dirichlet Process Mixture Model.\nInput: " << inputFile << std::endl // << "Number of iterations of Gibbs Sampler: " << NUM_SWEEPS << std::endl // << "Burn-In " << BURN_IN << std::endl // << "Initial number of clusters: " << initial_K << std::endl // << "Output: Samples for cluster covariances (sigma)\n" << std::endl; // for (arma::uword sweep = 0; sweep < CHAINSIZE; ++sweep) { // arma::uword K = chain_K[sweep]; // chainKFile << "Sweep #" << BURN_IN + sweep + 1 << "\n" << chain_K[sweep] << std::endl; // chainClustersFile << "Sweep #" << BURN_IN + sweep + 1 << "\n" << chain_clusters[sweep] << std::endl; // chainClusterSizesFile << "Sweep #" << BURN_IN + sweep + 1 << "\n" << chain_clusterSizes[sweep].rows(0, K - 1) << std::endl; // chainMuFile << "Sweep #" << BURN_IN + sweep + 1 << "\n" << chain_mu[sweep].rows(0, K - 1) << std::endl; // chainSigmaFile << "Sweep #" << BURN_IN + sweep + 1<< "\n"; // for (arma::uword i = 0; i < K; ++i) { // chainSigmaFile << chain_sigma[sweep][i] << std::endl; // } // chainSigmaFile << std::endl; // } // } return 0; }
void gmm_fisher_save_soft_assgn(int n, const float *v, const gmm_t * g, int flags, float *dp_dlambda, float *word_total_soft_assignment) { long d=g->d, k=g->k; float *p = fvec_new(n * k); long i,j,l; long ii=0; float * vp = NULL; /* v*p */ float * sum_pj = NULL; /* sum of p's for a given j */ gmm_compute_p(n,v,g,p,flags | GMM_FLAGS_W); #define P(j,i) p[(i)*k+(j)] #define V(l,i) v[(i)*d+(l)] #define MU(l,j) g->mu[(j)*d+(l)] #define SIGMA(l,j) g->sigma[(j)*d+(l)] #define VP(l,j) vp[(j)*d+(l)] // Save total soft assignment per centroid if (word_total_soft_assignment != NULL) { for (j=0; j<k; j++) { double sum=0; for (i=0; i<n; i++) { sum += P(j,i); } if (n != 0) { word_total_soft_assignment[j] = (float)(sum/n); } else { word_total_soft_assignment[j] = 0.0; } } } if(flags & GMM_FLAGS_W) { for(j=1; j<k; j++) { double accu=0; for(i=0; i<n; i++) accu+= P(j,i)/g->w[j] - P(0,i)/g->w[0]; /* normalization */ double f=n*(1/g->w[j]+1/g->w[0]); dp_dlambda[ii++]=accu/sqrt(f); } } if(flags & GMM_FLAGS_MU) { float *dp_dmu=dp_dlambda+ii; #define DP_DMU(l,j) dp_dmu[(j)*d+(l)] if(0) { /* simple and slow */ for(j=0; j<k; j++) { for(l=0; l<d; l++) { double accu=0; for(i=0; i<n; i++) accu += P(j,i) * (V(l,i)-MU(l,j)) / SIGMA(l,j); DP_DMU(l,j)=accu; } } } else { /* complicated and fast */ /* precompute tables that may be useful for sigma too */ vp = fvec_new(k * d); fmat_mul_tr(v,p,d,k,n,vp); sum_pj = fvec_new(k); for(j=0; j<k; j++) { double sum=0; for(i=0; i<n; i++) sum += P(j,i); sum_pj[j] = sum; } for(j=0; j<k; j++) { for(l=0; l<d; l++) DP_DMU(l,j) = (VP(l,j) - MU(l,j) * sum_pj[j]) / SIGMA(l,j); } } /* normalization */ if(!(flags & GMM_FLAGS_NO_NORM)) { for(j=0; j<k; j++) for(l=0; l<d; l++) { float nf = sqrt(n*g->w[j]/SIGMA(l,j)); if(nf > 0) DP_DMU(l,j) /= nf; } } #undef DP_DMU ii+=d*k; } if(flags & (GMM_FLAGS_SIGMA | GMM_FLAGS_1SIGMA)) { if(flags & GMM_FLAGS_1SIGMA) { /* fast not implemented for 1 sigma */ for(j=0; j<k; j++) { double accu2=0; for(l=0; l<d; l++) { double accu=0; for(i=0; i<n; i++) accu += P(j,i) * (sqr(V(l,i)-MU(l,j)) / SIGMA(l,j) - 1) / sqrt(SIGMA(l,j)); if(flags & GMM_FLAGS_SIGMA) { double f=flags & GMM_FLAGS_NO_NORM ? 1.0 : 2*n*g->w[j]/SIGMA(l,j); dp_dlambda[ii++]=accu/sqrt(f); } accu2+=accu; } if(flags & GMM_FLAGS_1SIGMA) { double f=flags & GMM_FLAGS_NO_NORM ? 1.0 : 2*d*n*g->w[j]/SIGMA(0,j); dp_dlambda[ii++]=accu2/sqrt(f); } } } else { /* fast and complicated */ assert(flags & GMM_FLAGS_SIGMA); float *dp_dsigma = dp_dlambda + ii; if(!vp) { vp = fvec_new(k * d); fmat_mul_tr(v,p,d,k,n,vp); } if(!sum_pj) { sum_pj = fvec_new(k); for(j=0; j<k; j++) { double sum=0; for(i=0; i<n; i++) sum += P(j,i); sum_pj[j] = sum; } } float *v2 = fvec_new(n * d); for(i = n*d-1 ; i >= 0; i--) v2[i] = v[i] * v[i]; float *v2p = fvec_new(k * d); fmat_mul_tr(v2,p,d,k,n,v2p); free(v2); #define V2P(l,j) v2p[(j)*d+(l)] #define DP_DSIGMA(i,j) dp_dsigma[(i)+(j)*d] for(j=0; j<k; j++) { for(l=0; l<d; l++) { double accu; accu = V2P(l, j); accu += VP(l, j) * (- 2 * MU(l,j)); accu += sum_pj[j] * (sqr(MU(l,j)) - SIGMA(l,j)); /* normalization */ double f; if(flags & GMM_FLAGS_NO_NORM) { f = pow(SIGMA(l,j), -1.5); } else { f = 1 / (SIGMA(l,j) * sqrt(2*n*g->w[j])); } DP_DSIGMA(l,j) = accu * f; } } free(v2p); #undef DP_DSIGMA #undef V2P ii += d * k; } } assert(ii==gmm_fisher_sizeof(g,flags)); #undef P #undef V #undef MU #undef SIGMA free(p); free(sum_pj); free(vp); }
// draws cells void drawCells(float xrot, float yrot, int64_t ***lattice, cell_info_t *cells) { int i, j, x, y, z, sigma; for(i = 1; i <= numCells; i++) { sigma = i; DEBUG(printf("\t\tprinting cell sig %i with volume %i, membrane: %i, type: %i.\n", i, cells[i].volume, cells[i].membraneArea, cells[i].type);) for(j = 0; j < cells[i].volume; j++) { x = cells[i].subcells[j].x; y = cells[i].subcells[j].y; z = cells[i].subcells[j].z; glLoadIdentity(); // Reset the model-view matrix glTranslatef((x - (MODEL_SIZE_X/2)) * translationX, (y - (MODEL_SIZE_Y/2)) * translationY, (z - (MODEL_SIZE_Z/2)) * translationZ); glBegin(GL_QUADS); // set collor accoring to subcell type switch(cells[i].type){ case MEDIUM: return; break; case VASCULAR: glColor4f(1.0f, 0.0f, 0.0f, 0.4f); break; case TUMOR_NORM: glColor4f(0.0f, 1.0f, 0.0f, 0.4f); break; case TUMOR_NECROSIS: glColor4f(0.0f, 0.0f, 1.0f, 0.4f); break; case TUMOR_STEM: glColor4f(0.2f, 0.6f, 0.8f, 0.4f); break; default: glColor4f(1.0f, 1.0f, 1.0f, 0.4f); } // Top face if(y >= MODEL_SIZE_Y - 1 || SIGMA(lattice[x][y + 1][z]) != sigma) { glVertex3f( viewScaleX, viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, viewScaleY, viewScaleZ); glVertex3f( viewScaleX, viewScaleY, viewScaleZ); } // Bottom face if(y == 0 || SIGMA(lattice[x][y - 1][z]) != sigma) { glVertex3f( viewScaleX, -viewScaleY, viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, -viewScaleZ); glVertex3f( viewScaleX, -viewScaleY, -viewScaleZ); } // Front face if(z >= MODEL_SIZE_Z - 1 || SIGMA(lattice[x][y][z + 1]) != sigma) { glVertex3f( viewScaleX, viewScaleY, viewScaleZ); glVertex3f(-viewScaleX, viewScaleY, viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, viewScaleZ); glVertex3f( viewScaleX, -viewScaleY, viewScaleZ); } // Back face if(z == 0 || SIGMA(lattice[x][y][z - 1]) != sigma) { glVertex3f( viewScaleX, -viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, viewScaleY, -viewScaleZ); glVertex3f( viewScaleX, viewScaleY, -viewScaleZ); } // Left face if(x == 0 || SIGMA(lattice[x - 1][y][z]) != sigma) { glVertex3f(-viewScaleX, viewScaleY, viewScaleZ); glVertex3f(-viewScaleX, viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, -viewScaleZ); glVertex3f(-viewScaleX, -viewScaleY, viewScaleZ); } // Right face if(x >= MODEL_SIZE_X - 1 || SIGMA(lattice[x + 1][y][z]) != sigma) { glVertex3f(viewScaleX, viewScaleY, -viewScaleZ); glVertex3f(viewScaleX, viewScaleY, viewScaleZ); glVertex3f(viewScaleX, -viewScaleY, viewScaleZ); glVertex3f(viewScaleX, -viewScaleY, -viewScaleZ); } glEnd(); } }