view_matrix.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_CLIENT_UI_VIEW_MATRIX_H_
  5. #define REMOTING_CLIENT_UI_VIEW_MATRIX_H_
  6. #include <array>
  7. namespace remoting {
  8. // A 2D non-skew equally scaled transformation matrix.
  9. // | SCALE, 0, OFFSET_X, |
  10. // | 0, SCALE, OFFSET_Y, |
  11. // | 0, 0, 1 |
  12. class ViewMatrix {
  13. public:
  14. struct Vector2D {
  15. float x;
  16. float y;
  17. };
  18. // Same as Vector2D. This alias just serves as a context hint.
  19. using Point = Vector2D;
  20. // Creates an empty matrix (0 scale and offsets).
  21. ViewMatrix();
  22. ViewMatrix(float scale, const Vector2D& offset);
  23. ~ViewMatrix();
  24. // Applies the matrix on the point and returns the result.
  25. Point MapPoint(const Point& point) const;
  26. // Applies the matrix on the vector and returns the result. This only scales
  27. // the vector and does not apply offset.
  28. Vector2D MapVector(const Vector2D& vector) const;
  29. // Sets the scale factor, with the pivot point at (0, 0). This WON'T affect
  30. // the offset.
  31. void SetScale(float scale);
  32. // Returns the scale of this matrix.
  33. float GetScale() const;
  34. // Sets the offset.
  35. void SetOffset(const Point& offset);
  36. const Vector2D& GetOffset() const;
  37. // Adjust the matrix M to M' such that:
  38. // M * p_a = p_b => M' * p_a = scale * (p_b - pivot) + pivot
  39. void PostScale(const Point& pivot, float scale);
  40. // Applies translation to the matrix.
  41. // M * p_a = p_b => M' * p_a = p_b + delta
  42. void PostTranslate(const Vector2D& delta);
  43. // Returns the inverse of this matrix.
  44. ViewMatrix Invert() const;
  45. // Returns true if the scale and offsets are both 0.
  46. bool IsEmpty() const;
  47. // Converts to the 3x3 matrix array.
  48. std::array<float, 9> ToMatrixArray() const;
  49. private:
  50. float scale_;
  51. Vector2D offset_;
  52. };
  53. } // namespace remoting
  54. #endif // REMOTING_CLIENT_UI_VIEW_MATRIX_H_