# Converting a 2D Sphere Coordinate Into a 3D Normal.

This topic is 3837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

You know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.

##### Share on other sites
Quote:
 Original post by EmptyVoidYou know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.
A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:

/* Convert spherical coordinates to Cartesian coordinates in R3 */template < typename E, class A > voidspherical_to_cartesian(E radius, E theta, E phi, size_t axis,    SphericalType type, vector<E,A>& v){    typedef vector<E,A> vector_type;    typedef typename vector_type::value_type value_type;    /* Checking */    detail::CheckVec3(v);    detail::CheckIndex3(axis);        if (type == latitude) {        phi = constants<value_type>::pi_over_2() - phi;    }    value_type sin_phi = std::sin(phi);    value_type cos_phi = std::cos(phi);    value_type sin_phi_r = sin_phi * radius;    // Note: cyclic_permutation() computes i, j, and k as follows:    // i = axis    // j = (i + 1) % 3    // k = (j + 1) % 3    size_t i, j, k;    cyclic_permutation(axis, i, j, k);    v[i] = cos_phi * radius;    v[j] = sin_phi_r * std::cos(theta);    v[k] = sin_phi_r * std::sin(theta);}

The 'axis' argument indicates which cardinal axis (0 = x, 1 = y, 2 = z) should be considered 'up', and the 'type' argument determines whether 'phi' should be interpreted as latitude or colatitude.

##### Share on other sites
I have little clue what you're talking about in most of your post. However, for a sphere, the normal at a certain point is just the vector from the origin to that point normalized.

edit: somehow didn't notice above post.

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by EmptyVoidYou know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.
A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:

*** Source Snippet Removed ***
The 'axis' argument indicates which cardinal axis (0 = x, 1 = y, 2 = z) should be considered 'up', and the 'type' argument determines whether 'phi' should be interpreted as latitude or colatitude.

Wow just looked and man I've been getting very helpful posts lately I didn't look until now because I thought it would be "just google it" or something but this is really helpful thanks. and yeah I don't make much since to my self most of the time sorry I just have bad communication skills.

##### Share on other sites
OK... I don't really understand. This is as much as I can guess

float* spherical_to_normal(float x, float y){float v[3];v[0] = sin(x);v[1] = y;v[2] = cos(x);return v;}float* normal_to_spherical(float x, float y, float z){float v[2];v[0] = //don't know what goes herev[1] = //don't know what goes herereturn v;}

[EDIT]

[Edited by - EmptyVoid on July 16, 2007 8:21:47 PM]

##### Share on other sites
Quote:
 Original post by EmptyVoid*** Source Snippet Removed ***
What language is that?

Anyway, I posted code earlier for converting spherical coordinates (e.g. latitude and longitude) to Cartesian coordinates (e.g. a '3D normal'). Was that not what you were looking for?

##### Share on other sites
It's C++ what language are you using? and yes that was exactly what I was looking for also I realized I need the opposite a 3D coordinate to a 2D one sorry my bad.

[Edited by - EmptyVoid on July 16, 2007 6:06:38 PM]

##### Share on other sites
Returning the address of a temporary doesn't even work. That's really bad c++.

##### Share on other sites
Quote:
 Original post by EmptyVoidIt's C++ what language are you using? and yes that was exactly what I was looking for also I realized I need the opposite a 3D coordinate to a 2D one sorry my bad.
Here's the function for converting in the opposite direction:

/* Convert Cartesian coordinates to spherical coordinates in R3 */template < class VecT, typename Real > voidcartesian_to_spherical(const VecT& v, Real& radius, Real& theta, Real& phi,    size_t axis, SphericalType type,    Real tolerance = epsilon<Real>::placeholder()){    typedef Real value_type;    /* Checking */    detail::CheckVec3(v);    detail::CheckIndex3(axis);    size_t i, j, k;    cyclic_permutation(axis, i, j, k);    value_type len = length(v[j],v[k]);    theta = len < tolerance ? value_type(0) : std::atan2(v[k],v[j]);    radius = length(v[i], len);    if (radius < tolerance) {        phi = value_type(0);    } else {        phi = std::atan2(len,v[i]);        //phi = type.convert(phi);        if (type == latitude) {            phi = constants<value_type>::pi_over_2() - phi;        }    }}

Also, as alluded to earlier, the code you posted appears to contain several syntax and programming errors.

##### Share on other sites
Is that better? also I don't know what most of those functions do can you make that with the C++ math library "math.h" as I said not very good with C++ more of a VB person. if not how do I use the code above is it a function? or is it some from of cool data type converter that I had no idea C++ could do? also this is the error I get when I run the code:

------ Build started: Project: nam, Configuration: Debug Win32 ------
Compiling...
nam.cpp
c:\documents and settings\steven batchelor\my documents\visual studio 2005\projects\nam\nam\nam.cpp(16) : error C2061: syntax error : identifier 'SphericalType'
Build log was saved at "file://c:\Documents and Settings\Steven Batchelor\My Documents\Visual Studio 2005\Projects\nam\nam\Debug\BuildLog.htm"
nam - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

[Edited by - EmptyVoid on July 16, 2007 8:29:56 PM]

##### Share on other sites
Quote:
 Original post by EmptyVoidIs that better? also I don't know what most of those functions do can you make that with the C++ math library "math.h" as I said not very good with C++ more of a VB person. if not how do I use the code above is it a function? or is it some from of cool data type converter that I had no idea C++ could do? also this is the error I get when I run the code:------ Build started: Project: nam, Configuration: Debug Win32 ------Compiling...nam.cppc:\documents and settings\steven batchelor\my documents\visual studio 2005\projects\nam\nam\nam.cpp(16) : error C2061: syntax error : identifier 'SphericalType'Build log was saved at "file://c:\Documents and Settings\Steven Batchelor\My Documents\Visual Studio 2005\Projects\nam\nam\Debug\BuildLog.htm"nam - 1 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you're new to C++ (and/or programming in general), you might have some trouble trying to adapt those functions. Yes, the code is standard C++, but there are some external dependencies, so you won't be able to compile the code as is. What you'll have to do instead, most likely, is examine the functions to see how the conversions are being performed, and then re-write your own code using the same steps.

##### Share on other sites
OK I will try to figure it out thank you.

##### Share on other sites
Converting from (latitude, longitude) to (x,y,z) is not hard:

x = cos(latitude) * sin(longitude)
y = sin(latitude)
z = cos(latitude) * cos(longitude)

That's your normal (and also the point itself, if the sphere is centered at (0,0,0) and has radius 1).