@@ -22,6 +22,7 @@ public function create(Point $a, Point $b, Point $c, ?Point $d = null, ?float $j
2222 /** @return list<Plane> */
2323 public function fromTriangle (Point $ a , Point $ b , Point $ c , float $ voxelSizeDotThreshold ): array
2424 {
25+ $ this ->voxels = [];
2526 $ voxelSize = (int )$ voxelSizeDotThreshold ;
2627 $ voxelThreshold = max (1 , intval (str_replace ('0. ' , '' , abs ($ voxelSizeDotThreshold - $ voxelSize )))); // @phpstan-ignore argument.type
2728 if ($ voxelSize > 0 ) {
@@ -39,6 +40,7 @@ public function fromTriangle(Point $a, Point $b, Point $c, float $voxelSizeDotTh
3940 $ planes [] = (new Wall ($ voxelPoint ->clone ()->addX ($ voxelSize ), false , $ voxelSize , $ voxelSize ))->setNormal ($ this ->voxelNormal [0 ], $ this ->voxelNormal [1 ]);
4041 $ planes [] = (new Floor ($ voxelPoint ->clone ()->addY ($ voxelSize ), $ voxelSize , $ voxelSize ))->setNormal ($ this ->voxelNormal [0 ], $ this ->voxelNormal [1 ]);
4142 }
43+ $ this ->voxels = [];
4244 return $ planes ;
4345 }
4446
@@ -300,7 +302,6 @@ private function voxelizeTriangle(Point $a, Point $b, Point $c, int $voxelSize,
300302 ($ u [0 ] * $ v [1 ]) - ($ u [1 ] * $ v [0 ]),
301303 ));
302304
303- $ this ->voxels = [];
304305 $ this ->voxelizeLine ($ a , $ b );
305306 $ this ->voxelizeLine ($ b , $ c );
306307 $ this ->voxelizeLine ($ c , $ a );
@@ -333,27 +334,24 @@ private function voxelizeTriangle(Point $a, Point $b, Point $c, int $voxelSize,
333334 );
334335
335336 $ data = [];
336- for ($ y = $ bbMin ->y ; $ y <= $ bbMax ->y ; $ y ++) {
337- for ($ x = $ bbMin ->x ; $ x <= $ bbMax ->x ; $ x ++) {
338- for ($ z = $ bbMin ->z ; $ z <= $ bbMax ->z ; $ z ++) {
339- if (!isset ($ this ->voxels ["$ x, $ y, $ z " ])) {
340- continue ;
341- }
337+ foreach ($ this ->voxels as $ voxel ) {
338+ if ($ matchTriangleSize && ($ voxel ->x > $ bbMax ->x || $ voxel ->y > $ bbMax ->y || $ voxel ->z > $ bbMax ->z )) {
339+ continue ;
340+ }
342341
343- $ key = implode (', ' , [
344- (int )ceil (($ x - $ bbMin ->x ) / $ voxelSize ),
345- (int )ceil (($ y - $ bbMin ->y ) / $ voxelSize ),
346- (int )ceil (($ z - $ bbMin ->z ) / $ voxelSize ),
347- ]);
348- if (!isset ($ data [$ key ])) {
349- $ data [$ key ] = 0 ;
350- }
351- $ data [$ key ]++;
352- }
342+ $ key = implode (', ' , [
343+ (int ) ceil (($ voxel ->x - $ bbMin ->x ) / $ voxelSize ),
344+ (int ) ceil (($ voxel ->y - $ bbMin ->y ) / $ voxelSize ),
345+ (int ) ceil (($ voxel ->z - $ bbMin ->z ) / $ voxelSize ),
346+ ]);
347+ if (!isset ($ data [$ key ])) {
348+ $ data [$ key ] = 0 ;
353349 }
350+ $ data [$ key ]++;
354351 }
355352
356353 $ startPoints = [];
354+ $ halfHeight = (int )round ($ voxelSize / 2 );
357355 foreach ($ data as $ key => $ hits ) {
358356 if ($ hits < $ voxelThreshold ) {
359357 continue ;
@@ -362,7 +360,7 @@ private function voxelizeTriangle(Point $a, Point $b, Point $c, int $voxelSize,
362360 $ sizeIncrements = explode (', ' , $ key );
363361 $ startPoints [] = $ bbMin ->clone ()->addPart (
364362 $ voxelSize * (int )$ sizeIncrements [0 ],
365- $ voxelSize * (int )$ sizeIncrements [1 ],
363+ $ voxelSize * (int )$ sizeIncrements [1 ] - $ halfHeight ,
366364 $ voxelSize * (int )$ sizeIncrements [2 ],
367365 );
368366 }
0 commit comments