converting to sperical coordinates

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

converting to sperical coordinates

PhilipJ
Hi All,
I need a bit of help with some geometry, my maths in this area is a bit
rusty.
I want to be able to draw a "cylinder" between any two arbitrary points in
space, x1,y1,z1 and x2,y2,z2 but to draw the cylinder I need to rotate and
translate it i.e.
        translate([x1,y1,z1])
        {
                rotate(a=[a, b, c])
                {
                        cylinder(h=length, d=dia);
                }
        }
translate is easy, it's just x1,y1,z1 and cylinder length is easy, its is
square root of the sum of (xlength^2)+(ylength^2)+(zlength^2) but I just
can't seem to get the angles to rotate correct.

Has anyone done a module like this that they would be willing to share ?
Or is there another, easier way that doesn't require the sperical
coordinates ?

Many Thanks
PhilipJ





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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

Parkinbot
We had this theme some weeks ago. Ronaldo's mirror solution is brilliant.
http://forum.openscad.org/Rotation-question-tp24970p25000.html



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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

nophead
In reply to this post by PhilipJ
Also if you can accept spherical ends you can simply translate two spheres to
the end points and take the hull. That is better if you want to join them up
end to end.



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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ
Hi,

I did it with hull() as you suggest but it was rather slow and as there are
going to be a lot of these "rods" I wanted to try a different method by
adding a cylinder to fill in between the two spheres.

My aim is to draw up a geodisic dome by specifying the "node" points in
x,y,z coordinated, then create an stl file to 3D print it.

thanks
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

Ronaldo
PhilipJ wrote
> I did it with hull() as you suggest but it was rather slow and as there
> are
> going to be a lot of these "rods" I wanted to try a different method by
> adding a cylinder to fill in between the two spheres.

The hull() operation is rather fast. The union of a (large) number of rods
is the cause of longer render times. Adding cylinders to the spheres will be
worse.




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

runsun
In reply to this post by PhilipJ
You can also try a  *
<https://github.com/runsun/OpenSCAD_Tips/blob/master/snippets.md#line>
Line()*  function, which is also based on one of Ronaldo's codes.



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ
In reply to this post by Ronaldo
Hi again,

I tried the hull() method with just 4 rods and it took 6 seconds to render
when the file was double clicked compared to almost instanteneous (as fast
as openSCAD opens, the image is there on the screen).

I'm still using Version 2015.03-2, could this be a problem ?

code is:

$fn=100;

rod_dia=3;

module rod(x1, y1, z1, x2, y2, z2, d)
{
        hull()
        {
                translate([x1,y1,z1])
                        sphere(d/2);
                translate([x2,y2,z2]) {
                        sphere(d/2);
                }
        }
}

rod(0,0,0, 10,10, 30, rod_dia);
rod(20,0,0, 10,10, 30, rod_dia);
rod(0,20,0, 10,10, 30, rod_dia);
rod(20,20,0, 10,10, 30, rod_dia);

regards
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

MichaelPFrey
On 07.01.19 17:45, PhilipJ wrote:

> Hi again,
>
> I tried the hull() method with just 4 rods and it took 6 seconds to render
> when the file was double clicked compared to almost instanteneous (as fast
> as openSCAD opens, the image is there on the screen).
>
> I'm still using Version 2015.03-2, could this be a problem ?
>
> code is:
>
> $fn=100;

$fn=100 is pretty big.

Using a smaller number would cut down rendering and preview time.

Note that rendering (F6) is usually much slower then preview (F5)


with kind regards,

Michael Frey


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ
In reply to this post by runsun
Hi, thanks for the link.

I'm still trying to learn the language of openSCAD so could you explain:
in the module first line it says:
"module Line( pts, r=0.05, closed=false, color=undef, transp=1, fn=4 )"

what is "pts" ?
I assume it is the points for the line but how do I pass them into the
module through this one variable ?

As an example: how would I draw a line from 0,0,0 to 30,30,30 ?

Many thanks for your time
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ
In reply to this post by MichaelPFrey
> $fn=100 is pretty big.

> Using a smaller number would cut down rendering and preview time.

Thanks, I tried different values, 10 makes the rods look like hexagons, 30
was still a bit rough but rendered 4 rods in 11 seconds, still seems awfully
slow.

