A computationally cheap trick to determine shadow in a voxel model

: Representation of scenes on the Earth surface by using voxels is gaining attention because of its suitability for integrating heterogeneous data sources in simulations and quantitative models. Computation of shadows in such models is needed, for example, to obtain crop suitability of agricultural fields in the presence of trees and buildings, or to analyze urban heat island causes and effects. We present an efficient algorithm to compute which of the voxels in a dataset receive direct sunlight, given the solar azimuth and elevation angles. The algorithm can work with multiple (sparse and dense) voxel storage strategies.


INTRODUCTION
The interaction of sunlight with the Earth surface is the driver of many fundamental Earth processes.When looking at a level of detail where individual three-dimensional objects, such as buildings and trees, play a role, the interaction is heavily influenced by the question whether the surface at a particular position, and at a given moment in time, is either 'in the sun' or 'in the shadow', i.e. whether it receives direct sunlight (coming in a straight line from the sun) or only indirect light (coming for elsewhere in the blue sky, or from reflecting surfaces in the surrounding).Clearly, the answer depends on the presence of other objects on the path between the sun at that moment in time and the position of the surface under consideration.In the 1 Corresponding author absence of cloud cover, such objects blocking the sunlight, would be buildings, hills, trees etc., which are part of the scene.
Modelling these effects is of interest when studying, for example, urban microclimate, field-based crop suitability and yield prediction, or solar panel placement.It assumes that a sufficiently detailed 3D model of the area under consideration is available.Because the sun is at any given possible position only once (or twice) per year, the model needs to be run many times to get reliable estimates of sun and shadow durations over a long period, like a growing season.Therefore, computational efficiency is quite important.
Fig. 1 Voxel model with shadows of the Roundhouse at UNSW Kensington campus, Sydney.

Voxels
The study in this paper was conducted within a larger effort to determine the usefulness of representing 3-dimensional geospatial information in large 3D grids, in which the raster elements are called voxels.The expectation is that this, in comparison to vector models, will lead to a more unifying approach on integrating many kinds of geo-information into computational simulation models, allowing to better exploit the spatial and temporal relationships between different objects, processes, themes, layers, etc, stored in a 3D GIS.
Here, we are considering a 3D model of an outdoor scene in sunny weather.Sunlight enters the scene as parallel rays from a direction that is defined by a pair of angles denoting sun azimuth and elevation.The goal of the paper is to compute which parts in the scene receive direct sunlight -the other parts being in the shadow.Fig. 1 shows an example.
The model describes a scene on the Earth surface, such as a part of a city.It is filled with different materials, forming objects, such as the terrain, buildings, bridges, trees etc.Air is present in the scene as well, and it is the only 'material' that is transparent to sunlight.Sunlight is blocked (i.e.either absorbed or reflected) by all other materials.Therefore, the objects cast shadows, which will prevent certain parts of surfaces of other objects from receiving direct sunlight -the effect on object surfaces of indirect light coming from the sky or being reflected by other objects is not the subject of this paper.
The scene has the extent of a rectangular block, subdivided into little cubes called voxels.The cubes are indexed by threedimensional integer coordinates (x,y,z) ranging from (0,0,0) to (Nx-1,Ny-1,Nz-1) and therefore the extent of the scene, measured in voxels, equals Nx × Ny × Nz.We will assume the axes are parallel to a relevant (perhaps local) terrestrial (X,Y,Z) coordinate system.The increment in X, Y or Z corresponding to an integer step in x, y or z is the resolution of the model -we could think of non-cubic voxels (with shapes likes bricks or pizza boxes), but it would not add much to the argument.The same holds for having a rotation between the two coordinate systems -except that it would require the azimuth (and/or elevation) angle to be rotated as well.Furthermore, we consider only shadows that are cast by objects inside the block.There is no shadow from objects surrounding the block.Finally, we will assume that the terrain surface is inside the block, and that all objects are lower than the top of the block.Therefore, the top layer(s) of the block consist/s of air, and the lowest ones of terrain.
Voxels are considered homogeneous.Their content is denoted as a scalar value, representing a single material or material class.We will assume the voxel size (the resolution) to be such that an urban scene is represented at a scale that reflects building details like balconies, chimneys etc., but perhaps not smaller details like ornaments or door handles -a typical voxel size would be in the range between 0.1 and 1.0 m.If the indoor environment is to be considered for shadow determination as well, we must allow the sunlight to enter through windows.These will have to be modelled as holes in the walls of the building.Furthermore, if the sunlight is supposed to be only partially blocked by tree crowns, these will have to be represented as mixtures of 'air' and 'leaf' voxels.
Also, the question about objects being in the sun or shadow will be answered per entire voxel.This will mainly concern voxels being illuminated from above -or not.Depending on the incidence angle, however, two more faces except the top face of a voxel are candidates for being sunlit.This will be addressed below.
The result of shadow modelling in computational models addresses, for example, the amount of sunlight that is captured by objects of interest over time.This will boil down to counting sunlit voxels per object (and per epoch).This in contrast to many existing methods, including those in game engines, which mainly aim at producing fancier visualisations, and have been known for a long time [Crow, 1977].Such models can also be used to radiometrically correct satellite imagery prior to automatic change detection [van der Sande et. al. 2008], in order to prevent shadow differences being recorded as detected changes.
The straightforward method to obtain the result described above would be to have a ray of light entering a voxel at the top of the block at the desired angle, and trace it down through the 'air' voxels layer by layer until it hits an other-material (object) voxel.This is then marked as 'sunlit' and the ray stops.By repeating this process, starting from every voxel in the top layer of the block, one will end up with a collection of sunlit-marked voxels; the remaining ones are 'shadow'.To make it possible that each bottom voxel in the block could eventually be reached by a ray originating from the top, it will be necessary to extend the block sideways with 'air' voxels, depending on the azimuth and elevation angles, before starting to trace rays.This method is computationally expensive, because it has to traverse almost the entire voxel space sequentially, all the time performing computations to get from one voxel to the next, while intersecting the oblique ray with (perhaps several) voxels at each layer.We present a simple method to get the same result very little computational effort.

