Ejemplo n.º 1
0
/*
 * Main entry
 */
int main( int argc, char *argv[])
{
    Imod *imod;
    Iobj *obj;
    Ipoint point;
    Istore store;
    FILE *infp;
    char line[1024];
    int open = 0, zsort = 0, scat = 0, numPerCont = 0, fromZero = 0, zFromZero = 0;
    int err, nvals, nread, ob, co, lineNum, after, needcont, i, numOffset, linelen;
    int numPts = 0, numConts = 0, numObjs = 0;
    int sphere = 0, circle = 0;
    float tst1, tst2, xx, yy, zz, value;
    float zOffset = 0.;
    int numColors = 0;
    int numNames = 0;
    int hasValues = 0;
    int contValues = 0;
    int *red, *green, *blue;
    int directScale = 0;
    float xscale = 1., yscale = 1., zscale = 1., xtrans = 0., ytrans = 0., ztrans = 0.;
    char **names = NULL;

    char *progname = imodProgName(argv[0]);
    char *filename, *imagename;
    MrcHeader hdata;
    IrefImage *ref;
    FILE *fpimage = NULL;
    char *errString;
    int numOptArgs, numNonOptArgs;

    /* Fallbacks from    ../manpages/autodoc2man 2 1 point2model  */
    int numOptions = 16;
    const char *options[] = {
        "input:InputFile:FN:", "output:OutputFile:FN:", "open:OpenContours:B:",
        "scat:ScatteredPoints:B:", "number:PointsPerContour:I:", "planar:PlanarContours:B:",
        "zero:NumberedFromZero:B:", "zcoord:ZCoordinatesFromZero:B:",
        "values:ValuesInLastColumn:I:", "circle:CircleSize:I:", "sphere:SphereRadius:I:",
        "color:ColorOfObject:ITM:", "name:NameOfObject:CHM:",
        "image:ImageForCoordinates:FN:", "pixel:PixelSpacingOfImage:FT:",
        "origin:OriginOfImage:FT:"
    };

    /* Startup with fallback */
    PipReadOrParseOptions(argc, argv, options, numOptions, progname,
                          2, 1, 1, &numOptArgs, &numNonOptArgs, imodUsageHeader);

    /* Get input and output files */
    if (PipGetInOutFile("InputFile", 0, &filename))
        exitError("No input file specified");
    infp = fopen(filename, "r");
    if (!infp)
        exitError("Error opening input file %s", filename);
    free(filename);
    if (PipGetInOutFile("OutputFile", 1, &filename))
        exitError("No output file specified");

    if (!PipGetString("ImageForCoordinates", &imagename)) {
        fpimage = fopen(imagename, "rb");
        if (!fpimage)
            exitError("Could not open image file for coordinates: %s", imagename);
        if (mrc_head_read(fpimage, &hdata))
            exitError("Reading header from %s", imagename);
        free(imagename);
    }

    directScale = 2 - PipGetThreeFloats("PixelSpacingOfImage", &xscale, &yscale, &zscale)
                  - PipGetThreeFloats("OriginOfImage", &xtrans, &ytrans, &ztrans);
    if (directScale && fpimage)
        exitError("You cannot use -image together with -pixel or -origin");

    err = PipGetInteger("PointsPerContour", &numPerCont);
    err = PipGetInteger("SphereRadius", &sphere);
    err = PipGetInteger("CircleSize", &circle);
    err = PipGetBoolean("OpenContours", &open);
    err = PipGetBoolean("ScatteredPoints", &scat);
    err = PipGetBoolean("PlanarContours", &zsort);
    err = PipGetInteger("ValuesInLastColumn", &hasValues);
    err = PipGetBoolean("ZCoordinatesFromZero", &zFromZero);
    if (zFromZero)
        zOffset = 0.5;
    B3DCLAMP(hasValues, -1, 1);
    if (hasValues < 0) {
        hasValues = 1;
        contValues = 1;
    }
    err = PipGetBoolean("NumberedFromZero", &fromZero);
    numOffset = 1 - fromZero;
    if (numPerCont < 0)
        exitError("Number of points per contour must be positive or zero");
    if (open + scat > 1)
        exitError("Only one of -open or -scat may be entered");

    // Get colors
    err = PipNumberOfEntries("ColorOfObject", &numColors);
    if (numColors) {
        red = (int *)malloc(numColors * sizeof(int));
        green = (int *)malloc(numColors * sizeof(int));
        blue = (int *)malloc(numColors * sizeof(int));
        if (!red || !green || !blue)
            exitError("Allocating memory for colors");
        for (co = 0; co < numColors; co++)
            err = PipGetThreeIntegers("ColorOfObject", &red[co], &green[co],
                                      &blue[co]);
    }

    // Get names
    err = PipNumberOfEntries("NameOfObject", &numNames);
    if (numNames) {
        names = (char **)malloc(numNames * sizeof(char *));
        if (!names)
            exitError("Allocating memory for names");
        for (co = 0; co < numNames; co++)
            err = PipGetString("NameOfObject", &names[co]);
    }

    PipDone();

    // Read first line of file, figure out how many values
    if (fgetline(infp, line, 1024) <= 0)
        exitError("Reading beginning of file");

    nvals = sscanf(line, "%f %f %f %f %f %f", &tst1, &tst2, &xx, &yy, &zz, &value);
    nvals -= hasValues;
    if (nvals < 3)
        exitError("There must be at least %d values per line", 3 + hasValues);
    nvals = B3DMIN(nvals, 5);
    if (numPerCont && nvals > 3) {
        exitError("The point file has contour numbers and the -number option "
                  "cannot be used");
    }
    if (zsort && nvals > 3)
        exitError("The point file has contour numbers and the -planar option "
                  "cannot be used");

    rewind(infp);

    imod = imodNew();
    if (!imod)
        exitError("Failed to get model structure");

    // Set the image reference scaling
    if (fpimage) {
        imodSetRefImage(imod, &hdata);
        fclose(fpimage);
    } else if (directScale) {
        imod->refImage = (IrefImage *)malloc(sizeof(IrefImage));
        if (!imod->refImage)
            exitError("Allocating IrefImage structure");
        ref = imod->refImage;
        ref->ctrans.x = xtrans;
        ref->ctrans.y = ytrans;
        ref->ctrans.z = ztrans;
        ref->cscale.x = xscale;
        ref->cscale.y = yscale;
        ref->cscale.z = zscale;
        ref->oscale.x = ref->oscale.y = ref->oscale.z = 1.;
        ref->orot.x = ref->orot.y = ref->orot.z = 0.;
        ref->crot.x = ref->crot.y = ref->crot.z = 0.;
        ref->otrans.x = ref->otrans.y = ref->otrans.z = 0.;
    }

    ob = 0;
    co = 0;
    lineNum = 0;
    imod->xmax = 0.;
    imod->ymax = 0.;
    imod->zmax = 0.;
    store.type = GEN_STORE_VALUE1;
    store.flags = GEN_STORE_FLOAT << 2;

    // To do: error check contour and object #'s, and they are numbered from 1.
    while (1) {

        // get line, done on EOF, skip blank line
        linelen = fgetline(infp, line, 1024);
        if (linelen < 0)
            break;
        if (linelen == 0)
            continue;

        if (nvals == 3) {
            nread = sscanf(line, "%f %f %f %f", &xx, &yy, &zz, &value);
        } else if (nvals == 4) {
            nread = sscanf(line, "%d %f %f %f %f", &co, &xx, &yy, &zz, &value);
            co -= numOffset;
        } else {
            nread = sscanf(line, "%d %d %f %f %f %f", &ob, &co, &xx, &yy, &zz, &value);
            co -= numOffset;
            ob -= numOffset;
        }
        zz -= zOffset;
        lineNum++;

        // Skip line with no values
        if (nread <= 0)
            continue;
        if (B3DMIN(5 + hasValues, nread) != nvals + hasValues &&
                !(contValues && nread == nvals))
            exitError("Every line should have %d entries; line %d has %d",
                      nvals + hasValues, lineNum, nread);

        if (ob < 0 || co < 0)
            exitError("Illegal object or contour number (object %d, contour %d at line %d",
                      ob + numOffset, co + numOffset, lineNum);

        // Add objects if needed to get to the current object
        if (ob >= imod->objsize) {
            for (i = imod->objsize; i <= ob; i++) {
                if (imodNewObject(imod))
                    exitError("Failed to add object to model");
                if (open)
                    imod->obj[i].flags |= IMOD_OBJFLAG_OPEN;
                if (scat)
                    imod->obj[i].flags |= IMOD_OBJFLAG_SCAT | IMOD_OBJFLAG_OPEN;
                numObjs++;
                imod->obj[i].pdrawsize = B3DMAX(0, sphere);
                if (circle > 0) {
                    imod->obj[i].symsize = circle;
                    imod->obj[i].symbol = IOBJ_SYM_CIRCLE;
                }
                if (i < numColors) {
                    imod->obj[i].red = red[i] / 255.;
                    imod->obj[i].green = green[i] / 255.;
                    imod->obj[i].blue = blue[i] / 255.;
                }
                if (i < numNames) {
                    strncpy(imod->obj[i].name, names[i], IOBJ_STRSIZE - 1);
                    imod->obj[i].name[IOBJ_STRSIZE - 1] = 0x00;
                }
            }
        }

        // Determine if a contour is needed: either the contour number is too high
        // or the number limit is reached or there is a change in Z
        needcont = 0;
        obj = &imod->obj[ob];
        if (co >= obj->contsize)
            needcont = 1;
        else if ((numPerCont && obj->cont[co].psize >= numPerCont) ||
                 (zsort && B3DNINT(obj->cont[co].pts[0].z) != B3DNINT(zz))) {
            co++;
            needcont = 1;
        }

        if (needcont) {
            imodSetIndex(imod, ob, -1, -1);
            for (i = obj->contsize; i<= co; i++) {
                if (imodNewContour(imod))
                    exitError("Failed to add contour to model");
                numConts++;
            }
        }

        point.x = xx;
        point.y = yy;
        point.z = zz;
        imod->xmax = B3DMAX(imod->xmax, B3DNINT(xx + 10.));
        imod->ymax = B3DMAX(imod->ymax, B3DNINT(yy + 10.));
        imod->zmax = B3DMAX(imod->zmax, B3DNINT(zz + 1.));
        if (!imodPointAppend(&obj->cont[co], &point))
            exitError("Failed to add point to contour");
        numPts++;

        // take care of value for contour or point, only add one per contour
        if (hasValues) {
            store.value.f = value;
            err = 0;
            if (contValues && istoreLookup(obj->store, co, &after) < 0) {
                if (nread < nvals + 1)
                    exitError("The first point for a contour must have a value entry; line %d "
                              "has only %d entries", lineNum, nread);
                store.index.i = co;
                err = istoreInsert(&obj->store, &store);
            } else {
                store.index.i = obj->cont[co].psize - 1;
                err = istoreInsert(&obj->cont[co].store, &store);
            }
            if (err)
                exitError("Failed to add general value");
        }
    }

    // Get the object min/max values set up
    if (hasValues) {
        for (ob = 0; ob < imod->objsize; ob++) {
            if (imod->obj[ob].contsize && istoreFindAddMinMax1(&imod->obj[ob]))
                exitError("Adding min/max values to object");
        }
    }

    // Attach image file's size as the max values
    if (fpimage) {
        imod->xmax = hdata.nx;
        imod->ymax = hdata.ny;
        imod->zmax = hdata.nz;
    }

    fclose(infp);
    if (imodBackupFile(filename))
        printf("Warning: %s - Failed to make old version of %s be a backup file\n",
               progname, filename);

    imod->file = fopen(filename, "wb");
    if (!imod->file)
        exitError("Opening new model file %s", filename);
    if (imodWriteFile(imod))
        exitError("Writing model file %s", filename);
    free(filename);
    printf("Model created with %d objects, %d contours, %d points\n", numObjs,
           numConts, numPts);
    exit(0);
}
Ejemplo n.º 2
0
int main( int argc, char *argv[])
{
  int i;
  FILE *fin, *fout;
  struct Mod_Model *mod;
  int npatch = 0;
  int nvalue = 0, nvalue2 = 0;
  int ix, iy, iz;
  float dx, dy, dz;
  float value, maxval = -1.e30;
  float value2, maxval2 = -1.e30;
  int ob, co, listInd, listStart;
  Ipoint *pts;
  char format[10] = "%10.4f";
  char format2[10] = "%10.4f";

  setExitPrefix("ERROR: imod2patch - ");


  if (argc != 3){
    if (argc != 1)
      printf("ERROR: imod2patch - wrong # of arguments\n");
    printf("imod2patch usage:\n");
    printf("imod2patch imod_model patch_file\n");
    exit(1);
  }


  i = 1;

  mod = imodRead(argv[i]);
  if (!mod)
    exitError("Reading model %s\n", argv[i]);
  
  if (imodBackupFile(argv[++i]))
    exitError("Renaming existing output file to %s\n", argv[i]);
  
  fout = fopen(argv[i], "w");
  if (!fout)
    exitError("Could not open %s\n", argv[i]);

  /* Count up patches and values and find max of values */
  for (ob = 0; ob < mod->objsize; ob++) {
    listInd = 0;
    for (co = 0; co < mod->obj[ob].contsize; co++) {
      listStart = listInd;
      if (mod->obj[ob].cont[co].psize >= 2) {
        npatch++;
        if (istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE1, &value,
                            &listInd)) {
          maxval = B3DMAX(maxval, value);
          nvalue++;
        }
        listInd = listStart;
        if (istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE2, &value2,
                            &listInd)) {
          maxval2 = B3DMAX(maxval2, value2);
          nvalue2++;
        }
      }
    }
  }

  /* If values are greater than one they are probably residuals and only need
     2 decimal places */
  if (nvalue && maxval > 1.01)
    strcpy(format, "%10.2f");
  if (nvalue2 && maxval2 > 1.01)
    strcpy(format, "%10.2f");

  fprintf(fout, "%d   edited positions\n", npatch);
  for (ob = 0; ob < mod->objsize; ob++) {
    listInd = 0;
    for (co = 0; co < mod->obj[ob].contsize; co++) {
      listStart = listInd;
      if (mod->obj[ob].cont[co].psize >= 2) {
        pts = mod->obj[ob].cont[co].pts;
        ix = pts[0].x + 0.5;
        iy = pts[0].y + 0.5;
        iz = pts[0].z + 0.5;
        dx = (pts[1].x - pts[0].x) / mod->pixsize;
        dy = (pts[1].y - pts[0].y) / mod->pixsize;
        dz = (pts[1].z - pts[0].z) / mod->pixsize;
        if (mod->flags & IMODF_FLIPYZ)
          fprintf(fout, "%6d %5d %5d %8.2f %8.2f %8.2f", 
                  ix, iz, iy, dx, dz, dy);
        else
          fprintf(fout, "%6d %5d %5d %8.2f %8.2f %8.2f", 
                  ix, iy, iz, dx, dy, dz);
        if (nvalue) {
          value = 0.;
          istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE1, &value,
                          &listInd);
          fprintf(fout, format, value);
          
        }
        if (nvalue2) {
          listInd = listStart;
          value2 = 0.;
          istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE2, &value2,
                          &listInd);
          fprintf(fout, format2, value2);
          
        }
        fprintf(fout, "\n");

      }
    }
  }
  fclose(fout);
  exit(0);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
  Imod   model;
  FILE   *fin, *fout;
  char   *filename = NULL;
  int    i;
  int    mode = 0;
  int    useZscale = 0;
  int    doflip = 1;
  int    transopt = 0;
  int    rotScaleopt = 0;
  int    toImage = 0, fromImage = 0;
  int    oneLine = -1;
  int    toggleFlip = 0;
  int    flipModel = 0;
  float  zscale = 1.;
  float transScale = 1.;
  float  rx = 0., ry = 0., rz = 0.;
  float transx = 0., transy = 0., transz = 0.;
  int multiTrans = 0;
  int newNx = 0, newNy = 0, newNz = 0;
  char *progname = imodProgName(argv[0]);
  Ipoint newCen, tmpPt;
  Imat *mat = imodMatNew(3);
  Imat *normMat = imodMatNew(3);
  IrefImage useRef, *modRefp;
  MrcHeader hdata, hdataFirst;
  Ipoint unitPt = {1., 1., 1.};
  char prefix[100];
  sprintf(prefix, "ERROR: %s - ", progname);
  setExitPrefix(prefix);

  if (argc < 3){
    usage(progname);
  }
     
  for (i = 1; i < argc; i++) {
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {

      case '2':
        filename = argv[++i];
        mode += 2;
        break;
            
      case '3':
        filename = argv[++i];
        mode += 3;
        break;

      case 'S':
        sscanf(argv[++i], "%f", &transScale);
        break;

      case 'i':
        toImage = 1;
        if (NULL == (fin = fopen(argv[++i], "rb")))
          exitError("Couldn't open %s", argv[i]);

        if (mrc_head_read(fin, &hdata)) 
          exitError("Reading header from %s", argv[i]);
        fclose(fin);
        break;

      case 'I':
        fromImage = 1;
        if (NULL == (fin = fopen(argv[++i], "rb")))
          exitError("Couldn't open %s", argv[i]);

        if (mrc_head_read(fin, &hdataFirst)) 
          exitError("Reading header from %s", argv[i]);
        fclose(fin);
        break;

      case 'z':
        useZscale = 1;
        break;

      case 'f':
        doflip = 0;
        break;

      case 'Y':
        flipModel = 1;
        break;

      case 'T':
        toggleFlip = 1;
        break;

      case 'n':
        sscanf(argv[++i], "%d%*c%d%*c%d", &newNx, &newNy, &newNz);
        break;
            
      case 'l':
        oneLine = atoi(argv[++i]);
        break;

      case 't':  /* Translations */
        tmpPt.x = tmpPt.y = tmpPt.z = 0.;
        transopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-tx%f", &tmpPt.x);
          else
            sscanf(argv[++i], "%f", &tmpPt.x);
          if (transx)
            multiTrans = 1;
          transx = tmpPt.x;
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-ty%f", &tmpPt.y);
          else
            sscanf(argv[++i], "%f", &tmpPt.y);
          if (transy)
            multiTrans = 1;
          transy = tmpPt.y;
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-tz%f", &tmpPt.z);
          else
            sscanf(argv[++i], "%f", &tmpPt.z);
          if (transz)
            multiTrans = 1;
          transz = tmpPt.z;
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        imodMatTrans(mat, &tmpPt);
        break;

      case 's':  /* Scaling */
        tmpPt.x = tmpPt.y = tmpPt.z = 1.;
        transopt = 1;
        rotScaleopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sx%f", &tmpPt.x);
          else
            sscanf(argv[++i], "%f", &tmpPt.x);
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sy%f", &tmpPt.y);
          else
            sscanf(argv[++i], "%f", &tmpPt.y);
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-sz%f", &tmpPt.z);
          else
            sscanf(argv[++i], "%f", &tmpPt.z);
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        imodMatScale(mat, &tmpPt);
        tmpPt.x = 1. / tmpPt.x;
        tmpPt.y = 1. / tmpPt.y;
        tmpPt.z = 1. / tmpPt.z;
        imodMatScale(normMat, &tmpPt);
        break;

      case 'r':  /* Rotations */
        transopt = 1;
        rotScaleopt = 1;
        switch (argv[i][2]) {
        case 'x':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-rx%f", &rx);
          else
            sscanf(argv[++i], "%f", &rx);
          imodMatRot(mat, (double)rx, b3dX);
          imodMatRot(normMat, (double)rx, b3dX);
          break;
        case 'y':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-ry%f", &ry);
          else
            sscanf(argv[++i], "%f", &ry);
          imodMatRot(mat, (double)ry, b3dY);
          imodMatRot(normMat, (double)ry, b3dY);
          break;
        case 'z':
          if (argv[i][3] != 0x00)
            sscanf(argv[i], "-rz%f", &rz);
          else
            sscanf(argv[++i], "%f", &rz);
          imodMatRot(mat, (double)rz, b3dZ);
          imodMatRot(normMat, (double)rz, b3dZ);
          break;
        default:
          exitError("Invalid option %s", argv[i]);
        }
        break;

      default:
        exitError("Invalid option %s",  argv[i]);
      }

    }else{
      break;
    }
  }

  if (mode && mode / 2 != 1) 
    exitError("You cannot enter both -2 and -3");
  
  if (mode && rotScaleopt)  
    exitError("You cannot enter -r or -s options with -2 and -3");
  if (transopt && mode == 2 && oneLine < 1 && transz)
    exitError("You cannot enter -tz option with -2 unless transforming with one line");
  if (mode && multiTrans)
    exitError("You cannot enter -t options more than once with -2 and -3");
  
  if (i != argc - 2) {
    printf("ERROR: %s - Command line should end with two non-option "
            "arguments\n", progname);
    usage(progname);
  }

  fin = fopen(argv[i], "rb");
  if (!fin)
    exitError("Opening input file %s", argv[i]);
      
  imodDefault(&model);
  model.file = fin;
  if (imodReadFile(&model))
    exitError("Reading imod model (%s)", argv[i]);
  fclose(fin);

  if (imodBackupFile(argv[i + 1])) 
    exitError("Couldn't create backup file");

  fout = fopen(argv[i + 1], "wb");
  if (!fout)
    exitError("Opening output file %s", argv[i + 1]);

  /* Change image reference information */
  if (fromImage && imodSetRefImage(&model, &hdataFirst))
    exitError("Allocating a IrefImage structure");

  /* Do flipping operations first */
  if (flipModel)
    imodFlipYZ(&model);
  if (toggleFlip) {
    if (model.flags & IMODF_FLIPYZ)
      model.flags &= ~IMODF_FLIPYZ;
    else
      model.flags |= IMODF_FLIPYZ;
  }

  /* Do scaling to image reference next */
  if (toImage) {

    modRefp = model.refImage;
    if (modRefp && !(model.flags & IMODF_OTRANS_ORIGIN)) 
      exitError("Model has no image origin information; -i option invalid");

    /* get the target transformation */
    useRef.ctrans.x = hdata.xorg;
    useRef.ctrans.y = hdata.yorg;
    useRef.ctrans.z = hdata.zorg;
    useRef.crot.x = hdata.tiltangles[3];
    useRef.crot.y = hdata.tiltangles[4];
    useRef.crot.z = hdata.tiltangles[5];
    useRef.cscale = unitPt;
    if (hdata.xlen && hdata.mx)
      useRef.cscale.x = hdata.xlen/(float)hdata.mx;
    if (hdata.ylen && hdata.my)
      useRef.cscale.y = hdata.ylen/(float)hdata.my;
    if (hdata.zlen && hdata.mz)
      useRef.cscale.z = hdata.zlen/(float)hdata.mz;
    
    if (modRefp) {

      /* If there is a refImage, scale the data as when reading into 3dmod */
      useRef.otrans = modRefp->ctrans;
      useRef.orot = modRefp->crot;
      useRef.oscale = modRefp->cscale;
      imodTransFromRefImage(&model, &useRef, unitPt);
    } else {

      /* If there is no refImage, do not scale data but assign all the information
         just as if reading into 3dmod */
      model.refImage = (IrefImage *)malloc (sizeof(IrefImage));
      modRefp = model.refImage;
      if (!modRefp) 
        exitError("Allocating a IrefImage structure");
      model.flags |= IMODF_OTRANS_ORIGIN;
    }
    *modRefp = useRef;
    modRefp->otrans = useRef.ctrans;

    /* Adjust the maxes for this change */
    model.xmax = hdata.nx;
    model.ymax = hdata.ny;
    model.zmax = hdata.nz;
  }

  /* Use zscale if user indicated it */
  if (useZscale && model.zscale > 0.)
    zscale = model.zscale;

  /* Do flipping if model flipped and user did not turn it off */
  if (!(model.flags & IMODF_FLIPYZ))
    doflip = 0;

  /* Warning every time is too noxious!
  if (!newNx)
    printf("Assuming transformed images have same size as original "
            "images\n (use -n option to specify size of transformed "
            "images)\n");
  */
  newCen.x = (newNx ? newNx : model.xmax) * 0.5f;
  newCen.y = (newNy ? newNy : model.ymax) * 0.5f;
  newCen.z = (newNz ? newNz : model.zmax) * 0.5f;

  if (filename){
    if (filetrans(filename, &model, mode, oneLine, newCen, zscale, doflip, transScale,
                  transx, transy, transz)) 
      exitError("Transforming model.");

  } else if (transopt) {
    imodTransModel3D(&model, mat, normMat, newCen, zscale, doflip);
  }

  model.xmax = newNx ? newNx : model.xmax;
  model.ymax = newNy ? newNy : model.ymax;
  model.zmax = newNz ? newNz : model.zmax;
  imodWrite(&model, fout);
  exit(0);
}
Ejemplo n.º 4
0
int main( int argc, char *argv[])
{
  int i, ind, col, maxValType;
  FILE *fout;
  Imod *mod;
  int npatch = 0;
  int numValues[MAX_VALUE_COLS], valueIDs[MAX_VALUE_COLS];
  int ix, iy, iz;
  int colForVal1 = 1;
  float dx, dy, dz;
  float value, maxVal[MAX_VALUE_COLS];
  int ob, co, listInd, listStart, numCols;
  Ipoint *pts;
  char format[MAX_VALUE_COLS][10];
  int colToTypeMap[MAX_VALUE_COLS];

  setExitPrefix("ERROR: imod2patch - ");


  if (argc < 3){
    if (argc != 1)
      printf("ERROR: imod2patch - wrong # of arguments\n");
    printf("imod2patch usage:\n");
    printf("imod2patch [-v col] imod_model patch_file\n");
    exit(1);
  }
  for (ind = 0; ind < MAX_VALUE_COLS; ind++) {
    numValues[ind] = 0;
    valueIDs[ind] = 0;
    colToTypeMap[ind] = -1;
    maxVal[ind] = -1.e37;
  }

  i = 1;
  if (!strcmp(argv[i], "-v")) {
    i++;
    colForVal1 = atoi(argv[i++]);
  }

  mod = imodRead(argv[i]);
  if (!mod)
    exitError("Reading model %s\n", argv[i]);
  
  if (imodBackupFile(argv[++i]))
    exitError("Renaming existing output file to %s\n", argv[i]);
  
  fout = fopen(argv[i], "w");
  if (!fout)
    exitError("Could not open %s\n", argv[i]);

  /* Count up patches and values and find max of values */
  for (ob = 0; ob < mod->objsize; ob++) {
    listInd = 0;
    for (co = 0; co < mod->obj[ob].contsize; co++) {
      listStart = listInd;
      if (mod->obj[ob].cont[co].psize >= 2) {
        npatch++;
        for (ind = 0; ind < MAX_VALUE_COLS; ind++) {
          listInd = listStart;
          if (istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE1 + 2 * ind, &value,
                              &listInd)) {
            maxVal[ind] = B3DMAX(maxVal[ind], value);
            numValues[ind]++;
          }
        }
      }
    }
  }
  /* printf("numval %d %d %d %f %f %f\n", numValues[0], numValues[1], numValues[2], 
     maxVal[0], maxVal[1], maxVal[2]); */

  /* Get the value ID's if any and convert an entered ID to a type #.  Also set format
     for output based on maximum value */
  maxValType = -1;
  numCols = 0;
  for (ind = 0; ind < MAX_VALUE_COLS; ind++) {
    strcpy(format[ind], "%10.4f");
    if (numValues[ind]) {
      numCols++;
      maxValType = ind;
      if (maxVal[ind] > 10.1)
        strcpy(format[ind], "%10.2f");
      else if (maxVal[ind] > 1.01)
        strcpy(format[ind], "%10.3f");
    }
  }
  for (ind = 0; ind < B3DMIN(maxValType + 1, mod->obj[0].extra[IOBJ_EXSIZE - 1]); ind++)
    valueIDs[ind] = mod->obj[0].extra[IOBJ_EXSIZE - 2 - ind];
  /*printf("# vla ID %d %d %d %d\n", maxValType, valueIDs[0],valueIDs[1],valueIDs[2]);*/

  /* Error checks on the column for value 1 */
  if (colForVal1 < 1 || colForVal1 > numCols) {
      if (numCols)
        exitError("The column for general value type must be between 1 and %d", numCols);
      exitError("There are no general values stored in the model");
  }
  colForVal1--;
  
  /* Set up map from column to value type index */
  if (numValues[0])
    colToTypeMap[colForVal1] = 0;
  col = 0;
  for (ind = 1; ind <= maxValType; ind++) {
    if (numValues[ind]) {
      if (!colToTypeMap[col])
        col++;
      colToTypeMap[col++] = ind;
    }
  }
  /*printf("%d %d %d %d\n", numCols, colToTypeMap[0], colToTypeMap[1], colToTypeMap[2]);*/

  /* Output the header line */
  fprintf(fout, "%d   edited positions", npatch);
  if (mod->obj[0].extra[IOBJ_EXSIZE - 1] > 0)
    for (col = 0; col < numCols; col++)
      fprintf(fout, "  %d", valueIDs[colToTypeMap[col]]);
  fprintf(fout, "\n");

  for (ob = 0; ob < mod->objsize; ob++) {
    listInd = 0;
    for (co = 0; co < mod->obj[ob].contsize; co++) {
      listStart = listInd;
      if (mod->obj[ob].cont[co].psize >= 2) {
        pts = mod->obj[ob].cont[co].pts;
        ix = pts[0].x + 0.5;
        iy = pts[0].y + 0.5;
        iz = pts[0].z + 0.5;
        dx = (pts[1].x - pts[0].x) / mod->pixsize;
        dy = (pts[1].y - pts[0].y) / mod->pixsize;
        dz = (pts[1].z - pts[0].z) / mod->pixsize;
        if (mod->flags & IMODF_FLIPYZ)
          fprintf(fout, "%6d %5d %5d %8.2f %8.2f %8.2f", 
                  ix, iz, iy, dx, dz, dy);
        else
          fprintf(fout, "%6d %5d %5d %8.2f %8.2f %8.2f", 
                  ix, iy, iz, dx, dy, dz);
        for (col = 0; col < numCols; col++) {
          ind = colToTypeMap[col];
          if (numValues[ind]) {
            listInd = listStart;
            value = 0.;
            istoreFindValue(mod->obj[ob].store, co, GEN_STORE_VALUE1 + 2 * ind, &value,
                            &listInd);
            fprintf(fout, format[ind], value);
          }
        }
        fprintf(fout, "\n");

      }
    }
  }
  fclose(fout);
  exit(0);
}