Skip to content

Commit

Permalink
add icosphere
Browse files Browse the repository at this point in the history
  • Loading branch information
christian34 committed Nov 26, 2024
1 parent 00afc37 commit c2ee663
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 103 deletions.
106 changes: 40 additions & 66 deletions example/notebooks/Sky luminance.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/alinea/astk/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ def jet_colors(values, minval=None, maxval=None):
if maxval is None:
maxval = max(values)
cmap = ColorMap()
return map(lambda x: cmap(x, minval, maxval, 250., 20.), values)
return list(map(lambda x: cmap(x, minval, maxval, 250., 20.), values))
60 changes: 26 additions & 34 deletions src/alinea/astk/icosphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import math
import numpy
import warnings
from alinea.astk.colormap import jet_colors

display_enable = True
try:
Expand All @@ -34,6 +35,18 @@
warnings.warn('PlantGL not installed: display is not enable!')
display_enable = False

def sky_turtle(turtle_mesh, sky_sources):
vertices, faces = turtle_mesh
colors = jet_colors((lum for _,_,lum in sky_sources))
scene = pgl.Scene()
for i, face in enumerate(faces):
vtx = [vertices[v] for v in face]
idx = range(len(face))
mat = pgl.Material(pgl.Color3(*colors[i]))
shape = pgl.Shape(pgl.FaceSet(pointList=vtx, indexList=[idx]), mat)
scene += shape
return scene


def display(vertices, faces, colors=None, view=True):
"""3D display of a polyhedron with PlantGL
Expand Down Expand Up @@ -90,7 +103,7 @@ def spherical(points):
x, y, z = zip(*proj)
theta = numpy.arccos(z)
phi = numpy.arctan2(y, x)
return zip(theta, phi)
return list(zip(theta, phi))


def rotation_matrix(axis, theta):
Expand Down Expand Up @@ -396,21 +409,20 @@ def refine(level=0):
return iter_triangle, iter_star


def turtle_dome(refine_level=3):
def turtle_mesh(min_faces=46):
"""Generate faces of a dual icosphere polyhedron mapping the Z+ hemisphere
Args:
refine_level (int): the level of refinement of the dual icosphere. By
default 46 polygons are returned (refine_level=3).
For information, here are the number of faces obtained for the first ten
refinement level: 0: 6, 1: 16, 2: 26, 3: 46, 4: 66, 5: 91, 6: 136,
7: 196, 8: 251, 9: 341, 10: 406
min_faces (int) : the minimal number of faces for the polyhedron
The number of faces obtained for the first ten polyhedrons are:
6, 16, 26, 46, 66, 91, 136, 196, 251, 341, 406
Returns:
a list of vertices and a list of faces
"""

sectors = [ 1, 6, 16, 26, 46, 66, 91, 136, 196, 251, 341, 406]
refines = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
refine_level = refines[numpy.searchsorted(sectors, min(max(sectors), min_faces))]
vertices, faces = dual(*icosphere(*refine(refine_level)))
# filter faces with centroids below horizontal plane
centers = [centroid([vertices[p] for p in face]) for face in faces]
Expand All @@ -431,37 +443,17 @@ def turtle_dome(refine_level=3):
return new_vertices, new_faces


def turtle_sectors(nb_sectors=46):
"""Generate faces of a dual icosphere polyhedron mapping the Z+ hemisphere
Args:
refine_level (int): the level of refinement of the dual icosphere. By
default 46 polygons are returned (refine_level=3).
For information, here are the number of faces obtained for the first ten
refinement level: 0: 6, 1: 16, 2: 26, 3: 46, 4: 66, 5: 91, 6: 136,
7: 196, 8: 251, 9: 341, 10: 406
Returns:
a list of vertices and a list of faces
def spherical_face_centers(turtle_mesh):
""" Spherical coordinates of turtle mesh faces centers
"""
sectors = [ 1, 6, 16, 26, 46, 66, 91, 136, 196, 251, 341, 406]
refines = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
s2r = dict(zip(sectors,refines))

if nb_sectors not in s2r:
print('Use a value of nb_sectors in the set ', sectors)

refine_level = s2r[nb_sectors]

vertices, faces = turtle_dome(refine_level)
vertices, faces = turtle_mesh

# Compute the centroid of each face
centers = [centroid([vertices[p] for p in face]) for face in faces]

elevations, azimuths = spherical(centers)
zeniths, azimuths = zip(*spherical(centers))

return elevations, azimuths
return list(zip(90-numpy.degrees(zeniths), numpy.degrees(azimuths)))


def sample_faces(vertices, faces, iter=2, spheric=False):
Expand Down
4 changes: 2 additions & 2 deletions src/alinea/astk/meteorology/sun_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def sun_position(dates=None, daydate=_day, latitude=_latitude,
"""

if dates is None:
dates = pandas.date_range(daydate, periods=24, freq='H')
dates = pandas.date_range(daydate, periods=24, freq='h')

if dates.tz is None:
times = dates.tz_localize(timezone)
Expand Down Expand Up @@ -93,7 +93,7 @@ def sun_extraradiation(dates=None, daydate=_day, solar_constant=1366.1,
dates is not already localised.
"""
if dates is None:
dates = pandas.date_range(daydate, periods=24, freq='H')
dates = pandas.date_range(daydate, periods=24, freq='h')

if dates.tz is None:
times = dates.tz_localize(timezone)
Expand Down

0 comments on commit c2ee663

Please sign in to comment.