Syntactic Confectionery Delight PerlMonks

### Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?

by fraizerangus (Sexton)
 on Jun 26, 2012 at 18:39 UTC Need Help??
fraizerangus has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I need to rotate a 3D Cartesian point around a fixed axis 1deg at a time until it comes in contact with another point. I have the Axis Unit Vector, the x, y and z co-ordinates of a point on the line and the Cartesian co-ordinates of the point I want to rotate, my thoughts are that 'Polar Co-Ordinates' is the way forward with this? does anyone have any code I can borrow to do this? or are there any modules which exist on CPAN which are capable of doing this?

best wishes

Dan

• Comment on Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?

Replies are listed 'Best First'.
Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by zentara (Archbishop) on Jun 26, 2012 at 19:14 UTC
Hi, it sounds like PDL's PDL::Graphics::TriD module will display it. It also does a Polar coordinate system. You might want to ask on the PDL maillist. You will find some real experts there who can assist in setting up your piddles. The secret is in understanding the piddles. :-)

In the meantime, here are a couple of simple TriD examples. More examples, including how to do animation is at TriD_Tutorial

A polar coordinate system example:

```#!/usr/bin/perl
use warnings;
use PDL;
use PDL::Graphics::TriD;

# POLAR2D A 2-D polar coordinate system. [\$piddle] is interpreted as t
+he z coordi
#               nate over theta and r (theta = the first dimension of
+the piddle).

\$size = 25;

\$x = ( xvals zeroes \$size+ 1, \$size + 1 );# / \$size;

print "\$x\n";
print join ' ',dims \$x;
print "\n";

\$y = ( yvals zeroes \$size+ 1, \$size + 1 );# / \$size;
#\$z = 0.5 + 0.5 * ( sin( \$x * 6.3 ) * sin( \$y * 6.3 ) )**3;    # Bumps

\$z= \$x * 10;

\$r = \$x;
\$g = \$y;
\$b = \$z;

# The reason for the [] around \$x,\$y,\$z:
# 1. You can give all the coordinates and colors in one piddle.
\$c = ( zeroes 3, \$size + 1 ) / \$size;
\$coords = sin( ( 3 + 3 * xvals \$c) * yvals \$c);
\$colors = \$coords;

print "hit q to continue\n";

# 2. You can use defaults inside the brackets:
lattice3d [ \$z ], [ \$r ];  # Note: no \$x, \$y, and \$r is greyscale

print "hit q to continue\n";

# 3. You can plot in certain other systems as defaults
imag3d_ns [ POLAR2D, \$z ], [ \$r, \$g, \$b ];    # Draw the familiar
# bumpy surface in polar
# coordinates

A conventional 3d cartesian example using easy-to-see spheres

```#!/usr/bin/perl
use warnings;
use strict;
use PDL;
use PDL::Graphics::TriD;

keeptwiddling3d();

my \$piddle = pdl( [ [1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7
+,7,7],[8,8,8], [1,0,1] ] );

spheres3d \$piddle ;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by RichardK (Parson) on Jun 26, 2012 at 19:59 UTC

