Project 2D points to 3D sphere, single object for every country

℡╲_俬逩灬. 提交于 2019-12-24 10:46:18

问题


I'm using Maya and I'm trying to create a 3D sphere of the earth with all countries separated by objects, so I can later export the file to Unity. With this code i create planes that represent the Earth.

I managed to create a plane of the Earth with 2D geojson points of all contries. And now I am trying to project those points to 3D sphere.

With this code I create a plane Earth

# EXAMPLE OF THE POINTS FORMAT

d = {"type":"FeatureCollection","features":[
{"type":"Feature","id":"ALB","properties":{"name":"Albania"},"geometry":{"type":"Polygon","coordinates":[[[20.590247,41.855404],[20.463175,41.515089],[20.605182,41.086226],[21.02004,40.842727],[20.99999,40.580004],[20.674997,40.435],[20.615,40.110007],[20.150016,39.624998],[19.98,39.694993],[19.960002,39.915006],[19.406082,40.250773],[19.319059,40.72723],[19.40355,41.409566],[19.540027,41.719986],[19.371769,41.877548],[19.304486,42.195745],[19.738051,42.688247],[19.801613,42.500093],[20.0707,42.58863],[20.283755,42.32026],[20.52295,42.21787],[20.590247,41.855404]]]}},
{"type":"Feature","id":"ARE","properties":{"name":"United Arab Emirates"},"geometry":{"type":"Polygon","coordinates":[[[51.579519,24.245497],[51.757441,24.294073],[51.794389,24.019826],[52.577081,24.177439],[53.404007,24.151317],[54.008001,24.121758],[54.693024,24.797892],[55.439025,25.439145],[56.070821,26.055464],[56.261042,25.714606],[56.396847,24.924732],[55.886233,24.920831],[55.804119,24.269604],[55.981214,24.130543],[55.528632,23.933604],[55.525841,23.524869],[55.234489,23.110993],[55.208341,22.70833],[55.006803,22.496948],[52.000733,23.001154],[51.617708,24.014219],[51.579519,24.245497]]]}}
...

for feat in d.get("features"):
    r = []
    coords = feat.get("geometry").get("coordinates")
    type = feat.get("geometry").get("type")
    for coord in coords:
        for c in coord:
            if type == "MultiPolygon":
                r = []
                for a in c:
                    r.append((a[0],a[1],0))
                poly = cmds.polyCreateFacet(p=r)
                poly = cmds.rename(feat.get("properties").get("name"))
            else:
                r.append((c[0],c[1], 0))

    if not type == "MultiPoligon":
    poly = cmds.polyCreateFacet(p=r)
        poly = cmds.rename(feat.get("properties").get("name"))

Searching about how to project 2d points onto 3d sphere found:

how map 2d grid points (x,y) onto sphere as 3d points (x,y,z) https://forums.tigsource.com/index.php?topic=17522.0

I also looked up for Mercator Projection https://wiki.openstreetmap.org/wiki/Mercator#Elliptical_.28true.29_Mercator_Projection https://en.wikipedia.org/wiki/Mercator_projection

And I tried...


def range_n(n, min, max):
    return ((n - min) / (max - min)) * (1 - 0) + 0

def to_3d_v3(x,y):
    # this points are the bounds of the points
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    phi = y * 2 * math.pi
    z = 2 * x -1
    rho = math.sqrt(1-z*z)
    rho = 20

    x = rho * x
    y = rho * math.log(math.tan((y + math.pi/2)/2))

    return (rho * math.cos(x) * math.cos(y), rho * math.cos(x) * math.sin(y), rho * math.sin(x))

def to_3d_v2(x,y):
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    z = -1 + 2 * x
    phi = 2 * math.pi * y
    theta = math.asin(z)
    return (math.cos(theta) * math.cos(phi), math.cos(theta) * math.sin(phi), z)

def to_3d(x,y):
    x = range_n(x, -180, 180)
    y = range_n(y, -85.609038, 42.688247)

    z = 2 * x -1
    phi = y * x -1
    rho = 1
    rho = math.sqrt(1-z*z)
    return (rho * math.cos(y), rho * math.sin(y), z)

# not a lof of changes
# just filtering all points with to_3d function
for feat in d.get("features"):
    r = []
    coords = feat.get("geometry").get("coordinates")
    type = feat.get("geometry").get("type")
    for coord in coords:
        for c in coord:
            if type == "MultiPolygon":
                r = []
                for a in c:
                    r.append(to_3d(a[0],a[1]))
                poly = cmds.polyCreateFacet(p=r)
                poly = cmds.rename(feat.get("properties").get("name"))
            else:
                r.append(to_3d(c[0],c[1]))
    #print(feat.get("id"), r)

    if not type == "MultiPoligon":
        poly = cmds.polyCreateFacet(p=r)
        poly = cmds.rename(feat.get("properties").get("name"))

But the result was some weird things like this

to_3d_v3:

to_3d_v2:

to_3d:

Please, any suggestion?

Thank you


回答1:


Since you're using x and y as longitude and latitude, you should feed them directly into the formula for projecting latitude/longitude to a sphere how map 2d grid points (x,y) onto sphere as 3d points (x,y,z).

Also, since x is horizontal position, that means x is longitude. Similarly, y is vertical position, so y is latitude. You had them backwards.

def to_3d_v4(x,y):
    #convert from degrees to radians
    longitude = math.radians(x)
    latitude = math.radians(y)

    # select a radius:
    radius = 10

    # project to 3d
    return (
        rho * math.cos(latitude) * math.cos(longitude), 
        rho * math.cos(latitude) * math.sin(longitude), 
        rho * math.sin(latitude)
    )


来源:https://stackoverflow.com/questions/56153411/project-2d-points-to-3d-sphere-single-object-for-every-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!