VOXEL SHADOW ALGORITHM
We regard the block of voxels describing a scene as a stack of Nz horizontal layers, numbered from 0 to Nz -1, each having a size of Nx × Ny voxels.

The Basic Idea
The idea behind the algorithm is to shift each layer of the voxel model (Fig. 1) horizontally in the opposite direction of the sun azimuth angle, by an amount that depends on the sun elevation angle and the height of the layer.As a result (Fig. 3), points that are geometrically located on the same sunbeam (if this beam were not blocked by the first non-air point it hits), will be exactly above one-another after the shifting took place.In the next step of the process, the shifted voxel block is examined from top to bottom, one vertical column at a time, and the highest non-air voxel in each column is marked as 'sunlit'.All voxels that are located lower in the column remain unmarked, meaning 'shadow' (fig.4).Finally, the sunlit voxels are shifted back to their original positions, layer by layer, by the amount that belongs to that layer (Fig. 5).They become the sunlit voxels in the original voxel space; the remaining voxels are in the shadow (Fig. 6).

Implementations in different voxel storage structures
We distinguish two storage structures for matrices, which we will name dense and sparse -both can be applied to represent voxel spaces in RAM or on disk.Very generally speaking, dense matrices are quicker during processing, but sparse ones require less memory.However, the 'sparser' a dataset really is, the faster its processing will tend to be, and, depending on the operation, there may exist a break-even point where 'sparse' gets quicker than 'dense'.
Generally, computer memory is linear; data are stored in words (of e.g.64 bits) at addressable locations, where the range of addresses is a consecutive linear list.The advantage of the proposed layer shift algorithm, as compared to the 'straightforward' approach, is firstly that the amount of shift is constant within each layer, and needs to be computed only once.But more importantly, performing those shifts means to move relatively large chunks of data around in memory, and this can be done quite quickly, as far as these are stored compactly, i.e. as layers.If this is not the case, it may be advantageous to perform a generalized transpose first.In the shifted dataset, a search for the highest non-air voxel is performed, which is again a very simple operation (which may benefit from a pillar-wise storage, however).
The sparse matrix representation, on the other hand, attempts to take advantage of the fact that usually a majority of the voxels in a scene will share a single value or belong to a single class.In outdoor, above-ground, scenes this value or class will be the one denoting 'air'.In a sparse matrix, those voxels are not stored and do not occupy any space.At the downside, the positions of (other) voxels in the data structure cannot be easily computed on the basis of their position in the 3D space.Instead, the indices of the (non-air) voxels are stored explicitly.Therefore, the indices require space as well, which comes in addition to the space for the (non-air) values.Retrieving a voxel implies performing a search for the wanted index.Moreover, finding out that a voxel at a certain position is 'air' means to discover that it is not present in the data, which in a first approximation might involve checking the entire dataset.Fortunately, techniques exist, such as hashing and spatial indexing, to greatly speed up those searches, but on the other hand hash tables and indices require memory space as well.
The 'straightforward' shadow algorithm is expensive with sparse matrices, since tracing rays diagonally through the space requires continuous searching for the next (perhaps non-existing) voxel.
Layer shifting, on the other hand, is extremely efficient.It has to be done for the non-air voxels only, coordinate by coordinate, where (x,y) have to be changed by an amount only depending on z -this amount can be taken from a lookup table.Next, the shifted non-air voxels have to be re-grouped based on their new (x,y) indices, and the one with the highest z has to be identified in each group.Now the efficiency depends heavily on the applied hashing (or indexing) technique, but again there are only non-air voxels involved.The top ones obtain a modified value ('sun') and are shifted back to their original positions, replacing the original values.