Have a look at Math::Trig as is contains cartesian_to_cylindrical,cartesian_to_spherical etc. so should already do what you need.

Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by salva (Abbot) on Jun 26, 2012 at 21:51 UTC
Math::Vector::Real provides the method rotate_3d that already does what you want:
```use Math::Vector::Real;

\$offset = V(\$x, \$y, \$z);
\$p_rotated_1deg = \$axis->rotate_3d(1*\$pi/180, \$p - \$offset) + \$offset;
I've used this module before and been very impressed with it :) but where would you input the line unit vector to state the point at which you'd like to rotate? thanks
The line is defined by two vectors:

The first vector defines the direction of the line; it corresponds to \$axis on the code from my previous post.

The second vector indicates the positions of some point of the line relative to the origin (it can be any point on the line). It is what I have called \$offset.

The rotate_3d performs the rotation around a line passing through the origin and with the given direction, so to use it to rotate a vector \$p around a line that does not pass through the origin, we have to perform a translation, rotate the vector and undo the translation.

In practice, that means, subtracting the vector \$offset to \$p, performing the rotation, and then adding \$offset to the resulting value.

Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by jandrew (Chaplain) on Jun 27, 2012 at 00:20 UTC

If you enjoy linear algebra you could even stay in the Cartesian coordinate system using Matrix Math and use one of the Matrix Modules to do the rotation. I have had success with the pure perl Math::MatrixReal

Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by BrowserUk (Pope) on Jun 28, 2012 at 12:53 UTC

Your responses seem to point you at various modules that relate to 3D rotations, but not to how to use them to solve you specific problem.

But perhaps that is because your problem description is not very clear. Eg,

the x, y and z co-ordinates of a point on the line

Which "line"? You haven't mentioned a line before. Do you mean the axis of rotation partially described by the "Axis Unit Vector"?

And why are the coordinates of this point "x, y and z co-ordinates", and not "a 3D Cartesian point" or "the Cartesian co-ordinates of the point" as with the other two points you mention?

I think I understand what you are trying to achieve, but it took many reading of your post to arrive at what is essentially little more than a guess. So I'll describe what I think and see if it fits.

You have

1. the 3D coordinates of point A.
2. the unit vector and 3d coords for a point describing an axis of rotation: R
3. the 3D coordinates of a point B, that is a rotation of A about R.

What you are seeking to find, are the 3D coordinates of point A', as it rotates around R from A to B; in 1 degree steps.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

hello

Apologies my description was a little lacking, this is a 3D system and I have an axis line (which is a fixed point) and I would like to rotate a collection of points, which I have the Cartesian co-ordinates of (all in roughly the same position) by 1 degree each time and record their new Cartesian co-ordinates, then re-iterate this over until one of the points comes into contact with another set of points, hope that makes sense, sorry about the confusion?

best wishes

Dan

I have an axis line (which is a fixed point)

Sorry to be pedantic, but a line is not a point and point is not a line.

Can you confirm my description above is correct?

Do you have a set of sample data: 1) coordinates for the starting point(A above); 2) the axis unit vector and associated point:(R above); 3) coordinates for the stopping point(B above)?

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by erix (Vicar) on Jul 02, 2012 at 13:45 UTC

Is your data PDB? It certainly looks like it (always useful to call things what they are). Perhaps Ivan Tubert-Brohman's Chemistry stuff on CPAN is useful: Chemistry::File::PDB.

I'm also wondering whether existing GIS software (postgis for postgresql comes to mind, or even plain vanilla postgresql geometric datatypes) might not already contain what you need, although admittedly it's probably a bit of a learning curve.

erix, with great respect for your habit of providing actual solutions, this falls short:

Is your data PDB? ... Chemistry::File::PDB.

At most that will allow the OP to read his data files.

But then, the format is so simple that the relevant parts can be read using single lines of Perl code:

```## Load line data
my @linePts = map[ (split)[ 5,6,7 ] ], split "\n", <DATA>; # pp \@line
+Pts;

my @rotPts  = map[ (split)[ 6,7,8 ] ], split "\n", <DATA>; # pp \@rotP
+ts;

my @stopPts = map[ (split)[ 7,8,9 ] ], split "\n", <DATA>; # pp \@stop
+Pts;
I'm also wondering whether existing GIS software (postgis for postgresql comes to mind, or even plain vanilla postgresql geometric datatypes) might not already contain what you need, although admittedly it's probably a bit of a learning curve.

To the best of my ability to discern (you weren't kidding about the learning curce), postgis only deals with 2D data which wouldn't help for this.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

At most that will allow the OP to read his data files.

Well, it was also meant for others, and as a hint to the OP. Whatever the precise question; it is useful to know if a widely used format is being discussed. Taking myself as example: to realise that this data is PDB (I had to look up the format at the PDB site) got *me* interested -- and perhaps I'll give it a try with postgres/postgis later on (I think there is actually quite a bit of 3d stuff available between them).

The advantage would mainly be indexing, I think, so if a database approach can be made to work at all, it would mostly be useful if one wanted to look at many proteins.

Re: Polar Co-Ordinates: Rotating a 3D cartesian point around a fixed axis?
by BrowserUk (Pope) on Jul 02, 2012 at 17:53 UTC

Grr. I posted the code I promised you are a reply to myself instead of you.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Create A New User
Node Status?
node history
Node Type: perlquestion [id://978485]
Approved by Eliya
Front-paged by Corion
help
Chatterbox?
 [marto]: Corion Schoenfeld, but my friend has been telling me about this new airport for about 7-8 years now :P [marto]: I doubt it even exists as a building site :P [ambrus]: Corion: does building airports "somewhat outside of" a city ever work? cities always grow around the airport quickly, because it's practical to be close to it, and after that, people start to complain that the airplanes land so close to their house and sh [ambrus]: ould keep down the noise

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2017-02-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Before electricity was invented, what was the Electric Eel called?

Results (346 votes). Check out past polls.