Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #ifndef ANDROID_DVR_FIELD_OF_VIEW_H_ |
| 2 | #define ANDROID_DVR_FIELD_OF_VIEW_H_ |
| 3 | |
| 4 | #include <cmath> |
| 5 | |
| 6 | #include <private/dvr/eigen.h> |
| 7 | |
| 8 | namespace android { |
| 9 | namespace dvr { |
| 10 | |
| 11 | // Encapsulates a generalized, asymmetric field of view with four half angles. |
| 12 | // Each half angle denotes the angle between the corresponding frustum plane. |
| 13 | // Together with a near and far plane, a FieldOfView forms the frustum of an |
| 14 | // off-axis perspective projection. |
| 15 | class FieldOfView { |
| 16 | public: |
| 17 | // The default constructor sets an angle of 0 (in any unit) for all four |
| 18 | // half-angles. |
| 19 | FieldOfView() : left_(0.0f), right_(0.0f), bottom_(0.0f), top_(0.0f) {} |
| 20 | |
| 21 | // Constructs a FieldOfView from four angles. |
| 22 | FieldOfView(float left, float right, float bottom, float top) |
| 23 | : left_(left), right_(right), bottom_(bottom), top_(top) {} |
| 24 | |
| 25 | explicit FieldOfView(const float* fov) |
| 26 | : FieldOfView(fov[0], fov[1], fov[2], fov[3]) {} |
| 27 | |
| 28 | // Accessors for all four half-angles. |
| 29 | float GetLeft() const { return left_; } |
| 30 | float GetRight() const { return right_; } |
| 31 | float GetBottom() const { return bottom_; } |
| 32 | float GetTop() const { return top_; } |
| 33 | |
| 34 | // Setters for all four half-angles. |
| 35 | void SetLeft(float left) { left_ = left; } |
| 36 | void SetRight(float right) { right_ = right; } |
| 37 | void SetBottom(float bottom) { bottom_ = bottom; } |
| 38 | void SetTop(float top) { top_ = top; } |
| 39 | |
| 40 | Eigen::AffineMatrix<float, 4> GetProjectionMatrix(float z_near, |
| 41 | float z_far) const { |
| 42 | float x_left = -std::tan(left_) * z_near; |
| 43 | float x_right = std::tan(right_) * z_near; |
| 44 | float y_bottom = -std::tan(bottom_) * z_near; |
| 45 | float y_top = std::tan(top_) * z_near; |
| 46 | |
| 47 | float zero = 0.0f; |
| 48 | if (x_left == x_right || y_bottom == y_top || z_near == z_far || |
| 49 | z_near <= zero || z_far <= zero) { |
| 50 | return Eigen::AffineMatrix<float, 4>::Identity(); |
| 51 | } |
| 52 | |
| 53 | float x = (2 * z_near) / (x_right - x_left); |
| 54 | float y = (2 * z_near) / (y_top - y_bottom); |
| 55 | float a = (x_right + x_left) / (x_right - x_left); |
| 56 | float b = (y_top + y_bottom) / (y_top - y_bottom); |
| 57 | float c = (z_near + z_far) / (z_near - z_far); |
| 58 | float d = (2 * z_near * z_far) / (z_near - z_far); |
| 59 | |
| 60 | // Note: Eigen matrix initialization syntax is always 'column-major' |
| 61 | // even if the storage is row-major. Or in other words, just write the |
| 62 | // matrix like you'd see in a math textbook. |
| 63 | Eigen::AffineMatrix<float, 4> result; |
| 64 | result.matrix() << x, 0, a, 0, |
| 65 | 0, y, b, 0, |
| 66 | 0, 0, c, d, |
| 67 | 0, 0, -1, 0; |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | static FieldOfView FromProjectionMatrix( |
| 72 | const Eigen::AffineMatrix<float, 4>& m) { |
| 73 | // Compute tangents. |
| 74 | float tan_vert_fov = 1.0f / m(1, 1); |
| 75 | float tan_horz_fov = 1.0f / m(0, 0); |
| 76 | float t = (m(1, 2) + 1.0f) * tan_vert_fov; |
| 77 | float b = (m(1, 2) - 1.0f) * tan_vert_fov; |
| 78 | float l = (m(0, 2) - 1.0f) * tan_horz_fov; |
| 79 | float r = (m(0, 2) + 1.0f) * tan_horz_fov; |
| 80 | |
| 81 | return FieldOfView(std::atan(-l), std::atan(r), std::atan(-b), |
| 82 | std::atan(t)); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | float left_; |
| 87 | float right_; |
| 88 | float bottom_; |
| 89 | float top_; |
| 90 | }; |
| 91 | |
| 92 | } // namespace dvr |
| 93 | } // namespace android |
| 94 | |
| 95 | #endif // ANDROID_DVR_FIELD_OF_VIEW_H_ |