Permalink
Browse files

Created repo from existing code.

  • Loading branch information...
0 parents commit 0a3414375e90d1e876d9c0ae374d9b4bf8dee5d4 Jean-Francois Lalonde committed Oct 30, 2012
@@ -0,0 +1,17 @@
+License for Non-Commercial Use
+
+If this software is redistributed, this license must be included. The term
+software includes any source files, documentation, executables, models,
+and data.
+
+This software is available for general use by academic or non-profit, or
+government-sponsored researchers. It may also be used for evaluation
+purposes elsewhere. This license does not grant the right to use this
+software or any derivation of it in a for-profit enterprise. For commercial
+use, please contact the Tech Transfer Office at Carnegie Mellon University.
+
+This software comes with no warranty or guarantee of any kind. By using
+this software, the user accepts full liability.
+
+This license is written by Jean-Francois Lalonde, Copyright 2010.
+
@@ -0,0 +1,25 @@
+This package contains functions that convert to and from different types of environment map representations. Most of the code was implemented following the book:
+
+E. Reinhard, G. Ward, S. Pattanaik, and P. Debevec. High dynamic range imaging. Morgan Kaufman, 2005.
+
+Please see the book for more details. The "sky angular" format is a convenient way to visualize the top hemisphere of the environment map only, with minimal distorsions.
+
+The world coordinates [dx,dy,dz] have the following reference frame:
+
+ ^ y
+ |
+ |
+ .----> x
+ /
+ z v
+
+The camera would be looking in the negative z direction.
+
+** Note **
+
+All functions assume that their inputs is in double format in the [0, 1] range.
+im2double should do the trick.
+
+** History **
+
+04/19/11: Fixed small but in envmapWorld2SkyAngular.m which prevented correct conversion from skyAngular to latlong format.
@@ -0,0 +1,29 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapLatLong2ACube(angularEnvMap, dim)
+% Converts an environment map from the angular format to the "cube" format.
+%
+% Input parameters:
+% - angularEnvMap: environment map in angular format
+% - dim: dimensions of the output environment map (dim x dim)
+%
+% Output parameters:
+% - envMap: environment map in cube format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapAngular2Cube(angularEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxCube, dyCube, dzCube] = envmapCube2World(dim);
+indInside = (~isnan(dxCube) & ~isnan(dyCube) & ~isnan(dzCube));
+
+%% Get the values from the lat-long environment map representation
+envMap = zeros(size(dxCube, 1)*size(dxCube, 2), 3);
+envMap(indInside,:) = envmapWorld2Angular(angularEnvMap, dxCube(indInside), dyCube(indInside), dzCube(indInside));
+envMap = reshape(envMap, size(dxCube,1), size(dxCube,2), 3);
+
+
@@ -0,0 +1,24 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapAngular2LatLong(angularEnvMap, dim)
+% Converts an environment map from the angular format to the lat-long format
+%
+% Input parameters:
+% - angularEnvMap: environment map in angular format
+% - dim: dimensions of the output environment map (dim x 2*dim)
+%
+% Output parameters:
+% - envMap: environment map in lat-long format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapAngular2LatLong(angularEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxLatLong, dyLatLong, dzLatLong] = envmapLatLong2World(dim);
+
+%% Get the angular environment map representation
+envMap = envmapWorld2Angular(angularEnvMap, dxLatLong, dyLatLong, dzLatLong);
@@ -0,0 +1,27 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function [dx,dy,dz] = envmapAngular2World(dim)
+% Converts an environment map from the angular format to the [x,y,z] world directions
+%
+% Input parameters:
+% - dim: the angular environment dimensions
+%
+% Output parameters:
+% - [dx,dy,dz]: world directions
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function [dx,dy,dz] = envmapAngular2World(dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the desired world coordinates from the output angular map
+[u,v] = meshgrid(0:1/(dim-1):1, 0:1/(dim-1):1);
+
+thetaAngular = atan2(-2.*v+1, 2.*u-1);
+phiAngular = pi.*sqrt((2.*u-1).^2 + (2.*v-1).^2);
+
+dx = sin(phiAngular).*cos(thetaAngular);
+dy = sin(phiAngular).*sin(thetaAngular);
+dz = -cos(phiAngular);
@@ -0,0 +1,33 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapCube2Angular(cubeEnvMap, dim)
+% Converts an environment map from the cube format to the angular format.
+%
+% Input parameters:
+% - latLongEnvMap: environment map in latitude-longitude format
+% - dim: dimensions of the output environment map (dim x dim)
+%
+% Output parameters:
+% - envMap: environment map in angular format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapCube2Angular(cubeEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxAngular, dyAngular, dzAngular] = envmapAngular2World(dim);
+
+%% Get the values from the lat-long environment map representation
+envMap = envmapWorld2Cube(cubeEnvMap, dxAngular, dyAngular, dzAngular);
+
+%% Mask out the outside of the circle
+circleMask = zeros(dim);
+[c,r] = meshgrid(1:dim,1:dim);
+
+indCircle = (c-dim/2).^2+(r-dim/2).^2 <= (dim/2).^2;
+circleMask(indCircle) = 1;
+
+envMap = envMap .* repmat(circleMask, [1 1 3]);
@@ -0,0 +1,67 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function [dx,dy,dz] = envmapCube2World(dim)
+% Converts an environment map from the cube format to the [x,y,z] world directions
+%
+% Input parameters:
+% - dim: the cube environment dimensions
+%
+% Output parameters:
+% - [dx,dy,dz]: world directions
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function [dx,dy,dz] = envmapCube2World(dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+[u,v] = meshgrid(0:3/(3.*dim-1):3, 0:4/(4.*dim-1):4);
+dx = zeros(size(u)); dy = zeros(size(u)); dz = zeros(size(u));
+
+% up
+indUp = find(u >= 1 & u < 2 & v < 1);
+dx(indUp) = (u(indUp) - 1.5) .* 2;
+dy(indUp) = 1;
+dz(indUp) = (v(indUp) - 0.5) .* -2;
+
+% left
+indLeft = find(u < 1 & v >= 1 & v < 2);
+dx(indLeft) = -1;
+dy(indLeft) = (v(indLeft) - 1.5) .* -2;
+dz(indLeft) = (u(indLeft) - 0.5) .* -2;
+
+% forward
+indForward = find(u >= 1 & u < 2 & v >= 1 & v < 2);
+dx(indForward) = (u(indForward) - 1.5) .* 2;
+dy(indForward) = (v(indForward) - 1.5) .* -2;
+dz(indForward) = -1;
+
+% right
+indRight = find(u >= 2 & v >= 1 & v < 2);
+dx(indRight) = 1;
+dy(indRight) = (v(indRight) - 1.5) .* -2;
+dz(indRight) = (u(indRight) - 2.5) .* 2;
+
+% down
+indDown = find(u >= 1 & u < 2 & v >= 2 & v < 3);
+dx(indDown) = (u(indDown) - 1.5) .* 2;
+dy(indDown) = -1;
+dz(indDown) = (v(indDown) - 2.5) .* 2;
+
+% backward
+indBackward = find(u >= 1 & u < 2 & v >= 3);
+dx(indBackward) = (u(indBackward) - 1.5) .* 2;
+dy(indBackward) = (v(indBackward) - 3.5) .* 2;
+dz(indBackward) = 1;
+
+% normalize
+norm = sqrt(dx.^2 + dy.^2 + dz.^2);
+dx = dx ./ norm;
+dy = dy ./ norm;
+dz = dz ./ norm;
+
+
+
+
+
@@ -0,0 +1,33 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapLatLong2Angular(latLongEnvMap, dim)
+% Converts an environment map from the lat-long format to the angular format.
+%
+% Input parameters:
+% - latLongEnvMap: environment map in latitude-longitude format
+% - dim: dimensions of the output environment map (dim x dim)
+%
+% Output parameters:
+% - envMap: environment map in angular format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapLatLong2Angular(latLongEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxAngular, dyAngular, dzAngular] = envmapAngular2World(dim);
+
+%% Get the values from the lat-long environment map representation
+envMap = envmapWorld2LatLong(latLongEnvMap, dxAngular, dyAngular, dzAngular);
+
+%% Mask out the outside of the circle
+circleMask = zeros(dim);
+[c,r] = meshgrid(1:dim,1:dim);
+
+indCircle = (c-dim/2).^2+(r-dim/2).^2 <= (dim/2).^2;
+circleMask(indCircle) = 1;
+
+envMap = envMap .* repmat(circleMask, [1 1 size(envMap,3)]);
@@ -0,0 +1,29 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapLatLong2Cube(latLongEnvMap, dim)
+% Converts an environment map from the lat-long format to the cube format.
+%
+% Input parameters:
+% - latLongEnvMap: environment map in latitude-longitude format
+% - dim: dimensions of the output environment map (dim x dim)
+%
+% Output parameters:
+% - envMap: environment map in cube format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapLatLong2Cube(latLongEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxCube, dyCube, dzCube] = envmapCube2World(dim);
+indInside = (~isnan(dxCube) & ~isnan(dyCube) & ~isnan(dzCube));
+
+%% Get the values from the lat-long environment map representation
+envMap = zeros(size(dxCube, 1)*size(dxCube, 2), 3);
+envMap(indInside,:) = envmapWorld2LatLong(latLongEnvMap, dxCube(indInside), dyCube(indInside), dzCube(indInside));
+envMap = reshape(envMap, size(dxCube,1), size(dxCube,2), 3);
+
+
@@ -0,0 +1,33 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapLatLong2SkyAngular(latLongEnvMap, dim)
+% Converts an environment map from the lat-long format to the "sky angular" format.
+%
+% Input parameters:
+% - latLongEnvMap: environment map in latitude-longitude format
+% - dim: dimensions of the output environment map (dim x dim)
+%
+% Output parameters:
+% - envMap: environment map in sky angular format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapLatLong2SkyAngular(latLongEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxAngular, dyAngular, dzAngular] = envmapSkyAngular2World(dim);
+
+%% Get the values from the lat-long environment map representation
+envMap = envmapWorld2LatLong(latLongEnvMap, dxAngular, dyAngular, dzAngular);
+
+%% Mask out the outside of the circle
+circleMask = zeros(dim);
+[c,r] = meshgrid(1:dim,1:dim);
+
+indCircle = (c-dim/2).^2+(r-dim/2).^2 <= (dim/2).^2;
+circleMask(indCircle) = 1;
+
+envMap = envMap .* repmat(circleMask, [1 1 size(envMap,3)]);
@@ -0,0 +1,27 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function [dx,dy,dz] = envmapLatLong2World(height)
+% Converts an environment map from the angular format to the [x,y,z] world directions
+%
+% Input parameters:
+% - height: height of the latitude-longitude output (width = 2*height)
+%
+% Output parameters:
+% - [dx,dy,dz]: world directions
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function [dx,dy,dz] = envmapLatLong2World(height)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the desired world coordinates from the output lat-long map
+[u,v] = meshgrid(0:2/(2.*height-1):2, 0:1/(height-1):1);
+
+thetaLatLong = pi.*(u-1);
+phiLatLong = pi.*v;
+
+dx = sin(phiLatLong).*sin(thetaLatLong);
+dy = cos(phiLatLong);
+dz = -sin(phiLatLong).*cos(thetaLatLong);
@@ -0,0 +1,24 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% function envMap = envmapSkyAngular2LatLong(angularEnvMap, dim)
+% Converts an environment map from the "sky angular" format to the lat-long format
+%
+% Input parameters:
+% - angularEnvMap: environment map in sky angular format
+% - dim: dimensions of the output environment map (dim x 2*dim)
+%
+% Output parameters:
+% - envMap: environment map in the lat-long format
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function envMap = envmapSkyAngular2LatLong(angularEnvMap, dim)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Copyright 2006-2009 Jean-Francois Lalonde
+% Carnegie Mellon University
+% Do not distribute
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Get the 3-D directions
+[dxLatLong, dyLatLong, dzLatLong] = envmapLatLong2World(dim);
+
+%% Get the angular environment map representation
+envMap = envmapWorld2SkyAngular(angularEnvMap, dxLatLong, dyLatLong, dzLatLong);
Oops, something went wrong.

0 comments on commit 0a34143

Please sign in to comment.