-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry4d.cpp
195 lines (164 loc) · 4.1 KB
/
geometry4d.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Tessy - An Exploration in Four Dimensions
* geometry4d.cpp
* Copyright (C) 2017 Benjamin P. Jones
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "geometry4d.h"
#include "geometry3d.h"
#include "graphics.h"
#include <cmath> // std::signbit
#include <assert.h>
#include <vector>
#include <array>
#include <iostream>
Point4D::Point4D(double x, double y, double z, double w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
Point4D& Point4D::operator+=(Point4D const& rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
w += rhs.w;
return *this;
}
Point4D& Point4D::operator-=(Point4D const& rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
w -= rhs.w;
return *this;
}
Point4D Point4D::operator+(Point4D rhs)
{
rhs += *this;
return rhs;
}
Point4D Point4D::operator-(Point4D rhs)
{
rhs -= *this;
return rhs;
}
Point4D& Point4D::operator*=(double scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
return *this;
}
Point4D& Point4D::Transform(std::array<std::array<double,4>, 4> const& matrix)
{
x = matrix[0][0] * x
+ matrix[0][1] * y
+ matrix[0][2] * z
+ matrix[0][3] * w;
y = matrix[1][0] * x
+ matrix[1][1] * y
+ matrix[1][2] * z
+ matrix[1][3] * w;
z = matrix[2][0] * x
+ matrix[2][1] * y
+ matrix[2][2] * z
+ matrix[2][3] * w;
w = matrix[3][0] * x
+ matrix[3][1] * y
+ matrix[3][2] * z
+ matrix[3][3] * w;
return *this;
}
Point3D Point4D::Flatten()
{
return Point3D(x, y, z);
}
double Point4D::GetW()
{
return w;
}
Point4D operator*(double scalar, Point4D vector)
{
vector *= scalar;
return vector;
}
Point4D operator*(std::array<std::array<double,4>, 4> const& matrix,
Point4D vector)
{
vector.Transform(matrix);
return vector;
}
bool LineSegment4D::uniquelyIntersectsXYZ()
{
if (pt0.GetW() < 0 && pt1.GetW() > 0) return true;
if (pt0.GetW() > 0 && pt1.GetW() < 0) return true;
return false;
}
LineSegment4D::LineSegment4D(Point4D startPt, Point4D endPt)
:
pt0(startPt),
pt1(endPt)
{
}
Point3D LineSegment4D::Slice()
{
// In order for a return type of Point4D to make sense,
// The w=0 hyperplane should intersect the segment at a unique point.
// This uniqueness check should occur outside of this function.
assert(uniquelyIntersectsXYZ());
double dw = std::abs(pt1.GetW() - pt0.GetW());
double weight0 = std::abs(pt0.GetW()/dw);
double weight1 = std::abs(pt1.GetW()/dw);
return (weight0 * pt0 + weight1 * pt1).Flatten();
}
void Polychoron::DrawWireFrame(Graphics const& graphics) const {
Point3D pt0(0,0,0);
Point3D pt1(0,0,0);
for (int i=0; i < hyperedges.size(); ++i) {
LineSegment4D edge0(vertices[edges[hyperedges[i][0]][0]],
vertices[edges[hyperedges[i][0]][1]]);
LineSegment4D edge1(vertices[edges[hyperedges[i][1]][0]],
vertices[edges[hyperedges[i][1]][1]]);
if (edge0.uniquelyIntersectsXYZ()) {
pt0 = edge0.Slice();
} else {
continue;
}
if (edge1.uniquelyIntersectsXYZ()) {
pt1 = edge1.Slice();
} else {
continue;
}
LineSegment3D(pt0, pt1).Draw(graphics);
}
}
Polychoron& Polychoron::Transform(
std::array<std::array<double,4>, 4> const& matrix)
{
for (int i=0; i < vertices.size(); ++i) {
vertices[i].Transform(matrix);
}
return *this;
}
Polychoron& Polychoron::Shift(Point4D const& offset) {
for (int i=0; i < vertices.size(); ++i) {
vertices[i] += offset;
}
return *this;
}