• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef Vector3_h
30 #define Vector3_h
31 
32 #include <math.h>
33 
34 namespace WebCore {
35 
36 class Vector3 {
37 public:
Vector3()38     Vector3()
39         : m_x(0.0)
40         , m_y(0.0)
41         , m_z(0.0)
42     {
43     }
44 
Vector3(double x,double y,double z)45     Vector3(double x, double y, double z)
46         : m_x(x)
47         , m_y(y)
48         , m_z(z)
49     {
50     }
51 
Vector3(const float p[3])52     Vector3(const float p[3])
53         : m_x(p[0])
54         , m_y(p[1])
55         , m_z(p[2])
56     {
57     }
58 
Vector3(const double p[3])59     Vector3(const double p[3])
60         : m_x(p[0])
61         , m_y(p[1])
62         , m_z(p[2])
63     {
64     }
65 
abs()66     double abs() const
67     {
68         return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
69     }
70 
isZero()71     bool isZero() const
72     {
73         return !m_x && !m_y && !m_z;
74     }
75 
normalize()76     void normalize()
77     {
78         double absValue = abs();
79         if (!absValue)
80             return;
81 
82         double k = 1.0 / absValue;
83         m_x *= k;
84         m_y *= k;
85         m_z *= k;
86     }
87 
x()88     double x() const { return m_x; }
y()89     double y() const { return m_y; }
z()90     double z() const { return m_z; }
91 
92 private:
93     double m_x;
94     double m_y;
95     double m_z;
96 };
97 
98 inline Vector3 operator+(const Vector3& v1, const Vector3& v2)
99 {
100     return Vector3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
101 }
102 
103 inline Vector3 operator-(const Vector3& v1, const Vector3& v2)
104 {
105     return Vector3(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
106 }
107 
108 inline Vector3 operator*(double k, const Vector3& v)
109 {
110     return Vector3(k * v.x(), k * v.y(), k * v.z());
111 }
112 
113 inline Vector3 operator*(const Vector3& v, double k)
114 {
115     return Vector3(k * v.x(), k * v.y(), k * v.z());
116 }
117 
dot(const Vector3 & v1,const Vector3 & v2)118 inline double dot(const Vector3& v1, const Vector3& v2)
119 {
120     return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
121 }
122 
cross(const Vector3 & v1,const Vector3 & v2)123 inline Vector3 cross(const Vector3& v1, const Vector3& v2)
124 {
125     double x3 = v1.y() * v2.z() - v1.z() * v2.y();
126     double y3 = v1.z() * v2.x() - v1.x() * v2.z();
127     double z3 = v1.x() * v2.y() - v1.y() * v2.x();
128     return Vector3(x3, y3, z3);
129 }
130 
distance(const Vector3 & v1,const Vector3 & v2)131 inline double distance(const Vector3& v1, const Vector3& v2)
132 {
133     return (v1 - v2).abs();
134 }
135 
136 } // WebCore
137 
138 #endif // Vector3_h
139