home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Atom.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.3 KB  |  185 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Atom.php,v 1.4 2003/05/13 01:18:17 jmcastagnetto Exp $
  20. //
  21.  
  22. require_once "Science/Chemistry.php";
  23.  
  24. /**
  25.  * Base class representing an Atom
  26.  *
  27.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  28.  * @version 1.0
  29.  * @access  public
  30.  * @package Science_Chemistry
  31.  */
  32. class Science_Chemistry_Atom {
  33.  
  34.     /**
  35.      * Element symbol
  36.      *
  37.      * @var     string
  38.      * @access  private
  39.      *
  40.      * @see     getElement();
  41.      */
  42.     var $element="";
  43.  
  44.     /**
  45.      * Science_Chemistry_Coordinates object
  46.      *
  47.      * @var     object  Science_Chemistry_Coordinates
  48.      * @access  private
  49.      *
  50.      * @see     getCoordinates();
  51.      */
  52.     var $xyz;
  53.  
  54.     /**
  55.      * Constructor for the class, requires the element symbol
  56.      * and an optional array of coordinates
  57.      *
  58.      * @param   string  $element    chemical symbol
  59.      * @param   optional array   $coords     array of coordinates (x, y, z)
  60.      * @access  public
  61.      * @return  object  Science_Chemistry_Atom
  62.      *
  63.      * @see     setCoordinates()
  64.      */
  65.     function Science_Chemistry_Atom($element, $coords="") {
  66.         if ($element && ereg("[[:alpha:]]{1,2}", $element))
  67.             $this->element = $element;
  68.         else
  69.             return null;
  70.         if (is_array($coords) && count($coords) == 3)
  71.             if (!$this->xyz = new Science_Chemistry_Coordinates($coords))
  72.                 return null;
  73.     }
  74.  
  75.     /**
  76.      * Sets the coordinates for the atom object
  77.      *
  78.      * @param   array   $coords     array of coordinates (x, y, z)
  79.      * @return  boolean
  80.      * @access  public
  81.      */
  82.     function setCoordinates($coords) {
  83.         $this->xyz = new Science_Chemistry_Coordinates($coords);
  84.         return (is_object($this->xyz) && !empty($this->xyz));
  85.     }
  86.  
  87.     /**
  88.      * Returns the chemical symbol for the atom
  89.      *
  90.      * @return  string
  91.      * @access  public
  92.      *
  93.      * @see     $element;
  94.      */
  95.     function getElement() {
  96.         return $this->element;
  97.     }
  98.  
  99.     /**
  100.      * Returns the coordinates object for the atom
  101.      *
  102.      * @return  object  Science_Chemistry_Coordinates
  103.      * @access  public
  104.      *
  105.      * @see     $xyz;
  106.      */
  107.     function getCoordinates() {
  108.         return $this->xyz;
  109.     }
  110.  
  111.     /**
  112.      * Calculates the cartesian distance from this atom
  113.      * instance to another
  114.      *
  115.      * @param   object  Science_Chemistry_Atom $atom2
  116.      * @return  float   distance
  117.      * @access  public
  118.      */
  119.     function distance($atom2) {
  120.         if (!empty($this->xyz) && Science_Chemistry_Coordinates::areCoordinates($this->xyz) 
  121.             && Science_Chemistry_Atom::isAtom($atom2))
  122.             return $this->xyz->distance($atom2->xyz);
  123.         else
  124.             return -1.0;
  125.     }
  126.  
  127.     /**
  128.      * Checks if the object is an instance of Science_Chemistry_Atom
  129.      *
  130.      * @param   object  Science_Chemistry_Atom $obj
  131.      * @return  boolean
  132.      * @access  public
  133.      */
  134.     function isAtom($obj) {
  135.         return  (is_object($obj) && 
  136.                  (strtolower(get_class($obj)) == strtolower("Science_Chemistry_Atom")
  137.                   || is_subclass_of($obj, strtolower("Science_Chemistry_Atom")))
  138.                 );
  139.     }
  140.  
  141.     /**
  142.      * Returns a string representation of the Science_Chemistry_Atom object
  143.      * Alias of toXYZ()
  144.      *
  145.      * @return  string
  146.      * @access  public
  147.      * @see toXYZ()
  148.      */
  149.     function toString() {
  150.         return $this->toXYZ();
  151.     }
  152.  
  153.     /**
  154.      * Returns a XYZ representation of the Science_Chemistry_Atom object
  155.      *
  156.      * @return  string
  157.      * @access  public
  158.      * @see toString()
  159.      */
  160.     function toXYZ() {
  161.         if ($this->element && $this->xyz)
  162.             return sprintf("%2s",$this->element)." ".$this->xyz->toString();
  163.     }
  164.  
  165.     /**
  166.      * Returns a CML representation of the Science_Chemistry_Atom object
  167.      * Accepts an optional id
  168.      *
  169.      * @param   optional    string  $id
  170.      * @return  string
  171.      * @access  public
  172.      */
  173.     function toCML($id=1) {
  174.         $out = "   <atom title=\"atom\" id=\"$id\">\n";
  175.         $out .= "    <string title=\"name\">".$this->element."</string>\n";
  176.         $out .= "    ".$this->xyz->toCML();
  177.         $out .= "   </atom>\n";
  178.         return $out;
  179.     }
  180.  
  181. } // end of class Science_Chemistry_Atom
  182.  
  183. // vim: expandtab: ts=4: sw=4
  184. ?>
  185.