Ejemplo n.º 1
0
/*
 *  display()
 *  ------
 *  Display the scene
 */
void display(void)
{
  /* setup functions */  
  glClear(GL_COLOR_BUFFER_BIT);

  /* Draw Scene */
  drawScene();

  /* Flush and sanity check */
  glFlush();
  errCheck("display sanity check");
}
Ejemplo n.º 2
0
/*
 *  display()
 *  ------
 *  Display the scene
 */
void display(void)
{
  /* setup functions */  
  displayInit();
  displayEye();

  /* Draw Scene */
  drawScene();

  glFlush();
  glutSwapBuffers();

  errCheck("display sanity check");
}
Ejemplo n.º 3
0
  // =======================================================
  // =======================================================
  MpiCommCart::MpiCommCart(int mx, int my, int mz, int isPeriodic, int allowReorder)
    : MpiComm(), mx_(mx), my_(my), mz_(mz), myCoords_(new int[NDIM_3D]), is2D(false)
  {
    int dims[NDIM_3D]    = {mx, my, mz};
    int periods[NDIM_3D] = {isPeriodic, isPeriodic, isPeriodic};

    // create virtual topology cartesian 3D
    errCheck( MPI_Cart_create(MPI_COMM_WORLD, NDIM_3D, dims, periods, allowReorder, &comm_), "MPI_Cart_create" );

    // fill nProc_ and myRank_
    init();

    // get cartesian coordinates (myCoords_) of current process (myRank_)
    getCoords(myRank_, NDIM_3D, myCoords_);
  }
Ejemplo n.º 4
0
//
// gateway driver to call the multiway cut from matlab
//
// This is the matlab entry point
void 
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  int i, j;
  int ndim = 2;
  long n, m, nmin;
  double *src, *snk, *nbr; 
  double flow = 0;
  double *out;
	
  // check for proper number and size of arguments
  errCheck(nrhs == 4,"Arguments:  src snk d1 d2");
  errCheck(mxGetNumberOfDimensions(prhs[0])==2,"2D arguments only");
  errCheck(mxGetNumberOfDimensions(prhs[1])==2,"2D arguments only");
  errCheck(nrhs == ndim+2,"Number of args must match image dimensionality.");
  const int *dim1 = mxGetDimensions(prhs[0]);
  const int *dim2 = mxGetDimensions(prhs[1]);
  for (i = 0; i < ndim; i++) {
    errCheck(dim1[i] == dim2[i],"First two arguments must be same size.");
  }
  for (i = 0; i < ndim; i++) {
    //mexPrintf("%d:  %d vs. %d\n",i,ndim,mxGetNumberOfDimensions(prhs[2+i]));
    errCheck(ndim == mxGetNumberOfDimensions(prhs[2+i]),
             "Difference arguments must be same dimensionality.");
    dim2 = mxGetDimensions(prhs[2+i]);
    for (j = 0; j < ndim; j++) {
      //mexPrintf("Arg %d dimension %d:  %d vs. %d.\n",i,j,dim1
      errCheck(dim1[j] == dim2[j]+(j==i),"Difference argument sizes bad.");
    }
  }
  if (nlhs > 2) {
    mexErrMsgTxt("Too many output arguments.");
  }
  src = mxGetPr(prhs[0]);
  snk = mxGetPr(prhs[1]);

  //mexPrintf("c\nc maxflow - push-relabel (highest level)\nc\n");

  // set up graph data structure
  typedef Graph<double,double,double> GraphType;
  // had used int,int,int up to 6/30/11 -- could change cause trouble?
  GraphType *g = new GraphType(dim1[0]*dim1[1],
                               (dim1[0]-1)*dim1[1]+dim1[0]*(dim1[1]-1)); 

  // add nodes
  for (i = 0; i < dim1[0]*dim1[1]; i++) {
    g -> add_node(); 
  }

  // add source/sink weights
  for (i = 0; i < dim1[0]*dim1[1]; i++) {
    g -> add_tweights(i,src[i],snk[i]);
  }

  // add neighbor links
  nbr = mxGetPr(prhs[2]);
  for (i = 0; i < dim1[0]-1; i++) {
    for (j = 0; j < dim1[1]; j++) {
      if (nbr[i+j*(dim1[0]-1)] > 0) {
        g -> add_edge(i+j*dim1[0],i+j*dim1[0]+1,
                      nbr[i+j*(dim1[0]-1)],nbr[i+j*(dim1[0]-1)]);
      }
    }
  }
  nbr = mxGetPr(prhs[3]);
  for (i = 0; i < dim1[0]; i++) {
    for (j = 0; j < dim1[1]-1; j++) {
      if (nbr[i+j*dim1[0]] > 0) {
        g -> add_edge(i+j*dim1[0],i+(j+1)*dim1[0],
                      nbr[i+j*dim1[0]],nbr[i+j*dim1[0]]);
      }
    }
  }
  
  // do calculation
  flow = g -> maxflow();

  // allocate output space
  plhs[0] = mxCreateNumericArray(ndim, dim1, mxDOUBLE_CLASS, mxREAL);
  out = mxGetPr(plhs[0]);

  // copy results to output array
  for (i = 0; i < dim1[0]; i++) {
    for (j = 0; j < dim1[1]; j++) {
      out[i+j*dim1[0]] = g->what_segment(i+j*dim1[0]) == GraphType::SINK;
    }
  }

  // cost of cut
  if (nlhs == 2) {
    plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
    *mxGetPr(plhs[1]) = flow;
  }
  //mexPrintf("Cost of cut is %f.\n",flow);

  // let Matlab free stuff...
  delete g;
}
Ejemplo n.º 5
0
/*
 *  Load texture from BMP file
 */
