OLD | NEW |
---|---|
(Empty) | |
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_POINT3_H_ | |
6 #define UI_GFX_POINT3_H_ | |
7 #pragma once | |
8 | |
9 #include <algorithm> | |
10 #include <cmath> | |
11 #include "ui/gfx/point.h" | |
sky
2011/06/16 23:06:56
newline between 10 and 11.
| |
12 | |
13 namespace gfx { | |
14 | |
15 // A point has an x, y and z coordinate. | |
16 template <typename T> | |
sky
2011/06/16 23:06:56
Do we really need a template here? Do we plan on u
| |
17 class Point3 { | |
18 public: | |
19 Point3(); | |
20 | |
21 Point3(T x, T y, T z) { | |
22 data_[0] = x; | |
sky
2011/06/16 23:06:56
Use member initializer list.
| |
23 data_[1] = y; | |
24 data_[2] = z; | |
25 } | |
26 | |
27 Point3(const Point3<T>& other) { | |
sky
2011/06/16 23:06:56
See style guide, we generally don't use copy const
| |
28 memcpy(data_, other.data_, sizeof(T)*3); | |
29 } | |
30 | |
31 Point3(const Point& other) { | |
32 data_[0] = other.x(); | |
33 data_[1] = other.y(); | |
34 data_[2] = 0; | |
35 } | |
36 | |
37 ~Point3() {} | |
38 | |
39 void swap(Point3<T>& other) { | |
40 std::swap(data_, other.data_); | |
41 } | |
42 | |
43 Point3<T>& operator=(Point3<T> other) { | |
44 swap(other); | |
45 return *this; | |
46 } | |
47 | |
48 const T& operator[] (int i) const { return data_[i]; } | |
49 T& operator[] (int i) { return data_[i]; } | |
50 | |
51 Point3<T> operator+ (const Point3<T>& other) { | |
52 return Point3<T>( | |
53 data_[0] + other[0], | |
54 data_[1] + other[1], | |
55 data_[2] + other[2]); | |
56 } | |
57 | |
58 Point3<T> operator- (const Point3<T>& other) { | |
59 return Point3<T>( | |
60 data_[0] - other[0], | |
61 data_[1] - other[1], | |
62 data_[2] - other[2]); | |
63 } | |
64 | |
65 Point3<T> operator* (const T& s) { | |
66 return Point3<T>( | |
67 data_[0] * s, | |
68 data_[1] * s, | |
69 data_[2] * s); | |
70 } | |
71 | |
72 T dist2(const Point3<T>& rhs) const { | |
73 return data_[0] * rhs[0] + | |
74 data_[1] * rhs[1] + | |
75 data_[2] * rhs[2]; | |
76 } | |
77 | |
78 operator Point() { | |
79 return Point( | |
80 static_cast<int>(std::floor(data_[0])), | |
81 static_cast<int>(std::floor(data_[1]))); | |
82 } | |
83 | |
84 private: | |
85 T data_[3]; | |
86 }; | |
87 | |
88 // Could point this at Skia and we'd be ok. | |
89 typedef Point3<float> Point3f; | |
90 typedef Point3<double> Point3d; | |
91 | |
92 } // namespace gfx | |
93 | |
94 #endif // UI_GFX_POINT3_H_ | |
OLD | NEW |