DistanceCmd.h 1.85 KB
Newer Older
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// DistanceCmd.h:
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
6
// $Id: DistanceCmd.h,v 1.2 2005-09-16 23:08:11 fgygi Exp $
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#ifndef DISTANCECMD_H
#define DISTANCECMD_H

#include <iostream>
#include "UserInterface.h"
#include "Sample.h"
#include <cstdlib>

class DistanceCmd : public Cmd
{
  public:

  Sample *s;

  DistanceCmd(Sample *sample) : s(sample) {};

  char *name(void) const { return "distance"; }
  char *help_msg(void) const
  {
    return 
    "\n distance\n\n"
    " syntax: distance name1 name2\n\n"
    "   The distance command prints the distance between two atoms.\n\n";
  }

  int action(int argc, char **argv)
  {
    if ( argc != 3 )
    {
      if ( ui->onpe0() )
      {
        cout << " use: distance name1 name2" << endl;
      }
      return 1;
    }
    
    string name1(argv[1]);                
    string name2(argv[2]);                
    Atom* a1 = s->atoms.findAtom(name1);  
    Atom* a2 = s->atoms.findAtom(name2);  
    if ( a1 == 0 || a2 == 0 )             
    {                                     
      // either a1 or a2 was not found
      if ( ui->onpe0() )
      {
        if ( a1 == 0 )
          cout << " <!-- DistanceCmd: atom " << name1 << " not defined -->"
               << endl;
        if ( a2 == 0 )
          cout << " <!-- DistanceCmd: atom " << name2 << " not defined -->"
               << endl;
      }
      return 1;
    }
    
    if ( ui->onpe0() )
    {
      const double d = length(a1->position()-a2->position());
      cout.setf(ios::fixed,ios::floatfield);
      cout << " <!-- distance " << name1 << "-" << name2 << ": "  
           << setprecision(3)                                    
Francois Gygi committed
69
           << d << " (a.u.) / " << 0.529177*d << " (Ang) -->" << endl;     
70 71 72 73 74
    }
    return 0;
  }
};
#endif