1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef POINT3_H
17 #define POINT3_H
18
19 #include <string>
20 #include "utils/drawing_macros.h"
21 #include "utils/scalar.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 class DRAWING_API Point3 {
27 public:
28 inline Point3() noexcept;
29 inline Point3(const Point3& p) noexcept;
30 inline Point3(scalar x, scalar y, scalar z) noexcept;
31
~Point3()32 inline ~Point3() {}
33
34 inline scalar GetX() const;
35 inline scalar GetY() const;
36 inline scalar GetZ() const;
37
38 inline void SetX(scalar x);
39 inline void SetY(scalar y);
40 inline void SetZ(scalar z);
41
42 inline Point3& operator+=(const Point3& p);
43 inline Point3& operator-=(const Point3& p);
44 inline Point3& operator*=(scalar scale);
45 inline Point3& operator/=(scalar divisor);
46
47 friend inline const Point3 operator+(const Point3& p1, const Point3& p2);
48 friend inline const Point3 operator-(const Point3& p1, const Point3& p2);
49 friend inline const Point3 operator*(scalar scale, const Point3& p);
50 friend inline const Point3 operator*(const Point3& p, scalar scale);
51 friend inline const Point3 operator/(const Point3& p, scalar divisor);
52 friend inline const Point3 operator+(const Point3& p);
53 friend inline const Point3 operator-(const Point3& p);
54 friend inline bool operator==(const Point3& p1, const Point3& p2);
55 friend inline bool operator!=(const Point3& p1, const Point3& p2);
56
57 inline void Dump(std::string& out) const;
58
59 private:
60 scalar x_;
61 scalar y_;
62 scalar z_;
63 };
64
Point3()65 inline Point3::Point3() noexcept : x_(0.0), y_(0.0), z_(0.0) {}
66
Point3(const Point3 & p)67 inline Point3::Point3(const Point3& p) noexcept
68 {
69 // Tell the compiler there is no alias and to select wider load/store instructions.
70 scalar x = p.GetX();
71 scalar y = p.GetY();
72 scalar z = p.GetZ();
73 x_ = x;
74 y_ = y;
75 z_ = z;
76 }
77
Point3(scalar x,scalar y,scalar z)78 inline Point3::Point3(scalar x, scalar y, scalar z) noexcept : x_(x), y_(y), z_(z) {}
79
GetX()80 inline scalar Point3::GetX() const
81 {
82 return x_;
83 }
84
GetY()85 inline scalar Point3::GetY() const
86 {
87 return y_;
88 }
89
GetZ()90 inline scalar Point3::GetZ() const
91 {
92 return z_;
93 }
94
SetX(scalar x)95 inline void Point3::SetX(scalar x)
96 {
97 x_ = x;
98 }
99
SetY(scalar y)100 inline void Point3::SetY(scalar y)
101 {
102 y_ = y;
103 }
104
SetZ(scalar z)105 inline void Point3::SetZ(scalar z)
106 {
107 z_ = z;
108 }
109
110 inline Point3& Point3::operator+=(const Point3& p)
111 {
112 x_ += p.x_;
113 y_ += p.y_;
114 z_ += p.z_;
115 return *this;
116 }
117
118 inline Point3& Point3::operator-=(const Point3& p)
119 {
120 x_ -= p.x_;
121 y_ -= p.y_;
122 z_ -= p.z_;
123 return *this;
124 }
125
126 inline Point3& Point3::operator*=(scalar scale)
127 {
128 x_ = static_cast<int64_t>(x_ * scale);
129 y_ = static_cast<int64_t>(y_ * scale);
130 z_ = static_cast<int64_t>(z_ * scale);
131 return *this;
132 }
133
134 inline Point3& Point3::operator/=(scalar divisor)
135 {
136 if (divisor == 0) {
137 return *this;
138 }
139 x_ = static_cast<int>(x_ / divisor);
140 y_ = static_cast<int>(y_ / divisor);
141 z_ = static_cast<int>(z_ / divisor);
142 return *this;
143 }
144
145 inline const Point3 operator+(const Point3& p1, const Point3& p2)
146 {
147 return Point3(p1.x_ + p1.y_, p2.x_ + p2.y_, p1.z_ + p2.z_);
148 }
149
150 inline const Point3 operator-(const Point3& p1, const Point3& p2)
151 {
152 return Point3(p1.x_ - p2.x_, p1.y_ - p2.y_, p1.z_ - p2.z_);
153 }
154
155 inline const Point3 operator*(scalar scale, const Point3& p)
156 {
157 return Point3(
158 static_cast<int64_t>(scale * p.x_), static_cast<int64_t>(scale * p.y_), static_cast<int64_t>(scale * p.z_));
159 }
160
161 inline const Point3 operator*(const Point3& p, scalar scale)
162 {
163 return Point3(
164 static_cast<int64_t>(p.x_ * scale), static_cast<int64_t>(p.y_ * scale), static_cast<int64_t>(p.z_ * scale));
165 }
166
167 inline const Point3 operator/(const Point3& p, scalar divisor)
168 {
169 if (divisor == 0) {
170 return Point3(p.x_, p.y_, p.z_);
171 }
172 return Point3(p.x_ / divisor, p.y_ / divisor, p.z_ / divisor);
173 }
174
175 inline const Point3 operator+(const Point3& p)
176 {
177 return Point3(p.x_, p.y_, p.z_);
178 }
179
180 inline const Point3 operator-(const Point3& p)
181 {
182 return Point3(-p.x_, -p.y_, -p.z_);
183 }
184
185 inline bool operator==(const Point3& p1, const Point3& p2)
186 {
187 return IsScalarAlmostEqual(p1.x_, p2.x_) && IsScalarAlmostEqual(p1.y_, p2.y_) && IsScalarAlmostEqual(p1.z_, p2.z_);
188 }
189
190 inline bool operator!=(const Point3& p1, const Point3& p2)
191 {
192 return !IsScalarAlmostEqual(p1.x_, p2.x_) || !IsScalarAlmostEqual(p1.y_, p2.y_) ||
193 !IsScalarAlmostEqual(p1.z_, p2.z_);
194 }
195
Dump(std::string & out)196 inline void Point3::Dump(std::string& out) const
197 {
198 out += "[";
199 out += "x:" + std::to_string(x_);
200 out += " y:" + std::to_string(y_);
201 out += " z:" + std::to_string(z_);
202 out += "]";
203 }
204 } // namespace Drawing
205 } // namespace Rosen
206 } // namespace OHOS
207 #endif