2015-06-03 17:55:25 -04:00
|
|
|
// Compute the signed distance from a digitized image
|
|
|
|
|
// Two phases are present
|
|
|
|
|
// Phase 1 has value -1
|
|
|
|
|
// Phase 2 has value 1
|
|
|
|
|
// this code uses the segmented image to generate the signed distance
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2015-08-21 16:56:43 -04:00
|
|
|
#include "common/Array.h"
|
|
|
|
|
#include "common/Domain.h"
|
2015-06-03 17:55:25 -04:00
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
// Initialize MPI
|
|
|
|
|
int rank, nprocs;
|
|
|
|
|
MPI_Init(&argc,&argv);
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Comm comm = MPI_COMM_WORLD;
|
|
|
|
|
MPI_Comm_rank(comm,&rank);
|
|
|
|
|
MPI_Comm_size(comm,&nprocs);
|
2015-06-03 17:55:25 -04:00
|
|
|
|
|
|
|
|
int i,j,k,n,nn;
|
2015-06-03 21:11:50 -04:00
|
|
|
int iproc,jproc,kproc;
|
2015-06-03 17:55:25 -04:00
|
|
|
int nx,ny,nz;
|
|
|
|
|
int Nx, Ny, Nz, N;
|
2015-06-03 21:11:50 -04:00
|
|
|
int nprocx, nprocy, nprocz, nspheres;
|
|
|
|
|
double Lx, Ly, Lz;
|
2015-06-03 17:55:25 -04:00
|
|
|
Nx = Ny = Nz = 50;
|
2015-06-03 21:50:24 -04:00
|
|
|
nx = ny = nz = 50;
|
2015-06-03 17:55:25 -04:00
|
|
|
N = Nx*Ny*Nz;
|
2015-06-03 21:50:24 -04:00
|
|
|
nprocx=nprocy=nprocz=2;
|
|
|
|
|
Lx = Ly = Lz = 1.0;
|
|
|
|
|
int BC=0;
|
|
|
|
|
|
2015-06-03 17:55:25 -04:00
|
|
|
if (nprocs != 8){
|
|
|
|
|
ERROR("TestSegDist: Number of MPI processes must be equal to 8");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nprocx !=2 || nprocz !=2 || nprocy !=2 ){
|
|
|
|
|
ERROR("TestSegDist: MPI process grid must be 2x2x2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the rank info
|
|
|
|
|
Domain Dm(nx,ny,nz,rank,nprocx,nprocy,nprocz,Lx,Ly,Lz,BC);
|
2015-06-09 21:57:35 -04:00
|
|
|
for (k=0;k<nz;k++){
|
|
|
|
|
for (j=0;j<ny;j++){
|
|
|
|
|
for (i=0;i<nx;i++){
|
|
|
|
|
n = k*nx*ny+j*nx+i;
|
|
|
|
|
Dm.id[n] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-04 13:06:36 -04:00
|
|
|
Dm.CommInit(comm);
|
2015-06-04 06:58:28 -04:00
|
|
|
|
2015-06-03 17:55:25 -04:00
|
|
|
nx+=2; ny+=2; nz+=2;
|
2015-06-04 08:15:34 -04:00
|
|
|
N = nx*ny*nz;
|
2015-06-03 17:55:25 -04:00
|
|
|
int count = 0;
|
2015-06-04 07:42:08 -04:00
|
|
|
|
2015-06-03 17:55:25 -04:00
|
|
|
char *id;
|
|
|
|
|
id = new char [N];
|
2015-06-04 08:28:26 -04:00
|
|
|
double BubbleRadius = 25;
|
2015-06-03 17:55:25 -04:00
|
|
|
// Initialize the bubble
|
|
|
|
|
int x,y,z;
|
|
|
|
|
for (k=1;k<nz-1;k++){
|
|
|
|
|
for (j=1;j<ny-1;j++){
|
|
|
|
|
for (i=1;i<nx-1;i++){
|
2015-06-04 07:47:11 -04:00
|
|
|
x = (nx-2)*Dm.iproc+i;
|
|
|
|
|
y = (ny-2)*Dm.jproc+j;
|
|
|
|
|
z = (nz-2)*Dm.kproc+k;
|
2015-06-04 08:11:46 -04:00
|
|
|
n = k*nx*ny+j*nx+i;
|
2015-06-03 17:55:25 -04:00
|
|
|
|
|
|
|
|
// Initialize phase positions
|
|
|
|
|
if ((x-nx+1)*(x-nx+1)+(y-ny+1)*(y-ny+1)+(z-nz+1)*(z-nz+1) < BubbleRadius*BubbleRadius){
|
|
|
|
|
id[n] = 0;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
id[n]=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 07:49:03 -04:00
|
|
|
DoubleArray Distance(nx,ny,nz);
|
2015-06-03 17:55:25 -04:00
|
|
|
// Initialize the signed distance function
|
2015-06-04 08:28:26 -04:00
|
|
|
for (k=0;k<nz;k++){
|
|
|
|
|
for (j=0;j<ny;j++){
|
|
|
|
|
for (i=0;i<nx;i++){
|
|
|
|
|
n=k*nx*ny+j*nx+i;
|
2015-06-03 17:55:25 -04:00
|
|
|
// Initialize distance to +/- 1
|
|
|
|
|
Distance(i,j,k) = 2.0*id[n]-1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 07:42:08 -04:00
|
|
|
if (rank==0) printf("Nx = %i \n",(int)Distance.size(0));
|
|
|
|
|
if (rank==0) printf("Ny = %i \n",(int)Distance.size(1));
|
|
|
|
|
if (rank==0) printf("Nz = %i \n",(int)Distance.size(2));
|
|
|
|
|
|
2015-06-03 17:55:25 -04:00
|
|
|
printf("Initialized! Converting to Signed Distance function \n");
|
2015-06-04 08:28:26 -04:00
|
|
|
SSO(Distance,id,Dm,10);
|
2015-06-03 17:55:25 -04:00
|
|
|
|
|
|
|
|
char LocalRankFilename[40];
|
|
|
|
|
sprintf(LocalRankFilename,"Dist.%05i",rank);
|
|
|
|
|
FILE *DIST = fopen(LocalRankFilename,"wb");
|
|
|
|
|
fwrite(Distance.get(),8,Distance.length(),DIST);
|
|
|
|
|
fclose(DIST);
|
|
|
|
|
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Barrier(comm);
|
2015-06-03 17:55:25 -04:00
|
|
|
MPI_Finalize();
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|