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
35// {{{ Image_3D_Coordinate
36
37/**
38 * Image_3D_Coordinate
39 *
40 * Base class for coordinates eg. points in the space
41 *
42 * @category  Image
43 * @package   Image_3D
44 * @author    Kore Nordmann <3d@kore-nordmann.de>
45 * @copyright 1997-2005 Kore Nordmann
46 * @license   http://www.gnu.org/licenses/lgpl.txt lgpl 2.1
47 * @version   Release: @package_version@
48 * @link      http://pear.php.net/package/PackageName
49 * @since     Class available since Release 0.1.0
50 */
51class Image_3D_Coordinate
52{
53
54    // {{{ properties
55
56    /**
57     * X Coordiante
58     *
59     * @var float
60     */
61    protected $_x;
62
63    /**
64     * Y Coordiante
65     *
66     * @var float
67     */
68    protected $_y;
69
70    /**
71     * Z Coordiante
72     *
73     * @var float
74     */
75    protected $_z;
76
77    /**
78     * ID of the last transformation
79     *
80     * @var string
81     */
82    protected $_lastTransformation;
83
84    /**
85     * Variable saves if all relevant calculations for this point are done
86     *
87     * @var boolean
88     */
89    protected $_processed;
90
91    /**
92     * Screen coordinates (2d) of 3d-point
93     *
94     * @var array
95     */
96    protected $_screenCoordinates;
97
98    // }}}
99    // {{{ __construct()
100
101    /**
102     * Constructor for Image_3D_Coordinate
103     *
104     * Create a Point with the given coordinates
105     *
106     * @param mixed $x X Coordinate
107     * @param mixed $y Y Coordinate
108     * @param mixed $z Z Coordinate
109     *
110     * @return  Image_3D_Coordinate         Instance of Coordinate
111     */
112    public function __construct($x, $y, $z)
113    {
114        $this->_x = (float) $x;
115        $this->_y = (float) $y;
116        $this->_z = (float) $z;
117    }
118
119    // }}}
120    // {{{ transform()
121
122    /**
123     * Transform the Coordinate
124     *
125     * Use a transformationmatrix to transform (move) the point
126     *
127     * @param Image_3D_Matrix $matrix Transformationmatrix
128     * @param string          $id     Transformationid
129     *
130     * @return  void
131     */
132    public function transform(Image_3D_Matrix $matrix, $id = null)
133    {
134        // Point already transformed?
135        if (($id !== null) && ($this->_lastTransformation === $id)) {
136            return false;
137        }
138
139        $this->_lastTransformation = $id;
140
141        $point = clone($this);
142
143        $this->_x = $point->getX() * $matrix->getValue(0, 0) +
144                    $point->getY() * $matrix->getValue(1, 0) +
145                    $point->getZ() * $matrix->getValue(2, 0) +
146                    $matrix->getValue(3, 0);
147        $this->_y = $point->getX() * $matrix->getValue(0, 1) +
148                    $point->getY() * $matrix->getValue(1, 1) +
149                    $point->getZ() * $matrix->getValue(2, 1) +
150                    $matrix->getValue(3, 1);
151        $this->_z = $point->getX() * $matrix->getValue(0, 2) +
152                    $point->getY() * $matrix->getValue(1, 2) +
153                    $point->getZ() * $matrix->getValue(2, 2) +
154                    $matrix->getValue(3, 2);
155
156        $this->_screenCoordinates = null;
157    }
158
159    // }}}
160    // {{{ processed()
161
162    /**
163     * Set Coordinate processed
164     *
165     * Store the coordinate as processed
166     *
167     * @return  void
168     */
169    public function processed()
170    {
171        $this->_processed = true;
172    }
173
174    // }}}
175    // {{{ isProcessed()
176
177    /**
178     * Coordinate already processed
179     *
180     * Return if coordinate already was processsed
181     *
182     * @return  bool    processed
183     */
184    public function isProcessed()
185    {
186        return $this->_processed;
187    }
188
189    // }}}
190    // {{{ getX()
191
192    /**
193     * Return X coordinate
194     *
195     * Returns the X coordinate of the coordinate
196     *
197     * @return  float    X coordinate
198     */
199    public function getX()
200    {
201        return $this->_x;
202    }
203
204    // }}}
205    // {{{ getY()
206
207    /**
208     * Return Y coordinate
209     *
210     * Returns the Y coordinate of the coordinate
211     *
212     * @return  float    Y coordinate
213     */
214    public function getY()
215    {
216        return $this->_y;
217    }
218
219    // }}}
220    // {{{ getZ()
221
222    /**
223     * Return Z coordinate
224     *
225     * Returns the Z coordinate of the coordinate
226     *
227     * @return  float    Z coordinate
228     */
229    public function getZ()
230    {
231        return $this->_z;
232    }
233
234    // }}}
235    // {{{ setScreenCoordinates()
236
237    /**
238     * Set precalculated screen coordinates
239     *
240     * Store the screen coordinates calculated by the Renderer
241     *
242     * @param float $x X coordinate
243     * @param float $y Y coordinate
244     *
245     * @return  void
246     */
247    public function setScreenCoordinates($x, $y)
248    {
249        $this->_screenCoordinates = array((float) $x, (float) $y);
250    }
251
252    // }}}
253    // {{{ getScreenCoordinates()
254
255    /**
256     * Get screen coordinates
257     *
258     * Return an array with the screen coordinates
259     * array (     0 =>    (float) $x,
260                 1 =>    (float) $y )
261     *
262     * @return  array    Screen coordinates
263     */
264    public function getScreenCoordinates()
265    {
266        return $this->_screenCoordinates;
267    }
268
269    // }}}
270    // {{{ __toString()
271
272    /**
273     * Returns coordinate as string
274     *
275     * @return  string    Coordinate
276     */
277    public function __toString()
278    {
279        return sprintf('Coordinate: %2.f %2.f %2.f', $this->_x, $this->_y, $this->_z);
280    }
281
282    // }}}
283}
284
285// }}}
286