[vtkusers] vtkLookUpTable negative scalars

Cory Quammen cory.quammen at kitware.com
Fri Sep 4 16:47:53 EDT 2015


Hi James,

I had some trouble with the example code. The VTK window that pops up
displays the visualization briefly then goes black and is hidden by the
main JFrame. When I see the visualization, the scalar bar appears both when
the values are all negative and when they are all positive. I haven't seen
any problems with a completely negative range when looking at data in
ParaView, which uses VTK for visualization, either.

I suggest trying to get the VTK renderer in the main JFrame. You can take a
look at [1] to see an example of how this might work.

Thanks,
Cory

http://www.vtk.org/Wiki/VTK/Examples/Java/SwingIntegration/JFrameRenderer

On Mon, Aug 31, 2015 at 11:23 AM, James Labiak <
jim at jslengineeringsoftware.com> wrote:

> Hi Cory,
> Here's a java example below. Also attached with some_points.txt file
> (warning: z, theta, dR and not x,y,z).  Just comment the dRi sign change
> line in order to change the scalars from negative to positive.
> Thanks,
> Jim
>
> import java.awt.BorderLayout;
> import java.io.BufferedReader;
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
> import java.io.IOException;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.SwingUtilities;
> import vtk.*;
>
> public class PlotSpheres extends JPanel {
>
>     public static void main(String[] args) throws FileNotFoundException {
>         SwingUtilities.invokeLater(new Runnable() {
>             public void run() {
>                 JFrame frame = new JFrame("PlotSheres");
>                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>                 frame.getContentPane().setLayout(new BorderLayout());
>                 try {
>                     frame.getContentPane().add(new PlotSpheres(),
>                             BorderLayout.CENTER);
>                 } catch (FileNotFoundException e) {
>                     e.printStackTrace();
>                 }
>                 frame.setSize(1000, 1000);
>                 frame.setVisible(true);
>             }
>         });
>     }
>
>     // Load VTK libraries
>     static {
>         if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
>             for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
>                 if (!lib.IsLoaded())
>                     System.out.println(lib.GetLibraryName() + " not
> loaded");
>             }
>             System.out.println("Make sure the search path is correct: ");
>             System.out.println(System.getProperty("java.library.path"));
>         }
>         vtkNativeLibrary.DisableOutputWindow(null);
>     }
>
>     // Variables
>     private String dataFile;
>     private String dataFilePathString;
>     Double[] xValsArray = new Double[41];
>     Double[] yValsArray = new Double[721];
>     Double[][] zValsArray = new Double[41][721];
>     private vtkRenderWindowPanel renderWindowPanel = new
> vtkRenderWindowPanel();
>     private vtkPoints points;
>     private int pointsNum;
>     private vtkRenderWindowInteractor renderWindowInteractor;
>     private vtkPolyData polyData = new vtkPolyData();
>     private vtkFloatArray colorsScalars;
>
>     public PlotSpheres() throws FileNotFoundException {
>         super(new BorderLayout());
>         dataFilePathString = "C:\\vtk123";
>         dataFile = "some_points.txt";
>         calcSheres();
>         System.out.println("calcSheres done");
>     }
>
>     public void calcSheres() throws FileNotFoundException {
>         try {
>             renderWindowPanel.GetRenderer().RemoveAllViewProps();
>         } catch (Exception nullEx) {
>             System.out.println("renderWindowPanel props are null.");
>         }
>
>         // Read the file
>         // Count the lines in the file
>         BufferedReader br_count = new BufferedReader(new FileReader(
>                 dataFilePathString + File.separator + dataFile));
>         String lineData = null;
>         int lineNum = 0;
>         try {
>             lineData = br_count.readLine();
>             // The first data line has just been read
>             while (lineData != null) {
>                 // Do nothing except increment the counter
>                 lineNum = lineNum + 1; // on iteration 1, lineNum = 1
>                 // Read blank line
>                 lineData = br_count.readLine();
>                 // Next action is in for the  br_count.readLine() (the
>                 // next data line)
>                 lineData = br_count.readLine();
>                 System.out.println("lineNum = " + lineNum + "   " +
> lineData);
>             }
>         } catch (Exception e) {
>             e.printStackTrace();
>             System.out.println("Datafile line counting error");
>         }
>         try {
>             br_count.close();
>         } catch (IOException e) {
>             System.out.println("Error closing br_count");
>             e.printStackTrace();
>         }
>
>         BufferedReader br = new BufferedReader(new FileReader(
>                 dataFilePathString + File.separator + dataFile));
>         // Set number of data point sets (x,y,z sets)
>         pointsNum = lineNum;
>
>         // Fill colorsScalars array with the actual scalar values
>         // Initialize/reinitialize variables
>         String linej;
>         int indexNum = 0;
>         points = new vtkPoints();
>         points.SetNumberOfPoints(pointsNum);
>         Double ZPOSi;
>         Double theta;
>         Double dRi;
>         Double tempx;
>         Double tempy;
>         Double tempz;
>         // colorsScalars is the array of native (pos or neg) scalars
>         // the sign is changed to meet the required application convention
>         // the sign change line can be used to show difference between
>         // plotting of positive and negative scalars
>         colorsScalars = new vtkFloatArray();
>         colorsScalars.SetNumberOfTuples(pointsNum);
>         String[] linejArray = {};
>         try {
>             linej = br.readLine();
>             while (linej != null) {
>                 linejArray = linej.split("\\s+");
>                 ZPOSi = Double.parseDouble(linejArray[1]); // Z position
> (m)
>                 theta = Double.parseDouble(linejArray[2]); // theta (deg)
>                 dRi = Double.parseDouble(linejArray[3]); // dRi (m)
>                 // dRi negative value means smaller than nominal radius
>                 // tempx = (r - dr) * cos(theta)
>                 // tempy = (r - dr) * sin(theta)
>                 // tempz = ZPOSi
>                 tempx = (.094629 - dRi) * Math.cos((3.14159 / 180) *
> theta);
>                 tempy = (.094629 - dRi) * Math.sin((3.14159 / 180) *
> theta);
>                 tempz = ZPOSi;
>                 points.SetPoint(indexNum, tempx, tempy, tempz);
>                 System.out.println(tempx + " " + tempy + " " + tempz);
>                 // Account for convention used in application
>                 // Change sign of dRi:
>                 dRi *= -1;
>                 colorsScalars.SetValue(indexNum, dRi);
>                 indexNum = indexNum + 1;
>                 System.out.println("indexNum = " + indexNum
>                         + "......... dRi  = " + dRi);
>                 linej = br.readLine();
>                 // (Account for blank line...)
>                 linej = br.readLine();
>             }
>         } catch (Exception ex) {
>             ex.printStackTrace();
>             System.out.println("Error reading datafile");
>         }
>         try {
>             br_count.close();
>         } catch (IOException e) {
>             System.out.println("Error closing br_count");
>             e.printStackTrace();
>         }
>
>         // populate the polyData
>         this.polyData.SetPoints(points);
>
>         // find min and max of colorsScalars array
>         double[] tmpScalarRange = new double[2];
>         tmpScalarRange = this.colorsScalars.GetRange();
>         Double scalarMax = tmpScalarRange[1];
>         Double scalarMin = tmpScalarRange[0];
>
>         // populate the scalars
>         this.polyData.GetPointData().SetScalars(this.colorsScalars);
>
>         // create sphere source
>         vtkSphereSource sphere = new vtkSphereSource();
>         sphere.Update();
>
>         // setup glyph3D
>         vtkGlyph3D glyph3D = new vtkGlyph3D();
>         glyph3D.SetColorModeToColorByScalar();
>         glyph3D.SetSourceConnection(sphere.GetOutputPort());
>         glyph3D.SetInputData(polyData);
>         glyph3D.Update();
>
>         // visualize
>         vtkDataSetMapper mapper = new vtkDataSetMapper();
>         mapper.SetInputConnection(glyph3D.GetOutputPort());
>         mapper.SetScalarRange(scalarMin, scalarMax);
>         mapper.Update();
>
>         vtkActor actor = new vtkActor();
>         actor.GetProperty().SetSpecularPower(1.0);
>         actor.GetProperty().SetDiffuse(1.0);
>         actor.GetProperty().SetOpacity(1.0);
>         actor.SetMapper(mapper);
>
>         vtkRenderer renderer = new vtkRenderer();
>         renderer = renderWindowPanel.GetRenderer();
>         renderer.AddActor(actor);
>         renderer.SetBackground(0.0, 0.0, 0.0);
>         renderer.ResetCameraClippingRange();
>
>         // Create a lookup table to share between the mapper and the
> scalarbar
>         vtkLookupTable hueLut = new vtkLookupTable();
>         hueLut.SetTableRange(scalarMin, scalarMax);
>         hueLut.SetHueRange(0.6667, 0.0000); // H from HSV red (top) to blue
>         hueLut.SetSaturationRange(1.0, 1.0);
>         hueLut.SetValueRange(1.0, 1.0); // V from HSV
>         hueLut.Build();
>         mapper.SetLookupTable(hueLut);
>         mapper.Update();
>
>         vtkScalarBarWidget sbWidget = new vtkScalarBarWidget();
>         sbWidget.RepositionableOn();
>         renderWindowInteractor =
> renderWindowPanel.getRenderWindowInteractor();
>         sbWidget.SetInteractor(renderWindowInteractor);
>         sbWidget.GetScalarBarActor().SetTitle("dR (m)");
>
> sbWidget.GetScalarBarActor().SetLookupTable(mapper.GetLookupTable());
>
>         vtkTextProperty SBtprop = new vtkTextProperty();
>         SBtprop.SetFontSize(1);
>         SBtprop.SetBold(1);
>         sbWidget.GetScalarBarActor().SetLabelTextProperty(SBtprop);
>         sbWidget.GetScalarBarActor().SetBarRatio(.7);
>         SBtprop.SetLineOffset(-10);
>
>         renderer.ResetCameraClippingRange();
>
>         vtkCubeAxesActor2D axes = new vtkCubeAxesActor2D();
>         axes.SetCamera(renderer.GetActiveCamera());
>         axes.SetBounds(actor.GetBounds());
>         axes.SetFlyModeToOuterEdges();
>         renderer.AddActor(axes);
>         renderer.ResetCamera();
>         sbWidget.EnabledOn();
>
>         System.out.println("B4 this.add");
>         this.add(renderWindowPanel, BorderLayout.CENTER);
>
>         renderer.ResetCamera();
>         renderWindowInteractor.Start();
>
>     }
> }
>
> On 8/31/2015 10:08 AM, Cory Quammen wrote:
>
> Hmm, that should work. Mind posting a code example showing the problem?
>
> Thanks,
> Cory
>
> On Mon, Aug 31, 2015 at 9:51 AM, James Labiak <
> jim at jslengineeringsoftware.com> wrote:
>
>> (vtk 6.0, Java wrapper) When using a vtkLookUpTable to map scalar values
>> to hue range, it appears that everything works correctly when the table
>> range is entirely positive, but when the table range is entirely negative,
>> the graphical output is blank. If this is intended behavior, then I would
>> make my own LUT except for mapper.SetLookupTable(hueLut). Is this likely my
>> error or a LUT issue?
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> <http://www.kitware.com/opensource/opensource.html>
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> <http://www.vtk.org/Wiki/VTK_FAQ>http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers
>>
>
>
>
> --
> Cory Quammen
> R&D Engineer
> Kitware, Inc.
>
>
> --
> James Labiak
> JSL Engineering and Software
> Mobile: 231-638-3725
> email: jim at jslengineeringsoftware.com
>
>


-- 
Cory Quammen
R&D Engineer
Kitware, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150904/c81e9b66/attachment.html>


More information about the vtkusers mailing list