Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Sperical and Cartesian Coordinates (http://www.velocityreviews.com/forums/t752883-sperical-and-cartesian-coordinates.html)

 ldries46 08-16-2011 09:07 AM

Sperical and Cartesian Coordinates

I need to convert Spherical and Cartesian coordinates and back. In MATLAB
there are two routines mentioned cart2sph and sph2cart, in references about
these routines C/C++ versions are mentioned .
Is there anyone who knows where to find these routines (in which library) or
where to download them. Also other routines that make the conversion are
welcome.

L. Dries

 Leclerc 08-16-2011 09:33 AM

Re: Sperical and Cartesian Coordinates

On 16.8.2011. 11:07, ldries46 wrote:
> I need to convert Spherical and Cartesian coordinates and back. In
> MATLAB there are two routines mentioned cart2sph and sph2cart, in
> references about these routines C/C++ versions are mentioned .
> Is there anyone who knows where to find these routines (in which
> library) or where to download them. Also other routines that make the
> conversion are welcome.
>
> L. Dries

You don't need libs for something like that.
at MATLAB prompt type (without quotes) 'edit cart2sph' and/or 'edit
sph2cart', and you will see equations governing transform.

 Jens Thoms Toerring 08-16-2011 10:51 AM

Re: Sperical and Cartesian Coordinates

ldries46 <bertus.dries@planet.nl> wrote:
> I need to convert Spherical and Cartesian coordinates and back. In MATLAB
> there are two routines mentioned cart2sph and sph2cart, in references about
> these routines C/C++ versions are mentioned .
> Is there anyone who knows where to find these routines (in which library) or
> where to download them. Also other routines that make the conversion are
> welcome.

You can look up the formulas e.g. here

http://en.wikipedia.org/wiki/Spheric...rdinate_system

and it's straightforward to implement:

/******************************//**
* \brief Conversion from cartesian to spherical coordinates (if the
* resulting radius is 0 also the azimutal and inclination
* angles get set to 0).
*
* @param r Reference to return the radius
* @param theta Reference to return the inclination angle (in radian)
* @param phi Reference to return the azimutal angle (in radian)
* @param x x coordinate
* @param y y coordinate
* @param z z coordinate
******************************/

void
cartesianToSpherical( double & r,
double & theta,
double & phi,
double x,
double y,
double z )
{
if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
{
theta = acos( z / r );
phi = atan2( y, x );
}
else
theta = phi = 0.0;
}

/******************************//**
* \brief Conversion from spherical to cartesian coordinates
*
* @param x Reference to return the x coordinate
* @param y Reference to return the y coordinate
* @param z Reference to return the z coordinate
* @param r Radius (must be non-negative)
* @param theta Inclination angle (in radian)
* @param phi Azimutal angle (in radian)
******************************/

void
sphericalToCartesian( double & x,
double & y,
double & z,
double r,
double theta,
double phi )
{
if ( r < 0.0 )
throw "Negative radius in sphericalToCartesian()";

x = r * sin( theta ) * cos( phi );
y = r * sin( theta ) * sin( phi );
z = r * cos( theta );
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

 skuratiov@gmail.com 04-18-2013 08:16 AM

Re: Sperical and Cartesian Coordinates

On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
> ldries46 <bertus.dries@planet.nl> wrote:
> > I need to convert Spherical and Cartesian coordinates and back. In MATLAB
> > there are two routines mentioned cart2sph and sph2cart, in references about
> > these routines C/C++ versions are mentioned .
> > Is there anyone who knows where to find these routines (in which library) or
> > where to download them. Also other routines that make the conversion are
> > welcome.

>
> You can look up the formulas e.g. here
>
> http://en.wikipedia.org/wiki/Spheric...rdinate_system
>
> and it's straightforward to implement:
>
> /******************************//**
> * \brief Conversion from cartesian to spherical coordinates (if the
> * resulting radius is 0 also the azimutal and inclination
> * angles get set to 0).
> *
> * @param r Reference to return the radius
> * @param theta Reference to return the inclination angle (in radian)
> * @param phi Reference to return the azimutal angle (in radian)
> * @param x x coordinate
> * @param y y coordinate
> * @param z z coordinate
> ******************************/
>
> void
> cartesianToSpherical( double & r,
> double & theta,
> double & phi,
> double x,
> double y,
> double z )
> {
> if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
> {
> theta = acos( z / r );
> phi = atan2( y, x );
> }
> else
> theta = phi = 0.0;
> }
>
>
> /******************************//**
> * \brief Conversion from spherical to cartesian coordinates
> *
> * @param x Reference to return the x coordinate
> * @param y Reference to return the y coordinate
> * @param z Reference to return the z coordinate
> * @param r Radius (must be non-negative)
> * @param theta Inclination angle (in radian)
> * @param phi Azimutal angle (in radian)
> ******************************/
>
> void
> sphericalToCartesian( double & x,
> double & y,
> double & z,
> double r,
> double theta,
> double phi )
> {
> if ( r < 0.0 )
> throw "Negative radius in sphericalToCartesian()";
>
> x = r * sin( theta ) * cos( phi );
> y = r * sin( theta ) * sin( phi );
> z = r * cos( theta );
> }
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ jt@toerring.de
> \__________________________ http://toerring.de

Will not work if x == 0.0

 Ike Naar 04-18-2013 08:42 AM

Re: Sperical and Cartesian Coordinates

On 2013-04-18, skuratiov@gmail.com <skuratiov@gmail.com> wrote:
> On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <bertus.dries@planet.nl> wrote:
>> > I need to convert Spherical and Cartesian coordinates and back. In MATLAB
>> > there are two routines mentioned cart2sph and sph2cart, in references about
>> > these routines C/C++ versions are mentioned .
>> > Is there anyone who knows where to find these routines (in which library) or
>> > where to download them. Also other routines that make the conversion are
>> > welcome.

>>
>> You can look up the formulas e.g. here
>>
>> http://en.wikipedia.org/wiki/Spheric...rdinate_system
>>
>> and it's straightforward to implement:
>>
>> /******************************//**
>> * \brief Conversion from cartesian to spherical coordinates (if the
>> * resulting radius is 0 also the azimutal and inclination
>> * angles get set to 0).
>> *
>> * @param r Reference to return the radius
>> * @param theta Reference to return the inclination angle (in radian)
>> * @param phi Reference to return the azimutal angle (in radian)
>> * @param x x coordinate
>> * @param y y coordinate
>> * @param z z coordinate
>> ******************************/
>>
>> void
>> cartesianToSpherical( double & r,
>> double & theta,
>> double & phi,
>> double x,
>> double y,
>> double z )
>> {
>> if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
>> {
>> theta = acos( z / r );
>> phi = atan2( y, x );
>> }
>> else
>> theta = phi = 0.0;
>> }
>>
>> /******************************//**
>> * \brief Conversion from spherical to cartesian coordinates
>> *
>> * @param x Reference to return the x coordinate
>> * @param y Reference to return the y coordinate
>> * @param z Reference to return the z coordinate
>> * @param r Radius (must be non-negative)
>> * @param theta Inclination angle (in radian)
>> * @param phi Azimutal angle (in radian)
>> ******************************/
>>
>> void
>> sphericalToCartesian( double & x,
>> double & y,
>> double & z,
>> double r,
>> double theta,
>> double phi )
>> {
>> if ( r < 0.0 )
>> throw "Negative radius in sphericalToCartesian()";
>>
>> x = r * sin( theta ) * cos( phi );
>> y = r * sin( theta ) * sin( phi );
>> z = r * cos( theta );
>> }

> Will not work if x == 0.0

Can you elaborate on that?
Both sqrt(x*x + y*y + z*z) and atan2(y, x) are well-defined for x==0 .

 Fred Zwarts \(KVI\) 04-18-2013 08:51 AM

Re: Sperical and Cartesian Coordinates

wrote in message
>
>On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <bertus.dries@planet.nl> wrote:
>> > I need to convert Spherical and Cartesian coordinates and back. In
>> > MATLAB
>> > there are two routines mentioned cart2sph and sph2cart, in references
>> > these routines C/C++ versions are mentioned .
>> > Is there anyone who knows where to find these routines (in which
>> > library) or
>> > where to download them. Also other routines that make the conversion
>> > are
>> > welcome.

>>
>> You can look up the formulas e.g. here
>>
>> http://en.wikipedia.org/wiki/Spheric...rdinate_system
>>
>> and it's straightforward to implement:
>>
>> /******************************//**
>> * \brief Conversion from cartesian to spherical coordinates (if the
>> * resulting radius is 0 also the azimutal and inclination
>> * angles get set to 0).
>> *
>> * @param r Reference to return the radius
>> * @param theta Reference to return the inclination angle (in radian)
>> * @param phi Reference to return the azimutal angle (in radian)
>> * @param x x coordinate
>> * @param y y coordinate
>> * @param z z coordinate
>> ******************************/
>>
>> void
>> cartesianToSpherical( double & r,
>> double & theta,
>> double & phi,
>> double x,
>> double y,
>> double z )
>> {
>> if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
>> {
>> theta = acos( z / r );
>> phi = atan2( y, x );
>> }
>> else
>> theta = phi = 0.0;
>> }
>>
>>
>> /******************************//**
>> * \brief Conversion from spherical to cartesian coordinates
>> *
>> * @param x Reference to return the x coordinate
>> * @param y Reference to return the y coordinate
>> * @param z Reference to return the z coordinate
>> * @param r Radius (must be non-negative)
>> * @param theta Inclination angle (in radian)
>> * @param phi Azimutal angle (in radian)
>> ******************************/
>>
>> void
>> sphericalToCartesian( double & x,
>> double & y,
>> double & z,
>> double r,
>> double theta,
>> double phi )
>> {
>> if ( r < 0.0 )
>> throw "Negative radius in sphericalToCartesian()";
>>
>> x = r * sin( theta ) * cos( phi );
>> y = r * sin( theta ) * sin( phi );
>> z = r * cos( theta );
>> }
>> Regards, Jens
>> --
>> \ Jens Thoms Toerring ___ jt@toerring.de
>> \__________________________ http://toerring.de

>
>Will not work if x == 0.0

A very delayed response ....
In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
divide by x.
I am not sure what happens if both x and y equal 0.

 Jens Thoms Toerring 04-18-2013 11:39 AM

Re: Sperical and Cartesian Coordinates

"Fred Zwarts \(KVI\)" <F.Zwarts@kvi.nl> wrote:
> In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
> divide by x.

Exactly, that'w why one uses atan2(y, x) instead of atan(y/x).

> I am not sure what happens if both x and y equal 0.

I guess that could be a problem - the C++ standard doesn't tell
much about the behaviour of atan2(), but since it seems to be
aligned to the C standard that might be the one relevant here.
And the C99 (as well as the C89) standard says that a domain
error may occur if both arguments are zero. So it would be
prudent to check for this special case (whichh I missed). The
value of phi is not well-defined mathematically in that case
- a reasonable choice would seem to be 0 (but any value would
do in principle), while theta is either 0 when z > 0 or pi for
z < 0.
Best regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

 Fred Zwarts \(KVI\) 04-18-2013 02:26 PM

Re: Sperical and Cartesian Coordinates

"Juha Nieminen" wrote in message news:kkokd8\$2s8h\$2@adenine.netfront.net...
>
>"Fred Zwarts \(KVI\)" <F.Zwarts@kvi.nl> wrote:
>> In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
>> divide by x.

>
>What do you mean "necessarily"? It doesn't, period.

Are you sure it never does? Have you checked the implementation code on all
platforms?

What I mean is that there might be case where a division by x is a possible
implementation. Take, e.g., the case that |y| is much smaller than |x|. Then
atan2(y,x) equals y/x within rounding errors.

Of course for |x| much smaller than |y| other methods will be used.

 Nobody 04-18-2013 10:08 PM

Re: Sperical and Cartesian Coordinates

On Thu, 18 Apr 2013 11:39:06 +0000, Jens Thoms Toerring wrote:

> "Fred Zwarts \(KVI\)" <F.Zwarts@kvi.nl> wrote:
>> In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
>> divide by x.

>
> Exactly, that'w why one uses atan2(y, x) instead of atan(y/x).

No, one uses atan2(y,x) instead of atan(y/x) because the former chooses
the correct quadrant when x is negative, whereas the latter will be off by
pi radians = 180 degrees.

>> I am not sure what happens if both x and y equal 0.

If x and y both equal zero, then phi is undefined. This isn't an
implementation issue (what is the longitude of the north pole)?

 All times are GMT. The time now is 10:15 AM.