src/vertex.cpp

changeset 18
918b6c0f8b5b
parent 8
44679e468ba9
child 20
cef43609a374
equal deleted inserted replaced
17:a5111f4e6412 18:918b6c0f8b5b
29 this->y = y2; 29 this->y = y2;
30 this->z = z2; 30 this->z = z2;
31 } 31 }
32 */ 32 */
33 33
34 Vertex::ValueType& Vertex::operator[](Axis axis) 34 Point3D::CoordinateType& Point3D::get(Axis axis)
35 { 35 {
36 switch (axis) 36 switch (axis)
37 { 37 {
38 case X: 38 case X:
39 return this->x; 39 return this->x;
44 default: 44 default:
45 throw std::runtime_error("Non-axis given to Vertex::operator[]"); 45 throw std::runtime_error("Non-axis given to Vertex::operator[]");
46 } 46 }
47 } 47 }
48 48
49 Vertex::ValueType Vertex::operator[](Axis axis) const 49 Point3D::CoordinateType Point3D::get(Axis axis) const
50 { 50 {
51 switch (axis) 51 switch (axis)
52 { 52 {
53 case X: 53 case X:
54 return this->x; 54 return this->x;
59 default: 59 default:
60 return 0; 60 return 0;
61 } 61 }
62 } 62 }
63 63
64 void Vertex::setCoordinate(Axis axis, ValueType value) 64 void Point3D::assign(Axis axis, CoordinateType value)
65 { 65 {
66 (*this)[axis] = value; 66 this->get(axis) = value;
67 } 67 }
68 68
69 Vertex VertexFromVector(const QVector3D& vector) 69 Point3D VertexFromVector(const QVector3D& vector)
70 { 70 {
71 return {vector.x(), vector.y(), vector.z()}; 71 return {vector.x(), vector.y(), vector.z()};
72 } 72 }
73 73
74 Vertex Vertex::operator*(ValueType scalar) const 74 Point3D operator*(const Point3D& point, Point3D::CoordinateType scalar)
75 { 75 {
76 return {this->x * scalar, this->y * scalar, this->z * scalar}; 76 return {point.x * scalar, point.y * scalar, point.z * scalar};
77 } 77 }
78 78
79 Vertex& Vertex::operator+=(const QVector3D& other) 79 Point3D& operator+=(Point3D& point, const QVector3D& other)
80 { 80 {
81 this->x += other.x(); 81 point.x += other.x();
82 this->y += other.y(); 82 point.y += other.y();
83 this->z += other.z(); 83 point.z += other.z();
84 return *this; 84 return point;
85 } 85 }
86 86
87 Vertex Vertex::operator+(const QVector3D& other) const 87 Point3D operator+(Point3D point, const QVector3D& other)
88 { 88 {
89 Vertex result(*this); 89 point += other;
90 result += other; 90 return point;
91 return result; 91 }
92 } 92
93 93
94 94 QVector3D vertexToVector(const Point3D& vertex)
95 QVector3D vertexToVector(const Vertex& vertex)
96 { 95 {
97 return { 96 return {
98 static_cast<float>(vertex.x), 97 static_cast<float>(vertex.x),
99 static_cast<float>(vertex.y), 98 static_cast<float>(vertex.y),
100 static_cast<float>(vertex.z) 99 static_cast<float>(vertex.z)
101 }; 100 };
102 } 101 }
103 102
104 Vertex Vertex::operator-(const QVector3D& vector) const 103 Point3D operator-(Point3D point, const QVector3D& vector)
105 { 104 {
106 Vertex result = *this; 105 point -= vector;
107 result -= vector; 106 return point;
108 return result; 107 }
109 } 108
110 109 Point3D& operator-=(Point3D& point, const QVector3D& vector)
111 Vertex& Vertex::operator-=(const QVector3D& vector) 110 {
112 { 111 point.x -= vector.x();
113 this->x -= vector.x(); 112 point.y -= vector.y();
114 this->y -= vector.y(); 113 point.z -= vector.z();
115 this->z -= vector.z(); 114 return point;
116 return *this; 115 }
117 } 116
118 117 QVector3D operator-(const Point3D& point, const Point3D& other)
119 QVector3D Vertex::operator-(const Vertex& other) const
120 { 118 {
121 return { 119 return {
122 static_cast<float>(this->x - other.x), 120 static_cast<float>(point.x - other.x),
123 static_cast<float>(this->y - other.y), 121 static_cast<float>(point.y - other.y),
124 static_cast<float>(this->z - other.z) 122 static_cast<float>(point.z - other.z)
125 }; 123 };
126 } 124 }
127 125
128 Vertex& Vertex::operator*=(ValueType scalar) 126 Point3D& operator*=(Point3D& point, Point3D::CoordinateType scalar)
129 { 127 {
130 x *= scalar; 128 point.x *= scalar;
131 y *= scalar; 129 point.y *= scalar;
132 z *= scalar; 130 point.z *= scalar;
133 return *this; 131 return point;
134 } 132 }
135 133
136 bool Vertex::operator==(const Vertex& other) const 134 bool operator==(const Point3D& point, const Point3D& other)
137 { 135 {
138 return this->x == other.x and this->y == other.y and this->z == other.z; 136 return point.x == other.x and point.y == other.y and point.z == other.z;
139 } 137 }
140 138
141 bool Vertex::operator!=(const Vertex& other) const 139 bool operator!=(const Point3D& point, const Point3D& other)
142 { 140 {
143 return not(*this == other); 141 return not (point == other);
144 } 142 }
145 143
146 Vertex::operator QVariant() const 144 bool operator<(const Point3D& point, const Point3D& other)
147 { 145 {
148 return QVariant::fromValue<Vertex>(*this); 146 if (not qFuzzyCompare(point.x, other.x))
149 } 147 return point.x < other.x;
150 148 else if (not qFuzzyCompare(point.y, other.y))
151 bool Vertex::operator<(const Vertex& other) const 149 return point.y < other.y;
152 {
153 if (not qFuzzyCompare(this->x, other.x))
154 return this->x < other.x;
155 else if (not qFuzzyCompare(this->y, other.y))
156 return this->y < other.y;
157 else 150 else
158 return this->z < other.z; 151 return point.z < other.z;
159 } 152 }
160 153
161 /* 154 /*
162 * Transforms this vertex with a tranformation matrix and returns the result. 155 * Transforms this vertex with a tranformation matrix and returns the result.
163 */ 156 */
164 Vertex Vertex::transformed(const GLRotationMatrix& matrix) const 157 Point3D math::transform(const Point3D point, const GLRotationMatrix& matrix)
165 { 158 {
166 return { 159 return {
167 matrix(0, 0) * this->x 160 matrix(0, 0) * point.x
168 + matrix(0, 1) * this->y 161 + matrix(0, 1) * point.y
169 + matrix(0, 2) * this->z, 162 + matrix(0, 2) * point.z,
170 matrix(1, 0) * this->x 163 matrix(1, 0) * point.x
171 + matrix(1, 1) * this->y 164 + matrix(1, 1) * point.y
172 + matrix(1, 2) * this->z, 165 + matrix(1, 2) * point.z,
173 matrix(2, 0) * this->x 166 matrix(2, 0) * point.x
174 + matrix(2, 1) * this->y 167 + matrix(2, 1) * point.y
175 + matrix(2, 2) * this->z, 168 + matrix(2, 2) * point.z,
176 }; 169 };
177 } 170 }
178 171
179 /* 172 /*
180 * Returns the distance from one vertex to another. 173 * Returns the distance from one vertex to another.
181 */ 174 */
182 qreal distance(const Vertex& one, const Vertex& other) 175 qreal math::distance(const Point3D& one, const Point3D& other)
183 { 176 {
184 return (one - other).length(); 177 return (one - other).length();
185 } 178 }
186 179
187 /* 180 /*
188 * Returns a vertex with all coordinates inverted. 181 * Returns a vertex with all coordinates inverted.
189 */ 182 */
190 Vertex operator-(const Vertex& vertex) 183 Point3D operator-(const Point3D& vertex)
191 { 184 {
192 return {-vertex.x, -vertex.y, -vertex.z}; 185 return {-vertex.x, -vertex.y, -vertex.z};
193 } 186 }
194 187
195 /* 188 /*
196 * Inserts this vertex into a data stream. This is needed for vertices to be 189 * Inserts this vertex into a data stream. This is needed for vertices to be
197 * stored in QSettings. 190 * stored in QSettings.
198 */ 191 */
199 QDataStream& operator<<(QDataStream& out, const Vertex& vertex) 192 QDataStream& operator<<(QDataStream& out, const Point3D& vertex)
200 { 193 {
201 return out << vertex.x << vertex.y << vertex.z; 194 return out << vertex.x << vertex.y << vertex.z;
202 } 195 }
203 196
204 /* 197 /*
205 * Takes a vertex from a data stream. 198 * Takes a vertex from a data stream.
206 */ 199 */
207 QDataStream& operator>>(QDataStream& in, Vertex& vertex) 200 QDataStream& operator>>(QDataStream& in, Point3D& vertex)
208 { 201 {
209 return in >> vertex.x >> vertex.y >> vertex.z; 202 return in >> vertex.x >> vertex.y >> vertex.z;
210 } 203 }
211 204
212 unsigned int qHash(const Vertex& key) 205 unsigned int qHash(const Point3D& key)
213 { 206 {
214 return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z)); 207 return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
215 } 208 }
216 209
217 QString vertexToStringParens(const Vertex& vertex) 210 QString vertexToStringParens(const Point3D& vertex)
218 { 211 {
219 return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z); 212 return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
220 } 213 }

mercurial