Home | History | Annotate | Download | only in geometry
      1 // Copyright (c) 2011 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 
      5 #ifndef UI_GFX_GEOMETRY_POINT3_F_H_
      6 #define UI_GFX_GEOMETRY_POINT3_F_H_
      7 
      8 #include <iosfwd>
      9 #include <string>
     10 
     11 #include "ui/gfx/geometry/point_f.h"
     12 #include "ui/gfx/geometry/vector3d_f.h"
     13 #include "ui/gfx/gfx_export.h"
     14 
     15 namespace gfx {
     16 
     17 // A point has an x, y and z coordinate.
     18 class GFX_EXPORT Point3F {
     19  public:
     20   Point3F() : x_(0), y_(0), z_(0) {}
     21 
     22   Point3F(float x, float y, float z) : x_(x), y_(y), z_(z) {}
     23 
     24   explicit Point3F(const PointF& point) : x_(point.x()), y_(point.y()), z_(0) {}
     25 
     26   ~Point3F() {}
     27 
     28   void Scale(float scale) {
     29     Scale(scale, scale, scale);
     30   }
     31 
     32   void Scale(float x_scale, float y_scale, float z_scale) {
     33     SetPoint(x() * x_scale, y() * y_scale, z() * z_scale);
     34   }
     35 
     36   float x() const { return x_; }
     37   float y() const { return y_; }
     38   float z() const { return z_; }
     39 
     40   void set_x(float x) { x_ = x; }
     41   void set_y(float y) { y_ = y; }
     42   void set_z(float z) { z_ = z; }
     43 
     44   void SetPoint(float x, float y, float z) {
     45     x_ = x;
     46     y_ = y;
     47     z_ = z;
     48   }
     49 
     50   // Offset the point by the given vector.
     51   void operator+=(const Vector3dF& v) {
     52     x_ += v.x();
     53     y_ += v.y();
     54     z_ += v.z();
     55   }
     56 
     57   // Offset the point by the given vector's inverse.
     58   void operator-=(const Vector3dF& v) {
     59     x_ -= v.x();
     60     y_ -= v.y();
     61     z_ -= v.z();
     62   }
     63 
     64   // Returns the squared euclidean distance between two points.
     65   float SquaredDistanceTo(const Point3F& other) const {
     66     float dx = x_ - other.x_;
     67     float dy = y_ - other.y_;
     68     float dz = z_ - other.z_;
     69     return dx * dx + dy * dy + dz * dz;
     70   }
     71 
     72   PointF AsPointF() const { return PointF(x_, y_); }
     73 
     74   // Returns a string representation of 3d point.
     75   std::string ToString() const;
     76 
     77  private:
     78   float x_;
     79   float y_;
     80   float z_;
     81 
     82   // copy/assign are allowed.
     83 };
     84 
     85 inline bool operator==(const Point3F& lhs, const Point3F& rhs) {
     86   return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
     87 }
     88 
     89 inline bool operator!=(const Point3F& lhs, const Point3F& rhs) {
     90   return !(lhs == rhs);
     91 }
     92 
     93 // Add a vector to a point, producing a new point offset by the vector.
     94 GFX_EXPORT Point3F operator+(const Point3F& lhs, const Vector3dF& rhs);
     95 
     96 // Subtract a vector from a point, producing a new point offset by the vector's
     97 // inverse.
     98 GFX_EXPORT Point3F operator-(const Point3F& lhs, const Vector3dF& rhs);
     99 
    100 // Subtract one point from another, producing a vector that represents the
    101 // distances between the two points along each axis.
    102 GFX_EXPORT Vector3dF operator-(const Point3F& lhs, const Point3F& rhs);
    103 
    104 inline Point3F PointAtOffsetFromOrigin(const Vector3dF& offset) {
    105   return Point3F(offset.x(), offset.y(), offset.z());
    106 }
    107 
    108 inline Point3F ScalePoint(const Point3F& p,
    109                           float x_scale,
    110                           float y_scale,
    111                           float z_scale) {
    112   return Point3F(p.x() * x_scale, p.y() * y_scale, p.z() * z_scale);
    113 }
    114 
    115 inline Point3F ScalePoint(const Point3F& p, float scale) {
    116   return ScalePoint(p, scale, scale, scale);
    117 }
    118 
    119 // This is declared here for use in gtest-based unit tests but is defined in
    120 // the gfx_test_support target. Depend on that to use this in your unit test.
    121 // This should not be used in production code - call ToString() instead.
    122 void PrintTo(const Point3F& point, ::std::ostream* os);
    123 
    124 }  // namespace gfx
    125 
    126 #endif  // UI_GFX_GEOMETRY_POINT3_F_H_
    127