[vtkusers] vtkBoxWidget and vtkImageActor

Sercani sercanimailgroups at gmail.com
Fri Jul 24 03:45:03 EDT 2009


Hi Paulo;
I tried to clip vtkImageActor just like you formerly but i couldn't 
achieve it so i decided to use vtkImageClip(you are trying to clip the 
image, right?)...I put four lines which bound the vtkImageActor, and use 
these lines coordinates to find the structured points of underlying 
image corresponding to bounds of the lines area...I don't know if there 
is an easier way to do this but my code works for me...Actually you 
don't have to put vtkLineRepresentation like me, you can do the same 
thing with vtkBoxWidget.Check out my code, i'm sure it will give some 
ideas to you...:

Regards...

import java.awt.Point;
import java.awt.event.MouseEvent;
import org.akgun.client3d.MPRPanel;
import org.akgun.client3d.utils.Point3D;
import vtk.vtkImageClip;
import vtk.vtkImageData;
import vtk.vtkTransform;


public class ClipPlane2D {

    private MPRPanel panel;
    LineWidget left = new LineWidget();
    LineWidget right = new LineWidget();
    LineWidget top = new LineWidget();
    LineWidget bottom = new LineWidget();
    int dim[] = new int[3];
    Point3D topleftCorner = new Point3D();
    Point3D toprightCorner = new Point3D();
    Point3D bottomleftCorner = new Point3D();
    Point3D bottomrightCorner = new Point3D();
    int[] color = {255, 255, 255};
    private boolean enabled = false;
    private vtkImageClip clip;
    Point tl = new Point();
    Point br = new Point();
    int oldextent[] = new int[6];

    private enum lines {

        TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
    }
    private lines selected;

    public ClipPlane2D(MPRPanel panel) {
        this.panel = panel;
        clip = new vtkImageClip();
    }

    public void createVTKPipeline() {
        setLines(left);
        setLines(right);
        setLines(top);
        setLines(bottom);
        clip.SetInput(panel.getImageactor().GetInput());
    }

    private void setLines(ShapesWidget s) {
        s.setPanel(panel);
        s.setDashed(true);
        s.setLabelVisibility(false);
        s.addActorsToRenderer();
        s.setWhichOneIsThis(0);
        s.setLineColor(color);
        s.setVisibility(true);
    }

    public void setEnabled(boolean value) {
        panel.lock();
        if (value) {
            enabled = true;
            left.setVisibility(value);
            right.setVisibility(value);
            top.setVisibility(value);
            bottom.setVisibility(value);

            double bounds[] = new double[6];
            bounds = panel.getImageactor().GetBounds();
            System.out.print("Bounds = ");
            for (int i = 0; i < bounds.length; i++) {
                System.out.print(bounds[i] + " ");
            }
            System.out.println("");
            bottomleftCorner = new Point3D(bounds[0], bounds[2], 1);
            bottomrightCorner = new Point3D(bounds[1], bounds[2], 1);
            topleftCorner = new Point3D(bounds[0], bounds[3], 1);
            toprightCorner = new Point3D(bounds[1], bounds[3], 1);
            tl = topleftCorner.translateToPoint(panel);
            br = bottomrightCorner.translateToPoint(panel);
            drawThemAll();
        } else {
            enabled = false;
            left.setVisibility(value);
            right.setVisibility(value);
            top.setVisibility(value);
            bottom.setVisibility(value);
            drawThemAll();
        }
        panel.Render();
        panel.unlock();
    }

    public void drawThemAll() {
        panel.lock();

        left.setP1(topleftCorner.translateToPoint(panel));
        left.setP2(bottomleftCorner.translateToPoint(panel));
        right.setP1(toprightCorner.translateToPoint(panel));
        right.setP2(bottomrightCorner.translateToPoint(panel));
        top.setP1(topleftCorner.translateToPoint(panel));
        top.setP2(toprightCorner.translateToPoint(panel));
        bottom.setP1(bottomleftCorner.translateToPoint(panel));
        bottom.setP2(bottomrightCorner.translateToPoint(panel));
        top.draw();
        bottom.draw();
        left.draw();
        right.draw();
        clipThemAll();
        panel.unlock();
    }

