r/OpenPythonSCAD • u/Alacritous13 • Jun 01 '25
How do I do vector math cleanly?
My OpenSCAD code tends to have a lot of vector math in it. While I was setting up my first PythonSCAD project I came to the realization that Python can't do vector math the same way as OpenSCAD, at least not intuitively. Looking around, it looks like numpy is my best bet for doing vector math, but every vector needs to have tolist() on the end of it or it can't be interpreted.
Here's a simplified example of what my OpenSCAD code might look like:
vec_n=vec/norm(vec);
scalar=12;
translate(scalar*vec_n)
cube(5,center=true);And here's how I got it to work in PythonScad:
from openscad import *
import numpy as np
vec=np.array([5,4,3]);
vec_n=vec/np.linalg.norm(vec);
scalar=12;
c=cube(5,center=True)
c=c.translate((scalar*vec_n).tolist())
c.show();Is there anyway to make it work without all these references to numpy? Or at the very least, without having to call tolist() every time I translate something?
4
u/rebuyer10110 Jun 01 '25 edited Jun 01 '25
My take:
Bonus notes:
If you dislike the extra steps in translating between numpy and python list repeatedly, some of the functions can DIY reasonably, since the vectors will only be 3-dimenions (not arbitrary dimensions) and matrices are always 4x4 tranformation matrices.
Magnitude is your standard pythagorean
magnitude = math.sqrt(x**2 + y**2 + z**2), and scalar can be applied with list comprehension such as2
u/gadget3D Jun 01 '25
Its a good topic. Its Python language which cannot do that.we could ceate a Library function to Scale a vector to a given length or we could create an internal function to so the Same. Dot and Cross Product are Missing, top. Whats your Favorite Resolution?
2
u/gadget3D Jun 05 '25
Upcoming version will have some improvements:
* there will be norm(), cross() and dot() function available
* translate() and scale() can transform 3D vectors AND handles in addtion
you could implement your intent by
scale(yourvec, newlength/norm(yourvec))
1
u/WillAdams Jun 02 '25 edited Jun 02 '25
I would like to rephrase this discussion a bit, and maybe this will help inform whether or no some more basic or fundamental solution is warranted.
First question --- does OpenSCAD have a good solution/mechanism/interface for this? Given that BOSL2 has math.scad (https://github.com/BelfrySCAD/BOSL2/wiki/math.scad) and this question exists: https://stackoverflow.com/questions/77737306/openscad-syntax-modify-value-in-vector-list-array I am going to go with "no".
If not, why not?
What programming languages do handle vector math? Working from: https://stackoverflow.com/questions/508374/what-are-vectors-and-how-are-they-used-in-programming it seems that it's not a native part of any programming language?
Should there be some sort of low-level, in-built support for this as a fundamental datatype?
Given that I bailed on doing 3D motion for turtle graphics: https://forum.makerforums.info/t/turtle-programming-in-openpythonscad/93340 and if my understanding is correct that this sort of datatype would directly support that sort of work, my vote would be in the affirmative.
That folks like me don't understand/use such mathematics might be demonstrated by the fact that a search for the term on Hackernews turns up many submissions, but little discussion:
https://hn.algolia.com/?q=vector+math