mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.74k stars 1.33k forks source link

Code for projecting 3D spherical coordinates into 2D plane coordinates #12987

Closed sdsafsdfsaf closed 2 days ago

sdsafsdfsaf commented 4 days ago
locs3d -= sphere[:3]
 # use spherical (theta, pol) as (r, theta) for polar->cartesian
cart_coords = _cart_to_sph(locs3d)
out = _pol_to_cart(cart_coords[:, 1:][:, ::-1])
# scale from radians to mm
out *= cart_coords[:, [0]] / (np.pi / 2.0)
out += sphere[:2]
def _pol_to_cart(pol):
    """Transform polar coordinates to cartesian."""
    out = np.empty((len(pol), 2))
    if pol.shape[1] == 2:  # phi, theta
        out[:, 0] = pol[:, 0] * np.cos(pol[:, 1])
        out[:, 1] = pol[:, 0] * np.sin(pol[:, 1])
    else:  # radial distance, theta, phi
        d = pol[:, 0] * np.sin(pol[:, 2])
        out[:, 0] = d * np.cos(pol[:, 1])
        out[:, 1] = d * np.sin(pol[:, 1])
    return out

I cannot understand how the calculation formula for projecting condition 1 from 3D spherical coordinates to a two-dimensional plane is derived. Can you provide some references?

welcome[bot] commented 4 days ago

Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

hoechenberger commented 3 days ago

Hi! I quickly copied your question into ChatGPT (model o1-preview)

See the response here:

https://chatgpt.com/share/67457e4f-3be0-800e-9737-293f51737d8d

(If the page doesn't load on the first try, click the link again)

sdsafsdfsaf commented 3 days ago

Hi! I quickly copied your question into ChatGPT (model o1-preview)

See the response here:

https://chatgpt.com/share/67457e4f-3be0-800e-9737-293f51737d8d

(If the page doesn't load on the first try, click the link again)

Thank you, I have also inquired about chatgpt before, but after careful comparison, I found that the result was incorrect. This projection does not seem to simply take the original 3D coordinates of x and y, but rather performs a certain projection transformation on spherical coordinates, making the newly obtained x and y different from the original.

hoechenberger commented 3 days ago

This projection does not seem to simply take the original 3D coordinates of x and y, but rather performs a certain projection transformation on spherical coordinates,

In the first step, the origin is shifted:

locs3d -= sphere[:3]

And after that, MNE converts the cartesian XYZ coordinates to polar corrdinates, and then projects those to the cartesian XY plane and scales the result. Maybe I'm misunderstanding your question 🤔

sdsafsdfsaf commented 2 days ago

This projection does not seem to simply take the original 3D coordinates of x and y, but rather performs a certain projection transformation on spherical coordinates,

In the first step, the origin is shifted:

locs3d -= sphere[:3]

And after that, MNE converts the cartesian XYZ coordinates to polar corrdinates, and then projects those to the cartesian XY plane and scales the result. Maybe I'm misunderstanding your question 🤔

Thank you very much, I made a mistake with the theta angle and mistook its corner for the theta angle. I understand the calculation process of condition 2, but I still don't understand why the calculation of condition 1 is like this? And why is the scaling of x and y done in this way?

out *= cart_coords[:, [0]] / (np.pi / 2.0)

It's a bit strange that we have obtained the three-dimensional coordinates xyz, so why don't we just take xy, but instead need to transform it like this?

hoechenberger commented 2 days ago

There are numerous ways to project points from a sphere onto a 2d plane. We simply chose one that we thought was most appropriate for the kind of data we want to visualize. Think of all the different projections of the globe onto a 2d map... none of them is "correct", they all have advantages and disadvantages.

I believe what we're using here is a form of azimuthal equidistant projection.

sappelhoff commented 2 days ago

Think of all the different projections of the globe onto a 2d map... none of them is "correct", they all have advantages and disadvantages.

agreed :)

I personally always found https://en.wikipedia.org/wiki/Stereographic_projection to be most easy to understand.

(this is also what is used in eeg_positions)

hoechenberger commented 2 days ago

@sdsafsdfsaf I hope we could help you a bit. If you have any further questions, please refer to our user forum, which is more suitable for this kind of discussion than the GitHub issue tracker. Thanks!