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 "utils/drawing_macros.h"
20 #include "utils/scalar.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 class DRAWING_API Point3 {
26 public:
27     inline Point3() noexcept;
28     inline Point3(const Point3& p) noexcept;
29     inline Point3(scalar x, scalar y, scalar z) noexcept;
30 
~Point3()31     inline ~Point3() {}
32 
33     inline scalar GetX() const;
34     inline scalar GetY() const;
35     inline scalar GetZ() const;
36 
37     inline void SetX(scalar x);
38     inline void SetY(scalar y);
39     inline void SetZ(scalar z);
40 
41     inline Point3& operator+=(const Point3& p);
42     inline Point3& operator-=(const Point3& p);
43     inline Point3& operator*=(scalar scale);
44     inline Point3& operator/=(scalar divisor);
45 
46     friend inline const Point3 operator+(const Point3& p1, const Point3& p2);
47     friend inline const Point3 operator-(const Point3& p1, const Point3& p2);
48     friend inline const Point3 operator*(scalar scale, const Point3& p);
49     friend inline const Point3 operator*(const Point3& p, scalar scale);
50     friend inline const Point3 operator/(const Point3& p, scalar divisor);
51     friend inline const Point3 operator+(const Point3& p);
52     friend inline const Point3 operator-(const Point3& p);
53     friend inline bool operator==(const Point3& p1, const Point3& p2);
54     friend inline bool operator!=(const Point3& p1, const Point3& p2);
55 
56 private:
57     scalar x_;
58     scalar y_;
59     scalar z_;
60 };
61 
Point3()62 inline Point3::Point3() noexcept : x_(0.0), y_(0.0), z_(0.0) {}
63 
Point3(const Point3 & p)64 inline Point3::Point3(const Point3& p) noexcept : x_(p.GetX()), y_(p.GetY()), z_(p.GetZ()) {}
65 
Point3(scalar x,scalar y,scalar z)66 inline Point3::Point3(scalar x, scalar y, scalar z) noexcept : x_(x), y_(y), z_(z) {}
67 
GetX()68 inline scalar Point3::GetX() const
69 {
70     return x_;
71 }
72 
GetY()73 inline scalar Point3::GetY() const
74 {
75     return y_;
76 }
77 
GetZ()78 inline scalar Point3::GetZ() const
79 {
80     return z_;
81 }
82 
SetX(scalar x)83 inline void Point3::SetX(scalar x)
84 {
85     x_ = x;
86 }
87 
SetY(scalar y)88 inline void Point3::SetY(scalar y)
89 {
90     y_ = y;
91 }
92 
SetZ(scalar z)93 inline void Point3::SetZ(scalar z)
94 {
95     z_ = z;
96 }
97 
98 inline Point3& Point3::operator+=(const Point3& p)
99 {
100     x_ += p.x_;
101     y_ += p.y_;
102     z_ += p.z_;
103     return *this;
104 }
105 
106 inline Point3& Point3::operator-=(const Point3& p)
107 {
108     x_ -= p.x_;
109     y_ -= p.y_;
110     z_ -= p.z_;
111     return *this;
112 }
113 
114 inline Point3& Point3::operator*=(scalar scale)
115 {
116     x_ = static_cast<int64_t>(x_ * scale);
117     y_ = static_cast<int64_t>(y_ * scale);
118     z_ = static_cast<int64_t>(z_ * scale);
119     return *this;
120 }
121 
122 inline Point3& Point3::operator/=(scalar divisor)
123 {
124     if (divisor == 0) {
125         return *this;
126     }
127     x_ = static_cast<int>(x_ / divisor);
128     y_ = static_cast<int>(y_ / divisor);
129     z_ = static_cast<int>(z_ / divisor);
130     return *this;
131 }
132 
133 inline const Point3 operator+(const Point3& p1, const Point3& p2)
134 {
135     return Point3(p1.x_ + p1.y_, p2.x_ + p2.y_, p1.z_ + p2.z_);
136 }
137 
138 inline const Point3 operator-(const Point3& p1, const Point3& p2)
139 {
140     return Point3(p1.x_ - p2.x_, p1.y_ - p2.y_, p1.z_ - p2.z_);
141 }
142 
143 inline const Point3 operator*(scalar scale, const Point3& p)
144 {
145     return Point3(
146         static_cast<int64_t>(scale * p.x_), static_cast<int64_t>(scale * p.y_), static_cast<int64_t>(scale * p.z_));
147 }
148 
149 inline const Point3 operator*(const Point3& p, scalar scale)
150 {
151     return Point3(
152         static_cast<int64_t>(p.x_ * scale), static_cast<int64_t>(p.y_ * scale), static_cast<int64_t>(p.z_ * scale));
153 }
154 
155 inline const Point3 operator/(const Point3& p, scalar divisor)
156 {
157     if (divisor == 0) {
158         return Point3(p.x_, p.y_, p.z_);
159     }
160     return Point3(p.x_ / divisor, p.y_ / divisor, p.z_ / divisor);
161 }
162 
163 inline const Point3 operator+(const Point3& p)
164 {
165     return Point3(p.x_, p.y_, p.z_);
166 }
167 
168 inline const Point3 operator-(const Point3& p)
169 {
170     return Point3(-p.x_, -p.y_, -p.z_);
171 }
172 
173 inline bool operator==(const Point3& p1, const Point3& p2)
174 {
175     return IsScalarAlmostEqual(p1.x_, p2.x_) && IsScalarAlmostEqual(p1.y_, p2.y_) && IsScalarAlmostEqual(p1.z_, p2.z_);
176 }
177 
178 inline bool operator!=(const Point3& p1, const Point3& p2)
179 {
180     return !IsScalarAlmostEqual(p1.x_, p2.x_) || !IsScalarAlmostEqual(p1.y_, p2.y_) ||
181         !IsScalarAlmostEqual(p1.z_, p2.z_);
182 }
183 } // namespace Drawing
184 } // namespace Rosen
185 } // namespace OHOS
186 #endif