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

Generated on Wed May 16 01:49:01 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002