unsigned int loadTexBMP(char* file)
{
  unsigned int   texture;    /* Texture name */
  FILE*          f;          /* File pointer */
  unsigned short magic;      /* Image magic */
  unsigned int   dx,dy,size; /* Image dimensions */
  unsigned short nbp,bpp;    /* Planes and bits per pixel */
  unsigned char* image;      /* Image data */
  unsigned int   k;          /* Counter */

  /*  Open file */
  f = fopen(file,"rb");
  if (!f) fatal("Cannot open file %s\n",file);
  /*  Check image magic */
  if (fread(&magic,2,1,f)!=1) fatal("Cannot read magic from %s\n",file);
  if (magic!=0x4D42 && magic!=0x424D) fatal("Image magic not BMP in %s\n",file);
  /*  Seek to and read header */
  if (fseek(f,16,SEEK_CUR) || fread(&dx ,4,1,f)!=1 || fread(&dy ,4,1,f)!=1 ||
      fread(&nbp,2,1,f)!=1 || fread(&bpp,2,1,f)!=1 || fread(&k,4,1,f)!=1)
    fatal("Cannot read header from %s\n",file);
  /*  Reverse bytes on big endian hardware (detected by backwards magic) */
  if (magic==0x424D) {
    reverse(&dx,4);
    reverse(&dy,4);
    reverse(&nbp,2);
    reverse(&bpp,2);
    reverse(&k,4);
  }
  /*  Check image parameters */
  if (dx<1 || dx>65536) fatal("%s image width out of range: %d\n",file,dx);
  if (dy<1 || dy>65536) fatal("%s image height out of range: %d\n",file,dy);
  if (nbp!=1)  fatal("%s bit planes is not 1: %d\n",file,nbp);
  if (bpp!=24) fatal("%s bits per pixel is not 24: %d\n",file,bpp);
  if (k!=0)    fatal("%s compressed files not supported\n",file);
#ifndef GL_VERSION_2_0
  /*  OpenGL 2.0 lifts the restriction that texture size must be a power of two */
  for (k=1;k<dx;k*=2);
  if (k!=dx) fatal("%s image width not a power of two: %d\n",file,dx);
  for (k=1;k<dy;k*=2);
  if (k!=dy) fatal("%s image height not a power of two: %d\n",file,dy);
#endif

  /*  Allocate image memory */
  size = 3*dx*dy;
  image = (unsigned char*) malloc(size);
  if (!image) fatal("Cannot allocate %d bytes of memory for image %s\n",size,file);
  /*  Seek to and read image */
  if (fseek(f,20,SEEK_CUR) || fread(image,size,1,f)!=1) 
    fatal("Error reading data from image %s\n",file);
  fclose(f);
  /*  Reverse colors (BGR -> RGB) */
  for (k=0;k<size;k+=3) {
    unsigned char temp = image[k];
    image[k]   = image[k+2];
    image[k+2] = temp;
  }

  /*  Sanity check */
  errCheck("LoadTexBMP");
  /*  Generate 2D texture */
  glGenTextures(1,&texture);
  glBindTexture(GL_TEXTURE_2D,texture);
  /*  Copy image */
  glTexImage2D(GL_TEXTURE_2D,0,3,dx,dy,0,GL_RGB,GL_UNSIGNED_BYTE,image);
  if (glGetError()) fatal("Error in glTexImage2D %s %dx%d\n",file,dx,dy);
  /*  Scale linearly when image size doesn't match */
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

  /*  Free image memory */
  free(image);
  /*  Return texture name */
  return texture;
}
Ejemplo n.º 6
0
//
// gateway driver to call the distance transform code from matlab
//
// This is the matlab entry point
void 
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  int i, j, k, c, nrow, ncol, maxdim, nmpt;
  int mxfn, myfn, mrfn, mvxfn, mvyfn, mpfn, mchfn;
  int root, curt, ch, prnt, dtarrbase, dtarrlim;
  int ldim[3], *v, *mp, *mnch, *mdone, *mndesc, *dtarrhist;
  int *loc, *loc2, *lscratch, **locarr, **locarr2;
  char *pbimgc;
  double x_multiply, y_multiply, yx_ratio, maxd, *z;
  double *cost, *out, *scratch, *scratch2, *scl;
  double *mx, *my, *mr, *mvx, *mvy, *pbimg, *dt1, *tranarr;
  double **mch, **locp, **dtarr, **dtarrp;
  mxArray *cell, *field;

  // check for proper number and size of arguments
  errCheck(nrhs >= 2,"Arguments:  model, bimg, [maxd]");
  errCheck(nrhs <= 3,"Arguments:  model, bimg, [maxd]");
  errCheck(nlhs <= 3,"Outputs:  dt, [loc], [scale]");
  errCheck(mxIsStruct(prhs[0]),"model must be struct.");
  mxfn = mxGetFieldNumber(prhs[0],"x");
  errCheck(mxfn>=0,"Model must have field named x.");
  myfn = mxGetFieldNumber(prhs[0],"y");
  errCheck(myfn>=0,"Model must have field named y.");
  mrfn = mxGetFieldNumber(prhs[0],"r");
  errCheck(mrfn>=0,"Model must have field named r.");
  mvxfn = mxGetFieldNumber(prhs[0],"vx");
  errCheck(mvxfn>=0,"Model must have field named vx.");
  mvyfn = mxGetFieldNumber(prhs[0],"vy");
  errCheck(mvyfn>=0,"Model must have field named vy.");
  mpfn = mxGetFieldNumber(prhs[0],"parent");
  errCheck(mpfn>=0,"Model must have field named parent.");
  mchfn = mxGetFieldNumber(prhs[0],"children");
  errCheck(mchfn>=0,"Model must have field named children.");
  errCheck(mxIsDouble(prhs[1])||mxIsLogical(prhs[1])||mxIsUint8(prhs[1]),
           "Second argument must be binary image type.");
  errCheck(mxGetNumberOfDimensions(prhs[1])==2,
           "Second argument must be binary image type.");
  if (nrhs>2) {
    errCheck(mxIsDouble(prhs[2])&&!mxIsComplex(prhs[2]),
             "maxd must be real double.");
    errCheck(mxGetNumberOfElements(prhs[2])==1,
             "maxd must be scalar.");
    maxd = *mxGetPr(prhs[2]);
  } else {
    maxd = LARGE;
  }
  nmpt = (int)mxGetNumberOfElements(prhs[0]);
  nrow = (int)mxGetM(prhs[1]);
  ncol = (int)mxGetN(prhs[1]);

  // transcribe model variables
  root = -1;
  mx = (double*)mxMalloc(nmpt*sizeof(double));
  my = (double*)mxMalloc(nmpt*sizeof(double));
  mr = (double*)mxMalloc(nmpt*sizeof(double));
  mvx = (double*)mxMalloc(nmpt*sizeof(double));
  mvy = (double*)mxMalloc(nmpt*sizeof(double));
  mp = (int*)mxMalloc(nmpt*sizeof(int));
  mch = (double**)mxMalloc(nmpt*sizeof(double*));
  mnch = (int*)mxMalloc(nmpt*sizeof(int));
  for (int i = 0; i < nmpt; i++) {
    mx[i] = *mxGetPr(mxGetFieldByNumber(prhs[0],i,mxfn));
    errCheck(ABS(ROUND(mx[i])) < ncol,"Model displacement larger than image.");
    my[i] = *mxGetPr(mxGetFieldByNumber(prhs[0],i,myfn));
    errCheck(ABS(ROUND(my[i])) < nrow,"Model displacement larger than image.");
    mr[i] = *mxGetPr(mxGetFieldByNumber(prhs[0],i,mrfn));
    mvx[i] = *mxGetPr(mxGetFieldByNumber(prhs[0],i,mvxfn));
    mvy[i] = *mxGetPr(mxGetFieldByNumber(prhs[0],i,mvyfn));
    mp[i] = (int)(*mxGetPr(mxGetFieldByNumber(prhs[0],i,mpfn)));
    field = mxGetFieldByNumber(prhs[0],i,mchfn);
    mch[i] = mxGetPr(field);
    mnch[i] = (int)(mxGetNumberOfElements(field));
    if (mp[i]==0) {
      errCheck(root<0,"Model must have a single root.");
      root = i;
    }
  }
  errCheck(root>=0,"Model must have a root.");

  // allocate output space
  plhs[0] = mxCreateDoubleMatrix(nrow, ncol, mxREAL);
  out = mxGetPr(plhs[0]);
  if (nlhs>=2) {
    locp = (double**)mxMalloc(nrow*ncol*sizeof(double*));
    plhs[1] = mxCreateCellMatrix(nrow,ncol);
    for (i = 0; i < nrow*ncol; i++) {
      cell = mxCreateDoubleMatrix(2,nmpt,mxREAL);
      mxSetCell(plhs[1],i,cell);
      locp[i] = mxGetPr(cell);
    }
    loc = (int*)mxMalloc(2*nrow*ncol*sizeof(int));
    loc2 = loc+nrow*ncol;
    if (nlhs>=3) {
      plhs[2] = mxCreateDoubleMatrix(nrow,ncol,mxREAL);
      scl = mxGetPr(plhs[2]);
    }
  } else {
    loc = 0;
    loc2 = 0;
    locp = 0;
  }

  // transcribe image
  pbimg = mxGetPr(prhs[1]);
  pbimgc = (char*)pbimg;
  dt1 = out;  // this is where the answer will end up, eventually
  if (mxIsDouble(prhs[1])) {
    for (int i = 0; i <nrow*ncol; i++) {
      if (pbimg[i]) {
        dt1[i] = 0;
      } else {
        dt1[i] = maxd;
      }
    }
  } else {
    for (int i = 0; i <nrow*ncol; i++) {
      if (pbimgc[i]) {
        dt1[i] = 0;
      } else {
        dt1[i] = maxd;
      }
    }
  }

  // allocate scratch space for distance transform
  maxdim = MAX(nrow,ncol);
  scratch = (double*)mxMalloc(maxdim*sizeof(double));
  scratch2 = (double*)mxMalloc(nrow*ncol*sizeof(double));
  if (loc) {
    lscratch = (int*)mxMalloc(nrow*ncol*sizeof(int));
  } else {
    lscratch = 0;
  }
  v = (int*)mxMalloc(maxdim*sizeof(int));
  z = (double*)mxMalloc((maxdim+1)*sizeof(double));

  // allocate other variables
  mdone = (int*)mxMalloc(nmpt*sizeof(int));
  mndesc = (int*)mxMalloc(nmpt*sizeof(int));
  tranarr = (double*)mxMalloc(nrow*ncol*sizeof(double));  // stores translation
  dtarr = (double**)mxMalloc(nmpt*sizeof(double*));  // stores intermediate dt
  dtarrp = (double**)mxMalloc(nmpt*sizeof(double*));  // pointer to storage
  dtarrhist = (int*)mxMalloc(nmpt*sizeof(int));  // stores historical size
  locarr = (int**)mxMalloc(2*nmpt*sizeof(int*));  // stores intermediate loc
  locarr2 = locarr+nmpt;
  for (i = 0; i < nmpt; i++) {
    mdone[i] = 0;
    mndesc[i] = 1;
    dtarrp[i] = 0;
  }
  if (loc) {
    for (i = 0; i < nmpt; i++) {
      locarr[i] = (int*)mxMalloc(2*nrow*ncol*sizeof(int));
      locarr2[i] = locarr[i]+nrow*ncol;
    }
  } else {
    for (i = 0; i < nmpt; i++) {
      locarr[i] = 0;
      locarr2[i] = 0;
    }
  }

  // compute distance transform on input image
  gdt2d(dt1,dt1,0,v,z,nrow,ncol,1,1,scratch,scratch2,lscratch);

  // run computation
  curt = root;
  dtarrlim = 0;
  dtarrbase = 0;
  dtarrp[root] = dt1;
  k = 0;
  while (curt != -1) {

    // set base point
    if (mdone[curt]==0) {
      dtarrhist[curt] = dtarrbase;
    }

    // figure out if we need to descend to children
    if (mdone[curt]<mnch[curt]) {
      // figure out current child
      ch = (int)(mch[curt][mdone[curt]])-1;  // Matlab indices start from 1
     
      // we haven't yet computed the current child; set up to do that now
      ch = (int)(mch[curt][mdone[curt]])-1;  // Matlab indices start from 1
      
      // allocate new dt storage space if necessary
      if (dtarrlim==dtarrbase) {
        dtarr[dtarrlim] = (double*)mxMalloc(nrow*ncol*sizeof(double));
        dtarrlim++;
      }
      
      // copy dt1
      for (i = 0; i < nrow*ncol; i++) {
        dtarr[dtarrbase][i] = dt1[i];
      }
           
      // set up storage for this child
      dtarrp[ch] = dtarr[dtarrbase];
      dtarrbase++;
      
      // move on, but when we return, remember that this child is done
      mdone[curt]++;
      curt = ch;
    } else {
      // all children complete; assemble result
      for (c = 0; c < mnch[curt]; c++) {
        ch = (int)(mch[curt][c])-1;  // Matlab indices start from 1
        errCheck(dtarrp[ch]!=0,"Null pointer");
        mxAssert((ch>=0)&&(ch<nmpt),"Out of bounds");
        tranDT(dtarrp[ch],tranarr,nrow,ncol,mx[ch],my[ch],maxd*mndesc[ch]);
        gdt2d(tranarr,dtarrp[ch],locarr[ch],v,z,nrow,ncol,mvx[ch],mvy[ch],
               scratch,scratch2,lscratch);
        for (i = 0; i < nrow*ncol; i++) {
          dtarrp[curt][i] += dtarrp[ch][i];
        }
      }

      // now go back up to the parent
      if (mp[curt]>0) {
        mxAssert((curt>=0)&&(curt<nmpt),"Out of bounds");
        mxAssert((mp[curt]-1>=0)&&(mp[curt]-1<nmpt),"Out of bounds");
        mndesc[mp[curt]-1] += mndesc[curt];
      }
      dtarrbase = dtarrhist[curt];
      curt = mp[curt]-1;  // Matlab indices start from 1
    }
  }

  // do final translation and distance transform
  tranDT(dt1,tranarr,nrow,ncol,mx[root],my[root],maxd*nmpt);
  gdt2d(tranarr,dt1,locarr[root],v,z,nrow,ncol,mvx[root],mvy[root],
         scratch,scratch2,lscratch);

  // assemble loc information if required
  prnt = -1;
  if (loc) {
    for (i = 0; i < nmpt; i++) {
      mdone[i] = 0;
    }
    curt = root;

    while (curt != -1) {
      // first time through:
      if (mdone[curt] == 0) {
        int y = ROUND(my[curt]);
        int x = ROUND(mx[curt]);
        // copy results for this node
        for (i = 0; i < nrow; i++) {
          for (j = 0; j < ncol; j++) {
            if (prnt == -1) {
              locp[i+j*nrow][2*curt] = BOUND(j+x,0,ncol-1);
              locp[i+j*nrow][2*curt+1] = BOUND(i+y,0,nrow-1);
            } else {
              int jj = (int)BOUND(locp[i+j*nrow][2*prnt],0,ncol-1);
              int ii = (int)BOUND(locp[i+j*nrow][2*prnt+1],0,nrow-1);
              locp[i+j*nrow][2*curt] = locarr[curt][ii+nrow*jj]+x;
              locp[i+j*nrow][2*curt+1] = locarr2[curt][ii+nrow*jj]+y;
            }
          }
        }
      }

      // descend to children if any left
      if (mdone[curt]<mnch[curt]) {
        // descend to child
        prnt = curt;
        ch = (int)(mch[curt][mdone[curt]])-1;  // Matlab indices start from 1
        mdone[curt]++;
        curt = ch;
      } else {
        // come back up
        curt = prnt;  // Matlab indices start from 1
        prnt = mp[curt]-1;
      }
    }

    // add +1 to all indices for Matlab
    for (i = 0; i < nrow*ncol; i++) {
      for (j = 0; j < 2*nmpt; j++) {
        locp[i][j]++;
      }
    }
  }

  // compute scale if needed
  if (nlhs>=3) {
    for (i = 0; i < nrow*ncol; i++) {
      double tmp = 0;
      
      for (j = 0; j < nmpt; j++) {
        if (mp[j]>0) {
          tmp += sqrt(SQR(locp[i][2*j]-locp[i][2*mp[j]-2])+
                      SQR(locp[i][2*j+1]-locp[i][2*mp[j]-1]));
        }
      }
      scl[i] = tmp;
    }
  }

  // free stuff
  for (i = 0; i < dtarrlim; i++) {
    mxFree(dtarr[i]);
  }
  if (loc) {
    for (i = 0; i < nmpt; i++) {
      mxFree(locarr[i]);
    }
  }
  mxFree(locarr);
  mxFree(dtarrhist);
  mxFree(dtarrp);
  mxFree(dtarr);
  mxFree(mndesc);
  mxFree(mdone);
  mxFree(z);
  mxFree(v);
  if (locp) {
    mxFree(lscratch);
  }
  mxFree(scratch2);
  mxFree(scratch);
  if (nlhs==2) {
    mxFree(loc);
    mxFree(locp);
  }
  mxFree(mnch);
  mxFree(mch);
  mxFree(mp);
  mxFree(mvy);
  mxFree(mvx);
  mxFree(mr);
  mxFree(my);
  mxFree(mx);
}