Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r830 - trunk/StandAlone


Chronological Thread 
  • From: abe@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r830 - trunk/StandAlone
  • Date: Fri, 6 Jan 2006 13:48:45 -0700 (MST)

Author: abe
Date: Fri Jan  6 13:48:45 2006
New Revision: 830

Modified:
   trunk/StandAlone/v3c1_tools.cc
Log:

Added option to scale the vertices of a .v3c1 file.

M    v3c1_tools.cc


Modified: trunk/StandAlone/v3c1_tools.cc
==============================================================================
--- trunk/StandAlone/v3c1_tools.cc      (original)
+++ trunk/StandAlone/v3c1_tools.cc      Fri Jan  6 13:48:45 2006
@@ -41,6 +41,7 @@
 // v3c1 format.
 void v3c1_info( const char *file_name );
 void v3c1_clip( const char *file_name, const Point &plane_point, const 
Vector &plane_normal );
+void v3c1_scale( const char *file_name );
 
 FILE *out_file = 0;
 FILE *normal_out_file = 0;
@@ -158,7 +159,7 @@
   float v[3];
 };
 
-enum CommandType { NONE, CONVERT_VN, CONVERT_OBJ, CONVERT_M, INFO, CUT };
+enum CommandType { NONE, CONVERT_VN, CONVERT_OBJ, CONVERT_M, INFO, CUT, 
SCALE };
 
 
 int main( int argc, char **argv ) {
@@ -198,6 +199,10 @@
       input_scale[0] = atof(argv[++i]);
       input_scale[1] = atof(argv[++i]);
       input_scale[2] = atof(argv[++i]);
+
+      if (command == NONE) {
+        command = SCALE;
+      }
     }    
                else if (arg == "-out") {
                        output_file_name = argv[++i];
@@ -235,18 +240,20 @@
        
        // Check that both were found.
        if ((command == NONE) || (file_list_begin == 0)) {
-               printf( "Usage: v2v3c1 -vn3 -in <tanker paths...> -out 
<outfile.v3c1>\n"
-                                               "       v2v3c1 -obj -in 
<file1.obj ...>  [-out <outfile.v3c1>]\n"
+               printf( "Usage: v3c1_tools -vn3 -in <tanker paths...> -out 
<outfile.v3c1>\n"
+                                               "       v3c1_tools -obj -in 
<file1.obj ...>  [-out <outfile.v3c1>]\n"
             "          Related options:\n"
             "          -scale <x> <y> <z> -- Scale the model before 
writting\n"
             "          -normals           -- Output a vertex normal file.\n"
             "          Outfile optional uses group names otherwise.\n"
-            "       v2v3c1 -m   -in <file1.m ...>  -color <r> <g> <b> [-out 
<outfile.v3c1>]\n"
+            "       v3c1_tools -m   -in <file1.m ...>  -color <r> <g> <b> 
[-out <outfile.v3c1>]\n"
             "          Hughes Hoppe .m file format conversion.\n"
-                       "       v2v3c1 -info <infile.v3c1 ...>\n"
+                       "       v3c1_tools -info <infile.v3c1 ...>\n"
             "          Computes bounds and number of triangles, colors.\n"
-                                               "       v2v3c1 -clip <px> 
<py> <pz> <nx> <ny> <nz>\n"
-            "          Apply a clipping plane to the input files given a 
point and normal.\n");
+                                               "       v3c1_tools -clip <px> 
<py> <pz> <nx> <ny> <nz>\n"
+            "          Apply a clipping plane to the input files given a 
point and normal.\n"
+            "       v3c1_tools -scale <x> <y> <z>\n"
+            "          Scale the input v3c1 file and write to the output 
file.\n");
                return 1;
        }
        
@@ -357,6 +364,21 @@
                
                fclose(out_file);
        break;
+       case SCALE:
+               // Open the output file.
+               if ((out_file = fopen( output_file_name, "w" )) == 0) {
+                       perror( "Could not open output file." );
+                       return 1;
+               }
+               
+               for (int i=file_list_begin;i<file_list_end;++i) {
+                       v3c1_scale( argv[i] );
+               }
+               
+               std::cout << "All: faces: " << total_faces << "  "<< 
bounds[0] << " " << bounds[1] << std::endl;
+               
+               fclose(out_file);
+  break;
        };
 }
 
@@ -976,5 +998,130 @@
   if (use_groups) {
     fclose( out_group_file );
   }
