PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Math/IPoint3.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
IPoint3.hpp
1
2#ifndef GYM_PCB_MATH_IPOINT3_H
3#define GYM_PCB_MATH_IPOINT3_H
4
5#include "Math/IPoint2.hpp"
6
7 class IVector_3
8{
9public:
10 int32_t x, y, z;
11public:
12 IVector_3() { }
13 IVector_3(int X, int Y, int Z) : x(X), y(Y), z(Z) { }
14 IVector_3(const IVector_2 &v, int Z) : x(v.x), y(v.y), z(Z) { }
15 IVector_3(const IVector_3 &v) : x(v.x), y(v.y), z(v.z) { }
16 IVector_3& operator=(const IVector_3 &v) { x = v.x; y = v.y; z = v.z; return *this; }
17 bool operator==(const IVector_3 &v) const { return x == v.x && y == v.y && z == v.z; }
18 bool operator!=(const IVector_3 &v) const { return x != v.x || y != v.y || z != v.z; }
19 IVector_3 operator+(const IVector_3 &v) const { return IVector_3(x + v.x, y + v.y, z + v.z); }
20 IVector_3 operator-(const IVector_3 &v) const { return IVector_3(x - v.x, y - v.y, z - v.z); }
21 IVector_3 operator*(int s) const { return IVector_3(x * s, y * s, z * s); }
22 IVector_3 operator/(int s) const { return IVector_3((x + s - 1) / s, (y + s - 1) / s, (z + s - 1) / s); }
23 IVector_3& operator+=(const IVector_3 &v) { x += v.x; y += v.y; z += v.z; return *this; }
24 IVector_3& operator-=(const IVector_3 &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
25 IVector_3& operator*=(int s) { *this = *this * s; return *this; }
26 IVector_3& operator/=(int s) { *this = *this / s; return *this; }
27 IVector_3 abs() const { return IVector_3(std::abs(x), std::abs(y), std::abs(z)); }
28 int32_t dot(const IVector_3 &v) const { return x * v.x + y * v.y + z * v.z; }
29 int32_t norm1() const { return std::abs(x) + std::abs(y) + std::abs(z); }
30 int32_t hprod() const { return x * y * z; }
31 IVector_3 max_cw(const IVector_3 &v) const { return IVector_3(std::max(x, v.x), std::max(y, v.y), std::max(z, v.z)); }
32 IVector_3 min_cw(const IVector_3 &v) const { return IVector_3(std::min(x, v.x), std::min(y, v.y), std::min(z, v.z)); }
33};
34
35 class IPoint_3
36{
37public:
38 int32_t x, y, z;
39public:
40 IPoint_3() { }
41 constexpr IPoint_3(int X, int Y, int Z) : x(X), y(Y), z(Z) { }
42 IPoint_3(const IVector_3 &v) : x(v.x), y(v.y), z(v.z) { }
43 IPoint_3(const int *v) : x(v[0]), y(v[1]), z(v[2]) { }
44 IPoint_3(const uint *v) : x(v[0]), y(v[1]), z(v[2]) { }
45 IPoint_3& operator=(const IPoint_3 &P) { x = P.x; y = P.y; z = P.z; return *this; }
46 bool operator==(const IPoint_3 &P) const { return x == P.x && y == P.y && z == P.z; }
47 bool operator!=(const IPoint_3 &P) const { return x != P.x || y != P.y || z != P.z; }
48 bool isPositive() const { return x >= 0 && y >= 0 && z >= 0; }
49 uint index(const IVector_3 &size) const { assert(isPositive()); return z * size.z + y * size.y + x * size.x; }
50 IPoint_2 xy() const { return IPoint_2(x, y); }
51 IPoint_3 operator+(const IVector_3 &v) const { return IPoint_3(x + v.x, y + v.y, z + v.z); }
52 IPoint_3 operator-(const IVector_3 &v) const { return IPoint_3(x - v.x, y - v.y, z - v.z); }
53 IVector_3 operator-(const IPoint_3 &P) const { return IVector_3(x - P.x, y - P.y, z - P.z); }
54 IPoint_3& operator+=(const IVector_3 &v) { x += v.x; y += v.y; z += v.z; return *this; }
55 IPoint_3& operator-=(const IVector_3 &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
56 IVector_3 vector() const { return IVector_3(x, y, z); }
57};
58
59 struct IBox_3
60{
61 IBox_3() { }
62 constexpr IBox_3(const IPoint_3 &mn, const IPoint_3 &mx) : min(mn), max(mx) { }
63 IPoint_3 min;
64 IPoint_3 max;
65 bool nonzero() const { return min.x <= max.x && min.y <= max.y && min.z <= max.z; }
66 uint w() const { return max.x - min.x + 1; }
67 uint h() const { return max.y - min.y + 1; }
68 uint d() const { return max.z - min.z + 1; }
69 int64_t volume() const { return w() * h() * d(); }
70 constexpr static const IBox_3 EMPTY() { return IBox_3(IPoint_3(1,1,1), IPoint_3(0,0,0)); }
71};
72
73inline std::ostream& operator<<(std::ostream &os, const IVector_3 &V)
74{
75 return os << '(' << V.x << ',' << V.y << ',' << V.z << ')';
76}
77inline std::ostream& operator<<(std::ostream &os, const IPoint_3 &P)
78{
79 return os << '(' << P.x << ',' << P.y << ',' << P.z << ')';
80}
81inline std::ostream& operator<<(std::ostream &os, const IBox_3 &B)
82{
83 return os << '[' << B.min.x << ',' << B.min.y << ',' << B.min.z << "][" << B.max.x << ',' << B.max.y << ',' << B.max.z << ']';
84}
85
86#endif // GYM_PCB_MATH_IPOINT3_H
IPoint_2
Definition IPoint2.hpp:37
IPoint_3
Definition IPoint3.hpp:36
IVector_2
Definition IPoint2.hpp:10
IVector_3
Definition IPoint3.hpp:8
IBox_3
Definition IPoint3.hpp:60