Vector operations in Python Calculator

Dear support,

I am struggling to perform some basic operations to get the moments around an axis. My objective is to find the normal force around an axis following this sketch:

Where AB is the axis that I’ve defined using a line filter and P is the point where a vector force is stored in my dataset.

The first step is to get the AP vector which works fine using this python calculator:

# Get reference to the inputs
surf = inputs[0]
axis = inputs[1]

# Get axis vector
A = axis.Points[ 0,:]
B = axis.Points[-1,:]
AB = B-A # VTKArray - Shape(3,)
AB = AB.reshape((3, 1))

# AP vector
AP = surf.Points-A # VTKCompositeDataArray - Shape(35248, 3)

return AP

And there you have the magnitude of the AP vector which increases as you get away from A

But then I need to perform dot and cross operations between the AB and AP vectors I the following code crashes:

# Get reference to the inputs
surf = inputs[0]
axis = inputs[1]

# Get axis vector
A = axis.Points[ 0,:]
B = axis.Points[-1,:]
AB = B-A # VTKArray - Shape(3,)
AB = AB.reshape((3, 1))

# AP vector
AP = surf.Points-A # VTKCompositeDataArray - Shape(35248, 3)

return cross(AB,AP)

Is it possible to get the cross and dot product of 2 vectors which belong to different filters in a simple way? I know how to do this using the regular calculators but I am trying to do this in a python calculator to simplify a bit my pipeline :wink:

Thank you in advance for your time!

I’ve found a way to bypass this issue (after MANY tries) but I am a bit clueless of why this is the only solution that I could find so far:

import numpy as np

# Get reference to the inputs
surf = inputs[0]
axis = inputs[1]

# Get axis vector
A = axis.Points[ 0,:]
B = axis.Points[-1,:]
AB = B-A # VTKArray - Shape(3,)

# AP vector
AP = surf.Points-A # VTKCompositeDataArray - Shape(N Pts, 3)

# Bypass: Using an existing VTKCompositeDataArray, 
#         set it to 0 and sum your desired vector
AB_arr = AP * 0 + [AB[0], AB[1], AB[2]]

return cross(AP,AB_arr)

Also I’ve found another issue when using the “norm“ command to normalize the array that I had to bypass in a similar way. Instead of doing this:

n = norm(cross(AB_arr,r))

I have to do this:

# Bypass: Using an existing VTKCompositeDataArray, 
#         set it to 0 and sum your desired vector
Ones4dot_arr = AP * 0 + [1, 1, 1]

# Moment vector direction (n).
n = cross(AB_arr,r) / dot(Ones4dot_arr,Ones4dot_arr)
n = norm(n)

Is there any better/elegant way to perform these operations?

It would be very convenient to know how to declare new VTKArrays or VTKCompositeDataArrays inside the python calculator in an efficient manner to understand the structure of the data but I couldn’t find answers online :smiley: