Skip to content

Commit a1dce2b

Browse files
committed
1 parent 683c347 commit a1dce2b

File tree

1 file changed

+84
-11
lines changed
  • src/main/java/com/neuronrobotics/bowlerstudio/scripting/cadoodle

1 file changed

+84
-11
lines changed

src/main/java/com/neuronrobotics/bowlerstudio/scripting/cadoodle/Allign.java

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import com.google.gson.annotations.Expose;
99
import com.neuronrobotics.bowlerstudio.physics.TransformFactory;
1010
import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
11+
import com.neuronrobotics.sdk.common.Log;
1112

1213
import eu.mihosoft.vrl.v3d.Bounds;
1314
import eu.mihosoft.vrl.v3d.CSG;
1415
import eu.mihosoft.vrl.v3d.Transform;
16+
import eu.mihosoft.vrl.v3d.Vector3d;
1517
import javafx.scene.transform.Affine;
1618

1719
public class Allign extends CaDoodleOperation{
@@ -25,10 +27,15 @@ public class Allign extends CaDoodleOperation{
2527
public Allignment x=null;
2628
@Expose (serialize = true, deserialize = true)
2729
private TransformNR workplane=null;
30+
@Deprecated
2831
@Expose (serialize = true, deserialize = true)
2932
public StoragbeBounds bounds=null;
33+
@Expose (serialize = true, deserialize = true)
34+
private List<String> boundNames = null;
35+
3036
@Expose(serialize = true, deserialize = true)
3137
protected String name = null;
38+
3239
public String getName() {
3340
if (name == null) {
3441
setName(RandomStringFactory.generateRandomString());
@@ -58,12 +65,8 @@ public List<CSG> process(List<CSG> incoming) {
5865
ArrayList<CSG> back = new ArrayList<CSG>();
5966
back.addAll(incoming);
6067

61-
Bounds bounds2 ;//
62-
if(bounds!=null) {
63-
bounds2=bounds.getBounds();
64-
}else {
65-
throw new RuntimeException("Allign can not be initialized without bounds!");
66-
}
68+
Bounds bounds2 =getBounds(incoming);//
69+
6770
HashMap<String,TransformNR> moves= new HashMap<>();
6871
HashMap<String,CSG> objects = new HashMap<String, CSG>();
6972
for(String name :names) {
@@ -194,13 +197,83 @@ public Allign setWorkplane(TransformNR workplane) {
194197
return this;
195198
}
196199

197-
public Bounds getBounds() {
198-
return bounds.getBounds();
200+
public Bounds getBounds(List<CSG> incoming) {
201+
if(bounds!=null) {
202+
Log.error("Depricated Bounds in the allign step!");
203+
return bounds.getBounds();
204+
}if(boundNames!=null) {
205+
List<CSG> selectedCSG = getSelectedCSG(boundNames,incoming);
206+
return Allign.getBounds(selectedCSG, workplane, new HashMap<CSG, Bounds>());
207+
}
208+
else {
209+
throw new RuntimeException("Allign can not be initialized without bounds!");
210+
}
211+
199212
}
200-
201-
public Allign setBounds(Bounds bounds) {
202-
this.bounds = new StoragbeBounds(bounds);
213+
public List<CSG> getSelectedCSG(Iterable<String> sele,List<CSG> incoming) {
214+
ArrayList<CSG> back = new ArrayList<CSG>();
215+
for (String sel : sele) {
216+
CSG t = getSelectedCSG(sel,incoming);
217+
if (t != null) {
218+
back.add(t);
219+
}
220+
}
221+
return back;
222+
}
223+
private CSG getSelectedCSG(String string,List<CSG> incoming) {
224+
for (CSG c :incoming) {
225+
if (c.getName().contentEquals(string))
226+
return c;
227+
}
228+
return null;
229+
}
230+
public Allign setBounds(List<String> boundNames) {
231+
this.boundNames=boundNames;
203232
return this;
204233
}
234+
public static Bounds getBounds(List<CSG> incoming, TransformNR frame, HashMap<CSG, Bounds> cache) {
235+
if (cache == null)
236+
cache = new HashMap<>();
237+
Vector3d min = null;
238+
Vector3d max = null;
239+
// TickToc.tic("getSellectedBounds "+incoming.size());
205240

241+
for (CSG csg : incoming) {
242+
if (cache.get(csg) == null) {
243+
Transform inverse = TransformFactory.nrToCSG(frame).inverse();
244+
Affine af = csg.getManipulator();
245+
if(af!=null) {
246+
TransformNR afNR = TransformFactory.affineToNr(af);
247+
inverse = TransformFactory.nrToCSG(afNR.inverse().times(frame)).inverse();
248+
}
249+
cache.put(csg, csg.transformed(inverse).getBounds());
250+
}
251+
Bounds b = cache.get(csg);
252+
Vector3d min2 = b.getMin().clone();
253+
Vector3d max2 = b.getMax().clone();
254+
if (min == null)
255+
min = min2;
256+
if (max == null)
257+
max = max2;
258+
if (min2.x < min.x)
259+
min.x = min2.x;
260+
if (min2.y < min.y)
261+
min.y = min2.y;
262+
if (min2.z < min.z)
263+
min.z = min2.z;
264+
if (max.x < max2.x)
265+
max.x = max2.x;
266+
if (max.y < max2.y)
267+
max.y = max2.y;
268+
if (max.z < max2.z)
269+
max.z = max2.z;
270+
// TickToc.tic("Bounds for "+c.getName());
271+
if(min==null || max ==null) {
272+
Log.error("Failed to find bounds!");
273+
throw new RuntimeException("Failed to find bounds!!");
274+
}
275+
}
276+
277+
return new Bounds(min, max);
278+
}
206279
}

0 commit comments

Comments
 (0)