Add homogeneous coordinate functions
Add functions for transformations in 3 dimensional homogeneous space. These include functions that generate transformation matrices and perform matrix multiplication.
This commit is contained in:
parent
f4c37598ee
commit
1c6a635497
174
rtengine/homogeneouscoordinates.cc
Normal file
174
rtengine/homogeneouscoordinates.cc
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of RawTherapee.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2019 Lawrence Lee <billee@ucdavis.edu>
|
||||||
|
*
|
||||||
|
* RawTherapee is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* RawTherapee is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include "homogeneouscoordinates.h"
|
||||||
|
|
||||||
|
namespace rtengine
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
homogeneous::Vector<T> operator*(const homogeneous::Matrix<T>& a, const homogeneous::Vector<T>& b)
|
||||||
|
{
|
||||||
|
homogeneous::Vector<T> prod;
|
||||||
|
|
||||||
|
prod.fill(0);
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
prod[i] += a[i][j] * b[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
homogeneous::Matrix<T> operator*(const homogeneous::Matrix<T>& a, const homogeneous::Matrix<T>& b)
|
||||||
|
{
|
||||||
|
homogeneous::Matrix<T> prod;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
prod[i].fill(0);
|
||||||
|
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
for (int k = 0; k < 4; k++) {
|
||||||
|
prod[i][j] += a[i][k] * b[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace homogeneous
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> projectionMatrix(T location, Axis normal)
|
||||||
|
{
|
||||||
|
Matrix<T> matrix;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
matrix[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix[0][0] = location;
|
||||||
|
matrix[1][1] = location;
|
||||||
|
matrix[2][2] = location;
|
||||||
|
matrix[3][3] = 0;
|
||||||
|
|
||||||
|
switch (normal) {
|
||||||
|
case X:
|
||||||
|
matrix[3][0] = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Y:
|
||||||
|
matrix[3][1] = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Z:
|
||||||
|
matrix[3][2] = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> rotationMatrix(double radians, Axis axis)
|
||||||
|
{
|
||||||
|
Matrix<T> matrix;
|
||||||
|
|
||||||
|
switch (axis) {
|
||||||
|
case X:
|
||||||
|
matrix[0][0] = 1;
|
||||||
|
matrix[0][1] = 0;
|
||||||
|
matrix[0][2] = 0;
|
||||||
|
matrix[1][0] = 0;
|
||||||
|
matrix[1][1] = cos(radians);
|
||||||
|
matrix[1][2] = -sin(radians);
|
||||||
|
matrix[2][0] = 0;
|
||||||
|
matrix[2][1] = sin(radians);
|
||||||
|
matrix[2][2] = cos(radians);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Y:
|
||||||
|
matrix[0][0] = cos(radians);
|
||||||
|
matrix[0][1] = 0;
|
||||||
|
matrix[0][2] = sin(radians);
|
||||||
|
matrix[1][0] = 0;
|
||||||
|
matrix[1][1] = 1;
|
||||||
|
matrix[1][2] = 0;
|
||||||
|
matrix[2][0] = -sin(radians);
|
||||||
|
matrix[2][1] = 0;
|
||||||
|
matrix[2][2] = cos(radians);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Z:
|
||||||
|
matrix[0][0] = cos(radians);
|
||||||
|
matrix[0][1] = -sin(radians);
|
||||||
|
matrix[0][2] = 0;
|
||||||
|
matrix[1][0] = sin(radians);
|
||||||
|
matrix[1][1] = cos(radians);
|
||||||
|
matrix[1][2] = 0;
|
||||||
|
matrix[2][0] = 0;
|
||||||
|
matrix[2][1] = 0;
|
||||||
|
matrix[2][2] = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix[0][3] = 0;
|
||||||
|
matrix[1][3] = 0;
|
||||||
|
matrix[2][3] = 0;
|
||||||
|
matrix[3][0] = 0;
|
||||||
|
matrix[3][1] = 0;
|
||||||
|
matrix[3][2] = 0;
|
||||||
|
matrix[3][3] = 1;
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> translationMatrix(T x, T y, T z)
|
||||||
|
{
|
||||||
|
Matrix<T> matrix;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
matrix[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix[0][0] = 1;
|
||||||
|
matrix[1][1] = 1;
|
||||||
|
matrix[2][2] = 1;
|
||||||
|
matrix[0][3] = x;
|
||||||
|
matrix[1][3] = y;
|
||||||
|
matrix[2][3] = z;
|
||||||
|
matrix[3][3] = 1;
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
75
rtengine/homogeneouscoordinates.h
Normal file
75
rtengine/homogeneouscoordinates.h
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of RawTherapee.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2019 Lawrence Lee <billee@ucdavis.edu>
|
||||||
|
*
|
||||||
|
* RawTherapee is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* RawTherapee is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
namespace rtengine
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace homogeneous
|
||||||
|
{
|
||||||
|
|
||||||
|
enum Axis {X, Y, Z};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using Matrix = std::array<std::array<T, 4>, 4>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3 dimensional homogeneous vector.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
using Vector = std::array<T, 4>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a 3 dimensional transformation matrix for projection onto a plane.
|
||||||
|
* @param location Distance from the origin to the plane.
|
||||||
|
* @param normal Direction of the plane's normal.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> projectionMatrix(T location, Axis normal);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a 3 dimensional transformation matrix for rotation.
|
||||||
|
* @param radians Rotation angle.
|
||||||
|
* @param axis Axis of rotation.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> rotationMatrix(T radians, Axis axis);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a 3 dimensional transformation matrix for translation.
|
||||||
|
* @param x Translation in the the x-direction.
|
||||||
|
* @param y Translation in the the y-direction.
|
||||||
|
* @param z Translation in the the z-direction.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
Matrix<T> translationMatrix(T x, T y, T z);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
homogeneous::Vector<T> operator*(const homogeneous::Matrix<T>& a, const homogeneous::Vector<T>& b);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
homogeneous::Matrix<T> operator*(const homogeneous::Matrix<T>& a, const homogeneous::Matrix<T>& b);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "homogeneouscoordinates.cc"
|
Loading…
x
Reference in New Issue
Block a user