Skip to content

Commit ab0a945

Browse files
committed
make the thmubnail system fail out if it takes more than 100ms
1 parent 0d91ba7 commit ab0a945

File tree

3 files changed

+46
-54
lines changed

3 files changed

+46
-54
lines changed

src/main/java/eu/mihosoft/vrl/v3d/thumbnail/ThumbnailImage.java

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232

3333
public class ThumbnailImage {
3434
private static CullFace cullFaceValue = CullFace.BACK;
35-
private static int ImageSize=1000;
35+
private static int ImageSize = 1000;
36+
private WritableImage img;
3637

37-
public static Bounds getSellectedBounds(List<CSG> incoming) {
38+
public Bounds getSellectedBounds(List<CSG> incoming) {
3839
Vector3d min = null;
3940
Vector3d max = null;
4041
for (CSG c : incoming) {
@@ -68,12 +69,13 @@ public static Bounds getSellectedBounds(List<CSG> incoming) {
6869
return new Bounds(min, max);
6970
}
7071

71-
public static WritableImage get(List<CSG> c,CSGDatabaseInstance instance) {
72+
public WritableImage get(List<CSG> c, CSGDatabaseInstance instance) {
7273
ArrayList<CSG> csgList = new ArrayList<CSG>();
7374
for (CSG cs : c) {
7475
if (cs.hasManipulator()) {
7576
try {
76-
csgList.add(cs.transformed(TransformConverter.fromAffine(cs.getManipulator())).syncProperties(instance,cs));
77+
csgList.add(cs.transformed(TransformConverter.fromAffine(cs.getManipulator()))
78+
.syncProperties(instance, cs));
7779
} catch (MissingManipulatorException e) {
7880
// TODO Auto-generated catch block
7981
e.printStackTrace();
@@ -137,7 +139,6 @@ public static WritableImage get(List<CSG> c,CSGDatabaseInstance instance) {
137139
camera.getTransforms().add(camDist);
138140
//
139141

140-
141142
Scene scene = new Scene(root, getImageSize(), getImageSize(), true, SceneAntialiasing.BALANCED);
142143
scene.setFill(Color.TRANSPARENT);
143144
scene.setCamera(camera);
@@ -160,51 +161,42 @@ public static WritableImage get(List<CSG> c,CSGDatabaseInstance instance) {
160161
return snapshot;
161162
}
162163

163-
public static Thread writeImage(CSGDatabaseInstance instance,CSG incoming, File toPNG) {
164+
public void writeImage(CSGDatabaseInstance instance, CSG incoming, File toPNG) {
164165
ArrayList<CSG> bits = new ArrayList<CSG>();
165166
bits.add(incoming);
166-
return writeImage(instance,bits, toPNG);
167+
writeImage(instance, bits, toPNG);
167168
}
168169

169-
public static Thread writeImage(CSGDatabaseInstance instance,List<CSG> incoming, File toPNG) {
170-
Thread t = new Thread(new Runnable() {
171-
WritableImage img = null;
172-
173-
@Override
174-
public void run() {
175-
File image = toPNG;
176-
javafx.application.Platform.runLater(() -> img = ThumbnailImage.get(incoming,instance));
177-
long start = System.currentTimeMillis();
178-
while (img == null) {
179-
try {
180-
Thread.sleep(16);
181-
// com.neuronrobotics.sdk.common.Log.error("Waiting for image to write");
182-
} catch (InterruptedException e) {
183-
// Auto-generated catch block
184-
e.printStackTrace();
185-
return;
186-
}
187-
if((System.currentTimeMillis()-start)>1000) {
188-
System.err.println("Image failed to render!");
189-
throw new RuntimeException("Failed to load image");
190-
191-
}
192-
}
193-
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(img, null);
170+
public void writeImage(CSGDatabaseInstance instance, List<CSG> incoming, File toPNG) {
171+
img = null;
172+
173+
File image = toPNG;
174+
javafx.application.Platform.runLater(() -> img = get(incoming, instance));
175+
long start = System.currentTimeMillis();
176+
while (img == null) {
177+
try {
178+
Thread.sleep(16);
179+
// com.neuronrobotics.sdk.common.Log.error("Waiting for image to write");
180+
} catch (InterruptedException e) {
181+
// Auto-generated catch block
182+
e.printStackTrace();
183+
return;
184+
}
185+
if ((System.currentTimeMillis() - start) > 100) {
186+
System.err.println("Image failed to render!");
187+
throw new RuntimeException("Failed to load image");
194188

195-
try {
196-
ImageIO.write(bufferedImage, "png", image);
197-
} catch (IOException e) {
198-
throw new RuntimeException(e);
199-
}
200189
}
201-
});
202-
t.setUncaughtExceptionHandler((thread, throwable) -> {
203-
throwable.printStackTrace();
204-
//thread.interrupt();
205-
});
206-
t.start();
207-
return t;
190+
}
191+
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(img, null);
192+
193+
try {
194+
ImageIO.write(bufferedImage, "png", image);
195+
} catch (IOException e) {
196+
throw new RuntimeException(e);
197+
}
198+
199+
return;
208200
}
209201

210202
public static CullFace getCullFaceValue() {

src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void flame() throws IOException {
7979
throw new RuntimeException("Failed to load");
8080
try {
8181
ThumbnailImage.setCullFaceValue(CullFace.NONE);
82-
ThumbnailImage.writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png")).join();
83-
} catch (InterruptedException e) {
82+
new ThumbnailImage().writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png"));
83+
} catch (Exception e) {
8484
// Auto-generated catch block
8585
e.printStackTrace();
8686
}
@@ -130,8 +130,8 @@ public void box() throws IOException {
130130
ArrayList<CSG>parts =run(s);
131131
try {
132132
ThumbnailImage.setCullFaceValue(CullFace.NONE);
133-
ThumbnailImage.writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png")).join();
134-
} catch (InterruptedException e) {
133+
new ThumbnailImage().writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png"));
134+
} catch (Exception e) {
135135
// Auto-generated catch block
136136
e.printStackTrace();
137137
}
@@ -150,8 +150,8 @@ public void inside() throws IOException {
150150
ArrayList<CSG>parts =new ArrayList<>(Arrays.asList(CSG.unionAll(run(s))));
151151
try {
152152
ThumbnailImage.setCullFaceValue(CullFace.NONE);
153-
ThumbnailImage.writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png")).join();
154-
} catch (InterruptedException e) {
153+
new ThumbnailImage() .writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png"));
154+
} catch (Exception e) {
155155
// Auto-generated catch block
156156
e.printStackTrace();
157157
}
@@ -169,8 +169,8 @@ public void adversarial() throws IOException {
169169
ArrayList<CSG>parts =run(s);
170170
try {
171171
ThumbnailImage.setCullFaceValue(CullFace.NONE);
172-
ThumbnailImage.writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png")).join();
173-
} catch (InterruptedException e) {
172+
new ThumbnailImage().writeImage(CSGDatabase.getInstance(),parts,new File(svg.getAbsolutePath()+".png"));
173+
} catch (Exception e) {
174174
// Auto-generated catch block
175175
e.printStackTrace();
176176
}

src/test/java/eu/mihosoft/vrl/v3d/StlLoadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public void test() throws IOException {
2121
CSG loaded = STL.file(file.toPath());
2222
try {
2323
ThumbnailImage.setCullFaceValue(CullFace.NONE);
24-
ThumbnailImage.writeImage(CSGDatabase.getInstance(),loaded,new File(file.getAbsolutePath()+".png")).join();
25-
} catch (InterruptedException e) {
24+
new ThumbnailImage().writeImage(CSGDatabase.getInstance(),loaded,new File(file.getAbsolutePath()+".png"));
25+
} catch (Exception e) {
2626
// Auto-generated catch block
2727
e.printStackTrace();
2828
}

0 commit comments

Comments
 (0)