Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
container.php
1<?
5
12{
14 protected $boxes = array();
15 protected $availableVertexes = array(
16 array (0,0,0)
17 );
18
23 public function addBox(array $boxDims)
24 {
25 $box = new Box($boxDims);
26
27 foreach($this->availableVertexes as $vId => $v)
28 {
29 $box->move($v);
30
31 if(!$this->isVertexSuitable($box))
32 continue;
33
34 $this->boxes[] = $box;
35 unset($this->availableVertexes[$vId]);
36 $this->refreshVertexesAfterBoxAdd($box);
37 return true;
38 }
39
40 return false;
41 }
42
43 public function addBoxToVertex(array $boxDims, $vertexIdx)
44 {
45 if(!isset($this->availableVertexes[$vertexIdx]))
46 throw new SystemException('No such vertex');
47
48 $box = new Box($boxDims);
49 $box->move($this->availableVertexes[$vertexIdx]);
50
51 if(!$this->isVertexSuitable($box))
52 return false;
53
54 $this->boxes[] = $box;
55 unset($this->availableVertexes[$vertexIdx]);
56 $this->refreshVertexesAfterBoxAdd($box);
57 return true;
58 }
59
60 public function extractLastBox()
61 {
63 $box = array_pop($this->boxes);
64 $this->availableVertexes[] = $box->getVMin();
65 usort($this->availableVertexes, __CLASS__.'::distanceCompare');
66 $box->move(array(0,0,0));
67 return $box;
68 }
69
70 public function insertBox(Box $box, $vertexId = 0)
71 {
72 $box->move($this->availableVertexes[$vertexId]);
73
74 if($this->isVertexSuitable($box))
75 {
76 $this->boxes[] = $box;
77 unset($this->availableVertexes[$vertexId]);
78 $this->refreshVertexesAfterBoxAdd($box);
79 return true;
80 }
81
82 return false;
83 }
84
85 protected function refreshVertexesAfterBoxAdd(Box $box)
86 {
87 $bVert = $box->getVertexes();
88 $this->availableVertexes[] = array($bVert[1][0], $bVert[0][1], $bVert[0][2]);
89 $this->availableVertexes[] = array($bVert[0][0], $bVert[1][1], $bVert[0][2]);
90 $this->availableVertexes[] = array($bVert[0][0], $bVert[0][1], $bVert[1][2]);
91 $this->availableVertexes[] = array($bVert[1][0], $bVert[1][1], $bVert[0][2]);
92 usort($this->availableVertexes, __CLASS__.'::distanceCompare');
93 }
94
98 public function getBoxes()
99 {
100 $result = array();
101
102 foreach($this->boxes as $box)
103 $result[] = clone $box;
104
105 return $result;
106 }
107
108 public function getAvailableVertexes()
109 {
111 }
112
118 protected function isVertexSuitable(Box $newBox)
119 {
120 $result = true;
121
122 foreach($this->boxes as $existBox)
123 {
124 if($this->isBoxesIntersects($existBox, $newBox))
125 {
126 return false;
127 break;
128 }
129 }
130
131 return $result;
132 }
133
140 protected static function isBoxesIntersects(Box $box1, Box $box2)
141 {
142 $result = true;
143 $v1 = $box1->getVertexes();
144 $v2 = $box2->getVertexes();
145
146 for($i = 0; $i < 3; $i++)
147 $result = $result && self::isEdgesIntersects($v1[0][$i], $v1[1][$i], $v2[0][$i], $v2[1][$i]);
148
149 return $result;
150 }
151
160 protected static function isEdgesIntersects($min1, $max1, $min2, $max2)
161 {
162 return !($min1 >= $max2 || $max1 <= $min2);
163 }
164
168 public function getFilledDimensions()
169 {
170 if(empty($this->boxes))
171 return(array(0,0,0));
172
173 $maxX = $maxY = $maxZ = 0;
174
175 foreach($this->boxes as $box)
176 {
177 $v = $box->getVertexes();
178
179 if($maxX < $v[1][0])
180 $maxX = $v[1][0];
181
182 if($maxY < $v[1][1])
183 $maxY = $v[1][1];
184
185 if($maxZ < $v[1][2])
186 $maxZ = $v[1][2];
187 }
188
189 return array($maxX, $maxY, $maxZ);
190 }
191
195 public function getFilledVolume()
196 {
197 $dims = $this->getFilledDimensions();
198 return $dims[0]*$dims[1]*$dims[2];
199 }
200
207 public static function distanceCompare(array $p1, array $p2)
208 {
209 $zero = array(0,0,0);
210 $d1 = self::calculateDistance($p1, $zero);
211 $d2 = self::calculateDistance($p2, $zero);
212
213 if($d1 == $d2)
214 return 0;
215
216 return ($d1 < $d2) ? -1 : 1;
217 }
218
225 public static function calculateDistance(array $p1, array $p2)
226 {
227 return sqrt(pow($p1[0]-$p2[0], 2) + pow($p1[1]-$p2[1], 2) + pow($p1[2]-$p2[2], 2));
228 }
229}
static distanceCompare(array $p1, array $p2)
static isBoxesIntersects(Box $box1, Box $box2)
static calculateDistance(array $p1, array $p2)
addBoxToVertex(array $boxDims, $vertexIdx)
Definition container.php:43
static isEdgesIntersects($min1, $max1, $min2, $max2)
insertBox(Box $box, $vertexId=0)
Definition container.php:70