3232
3333public 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 () {
0 commit comments