Ply-vn.cpp code

  1.#include<stdio.h>
  2.#include<stdlib.h>
  3.#include "Cvector3.h"
  4. 
  5.int main(int argc, char **argv)
  6.{
  7.   FILE *in, *out;
  8.   unsigned char cccc[1];
  9.   char junk[1024], junk1[7], junk2[7];
 10.   int i, j, k, l, e, nv, ntri, four[4], *fc;
 11.   Cvector3 *vert, *norm, a, b, c;
 12.   float  point[50], *ncnt;
 13. 
 14.   in=fopen(argv[1],"r");
 15.   if( in == NULL ) { fprintf(stderr,"File %s does not exist.\n",argv[1]); exit(-1); }
 16.//--------------------------------------------------------------------
 17.// Read in PLY dataset
 18.//--------------------------------------------------------------------
 19.//
 20.// Read in Header
 21.//
 22.   printf("Reading %s\n",argv[1]);
 23.   fgets(junk,1024,in);                 // ply
 24.   fgets(junk,1024,in);                 // format ascii | binary
 25.   fgets(junk,1024,in);                 // comment line
 26.   fgets(junk,1024,in);                 // comment line
 27.   fgets(junk,1024,in);                 // number of vertices
 28.   sscanf(junk,"%s %s %d",&junk1,&junk2,&nv);
 29.   fgets(junk,1024,in);                 // x
 30.   fgets(junk,1024,in);                 // y
 31.   fgets(junk,1024,in);                 // z
 32.   fgets(junk,1024,in);                 // element face
 33.   sscanf(junk,"%s %s %d",&junk1,&junk2,&ntri);
 34.   fgets(junk,1024,in);                 // property list
 35.   fgets(junk,1024,in);                 // end_header
 36.   printf("Number of Vertices  = %d \n",nv);
 37.   printf("Number of Triangles = %d \n",ntri);
 38.//
 39.// Allocate memory for arrays & initialize to zero
 40.//
 41.   vert = new Cvector3 [nv];
 42.   norm = new Cvector3 [nv];
 43.   ncnt = new float [nv];
 44.   fc= (int   *) calloc(ntri*3,sizeof(int));
 45.//
 46.// Read in 3 floating points (x,y,z)
 47.//
 48.   for (i=0;i<nv;i++) {
 49.     e = fread(point,sizeof(float),3,in);
 50.     vert[i].x = point[0];
 51.     vert[i].y = point[1];
 52.     vert[i].z = point[2];
 53.   }
 54.//
 55.// Read in Faces
 56.//
 57.   for(i=0;i<ntri;i++) {
 58.     fread(cccc,sizeof(unsigned char),1,in);
 59.     fread(&four[0],sizeof(int),3,in);
 60.     fc[3*i+0] = four[0];
 61.     fc[3*i+1] = four[1];
 62.     fc[3*i+2] = four[2];
 63.   }
 64.   fclose(in);
 65.//--------------------------------------------------------------------
 66.// Compute Vertex Normals
 67.//--------------------------------------------------------------------
 68.//
 69.// Step 1. Loop thru faces; calculate face normals; add face normals to vertex normals
 70.//
 71.   for(i=0;i<ntri;i++) {
 72.     l = fc[3*i+0];
 73.     k = fc[3*i+1];
 74.     j = fc[3*i+2];
 75.     a = vert[j] - vert[k];
 76.     b = vert[l] - vert[k];
 77.     c.Cross(a,b);
 78.     norm[j] += c;
 79.     norm[k] += c;
 80.     norm[l] += c;
 81.     ncnt[j] += 1.0;
 82.     ncnt[k] += 1.0;
 83.     ncnt[l] += 1.0;
 84.   }
 85.//
 86.// Step 2. Loop thru vertex normals and normalize
 87.//
 88.   for(i=0;i<nv;i++) {
 89.     norm[i] = norm[i]/ncnt[i];
 90.   }
 91.   for(i=0;i<nv;i++) {
 92.     norm[i].Normalize();
 93.   }
 94.//--------------------------------------------------------------------
 95.// Write out PLY dataset
 96.//--------------------------------------------------------------------
 97.   out=fopen(argv[2],"w");
 98.   if( out == NULL ) { fprintf(stderr,"File %s does not exist.\n",argv[2]); exit(-1); }
 99.//
100.// This writes out in PLY format, which ParaView can read in.
101.// But you cannot see the smoothing performed by the vertex normals.
102.//
103.   printf("Writing PLY file.\n");
104. 
105.   fprintf(out,"ply\n");
106.   fprintf(out,"format binary_little_endian 1.0\n");
107.   fprintf(out,"comment paul adams\n");
108.   fprintf(out,"comment erdc msrc svc\n");
109.   fprintf(out,"element vertex %d\n",nv);
110.   fprintf(out,"property float x\n");
111.   fprintf(out,"property float y\n");
112.   fprintf(out,"property float z\n");
113.   fprintf(out,"property float nx\n");
114.   fprintf(out,"property float ny\n");
115.   fprintf(out,"property float nz\n");
116.   fprintf(out,"element face %d\n",ntri);
117.   fprintf(out,"property list uchar int vertex_indices\n");
118.   fprintf(out,"end_header\n");
119.   for(i=0;i<nv;i++) {
120.     point[0] = vert[i].x;
121.     point[1] = vert[i].y;
122.     point[2] = vert[i].z;
123.     point[3] = norm[i].x;
124.     point[4] = norm[i].y;
125.     point[5] = norm[i].z;
126.     e = fwrite(point,sizeof(float),6,out);
127.   }
128.   for(i=0;i<ntri;i++) {
129.     fwrite(cccc,sizeof(unsigned char),1,out);
130.     fwrite(&fc[3*i],sizeof(int),3,out);
131.   }
132.   fclose(out);
133.//--------------------------------------------------------------------
134.// Clean Up
135.//--------------------------------------------------------------------
136.   delete [] vert;
137.   delete [] norm;
138.   free(fc);
139.   return 0;
140.}

Back to PLY_with_Vertex_Normals