vector3d_f.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2012 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. // Defines a simple float vector class. This class is used to indicate a
  5. // distance in two dimensions between two points. Subtracting two points should
  6. // produce a vector, and adding a vector to a point produces the point at the
  7. // vector's distance from the original point.
  8. #ifndef UI_GFX_GEOMETRY_VECTOR3D_F_H_
  9. #define UI_GFX_GEOMETRY_VECTOR3D_F_H_
  10. #include <iosfwd>
  11. #include <string>
  12. #include "ui/gfx/geometry/geometry_export.h"
  13. #include "ui/gfx/geometry/vector2d_f.h"
  14. namespace gfx {
  15. class GEOMETRY_EXPORT Vector3dF {
  16. public:
  17. constexpr Vector3dF() : x_(0), y_(0), z_(0) {}
  18. constexpr Vector3dF(float x, float y, float z) : x_(x), y_(y), z_(z) {}
  19. constexpr explicit Vector3dF(const Vector2dF& other)
  20. : x_(other.x()), y_(other.y()), z_(0) {}
  21. constexpr float x() const { return x_; }
  22. void set_x(float x) { x_ = x; }
  23. constexpr float y() const { return y_; }
  24. void set_y(float y) { y_ = y; }
  25. constexpr float z() const { return z_; }
  26. void set_z(float z) { z_ = z; }
  27. // True if all components of the vector are 0.
  28. bool IsZero() const;
  29. // Add the components of the |other| vector to the current vector.
  30. void Add(const Vector3dF& other);
  31. // Subtract the components of the |other| vector from the current vector.
  32. void Subtract(const Vector3dF& other);
  33. void operator+=(const Vector3dF& other) { Add(other); }
  34. void operator-=(const Vector3dF& other) { Subtract(other); }
  35. void SetToMin(const Vector3dF& other) {
  36. x_ = x_ <= other.x_ ? x_ : other.x_;
  37. y_ = y_ <= other.y_ ? y_ : other.y_;
  38. z_ = z_ <= other.z_ ? z_ : other.z_;
  39. }
  40. void SetToMax(const Vector3dF& other) {
  41. x_ = x_ >= other.x_ ? x_ : other.x_;
  42. y_ = y_ >= other.y_ ? y_ : other.y_;
  43. z_ = z_ >= other.z_ ? z_ : other.z_;
  44. }
  45. // Gives the square of the diagonal length of the vector.
  46. double LengthSquared() const;
  47. // Gives the diagonal length of the vector.
  48. float Length() const;
  49. // Scale all components of the vector by |scale|.
  50. void Scale(float scale) { Scale(scale, scale, scale); }
  51. // Scale the each component of the vector by the given scale factors.
  52. void Scale(float x_scale, float y_scale, float z_scale);
  53. // Take the cross product of this vector with |other| and become the result.
  54. void Cross(const Vector3dF& other);
  55. // |out| is assigned a unit-length vector in the direction of |this| iff
  56. // this function returns true. It can return false if |this| is too short.
  57. bool GetNormalized(Vector3dF* out) const;
  58. std::string ToString() const;
  59. private:
  60. float x_;
  61. float y_;
  62. float z_;
  63. };
  64. inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
  65. return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
  66. }
  67. inline bool operator!=(const Vector3dF& lhs, const Vector3dF& rhs) {
  68. return !(lhs == rhs);
  69. }
  70. inline Vector3dF operator-(const Vector3dF& v) {
  71. return Vector3dF(-v.x(), -v.y(), -v.z());
  72. }
  73. inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
  74. Vector3dF result = lhs;
  75. result.Add(rhs);
  76. return result;
  77. }
  78. inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
  79. Vector3dF result = lhs;
  80. result.Add(-rhs);
  81. return result;
  82. }
  83. // Return the cross product of two vectors.
  84. inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
  85. Vector3dF result = lhs;
  86. result.Cross(rhs);
  87. return result;
  88. }
  89. // Return the dot product of two vectors.
  90. GEOMETRY_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
  91. // Return a vector that is |v| scaled by the given scale factors along each
  92. // axis.
  93. GEOMETRY_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
  94. float x_scale,
  95. float y_scale,
  96. float z_scale);
  97. // Return a vector that is |v| scaled by the components of |s|
  98. inline Vector3dF ScaleVector3d(const Vector3dF& v, const Vector3dF& s) {
  99. return ScaleVector3d(v, s.x(), s.y(), s.z());
  100. }
  101. // Return a vector that is |v| scaled by the given scale factor.
  102. inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
  103. return ScaleVector3d(v, scale, scale, scale);
  104. }
  105. // Returns the angle between |base| and |other| in degrees.
  106. GEOMETRY_EXPORT float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
  107. const gfx::Vector3dF& other);
  108. // Returns the clockwise angle between |base| and |other| where |normal| is the
  109. // normal of the virtual surface to measure clockwise according to.
  110. GEOMETRY_EXPORT float ClockwiseAngleBetweenVectorsInDegrees(
  111. const gfx::Vector3dF& base,
  112. const gfx::Vector3dF& other,
  113. const gfx::Vector3dF& normal);
  114. // This is declared here for use in gtest-based unit tests but is defined in
  115. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  116. // test. This should not be used in production code - call ToString() instead.
  117. void PrintTo(const Vector3dF& vector, ::std::ostream* os);
  118. } // namespace gfx
  119. #endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_