Thin structures with low sun positions
The results of the above-described algorithm are correct, provided the sun elevation angle is larger than 45 degrees.At a smaller angle, it occurs that a layer a height z needs to be shifted more than one voxel further than the layer a height z-1.Structures of only one voxel thickness, in such a case, may start to show 'air' between subsequent layers -hopefully Fig. 7 illustrates the effect sufficiently clearly.The cause is shown in Fig. 8: Differences in shifts between subsequent layers are more than the wall thickness.The solution is to perform the shifting bottom up, keep track of the shifts between layers, and fill the holes as the (might) occur (Fig. 9).After that, the entire space under the shifted wall will be 'filled' with uninterrupted shadow, and remain so after shifting back (Fig. 10).

Side views of sunlit walls
The proposed algorithm is only concerned with how sun is shining on objects from above.It gives correct results, for example, at the terrain and at the roofs of buildings.In addition, all other voxels in the shadow, which are part of a wall, for example, will never show up as 'sunlit'.Walls that are sunlit, however, may get strangely striped (Fig 11).The cause of this gets immediately clear when inspecting the shifted block (Fig 12).Indeed there are sunlit (top) voxels along the walls.As a solution, one might choose to allow only voxels that have 'air' above them n the original models as candidates for being 'sunlit'.They can be selected with the same algorithms as the sunlit voxels in the shifted model.The selection can be made before or after shifting, and the result is shown in Fig. 12.The result visually more appealing, but not necessarily more useful from a modelling point of view, where one might quantize the interaction between sunlight and objects by counting sunlit voxels -then the result in Fig. 11 is actually not bad.

EVALUATION AND CONCLUSION
We have shown the feasibility of computing shadowed vs. sunlit voxels using a little algorithm that directly runs in the voxel domain.Its main attraction is simplicity.Apart from delivering a useful result, for which several applications have been identified, it shows the potential of voxel-based modelling of Earth processes.

Fig. 7 .
Fig. 7. Thin wall and low sun give a broken shadow.

Fig 10 .
Fig 10.Corrected shadow (compared to Fig. 7) of thin wall at low sun position

Fig. 13 .
Fig. 13.Shadow model where only top voxels are sunlit