POV-Ray : Newsgroups : povray.general : mesh to mesh2 conversion : Re: mesh to mesh2 conversion Server Time
19 Nov 2024 05:24:12 EST (-0500)
  Re: mesh to mesh2 conversion  
From: Ian Shumsky
Date: 23 Apr 2002 15:23:35
Message: <3cc5b4b7@news.povray.org>
Hi,

Here is some sample C code I wrote a two years ago - IIRC you pass the file
name containing (a single) mesh on the command line and it writes a mesh2
object to stdout. I think it will need a little work to convert to the
latest mesh2 format, but it could be a good starting point.

If it works for you, let me know! Also, if you have any questions, I'll trya
nd answer them.

Cheers,
Ian.

http://www.outerarm.demon.co.uk/graphics/graphics.html
(About time for a spring clean)




#include <stdio.h>
#include <stdlib.h>

typedef struct _vertex
{
 char x[64];
 char y[64];
 char z[64];
} vertex;

vertex vlist[10000];
int vindex = 0;

main (int argc, char* argv[])
{
 FILE *in_file;
 FILE *out_file;
 char c;
 char string_x[64];
 char string_y[64];
 char string_z[64];
 double x,y,z;
 int i;
 int no_tri;
 vertex temp_vertex;
 int vcount;

 //
 // Open input and output files.
 //

 in_file = fopen (argv[1], "r");

 if (in_file == NULL)
 {
  printf ("ERROR: Unable to open file '%s'\n", argv[1]);
  return -1;
 }

 //
 // Scan input file for vertex vectors and store, removing duplicates.
 // Vertex vectors are in the form <x, y, z>
 //

 no_tri = 0;

 while ((c = getc(in_file)) != EOF)
 {
  if (c == '<')
  {
   //
   // We have found a vertex, so extract the 'x' part.
   //

   i = 0;

   while ((c = getc (in_file)) != ',')
   {
    string_x[i] = c;
    i++;
   }

   string_x[i] = '\0';

   //
   // Now get the 'y' part.
   //

   i = 0;

   while ((c = getc (in_file)) != ',')
   {
    string_y[i] = c;
    i++;
   }

   string_y[i] = '\0';

   //
   // Now get the 'z' part.
   //

   i = 0;

   while ((c = getc (in_file)) != '>')
   {
    string_z[i] = c;
    i++;
   }

   string_z[i] = '\0';

   //
   // increase the count of the number of triangles we have found.
   //

   no_tri++;

   //
   // We now have the x, y and z of the vertex. Convert to a vertex
   // type and check that it is not already in the vertex array. If
   // it is not, add it to the end.
   //

   strcpy (temp_vertex.x, string_x);
   strcpy (temp_vertex.y, string_y);
   strcpy (temp_vertex.z, string_z);

   if (vmatch (temp_vertex) < 0)
   {
    strcpy (vlist[vindex].x, temp_vertex.x);
    strcpy (vlist[vindex].y, temp_vertex.y);
    strcpy (vlist[vindex].z, temp_vertex.z);
    vindex++;
   }
  }

 }

 //
 // Close the input file.
 //

 close (in_file);

 //
 // Write out vertex vectors.
 //

 printf ("mesh2\n");
 printf ("{\n");
 printf ("\tvertex_vectors\n");
 printf ("\t{\n");
 printf ("\t\t%d\n", vindex);

 for (i = 0; i < vindex; i++)
 {
  printf ("\t\t<%s,%s,%s>\n", vlist[i].x, vlist[i].y, vlist[i].z);
 }

 printf ("\t}\n");

 printf ("\tface_indices\n");
 printf ("\t{\n");
 printf ("\t\t%d\n", no_tri / 3);

 //
 // Open input and output files.
 //

 in_file = fopen (argv[1], "r");

 if (in_file == NULL)
 {
  printf ("ERROR: Unable to open file input.txt\n");
  return -1;
 }

 //
 // Scan input file for triangles, find index in vertex list and write
 // out.
 //

 vcount = 0;

 while ((c = getc(in_file)) != EOF)
 {
  if (c == '<')
  {
   //
   // We have found a vertex, so extract the 'x' part.
   //

   i = 0;

   while ((c = getc (in_file)) != ',')
   {
    string_x[i] = c;
    i++;
   }

   string_x[i] = '\0';

   //
   // Now get the 'y' part.
   //

   i = 0;

   while ((c = getc (in_file)) != ',')
   {
    string_y[i] = c;
    i++;
   }

   string_y[i] = '\0';

   //
   // Now get the 'z' part.
   //

   i = 0;

   while ((c = getc (in_file)) != '>')
   {
    string_z[i] = c;
    i++;
   }

   string_z[i] = '\0';

   //
   // We now have the x, y and z of the vertex. Convert to a vertex
   // type and get its index from the array.
   //

   strcpy (temp_vertex.x, string_x);
   strcpy (temp_vertex.y, string_y);
   strcpy (temp_vertex.z, string_z);

   i = vmatch (temp_vertex);

   if (vcount == 0)
   {
    printf ("\t\t<");
   }

   printf ("%d", i);

   vcount++;

   if ((vcount == 1) || (vcount == 2))
   {
    printf (",");
   }
   else
   {
    printf (">\n");
   }

   if (vcount == 3)
   {
    vcount = 0;
   }
  }
 }

 printf ("\t}\n");
 printf ("}\n");

 //
 // Close the files.
 //

 close (in_file);
}

int vmatch (vertex temp_vertex)
{
 int i;

 for (i = 0; i < vindex; i++)
 {
  if ((strcmp (vlist[i].x, temp_vertex.x) == 0) &&
   (strcmp (vlist[i].y, temp_vertex.y) == 0) &&
   (strcmp (vlist[i].z, temp_vertex.z) == 0))
  {
   return i;
  }
 }

 return -1;
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.