-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint3D.h
103 lines (97 loc) · 4.38 KB
/
Point3D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef Point3D_H
#define Point3D_H
#include <QPoint>
#include <cmath>
class Point3D
{
public:
typedef long double dataType ;
private:
dataType xCoord, yCoord, zCoord;
public:
Point3D(dataType x = 0, dataType y = 0, dataType z = 0) {xCoord = x; yCoord = y; zCoord = z;}
Point3D(const Point3D &newPoint) {xCoord = newPoint.x(); yCoord = newPoint.y(); zCoord = newPoint.z();}
bool isNull() const {return ((xCoord == 0) && (yCoord == 0) && (zCoord == 0));}
dataType manhattanLength() const {return fabsl(xCoord) + fabsl(yCoord) + fabsl(zCoord);}
dataType length() const {return sqrtl(magSqr()); }
dataType mag() const {return length();}
dataType magSqr() const
{// if one of the coordinates is 0, don't calculate that distance
if (zCoord == 0) return fabsl(xCoord*xCoord + yCoord*yCoord);
if (yCoord == 0) return fabsl(xCoord*xCoord + zCoord*zCoord);
if (xCoord == 0) return fabsl(yCoord*yCoord + zCoord*zCoord);
return xCoord*xCoord + yCoord*yCoord + zCoord*zCoord;
}
void setX(dataType x) {xCoord = x;}
void setY(dataType y) {yCoord = y;}
void setZ(dataType z) {zCoord = z;}
void setXY(dataType x, dataType y) {xCoord = x; yCoord = y;}
void setXZ(dataType x, dataType z) {xCoord = x; zCoord = z;}
void setYZ(dataType y, dataType z) {yCoord = y; zCoord = z;}
void setXYZ(dataType x, dataType y, dataType z) {xCoord = x; yCoord = y; zCoord = z;}
void setVar(int var, dataType value) {if (var == 0) xCoord = value; else if (var == 1) yCoord = value; else zCoord = value;}
QPoint toPoint() const {return QPoint(xCoord, yCoord);}
QPointF toPointF() const {return QPointF(xCoord, yCoord);}
dataType x() const {return xCoord;}
dataType y() const {return yCoord;}
dataType z() const {return zCoord;}
dataType var(int index) const
{
if (index == 0) return xCoord;
if (index == 1) return yCoord;
if (index == 2) return zCoord;
return -0x123456;
}
QString toString(bool isHorizontal = true) const;
Point3D round(int digits) const;
Point3D operator+= (const Point3D & point)
{
xCoord += point.x();
yCoord += point.y();
zCoord += point.z();
return *this ;
}
Point3D operator-= (const Point3D & point)
{
xCoord -= point.x();
yCoord -= point.y();
zCoord -= point.z();
return *this;
}
Point3D operator/= (const dataType divisor)
{
xCoord /= divisor;
yCoord /= divisor;
zCoord /= divisor;
return *this;
}
Point3D operator*= (const dataType factor)
{
xCoord *= factor;
yCoord *= factor;
zCoord *= factor;
return *this;
}
const Point3D operator=(const Point3D p2) {xCoord = p2.x(); yCoord = p2.y(); zCoord = p2.z(); return *this;}
bool operator!=(const Point3D p2) const {return ((xCoord != p2.x()) || (yCoord != p2.y()) || (zCoord != p2.z()));}
bool operator==(const Point3D p2) const {return ((xCoord == p2.x()) && (yCoord == p2.y()) && (zCoord == p2.z()));}
const Point3D normal() const {dataType len = length(); return Point3D(xCoord/len, yCoord/len, zCoord/len);}
const Point3D norm() const {return normal();}
};
const Point3D operator*(const Point3D & p, Point3D::dataType factor);
const Point3D operator*(Point3D::dataType factor, const Point3D & p);
const Point3D operator+(const Point3D & p1, const Point3D & p2);
const Point3D operator-(const Point3D & p1, const Point3D & p2);
const Point3D operator/(const Point3D & p, Point3D::dataType divisor);
QVector<Point3D> add(QVector<Point3D> vect1, QVector<Point3D> vect2);
QVector<Point3D> sub(QVector<Point3D> vect1, QVector<Point3D> vect2);
QVector<Point3D> operator*(QVector<Point3D> vect, Point3D::dataType factor);
QVector<Point3D> operator*(Point3D::dataType factor, QVector<Point3D> vect);
QVector<Point3D> operator/(QVector<Point3D> vect, Point3D::dataType factor);
const Point3D operator-(Point3D & p);
const Point3D operator%(const Point3D p1, const Point3D p2);
const Point3D cross(const Point3D p1, const Point3D p2);
Point3D::dataType dot(const Point3D p1, const Point3D p2);
Point3D normalize(const Point3D point, Point3D::dataType length = 0);
Point3D::dataType distance(const Point3D point1, const Point3D point2 = Point3D(0,0,0));
#endif // Point3D_H