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/Paintable/Object.php');
36
37/**
38 * Image_3D_Object_Sphere
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_Object_Torus extends Image_3D_Object {
50
51    public function __construct($options) {
52        parent::__construct();
53
54        $inner_radius = (float) $options['inner_radius'];
55        $outer_radius = (float) $options['outer_radius'];
56
57        $r1 = ($outer_radius - $inner_radius) / 2;
58        $r2 = $inner_radius + $r1;
59
60        $d1 = (int) round(max(1, $options['detail_1']) * 4);
61        $d2 = (int) round(max(1, $options['detail_2']) * 4);
62
63        $rings = array();
64        for ($i = 0; $i < $d1; ++$i) {
65            $rings[$i] = array();
66            for ($j = 0; $j < $d2; ++$j) {
67                $_i = $i / $d1;
68                $_j = $j / $d2;
69
70                $z = cos($_j * pi() * 2) * $r1;
71                $z2 = sin($_j * pi() * 2) * $r1;
72
73                $x = ($r2 + $z2) * cos($_i * pi() * 2);
74                $y = ($r2 + $z2) * sin($_i * pi() * 2);
75
76                $rings[$i][] = new Image_3D_Point($x, $y, $z);
77            }
78        }
79
80        foreach($rings as $i => $ring) {
81            $i_next = ($i + 1) % count($rings);
82            foreach($ring as $j => $point) {
83                $j_next = ($j + 1) % count($ring);
84
85                $this->_addPolygon(new Image_3D_Polygon($rings[$i_next][$j], $rings[$i][$j], $rings[$i][$j_next]));
86                $this->_addPolygon(new Image_3D_Polygon($rings[$i_next][$j], $rings[$i][$j_next], $rings[$i_next][$j_next]));
87            }
88        }
89    }
90
91}
92