discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

How to find the current x, y, z location

J
jamcultur
Sat, Mar 2, 2019 2:29 AM

I have code with recursive functions that use translate() to change the
current location. The code works fine, but now I have to add code that needs
to know the current x, y, z location that resulted from many nested
translate() functions. How can I find the current x, y, z location?
Thank you!

--
Sent from: http://forum.openscad.org/

I have code with recursive functions that use translate() to change the current location. The code works fine, but now I have to add code that needs to know the current x, y, z location that resulted from many nested translate() functions. How can I find the current x, y, z location? Thank you! -- Sent from: http://forum.openscad.org/
A
adrianv
Sat, Mar 2, 2019 5:07 AM

jamcultur wrote

I have code with recursive functions that use translate() to change the
current location. The code works fine, but now I have to add code that
needs
to know the current x, y, z location that resulted from many nested
translate() functions. How can I find the current x, y, z location?

Your question is too vague.  I think you need to post a code example and
explain what you are trying to do.  What is the "current location"?  The
(x,y,z) location of what?  And what are you hoping to do with it once you
find it?

--
Sent from: http://forum.openscad.org/

jamcultur wrote > I have code with recursive functions that use translate() to change the > current location. The code works fine, but now I have to add code that > needs > to know the current x, y, z location that resulted from many nested > translate() functions. How can I find the current x, y, z location? Your question is too vague. I think you need to post a code example and explain what you are trying to do. What is the "current location"? The (x,y,z) location of what? And what are you hoping to do with it once you find it? -- Sent from: http://forum.openscad.org/
J
jamcultur
Sat, Mar 2, 2019 5:32 AM

To me, the "current location" is location that is the reference point of any
object that you draw. For example, if you wrote sphere(d=10);, the center of
the sphere would be at the current location. That location could have any x,
y, and z coordinates, depending on the previous translates and rotates. I
need to know those coordinates in order to determine whether to draw the
next object.

I should also mention that there are also many rotate() functions in the
recursive functions I mentioned previously.

--
Sent from: http://forum.openscad.org/

To me, the "current location" is location that is the reference point of any object that you draw. For example, if you wrote sphere(d=10);, the center of the sphere would be at the current location. That location could have any x, y, and z coordinates, depending on the previous translates and rotates. I need to know those coordinates in order to determine whether to draw the next object. I should also mention that there are also many rotate() functions in the recursive functions I mentioned previously. -- Sent from: http://forum.openscad.org/
M
MichaelAtOz
Sat, Mar 2, 2019 5:53 AM

Short answer is you can't get it from OpenSCAD.
You do all the translates etc, so you can calculate where 'here' is.

There are libraries which can help like  local
https://github.com/jreinhardt/local-scad  , there are others, I just can't
find them ATM.


Admin - email* me if you need anything, or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

Sent from: http://forum.openscad.org/

Short answer is you can't get it from OpenSCAD. You do all the translates etc, so you can calculate where 'here' is. There are libraries which can help like local <https://github.com/jreinhardt/local-scad> , there are others, I just can't find them ATM. ----- Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Mar 2, 2019 8:17 AM

You mention rotate() and translate() function but I assume you mean modules.

You could replace the recursive modules that use translate and rotate with
recursive functions that generate a transformation matrix instead by
multiplying translation and rotation matrices. Then you could apply that to
position your object using multmatrix() and you could also multiply it by
[0, 0, 0, 1] to get the numeric position as a vector.

Basically you always tell OpenSCAD where something is, you can never ask it.

On Sat, 2 Mar 2019 at 05:59, MichaelAtOz oz.at.michael@gmail.com wrote:

Short answer is you can't get it from OpenSCAD.
You do all the translates etc, so you can calculate where 'here' is.

There are libraries which can help like  local
https://github.com/jreinhardt/local-scad  , there are others, I just
can't
find them ATM.


Admin - email* me if you need anything, or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the
Public Domain; to the extent possible under law, I have waived all
copyright and related or neighbouring rights to this work. Obviously
inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it!
http://www.ourfairdeal.org/  time is running out!

Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

