SkPoint3.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkPoint3_DEFINED
  8. #define SkPoint3_DEFINED
  9. #include "include/core/SkPoint.h"
  10. struct SK_API SkPoint3 {
  11. SkScalar fX, fY, fZ;
  12. static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z) {
  13. SkPoint3 pt;
  14. pt.set(x, y, z);
  15. return pt;
  16. }
  17. SkScalar x() const { return fX; }
  18. SkScalar y() const { return fY; }
  19. SkScalar z() const { return fZ; }
  20. void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; }
  21. friend bool operator==(const SkPoint3& a, const SkPoint3& b) {
  22. return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ;
  23. }
  24. friend bool operator!=(const SkPoint3& a, const SkPoint3& b) {
  25. return !(a == b);
  26. }
  27. /** Returns the Euclidian distance from (0,0,0) to (x,y,z)
  28. */
  29. static SkScalar Length(SkScalar x, SkScalar y, SkScalar z);
  30. /** Return the Euclidian distance from (0,0,0) to the point
  31. */
  32. SkScalar length() const { return SkPoint3::Length(fX, fY, fZ); }
  33. /** Set the point (vector) to be unit-length in the same direction as it
  34. already points. If the point has a degenerate length (i.e., nearly 0)
  35. then set it to (0,0,0) and return false; otherwise return true.
  36. */
  37. bool normalize();
  38. /** Return a new point whose X, Y and Z coordinates are scaled.
  39. */
  40. SkPoint3 makeScale(SkScalar scale) const {
  41. SkPoint3 p;
  42. p.set(scale * fX, scale * fY, scale * fZ);
  43. return p;
  44. }
  45. /** Scale the point's coordinates by scale.
  46. */
  47. void scale(SkScalar value) {
  48. fX *= value;
  49. fY *= value;
  50. fZ *= value;
  51. }
  52. /** Return a new point whose X, Y and Z coordinates are the negative of the
  53. original point's
  54. */
  55. SkPoint3 operator-() const {
  56. SkPoint3 neg;
  57. neg.fX = -fX;
  58. neg.fY = -fY;
  59. neg.fZ = -fZ;
  60. return neg;
  61. }
  62. /** Returns a new point whose coordinates are the difference between
  63. a and b (i.e., a - b)
  64. */
  65. friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) {
  66. SkPoint3 v;
  67. v.set(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ);
  68. return v;
  69. }
  70. /** Returns a new point whose coordinates are the sum of a and b (a + b)
  71. */
  72. friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) {
  73. SkPoint3 v;
  74. v.set(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ);
  75. return v;
  76. }
  77. /** Add v's coordinates to the point's
  78. */
  79. void operator+=(const SkPoint3& v) {
  80. fX += v.fX;
  81. fY += v.fY;
  82. fZ += v.fZ;
  83. }
  84. /** Subtract v's coordinates from the point's
  85. */
  86. void operator-=(const SkPoint3& v) {
  87. fX -= v.fX;
  88. fY -= v.fY;
  89. fZ -= v.fZ;
  90. }
  91. /** Returns true if fX, fY, and fZ are measurable values.
  92. @return true for values other than infinities and NaN
  93. */
  94. bool isFinite() const {
  95. SkScalar accum = 0;
  96. accum *= fX;
  97. accum *= fY;
  98. accum *= fZ;
  99. // accum is either NaN or it is finite (zero).
  100. SkASSERT(0 == accum || SkScalarIsNaN(accum));
  101. // value==value will be true iff value is not NaN
  102. // TODO: is it faster to say !accum or accum==accum?
  103. return !SkScalarIsNaN(accum);
  104. }
  105. /** Returns the dot product of a and b, treating them as 3D vectors
  106. */
  107. static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) {
  108. return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
  109. }
  110. SkScalar dot(const SkPoint3& vec) const {
  111. return DotProduct(*this, vec);
  112. }
  113. /** Returns the cross product of a and b, treating them as 3D vectors
  114. */
  115. static SkPoint3 CrossProduct(const SkPoint3& a, const SkPoint3& b) {
  116. SkPoint3 result;
  117. result.fX = a.fY*b.fZ - a.fZ*b.fY;
  118. result.fY = a.fZ*b.fX - a.fX*b.fZ;
  119. result.fZ = a.fX*b.fY - a.fY*b.fX;
  120. return result;
  121. }
  122. SkPoint3 cross(const SkPoint3& vec) const {
  123. return CrossProduct(*this, vec);
  124. }
  125. };
  126. typedef SkPoint3 SkVector3;
  127. typedef SkPoint3 SkColor3f;
  128. #endif