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_Map
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_Map extends Image_3D_Object {
50
51    protected $_points;
52
53    public function __construct($points = array()) {
54        parent::__construct();
55        $this->_points = array();
56
57        foreach ($points as $row) {
58            if (is_array($row)) {
59                $this->addRow($row);
60            } else {
61                $this->addRow(array($row));
62            }
63        }
64    }
65
66    public function addRow($row)
67    {
68        $rowNbr = array_push($this->_points, array()) - 1;
69        foreach ($row as $point) {
70            if (is_object($point) && ($point instanceof Image_3D_Point)) $this->_points[$rowNbr][] = $point;
71        }
72
73        if (!count($this->_points[$rowNbr])) {
74            unset($this->_points[$rowNbr]);
75            return false;
76        }
77
78        if (count($this->_points) > 1) {
79            $newRow = count($this->_points) - 1;
80            $lastRow = $newRow - 1;
81
82            $newCount = count($this->_points[$newRow]);
83            $lastCount = count($this->_points[$lastRow]);
84
85            if ($newCount < $lastCount) {
86                $tmp = $newRow;
87                $newRow = $lastRow;
88                $lastRow = $tmp;
89
90                $tmp = $newCount;
91                $newCount = $lastCount;
92                $lastCount = $tmp;
93            }
94
95            $top = (($newCount == 1) ? 1 : 1 / ($newCount - 1));
96            $bottom = (($lastCount == 1) ? 32768 : 1 / ($lastCount - 1));
97
98            $k = 0;
99            for ($i = 1; $i < $newCount; $i++) {
100                if (($i * $top) > ($k * $bottom + $bottom / 2)) {
101                    // Nach unten geoeffnetes Polygon einfuegen /\
102                    $this->_addPolygon(new Image_3D_Polygon($this->_points[$newRow][$i - 1], $this->_points[$lastRow][$k + 1], $this->_points[$lastRow][$k]));
103                    $k++;
104                }
105                // Nach oben geoeffnetes Polygon einfuegen \/
106                $this->_addPolygon(new Image_3D_Polygon($this->_points[$newRow][$i - 1], $this->_points[$newRow][$i], $this->_points[$lastRow][$k]));
107            }
108        }
109        return true;
110    }
111
112    public function getRow($int) {
113        if (!isset($this->_points[$int])) return false;
114        return $this->_points[$int];
115    }
116
117    public function getPoint($x, $y) {
118        if (!isset($this->_points[$x][$y])) return false;
119        return $this->_points[$x][$y];
120    }
121
122    public function setOption($option, $value) {
123//        if ($option === Image_3D::IMAGE_3D_OPTION_BF_CULLING) $value = false;
124        parent::setOption($option, $value);
125    }
126}
127