ihmcrobotics / euclid

Vector math, geometry, reference frame, and shapes 2D & 3D
Apache License 2.0
31 stars 9 forks source link

Add `ReferenceFrame#hasBeenRemoved` #57

Open calvertdw opened 1 year ago

calvertdw commented 1 year ago

There's not way to check if a ReferenceFrame has been removed from the tree in order to handle that case properly. The only option currently is checkIsRemoved which is protected and throws a RuntimeException.

SylvainBertrand commented 1 year ago

Typically, the frame gets removed for garbage collection, this was not intended to be a feature allowing a frame to be added and then removed by hand really. I'm getting the feel that you're trying to use the remove thingy in a particular way and we should prob do an iteration on the class to support that properly. Can you expand on what you're trying to do?

calvertdw commented 1 year ago

Sneaky workaround

private static final Field referenceFrameHasBeenRemoved;
static
{
   try
   {
      referenceFrameHasBeenRemoved = ReferenceFrame.class.getDeclaredField("hasBeenRemoved");
      referenceFrameHasBeenRemoved.setAccessible(true);
   }
   catch (NoSuchFieldException e)
   {
      throw new RuntimeException(e);
   }
}
public static boolean hasBeenRemoved(ReferenceFrame referenceFrame)
{
   try
   {
      return referenceFrameHasBeenRemoved.getBoolean(referenceFrame);
   }
   catch (IllegalAccessException e)
   {
      throw new RuntimeException(e);
   }
}
calvertdw commented 1 year ago

Two improvements would help: