Beispiel #1
0
int		main(int argc, char *argv[])
{
  int		ok = 1,
  		option,
  		usage = 0,
		dim = 2,
		nVtx = 0,
		markerSz = 1;
  FILE		*fP = NULL;
  char		*iFile,
  		*oObjFile;
  const char	*errMsg;
  WlzVertexType	vType;
  WlzVertexP	vtx;
  WlzObject	*obj = NULL;
  WlzMarkerType	markerType = WLZ_MARKER_SPHERE;
  WlzErrorNum	errNum = WLZ_ERR_NONE;
  static char   optList[] = "h23o:s:t:";
  const char    iFileDef[] = "-",
  		oObjFileDef[] = "-";

  opterr = 0;
  vtx.v = NULL;
  iFile = (char *)iFileDef;
  oObjFile = (char *)oObjFileDef;
  while(ok && ((option = getopt(argc, argv, optList)) != EOF))
  {
    switch(option)
    {
      case '2':
        dim = 2;
	break;
      case '3':
        dim = 3;
	break;
      case 'o':
        oObjFile = optarg;
	break;
      case 's':
	if(sscanf(optarg, "%d", &markerSz) != 1)
	{
	  usage = 1;
	}
        break;
      case 't':
	markerType = WlzStringToMarkerType(optarg, &errNum);
	if(errNum != WLZ_ERR_NONE)
	{
	  usage = 1;
	}
        break;
      case 'h': /* FALLTROUGH */
      default:
	usage = 1;
	break;
    }
  }
  if(!usage)
  {
    if((oObjFile == NULL) || (*oObjFile == '\0') ||
       (iFile == NULL) || (*iFile == '\0'))
    {
      usage = 1;
    }
  }
  if(!usage && (optind < argc))
  {
    if((optind + 1) != argc)
    {
      usage = 1;
    }
    else
    {
      iFile = *(argv + optind);
    }
  }
  ok = !usage;
  if(ok)
  {
    if(strcmp(iFile, "-"))
    {
      if((fP = fopen(iFile, "r")) == NULL)
      {
        ok = 0;
	errNum = WLZ_ERR_READ_EOF;
	(void )WlzStringFromErrorNum(errNum, &errMsg);
	(void )fprintf(stderr,
		       "%s: failed to open vertices file %s (%s).\n",
		       *argv, iFile, errMsg);
      }
    }
    else
    {
      fP = stdin;
    }
  }
  if(ok)
  {
    vtx = WlzMTDReadVtxArray(fP, dim, &nVtx, &vType, &errNum);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
      		     "%s: failed to read vertices from file %s (%s).\n",
		     *argv, iFile, errMsg);
    }
  }
  if(fP)
  {
    if(strcmp(iFile, "-"))
    {
      fclose(fP);
    }
    fP = NULL;
  }
  if(ok)
  {
    obj = WlzMakeMarkers(vType, nVtx, vtx, markerType, markerSz,
    			      &errNum);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
      		     "%s: Failed to create domain from vertices (%s).\n",
      		     argv[0],
		     errMsg);
    }
  }
  if(ok)
  {
    if((fP = (strcmp(oObjFile, "-")? fopen(oObjFile, "w"): stdout)) == NULL)
    {
      ok = 0;
      (void )fprintf(stderr,
		     "%s: Failed to open output file %s.\n",
		     argv[0], oObjFile);
    }
  }
  if(ok)
  {
    errNum = WlzWriteObj(fP, obj);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: Failed to write output object, %s.\n",
		     argv[0], errMsg);
    }
  }
  if(fP && strcmp(oObjFile, "-"))
  {
    (void )fclose(fP);
  }
  AlcFree(vtx.v);
  (void )WlzFreeObj(obj);
  if(usage)
  {
    (void )fprintf(stderr,
    "Usage: %s [-h] [-o<output file>] [-2] [-3] [-s #] [-t <type>]\n"
    "                         [<input file>]\n"
    "  -h  Output this usage message.\n"
    "  -o  Output file name, default is the standard output.\n"
    "  -2  Vertices and output domain are 2D.\n"
    "  -3  Vertices and output domain are 3D.\n"
    "  -s  Marker size.\n"
    "  -t  Marker type: Only valid type is 'sphere'.\n"
    "Reads a list of 2D or 3D vertices either from the given file or the\n"
    "standard input (default). These vertices are then used to create\n"
    "either a 2D or 3D domain with a marker at the position of each vertex.\n",
    argv[0]);
  }
  return(!ok);
}
int		main(int argc, char *argv[])
{
  int		ok = 1,
  		option,
  		usage = 0,
		markerSep = 16,
		markerSz = 4;
  FILE		*fP = NULL;
  char		*iFile,
  		*oFile;
  const char	*errMsg;
  WlzObject	*iObj = NULL,
  		*oObj = NULL;
  WlzMarkerType	markerType = WLZ_MARKER_SPHERE;
  WlzErrorNum	errNum = WLZ_ERR_NONE;
  static char   optList[] = "ho:s:t:z:";
  const char    defFile[] = "-";

  opterr = 0;
  iFile = (char *)defFile;
  oFile = (char *)defFile;
  while(ok && ((option = getopt(argc, argv, optList)) != EOF))
  {
    switch(option)
    {
      case 'o':
        oFile = optarg;
	break;
      case 's':
	if(sscanf(optarg, "%d", &markerSep) != 1)
	{
	  usage = 1;
	}
        break;
      case 't':
	markerType = WlzStringToMarkerType(optarg, &errNum);
	if(errNum != WLZ_ERR_NONE)
	{
	  usage = 1;
	}
        break;
      case 'z':
	if(sscanf(optarg, "%d", &markerSz) != 1)
	{
	  usage = 1;
	}
        break;
      case 'h': /* FALLTROUGH */
      default:
	usage = 1;
	break;
    }
  }
  if(usage == 0)
  {
    if((oFile == NULL) || (*oFile == '\0') ||
       (iFile == NULL) || (*iFile == '\0'))
    {
      usage = 1;
    }
  }
  if((usage == 0) && (optind < argc))
  {
    if((optind + 1) != argc)
    {
      usage = 1;
    }
    else
    {
      iFile = *(argv + optind);
    }
  }
  ok = !usage;
  if(ok)
  {
    if((fP = (strcmp(iFile, "-")? fopen(iFile, "r"): stdin)) == NULL)
    {
      ok = 0;
      (void )fprintf(stderr,
		     "%s: Failed to open input file %s.\n",
		     argv[0], iFile);
    }
  }
  if(ok)
  {
    iObj = WlzReadObj(fP, &errNum);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: Failed to read input domain object, %s.\n",
		     argv[0], errMsg);
    }
  }
  if(fP && strcmp(iFile, "-"))
  {
    (void )fclose(fP);
  }
  if(ok)
  {
    oObj = WlzMarkerLattice(iObj, markerType, markerSz, markerSep, &errNum);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
      		     "%s: Failed to create domain from vertices (%s).\n",
      		     argv[0],
		     errMsg);
    }
  }
  if(ok)
  {
    if((fP = (strcmp(oFile, "-")? fopen(oFile, "w"): stdout)) == NULL)
    {
      ok = 0;
      (void )fprintf(stderr,
		     "%s: Failed to open output file %s.\n",
		     argv[0], oFile);
    }
  }
  if(ok)
  {
    errNum = WlzWriteObj(fP, oObj);
    if(errNum != WLZ_ERR_NONE)
    {
      ok = 0;
      (void )WlzStringFromErrorNum(errNum, &errMsg);
      (void )fprintf(stderr,
                     "%s: Failed to write output object, %s.\n",
		     argv[0], errMsg);
    }
  }
  if(fP && strcmp(oFile, "-"))
  {
    (void )fclose(fP);
  }
  (void )WlzFreeObj(iObj);
  (void )WlzFreeObj(oObj);
  if(usage)
  {
    (void )fprintf(stderr,
    "Usage: %s [-h] [-o<output>] [-s #] [-t <type>] [-z #]\n"
    "                                  [<input>]\n"
    "Version: %s\n"
    "Options:\n"
    "  -h  Output this usage message.\n"
    "  -o  Output file name, default is the standard output.\n"
    "  -s  Marker separation.\n"
    "  -t  Marker type: Only valid type is 'sphere'.\n"
    "  -z  Marker size.\n"
    "Reads either a 2D or 3D spatial domain object from a given file or\n"
    "the standard input (default). A new domainis then constructed which\n"
    "covers the given domain with a lattice of markers.\n",
    argv[0],
    WlzVersion());
  }
  return(!ok);
}