1<?php 2 3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 4 5/** 6 * 3d Library 7 * 8 * PHP versions 5 9 * 10 * LICENSE: 11 * This library is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU Lesser General Public 13 * License as published by the Free Software Foundation; either 14 * version 2.1 of the License, or (at your option) any later version. 15 * 16 * This library is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * Lesser General Public License for more details. 20 * 21 * You should have received a copy of the GNU Lesser General Public 22 * License along with this library; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 * 25 * @category Image 26 * @package Image_3D 27 * @author Kore Nordmann <3d@kore-nordmann.de> 28 * @copyright 1997-2005 Kore Nordmann 29 * @license http://www.gnu.org/licenses/lgpl.txt lgpl 2.1 30 * @version CVS: $Id$ 31 * @link http://pear.php.net/package/PackageName 32 * @since File available since Release 0.1.0 33 */ 34 35require_once('Image/3D/Coordinate.php'); 36 37/** 38 * Image_3D_Vector 39 * 40 * @category Image 41 * @package Image_3D 42 * @author Kore Nordmann <3d@kore-nordmann.de> 43 * @copyright 1997-2005 Kore Nordmann 44 * @license http://www.gnu.org/licenses/lgpl.txt lgpl 2.1 45 * @version Release: @package_version@ 46 * @link http://pear.php.net/package/PackageName 47 * @since Class available since Release 0.1.0 48 */ 49class Image_3D_Vector extends Image_3D_Coordinate { 50 51 protected $_length; 52 53 public function getAngle(Image_3D_Vector $vector) 54 { 55 $length = $vector->length() * $this->length(); 56 if ($length < 0.0001) return 1; 57 58 return abs(acos($this->scalar($vector) / $length) / M_PI - .5) * 2; 59 } 60 61 public function getSide(Image_3D_Vector $vector) { 62// $vector->unify(); 63// $this->unify(); 64 return $this->scalar($vector); 65 } 66 67 public function unify() { 68 if ($this->length() == 0) return false; 69 if ($this->_length == 1) return $this; 70 71 $this->_x /= $this->_length; 72 $this->_y /= $this->_length; 73 $this->_z /= $this->_length; 74 $this->_length = 1; 75 return $this; 76 } 77 78 public function length() { 79 if (empty($this->_length)) { 80 $this->_length = sqrt(pow($this->_x, 2) + pow($this->_y, 2) + pow($this->_z, 2)); 81 } 82 return $this->_length; 83 } 84 85 public function add(Image_3D_Coordinate $vector) 86 { 87 $this->_x += $vector->getX(); 88 $this->_y += $vector->getY(); 89 $this->_z += $vector->getZ(); 90 $this->_length = null; 91 return $this; 92 } 93 94 public function sub(Image_3D_Coordinate $vector) 95 { 96 $this->_x -= $vector->getX(); 97 $this->_y -= $vector->getY(); 98 $this->_z -= $vector->getZ(); 99 $this->_length = null; 100 return $this; 101 } 102 103 public function multiply($scalar) { 104 if ($scalar instanceof Image_3D_Vector) return $this->scalar($scalar); 105 106 $this->_x *= $scalar; 107 $this->_y *= $scalar; 108 $this->_z *= $scalar; 109 $this->_length = null; 110 return $this; 111 } 112 113 public function scalar(Image_3D_Coordinate $vector) { 114 return (($this->_x * $vector->getX()) + 115 ($this->_y * $vector->getY()) + 116 ($this->_z * $vector->getZ())); 117 } 118 119 public function crossProduct(Image_3D_Coordinate $vector) { 120 return new Image_3D_Vector( 121 $this->getY() * $vector->getZ() - $this->getZ() * $vector->getY(), 122 $this->getZ() * $vector->getX() - $this->getX() * $vector->getZ(), 123 $this->getX() * $vector->getY() - $this->getY() * $vector->getX() 124 ); 125 } 126} 127 128?> 129