-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLevelSetGrid2D.cpp
69 lines (61 loc) · 1.4 KB
/
LevelSetGrid2D.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
#include "stdafx.h"
#include "LevelSetGrid2D.h"
LevelSetGrid2D::LevelSetGrid2D(int xResolution, int yResolution)
{
size[0] = xResolution + 2 * bufferSize;
size[1] = yResolution + 2 * bufferSize;
gridLength = size[0] * size[1];
grid = new double[gridLength];
std::fill(grid, grid + gridLength, 0);
}
LevelSetGrid2D::~LevelSetGrid2D()
{
delete[] grid;
}
double& LevelSetGrid2D::get(int x, int y)
{
x += bufferSize;
y += bufferSize;
if (x < 0 || x >= size[0] || y < 0 || y >= size[1]) throw std::exception();
return grid[getindex(x, y)];
}
void LevelSetGrid2D::UpdateBorders()
{
for (int i = 0; i < size[0]; i++)
{
for (int j = 0; j < size[1]; j++)
{
int index[2] = { i, j };
double dist[2] = { 0, 0 };
for (int m = 0; m < 2; m++)
{
if (index[m] < bufferSize)
{
dist[m] = abs(index[m] - bufferSize);
index[m] = bufferSize;
}
else if (size[m] - index[m] <= bufferSize)
{
dist[m] = abs(size[m] - 1 - index[m] - bufferSize);
index[m] = size[m] - 1 - bufferSize;
}
}
double distance = sqrt(dist[0] * dist[0] + dist[1] * dist[1]);
double& phi = grid[getindex(i, j)];
phi = grid[getindex(index[0], index[1])];
//added this so that phi values beyond the border are more accurate
if (phi < 0)
{
phi -= distance;
}
else
{
phi += distance;
}
}
}
}
int LevelSetGrid2D::getindex(int x, int y)
{
return x * size[1] + y;
}