Geometry

The functions from the Geometry category are useful to perform transformations between cartesian and polar or spherical coordinates. It is also possible to compute a cartesian 2-dimensional grid with meshgrid or to scatter three-dimensional nodes over a sphere with the sphere and sphere2 (Fibonacci sphere) functions.

cart2pol

template<typename R, typename S>
inline auto castor::cart2pol(R const &X, S const &Y)
template<typename R, typename S>
auto castor::cart2pol(matrix<R> const &X, matrix<S> const &Y)

Transform Cartesian to polar coordinates.

(THE,RHO) = cart2pol(X,Y) transforms corresponding elements of data stored in cartesian coordinates (X,Y) to polar coordinates (THE,RHO).

The arrays X and Y must be the same size and angle THE is returned in radians.

Remark: This function supports scalar arguments.

matrix<> X = {1,-1,-1,1};
matrix<> Y = {1,1,-1,-1};
matrix<> THE, RHO;
std::tie(THE,RHO) = cart2pol(X,Y);
disp(rad2deg(THE));
disp(RHO);

See pol2cart, cart2sph, sph2cart.

cart2sph

template<typename Q, typename R, typename S>
inline auto castor::cart2sph(Q const &X, R const &Y, S const &Z)
template<typename Q, typename R, typename S>
auto castor::cart2sph(matrix<Q> const &X, matrix<R> const &Y, matrix<S> const &Z)

Transform Cartesian to spherical coordinates.

(THE,PHI,RHO) = cart2sph(X,Y,Z) transforms corresponding elements of data stored in Cartesian coordinates (X,Y,Z) to spherical coordinates (THE,PHI,RHO). THE is the counterclockwise angle in the xy plane measured from the positive x axis and PHI is the elevation angle from the xy plane.

The arrays X,Y and Z must be the same size and angles (THE,PHI) are returned in radians.

Remark: This function supports scalar arguments.

matrix<> X = {1,-1,-1,1};
matrix<> Y = {1,1,-1,-1};
matrix<> Z = {1,1,-1,-1};
matrix<> THE, PHI, RHO;
std::tie(THE,PHI,RHO) = cart2sph(X,Y,Z);
disp(rad2deg(THE));
disp(rad2deg(PHI));
disp(RHO);

See sph2cart, cart2pol, pol2cart.

idx2sph

template<typename T = double>
auto castor::idx2sph(matrix<std::size_t> const &idx, std::size_t m = 0, std::size_t n = 0)

Discretization index to sherical coordinates (in radian).

(AZM,ELV) = idx2sph(IDX) gives azimut AZM and elevation ELV coordinates of corresponding indices IDX for a 1-degree sphere.

matrix<std::size_t> I = range(0,360*181);
matrix<> AZM, ELV;
std::tie(AZM,ELV) = idx2sph(I);
matrix<> J = sph2idx(AZM,ELV);
disp(max(J-I));

(AZM,ELV) = idx2sph(IDX,M) uses M values both in azimut and elevation.

(AZM,ELV) = idx2sph(IDX,M,N) convert indices matrix IDX, corresponding to M-by-N spherical discretization, to azimut and elevation angles.

See sph2idx, sphere.

meshgrid

template<typename T>
auto castor::meshgrid(matrix<T> const &x, matrix<T> const &y = {})

Cartesian rectangular grid in 2-D.

[X,Y] = meshgrid(x,y) returns 2-D grid coordinates based on the coordinates contained in vectors x and y. X is a matrix where each row is a copy of x, and Y is a matrix where each column is a copy of y. The grid represented by the coordinates X and Y has length(y) rows and length(x) columns.

[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x), returning square grid coordinates with grid size length(x)-by-length(x).

matrix<> X,Y;
std::tie(X,Y) = meshgrid(linspace(-1,1,10),linspace(0,2,5));
disp(X);
disp(Y);

See kron.

pol2cart

