-
Notifications
You must be signed in to change notification settings - Fork 20
/
Bresenham.cpp
144 lines (132 loc) · 3.49 KB
/
Bresenham.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
#include "Bresenham.h"
#include "node.h"
#include <algorithm>
#include <stdexcept>
void iBresenham::bresenham3d(int x1, int y1, int z1, const int x2, const int y2, const int z2) {
int i, dx, dy, dz, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
int point[3];
point[0] = x1;
point[1] = y1;
point[2] = z1;
dx = x2 - x1;
dy = y2 - y1;
dz = z2 - z1;
if (dx == 0) {
x_inc = 0;
} else if (dx < 0) {
x_inc = -1;
} else {
x_inc = 1;
}
l = abs(dx);
if (dy == 0) {
y_inc = 0;
} else if (dy < 0) {
y_inc = -1;
} else {
y_inc = 1;
}
m = abs(dy);
if (dz == 0) {
z_inc = 0;
} else if (dz < 0) {
z_inc = -1;
} else {
z_inc = 1;
}
n = abs(dz);
dx2 = l << 1;
dy2 = m << 1;
dz2 = n << 1;
if ((l >= m) && (l >= n)) {
err_1 = dy2 - l;
err_2 = dz2 - l;
for (i = 0; i < l; i++) {
if (!ProcessPoint(point[0], point[1], point[2])) {
return;
}
if (err_1 > 0) {
point[1] += y_inc;
err_1 -= dx2;
}
if (err_2 > 0) {
point[2] += z_inc;
err_2 -= dx2;
}
err_1 += dy2;
err_2 += dz2;
point[0] += x_inc;
}
} else if ((m >= l) && (m >= n)) {
err_1 = dx2 - m;
err_2 = dz2 - m;
for (i = 0; i < m; i++) {
if (!ProcessPoint(point[0], point[1], point[2])) {
return;
}
if (err_1 > 0) {
point[0] += x_inc;
err_1 -= dy2;
}
if (err_2 > 0) {
point[2] += z_inc;
err_2 -= dy2;
}
err_1 += dx2;
err_2 += dz2;
point[1] += y_inc;
}
} else {
err_1 = dy2 - n;
err_2 = dx2 - n;
for (i = 0; i < n; i++) {
if (!ProcessPoint(point[0], point[1], point[2])) {
return;
}
if (err_1 > 0) {
point[1] += y_inc;
err_1 -= dz2;
}
if (err_2 > 0) {
point[0] += x_inc;
err_2 -= dz2;
}
err_1 += dy2;
err_2 += dx2;
point[2] += z_inc;
}
}
if (!ProcessPoint(point[0], point[1], point[2])) {
return;
}
}
LineOfSight::LineOfSight(const Map& init_map) : force_stopped(false), map(init_map) {}
bool LineOfSight::ProcessPoint(int i, int j, int h) {
if (map.CellIsObstacle(i, j, h)) {
force_stopped = true;
return false;
}
return true;
}
bool LineOfSight::line_of_sight(int i0, int j0, int h0, int i1, int j1, int h1) {
bresenham3d(i0, j0, h0, i1, j1, h1);
return !force_stopped;
}
bool LineOfSight::line_of_sight(const Node &from, const Node &to) {
return line_of_sight(from.i, from.j, from.z, to.i, to.j, to.z);
}
Liner::Liner(const Map &init_map, std::list<Node> *init_path) : map(init_map), path(init_path) {}
bool Liner::ProcessPoint(int i, int j, int h) {
Node newNode;
newNode.i = i;
newNode.j = j;
newNode.z = h;
path->push_back(newNode);
return true;
}
void Liner::append_line(int i0, int j0, int h0, int i1, int j1, int h1) {
bresenham3d(i0, j0, h0, i1, j1, h1);
}
void Liner::append_line(const Node &from, const Node &to) {
bresenham3d(from.i, from.j, from.z, to.i, to.j, to.z);
}