Porting VectorDot example to Java

Hi, in my long journey to build a swept surface with VTK (How to build a swept surface ?), I begun by the begining: creating the enveloppe curve defined by a dot product. Thus I tried to port the VectorDot example (https://examples.vtk.org/site/Cxx/Math/VectorDot/) to Java.

Here is the code:

import vtk.vtkFloatArray;
import vtk.vtkNativeLibrary;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkVectorDot;

public class VtkVectorDot {
	static {
		if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
			for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
				if (!lib.IsLoaded()) {
					System.out.println(lib.GetLibraryName() + " not loaded");
				}
			}
		}
		vtkNativeLibrary.DisableOutputWindow(null);
	}

	public static void main(String[] args) {

		// Generate data
		vtkPoints points = new vtkPoints();
		points.InsertNextPoint(0, 0, 0);
		points.InsertNextPoint(1, 0, 0);
		points.InsertNextPoint(2, 0, 0);

		vtkPolyData polydata = new vtkPolyData();
		polydata.SetPoints(points);

		// Add normals
		vtkFloatArray normals = new vtkFloatArray();
		normals.SetNumberOfComponents(3);
		normals.SetName("Normals");

		normals.InsertNextTuple3(1, 0, 0);
		normals.InsertNextTuple3(1, 0, 0);
		normals.InsertNextTuple3(3, 0, 0);

		polydata.GetPointData().SetNormals(normals);

		// Add vectors.
		vtkFloatArray vectors = new vtkFloatArray();
		vectors.SetNumberOfComponents(3);
		vectors.SetName("Vectors");

		vectors.InsertNextTuple3(1, 1, 1);
		vectors.InsertNextTuple3(.707, .707, 0);
		vectors.InsertNextTuple3(0, 1, 0);

		polydata.GetPointData().SetVectors(vectors);

		// Compute the dot products between normals and vectors.
		vtkVectorDot vectorDot = new vtkVectorDot();
		vectorDot.SetInputData(polydata);
		vectorDot.Update();

		// Get the results.
		vtkFloatArray data = (vtkFloatArray) vectorDot.GetOutput().GetPointData().GetScalars();

		// Output the results.
		for (int i = 0; i < data.GetNumberOfTuples(); i++) {
			System.out.println("Value " + i + " : " + data.GetValue(i));
		}
	}
}

My problem is this code, which IMHO is a line for line translation of the cxx version, doesn’t work :-(.

The output is:

Value 0 : NaN
Value 1 : Infinity
Value 2 : Infinity

I checked it and checked it again, I can’t figure out where the problem is coming from.

Any help would be gratefully appreciated.

Thanks in advance.

jMaxR

PS: Once the code fixed, feel free to add it to VTK examples java pages.

I can confirm that the C++ code produces the same result:

Value 0 : -nan
Value 1 : inf
Value 2 : inf

So your java code is Ok.

Adding vectorDot->MapScalarsOff(); after line 54 in the C++ code
gives the correct ressult for the dot products, namely:

Value 0 : 1
Value 1 : 0.707
Value 2 : 0

There is definitely an issue with vectorDot->MapScalarsOff();, in that it doesn’t matter what range you specify.

E.g. replace the line vectorDot->MapScalarsOn(); with:

  vectorDot->MapScalarsOn();
  vectorDot->SetScalarRange(-1.0, 1.0);

Then try different ranges.

You may like to create an issue here issues with a link to this discussion. Doing this may ensure it is fixed for the upcoming release.

Thanks for your confirmation.

Issue filled: https://gitlab.kitware.com/vtk/vtk/-/issues/19706

To document a bit more: seems negative numbers behave a bit better. Result is numerical, but still erroneous.

normals.InsertNextTuple3(1, 0, 0);
normals.InsertNextTuple3(1, 0, 0);
normals.InsertNextTuple3(1, 0, 0);

vectors.InsertNextTuple3(-1, 1, 1); // negative value here
vectors.InsertNextTuple3(.707, .707, 0);
vectors.InsertNextTuple3(0, 1, 0);

vtkVectorDot vectorDot = new vtkVectorDot();
vectorDot.MapScalarsOn();
vectorDot.SetScalarRange(0, 1.0);
vectorDot.Update();

returns

Value 0 : 1.0
Value 1 : 1.0
Value 2 : 1.0

Thanks for doing that.