If th rods are made of cylinders and spheres with no hull function then the
Render takes fractions of a second, but I'm still trying to resolve the
cartesian to spherical coods bit so I can rotate the cylinder to the correct
angles to join up...

thanks for the suggestions
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

JordanBrown
In reply to this post by PhilipJ
On 1/7/2019 9:00 AM, PhilipJ wrote:
Hi, thanks for the link.

I'm still trying to learn the language of openSCAD so could you explain:
in the module first line it says:
"module Line( pts, r=0.05, closed=false, color=undef, transp=1, fn=4 )"

what is "pts" ?
I assume it is the points for the line but how do I pass them into the
module through this one variable ?

As an example: how would I draw a line from 0,0,0 to 30,30,30 ?

Many thanks for your time


pts is an array of 3D points to connect.

The tersest answer to your question is

Line([[0,0,0],[30,30,30]]);

Reformatting a bit for readability:

Line([
    [0,0,0],
    [30,30,30]
]);

That will yield the default radius of 0.05 units.  You probably want some other radius, so perhaps

Line([
    [0,0,0],
    [30,30,30]
], 5);

I'd recommend that you use positional arguments only in the most trivial of cases, and that in more complex cases you use named arguments:

Line(r=5, pts=[
    [0,0,0],
    [30,30,30]
]);

The default for that module is to generate four-sided "cylinders".  You can adjust that:

Line(r=5, fn=20, pts=[
    [0,0,0],
    [30,30,30]
]);

though I don't understand why there's an explicit fn parameter instead of just using $fn.  I would remove the ", $fn=fn" from the cylinder call at the end, and then any of these will work:

Line(r=5, $fn=20, pts=[
    [0,0,0],
    [30,30,30]
]);

$fn = 20;
Line(r=5, pts=[
    [0,0,0],
    [30,30,30]
]);

$fa = 10;
$fs = 0.1;
Line(r=5, pts=[
    [0,0,0],
    [30,30,30]
]);

I like that better because it integrates better with other OpenSCAD constructs.

Similarly, I don't understand why there is an explicit color and transparency options, rather than letting you control those using the color() transformation.

Note that it's a list; you can connect multiple points, e.g.:

Line(r=5, pts=[
    [0,0,0],
    [30,30,30],
    [-30,30,30]
]);



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ

> pts is an array of 3D points to connect.
>
> The tersest answer to your question is
>
> Line([[0,0,0],[30,30,30]]);
>
> Reformatting a bit for readability:
>
> Line([
>     [0,0,0],
>     [30,30,30]
> ]);

Thanks, that is a great and comprehensive explanation, I appreciate it :-)

regards
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

Ronaldo
In reply to this post by JordanBrown
PhilipJ PhilipJ
> $fn=100 is pretty big.

> Using a smaller number would cut down rendering and preview time.

Thanks, I tried different values, 10 makes the rods look like hexagons, 30
was still a bit rough but rendered 4 rods in 11 seconds, still seems awfully
slow.

If th rods are made of cylinders and spheres with no hull function then the
Render takes fractions of a second, but I'm still trying to resolve the
cartesian to spherical coods bit so I can rotate the cylinder to the correct
angles to join up...

The long render times are not due to the hull but the union of the rods. Better times result if the rods are just cylinders without the spherical cap because the vertex count is smaller. The following code runs relatively fast compared to the hull solution, however the rod joint is not as much smooth.

$fn=30;

rod_dia=3;

rod([ 0, 0,0], [10,10,30], rod_dia);
rod([20, 0,0], [10,10,30], rod_dia);
rod([ 0,20,0], [10,10,30], rod_dia);
rod([20,20,0], [10,10,30], rod_dia); 

module rod(p1,p2,d) 
  translate(p1)
    rotFromTo([0,0,1], p2-p1)
      cylinder(d=d, h=norm(p2-p1));

module rotFromTo(di,do) 
    if( norm(di-do)==0 || norm(di)==0 || norm(do)==0 ) 
        children();
    else 
        mirror(do/norm(do)+di/norm(di)) mirror(di) children();

The module rotFromTo does exactly what you were searching for: rotates the cylinder from the upward direction ( [0,0,1] ) to the desired direction.

