Fix inherited 3MF triangle property group resolution
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 50s

This commit is contained in:
2026-11-08 18:59:12 +00:00
parent 4e675510fd
commit fd6a7cd2f3
2 changed files with 99 additions and 7 deletions
+10 -7
View File
@@ -561,7 +561,7 @@ class ThreeMfGeometryExtractor
$ancestry[] = $objectId; $ancestry[] = $objectId;
if ($object['mesh'] !== null) { if ($object['mesh'] !== null) {
$defaultColor = $this->resolveColor($baseMaterials, $object['pid'], $object['pindex']); $this->resolveColor($baseMaterials, $object['pid'], $object['pindex']);
$determinant = $transform[0] * ($transform[4] * $transform[8] - $transform[5] * $transform[7]) $determinant = $transform[0] * ($transform[4] * $transform[8] - $transform[5] * $transform[7])
- $transform[1] * ($transform[3] * $transform[8] - $transform[5] * $transform[6]) - $transform[1] * ($transform[3] * $transform[8] - $transform[5] * $transform[6])
+ $transform[2] * ($transform[3] * $transform[7] - $transform[4] * $transform[6]); + $transform[2] * ($transform[3] * $transform[7] - $transform[4] * $transform[6]);
@@ -578,9 +578,11 @@ class ThreeMfGeometryExtractor
throw new RuntimeException('3MF triangle references a vertex index that does not exist.'); throw new RuntimeException('3MF triangle references a vertex index that does not exist.');
} }
$color = $triangle['pid'] !== null $color = $this->resolveColor(
? $this->resolveColor($baseMaterials, $triangle['pid'], $this->resolveTriangleVertexIndex($triangle)) $baseMaterials,
: $defaultColor; $triangle['pid'] ?? $object['pid'],
$this->resolveTriangleVertexIndex($triangle, $object['pindex']),
);
$triangles[] = [ $triangles[] = [
'v' => [ 'v' => [
@@ -640,13 +642,14 @@ class ThreeMfGeometryExtractor
* the face. This renderer only paints one flat colour per triangle, so a * the face. This renderer only paints one flat colour per triangle, so a
* triangle that actually declares differing per-vertex indices is * triangle that actually declares differing per-vertex indices is
* rejected explicitly rather than silently picking p1 and discarding the * rejected explicitly rather than silently picking p1 and discarding the
* rest of the declared colour data. * rest of the declared colour data. The property group and first index
* inherit independently from object pid and pindex; omitted p2/p3 use p1.
* *
* @param array{v1:int,v2:int,v3:int,pid:?int,p1:?int,p2:?int,p3:?int} $triangle * @param array{v1:int,v2:int,v3:int,pid:?int,p1:?int,p2:?int,p3:?int} $triangle
*/ */
private function resolveTriangleVertexIndex(array $triangle): ?int private function resolveTriangleVertexIndex(array $triangle, ?int $objectPindex): ?int
{ {
$p1 = $triangle['p1']; $p1 = $triangle['p1'] ?? $objectPindex;
$p2 = $triangle['p2'] ?? $p1; $p2 = $triangle['p2'] ?? $p1;
$p3 = $triangle['p3'] ?? $p1; $p3 = $triangle['p3'] ?? $p1;
@@ -2,7 +2,11 @@
namespace Tests\Unit; namespace Tests\Unit;
use App\Services\ManufacturingFileValidator;
use App\Services\ProductImageRenderer;
use App\Services\ThreeMfGeometryExtractor; use App\Services\ThreeMfGeometryExtractor;
use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException; use RuntimeException;
use Tests\TestCase; use Tests\TestCase;
use ZipArchive; use ZipArchive;
@@ -131,6 +135,91 @@ class ThreeMfGeometryExtractorTest extends TestCase
unlink($path); unlink($path);
} }
private function validatedTetrahedron(string $triangleProperties): string
{
$faces = '';
foreach ([[0, 2, 1], [0, 1, 3], [0, 3, 2], [1, 2, 3]] as [$v1, $v2, $v3]) {
$faces .= '<triangle v1="'.$v1.'" v2="'.$v2.'" v3="'.$v3.'" '.$triangleProperties.'/>';
}
$resources = '<m:colorgroup xmlns:m="http://schemas.microsoft.com/3dmanufacturing/material/2015/02" id="2">'
.'<m:color color="#FF0000"/><m:color color="#0000FF"/><m:color color="#00FF0080"/></m:colorgroup>'
.'<object id="1" type="model" pid="2" pindex="0"><mesh><vertices>'
.'<vertex x="0" y="0" z="0"/><vertex x="10" y="0" z="0"/>'
.'<vertex x="0" y="10" z="0"/><vertex x="0" y="0" z="10"/>'
.'</vertices><triangles>'.$faces.'</triangles></mesh></object>';
$path = $this->validArchive($this->model($resources, '<item objectid="1"/>'));
try {
$validated = (new ManufacturingFileValidator)->validate(new UploadedFile($path, 'tetrahedron.3mf', test: true));
$this->assertSame('3mf', $validated['extension']);
} catch (\Throwable $exception) {
unlink($path);
throw $exception;
}
return $path;
}
/** @return array<string, array{string}> */
public static function flatInheritedProperties(): array
{
return [
'p2 and p3 default to p1' => ['p1="1"'],
'all vertex properties specified' => ['p1="1" p2="1" p3="1"'],
];
}
#[DataProvider('flatInheritedProperties')]
public function test_inherited_triangle_property_group_applies_flat_override_through_png_rendering(string $properties): void
{
$path = $this->validatedTetrahedron($properties);
try {
$scene = $this->extractor->extract($path);
$this->assertSame(4, $scene['triangle_count']);
foreach ($scene['triangles'] as $triangle) {
$this->assertSame([0, 0, 255, 255], $triangle['color']);
}
$rendered = (new ProductImageRenderer)->render($scene['triangles']);
$this->assertSame('image/png', $rendered['mime']);
$image = imagecreatefromstring($rendered['bytes']);
$this->assertNotFalse($image);
try {
$pixel = imagecolorsforindex($image, imagecolorat($image, 256, 256));
$this->assertSame(0, $pixel['red']);
$this->assertSame(0, $pixel['green']);
$this->assertGreaterThan(0, $pixel['blue']);
$this->assertSame(0, $pixel['alpha']);
} finally {
imagedestroy($image);
}
} finally {
unlink($path);
}
}
public function test_inherited_triangle_property_group_rejects_interpolated_override(): void
{
$path = $this->validatedTetrahedron('p1="0" p2="1" p3="0"');
try {
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/interpolat/i');
(new ProductImageRenderer)->render($this->extractor->extract($path)['triangles']);
} finally {
unlink($path);
}
}
public function test_inherited_triangle_property_group_rejects_transparent_override(): void
{
$path = $this->validatedTetrahedron('p1="2"');
try {
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/transparen/i');
(new ProductImageRenderer)->render($this->extractor->extract($path)['triangles']);
} finally {
unlink($path);
}
}
public function test_resolves_object_level_color_from_a_colorgroup(): void public function test_resolves_object_level_color_from_a_colorgroup(): void
{ {
$resources = '<colorgroup id="5"><color color="#00CC33"/></colorgroup>' $resources = '<colorgroup id="5"><color color="#00CC33"/></colorgroup>'