You mention rotate() and translate() function but I assume you mean modules. You could replace the recursive modules that use translate and rotate with recursive functions that generate a transformation matrix instead by multiplying translation and rotation matrices. Then you could apply that to position your object using multmatrix() and you could also multiply it by [0, 0, 0, 1] to get the numeric position as a vector. Basically you always tell OpenSCAD where something is, you can never ask it. On Sat, 2 Mar 2019 at 05:59, MichaelAtOz <oz.at.michael@gmail.com> wrote: > Short answer is you can't get it from OpenSCAD. > You do all the translates etc, so you can calculate where 'here' is. > > There are libraries which can help like local > <https://github.com/jreinhardt/local-scad> , there are others, I just > can't > find them ATM. > > > > ----- > Admin - email* me if you need anything, or if I've done something stupid... > > * click on my MichaelAtOz label, there is a link to email me. > > Unless specifically shown otherwise above, my contribution is in the > Public Domain; to the extent possible under law, I have waived all > copyright and related or neighbouring rights to this work. Obviously > inclusion of works of previous authors is not included in the above. > > The TPP is no simple “trade agreement.” Fight it! > http://www.ourfairdeal.org/ time is running out! > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
B
bradipao
Sat, Mar 2, 2019 10:27 AM

If I correctly understand your needs: you could add absolute position
(vector) as a parameter of the recursive function, so every time you call
again the function, you update the vector you pass accordingly to the
applied translation.

I used a similar approach to apply color to a 3D fractal, depending on the
cumulative displacement from [0,0,0].

jamcultur wrote

I have code with recursive functions that use translate() to change the
current location. The code works fine, but now I have to add code that
needs
to know the current x, y, z location that resulted from many nested
translate() functions. How can I find the current x, y, z location?
Thank you!

If I correctly understand your needs: you could add absolute position (vector) as a parameter of the recursive function, so every time you call again the function, you update the vector you pass accordingly to the applied translation. I used a similar approach to apply color to a 3D fractal, depending on the cumulative displacement from [0,0,0]. jamcultur wrote > I have code with recursive functions that use translate() to change the > current location. The code works fine, but now I have to add code that > needs > to know the current x, y, z location that resulted from many nested > translate() functions. How can I find the current x, y, z location? > Thank you! -- Sent from: http://forum.openscad.org/
J
jamcultur
Sat, Mar 2, 2019 3:49 PM

You understand correctly, and I was thinking of doing exactly what you
suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD
obviously knows the current [x, y, z] and it seems like it should be simple
for it to make that information available to the program.

--
Sent from: http://forum.openscad.org/

You understand correctly, and I was thinking of doing exactly what you suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD obviously knows the current [x, y, z] and it seems like it should be simple for it to make that information available to the program. -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Mar 2, 2019 5:37 PM

The generated geometry depends on the variables. Making the variables
dependent on the geometry could create a circular dependency unless it is
restricted by scope rules and would require more passes.

On Sat, 2 Mar 2019 at 15:54, jamcultur nyponen@gmail.com wrote:

You understand correctly, and I was thinking of doing exactly what you
suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD
obviously knows the current [x, y, z] and it seems like it should be simple
for it to make that information available to the program.

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

The generated geometry depends on the variables. Making the variables dependent on the geometry could create a circular dependency unless it is restricted by scope rules and would require more passes. On Sat, 2 Mar 2019 at 15:54, jamcultur <nyponen@gmail.com> wrote: > You understand correctly, and I was thinking of doing exactly what you > suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD > obviously knows the current [x, y, z] and it seems like it should be simple > for it to make that information available to the program. > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
P
Parkinbot
Sun, Mar 3, 2019 2:57 AM

don't know whether it will help you, but the way nophead described, is not
too difficult to go, if you have translate(), rotate(), and scale() as
functions.

Say your module contains code like this

rotate([100,0,100])
translate([0,30,100])
rotate([33,0,100])
translate([0,0,100])
cube(100);

if you want to know where cube is mapped, or where any other point q will be
mapped, you just use the same transformation sequence in function syntax.

q=[0,0,0];
q1 =
rotate([100,0,100],
translate([0,30,100],
rotate([33,0,100],
translate([0,0,100], q))));
echo(q1);

Here is my implementation of the three functions. The code looks more
difficult than it is, because the functions are part of a richer interface,
accept parameter calls without [] and work recursive. They operate over
points, lists of points, lists of lists of points and so on. I use them
mainly in connection with sweep(), but they will also serve your needs.

