-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixSpace.cc
159 lines (136 loc) · 4.55 KB
/
MatrixSpace.cc
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
// Copyright (c) 2021 Chanjung Kim. All rights reserved.
// Licensed under the MIT License.
#include <leth/MatrixSpace.hh>
#include <limits>
#include <random>
MatrixSpace::MatrixSpace(uint16_t width, uint16_t height) :
Space { width, height },
_pos2I(static_cast<size_t>(width) * height, std::numeric_limits<size_t>::max())
{
_A.reserve(static_cast<size_t>(width) * width * height * height);
_x.reserve(static_cast<size_t>(width) * height);
_b.reserve(static_cast<size_t>(width) * height);
_i2Pos.reserve(static_cast<size_t>(width) * height);
}
char const* MatrixSpace::GetName() noexcept
{
return "MatrixSpace";
}
char const* MatrixSpace::GetErrorMessage(ErrorCode errorCode) noexcept
{
return GetErrorMessageInternal(static_cast<ErrorType>(errorCode));
}
char const* MatrixSpace::GetErrorMessageInternal(ErrorType errorType) noexcept
{
switch (errorType)
{
case ErrorType::Success: return "Success";
case ErrorType::InvalidEquation: return "Insufficient boundary condition";
}
return "Unknown error";
}
ErrorCode MatrixSpace::RunSimulation(Point const* input, float* output) noexcept
{
return static_cast<ErrorCode>(RunSimulationInternal(input, output));
}
MatrixSpace::ErrorType MatrixSpace::RunSimulationInternal(Point const* input,
float* output) noexcept
{
if (!BuildEquation(input))
return ErrorType::InvalidEquation;
SolveEquation(_A, _x, _b);
CopyResults(input, output);
return ErrorType::Success;
}
bool MatrixSpace::BuildEquation(Point const* input) noexcept
{
_A.clear();
_x.clear();
_b.clear();
_i2Pos.clear();
_i2NumNeighboringWalls.clear();
constexpr int16_t offsets[4][2] {
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
};
for (uint16_t i { 0 }, iEnd { height() }; i < iEnd; ++i)
{
for (uint16_t j { 0 }, jEnd { width() }; j < jEnd; ++j)
{
size_t const idx { GetIndex(i, j) };
if (input[idx].type == PointType::GroundTruth)
{
uint32_t numNeighboringWalls { 0 };
for (auto& offset : offsets)
{
if (!Inside(i + offset[0], j + offset[1]))
{
++numNeighboringWalls;
continue;
}
if (input[GetIndex(i + offset[0], j + offset[1])].type == PointType::OutOfRange)
{
++numNeighboringWalls;
continue;
}
}
_pos2I[idx] = _i2Pos.size();
_i2NumNeighboringWalls.push_back(numNeighboringWalls);
_i2Pos.push_back(Pos { static_cast<uint16_t>(j), static_cast<uint16_t>(i) });
}
else
_pos2I[idx] = std::numeric_limits<size_t>::max();
}
}
size_t const numVars { _i2Pos.size() };
_A.resize(numVars * numVars, 0.0f);
_x.resize(numVars, 0.0f);
_b.resize(numVars, 0.0f);
for (size_t i { 0 }, iEnd { _i2Pos.size() }; i < iEnd; ++i)
{
_A[i * numVars + i] = -4 + static_cast<float>(_i2NumNeighboringWalls[i]);
size_t const idx { GetIndex(_i2Pos[i].y, _i2Pos[i].x) };
for (auto& offset : offsets)
{
if (!Inside(_i2Pos[i].y + offset[0], _i2Pos[i].x + offset[1]))
continue;
auto const offsetAppliedIdx {
GetIndex(_i2Pos[i].y + offset[0], _i2Pos[i].x + offset[1]),
};
switch (input[offsetAppliedIdx].type)
{
case PointType::Boundary:
{
_b[i] -= input[offsetAppliedIdx].temp;
break;
}
case PointType::GroundTruth:
{
size_t const offsetAppliedI { _pos2I[offsetAppliedIdx] };
_A[i * numVars + offsetAppliedI] += 1;
break;
}
}
}
}
return true;
}
void MatrixSpace::CopyResults(Point const* input, float* output) noexcept
{
for (size_t i { 0 }, iEnd { _i2Pos.size() }; i < iEnd; ++i)
{
size_t idx { GetIndex(_i2Pos[i].y, _i2Pos[i].x) };
output[idx] = _x[i];
}
for (uint16_t i { 0 }, iEnd { height() }; i < iEnd; ++i)
{
for (uint16_t j { 0 }, jEnd { width() }; j < jEnd; ++j)
{
size_t idx { GetIndex(i, j) };
if (input[idx].type == PointType::Boundary)
output[idx] = input[idx].temp;
}
}
}