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

Axes.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: Axes.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.63 $       $Date: 2008/03/27 19:36:35 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * A Displayable object which consists of a set of axes, which may be
00020  * drawn anywhere on the screen, at any size.
00021  *
00022  ***************************************************************************/
00023 
00024 #include <string.h>
00025 #include <stdio.h>
00026 #include "Axes.h"
00027 #include "DisplayDevice.h"
00028 #include "DispCmds.h"
00029 #include "utilities.h"
00030 #include "Scene.h"
00031 
00032 // radius for the cylinders that make up the axes
00033 #define AXESRAD         0.12f
00034 #define AXESCAPRAD      0.25f
00035 #define AXESRODLEN      0.85f
00036 #define AXESTXTLEN      1.35f
00037 #define AXESRES         32
00038 #define AXESSPHRES      6
00039 
00040 // default colors to use
00041 #define AXESXCOL        REGRED
00042 #define AXESYCOL        REGGREEN
00043 #define AXESZCOL        REGBLUE
00044 #define AXESOCOL        REGCYAN
00045 #define AXESTCOL        REGWHITE
00046 
00047 
00048 // string descriptions of axes locations
00049 static char *axesloc[Axes::AXESPOS_TOTAL] = {
00050   (char *) "Off",        (char *) "Origin",    (char *) "LowerLeft", 
00051   (char *) "LowerRight", (char *) "UpperLeft", (char *) "UpperRight"};
00052 
00053 
00055 Axes::Axes(DisplayDevice *d, Displayable *par) 
00056 : Displayable(par), disp(d) {
00057   colorCat = (-1); // indicate we don't yet have a color object to use
00058   need_create_cmdlist = TRUE;
00059   movedAxes = FALSE;
00060 
00061   // initialize coordinates for axes lines
00062   origin[0] = yLine[0] = zLine[0] = 0.0;  
00063   xLineCap[1] = xLineCap[2] = 0.0;
00064   xLine[0] = AXESRODLEN; xLineCap[0] = 1.0;
00065 
00066   origin[1] = xLine[1] = zLine[1] = 0.0;  
00067   yLineCap[0] = yLineCap[2] = 0.0;
00068   yLine[1] = AXESRODLEN; yLineCap[1] = 1.0;
00069 
00070   origin[2] = xLine[2] = yLine[2] = 0.0;
00071   zLineCap[0] = zLineCap[1] = 0.0;
00072   zLine[2] = AXESRODLEN; zLineCap[2] = 1.0;
00073 
00074   xText[0] = AXESTXTLEN * xLine[0]; xText[1] = xLine[1]; xText[2] = xLine[2];
00075   yText[1] = AXESTXTLEN * yLine[1]; yText[0] = yLine[0]; yText[2] = yLine[2];
00076   zText[2] = AXESTXTLEN * zLine[2]; zText[0] = zLine[0]; zText[1] = zLine[1];
00077   
00078   // Displayable characteristics
00079   rot_on();
00080   glob_trans_off();
00081   cent_trans_off();
00082   
00083   // set scaling factor to a small amount
00084   scale_on();
00085   set_scale(0.25);
00086   scale_off();
00087   
00088   // put axes in lower left corner by default
00089   axesPos = AXES_LOWERLEFT;
00090   Aspect = (-1.0);
00091 
00092   colorCat = scene->add_color_category("Axes");
00093   
00094   // add components, and their default colors
00095   scene->add_color_item(colorCat, "X", AXESXCOL);
00096   scene->add_color_item(colorCat, "Y", AXESYCOL);
00097   scene->add_color_item(colorCat, "Z", AXESZCOL);
00098   scene->add_color_item(colorCat, "Origin", AXESOCOL);
00099   scene->add_color_item(colorCat, "Labels", AXESTCOL);
00100 
00101   do_color_changed(colorCat);
00102 }
00103 
00104 
00106 Axes::~Axes(void) {
00107   // do nothing
00108 }
00109 
00110 
00112 
00113 void Axes::do_color_changed(int clr) {
00114   if(clr == colorCat) {
00115     usecolors[0] = scene->category_item_value(colorCat, "X");
00116     usecolors[1] = scene->category_item_value(colorCat, "Y");
00117     usecolors[2] = scene->category_item_value(colorCat, "Z");
00118     usecolors[3] = scene->category_item_value(colorCat, "Origin");
00119     usecolors[4] = scene->category_item_value(colorCat, "Labels");
00120 
00121     // color changed for us, recreate command list
00122     need_create_cmdlist = TRUE;
00123   }
00124 }
00125 
00127 
00128 // create the drawing command list
00129 void Axes::create_cmdlist(void) {
00130   char commentBuffer[128];
00131 
00132   reset_disp_list(); // regenerate both data block and display commands
00133   sprintf(commentBuffer, "VMD: Starting axes output.");
00134   cmdCommentX.putdata(commentBuffer, cmdList);
00135 
00136 #if defined(USELINEAXES)
00137   DispCmdLine cmdline;
00138   float arrow1[3], arrow2[3];
00139 
00140   // turn on material characteristics
00141   append(DMATERIALOFF);
00142 
00143   // Draw axes as lines of width 2, faster for wimpy machines...
00144   DispCmdLineWidth cmdwidth;
00145   cmdwidth.putdata(2, cmdList);
00146 
00147   // x-axis
00148   xcol.putdata(usecolors[0], cmdList);
00149   cmdline.putdata(origin, xLineCap, cmdList);
00150   arrow1[0] = xLine[0];
00151   arrow1[1] = xLine[1] - AXESCAPRAD/sqrt(2.0f);
00152   arrow1[2] = xLine[2] + AXESCAPRAD/sqrt(2.0f);
00153   arrow2[0] = xLine[0];
00154   arrow2[1] = xLine[1] + AXESCAPRAD/sqrt(2.0f);
00155   arrow2[2] = xLine[2] - AXESCAPRAD/sqrt(2.0f);
00156   cmdline.putdata(xLineCap, arrow1, cmdList);
00157   cmdline.putdata(xLineCap, arrow2, cmdList);
00158 
00159   // y-axis
00160   xcol.putdata(usecolors[1], cmdList);
00161   cmdline.putdata(origin, yLineCap, cmdList);
00162   arrow1[0] = yLine[0] + AXESCAPRAD/sqrt(2.0f);
00163   arrow1[1] = yLine[1];
00164   arrow1[2] = yLine[2] - AXESCAPRAD/sqrt(2.0f);
00165   arrow2[0] = yLine[0] - AXESCAPRAD/sqrt(2.0f);
00166   arrow2[1] = yLine[1];
00167   arrow2[2] = yLine[2] + AXESCAPRAD/sqrt(2.0f);
00168   cmdline.putdata(yLineCap, arrow1, cmdList);
00169   cmdline.putdata(yLineCap, arrow2, cmdList);
00170 
00171   // z-axis
00172   xcol.putdata(usecolors[2], cmdList);
00173   cmdline.putdata(origin, zLineCap, cmdList);
00174   arrow1[0] = zLine[0] - AXESCAPRAD/sqrt(2.0f);
00175   arrow1[1] = zLine[1] + AXESCAPRAD/sqrt(2.0f);
00176   arrow1[2] = zLine[2];
00177   arrow2[0] = zLine[0] + AXESCAPRAD/sqrt(2.0f);
00178   arrow2[1] = zLine[1] - AXESCAPRAD/sqrt(2.0f);
00179   arrow2[2] = zLine[2];
00180   cmdline.putdata(zLineCap, arrow1, cmdList);
00181   cmdline.putdata(zLineCap, arrow2, cmdList);
00182 #else
00183   // Draw axes as solid cylinders/cones, for faster machines...
00184   // turn on material characteristics
00185   append(DMATERIALON);
00186 
00187   // set sphere type and resolution
00188   sphres.putdata(AXESSPHRES,  cmdList);
00189   sphtype.putdata(SOLIDSPHERE, cmdList);
00190 
00191   // put in commands to draw lines
00192   // x-axis
00193   xcol.putdata(usecolors[0], cmdList);
00194   xcyl.putdata(origin, xLine, AXESRAD, AXESRES, 0, cmdList);
00195   xcap.putdata(xLine, xLineCap, AXESCAPRAD, 0, AXESRES, cmdList);
00196 
00197   // y-axis
00198   xcol.putdata(usecolors[1], cmdList);
00199   xcyl.putdata(origin, yLine, AXESRAD, AXESRES, 0, cmdList);
00200   xcap.putdata(yLine, yLineCap, AXESCAPRAD, 0, AXESRES, cmdList);
00201 
00202   // z-axis
00203   xcol.putdata(usecolors[2], cmdList);
00204   xcyl.putdata(origin, zLine, AXESRAD, AXESRES, 0, cmdList);
00205   xcap.putdata(zLine, zLineCap, AXESCAPRAD, 0, AXESRES, cmdList);
00206 
00207   // put in commands to draw sphere at origin
00208   xcol.putdata(usecolors[3], cmdList);
00209   sph.putdata(origin, AXESRAD, cmdList);
00210 
00211   // turn off material characteristics
00212   append(DMATERIALOFF);
00213 #endif
00214 
00215   // put in commands to label the axes
00216   xcol.putdata(usecolors[4], cmdList);
00217   txt.putdata(xText, "x", cmdList);
00218   txt.putdata(yText, "y", cmdList);
00219   txt.putdata(zText, "z", cmdList);
00220 
00221   // put in commands to draw pickpoints at axes endpoints
00222   pickPoint.putdata(origin,   0, cmdList);
00223   pickPoint.putdata(xLine,    1, cmdList);
00224   pickPoint.putdata(xLineCap, 2, cmdList);
00225   pickPoint.putdata(yLine,    3, cmdList);
00226   pickPoint.putdata(yLineCap, 4, cmdList);
00227   pickPoint.putdata(zLine,    5, cmdList);
00228   pickPoint.putdata(zLineCap, 6, cmdList);
00229 
00230   // done drawing axes
00231   sprintf(commentBuffer, "VMD: Done with axes.");
00232   cmdCommentX.putdata(commentBuffer, cmdList);
00233 
00234   need_create_cmdlist = FALSE;
00235 }
00236 
00237 
00238 // set axes display mode; return success
00239 int Axes::location(int ap) {
00240   axesPos = ap;
00241   movedAxes = FALSE;
00242   if(ap == NO_AXES) {
00243     off();
00244   } else {
00245     on();
00246     Aspect = (-1.0);
00247   }
00248 
00249   return TRUE;
00250 }
00251 
00252 
00253 // return descripton of location
00254 char *Axes::loc_description(int ap) {
00255   return axesloc[ap];
00256 }
00257 
00258 
00260 
00261 // routine to prepare the displayable object; must set the origin properly
00262 void Axes::prepare() {
00263   float asp, xpos, ypos;
00264   float poscale = 0.95f;
00265 
00266   // recreate command list if needed
00267   if (need_create_cmdlist)
00268     create_cmdlist();
00269 
00270   if (axesPos == NO_AXES || movedAxes) {  // don't do nuthin'
00271     return;
00272   }
00273   
00274   if((asp = disp->aspect()) != Aspect) {
00275     // move the axes to their proper position
00276     if(axesPos == AXES_LOWERLEFT) {
00277       xpos = -poscale * asp;
00278       ypos = -poscale;
00279     } else if(axesPos == AXES_LOWERRIGHT) {
00280       xpos = poscale * asp;
00281       ypos = -poscale;
00282     } else if(axesPos == AXES_UPPERLEFT) {
00283       xpos = -poscale * asp;
00284       ypos = poscale;
00285     } else if(axesPos == AXES_UPPERRIGHT) {
00286       xpos = poscale * asp;
00287       ypos = poscale;
00288     } else {
00289       xpos = ypos = 0.0;
00290     }
00291 
00292     // update the current transformation
00293     Aspect = asp;
00294     glob_trans_on();
00295     set_glob_trans(xpos, ypos, 0.0);
00296     glob_trans_off();
00297   }
00298 }
00299 
00300 
00302 
00303 
00304 //
00305 // When the Axes are picked and moved with a pointer, this is used to move
00306 // the Axes to a new position.  The initial pointer position is remembered,
00307 // and subsequent motions add a global translation to the Axes.
00308 // This is done for any pick mode, and any button.  This is only done if the
00309 // item selected is actually the specific Axes object.
00310 //
00311 
00312 // called when a pick moves:
00313 //      args = display to use, obj picked, button, mode, tag, dim, pos
00314 // For 2D version: x & y are 0 ... 1, represent 'relative, scaled' coords.
00315 // For 3D version: x,y,z are transformed position of pointer
00316 // For the Axes, when they are selected and the pointer moves, we wish
00317 // to move the axes as well.
00318 void Axes::pick_move(PickMode *, DisplayDevice *d, 
00319                                 int, int dim, const float *pos) {
00320   float moveAxesOrigPos[3], newAxesOrigPos[3];
00321   const float *newpos;
00322 
00323   // calculate amount to translate axes
00324   if(dim == 2) {
00325     tm.multpoint3d(origin, moveAxesOrigPos);
00326     d->find_3D_from_2D(moveAxesOrigPos, pos, newAxesOrigPos);
00327     newpos = newAxesOrigPos;
00328   } else {
00329     newpos = pos;
00330   }
00331   
00332   // apply transformation
00333   glob_trans_on();
00334   set_glob_trans(newpos[0], newpos[1], newpos[2]);
00335   glob_trans_off();
00336   
00337   movedAxes = TRUE;
00338 }
00339 

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