+}
+
+void v3c1_scale( const char *file_name ) {
+
+  // Open the data file.
+  FILE *in_file = fopen( file_name, "r" );
+  if (!in_file) {
+    perror( "Could not open data file." );
+    return;
+  }
+  
+  // Determine the file size.
+  fseek( in_file, 0, SEEK_END );
+  long file_size = ftell( in_file );
+  fseek( in_file, 0, SEEK_SET );
+  
+  // Determine the total number of faces in this file.
+  long file_faces = file_size / (sizeof(v3c1_face));
+  
+  // Output number of faces.
+  if ((file_size % sizeof(v3c1_face)) != 0) {
+    printf( "%s size %d is not a multiple of sizeof(v3c1_face) (%d)\n",
+           file_name, (int)file_size, (int)sizeof(v3c1_face) );
+  }
+  else {
+    printf( "%s faces: %d\n", file_name, (int)file_faces );
+  }
+  
+  
////////////////////////////////////////////////////////////////////////////
+  // Determine normal file name.
+  char normal_file_name[64];
+  sprintf( normal_file_name, "%s.nor", file_name );
+  printf( "Checking for normal file: %s\n", normal_file_name );
+  
+  // Check to see if normal data is available.
+  FILE *normal_file = 0;
+  bool use_normals = true;
+  if ((normal_file = fopen( normal_file_name, "r" )) == 0) {
+    perror( "Could not find normal file.");
+    use_normals = false;
+  }
+       
+  // Check to make sure there are the correct number of normals.
+  int file_normals = 0;
+  if (use_normals) {
+    fseek( normal_file, 0, SEEK_END );
+    file_size = ftell( normal_file );
+    fseek( normal_file, 0, SEEK_SET );
+    
+    // Check for incomplete data.
+    if ((file_size % (sizeof(Vectorf)*3)) != 0) {
+      printf( "%s size %d is not a multiple of 3*sizeof(Vectorf) (%d)\n",
+             normal_file_name, (int)file_size, (int)(3*sizeof(Vectorf)) );
+    }
+         
+    // Check for matching number of normals.
+    file_normals = file_size / sizeof(Vectorf);
+    if ((file_faces*3) != file_normals) {
+      printf( "%s contains %d normals which does not match %d faces *3\n",
+             normal_file_name, file_normals, (int)file_faces );
+    }
+  }
+       
+  ////////////////////////////////////////////////////////////////////
+  // Read in the data.
+  
+  // Faces
+  out_buffer.resize( file_faces );
+  fread( &out_buffer[0], sizeof(v3c1_face), file_faces, in_file );     
+  fclose( in_file );
+  
+  // Normals
+  FILE *out_normal_file;
+  if (use_normals) {
+    // Read in the normals.
+    normal_buffer.resize( file_faces );
+    fread( &normal_buffer[0], sizeof(vertex_normals), file_faces, 
normal_file );
+    fclose( normal_file );
+    
+    sprintf( normal_file_name, "%s.nor", output_file_name );
+    
+    // Open normal output file.
+    if ((out_normal_file = fopen( normal_file_name, "w" )) == 0) {
+      perror( "Could not open normal output file for writing\n" );
+    }
+  }
+
+  /////////////////////////////////////////////////////////////////////
+  // Copy the faces into the output buffer.
+  for (int i=0;i<file_faces;++i) {
+    
+    // Compare the face to the cutting plane.
+    for (int f=0;f<3;++f) {
+      
+      // Check to see if this vertex is on the back side of the plane.
+      Point vertex( out_buffer[i].vertex(f)[0] *= input_scale[0], 
+                    out_buffer[i].vertex(f)[1] *= input_scale[1], 
+                    out_buffer[i].vertex(f)[2] *= input_scale[2] );
+      
+      bounds.extendByPoint( vertex );
+    }
+    
+    // Output the face.
+    ++total_faces;
+    fwrite( &out_buffer[i], sizeof( v3c1_face ), 1, out_file );
+      
+    if (use_normals) {
+
+      Vector scale( input_scale[0], input_scale[1], input_scale[2] );
+      
+      // Scale the normal.
+      normal_buffer[i].normal[0] *= scale;
+      normal_buffer[i].normal[1] *= scale;
+      normal_buffer[i].normal[2] *= scale;
+      
+      // Output the normals.
+      fwrite( &normal_buffer[i], sizeof(vertex_normals), 1, out_normal_file 
);
+    }
+      
+  }
+
+  if (use_normals) {
+    fclose( out_normal_file );
+  }
+
 }
 




  • [MANTA] r830 - trunk/StandAlone, abe, 01/06/2006

Archive powered by MHonArc 2.6.16.

Top of page