template<typename R, typename S>
inline auto castor::pol2cart(R const &THE, S const &RHO)
template<typename R, typename S>
auto castor::pol2cart(matrix<R> const &THE, matrix<S> const &RHO)

Transform polar to Cartesian coordinates.

(X,Y) = pol2cart(THE,RHO) transforms corresponding elements of data stored in polar coordinates (THE,RHO) to Cartesian coordinates (X,Y). The arrays THE and RHO must the same size and angle THE must be in radians.

Remark: This function supports scalar arguments.

matrix<> THE = {0,M_PI/2,M_PI,-M_PI/2};
matrix<> RHO = {1,1,1,1};
matrix<> X, Y;
std::tie(X,Y) = pol2cart(THE,RHO);
disp(X);
disp(Y);

See cart2pol, cart2sph, sph2cart.

sph2cart

template<typename Q, typename R, typename S>
inline auto castor::sph2cart(Q const &THE, R const &PHI, S const &RHO)
template<typename Q, typename R, typename S>
auto castor::sph2cart(matrix<Q> const &THE, matrix<R> const &PHI, matrix<S> const &RHO)

Transform spherical to Cartesian coordinates.

(X,Y,Z) = sph2cart(THE,PHI,RHO) transforms corresponding elements of data stored in spherical coordinates (THE,PHI,RHO) to Cartesian coordinates X,Y,Z. The arrays THE, PHI, and RHO must be the same size. THE and PHI must be in radians.

THE is the counterclockwise angle in the xy plane measured from the positive x axis. PHI is the elevation angle from the xy plane.

Remark: This function supports scalar arguments.

matrix<> THE = {0,M_PI/2,M_PI,-M_PI/2};
matrix<> PHI = {0,0,0,0};
matrix<> RHO = {1,1,1,1};
matrix<> X, Y, Z;
std::tie(X,Y,Z) = sph2cart(THE,PHI,RHO);
disp(X);
disp(Y);
disp(Z);

See cart2sph, cart2pol, pol2cart.

sph2idx

template<typename R, typename S>
matrix<std::size_t> castor::sph2idx(matrix<R> const &azm, matrix<S> const &elv, std::size_t m = 0, std::size_t n = 0)

Sherical coordinate (in radian) to discretization index.

IDX = sph2idx(AZM,ELV) give indices of a 1-degree spherical discretization cooresponding to azimut AZM and elevation ELV

matrix<std::size_t> I = range(0,360*181);
matrix<> AZM, ELV;
std::tie(AZM,ELV) = idx2sph(I);
matrix<> J = sph2idx(AZM,ELV);
disp(max(J-I));

IDX = sph2idx(AZM,ELV,M) use M azimut and elevation.

IDX = sph2idx(AZM,ELV,M,N) convert azimuth AZM and elevation ELV angles to indices IDX for spherical coordinates of M-by-N size.

See idx2sph, sphere.

sphere

template<typename T = double>
auto castor::sphere(std::size_t m = 0, std::size_t n = 0)

Generate sphere.

[X,Y,Z] = sphere() generate Carthesian coordinates of a uniform 1-degree discretisation sphere.

[X,Y,Z] = sphere(M) generate sphere with M azimuts and elevations.

[X,Y,Z] = sphere(M,N) generates carthesian coordinates (X,Y,Z) of a unit sphere with M elevations and N azimuts.

matrix<> X,Y,Z;
std::tie(X,Y,Z) = sphere(3,4);
disp(X);
disp(Y);
disp(Z);

See idx2sph, sph2idx.

sphere2

template<typename T = double>
auto castor::sphere2(std::size_t n, T rho = 1)

Generate sphere using fibonacci rules.

[X,Y,Z] = sphere2(N) generate a quasi-uniform unit sphere with N vertices.

[X,Y,Z] = sphere2(N,rho) generate N-sphere of radius rho.

matrix<> X,Y,Z;
std::tie(X,Y,Z) = sphere2(100);
disp(X);
disp(Y);
disp(Z);

See sphere.