Math_Vector
[ class tree: Math_Vector ] [ index: Math_Vector ] [ all elements ]

Source for file VectorOp.php

Documentation is available at VectorOp.php

  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: VectorOp.php 304045 2010-10-05 00:16:53Z clockwerx $
  20. //
  21.  
  22. /**
  23.  * Vector operation class.
  24.  * A static class implementing methods to operate on Vector objects.
  25.  * Originally this class was part of NumPHP (Numeric PHP package)
  26.  *
  27.  * @author    Jesus M. Castagnetto <jmcastagnetto@php.net>
  28.  * @version    1.0
  29.  * @access    public
  30.  * @package    Math_Vector
  31.  */
  32. class Math_VectorOp {
  33.  
  34.     /**
  35.      * Checks if object is of Math_Vector class (or a subclass of Math_Vector)
  36.      *
  37.      * @access    public
  38.      * @param    object    $obj 
  39.      * @return    boolean    true on success
  40.      */
  41.     function isVector($obj/*{{{*/
  42.     {
  43.         if (function_exists("is_a"))
  44.             return (is_object($obj&& is_a($obj"Math_Vector"));
  45.         else
  46.             return (is_object($obj&& (strtolower(get_class($obj)) == "math_vector" || 
  47.                         is_subclass_of($obj"Math_Vector")));
  48.     }/*}}}*/
  49.  
  50.     /**
  51.      * Checks if object is of Math_Vector2 class (or a subclass of Math_Vector2)
  52.      *
  53.      * @access    public
  54.      * @param    object    $obj 
  55.      * @return    boolean    true on success
  56.      */
  57.     function isVector2($obj/*{{{*/
  58.     {
  59.         if (function_exists("is_a"))
  60.             return (is_object($obj&& is_a($obj"Math_Vector2"));
  61.         else
  62.             return (is_object($obj&& (strtolower(get_class($obj)) == "math_vector2" ||
  63.                 is_subclass_of($obj"Math_Vector2")));
  64.     }/*}}}*/
  65.  
  66.     /**
  67.      * Checks if object is of Math_Vector3 class (or a subclass of Math_Vector3)
  68.      *
  69.      * @access    public
  70.      * @param    object    $obj 
  71.      * @return    boolean    true on success
  72.      */
  73.     function isVector3($obj/*{{{*/
  74.     {
  75.         if (function_exists("is_a"))
  76.             return (is_object($obj&& is_a($obj"Math_Vector3"));
  77.         else
  78.             return (is_object($obj&& (strtolower(get_class($obj)) == "math_vector3" ||
  79.                     is_subclass_of($obj"Math_Vector3")) );
  80.     }/*}}}*/
  81.  
  82.     /**
  83.      * Creates a vector of a given size in which all elements have the same value
  84.      *
  85.      * @access    public
  86.      * @param    int    $size    vector size
  87.      * @param    numeric    $value    value to assign to the elements
  88.      * @return    object    if ($size == 2) Math_Vector2 elseif ($size == 3) Math_Vector3 else Math_Vector
  89.      */
  90.     function create ($size$value/*{{{*/
  91.     {
  92.         if ($size == 2)
  93.             $VClass "Math_Vector2";
  94.         elseif ($size == 3)
  95.             $VClass "Math_Vector3";
  96.         else
  97.             $VClass "Math_Vector";
  98.         return new $VClass(Math_VectorOp::_fill(0$size$value));
  99.     }/*}}}*/
  100.  
  101.     /**
  102.      * Creates a zero-filled vector of the given size
  103.      *
  104.      * @access    public
  105.      * @param    int    $size    vector size
  106.      * @return    object    if ($size == 2) Math_Vector2 elseif ($size == 3) Math_Vector3 else Math_Vector
  107.      *
  108.      * @see    create()
  109.      */
  110.     function createZero ($size
  111.     {
  112.         return Math_VectorOp::create ($size0);
  113.     }
  114.  
  115.     /**
  116.      * Creates a one-filled vector of the given size
  117.      *
  118.      * @access    public
  119.      * @param    int    $size    vector size
  120.      * @return    object    if ($size == 2) Math_Vector2 elseif ($size == 3) Math_Vector3 else Math_Vector
  121.      *
  122.      * @see    create()
  123.      */
  124.     function createOne ($size/*{{{*/
  125.     {
  126.         return Math_VectorOp::create ($size1);
  127.     }/*}}}*/
  128.  
  129.  
  130.     /**
  131.      * Creates a basis vector of the given size
  132.      * A basis vector of size n, has n - 1 elements equal to 0
  133.      * and one element equal to 1
  134.      *
  135.      * @access    public
  136.      * @param    int    $size    vector size
  137.      * @param    int    $index    element to be set at 1
  138.      * @return    object    if ($size == 2) Math_Vector2 elseif ($size == 3) Math_Vector3 else Math_Vector, on error PEAR_Error
  139.      *
  140.      * @see    createZero()
  141.      */
  142.     function createBasis ($size$index/*{{{*/
  143.     {
  144.         if ($index >= $size)
  145.             return PEAR::raiseError("Incorrect index for size: $index >= $size");
  146.         $v Math_VectorOp::createZero($size);
  147.         $res =$v->set($index1);
  148.         if (PEAR::isError($res))
  149.             return $res;
  150.         else
  151.             return $v;
  152.     }/*}}}*/
  153.  
  154.     /**
  155.      * Vector addition
  156.      * v + w = <v1 + w1, v2 + w2, ..., vk + wk>
  157.      *
  158.      * @access    public
  159.      * @param    object    Math_Vector (or subclass)    $v1
  160.      * @param    object    Math_Vector (or subclass)    $v2
  161.      * @return    object    Math_Vector (or subclass) on success, PEAR_Error otherwise
  162.      *
  163.      * @see     isVector()
  164.      */
  165.     function add ($v1$v2/*{{{*/
  166.     {
  167.         if (Math_VectorOp::isVector($v1&& Math_VectorOp::isVector($v2)) {
  168.             $n $v1->size();
  169.             if ($v2->size(!= $n)
  170.                 return PEAR::raiseError("Vectors must of the same size");
  171.             for ($i=0; $i $n$i++)
  172.                 $arr[$i$v1->get($i$v2->get($i);
  173.             return new Math_Vector($arr);
  174.         else {
  175.             return PEAR::raiseError("V1 and V2 must be Math_Vector objects");
  176.         }
  177.     }/*}}}*/
  178.  
  179.     /**
  180.      * Vector substraction
  181.      * v - w = <v1 - w1, v2 - w2, ..., vk - wk>
  182.      *
  183.      * @access    public
  184.      * @param    object    Math_Vector (or subclass)    $v1
  185.      * @param    object    Math_Vector (or subclass)    $v2
  186.      * @return    object    Math_Vector (or subclass) on success, PEAR_Error otherwise
  187.      *
  188.      * @see     isVector()
  189.      */
  190.     function substract ($v1$v2/*{{{*/
  191.     {
  192.         if (Math_VectorOp::isVector($v1&& Math_VectorOp::isVector($v2)) {
  193.             $n $v1->size();
  194.             if ($v2->size(!= $n)
  195.                 return PEAR::raiseError("Vectors must of the same size");
  196.             for ($i=0; $i $n$i++)
  197.                 $arr[$i$v1->get($i$v2->get($i);
  198.             return new Math_Vector($arr);
  199.         else {
  200.             return PEAR::raiseError("V1 and V2 must be Math_Vector objects");
  201.         }
  202.     }/*}}}*/
  203.  
  204.     /**
  205.      * Vector multiplication
  206.      * v * w = <v1 * w1, v2 * w2, ..., vk * wk>
  207.      *
  208.      * @access    public
  209.      * @param    object    Math_Vector (or subclass)    $v1
  210.      * @param    object    Math_Vector (or subclass)    $v2
  211.      * @return    object    Math_Vector (or subclass) on success, PEAR_Error otherwise
  212.      *
  213.      * @see     isVector()
  214.      */
  215.     function multiply ($v1$v2/*{{{*/
  216.     {
  217.         if (Math_VectorOp::isVector($v1&& Math_VectorOp::isVector($v2)) {
  218.             $n $v1->size();
  219.             if ($v2->size(!= $n)
  220.                 return PEAR::raiseError("Vectors must of the same size");
  221.             for ($i=0; $i $n$i++)
  222.                 $arr[$i$v1->get($i$v2->get($i);
  223.             return new Math_Vector($arr);
  224.         else {
  225.             return PEAR::raiseError("V1 and V2 must be Math_Vector objects");
  226.         }
  227.     }/*}}}*/
  228.  
  229.     /**
  230.      * Vector scaling
  231.      * f * w = <f * w1, f * w2, ..., f * wk>
  232.      *
  233.      * @access    public
  234.      * @param    numeric    $f    scaling factor
  235.      * @param    object    Math_Vector (or subclass)    $v
  236.      * @return    object    Math_Vector (or subclass) on success, PEAR_Error otherwise
  237.      *
  238.      * @see     isVector()
  239.      */
  240.     function scale ($f$v/*{{{*/
  241.     {
  242.         if (is_numeric($f&& Math_VectorOp::isVector($v)) {
  243.             $n $v->size();
  244.             for ($i=0; $i $n$i++)
  245.                 $arr[$i$v->get($i$f;
  246.             return new Math_Vector($arr);
  247.         else {
  248.             return PEAR::raiseError("Requires a numeric factor and a Math_Vector object");
  249.         }
  250.     }/*}}}*/
  251.  
  252.     /**
  253.      * Vector division
  254.      * v / w = <v1 / w1, v2 / w2, ..., vk / wk>
  255.      *
  256.      * @access    public
  257.      * @param    object    Math_Vector (or subclass)    $v1
  258.      * @param    object    Math_Vector (or subclass)    $v2
  259.      * @return    object    Math_Vector (or subclass) on success, PEAR_Error otherwise
  260.      *
  261.      * @see     isVector()
  262.      */
  263.     function divide ($v1$v2/*{{{*/
  264.     {
  265.         if (Math_VectorOp::isVector($v1&& Math_VectorOp::isVector($v2)) {
  266.             $n $v1->size();
  267.             if ($v2->size(!= $n)
  268.                 return PEAR::raiseError("Vectors must of the same size");
  269.             for ($i=0; $i $n$i++{
  270.                 $d $v2->get($i);
  271.                 if ($d == 0)
  272.                     return PEAR::raiseError("Division by zero: Element $i in V2 is zero");
  273.                 $arr[$i$v1->get($i$d;
  274.             }
  275.             return new Math_Vector($arr);
  276.         else {
  277.             return PEAR::raiseError("V1 and V2 must be Math_Vector objects");
  278.         }
  279.     }/*}}}*/
  280.  
  281.     /**
  282.      * Vector dot product = v . w = |v| |w| cos(theta)
  283.      *
  284.      * @access    public
  285.      * @param    object    Math_Vector2 or MathVector3 (or subclass)    $v1
  286.      * @param    object    Math_Vector2 or MathVector3 (or subclass)    $v2
  287.      * @return    mixed    the dot product (float) on success, a PEAR_Error object otherwise
  288.      *
  289.      * @see     isVector2()
  290.      * @see        isVector3()
  291.      */
  292.     function dotProduct ($v1$v2)/*{{{*/
  293.     {
  294.         if (Math_VectorOp::isVector2($v1&& Math_VectorOp::isVector2($v2))
  295.             return $v1->getX($v2->getX(+
  296.                      $v1->getY($v2->getY() );
  297.         elseif (Math_VectorOp::isVector3($v1&& Math_VectorOp::isVector3($v2))
  298.             return $v1->getX($v2->getX(+
  299.                      $v1->getY($v2->getY(+
  300.                      $v1->getZ($v2->getZ() );
  301.         else
  302.             return PEAR::raiseError("Vectors must be both of the same type");
  303.     }/*}}}*/
  304.  
  305.     /**
  306.      * Vector cross product = v x w
  307.      *
  308.      * @access    public
  309.      * @param    object    Math_Vector3 (or subclass)    $v1
  310.      * @param    object    Math_Vector3 (or subclass)    $v2
  311.      * @return    object    the cross product vector (Math_Vector3) on success, a PEAR_Error object otherwise
  312.      *
  313.      * @see        isVector3()
  314.      */
  315.     function crossProduct ($v1$v2)
  316.     {
  317.         if (Math_VectorOp::isVector3($v1&& Math_VectorOp::isVector3($v2)) {
  318.             $arr[0$v1->getY($v2->getZ($v1->getZ($v2->getY();
  319.             $arr[1$v1->getZ($v2->getX($v1->getX($v2->getZ();
  320.             $arr[2$v1->getX($v2->getY($v1->getY($v2->getX();
  321.             return new Math_Vector3($arr);
  322.         else {
  323.             return PEAR::raiseError("Vectors must be both of the same type");
  324.         }
  325.     }
  326.  
  327.     /**
  328.      * Vector triple scalar product =  v1 . (v2 x v3)
  329.      *
  330.      * @access    public
  331.      * @param    object    Math_Vector3 (or subclass)    $v1
  332.      * @param    object    Math_Vector3 (or subclass)    $v2
  333.      * @param    object    Math_Vector3 (or subclass)    $v3
  334.      * @return    mixed    the triple scalar product (float) on success, a PEAR_Error object otherwise
  335.      *
  336.      * @see        isVector3()
  337.      * @see        dotProduct()
  338.      * @see        crossProduct()
  339.      */
  340.     function tripleScalarProduct ($v1$v2$v3/*{{{*/
  341.     {
  342.         if (Math_VectorOp::isVector3($v1)
  343.             && Math_VectorOp::isVector3($v2)
  344.             && Math_VectorOp::isVector3($v3))
  345.             return Math_VectorOp::dotProduct($v1,Math_VectorOp::crossProduct($v2$v3));
  346.         else
  347.             return PEAR_Error("All three vectors must be of the same type");
  348.     }/*}}}*/
  349.  
  350.     /**
  351.      * Angle between vectors, using the equation: v . w = |v| |w| cos(theta)
  352.      *
  353.      * @access    public
  354.      * @param    object    Math_Vector2 or MathVector3 (or subclass)    $v1
  355.      * @param    object    Math_Vector2 or MathVector3 (or subclass)    $v2
  356.      * @return    mixed    the angle between vectors (float, in radians) on success, a PEAR_Error object otherwise
  357.      *
  358.      * @see     isVector2()
  359.      * @see        isVector3()
  360.      * @see        dotProduct()
  361.      */
  362.     function angleBetween($v1$v2/*{{{*/
  363.     {
  364.         if ( (Math_VectorOp::isVector2($v1&& Math_VectorOp::isVector2($v2)) || 
  365.              (Math_VectorOp::isVector3($v1&& Math_VectorOp::isVector3($v2)) ) {
  366.             $v1->normalize();
  367.             $v2->normalize();
  368.             return acosMath_VectorOp::dotProduct($v1,$v2) );
  369.         else {
  370.             return PEAR::raiseError("Vectors must be both of the same type");
  371.         }
  372.     }/*}}}*/
  373.  
  374.     /**
  375.      * To generate an array of a given size filled with a single value
  376.      * If available uses array_fill()
  377.      * 
  378.      * @access    private
  379.      * @param    int    $index    starting index
  380.      * @param    int    $size    size of the array
  381.      * @param    numeric    $value    value to use for filling the array
  382.      * @return    array 
  383.      */
  384.     function _fill($index$size$value)/*{{{*/
  385.     {
  386.         if (function_exists("array_fill"))
  387.             return array_fill($index$size$value);
  388.  
  389.         for ($i=$index$i ($index $size)$i++)
  390.             $arr[$i$value;
  391.         return $arr;
  392.     }/*}}}*/
  393. }
  394.  
  395.  
  396. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.