Sander Voss
Sander Voss

Reputation: 33

How to compute shadow casting of simple polygon geometry objects in 3D using python

I am building a python library to compute solar power generation (of satellites). In order to account for shadows cast by parts of the satellite onto its solar panels, I need to compute the shadows.

The satellites will be made up of very simple geometries, generally a cube (i.e. 6 polygons) with some panels (a rectangular polygon) sticking out from some side of it. Shadows will be cast by the sun which can be viewed as a point light source at locations (x,y,z).

I wanted to avoid writing my own geometry library, and figured this must surely be a solved problem. I have been looking into a myriad of existing libraries, e.g. PyVista, PyOpenGL, Panda3D, Pygame, SymPy). However, I have found the following limitations

  1. They are built for visualisation, not analysis. In other words, they would allow me to render the shadows on screen but provide me with no way of analysing the actual shadow cast on a particular polygon.
  2. They do not deal with polygons in 3D.

If there is no library that can provide the shadow casting natively, then if there is a good 3D library then I can do the following:

  1. Define all faces of the satellite including panels as polygons.
  2. Create a 'light' at location (x,y,z)
  3. For each face:
    1. Create a plane that coincides with the face

    2. Cast a ray from the light location through each point of each other face

    3. Calculate the intersection point of the ray and the plane

    4. Connect the intersection points to create a shadow polygon on the plane

    5. Calculate the overlap of the shadow polygon and the face that is being analysed

I have tried working with PyVista, trimesh, pyrender, but could not find a fitting solution.

Upvotes: 0

Views: 969

Answers (1)

Sneftel
Sneftel

Reputation: 41522

Keep in mind that a particular point on a face may be shadowed by multiple other faces; in order to compute an exact shadow area using polygon clipping you'd need to do extra boolean operations on the shadow polygons. You can't beat this approach for precision, but it's likely to be extremely slow.

So this wouldn't ordinarily be done through polygon clipping, but through raytracing/sampling/rasterization. Just fire a bunch of sun-parallel rays at the geometry and look for first intersections; the number of rays that hit a face, divided by its area, is proportional to the average irradiance of that face (as affected by both occlusion and the face normal). Any basic physics engine or geometry processing system should support the necessary ray-casting primitive.

Upvotes: 0

Related Questions