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 5 #include "ui/gfx/geometry/vector3d_f.h" 6 7 #include <cmath> 8 9 #include "base/strings/stringprintf.h" 10 11 namespace gfx { 12 13 Vector3dF::Vector3dF() 14 : x_(0), 15 y_(0), 16 z_(0) { 17 } 18 19 Vector3dF::Vector3dF(float x, float y, float z) 20 : x_(x), 21 y_(y), 22 z_(z) { 23 } 24 25 Vector3dF::Vector3dF(const Vector2dF& other) 26 : x_(other.x()), 27 y_(other.y()), 28 z_(0) { 29 } 30 31 std::string Vector3dF::ToString() const { 32 return base::StringPrintf("[%f %f %f]", x_, y_, z_); 33 } 34 35 bool Vector3dF::IsZero() const { 36 return x_ == 0 && y_ == 0 && z_ == 0; 37 } 38 39 void Vector3dF::Add(const Vector3dF& other) { 40 x_ += other.x_; 41 y_ += other.y_; 42 z_ += other.z_; 43 } 44 45 void Vector3dF::Subtract(const Vector3dF& other) { 46 x_ -= other.x_; 47 y_ -= other.y_; 48 z_ -= other.z_; 49 } 50 51 double Vector3dF::LengthSquared() const { 52 return static_cast<double>(x_) * x_ + static_cast<double>(y_) * y_ + 53 static_cast<double>(z_) * z_; 54 } 55 56 float Vector3dF::Length() const { 57 return static_cast<float>(std::sqrt(LengthSquared())); 58 } 59 60 void Vector3dF::Scale(float x_scale, float y_scale, float z_scale) { 61 x_ *= x_scale; 62 y_ *= y_scale; 63 z_ *= z_scale; 64 } 65 66 void Vector3dF::Cross(const Vector3dF& other) { 67 float x = y_ * other.z() - z_ * other.y(); 68 float y = z_ * other.x() - x_ * other.z(); 69 float z = x_ * other.y() - y_ * other.x(); 70 x_ = x; 71 y_ = y; 72 z_ = z; 73 } 74 75 float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs) { 76 return lhs.x() * rhs.x() + lhs.y() * rhs.y() + lhs.z() * rhs.z(); 77 } 78 79 Vector3dF ScaleVector3d(const Vector3dF& v, 80 float x_scale, 81 float y_scale, 82 float z_scale) { 83 Vector3dF scaled_v(v); 84 scaled_v.Scale(x_scale, y_scale, z_scale); 85 return scaled_v; 86 } 87 88 } // namespace gfx 89