    public void clipThemAll() {
        panel.lock();

        vtkImageData data = panel.getImageactor().GetInput();
        double pcoords[] = new double[3];
        int ijk_topleft[] = new int[3];
        data.ComputeStructuredCoordinates(topleftCorner.getAll(), 
ijk_topleft, pcoords);
                int ijk_topright[] = new int[3];
        data.ComputeStructuredCoordinates(toprightCorner.getAll(), 
ijk_topright, pcoords);
        int ijk_bottomleft[] = new int[3];
        data.ComputeStructuredCoordinates(bottomleftCorner.getAll(), 
ijk_bottomleft, pcoords);
               int ijk_bottomright[] = new int[3];
        data.ComputeStructuredCoordinates(bottomrightCorner.getAll(), 
ijk_bottomright, pcoords);
               int[] extent = panel.getImageactor().GetInput().GetExtent();
        int newextent[] = new int[6];
        newextent[0] = ijk_bottomleft[0];
        newextent[1] = ijk_bottomright[0];
        newextent[2] = ijk_bottomleft[1];
        newextent[3] = ijk_topleft[1];
        newextent[4] = ijk_bottomleft[2];
        newextent[5] = ijk_bottomleft[2];
        for (int i = 0; i < 6; i++) {
            if (newextent[i] < 0) {
                System.out.println("i%2=" + (i % 2));
            }
        }
        System.out.println("Extent = " + newextent[0] + " " + 
newextent[1] + " " + newextent[2] + " " + newextent[3] + " " + 
newextent[4] + " " + newextent[5]);
        clip.SetOutputWholeExtent(newextent[0], newextent[1], 
newextent[2], newextent[3], newextent[4], newextent[5]);
        panel.getImageactor().SetInput(clip.GetOutput());
        panel.unlock();
    }

    public void mousePressed(MouseEvent event) {
        if (enabled) {
            Point p = event.getPoint();
            selected = null;
            if (left.isSelected(p) && top.isSelected(p)) {
                selected = lines.TOP_LEFT;
            } else if (right.isSelected(p) && top.isSelected(p)) {
                selected = lines.TOP_RIGHT;
            } else if (bottom.isSelected(p) && left.isSelected(p)) {
                selected = lines.BOTTOM_LEFT;
            } else if (bottom.isSelected(p) && right.isSelected(p)) {
                selected = lines.BOTTOM_RIGHT;
            }
            if (selected != null) {
                System.out.println("Selected=" + selected.toString());
            }
        }
    }

    public void mouseDragged(MouseEvent event) {
        if (enabled) {
            Point p = event.getPoint();
            if (selected != null) {
                switch (selected) {
                    case TOP_LEFT:
                        System.out.println("Selected=" + 
selected.toString());
                        System.out.println("Dragged");
                        topleftCorner.translateFromPoint(p, new 
vtkTransform(), panel);
                        bottomleftCorner.x = topleftCorner.x;
                        toprightCorner.y = topleftCorner.y;
                        break;
                    case TOP_RIGHT:
                        System.out.println("Selected=" + 
selected.toString());
                        System.out.println("Dragged");
                        toprightCorner.translateFromPoint(p, new 
vtkTransform(), panel);
                        topleftCorner.y = toprightCorner.y;
                        bottomrightCorner.x = toprightCorner.x;
                        break;
                    case BOTTOM_LEFT:
                        System.out.println("Selected=" + 
selected.toString());
                        System.out.println("Dragged");
                        bottomleftCorner.translateFromPoint(p, new 
vtkTransform(), panel);
                        bottomrightCorner.y = bottomleftCorner.y;
                        topleftCorner.x = bottomleftCorner.x;
                        break;
                    case BOTTOM_RIGHT:
                        System.out.println("Selected=" + 
selected.toString());
                        System.out.println("Dragged");
                        bottomrightCorner.translateFromPoint(p, new 
vtkTransform(), panel);
                        bottomleftCorner.y = bottomrightCorner.y;
                        toprightCorner.x = bottomrightCorner.x;
                        break;
                }
                drawThemAll();
            }
        }

    }

    public void mouseReleased(MouseEvent event) {
        selected = null;
    }
}






Paulo Henrique Junqueira Amorim wrote:
> Hi Sercani,
>
> I'm trying tdo use the vtkBoxWidget overlapped with vtkImageActor, but 
> when you step on the mouse on the Interactor my application opens and 
> appears the error run time error (image attached).
>
> I'm using InteractorStyleImage.
>
> The VTK is 5.4.2 with Python wrapper, Windows XP SP2
>
> The code add widget:
>
>         wid = vtk.vtkBoxWidget()
>         wid.SetInteractor(self.interactor)
>         wid.SetInput(self.imagedata)
>         wid.PlaceWidget()
>         wid.On()
>
> You have idea?
>
>
> Regards,
> Paulo Amorim
>
>
> ------------------------------------------------------------------------
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090724/fd499bd7/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 24471 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090724/fd499bd7/attachment.jpeg>


More information about the vtkusers mailing list