]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Vector3.h
3c40b6111925a3ff2b8a776ddc867e65c82051f0
2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
45 Vector3(double x
, double y
, double z
)
52 Vector3(const float p
[3])
59 Vector3(const double p
[3])
68 return sqrt(m_x
* m_x
+ m_y
* m_y
+ m_z
* m_z
);
73 return !m_x
&& !m_y
&& !m_z
;
78 double absValue
= abs();
82 double k
= 1.0 / absValue
;
88 double x() const { return m_x
; }
89 double y() const { return m_y
; }
90 double z() const { return m_z
; }
98 inline Vector3
operator+(const Vector3
& v1
, const Vector3
& v2
)
100 return Vector3(v1
.x() + v2
.x(), v1
.y() + v2
.y(), v1
.z() + v2
.z());
103 inline Vector3
operator-(const Vector3
& v1
, const Vector3
& v2
)
105 return Vector3(v1
.x() - v2
.x(), v1
.y() - v2
.y(), v1
.z() - v2
.z());
108 inline Vector3
operator*(double k
, const Vector3
& v
)
110 return Vector3(k
* v
.x(), k
* v
.y(), k
* v
.z());
113 inline Vector3
operator*(const Vector3
& v
, double k
)
115 return Vector3(k
* v
.x(), k
* v
.y(), k
* v
.z());
118 inline double dot(const Vector3
& v1
, const Vector3
& v2
)
120 return v1
.x() * v2
.x() + v1
.y() * v2
.y() + v1
.z() * v2
.z();
123 inline Vector3
cross(const Vector3
& v1
, const Vector3
& v2
)
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
);
131 inline double distance(const Vector3
& v1
, const Vector3
& v2
)
133 return (v1
- v2
).abs();