mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.94k stars 574 forks source link

How to proj longitude and latitude to icosphere? #1850

Open dongZheX opened 1 year ago

dongZheX commented 1 year ago

Hi, I am trying to use trimesh to reproduce Graphcast , which is a weather forecast model ?

Now, I have build a mesh by icosphere(6), and I have to proj lon and lat to this mesh.

First, I connect the lat/lon grid node which is closest the icosphere vertices.

But I don't know how to implement that find the face in icosphere which contains lat/lon grid ? Is there any exist method to realize it? (nearby_face will return many faces, and I only need one).

Thanks

Kiord commented 1 year ago

You are maybe looking for trimesh.proximity.closest_point.

Else I think you can directly use the cartesian coordinates of your mesh :

import trimesh as tm
import numpy as np

mesh = trimesh.creation.icosphere(6) # unit sphere
x, y, z = mesh.vertices.T

theta = np.arccos(z)
phi = np.arctan(y/x) + np.pi / 2
phi[np.isnan(phi)] = 0 # clean nans
phi[x<0] += np.pi # phi all around the sphere
phi = phi / 2 # 0 to pi only

# convert to long lat
lat = np.rad2deg(-theta + np.pi / 2)
long = np.rad2deg(phi - np.pi / 2)
dongZheX commented 1 year ago

Thanks, I will try it.

---- Replied Message ---- | From | @.> | | Date | 02/23/2023 03:58 | | To | @.> | | Cc | @.>@.> | | Subject | Re: [mikedh/trimesh] How to proj longitude and latitude to icosphere? (Issue #1850) |

You are maybe looking for trimesh.proximity.closest_point.

Else I think you can directly use the cartesian coordinates of your mesh :

importtrimeshastmimportnumpyasnpmesh=trimesh.creation.icosphere(6) # unit spherex, y, z=mesh.vertices.Ttheta=np.arccos(z) phi=np.arctan(y/x) +np.pi/2phi[np.isnan(phi)] =0# clean nansphi[x<0] +=np.pi# phi all around the spherephi=phi/2# 0 to pi only# convert to long latlat=np.rad2deg(-theta+np.pi/2) long=np.rad2deg(phi-np.pi/2)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>