77import com .neuronrobotics .bowlerstudio .physics .TransformFactory ;
88import com .neuronrobotics .sdk .addons .kinematics .math .RotationNR ;
99import com .neuronrobotics .sdk .addons .kinematics .math .TransformNR ;
10+ import com .neuronrobotics .sdk .common .Log ;
1011
1112import eu .mihosoft .vrl .v3d .Bounds ;
1213import eu .mihosoft .vrl .v3d .CSG ;
2728import javafx .scene .transform .Affine ;
2829
2930public class ThumbnailImage {
30- private HashMap <String ,CSG > csgs = new HashMap <String , CSG >();
31- private HashMap <String ,MeshView > views = new HashMap <String , MeshView >();
31+ private HashMap <String , CSG > csgs = new HashMap <String , CSG >();
32+ private HashMap <String , MeshView > views = new HashMap <String , MeshView >();
3233 // Create a group to hold all the meshes
33- private Group root = new Group ();
34- public Bounds getSellectedBounds () {
34+ private Group root = new Group ();
35+ private Scene scene ;
36+
37+ public Bounds getSellectedBounds () {
3538 Vector3d min = null ;
3639 Vector3d max = null ;
3740 for (CSG c : csgs .values ()) {
38- if (c .isHide ())
41+ if (c .isHide ())
3942 continue ;
40- if (c .isInGroup ())
43+ if (c .isInGroup ())
4144 continue ;
4245 Vector3d min2 = c .getBounds ().getMin ().clone ();
4346 Vector3d max2 = c .getBounds ().getMax ().clone ();
@@ -58,59 +61,60 @@ public Bounds getSellectedBounds() {
5861 if (max .z < max2 .z )
5962 max .z = max2 .z ;
6063 }
61- if (max == null )
62- max = new Vector3d (0 ,0 , 0 );
63- if (min == null )
64- min = new Vector3d (0 ,0 , 0 );
64+ if (max == null )
65+ max = new Vector3d (0 , 0 , 0 );
66+ if (min == null )
67+ min = new Vector3d (0 , 0 , 0 );
6568 return new Bounds (min , max );
6669 }
6770
68- public WritableImage get (CSGDatabaseInstance instance ,List <CSG > c ) {
69- ArrayList <CSG > csgList = new ArrayList <CSG >() ;
70- for (CSG cs : c ) {
71+ public WritableImage get (CSGDatabaseInstance instance , List <CSG > c ) {
72+ ArrayList <CSG > csgList = new ArrayList <CSG >();
73+ for (CSG cs : c ) {
7174 if (csgs .containsKey (cs .getName ()))
7275 continue ;
73- csgs .put (cs .getName (), cs );
74- if (cs .hasManipulator ()) {
76+ csgs .put (cs .getName (), cs );
77+ if (cs .hasManipulator ()) {
7578 TransformNR nr ;
7679 try {
7780 nr = TransformFactory .affineToNr (cs .getManipulator ());
78- csgList .add (cs .transformed (TransformFactory .nrToCSG (nr )).syncProperties (instance ,cs ));
81+ csgList .add (cs .transformed (TransformFactory .nrToCSG (nr )).syncProperties (instance , cs ));
7982 } catch (MissingManipulatorException e ) {
8083 // TODO Auto-generated catch block
8184 e .printStackTrace ();
8285 }
83- }else
86+ } else
8487 csgList .add (cs );
8588 }
8689 ArrayList <String > toRemove = new ArrayList <String >();
87- for (String s : csgs .keySet ()) {
88- boolean exists = false ;
89- for (CSG cs : c ) {
90- if (cs .getName ().contentEquals (s ))
91- exists = true ;
90+ for (String s : csgs .keySet ()) {
91+ boolean exists = false ;
92+ for (CSG cs : c ) {
93+ if (cs .getName ().contentEquals (s ))
94+ exists = true ;
9295 }
93- if (!exists ) {
96+ if (!exists ) {
9497 toRemove .add (s );
9598 }
9699 }
97- for (String s : toRemove ) {
100+ for (String s : toRemove ) {
98101 csgs .remove (s );
99102 MeshView mv = views .remove (s );
100- root .getChildren ().add (mv );
103+ if (mv != null )
104+ root .getChildren ().remove (mv );
105+ Log .debug ("Removing from thumbnail " + s );
101106 }
102107
103-
104108 // Add all meshes to the group
105109 Bounds b = getSellectedBounds ();
106110
107- double yOffset = (b .getMax ().y - b .getMin ().y )/ 2 ;
108- double xOffset =(b .getMax ().x -b .getMin ().x )/ 2 ;
109- double zCenter = (b .getMax ().z -b .getMin ().z )/ 2 ;
111+ double yOffset = (b .getMax ().y - b .getMin ().y ) / 2 ;
112+ double xOffset = (b .getMax ().x - b .getMin ().x ) / 2 ;
113+ double zCenter = (b .getMax ().z - b .getMin ().z ) / 2 ;
110114 for (CSG csg : csgList ) {
111- if (csg .isHide ())
115+ if (csg .isHide ())
112116 continue ;
113- if (csg .isInGroup ())
117+ if (csg .isInGroup ())
114118 continue ;
115119 try {
116120 MeshView meshView = csg .movez (-zCenter ).getMesh ();
@@ -124,10 +128,15 @@ public WritableImage get(CSGDatabaseInstance instance,List<CSG> c) {
124128 material .setSpecularColor (javafx .scene .paint .Color .WHITE );
125129 meshView .setCullFace (CullFace .BACK );
126130 root .getChildren ().add (meshView );
127- }catch (Throwable t ) {
131+ Log .debug ("Adding to thumbnail " + csg .getName ());
132+
133+ } catch (Throwable t ) {
128134 com .neuronrobotics .sdk .common .Log .error (t );
129135 }
130136 }
137+ if (root .getChildren ().size () == 0 ) {
138+ Log .error ("Thumbnail is empty!" );
139+ }
131140
132141 // Calculate the bounds of all CSGs combined
133142 double totalz = b .getMax ().z - b .getMin ().z ;
@@ -139,39 +148,29 @@ public WritableImage get(CSGDatabaseInstance instance,List<CSG> c) {
139148
140149 // Calculate camera position to fit all objects in view
141150 double maxDimension = Math .max (totalx , Math .max (totaly , totalz ));
142- double cameraDistance = (maxDimension / Math .tan (Math .toRadians (camera .getFieldOfView () / 2 )))* 0.8 ;
151+ double cameraDistance = (maxDimension / Math .tan (Math .toRadians (camera .getFieldOfView () / 2 ))) * 0.8 ;
143152
144153 TransformNR camoffset = new TransformNR (xOffset , yOffset , 0 );
145154 TransformNR camDist = new TransformNR (0 , 0 , -cameraDistance );
146155 TransformNR rot = new TransformNR (new RotationNR (-150 , 45 , 0 ));
147-
156+
148157 Affine af = TransformFactory .nrToAffine (camoffset .times (rot .times (camDist )));
149158 camera .getTransforms ().add (af );
150- // Position the camera
151- // camera.setTranslateX();
152- // camera.setTranslateY();
153- // camera.setTranslateZ();
154- // // Apply rotations to the root group instead of the camera
155- // root.getTransforms().addAll(
156- // new Rotate(-5, Rotate.Y_AXIS),
157- // new Rotate(-45, Rotate.X_AXIS)
158- // );
159- // Create a scene with the group and camera
160159 int i = 100 ;
161- Scene scene = new Scene (root , i , i , true , SceneAntialiasing .BALANCED );
162- scene .setFill (Color .TRANSPARENT );
163- scene .setCamera (camera );
164-
160+ if (scene == null ) {
161+ scene = new Scene (root , i , i , true , SceneAntialiasing .BALANCED );
162+ scene .setFill (Color .TRANSPARENT );
163+ scene .setCamera (camera );
164+ }
165165 // Set up snapshot parameters
166166 SnapshotParameters params = new SnapshotParameters ();
167167 params .setFill (Color .TRANSPARENT );
168168 params .setCamera (camera );
169169 params .setDepthBuffer (true );
170170 params .setTransform (Transform .scale (1 , 1 ));
171171 // Set the near and far clip
172- camera .setNearClip (0.1 ); // Set the near clip plane
173- camera .setFarClip (9000.0 ); // Set the far clip plane
174-
172+ camera .setNearClip (0.1 ); // Set the near clip plane
173+ camera .setFarClip (9000.0 ); // Set the far clip plane
175174
176175 // Create the WritableImage first
177176 WritableImage snapshot = new WritableImage (i , i );
0 commit comments