function translate(x=0, y=0, z=0, v) =
let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z])
len(v[0])?[for (i=v) translate(x,i)]:v+x;

function rotate(x=0, y=0, z=0, v) =  // 2D vectors allowed
let(v = (len(x)==3)?y:v, x=(len(x)==3)?x:[x, y, z])
len(v[0])? [for(i=v) rotate(x,i)]:Rz(x[2], Ry(x[1], Rx(x[0], v)));
function Rx(x, A) = len(A[0][0])?[for(i=A) Rx(x, i)]:
A*[[1, 0, 0], [0, cos(x), sin(x)], [0, -sin(x), cos(x)]];
function Ry(y, A) = len(A[0][0])?[for(i=A) Ry(y, i)]:
A*[[cos(y), 0, sin(y)], [0, 1, 0], [-sin(y), 0, cos(y)]];
function Rz(z, A) = len(A[0][0])?
[for(i=A) Rz(z, i)]:
len(A[0])==2?
A*[[cos(z), sin(z)], [-sin(z), cos(z)]]:
A*[[cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]];

function scale(x=1, y=1, z=1, v) =
let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z])
len(v[0])?[for (i=v) S_(x,i)]:[v[0]*x[0], v[1]*x[1], v[2]*x[2]];

--
Sent from: http://forum.openscad.org/

don't know whether it will help you, but the way nophead described, is not too difficult to go, if you have translate(), rotate(), and scale() as functions. Say your module contains code like this rotate([100,0,100]) translate([0,30,100]) rotate([33,0,100]) translate([0,0,100]) cube(100); if you want to know where cube is mapped, or where any other point q will be mapped, you just use the same transformation sequence in function syntax. q=[0,0,0]; q1 = rotate([100,0,100], translate([0,30,100], rotate([33,0,100], translate([0,0,100], q)))); echo(q1); Here is my implementation of the three functions. The code looks more difficult than it is, because the functions are part of a richer interface, accept parameter calls without [] and work recursive. They operate over points, lists of points, lists of lists of points and so on. I use them mainly in connection with sweep(), but they will also serve your needs. function translate(x=0, y=0, z=0, v) = let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z]) len(v[0])?[for (i=v) translate(x,i)]:v+x; function rotate(x=0, y=0, z=0, v) = // 2D vectors allowed let(v = (len(x)==3)?y:v, x=(len(x)==3)?x:[x, y, z]) len(v[0])? [for(i=v) rotate(x,i)]:Rz(x[2], Ry(x[1], Rx(x[0], v))); function Rx(x, A) = len(A[0][0])?[for(i=A) Rx(x, i)]: A*[[1, 0, 0], [0, cos(x), sin(x)], [0, -sin(x), cos(x)]]; function Ry(y, A) = len(A[0][0])?[for(i=A) Ry(y, i)]: A*[[cos(y), 0, sin(y)], [0, 1, 0], [-sin(y), 0, cos(y)]]; function Rz(z, A) = len(A[0][0])? [for(i=A) Rz(z, i)]: len(A[0])==2? A*[[cos(z), sin(z)], [-sin(z), cos(z)]]: A*[[cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]]; function scale(x=1, y=1, z=1, v) = let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z]) len(v[0])?[for (i=v) S_(x,i)]:[v[0]*x[0], v[1]*x[1], v[2]*x[2]]; -- Sent from: http://forum.openscad.org/
JB
Jordan Brown
Sun, Mar 3, 2019 8:17 PM

On 3/2/2019 12:17 AM, nop head wrote:

Basically you always tell OpenSCAD where something is, you can never
ask it.

(I know that you know this.)

And even then, you can only tell it where something is relative to its
own transformations... you can't specify an absolute location.

That is, you can't say

sequence( ) of( ) transformations() {
    translate_back_to_origin( ) {
        object( );
    }
}

The only way to do that would be to know the sequence of transformations
and unwind them.

On 3/2/2019 12:17 AM, nop head wrote: > Basically you always tell OpenSCAD where something is, you can never > ask it. (I know that you know this.) And even then, you can only tell it where something is relative to its own transformations... you can't specify an absolute location. That is, you can't say sequence( ) of( ) transformations() {     translate_back_to_origin( ) {         object( );     } } The only way to do that would be to know the sequence of transformations and unwind them.