Skip to content

Commit 7da7700

Browse files
committed
Next
1 parent 06fe7db commit 7da7700

File tree

16 files changed

+348
-77
lines changed

16 files changed

+348
-77
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
APP_NAME=Laravel
1+
APP_NAME=Image2Cut
22
APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true

app/Http/Controllers/MetalCuttingController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Services\DxfService;
65
use Illuminate\Http\Request;
76
use App\Services\ImageService;
87
use App\Services\FunctionService;
@@ -27,7 +26,7 @@ public function __construct(PatterneService $patterneService ,
2726
// Affiche le formulaire
2827
public function index()
2928
{
30-
return view('metal-cutting.index');
29+
return view('image-cut.index');
3130
}
3231

3332
// Traite les données du formulaire et génère l'image
@@ -40,7 +39,7 @@ public function process(Request $request)
4039
'min_tool_diameter' => 'required|numeric|min:1',
4140
'max_tool_diameter' => 'required|numeric|gt:min_tool_diameter',
4241
'image' => 'required|image|mimes:jpeg,png,jpg|max:2048', // Upload image
43-
'shape' => 'required|in:circle,square,rectangle,triangle,random',
42+
'shape' => 'required|in:circle,square,rectangle,triangle,hexagon,random',
4443
'angle' => 'required|numeric|min:0|max:360',
4544
'espace' => 'required|numeric|min:2|gt:max_tool_diameter',
4645
'ignoreThreshold' => 'required|numeric|min:0|max:100',
@@ -57,6 +56,7 @@ public function process(Request $request)
5756
$angle = $request->input('angle');
5857
$espace = $request->input('espace');
5958
$fade = $request->has('fade');
59+
$invert = $request->has('invert');
6060
$ignoreThreshold = $request->input('ignoreThreshold');
6161
$alignment = $request->input('alignment');
6262
$pen = $request->input('pen');
@@ -86,7 +86,8 @@ public function process(Request $request)
8686
$maxToolDiameter,
8787
$fullImagePath,
8888
$shape,
89-
$fade,
89+
$fade,
90+
$invert,
9091
$alignment,
9192
$angle,
9293
$ignoreThreshold,
@@ -110,15 +111,16 @@ public function process(Request $request)
110111
$partSizeY = $result['partSizeY'];
111112

112113
// Rediriger avec l'URL du pattern généré
113-
return redirect()->route('metal-cutting.index')
114+
return redirect()->route('image-cut.index')
114115
->withInput(input: $validatedData)
115116
->with('generateTools', $generateTools)
116117
->with('usedTools', $usedTools)
117118
->with('hitcount', $hitcount)
118119
->with('shape', $shape)
119120
->with('angle', $angle)
120121
->with('espace', $espace)
121-
->with('fade', $fade)
122+
->with('fade', $fade)
123+
->with('invert', $invert)
122124
->with('ignoreThreshold', $ignoreThreshold)
123125
->with('partSizeY', $partSizeY)
124126
->with('pen', $pen)
@@ -130,6 +132,6 @@ public function process(Request $request)
130132
->with('originalImageUrl', $publicImageUrl);
131133
}
132134

133-
return redirect()->route('metal-cutting.index')->with('error', 'Aucune image n\'a été uploadée.');
135+
return redirect()->route('image-cut.index')->with('error', 'Aucune image n\'a été uploadée.');
134136
}
135137
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
class WelcomeController extends Controller
6+
{
7+
public function index()
8+
{
9+
return view('welcome');
10+
}
11+
}

app/Services/DxfService.php

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function saveDXFFile($filename)
2929
}
3030

