Skip to content

Commit

Permalink
Add project image preview
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Feb 13, 2024
1 parent d223238 commit 7475d6e
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 117 deletions.
5 changes: 4 additions & 1 deletion backend/api.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### get all project
GET http://localhost:8080/api/all_projects

### get project
GET http://localhost:8080/api/project/laser_gridfinity_boxes_0

Expand Down Expand Up @@ -31,7 +34,7 @@ Content-Type: application/json
"project_id": "laser_gridfinity_boxes_open_scad",
"file_counts": {
"1x1+0": 1,
"1x2+1": 0,
"1x2+1": 1,
"1x3+2": 1,
"1x4+3": 0,
"2x2+4": 0,
Expand Down
10 changes: 4 additions & 6 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.nestapp"
version = "0.3.0"
version = "0.3.2"

application {
mainClass.set("com.nestapp.Main")
Expand All @@ -16,7 +16,7 @@ ktor {
docker {
jreVersion.set(JavaVersion.VERSION_17)
localImageName.set("nest2d")
imageTag.set("0.3.0")
imageTag.set(project.version.toString())
externalRegistry.set(
io.ktor.plugin.features.DockerImageRegistry.dockerHub(
appName = provider { "nest2d" },
Expand Down Expand Up @@ -54,13 +54,11 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")

implementation("com.xhiteam.dxf:dxf:1.0.0")

implementation("com.github.jchamlin:clipper-java:b4dcd50c51")
implementation("com.google.code.gson:gson:2.10")
implementation("org.uncommons.watchmaker:watchmaker-framework:0.7.1")
implementation("org.apache.xmlgraphics:batik-svg-dom:1.14")
implementation("org.apache.xmlgraphics:batik-swing:1.14")
implementation("org.apache.xmlgraphics:batik-svg-dom:1.17")
implementation("org.apache.xmlgraphics:batik-swing:1.17")
implementation("org.dom4j:dom4j:2.1.3")
implementation("org.uncommons.watchmaker:watchmaker-swing:0.7.1")
implementation("io.jenetics:jenetics:5.2.0")
Expand Down
8 changes: 8 additions & 0 deletions backend/src/main/java/com/nestapp/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nestapp

import java.io.File

class Configuration(
val baseUrl: String,
val projectsFolder: File,
)
28 changes: 19 additions & 9 deletions backend/src/main/java/com/nestapp/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.nestapp.nest_api.UserInputExecution
import com.nestapp.nest_api.nestRestApi
import com.nestapp.projects.ProjectsRepository
import com.nestapp.projects.projectsRest
import com.nestapp.test.testTrigger
import io.ktor.http.HttpMethod.Companion.DefaultMethods
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
Expand All @@ -31,12 +30,6 @@ internal object Main {

@JvmStatic
fun main(args: Array<String>) {
/*try {
testTrigger()
} catch (e: Exception) {
e.printStackTrace()
}*/

val json = Json {
prettyPrint = true
ignoreUnknownKeys = true
Expand Down Expand Up @@ -74,10 +67,27 @@ internal object Main {
json()
}

val baseUrl = "https://nest2d.online/api"
//val baseUrl = "http://localhost:8080/api"

val configuration = Configuration(
baseUrl = baseUrl,
projectsFolder = File("mount/projects"),
)

routing {
route("/api") {
projectsRest(File("mount/projects"), projectsRepository, SvgFromDxf())
nestRestApi(projectsRepository, nestedRepository, File("mount/projects"))
projectsRest(
configuration,
File("mount/projects"),
projectsRepository,
SvgFromDxf()
)
nestRestApi(
projectsRepository,
nestedRepository,
File("mount/projects")
)

get("/version") {
call.respondText("Some version")
Expand Down
30 changes: 7 additions & 23 deletions backend/src/main/java/com/nestapp/nest/data/NestPath.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.nestapp.nest.data;

import java.util.ArrayList;
import java.util.List;

import com.nestapp.nest.config.Config;
import org.apache.batik.ext.awt.geom.Polygon2D;

import com.nestapp.nest.config.Config;
import java.util.ArrayList;
import java.util.List;

/**
* @author yisa
Expand All @@ -15,11 +14,8 @@ public class NestPath implements Comparable<NestPath> {
private final List<Segment> segments;
public double offsetX;
public double offsetY;

private int id;
private int source;
private int rotation; // angolo rotazione

public int[] rotations;
public Config config;
public double area;
Expand Down Expand Up @@ -52,7 +48,6 @@ public NestPath(NestPath srcNestPath) {
this.id = srcNestPath.id;
this.rotation = srcNestPath.rotation;
this.rotations = srcNestPath.rotations; //TODO not clone.
this.source = srcNestPath.source;
this.offsetX = srcNestPath.offsetX;
this.offsetY = srcNestPath.offsetY;
this.bid = srcNestPath.bid;
Expand Down Expand Up @@ -99,9 +94,7 @@ public void reverse() {
rever.add(segments.get(i));
}
segments.clear();
for (Segment s : rever) {
segments.add(s);
}
segments.addAll(rever);
}

public Segment get(int i) {
Expand All @@ -111,7 +104,7 @@ public Segment get(int i) {
@Override
public String toString() {
String res = "";
res += "id = " + id + " , source = " + source + " , rotation = " + rotation + "\n";
res += "id = " + id + " , rotation = " + rotation + "\n";
int count = 0;
for (Segment s : segments) {
res += "Segment " + count + "\n";
Expand All @@ -129,15 +122,6 @@ public void setRotation(int rotation) {
this.rotation = rotation;
}

public int getSource() {
return source;
}

public void setSource(int source) {
this.source = source;
}


public void clear() {
segments.clear();
}
Expand Down Expand Up @@ -242,8 +226,8 @@ public void setBid(int bid) {
public Polygon2D toPolygon2D() {///TODO optimize
Polygon2D newp;

List<Float> xp = new ArrayList<Float>();
List<Float> yp = new ArrayList<Float>();
List<Float> xp = new ArrayList<>();
List<Float> yp = new ArrayList<>();
for (Segment s : segments) {
xp.add((float) s.getX());
yp.add((float) s.getY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ public static NestPath rotatePolygon2Polygon(NestPath polygon , int degrees ){
}
rotated.setBid(polygon.getBid());
rotated.setId(polygon.getId());
rotated.setSource(polygon.getSource());
return rotated;
}

Expand Down
23 changes: 11 additions & 12 deletions backend/src/main/java/com/nestapp/nest/util/Placementworker.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
*/
public class Placementworker {
private static Gson gson = new GsonBuilder().create();

public NestPath binPolygon;
private Config config;

public Map<String, List<NestPath>> nfpCache;

/**
Expand All @@ -53,20 +53,19 @@ public Placementworker(NestPath binPolygon, Config config, Map<String, List<Nest
* @return
*/
public Result placePaths(List<NestPath> paths) {

// rotazione dei NestPaths passati (paths)
List<NestPath> rotated = new ArrayList<>();
for (int i = 0; i < paths.size(); i++) {
NestPath r = GeometryUtil.rotatePolygon2Polygon(paths.get(i), paths.get(i).getRotation());
r.setRotation(paths.get(i).getRotation());
r.setPossibleRotations(paths.get(i).getPossibleRotations());
r.setSource(paths.get(i).getSource());
r.setId(paths.get(i).getId());
rotated.add(r);
}
paths = rotated;
paths = rotated;


List<List<PathPlacement>> allplacements = new ArrayList<>();
// Now the fitness is defined as the width of material used.
double fitness = 0;
Expand All @@ -82,12 +81,12 @@ public Result placePaths(List<NestPath> paths) {

//fitness += 1;
double minwidth = Double.MAX_VALUE; // valore che verrà assegnato alla fitness

// Loops over all the polygons (paths)
for (int i = 0; i < paths.size(); i++) {
NestPath path = paths.get(i);


//inner NFP ***************************************************************
key = gson.toJson(new NfpKey(-1, path.getId(), true, 0, path.getRotation()));
if (!nfpCache.containsKey(key)) {
Expand All @@ -108,7 +107,7 @@ public Result placePaths(List<NestPath> paths) {
continue;
}//***************************************************************


PathPlacement position = null;
if (placed.size() == 0) {
// first placement , put it on the left
Expand Down Expand Up @@ -201,7 +200,7 @@ public Result placePaths(List<NestPath> paths) {
// System.out.println("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU");
for (NestPath element : finalNfpf) {
nf = element;
if (Math.abs(GeometryUtil.polygonArea(nf)) < 2) {
if (Math.abs(GeometryUtil.polygonArea(nf)) < 2) {
continue;
}
for (int k = 0; k < nf.size(); k++) {
Expand Down Expand Up @@ -248,7 +247,7 @@ public Result placePaths(List<NestPath> paths) {
fitness += minwidth; /// binarea;
//fitness = minwidth;
}

for (int i = 0; i < placed.size(); i++) {
int index = paths.indexOf(placed.get(i));
if (index >= 0) {
Expand Down
64 changes: 64 additions & 0 deletions backend/src/main/java/com/nestapp/projects/AllProjectsResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nestapp.projects

import com.nestapp.Configuration
import io.ktor.http.ContentDisposition
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.response.header
import io.ktor.server.response.respond
import io.ktor.server.response.respondFile
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.io.File

fun Route.allProjects(
configuration: Configuration,
projectsRepository: ProjectsRepository,
) {
get("/all_projects") {
val result = projectsRepository.getProjects()
.map { (_, project) ->
AllProjectsResponse.Project(
id = project.id,
name = project.name,
preview = "${configuration.baseUrl}/project/${project.id.value}/preview",
)
}
call.respond(HttpStatusCode.OK, result)
}

get("/project/{project_id}/preview") {
val projectId = ProjectId(call.parameters["project_id"] ?: throw Exception("project_id not found"))

val svgFile = File(configuration.projectsFolder, "${projectId.value}/media/preview.png")

call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Attachment.withParameter(
ContentDisposition.Parameters.FileName,
"preview.png",
).toString()
)
call.respondFile(svgFile)
}
}

@Serializable
data class AllProjectsResponse(
@SerialName("project")
val project: List<Project>
) {
@Serializable
data class Project(
@SerialName("id")
val id: ProjectId,
@SerialName("name")
val name: String,
@SerialName("preview")
val preview: String,
)
}

Loading

0 comments on commit 7475d6e

Please sign in to comment.