Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ArtDisplayDevice.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: ArtDisplayDevice.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.29 $       $Date: 2008/03/27 19:36:34 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Writes to the ART raytracer.  This is available from gondwana.ecr.mu.oz.au
00019  * as part of the vort package.  To see the output I suggest:
00020  *   art plot.scn 1000 1000
00021  *   vort2ppm plot.pix > plot.ppm
00022  *   fromppm plot.ppm plot.rgb
00023  *   ipaste plot.rgb
00024  *
00025  ***************************************************************************/
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <math.h>
00029 #include "ArtDisplayDevice.h"
00030 #include "Matrix4.h"
00031 #include "DispCmds.h"
00032 #include "Inform.h"
00033 #include "utilities.h"
00034 
00035 #define DEFAULT_RADIUS 0.002f
00036 #define DASH_LENGTH 0.02f
00037 
00038 // Be careful when you modify the coordinates.  To make things view the
00039 // right way, I have to rotate everything around the (x,y,z) = (1,1,1)
00040 // vector so that x->z, y->x, and z->y
00041 
00042 #define ORDER(x,y,z) -z, -x, y
00043 //#define ORDER(x,y,z) x,y,z
00044 
00046 
00047 // constructor ... initialize some variables
00048 ArtDisplayDevice::ArtDisplayDevice() 
00049 : FileRenderer("ART", "plot.scn", "art %s 500 650"){ }
00050 
00051 //destructor
00052 ArtDisplayDevice::~ArtDisplayDevice(void) { }
00053 
00055 
00056 // draw a point
00057 void ArtDisplayDevice::point(float * spdata) {
00058   float vec[3];
00059 
00060   // transform the world coordinates
00061   (transMat.top()).multpoint3d(spdata, vec);
00062    
00063   // draw the sphere
00064   fprintf(outfile, "sphere {\ncolour %f,%f,%f\n",
00065           matData[colorIndex][0],
00066           matData[colorIndex][1],
00067           matData[colorIndex][2]);
00068 
00069   fprintf(outfile, "radius %f\n", float(lineWidth) * DEFAULT_RADIUS);
00070   fprintf(outfile, "center (%f,%f,%f)\n}\n", ORDER(vec[0], vec[1], vec[2]));
00071 }
00072 
00073 // draw a sphere
00074 void ArtDisplayDevice::sphere(float * spdata) {
00075   float vec[3];
00076   float radius;
00077     
00078   // transform the world coordinates
00079   (transMat.top()).multpoint3d(spdata, vec);
00080   radius = scale_radius(spdata[3]);
00081    
00082   // draw the sphere
00083   fprintf(outfile, "sphere {\ncolour %f,%f,%f\n",
00084           matData[colorIndex][0],
00085           matData[colorIndex][1],
00086           matData[colorIndex][2]);
00087 
00088   fprintf(outfile, "radius %f\n", radius);
00089   fprintf(outfile, "center (%f,%f,%f)\n}\n", ORDER(vec[0], vec[1], vec[2]));
00090 }
00091 
00092 
00093 // draw a line (cylinder) from a to b
00094 void ArtDisplayDevice::line(float *a, float *b) {
00095     int i, j, test;
00096     float dirvec[3], unitdirvec[3];
00097     float from[3], to[3], tmp1[3], tmp2[3];
00098     float len;
00099     
00100     if(lineStyle == ::SOLIDLINE ) {
00101         // transform the world coordinates
00102         (transMat.top()).multpoint3d(a, from);
00103         (transMat.top()).multpoint3d(b, to);
00104     
00105         // draw the cylinder
00106         fprintf(outfile, "cylinder {\n");
00107         fprintf(outfile, "colour %f,%f,%f\n",
00108           matData[colorIndex][0],
00109           matData[colorIndex][1],
00110           matData[colorIndex][2]);
00111         fprintf(outfile, "center(%f,%f,%f)\n", 
00112                 ORDER(from[0], from[1], from[2])); // first point
00113         fprintf(outfile, "center(%f,%f,%f)\n", 
00114                 ORDER(to[0], to[1], to[2])); // second point
00115         fprintf(outfile, "radius %f\n}\n", 
00116                 float(lineWidth)*DEFAULT_RADIUS); // radius
00117         
00118     } else if (lineStyle == ::DASHEDLINE ) {
00119         
00120          // transform the world coordinates
00121         (transMat.top()).multpoint3d(a, tmp1);
00122         (transMat.top()).multpoint3d(b, tmp2);
00123 
00124         // how to create a dashed line
00125         for(i=0;i<3;i++) {
00126             dirvec[i] = tmp2[i] - tmp1[i];  // vector from a to b
00127         }
00128         len = sqrtf( dirvec[0]*dirvec[0] + dirvec[1]*dirvec[1] + dirvec[2]*dirvec[2] );
00129         for(i=0;i<3;i++) {
00130             unitdirvec[i] = dirvec[i] / sqrtf(len);  // unit vector pointing from a to b
00131         }
00132            
00133         test = 1;
00134         i = 0;
00135 
00136         while( test == 1 ) {
00137             for(j=0;j<3;j++) {
00138                 from[j] = tmp1[j] + (2*i)*DASH_LENGTH*unitdirvec[j];
00139                 to[j] = tmp1[j] + (2*i + 1)*DASH_LENGTH*unitdirvec[j];
00140             }
00141             if( fabsf(tmp1[0] - to[0]) >= fabsf(dirvec[0]) ) {
00142                 for(j=0;j<3;j++) {
00143                     to[j] = tmp2[j];
00144                 }
00145                 test = 0;
00146             }
00147     
00148             // draw the cylinder
00149             fprintf(outfile, "cylinder {\n");
00150             fprintf(outfile, "colour %f,%f,%f\n",
00151               matData[colorIndex][0],
00152               matData[colorIndex][1],
00153               matData[colorIndex][2]);
00154 
00155             // first point
00156             fprintf(outfile, "center(%f,%f,%f)\n", 
00157                     ORDER(from[0], from[1], from[2]));
00158             // second point
00159             fprintf(outfile, "center(%f,%f,%f)\n", 
00160                     ORDER(to[0], to[1], to[2])); 
00161             // radius
00162             fprintf(outfile, "radius %f\n}\n", 
00163                     float(lineWidth)*DEFAULT_RADIUS); 
00164 
00165             i++;
00166         }
00167     } else {
00168         msgErr << "ArtDisplayDevice: Unknown line style " << lineStyle << sendmsg;
00169     }
00170 
00171 }
00172 
00173 // draw a cylinder
00174 void ArtDisplayDevice::cylinder(float *a, float *b, float r,int /*filled*/) {
00175 
00176   float vec1[3], vec2[3];
00177   
00178   // transform the world coordinates
00179   (transMat.top()).multpoint3d(a, vec1);
00180   (transMat.top()).multpoint3d(b, vec2);
00181     
00182   // draw the cylinder
00183   fprintf(outfile, "cylinder {\n");
00184   fprintf(outfile, "colour %f,%f,%f\n",
00185           matData[colorIndex][0],
00186           matData[colorIndex][1],
00187           matData[colorIndex][2]);
00188   // first point
00189   fprintf(outfile, "center(%f,%f,%f)\n", 
00190           ORDER(vec1[0], vec1[1], vec1[2]));
00191   // second point
00192   fprintf(outfile, "center(%f,%f,%f)\n", 
00193           ORDER(vec2[0], vec2[1], vec2[2])); 
00194   // radius
00195   fprintf(outfile, "radius %f\n}\n", 
00196           scale_radius(r));
00197 }
00198 
00199 // draw a cone
00200 void ArtDisplayDevice::cone(float *a, float *b, float r) {
00201 
00202   float vec1[3], vec2[3];
00203   
00204   // transform the world coordinates
00205   (transMat.top()).multpoint3d(a, vec1);
00206   (transMat.top()).multpoint3d(b, vec2);
00207     
00208   fprintf(outfile, "cone {\n");
00209   fprintf(outfile, "colour %f,%f,%f\n",
00210           matData[colorIndex][0],
00211           matData[colorIndex][1],
00212           matData[colorIndex][2]);
00213   // second point
00214   fprintf(outfile, "vertex(%f,%f,%f)\n", 
00215           ORDER(vec2[0], vec2[1], vec2[2])); 
00216   // first point
00217   fprintf(outfile, "center(%f,%f,%f)\n", 
00218           ORDER(vec1[0], vec1[1], vec1[2]));
00219   // radius
00220   fprintf(outfile, "radius %f\n}\n", scale_radius(r));
00221 }
00222 
00223 // draw a triangle
00224 void ArtDisplayDevice::triangle(const float *a, const float *b, const float *c, const float *n1, 
00225 const float *n2, const float *n3) {
00226 
00227   float vec1[3], vec2[3], vec3[3];
00228   float nor1[3], nor2[3], nor3[3];
00229   
00230   // transform the world coordinates
00231   (transMat.top()).multpoint3d(a, vec1);
00232   (transMat.top()).multpoint3d(b, vec2);
00233   (transMat.top()).multpoint3d(c, vec3);
00234 
00235   // and the normals
00236   (transMat.top()).multnorm3d(n1, nor1);
00237   (transMat.top()).multnorm3d(n2, nor2);
00238   (transMat.top()).multnorm3d(n3, nor3);
00239 
00240   // draw the triangle
00241   fprintf(outfile, "polygon {\n");
00242   fprintf(outfile, "colour %f,%f,%f\n",
00243           matData[colorIndex][0],
00244           matData[colorIndex][1],
00245           matData[colorIndex][2]);
00246   
00247   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00248           ORDER(vec1[0], vec1[1], vec1[2]), // point one
00249           ORDER(nor1[0], nor1[1], nor1[2]));
00250   
00251   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00252           ORDER(vec2[0], vec2[1], vec2[2]), // point two
00253           ORDER(nor2[0], nor2[1], nor2[2]));
00254   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00255           ORDER(vec3[0], vec3[1], vec3[2]), // point three
00256           ORDER(nor3[0], nor3[1], nor3[2]));
00257   fprintf(outfile, "}\n");
00258 }
00259 
00260 // draw a square
00261 void ArtDisplayDevice::square(float *, float *a, float *b, float *c, float *d) {
00262   
00263   float vec1[3], vec2[3], vec3[3], vec4[3];
00264   
00265   // transform the world coordinates
00266   (transMat.top()).multpoint3d(a, vec1);
00267   (transMat.top()).multpoint3d(b, vec2);
00268   (transMat.top()).multpoint3d(c, vec3);
00269   (transMat.top()).multpoint3d(d, vec4);
00270 
00271   // draw the square
00272   fprintf(outfile, "polygon {\n");
00273   fprintf(outfile, "colour %f,%f,%f\n", 
00274           matData[colorIndex][0],
00275           matData[colorIndex][1],
00276           matData[colorIndex][2]);
00277 
00278   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00279           ORDER(vec1[0], vec1[1], vec1[2])); // point one
00280   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00281           ORDER(vec2[0], vec2[1], vec2[2])); // point two
00282   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00283           ORDER(vec3[0], vec3[1], vec3[2])); // point three
00284   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00285           ORDER(vec4[0], vec4[1], vec4[2])); // point four
00286   fprintf(outfile, "}\n");
00287   
00288 }
00289 
00290 void ArtDisplayDevice::comment(const char *s)
00291 {
00292   fprintf(outfile, "// %s\n", s);
00293 }
00294 
00296 
00297 // initialize the file for output
00298 void ArtDisplayDevice::write_header() {
00299 
00300     Initialized = TRUE;
00301 
00302     fprintf(outfile, "up(0, 0, 1) \n");
00303     fprintf(outfile, "lookat(-1, 0, 0, 0, 0, 0, 0)\n");
00304     fprintf(outfile, "fieldofview 45 \n");
00305     
00306 
00307     // write the light sources
00308     // The light code doesn't work because at this point I don't have the
00309     //  correct transformation matrix, so I'll leave only the one light source
00310     fprintf(outfile, "light {\n\tcolour 1, 1, 1\n"
00311             "\tlocation (-10, 0, 0)\n}\n");
00312 
00313     // set the background
00314     fprintf(outfile, "background %f, %f, %f\n", backColor[0],
00315             backColor[1], backColor[2]);
00316 
00317     // everything is plastic-like
00318     fprintf(outfile, "material 0.0, 0.75, 0.25, 20.0\n");
00319 }
00320 
00321     
00322 // clean up after yourself
00323 void ArtDisplayDevice::write_trailer() {
00324     fprintf(outfile, "//End of tokens \n");
00325     msgInfo << "Art file generation finished" << sendmsg;
00326 }
00327 

Generated on Thu Jul 24 01:26:59 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002