3131
// Dessine une forme à une position donnée dans le fichier DXF
32-
public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape, &$positions, $angle = null, $ignoreThreshold, $pen)
32+
public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape, &$positions, $angle = null, $ignoreThreshold, $invert, $pen)
3333
{
3434

3535
// Vérifier que $x et $y sont dans les limites de l'image redimensionnée
@@ -40,12 +40,20 @@ public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape
4040
$grayValue = imagecolorat($resizedImage, $x, $y) & 0xFF; // Extraire la composante grise
4141

4242
// Ignorer les pixels très sombres
43-
if ($grayValue < $ignoreThreshold) {
44-
return;
43+
if ($invert) {
44+
// Ignorer les pixels très clairs si on inverse la logique
45+
if ($grayValue > (255 - $ignoreThreshold)) {
46+
return;
47+
}
48+
} else {
49+
// Ignorer les pixels très sombres (logique normale)
50+
if ($grayValue < $ignoreThreshold) {
51+
return;
52+
}
4553
}
46-
54+
4755
// Mapper la nuance de gris sur une taille d'outil dans le tableau généré
48-
$toolSize = $this->functionService->mapGrayToToolSize($grayValue, $toolSizes);
56+
$toolSize = $this->functionService->mapGrayToToolSize($grayValue, $toolSizes, $invert);
4957

5058
// Vérifier si l'outil est trop proche des bords pour éviter de dessiner hors de l'image
5159
if ($x - $toolSize / 2 < 0 || $x + $toolSize / 2 > imagesx($resizedImage) || $y - $toolSize / 2 < 0 || $y + $toolSize / 2 > imagesy($resizedImage)) {
@@ -79,8 +87,16 @@ public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape
7987
case 'square':
8088
$this->writeSquareToDXF($x, -$y, $toolSize, $angle, $pen);
8189
break;
90+
case 'rectangle':
91+
$this->writeRectangleToDXF($x, -$y, $toolSize*2, $toolSize,$angle, $pen);
92+
break;
8293
case 'triangle':
83-
$this->writeTriangleeToDXF($x, -$y, $toolSize, $angle, $pen);
94+
$this->writeTriangleToDXF($x, -$y, $toolSize, $angle, $pen);
95+
break;
96+
case 'hexagon':
97+
// Calcul des points pour un hexagone
98+
$points = $this->functionService->calculateHexagonPoints($toolSize);
99+
$this->writeHexagonToDXF($x, -$y, $toolSize, $angle, $pen);
84100
break;
85101
}
86102

@@ -107,7 +123,7 @@ private function writeCircleToDXF($x, $y, $radius, $angle = null, $pen)
107123
fwrite($this->dxfFile, "0"); // Fin de l'entité
108124
}
109125

110-
private function writeSquareToDXF($x, $y, $size, $angle, $pen) {
126+
private function writeSquareToDXF($x, $y, $size, $angle, $pen) {
111127
// Génère un carré au format DXF (les coordonnées des 4 points après rotation)
112128
$halfSize = $size / 2;
113129

@@ -123,7 +139,24 @@ private function writeSquareToDXF($x, $y, $size, $angle, $pen) {
123139
return $this->generateDXFPolygon($points, $pen);
124140
}
125141

126-
private function writeTriangleeToDXF($x, $y, $size, $angle, $pen) {
142+
private function writeRectangleToDXF($x, $y, $width, $height, $angle, $pen) {
143+
// Génère un rectangle au format DXF (les coordonnées des 4 points après rotation)
144+
$halfWidth = $width / 2;
145+
$halfHeight = $height / 2;
146+
147+
// Calcul des points avec rotation
148+
$points = $this->rotatePoints([
149+
['x' => $x - $halfWidth, 'y' => $y - $halfHeight], // coin supérieur gauche
150+
['x' => $x + $halfWidth, 'y' => $y - $halfHeight], // coin supérieur droit
151+
['x' => $x + $halfWidth, 'y' => $y + $halfHeight], // coin inférieur droit
152+
['x' => $x - $halfWidth, 'y' => $y + $halfHeight], // coin inférieur gauche
153+
['x' => $x - $halfWidth, 'y' => $y - $halfHeight], // retour au coin supérieur gauche
154+
], $x, $y, $angle);
155+
156+
return $this->generateDXFPolygon($points, $pen);
157+
}
158+
159+
private function writeTriangleToDXF($x, $y, $size, $angle, $pen) {
127160
// Génère un triangle au format DXF (les coordonnées des 3 points après rotation)
128161
$halfSize = $size / 2;
129162
$points = [
@@ -138,6 +171,33 @@ private function writeTriangleeToDXF($x, $y, $size, $angle, $pen) {
138171
return $this->generateDXFPolygon($rotatedPoints, $pen);
139172
}
140173

174+
private function writeHexagonToDXF($x, $y, $toolSize, $angle, $pen) {
175+
// Calcul des points de l'hexagone
176+
$points = $this->calculateHexagonPoints($x, $y, $toolSize);
177+
178+
// Appliquer la rotation aux points
179+
$rotatedPoints = $this->rotatePoints($points, $x, $y, $angle);
180+
181+
// Générer l'hexagone en DXF avec les points tournés
182+
return $this->generateDXFPolygon($rotatedPoints, $pen);
183+
}
184+
185+
public function calculateHexagonPoints($x, $y, $toolSize) {
186+
$radius = $toolSize / 2; // Le rayon du cercle circonscrit
187+
$angleStep = 60; // Chaque angle entre les sommets est de 60 degrés
188+
189+
$points = [];
190+
for ($i = 0; $i < 7; $i++) {
191+
$angle = deg2rad($i * $angleStep); // Convertir l'angle en radians
192+
$points[] = [
193+
'x' => $x + $radius * cos($angle), // Calcul de la coordonnée x
194+
'y' => $y + $radius * sin($angle) // Calcul de la coordonnée y
195+
];
196+
}
197+
198+
return $points;
199+
}
200+
141201
private function generateDXFPolygon($points, $pen) {
142202
// Génère un polygone DXF à partir d'une liste de points
143203
$dxfData = "0\nPOLYLINE\n8\n0\n66\n1\n62\n{$pen}\n";

app/Services/FunctionService.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
class FunctionService
66
{
77

8-
public function mapGrayToToolSize($grayValue, $toolSizes)
8+
public function mapGrayToToolSize($grayValue, $toolSizes, $invert = false)
99
{
10-
// Mapper la valeur grise à une taille d'outil
11-
$index = (int) ($grayValue / 255 * (count($toolSizes) - 1)); // Calculer l'indice en fonction de la nuance de gris
12-
return $toolSizes[$index]; // Retourner la taille d'outil correspondante
10+
// Calculer l'indice en fonction de la nuance de gris
11+
$index = (int) ($grayValue / 255 * (count($toolSizes) - 1));
12+
13+
// Si l'inversion est activée, inverser l'indice
14+
if ($invert) {
15+
$index = (count($toolSizes) - 1) - $index; // Inverser l'indice
16+
}
17+
18+
// Retourner la taille d'outil correspondante
19+
return $toolSizes[$index];
1320
}
1421

1522
public function generateToolSizes($numberOfTools, $minToolDiameter, $maxToolDiameter)
@@ -42,4 +49,24 @@ public function isOverlapping($x, $y, $toolSize, $positions)
4249
return false; // Aucune forme ne se chevauche
4350
}
4451

52+
public function calculateHexagonPoints($toolSize)
53+
{
54+
$halfSize = $toolSize / 2;
55+
$radius = $toolSize / 2; // Rayon de l'hexagone
56+
57+
// Points pour un hexagone régulier (angle entre chaque point : 60 degrés)
58+
$points = [];
59+
for ($i = 0; $i < 6; $i++) {
60+
// Calculer les angles pour chaque sommet de l'hexagone
61+
$angle = deg2rad(60 * $i);
62+
// Calculer les coordonnées en fonction du rayon et de l'angle
63+
$x = $halfSize + $radius * cos($angle); // Décalage en X
64+
$y = $halfSize + $radius * sin($angle); // Décalage en Y
65+
$points[] = $x;
66+
$points[] = $y;
67+
}
68+
69+
return $points;
70+
}
71+
4572
}

0 commit comments

Comments
 (0)