Class Clipping

java.lang.Object
com.eco.bio7.spatial.Clipping

public class Clipping extends Object
Clipping of the volume and the iso surface in the 3D view (to make the interior visible):
  • A clipping box: the parts outside of the box are removed (relative positions 0 - 1 in the extent of the displayed data).
  • A free cutting plane (azimuth, elevation and offset). The part on the front side of the plane is removed (flip removes the other side).
  • A slice: the cross-section of the plane with the data (the colors of the volume, see VolumeRenderer) and the outline of the plane.
The clipping uses the OpenGL clip planes (fast, only the display is clipped). With setCutMesh the iso surface mesh is cut (closed with caps, the export contains the cut surface).

Voxels can be removed with a mask (cuts with an ImageJ selection, a mask image or an array, see cutRoi and setMask). The mask is applied to the volume and to the mesh of the iso surface.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    The removed voxels (immutable).
    static interface 
    The listener of the measurement tools: the mouse events with the window position (pixels of the OpenGL surface, y from the top) and the view ray through the data (relative positions, start and end, or null outside).
    static final class 
    The region of the clipping (the kept side of the box and the plane and the kept voxels of the mask) for relative positions (0 - 1, y = 0 is the first row like in ImageJ).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Measurement: an angle (three clicks, the second point is the vertex).
    static final int
    The mouse removes voxels along the view direction (Shift: restores voxels).
    static final int
    Measurement: a freehand area on the surface (drag).
    static final int
    The mouse rotates the view (no tool).
    static final int
    A mouse click picks a point on the displayed data (see setPickListener): a line (ruler).
    static final int
    The mouse moves the cutting plane (Shift: rotates the plane).
    static final int
    Measurement: single points (position, value, object).
    static final int
    Measurement: a polygon on the surface (clicks, a double-click closes it).
    static final int
    Measurement: a straight line (two clicks or a drag).
    static final int
    The surface eraser: a drag removes the visible triangles of the iso surface under the brush (cut open).
    static final int
    Measurement: the volume of an object (click) or inside of an outline (drag).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a listener which is called after a change of the clipping (not in the user interface thread).
    void
    begin(com.jogamp.opengl.GL2 gl, double[] bounds)
    Enables the clip planes for the following rendering (the scene coordinates of the current model view matrix).
    boolean
    Returns true if a change of the mask can be undone.
    void
    Removes the mask (all cuts).
    boolean
    cutRoi(ij.gui.Roi roi, int w, int h, int d, boolean inside, int firstSlice, int lastSlice)
    Removes the voxels inside (or outside) of a selection (in the pixel coordinates of a stack with the given size) in a range of slices.
    boolean
    cutRoi(ij.ImagePlus imp, boolean inside)
    Removes the voxels inside (or outside) of the selection of the image in all slices (the cuts are combined, see undoCut and clearMask).
    boolean
    cutRoi(ij.ImagePlus imp, boolean inside, int firstSlice, int lastSlice)
    Removes the voxels inside (or outside) of the selection of the image in a range of slices (the cuts are combined, see undoCut and clearMask).
    void
    drawOutline(com.jogamp.opengl.GL2 gl, double[] bounds)
    Draws the outline of the cutting plane (if enabled).
    void
    end(com.jogamp.opengl.GL2 gl)
    Disables the clip planes.
    double[]
    Returns the box (xmin, xmax, ymin, ymax, zmin, zmax relative 0 - 1).
    int
     
    Returns the mask of the removed voxels or null.
    double[]
     
    List<double[]>
    getPlanes(double[] bounds)
    Returns the planes of the box and of the cutting plane (if enabled).
    getRegion(double[] extent, boolean flip)
    Returns the region of the clipping.
    int
     
    void
    Inverts the mask (the removed voxels are kept and vice versa).
    boolean
    Returns true if a clipping is enabled.
    boolean
     
    boolean
     
    boolean
     
    static boolean
    isMeasureTool(int tool)
    Returns true if the tool is a measurement tool (see setMeasureToolListener).
    boolean
     
    boolean
     
    boolean
    Returns true if the shift key was pressed with the last press of the mouse.
    boolean
     
    List<double[]>
    planePolygon(double[] bounds)
    Returns the polygon of the cutting plane inside of the (clipped) box (the corners in scene coordinates, sorted around the center) or null.
    double[]
    projectToWindow(double[] relative)
    Returns the window position (pixels of the OpenGL surface, y from the top, like the mouse events of the tools) of a position of the data (relative 0 - 1, y = 0 is the first row) in the last drawn frame, or null.
    double[][]
    rayAt(double x, double y)
    Returns the view ray (start and end, relative positions 0 - 1 of the data) of a window position (pixels of the OpenGL surface, from the left and from the top, like the mouse events of the tools) in the last drawn frame, or null.
    void
     
    void
    Resets the box, the plane (disabled), the mesh cut and removes the mask.
    void
    setBox(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax)
    Sets the clipping box (relative positions 0 - 1 in the extent of the displayed data, y is the vertical axis).
    void
    setBoxEnabled(boolean enabled)
     
    void
    setBoxLimit(int axis, boolean max, double value)
    Sets the minimum or maximum of an axis (axis 0 = x, 1 = y, 2 = z).
    void
    setCutMesh(boolean cutMesh)
    Cuts the mesh of the iso surface with the box and the plane (computed again, the cut surface is closed with caps and is exported).
    void
    setDataSize(int w, int h, int d)
    Sets the size of the displayed data (the resolution of a new mask of the eraser).
    void
    setEraserSize(int size)
    Sets the radius of the eraser (in voxels of the mask).
    void
    setFlip(boolean flip)
    Removes the other side of the plane.
    void
    setMask(boolean[] keep, int w, int h, int d)
    Sets a mask (e.g., of a model): the voxels with false are removed.
    void
    setMask(ij.ImagePlus imp)
    Sets a mask image (a binary stack, e.g., of a segmentation): the voxels with the value 0 are removed (the current channel and frame).
    void
    Sets the listener of the measurement tools (see MeasureToolListener).
    void
    setOutline(boolean outline)
    Draws the outline of the cutting plane.
    void
    setPickListener(Consumer<double[][]> listener)
    Sets the listener of the pick tool (TOOL_PICK).
    void
    setPlane(double azimuth, double elevation, double offset)
    Sets the cutting plane.
    void
    setPlaneEnabled(boolean enabled)
     
    void
    setSlice(boolean slice)
    Displays the cross-section of the plane with the data (see VolumeRenderer).
    void
    setTool(int tool)
    Sets the mouse tool of the 3D view (the left mouse button): TOOL_NONE, TOOL_PLANE or TOOL_ERASER.
    void
    setView(com.jogamp.opengl.GL2 gl, double[] bounds, boolean flip)
    Stores the matrices of the view for the mouse tools (of each frame).
    void
    toolDragged(double x, double y)
    Continues the drag of the mouse tool (see toolPressed).
    void
    toolMoved(double x, double y)
    The mouse moved without a pressed button: sent to the measurement tools (e.g., the preview of the volume tool).
    boolean
    toolPressed(double x, double y, boolean shift)
    Starts a drag of the mouse tool.
    boolean
    toolPressed(double x, double y, boolean shift, int clickCount)
    Starts a drag of the mouse tool (see toolPressed(double, double, boolean)) with the click count of the mouse event (2 = a double-click, e.g., closes a polygon).
    void
    Ends the drag of the mouse tool.
    void
    toolReleased(double x, double y)
    Ends the drag of the mouse tool at the window position (see toolPressed).
    void
    Undoes the last change of the mask (a cut, setMask, invertMask or clearMask).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TOOL_NONE

      public static final int TOOL_NONE
      The mouse rotates the view (no tool).
      See Also:
    • TOOL_PLANE

      public static final int TOOL_PLANE
      The mouse moves the cutting plane (Shift: rotates the plane).
      See Also:
    • TOOL_ERASER

      public static final int TOOL_ERASER
      The mouse removes voxels along the view direction (Shift: restores voxels).
      See Also:
    • TOOL_PICK

      public static final int TOOL_PICK
      A mouse click picks a point on the displayed data (see setPickListener): a line (ruler).
      See Also:
    • TOOL_POINT

      public static final int TOOL_POINT
      Measurement: single points (position, value, object).
      See Also:
    • TOOL_POLYGON

      public static final int TOOL_POLYGON
      Measurement: a polygon on the surface (clicks, a double-click closes it).
      See Also:
    • TOOL_FREEHAND

      public static final int TOOL_FREEHAND
      Measurement: a freehand area on the surface (drag).
      See Also:
    • TOOL_VOLUME

      public static final int TOOL_VOLUME
      Measurement: the volume of an object (click) or inside of an outline (drag).
      See Also:
    • TOOL_STRAIGHT

      public static final int TOOL_STRAIGHT
      Measurement: a straight line (two clicks or a drag).
      See Also:
    • TOOL_ANGLE

      public static final int TOOL_ANGLE
      Measurement: an angle (three clicks, the second point is the vertex).
      See Also:
    • TOOL_SURFACE_ERASER

      public static final int TOOL_SURFACE_ERASER
      The surface eraser: a drag removes the visible triangles of the iso surface under the brush (cut open).
      See Also:
  • Constructor Details

    • Clipping

      public Clipping()
  • Method Details

    • isMeasureTool

      public static boolean isMeasureTool(int tool)
      Returns true if the tool is a measurement tool (see setMeasureToolListener).
    • setMeasureToolListener

      public void setMeasureToolListener(Clipping.MeasureToolListener listener)
      Sets the listener of the measurement tools (see MeasureToolListener).
    • isShiftPressed

      public boolean isShiftPressed()
      Returns true if the shift key was pressed with the last press of the mouse.
    • getRegion

      public Clipping.Region getRegion(double[] extent, boolean flip)
      Returns the region of the clipping.
      Parameters:
      extent - the displayed size of the data (width, height, depth with the z scale)
      flip - true if the first row of the data is at the bottom
      Returns:
      the region
    • setPickListener

      public void setPickListener(Consumer<double[][]> listener)
      Sets the listener of the pick tool (TOOL_PICK). The listener gets the view ray through the data: the start and the end point (relative positions 0 - 1, y = 0 is the first row) and is not called in the user interface thread.
    • addChangeListener

      public void addChangeListener(Runnable listener)
      Adds a listener which is called after a change of the clipping (not in the user interface thread).
    • removeChangeListener

      public void removeChangeListener(Runnable listener)
    • isActive

      public boolean isActive()
      Returns true if a clipping is enabled.
    • isBoxEnabled

      public boolean isBoxEnabled()
    • setBoxEnabled

      public void setBoxEnabled(boolean enabled)
    • setBox

      public void setBox(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax)
      Sets the clipping box (relative positions 0 - 1 in the extent of the displayed data, y is the vertical axis).
    • getBox

      public double[] getBox()
      Returns the box (xmin, xmax, ymin, ymax, zmin, zmax relative 0 - 1).
    • setBoxLimit

      public void setBoxLimit(int axis, boolean max, double value)
      Sets the minimum or maximum of an axis (axis 0 = x, 1 = y, 2 = z).
    • isPlaneEnabled

      public boolean isPlaneEnabled()
    • setPlaneEnabled

      public void setPlaneEnabled(boolean enabled)
    • setPlane

      public void setPlane(double azimuth, double elevation, double offset)
      Sets the cutting plane.
      Parameters:
      azimuth - the direction of the normal around the vertical axis (degree, 0 = +x)
      elevation - the elevation of the normal (degree, 90 = upwards)
      offset - the position along the normal (-1 - 1, 0 = the center)
    • getPlane

      public double[] getPlane()
    • isFlip

      public boolean isFlip()
    • setFlip

      public void setFlip(boolean flip)
      Removes the other side of the plane.
    • isOutline

      public boolean isOutline()
    • setOutline

      public void setOutline(boolean outline)
      Draws the outline of the cutting plane.
    • isSlice

      public boolean isSlice()
    • setSlice

      public void setSlice(boolean slice)
      Displays the cross-section of the plane with the data (see VolumeRenderer).
    • isCutMesh

      public boolean isCutMesh()
    • setCutMesh

      public void setCutMesh(boolean cutMesh)
      Cuts the mesh of the iso surface with the box and the plane (computed again, the cut surface is closed with caps and is exported). Else only the display is clipped.
    • getTool

      public int getTool()
    • setTool

      public void setTool(int tool)
      Sets the mouse tool of the 3D view (the left mouse button): TOOL_NONE, TOOL_PLANE or TOOL_ERASER.
    • getEraserSize

      public int getEraserSize()
    • setEraserSize

      public void setEraserSize(int size)
      Sets the radius of the eraser (in voxels of the mask).
    • setDataSize

      public void setDataSize(int w, int h, int d)
      Sets the size of the displayed data (the resolution of a new mask of the eraser).
    • setView

      public void setView(com.jogamp.opengl.GL2 gl, double[] bounds, boolean flip)
      Stores the matrices of the view for the mouse tools (of each frame).
      Parameters:
      gl - the OpenGL context (the scene coordinates of the current model view matrix)
      bounds - the extent of the data (x0, x1, y0, y1, z0, z1) or null
      flip - true if the first row of the data is at the bottom
    • toolPressed

      public boolean toolPressed(double x, double y, boolean shift)
      Starts a drag of the mouse tool.
      Parameters:
      x - the x position (pixels of the OpenGL surface, from the left)
      y - the y position (pixels of the OpenGL surface, from the top)
      shift - true if the shift key is pressed
      Returns:
      true if the tool handles the drag (else the view is rotated)
    • toolPressed

      public boolean toolPressed(double x, double y, boolean shift, int clickCount)
      Starts a drag of the mouse tool (see toolPressed(double, double, boolean)) with the click count of the mouse event (2 = a double-click, e.g., closes a polygon).
    • toolMoved

      public void toolMoved(double x, double y)
      The mouse moved without a pressed button: sent to the measurement tools (e.g., the preview of the volume tool).
    • toolDragged

      public void toolDragged(double x, double y)
      Continues the drag of the mouse tool (see toolPressed).
    • toolReleased

      public void toolReleased()
      Ends the drag of the mouse tool.
    • toolReleased

      public void toolReleased(double x, double y)
      Ends the drag of the mouse tool at the window position (see toolPressed).
    • rayAt

      public double[][] rayAt(double x, double y)
      Returns the view ray (start and end, relative positions 0 - 1 of the data) of a window position (pixels of the OpenGL surface, from the left and from the top, like the mouse events of the tools) in the last drawn frame, or null.
    • projectToWindow

      public double[] projectToWindow(double[] relative)
      Returns the window position (pixels of the OpenGL surface, y from the top, like the mouse events of the tools) of a position of the data (relative 0 - 1, y = 0 is the first row) in the last drawn frame, or null.
    • reset

      public void reset()
      Resets the box, the plane (disabled), the mesh cut and removes the mask.
    • getMask

      public Clipping.Mask getMask()
      Returns the mask of the removed voxels or null.
    • cutRoi

      public boolean cutRoi(ij.ImagePlus imp, boolean inside)
      Removes the voxels inside (or outside) of the selection of the image in all slices (the cuts are combined, see undoCut and clearMask).
      Parameters:
      imp - the image with the selection (the size of the stack of the volume or iso surface)
      inside - true removes the inside, false the outside of the selection
      Returns:
      false if the image has no area selection
    • cutRoi

      public boolean cutRoi(ij.ImagePlus imp, boolean inside, int firstSlice, int lastSlice)
      Removes the voxels inside (or outside) of the selection of the image in a range of slices (the cuts are combined, see undoCut and clearMask).
      Parameters:
      imp - the image with the selection (the size of the stack of the volume or iso surface)
      inside - true removes the inside, false the outside of the selection
      firstSlice - the first slice (1 - n)
      lastSlice - the last slice (1 - n, larger values are the last slice)
      Returns:
      false if the image has no area selection
    • cutRoi

      public boolean cutRoi(ij.gui.Roi roi, int w, int h, int d, boolean inside, int firstSlice, int lastSlice)
      Removes the voxels inside (or outside) of a selection (in the pixel coordinates of a stack with the given size) in a range of slices.
      Parameters:
      roi - the area selection
      w - the width of the stack
      h - the height of the stack
      d - the number of slices of the stack
      inside - true removes the inside, false the outside of the selection
      firstSlice - the first slice (1 - n)
      lastSlice - the last slice (1 - n, larger values are the last slice)
      Returns:
      false if the selection is not an area
    • setMask

      public void setMask(ij.ImagePlus imp)
      Sets a mask image (a binary stack, e.g., of a segmentation): the voxels with the value 0 are removed (the current channel and frame). The previous cuts are replaced.
      Parameters:
      imp - the mask image (the size of the stack of the volume or iso surface)
    • setMask

      public void setMask(boolean[] keep, int w, int h, int d)
      Sets a mask (e.g., of a model): the voxels with false are removed. The previous cuts are replaced.
      Parameters:
      keep - the kept voxels (index = (z * height + y) * width + x)
      w - the width
      h - the height
      d - the depth
    • invertMask

      public void invertMask()
      Inverts the mask (the removed voxels are kept and vice versa).
    • undoCut

      public void undoCut()
      Undoes the last change of the mask (a cut, setMask, invertMask or clearMask).
    • canUndoCut

      public boolean canUndoCut()
      Returns true if a change of the mask can be undone.
    • clearMask

      public void clearMask()
      Removes the mask (all cuts).
    • begin

      public void begin(com.jogamp.opengl.GL2 gl, double[] bounds)
      Enables the clip planes for the following rendering (the scene coordinates of the current model view matrix).
      Parameters:
      gl - the OpenGL context
      bounds - the extent of the data (x0, x1, y0, y1, z0, z1)
    • getPlanes

      public List<double[]> getPlanes(double[] bounds)
      Returns the planes of the box and of the cutting plane (if enabled). Kept are the points with a x + b y + c z + d >= 0 (the normal (a, b, c) has the length 1, the distance is in the units of the bounds).
      Parameters:
      bounds - the extent of the data (x0, x1, y0, y1, z0, z1)
      Returns:
      the planes (a, b, c, d)
    • end

      public void end(com.jogamp.opengl.GL2 gl)
      Disables the clip planes.
    • planePolygon

      public List<double[]> planePolygon(double[] bounds)
      Returns the polygon of the cutting plane inside of the (clipped) box (the corners in scene coordinates, sorted around the center) or null.
      Parameters:
      bounds - the extent of the data (x0, x1, y0, y1, z0, z1)
      Returns:
      the polygon or null
    • drawOutline

      public void drawOutline(com.jogamp.opengl.GL2 gl, double[] bounds)
      Draws the outline of the cutting plane (if enabled).