-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster.pde
147 lines (140 loc) · 4.4 KB
/
raster.pde
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
int signum(float f) {
if (f > 0) return 1;
if (f < 0) return -1;
return 0;
}
// the internet says this is "bresenham's". whatevs.
List<Coordinate> line3d(int startx, int starty, int startz, int endx, int endy, int endz) {
int dx = endx - startx;
int dy = endy - starty;
int dz = endz - startz;
int ax = abs(dx) << 1;
int ay = abs(dy) << 1;
int az = abs(dz) << 1;
int signx = (int) signum(dx);
int signy = (int) signum(dy);
int signz = (int) signum(dz);
int x = startx;
int y = starty;
int z = startz;
int deltax, deltay, deltaz;
List<Coordinate> results = new ArrayList<Coordinate>();
if (ax >= max(ay, az)) /* x dominant */ {
deltay = ay - (ax >> 1);
deltaz = az - (ax >> 1);
while (true) {
results.add(new Coordinate(x, y, z));
if (x == endx) return results;
if (deltay >= 0) {
y += signy;
deltay -= ax;
}
if (deltaz >= 0) {
z += signz;
deltaz -= ax;
}
x += signx;
deltay += ay;
deltaz += az;
}
} else if (ay >= max(ax, az)) /* y dominant */ {
deltax = ax - (ay >> 1);
deltaz = az - (ay >> 1);
while (true) {
results.add(new Coordinate(x,y,z));
if (y == endy) return results;
if (deltax >= 0) {
x += signx;
deltax -= ay;
}
if (deltaz >= 0) {
z += signz;
deltaz -= ay;
}
y += signy;
deltax += ax;
deltaz += az;
}
} else if (az >= max(ax, ay)) /* z dominant */ {
deltax = ax - (az >> 1);
deltay = ay - (az >> 1);
while (true) {
results.add(new Coordinate(x,y,z));
if (z == endz) return results;
if (deltax >= 0) {
x += signx;
deltax -= az;
}
if (deltay >= 0) {
y += signy;
deltay -= az;
}
z += signz;
deltax += ax;
deltay += ay;
}
}
return results;
}
void rasterizeModelToPixels() {
int numOversizedStrips = 0;
List<VirtualPixel> virtualPixels = new ArrayList<VirtualPixel>();
for (VirtualStrip strip : ksVirtualStrips) {
VirtualWayPoint prev = null;
int pixelId = 0;
for (VirtualWayPoint wayPoint : strip.wayPoints) {
if (prev != null) {
VirtualWayPoint cur = wayPoint;
for (Coordinate coord : line3d(int(prev.coord.x), int(prev.coord.y), int(prev.coord.z),
int(cur.coord.x), int(cur.coord.y), int(cur.coord.z))) {
virtualPixels.add(new VirtualPixel(strip.controllerId, strip.stripId,
pixelId, coord.x, coord.y, coord.z));
pixelId++;
}
}
prev = wayPoint;
}
if (pixelId > 96) {
// 10' strips have 96 pixels, 15' strips have more
println("controller "+strip.controllerId+", strip "+strip.stripId+", has "+pixelId+" pixels (>96)");
numOversizedStrips++;
}
}
ksVirtualPixels = virtualPixels.toArray(new VirtualPixel[virtualPixels.size()]);
println(ksVirtualStrips.length+" strips to "+ksVirtualPixels.length+" pixels");
println("number of strips > 10 feet: "+numOversizedStrips);
for (VirtualPixel vp : ksVirtualPixels) {
if (vp.coord.y > maxY) maxY = vp.coord.y;
if (vp.coord.y < minY) minY = vp.coord.y;
if (vp.coord.x > maxX) maxX = vp.coord.x;
if (vp.coord.x < minX) minX = vp.coord.x;
}
}
int maxControllerId = -1;
int maxStripId = -1;
int maxPixelId = -1;
void initControllerStripMap() {
for (VirtualPixel vp : ksVirtualPixels) {
if (vp.controllerId > maxControllerId) maxControllerId = vp.controllerId;
if (vp.stripId > maxStripId) maxStripId = vp.stripId;
if (vp.pixelId > maxPixelId) maxPixelId = vp.pixelId;
}
controllerStripMap = new int[maxControllerId+1][maxStripId+1][maxPixelId+1];
for (int i = 0; i < ksVirtualPixels.length; i++) {
VirtualPixel vp = ksVirtualPixels[i];
controllerStripMap[vp.controllerId][vp.stripId][vp.pixelId] = i;
}
if (maxControllerId == -1 || maxStripId == -1 || maxPixelId == -1) {
println("*** fatal: maxControllerId("+maxControllerId+") or StripId("
+maxStripId+") or PixelId("+maxPixelId+") is -1!");
exit();
}
}
void dumpControllerStripMap(String file)
{
List<String> strings = new ArrayList<String>();
for (VirtualPixel vp : ksVirtualPixels) {
strings.add(vp.coord.x+","+vp.coord.y+","+vp.coord.z+";"+vp.controllerId+":"+vp.stripId+":"+vp.pixelId);
}
saveStrings(file, strings.toArray(new String[strings.size()]));
}