For another approach, take a look on 


BTW, you may want to subscribe to the mailing list in order to have a broader audience to your messages. Some people, like me, usually don't read the forum messages in the forum site but rather from their mail box.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

PhilipJ
In reply to this post by runsun
Si I have found the solution to my conversion problem, it was in the help
text under rotate (if only I'd read it properly!!).

>> Thus if "a" is fixed to zero, and "b" and "c" are manipulated
>> appropriately, this is the spherical coordinate system.
>> So, to construct a cylinder from the origin to some other point (x,y,z):

>> x= 10; y = 10; z = 10; // point coordinates of end of cylinder
 
>> length = norm([x,y,z]);  // radial distance
>> b = acos(z/length); // inclination angle
>> c = atan2(y,x);     // azimuthal angle
>>
>> rotate([0, b, c])
>>     cylinder(h=length, r=0.5);
>> %cube([x,y,z]); // corner of cube should coincide with end of cylinder

All my attempts used atan2() for both angles, this uses acos for one of
them, I don't know what the difference is but this one works for all four
quadrant angles.

Thanks to everyone who took time to offer suggestions, now moving on to the
geodesic dome :-)
PhilipJ




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

runsun
In reply to this post by PhilipJ
JordanBrown answered it in details. One more "pt" to add: that *Line(pts...)*
function is able to take multiple points:

Line( pts= [ [2,3,4],[5,6,7],[1,2,3] ... ] );

So you can throw in hundreds or even thousands of pts and it will draw like
a cham. This efficiency is something the hull() can't achieve.



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

Ronaldo
Em ter, 8 de jan de 2019 às 22:05, runsun <[hidden email]> escreveu:
Line( pts= [ [2,3,4],[5,6,7],[1,2,3] ... ] );

So you can throw in hundreds or even thousands of pts and it will draw like
a cham. This efficiency is something the hull() can't achieve.

runsun,

I disagree. The hull() operation (at least in this case of convex sets) is very fast compared to the union of the rods. I have done some tests to show it.

1) define the rods and the Phillip's tent frame as:

$fn=30;
translate([ 0, 0,0]) rotFromTo([0,0,1],-([ 0, 0,0] - [10,10,30]))
  rod(rod_dia);
//translate([20, 0,0]) rotFromTo([0,0,1],-([20, 0,0] - [10,10,30]))
  rod(rod_dia);
//translate([ 0,20,0]) rotFromTo([0,0,1],-([ 0,20,0] - [10,10,30]))
  rod(rod_dia);
//translate([20,20,0]) rotFromTo([0,0,1],-([20,20,0] - [10,10,30]))
  rod(rod_dia); 

module rod(d) 
  hull() {
    sphere(d/2);
    translate([0,0,norm([ 0, 0,0] - [10,10,30])]) sphere(d/2);
  }

Now, the rods are fixed length and vertical. Render this code with a clean cache. The hulled rod will be in the cache. Render again dropping the comments in the main code. The last running time will be spent just in the union. In my computer, it is (surprisingly) greater then the render time of the original code.

2)  In the original code, instead of the hulled rods draw just a sphere at the top end. The render time will again show the union render time. In my computer, it is again (and very surprisingly) greater then the render time of the original code.

Conclusion: the render time of the hull() operation is negligible compared to the union operation. Even the union of 4 disjoint sphere spends more render time than the hull() of them.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

runsun
@Ronaldo,  Thx. I did mention "hundreds or thousands of points". Have you
tested that many points?



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

runsun
Have to correct myself on this --- in the past I did preview, never did
render. With some preliminary tests now, rendering on "hundreds/thousands of
pts" with either Line() or rod() seems impossible (takes too long), so it's
hard to state which is faster in my tests.



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: converting to sperical coordinates

Ronaldo


Em qui, 10 de jan de 2019 às 00:15, runsun <[hidden email]> escreveu:
Have to correct myself on this --- in the past I did preview, never did
render. With some preliminary tests now, rendering on "hundreds/thousands of
pts" with either Line() or rod() seems impossible (takes too long), so it's
hard to state which is faster in my tests.

I use my version of Line only in preview mode for debugging geometry. To avoid any accidental render of lines, I check $preview inside of Line and inhibit the drawing if false.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org