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

Axes.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2019 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.76 $       $Date: 2021/10/25 18:13:40 $
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] = { 0 };
00130 
00131   reset_disp_list(); // regenerate both data block and display commands
00132   sprintf(commentBuffer, "VMD Axes");
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, 1.0f, 0, 0, cmdList);
00219   txt.putdata(yText, "y", 1.0f, 1.0f, 0, 0, cmdList);
00220   txt.putdata(zText, "z", 1.0f, 1.0f, 0, 0, cmdList);
00221 
00222   // put in commands to draw pickpoints at axes endpoints
00223   ResizeArray<float> pickpointcoords;
00224   ResizeArray<int> pickpointindices;
00225   pickpointcoords.append3(origin);
00226   pickpointindices.append(0);
00227   pickpointcoords.append3(xLine);
00228   pickpointindices.append(1);
00229   pickpointcoords.append3(xLineCap);
00230   pickpointindices.append(2);
00231   pickpointcoords.append3(yLine);
00232   pickpointindices.append(3);
00233   pickpointcoords.append3(yLineCap);
00234   pickpointindices.append(4);
00235   pickpointcoords.append3(zLine);
00236   pickpointindices.append(5);
00237   pickpointcoords.append3(zLineCap);
00238   pickpointindices.append(6);
00239   DispCmdPickPointArray pickPointArray;
00240   pickPointArray.putdata(pickpointindices.num(), &pickpointindices[0],
00241                          &pickpointcoords[0], cmdList);
00242 
00243   need_create_cmdlist = FALSE;
00244 }
00245 
00246 
00247 // set axes display mode; return success
00248 int Axes::location(int ap) {
00249   axesPos = ap;
00250   movedAxes = FALSE;
00251   if (ap == NO_AXES) {
00252     off();
00253   } else {
00254     on();
00255     Aspect = -1.0;
00256   }
00257 
00258   return TRUE;
00259 }
00260 
00261 
00262 // return descripton of location
00263 char *Axes::loc_description(int ap) {
00264   return axesloc[ap];
00265 }
00266 
00267 
00269 
00270 // routine to prepare the displayable object; must set the origin properly
00271 void Axes::prepare() {
00272   float asp, xpos, ypos;
00273   float poscale = 0.95f;
00274 
00275   // recreate command list if needed
00276   if (need_create_cmdlist)
00277     create_cmdlist();
00278 
00279   if (axesPos == NO_AXES || movedAxes) {
00280     return; // don't update/modify the Axes position
00281   }
00282   
00283   if ((asp = disp->aspect()) != Aspect) {
00284     // move the axes to their proper position
00285     switch (axesPos) {
00286       case AXES_LOWERLEFT:
00287         xpos = -poscale * asp;
00288         ypos = -poscale;
00289         break;
00290       case AXES_LOWERRIGHT:
00291         xpos = poscale * asp;
00292         ypos = -poscale;
00293         break;
00294       case AXES_UPPERLEFT:
00295         xpos = -poscale * asp;
00296         ypos = poscale;
00297         break;
00298       case AXES_UPPERRIGHT:
00299         xpos = poscale * asp;
00300         ypos = poscale;
00301         break;
00302       default:
00303         xpos = ypos = 0.0;
00304     }
00305 
00306     // update the current transformation
00307     Aspect = asp;
00308     glob_trans_on();
00309     set_glob_trans(xpos, ypos, 0.0);
00310     glob_trans_off();
00311   }
00312 }
00313 
00314 
00316 
00317 
00318 //
00319 // When the Axes are picked and moved with a pointer, this is used to move
00320 // the Axes to a new position.  The initial pointer position is remembered,
00321 // and subsequent motions add a global translation to the Axes.
00322 // This is done for any pick mode, and any button.  This is only done if the
00323 // item selected is actually the specific Axes object.
00324 //
00325 
00326 // called when a pick moves:
00327 //   args = display to use, obj picked, button, mode, tag, dim, pos
00328 // For 2D version: x & y are 0 ... 1, represent 'relative, scaled' coords.
00329 // For 3D version: x,y,z are transformed position of pointer
00330 // For the Axes, when they are selected and the pointer moves, we wish
00331 // to move the axes as well.
00332 void Axes::pick_move(PickMode *, DisplayDevice *d, 
00333                      int, int dim, const float *pos) {
00334   float moveAxesOrigPos[3], newAxesOrigPos[3];
00335   const float *newpos;
00336 
00337   // calculate amount to translate axes
00338   if (dim == 2) {
00339     tm.multpoint3d(origin, moveAxesOrigPos);
00340     d->find_3D_from_2D(moveAxesOrigPos, pos, newAxesOrigPos);
00341     newpos = newAxesOrigPos;
00342   } else {
00343     newpos = pos;
00344   }
00345   
00346   // apply transformation
00347   glob_trans_on();
00348   set_glob_trans(newpos[0], newpos[1], newpos[2]);
00349   glob_trans_off();
00350   
00351   movedAxes = TRUE;
00352 }
00353 

Generated on Fri Apr 19 02:43:45 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002