15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Defines a simple float vector class. This class is used to indicate a 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// distance in two dimensions between two points. Subtracting two points should 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// produce a vector, and adding a vector to a point produces the point at the 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// vector's distance from the original point. 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef UI_GFX_GEOMETRY_VECTOR3D_F_H_ 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_GFX_GEOMETRY_VECTOR3D_F_H_ 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <iosfwd> 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string> 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/geometry/vector2d_f.h" 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/gfx_export.h" 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace gfx { 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class GFX_EXPORT Vector3dF { 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector3dF(); 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector3dF(float x, float y, float z); 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) explicit Vector3dF(const Vector2dF& other); 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x() const { return x_; } 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_x(float x) { x_ = x; } 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y() const { return y_; } 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_y(float y) { y_ = y; } 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float z() const { return z_; } 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_z(float z) { z_ = z; } 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // True if all components of the vector are 0. 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool IsZero() const; 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Add the components of the |other| vector to the current vector. 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Add(const Vector3dF& other); 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Subtract the components of the |other| vector from the current vector. 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Subtract(const Vector3dF& other); 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator+=(const Vector3dF& other) { Add(other); } 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator-=(const Vector3dF& other) { Subtract(other); } 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMin(const Vector3dF& other) { 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ <= other.x_ ? x_ : other.x_; 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ <= other.y_ ? y_ : other.y_; 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) z_ = z_ <= other.z_ ? z_ : other.z_; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMax(const Vector3dF& other) { 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ >= other.x_ ? x_ : other.x_; 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ >= other.y_ ? y_ : other.y_; 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) z_ = z_ >= other.z_ ? z_ : other.z_; 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the square of the diagonal length of the vector. 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) double LengthSquared() const; 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the diagonal length of the vector. 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float Length() const; 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Scale all components of the vector by |scale|. 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Scale(float scale) { Scale(scale, scale, scale); } 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Scale the each component of the vector by the given scale factors. 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Scale(float x_scale, float y_scale, float z_scale); 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Take the cross product of this vector with |other| and become the result. 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Cross(const Vector3dF& other); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string ToString() const; 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x_; 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y_; 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float z_; 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) { 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z(); 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector3dF operator-(const Vector3dF& v) { 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return Vector3dF(-v.x(), -v.y(), -v.z()); 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) { 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector3dF result = lhs; 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(rhs); 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) { 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector3dF result = lhs; 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(-rhs); 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return the cross product of two vectors. 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) { 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector3dF result = lhs; 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Cross(rhs); 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return the dot product of two vectors. 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GFX_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs); 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return a vector that is |v| scaled by the given scale factors along each 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// axis. 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GFX_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v, 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x_scale, 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y_scale, 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float z_scale); 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return a vector that is |v| scaled by the given scale factor. 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) { 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ScaleVector3d(v, scale, scale, scale); 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This is declared here for use in gtest-based unit tests but is defined in 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the gfx_test_support target. Depend on that to use this in your unit test. 125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This should not be used in production code - call ToString() instead. 126116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PrintTo(const Vector3dF& vector, ::std::ostream* os); 127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace gfx 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_ 131