
/*
**
**    Object XYZ Translator
**    Another sample file for Opticks..
**
**    By Brian Reed
**    Copyright 1989
**
**    Tested with Manx 3.6a, 32-bit mode..
*/

#include <stdio.h>

#define MAX_POINTS      2000
#define MAX_VERTICIES   6000
#define TRUE  1
#define FALSE 0

float  x[MAX_POINTS], y[MAX_POINTS], z[MAX_POINTS];
int    obj_vert[MAX_VERTICIES];


main()
{
FILE   *fopen(), *fp;
char   string[84], name[32];
float  xt, yt, zt;
int    point_count=0;
int    poly_count=0;
int    verticies=0;
int    i, ii, dummy, next_val_is_color;

  /*  Prompt for, and open input file  */
  printf("\nTranslator\n");
  printf("\nWhat is the object input file name ? ");
  scanf("%s", &string[0]);
  strcpy(&name[0], &string[0]);
  if ((fp = fopen(&name[0], "r")) == NULL) {
    printf("Couldn't open input file.\n");
    exit(0);
  }

  /*  Get file header  */
  fscanf(fp, "%s %d %d", &string[0], &dummy, &i);
  if (i != 11) {
    printf("The input file is not an Opticks v1.0 polygonal object.\n");
    exit(0);
  }
  fscanf(fp, "%d %d %d\n", &point_count, &poly_count, &verticies);
  printf("points %d  polys %d verts %d\n",
    point_count, poly_count, verticies);

  /*  Read in point list  */
  for (i=0; i<point_count; i++) {
    fscanf(fp, "%f %f %f\n", &x[i], &y[i], &z[i]);
  }
  /*  Read in verticie lists  */
  /*  Add in poly count, cause the last digit on each poly data line  */
  /*    is COLOR and not included in the verticies count.  */
  for (i=0; i<verticies+poly_count; i++) {
    fscanf(fp, "%d ", &obj_vert[i]);
  }
  fclose(fp);

  printf("\nEnter 3 translate values in 'X Y Z' format ->  ");
  scanf("%f %f %f", &xt, &yt, &zt);

  /*  Prompt, and open output file  */
  printf("Write output file (y/n)? ");
  scanf("%s", &string[0]);
  if (string[0] != 'y' && string[0] != 'Y')
    exit(0);
  strcpy(&string[0], &name[0]);      /*  Save for name in file  */
  if ((fp = fopen(&name[0], "w")) == NULL) {
    printf("Couldn't open output file.\n");
    exit(0);
  }

  /*  Write Opticks object file header  */
  fprintf(fp, "\nOBJECTv1.0\n1\n11\n");
  fprintf(fp, "%d %d %d\n", point_count, poly_count, verticies);

  /*  Write out point list  */
  for (i=0; i<point_count; i++) {
    fprintf(fp, "%f %f %f\n", x[i]+xt, y[i]+yt, z[i]+zt);
  }
  /*  Write out verticie lists  */
  next_val_is_color = FALSE;
  for (i=0; i<verticies+poly_count; i++) {
    fprintf(fp, "%d ", obj_vert[i]);
    if (next_val_is_color) {
      fprintf(fp, "\n");
      next_val_is_color = FALSE;
    }
    if (obj_vert[i] < 0)
      next_val_is_color = TRUE;
  }
  fprintf(fp, "\n");

  /*  Close output file, done  */
  fclose(fp);
  printf("Output file written.\n");
}

