1 | /* |
---|
2 | |
---|
3 | Brian Curless |
---|
4 | |
---|
5 | Computer Graphics Laboratory |
---|
6 | Stanford University |
---|
7 | |
---|
8 | --------------------------------------------------------------------- |
---|
9 | |
---|
10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
11 | University. Except for commercial resale, lease, license or other |
---|
12 | commercial transactions, permission is hereby given to use, copy, |
---|
13 | modify this software for academic purposes only. No part of this |
---|
14 | software or any derivatives thereof may be used in the production of |
---|
15 | computer models for resale or for use in a commercial |
---|
16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #include <math.h> |
---|
23 | #include <limits.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <unistd.h> |
---|
26 | |
---|
27 | #include "vrip.h" |
---|
28 | #include "vripGlobals.h" |
---|
29 | #include "resample.h" |
---|
30 | #include "occFunc.h" |
---|
31 | #include "scanRLE.h" |
---|
32 | |
---|
33 | |
---|
34 | static OccGridRLE *theOutGrid; |
---|
35 | static int currentType; |
---|
36 | static RunLength currentLength; |
---|
37 | static OccElement lastElement; |
---|
38 | |
---|
39 | |
---|
40 | static void initScanline(OccGridRLE *, int, int); |
---|
41 | static void appendScanlinePortion(OccGridRLE *inGrid, int inType, |
---|
42 | OccElement *inElement, RunLength length); |
---|
43 | static void appendScanline(OccElement *inElement); |
---|
44 | static void sealScanline(); |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | // What happens when resample and updateCell are essentially no-ops? |
---|
49 | |
---|
50 | #if 0 |
---|
51 | |
---|
52 | #define resample(depthMap, xOff, yOff, pDepth, pConfidence) *pDepth = 100; *pConfidence = 0; |
---|
53 | |
---|
54 | #define updateCell(zz, depth, sampleSpacing, confidence, buf) confidence = 1; |
---|
55 | |
---|
56 | #endif |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | void |
---|
61 | scanConvert(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
62 | OrthoShear *shear, DepthMap *depthMap) |
---|
63 | { |
---|
64 | float xOff, yOff, res, depth, confidence; |
---|
65 | OccElement *buf, *scanline; |
---|
66 | int xx,yy,zz; |
---|
67 | |
---|
68 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
69 | |
---|
70 | res = gridIn->resolution; |
---|
71 | |
---|
72 | gridOut->copyParams(gridIn); |
---|
73 | |
---|
74 | gridOut->reset(); |
---|
75 | |
---|
76 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
77 | if (!Quiet) |
---|
78 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
79 | fflush(stdout); |
---|
80 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
81 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
82 | buf = scanline = gridIn->getScanline(yy,zz); |
---|
83 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
84 | for (xx = 0; xx < gridIn->xdim; xx++, xOff++, buf++) { |
---|
85 | resample(depthMap, xOff, yOff, &depth, &confidence); |
---|
86 | |
---|
87 | // Using the full-blown filler! |
---|
88 | updateCell(gridIn->sliceOrigins[zz][2], depth, |
---|
89 | sampleSpacing, confidence, buf); |
---|
90 | } |
---|
91 | gridOut->putScanline(scanline,yy,zz); |
---|
92 | } |
---|
93 | } |
---|
94 | if (!Quiet) |
---|
95 | printf("\n"); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void |
---|
100 | scanConvertDragTails(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
101 | OrthoShear *shear, DepthMap *depthMap) |
---|
102 | { |
---|
103 | float xOff, yOff, res, depth, confidence; |
---|
104 | OccElement *buf, *scanline; |
---|
105 | int xx,yy,zz; |
---|
106 | |
---|
107 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
108 | |
---|
109 | res = gridIn->resolution; |
---|
110 | |
---|
111 | gridOut->copyParams(gridIn); |
---|
112 | |
---|
113 | gridOut->reset(); |
---|
114 | |
---|
115 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
116 | if (!Quiet) |
---|
117 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
118 | fflush(stdout); |
---|
119 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
120 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
121 | buf = scanline = gridIn->getScanline(yy,zz); |
---|
122 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
123 | for (xx = 0; xx < gridIn->xdim; xx++, xOff++, buf++) { |
---|
124 | resample(depthMap, xOff, yOff, &depth, &confidence); |
---|
125 | |
---|
126 | // Using the full-blown filler! |
---|
127 | updateCellForCarving(gridIn->sliceOrigins[zz][2], depth, |
---|
128 | sampleSpacing, confidence, buf); |
---|
129 | } |
---|
130 | gridOut->putScanline(scanline,yy,zz); |
---|
131 | } |
---|
132 | } |
---|
133 | if (!Quiet) |
---|
134 | printf("\n"); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | void |
---|
140 | scanConvertTree(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
141 | OrthoShear *shear, DepthMap *depthMap) |
---|
142 | { |
---|
143 | float xOff, yOff, res, depth, confidence; |
---|
144 | OccElement *buf, *scanline; |
---|
145 | int xx,yy,zz; |
---|
146 | int *depthRuns, numRuns; |
---|
147 | float zmin, zmax, xNewOff; |
---|
148 | int xmin, xmax; |
---|
149 | OccScanlineRLE *rleScanline; |
---|
150 | |
---|
151 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
152 | |
---|
153 | res = gridIn->resolution; |
---|
154 | |
---|
155 | gridOut->copyParams(gridIn); |
---|
156 | |
---|
157 | gridOut->reset(); |
---|
158 | |
---|
159 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
160 | if (!Quiet) |
---|
161 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
162 | fflush(stdout); |
---|
163 | |
---|
164 | // Something is really backwards here! |
---|
165 | |
---|
166 | /* |
---|
167 | zmin = gridIn->sliceOrigins[zz][2] + C5/sampleSpacing; |
---|
168 | zmax = gridIn->sliceOrigins[zz][2] + 2*C1/sampleSpacing; |
---|
169 | */ |
---|
170 | zmin = gridIn->sliceOrigins[zz][2] - C1/sampleSpacing; |
---|
171 | zmax = gridIn->sliceOrigins[zz][2] - C5/sampleSpacing; |
---|
172 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
173 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
174 | depthRuns = depthMap->getDepthRuns(yOff, zmin, zmax, &numRuns); |
---|
175 | if (numRuns == 0) { |
---|
176 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
177 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
178 | continue; |
---|
179 | } |
---|
180 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
181 | scanline = gridIn->getScanline(yy,zz); |
---|
182 | for (int i = 0; i < numRuns; i++) { |
---|
183 | xmin = int(depthRuns[2*i] - xOff); |
---|
184 | xmin = MAX(xmin, 0); |
---|
185 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
186 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
187 | xNewOff = xOff + xmin; |
---|
188 | buf = scanline + xmin; |
---|
189 | for (xx = xmin; xx <= xmax; xx++, xNewOff++, buf++) { |
---|
190 | resample(depthMap, xNewOff, yOff, &depth, &confidence); |
---|
191 | updateCell(gridIn->sliceOrigins[zz][2], depth, |
---|
192 | sampleSpacing, confidence, buf); |
---|
193 | } |
---|
194 | } |
---|
195 | gridOut->putScanline(scanline,yy,zz); |
---|
196 | } |
---|
197 | } |
---|
198 | if (!Quiet) |
---|
199 | printf("\n"); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | void |
---|
204 | scanConvertTreeFast(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
205 | OrthoShear *shear, DepthMap *depthMap) |
---|
206 | { |
---|
207 | float xOff, yOff, res, depth, confidence; |
---|
208 | OccElement *buf, *scanline, outElement; |
---|
209 | int xx,yy,zz; |
---|
210 | int *depthRuns, numRuns; |
---|
211 | float zmin, zmax, xNewOff; |
---|
212 | int xmin, xmax; |
---|
213 | OccScanlineRLE *rleScanline; |
---|
214 | |
---|
215 | |
---|
216 | //printf("Using bias toward emptiness\n"); |
---|
217 | |
---|
218 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
219 | |
---|
220 | res = gridIn->resolution; |
---|
221 | |
---|
222 | gridOut->copyParams(gridIn); |
---|
223 | |
---|
224 | gridOut->reset(); |
---|
225 | |
---|
226 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
227 | if (!Quiet) |
---|
228 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
229 | fflush(stdout); |
---|
230 | |
---|
231 | // Something is really backwards here! |
---|
232 | |
---|
233 | /* |
---|
234 | zmin = gridIn->sliceOrigins[zz][2] + C5/sampleSpacing; |
---|
235 | zmax = gridIn->sliceOrigins[zz][2] + 2*C1/sampleSpacing; |
---|
236 | */ |
---|
237 | zmin = gridIn->sliceOrigins[zz][2] - C1/sampleSpacing; |
---|
238 | zmax = gridIn->sliceOrigins[zz][2] - C5/sampleSpacing; |
---|
239 | |
---|
240 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
241 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
242 | depthRuns = depthMap->getDepthRuns(yOff, zmin, zmax, &numRuns); |
---|
243 | if (numRuns == 0) { |
---|
244 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
245 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
246 | continue; |
---|
247 | } |
---|
248 | |
---|
249 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
250 | xx = 0; |
---|
251 | |
---|
252 | // Prepare input scanline |
---|
253 | gridIn->setScanline(yy, zz); |
---|
254 | RunLength inLength = gridIn->getNextLength(); |
---|
255 | int inType = gridIn->getRunType(&inLength); |
---|
256 | int nextInOffset = inLength; |
---|
257 | OccElement *inElement; |
---|
258 | |
---|
259 | if (inType == OccGridRLE::CONSTANT_DATA) |
---|
260 | inElement = gridIn->getNextElement(); |
---|
261 | |
---|
262 | // Prepare output scanline |
---|
263 | initScanline(gridOut, yy, zz); |
---|
264 | |
---|
265 | for (int i = 0; i < numRuns; i++) { |
---|
266 | xmin = int(depthRuns[2*i] - xOff); |
---|
267 | xmin = MAX(xmin, 0); |
---|
268 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
269 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
270 | xNewOff = xOff + xmin; |
---|
271 | |
---|
272 | // Copy over all whole runs that precede the current xmin |
---|
273 | |
---|
274 | while (nextInOffset < xmin) { |
---|
275 | if (nextInOffset != xx) { |
---|
276 | appendScanlinePortion(gridIn, inType, inElement, |
---|
277 | nextInOffset - xx); |
---|
278 | } |
---|
279 | xx = nextInOffset; |
---|
280 | |
---|
281 | // Update for next run |
---|
282 | inLength = gridIn->getNextLength(); |
---|
283 | inType = gridIn->getRunType(&inLength); |
---|
284 | nextInOffset += inLength; |
---|
285 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
286 | inElement = gridIn->getNextElement(); |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | // Copy over the remaining portion of the first |
---|
292 | // overlapping run |
---|
293 | |
---|
294 | if (xx != xmin) { |
---|
295 | appendScanlinePortion(gridIn, inType, inElement, |
---|
296 | xmin - xx); |
---|
297 | } |
---|
298 | |
---|
299 | xx = xmin; |
---|
300 | |
---|
301 | // Update the output based on new data combined with |
---|
302 | // old data |
---|
303 | |
---|
304 | while (xx <= xmax) { |
---|
305 | |
---|
306 | // If old input run is finished, start a new one |
---|
307 | |
---|
308 | if (xx == nextInOffset) { |
---|
309 | inLength = gridIn->getNextLength(); |
---|
310 | inType = gridIn->getRunType(&inLength); |
---|
311 | nextInOffset += inLength; |
---|
312 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
313 | inElement = gridIn->getNextElement(); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | // If varying run, get next element |
---|
318 | |
---|
319 | if (inType == OccGridRLE::VARYING_DATA) { |
---|
320 | inElement = gridIn->getNextElement(); |
---|
321 | } |
---|
322 | |
---|
323 | // Copy over element and update |
---|
324 | |
---|
325 | outElement = *inElement; |
---|
326 | //resample(depthMap, xNewOff, yOff, &depth, &confidence); |
---|
327 | resampleBetter(depthMap, xNewOff, yOff, &depth, &confidence); |
---|
328 | |
---|
329 | updateCell(gridIn->sliceOrigins[zz][2], |
---|
330 | depth, |
---|
331 | sampleSpacing, confidence, |
---|
332 | &outElement); |
---|
333 | |
---|
334 | // Write element to the output grid |
---|
335 | appendScanline(&outElement); |
---|
336 | |
---|
337 | xNewOff++; |
---|
338 | xx++; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | // Finish up any remaining runs |
---|
343 | |
---|
344 | while (xx < gridOut->xdim) { |
---|
345 | if (nextInOffset != xx) { |
---|
346 | appendScanlinePortion(gridIn, inType, inElement, |
---|
347 | nextInOffset - xx); |
---|
348 | } |
---|
349 | xx = nextInOffset; |
---|
350 | |
---|
351 | // Update for next run |
---|
352 | inLength = gridIn->getNextLength(); |
---|
353 | inType = gridIn->getRunType(&inLength); |
---|
354 | |
---|
355 | if (inType == OccGridRLE::END_OF_RUN) { |
---|
356 | break; |
---|
357 | } |
---|
358 | |
---|
359 | nextInOffset += inLength; |
---|
360 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
361 | inElement = gridIn->getNextElement(); |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | // Seal off the old run |
---|
366 | sealScanline(); |
---|
367 | } |
---|
368 | } |
---|
369 | if (!Quiet) |
---|
370 | printf("\n"); |
---|
371 | } |
---|
372 | |
---|
373 | |
---|
374 | void |
---|
375 | scanConvertTreeBiasEdgesEmpty(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
376 | OrthoShear *shear, DepthMap *depthMap) |
---|
377 | { |
---|
378 | float xOff, yOff, res, depth, confidence; |
---|
379 | OccElement *buf, *scanline, outElement; |
---|
380 | int xx,yy,zz; |
---|
381 | int *depthRuns, numRuns; |
---|
382 | float zmin, zmax, xNewOff; |
---|
383 | float edgeConfidence; |
---|
384 | int xmin, xmax; |
---|
385 | OccScanlineRLE *rleScanline; |
---|
386 | |
---|
387 | |
---|
388 | // printf("Using bias toward emptiness\n"); |
---|
389 | |
---|
390 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
391 | |
---|
392 | res = gridIn->resolution; |
---|
393 | |
---|
394 | gridOut->copyParams(gridIn); |
---|
395 | |
---|
396 | gridOut->reset(); |
---|
397 | |
---|
398 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
399 | if (!Quiet) |
---|
400 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
401 | fflush(stdout); |
---|
402 | |
---|
403 | // Something is really backwards here! |
---|
404 | |
---|
405 | /* |
---|
406 | zmin = gridIn->sliceOrigins[zz][2] + C5/sampleSpacing; |
---|
407 | zmax = gridIn->sliceOrigins[zz][2] + 2*C1/sampleSpacing; |
---|
408 | */ |
---|
409 | zmin = gridIn->sliceOrigins[zz][2] - C1/sampleSpacing; |
---|
410 | zmax = gridIn->sliceOrigins[zz][2] - C5/sampleSpacing; |
---|
411 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
412 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
413 | depthRuns = depthMap->getDepthRuns(yOff, zmin, zmax, &numRuns); |
---|
414 | if (numRuns == 0) { |
---|
415 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
416 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
417 | continue; |
---|
418 | } |
---|
419 | |
---|
420 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
421 | xx = 0; |
---|
422 | |
---|
423 | // Prepare input scanline |
---|
424 | gridIn->setScanline(yy, zz); |
---|
425 | RunLength inLength = gridIn->getNextLength(); |
---|
426 | int inType = gridIn->getRunType(&inLength); |
---|
427 | int nextInOffset = inLength; |
---|
428 | OccElement *inElement; |
---|
429 | |
---|
430 | if (inType == OccGridRLE::CONSTANT_DATA) |
---|
431 | inElement = gridIn->getNextElement(); |
---|
432 | |
---|
433 | // Prepare output scanline |
---|
434 | initScanline(gridOut, yy, zz); |
---|
435 | |
---|
436 | for (int i = 0; i < numRuns; i++) { |
---|
437 | xmin = int(depthRuns[2*i] - xOff); |
---|
438 | xmin = MAX(xmin, 0); |
---|
439 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
440 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
441 | xNewOff = xOff + xmin; |
---|
442 | |
---|
443 | // Copy over all whole runs that precede the current xmin |
---|
444 | |
---|
445 | while (nextInOffset < xmin) { |
---|
446 | if (nextInOffset != xx) { |
---|
447 | appendScanlinePortion(gridIn, inType, inElement, |
---|
448 | nextInOffset - xx); |
---|
449 | } |
---|
450 | xx = nextInOffset; |
---|
451 | |
---|
452 | // Update for next run |
---|
453 | inLength = gridIn->getNextLength(); |
---|
454 | inType = gridIn->getRunType(&inLength); |
---|
455 | nextInOffset += inLength; |
---|
456 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
457 | inElement = gridIn->getNextElement(); |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | // Copy over the remaining portion of the first |
---|
463 | // overlapping run |
---|
464 | |
---|
465 | if (xx != xmin) { |
---|
466 | appendScanlinePortion(gridIn, inType, inElement, |
---|
467 | xmin - xx); |
---|
468 | } |
---|
469 | |
---|
470 | xx = xmin; |
---|
471 | |
---|
472 | // Update the output based on new data combined with |
---|
473 | // old data |
---|
474 | |
---|
475 | while (xx <= xmax) { |
---|
476 | |
---|
477 | // If old input run is finished, start a new one |
---|
478 | |
---|
479 | if (xx == nextInOffset) { |
---|
480 | inLength = gridIn->getNextLength(); |
---|
481 | inType = gridIn->getRunType(&inLength); |
---|
482 | nextInOffset += inLength; |
---|
483 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
484 | inElement = gridIn->getNextElement(); |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | // If varying run, get next element |
---|
489 | |
---|
490 | if (inType == OccGridRLE::VARYING_DATA) { |
---|
491 | inElement = gridIn->getNextElement(); |
---|
492 | } |
---|
493 | |
---|
494 | // Copy over element and update |
---|
495 | |
---|
496 | outElement = *inElement; |
---|
497 | resampleWithEdge(depthMap, xNewOff, yOff, |
---|
498 | &depth, &confidence, &edgeConfidence); |
---|
499 | |
---|
500 | updateCellBiasEdgesEmpty(gridIn->sliceOrigins[zz][2], |
---|
501 | depth, |
---|
502 | sampleSpacing, confidence, |
---|
503 | edgeConfidence, |
---|
504 | &outElement); |
---|
505 | |
---|
506 | // Write element to the output grid |
---|
507 | appendScanline(&outElement); |
---|
508 | |
---|
509 | xNewOff++; |
---|
510 | xx++; |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | // Finish up any remaining runs |
---|
515 | |
---|
516 | while (xx < gridOut->xdim) { |
---|
517 | if (nextInOffset != xx) { |
---|
518 | appendScanlinePortion(gridIn, inType, inElement, |
---|
519 | nextInOffset - xx); |
---|
520 | } |
---|
521 | xx = nextInOffset; |
---|
522 | |
---|
523 | // Update for next run |
---|
524 | inLength = gridIn->getNextLength(); |
---|
525 | inType = gridIn->getRunType(&inLength); |
---|
526 | |
---|
527 | if (inType == OccGridRLE::END_OF_RUN) { |
---|
528 | break; |
---|
529 | } |
---|
530 | |
---|
531 | nextInOffset += inLength; |
---|
532 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
533 | inElement = gridIn->getNextElement(); |
---|
534 | } |
---|
535 | } |
---|
536 | |
---|
537 | // Seal off the old run |
---|
538 | sealScanline(); |
---|
539 | } |
---|
540 | } |
---|
541 | if (!Quiet) |
---|
542 | printf("\n"); |
---|
543 | } |
---|
544 | |
---|
545 | |
---|
546 | |
---|
547 | static void |
---|
548 | initScanline(OccGridRLE *grid, int yy, int zz) |
---|
549 | { |
---|
550 | theOutGrid = grid; |
---|
551 | theOutGrid->allocNewRun(yy, zz); |
---|
552 | currentLength = 0; |
---|
553 | } |
---|
554 | |
---|
555 | |
---|
556 | static void |
---|
557 | appendScanlinePortion(OccGridRLE *inGrid, int inType, |
---|
558 | OccElement *inElement, RunLength length) |
---|
559 | { |
---|
560 | int i; |
---|
561 | |
---|
562 | if (currentType == inType && currentLength > 0) { |
---|
563 | |
---|
564 | if (currentType == OccGridRLE::CONSTANT_DATA) { |
---|
565 | |
---|
566 | if (inElement->value == lastElement.value && |
---|
567 | inElement->totalWeight == lastElement.totalWeight) { |
---|
568 | |
---|
569 | // If same constant then just increment length. |
---|
570 | currentLength += length; |
---|
571 | } else { |
---|
572 | |
---|
573 | // If different constants, then seal old run and start |
---|
574 | // a new one |
---|
575 | theOutGrid->setRunType(¤tLength, currentType); |
---|
576 | theOutGrid->putNextLength(currentLength); |
---|
577 | |
---|
578 | theOutGrid->putNextElement(inElement); |
---|
579 | lastElement = *inElement; |
---|
580 | currentLength = length; |
---|
581 | } |
---|
582 | } else { |
---|
583 | |
---|
584 | // If old and new are varying, then add on new data |
---|
585 | for (i = 0; i < length-1; i++) { |
---|
586 | theOutGrid->putNextElement(inGrid->getNextElement()); |
---|
587 | } |
---|
588 | lastElement = *(inGrid->getNextElement()); |
---|
589 | theOutGrid->putNextElement(&lastElement); |
---|
590 | currentLength += length; |
---|
591 | } |
---|
592 | } |
---|
593 | else { |
---|
594 | // If different, then seal up the old run and start a |
---|
595 | // new one |
---|
596 | |
---|
597 | if (currentLength > 0) { |
---|
598 | theOutGrid->setRunType(¤tLength, currentType); |
---|
599 | theOutGrid->putNextLength(currentLength); |
---|
600 | } |
---|
601 | |
---|
602 | if (inType == OccGridRLE::CONSTANT_DATA) { |
---|
603 | theOutGrid->putNextElement(inElement); |
---|
604 | lastElement = *inElement; |
---|
605 | } else { |
---|
606 | for (i = 0; i < length-1; i++) { |
---|
607 | theOutGrid->putNextElement(inGrid->getNextElement()); |
---|
608 | } |
---|
609 | lastElement = *(inGrid->getNextElement()); |
---|
610 | theOutGrid->putNextElement(&lastElement); |
---|
611 | } |
---|
612 | currentLength = length; |
---|
613 | currentType = inType; |
---|
614 | } |
---|
615 | } |
---|
616 | |
---|
617 | |
---|
618 | static void |
---|
619 | appendScanline(OccElement *inElement) |
---|
620 | { |
---|
621 | int i, inType; |
---|
622 | |
---|
623 | inType = theOutGrid->decideType(inElement); |
---|
624 | if (currentType == inType && currentLength > 0) { |
---|
625 | if (currentType == OccGridRLE::CONSTANT_DATA) { |
---|
626 | if (inElement->value == lastElement.value && |
---|
627 | inElement->totalWeight == lastElement.totalWeight) { |
---|
628 | |
---|
629 | // If same constant then just increment length. |
---|
630 | currentLength++; |
---|
631 | } else { |
---|
632 | |
---|
633 | // If different constants, then seal old run and start |
---|
634 | // a new one |
---|
635 | theOutGrid->setRunType(¤tLength, currentType); |
---|
636 | theOutGrid->putNextLength(currentLength); |
---|
637 | theOutGrid->putNextElement(inElement); |
---|
638 | lastElement = *inElement; |
---|
639 | currentLength = 1; |
---|
640 | } |
---|
641 | } else { |
---|
642 | |
---|
643 | // If old and new are varying, then add on new data |
---|
644 | theOutGrid->putNextElement(inElement); |
---|
645 | lastElement = *inElement; |
---|
646 | currentLength++; |
---|
647 | } |
---|
648 | } |
---|
649 | else { |
---|
650 | // If different, then seal up the old run and start a |
---|
651 | // new one |
---|
652 | |
---|
653 | if (currentLength > 0) { |
---|
654 | theOutGrid->setRunType(¤tLength, currentType); |
---|
655 | theOutGrid->putNextLength(currentLength); |
---|
656 | } |
---|
657 | |
---|
658 | lastElement = *inElement; |
---|
659 | theOutGrid->putNextElement(inElement); |
---|
660 | currentLength = 1; |
---|
661 | currentType = inType; |
---|
662 | } |
---|
663 | } |
---|
664 | |
---|
665 | |
---|
666 | static void |
---|
667 | sealScanline() |
---|
668 | { |
---|
669 | theOutGrid->setRunType(¤tLength, currentType); |
---|
670 | theOutGrid->putNextLength(currentLength); |
---|
671 | theOutGrid->putNextLength(OccGridRLE::END_OF_RUN); |
---|
672 | } |
---|
673 | |
---|
674 | |
---|
675 | void |
---|
676 | scanConvertTreeDragTails(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
677 | OrthoShear *shear, DepthMap *depthMap) |
---|
678 | { |
---|
679 | float xOff, yOff, res, depth, confidence; |
---|
680 | OccElement *buf, *scanline; |
---|
681 | int xx,yy,zz; |
---|
682 | int *depthRuns, numRuns, oldNumRuns; |
---|
683 | float zmin, zmax, xNewOff; |
---|
684 | int xmin, xmax; |
---|
685 | OccScanlineRLE *rleScanline; |
---|
686 | |
---|
687 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
688 | |
---|
689 | res = gridIn->resolution; |
---|
690 | |
---|
691 | gridOut->copyParams(gridIn); |
---|
692 | |
---|
693 | gridOut->reset(); |
---|
694 | |
---|
695 | for (zz = 0; zz < gridIn->zdim; zz++) { |
---|
696 | if (!Quiet) |
---|
697 | printf("\rTraversing slice %d of %d.", zz, gridIn->zdim-1); |
---|
698 | fflush(stdout); |
---|
699 | |
---|
700 | // Something is really backwards here! |
---|
701 | |
---|
702 | /* |
---|
703 | zmin = gridIn->sliceOrigins[zz][2] + C5/sampleSpacing; |
---|
704 | zmax = gridIn->sliceOrigins[zz][2] + 2*C1/sampleSpacing; |
---|
705 | */ |
---|
706 | zmin = gridIn->sliceOrigins[zz][2] - C1/sampleSpacing; |
---|
707 | zmax = gridIn->sliceOrigins[zz][2] - C5/sampleSpacing; |
---|
708 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
709 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
710 | |
---|
711 | // Points near the surface |
---|
712 | depthRuns = depthMap->getDepthRuns(yOff, zmin, zmax, &numRuns); |
---|
713 | if (numRuns != 0) { |
---|
714 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
715 | scanline = gridIn->getScanline(yy,zz); |
---|
716 | for (int i = 0; i < numRuns; i++) { |
---|
717 | xmin = int(depthRuns[2*i] - xOff); |
---|
718 | xmin = MAX(xmin, 0); |
---|
719 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
720 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
721 | xNewOff = xOff + xmin; |
---|
722 | buf = scanline + xmin; |
---|
723 | for (xx = xmin; xx <= xmax; xx++, xNewOff++, buf++) { |
---|
724 | resampleForCarving(depthMap, xNewOff, yOff, |
---|
725 | &depth, &confidence); |
---|
726 | updateCellForCarving(gridIn->sliceOrigins[zz][2], |
---|
727 | depth, |
---|
728 | sampleSpacing, confidence, buf); |
---|
729 | } |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | oldNumRuns = numRuns; |
---|
734 | |
---|
735 | // Points in front of the surface |
---|
736 | depthRuns = depthMap->getDepthRunsUpperBound |
---|
737 | (yOff, zmin, &numRuns); |
---|
738 | |
---|
739 | int skip = 0; |
---|
740 | if (oldNumRuns == 0 && numRuns == 0) { |
---|
741 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
742 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
743 | skip = 1; |
---|
744 | } else if (numRuns == 0) { |
---|
745 | gridOut->putScanline(scanline,yy,zz); |
---|
746 | skip = 1; |
---|
747 | } else if (oldNumRuns == 0) { |
---|
748 | scanline = gridIn->getScanline(yy,zz); |
---|
749 | skip = 0; |
---|
750 | } |
---|
751 | |
---|
752 | if (!skip) { |
---|
753 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
754 | for (int i = 0; i < numRuns; i++) { |
---|
755 | xmin = int(depthRuns[2*i] - xOff); |
---|
756 | xmin = MAX(xmin, 0); |
---|
757 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
758 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
759 | buf = scanline + xmin; |
---|
760 | for (xx = xmin; xx <= xmax; xx++, buf++) { |
---|
761 | if (buf->totalWeight == 0) { |
---|
762 | buf->value = 0; |
---|
763 | } |
---|
764 | } |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | oldNumRuns += numRuns; |
---|
769 | |
---|
770 | depthRuns = depthMap->getDepthRunsEdges |
---|
771 | (yOff, zmin, &numRuns); |
---|
772 | |
---|
773 | if (oldNumRuns == 0 && numRuns == 0) { |
---|
774 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
775 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
776 | continue; |
---|
777 | } else if (numRuns == 0) { |
---|
778 | gridOut->putScanline(scanline,yy,zz); |
---|
779 | continue; |
---|
780 | } else if (oldNumRuns == 0) { |
---|
781 | scanline = gridIn->getScanline(yy,zz); |
---|
782 | } |
---|
783 | |
---|
784 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
785 | for (int i = 0; i < numRuns; i++) { |
---|
786 | xmin = int(depthRuns[2*i] - xOff); |
---|
787 | xmin = MAX(xmin, 0); |
---|
788 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
789 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
790 | xNewOff = xOff + xmin; |
---|
791 | buf = scanline + xmin; |
---|
792 | for (xx = xmin; xx <= xmax; xx++, xNewOff++, buf++) { |
---|
793 | resampleForCarving(depthMap, xNewOff, yOff, |
---|
794 | &depth, &confidence); |
---|
795 | updateCellForCarving(gridIn->sliceOrigins[zz][2], depth, |
---|
796 | sampleSpacing, confidence, buf); |
---|
797 | } |
---|
798 | } |
---|
799 | |
---|
800 | gridOut->putScanline(scanline,yy,zz); |
---|
801 | } |
---|
802 | } |
---|
803 | if (!Quiet) |
---|
804 | printf("\n"); |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | |
---|
809 | int |
---|
810 | scanConvertTreeDragTailsOneLine(OccGridRLE *gridIn, OccGridRLE *gridOut, |
---|
811 | OrthoShear *shear, DepthMap *depthMap, |
---|
812 | int line) |
---|
813 | { |
---|
814 | float xOff, yOff, res, depth, confidence; |
---|
815 | OccElement *buf, *scanline; |
---|
816 | int xx,yy,zz; |
---|
817 | int nothingNew; |
---|
818 | int *depthRuns, numRuns, oldNumRuns; |
---|
819 | float zmin, zmax, xNewOff; |
---|
820 | int xmin, xmax; |
---|
821 | OccScanlineRLE *rleScanline; |
---|
822 | |
---|
823 | float sampleSpacing = sqrt(1 + shear->sx*shear->sx + shear->sy*shear->sy); |
---|
824 | |
---|
825 | res = gridIn->resolution; |
---|
826 | |
---|
827 | gridOut->copyParams(gridIn); |
---|
828 | |
---|
829 | gridOut->reset(); |
---|
830 | |
---|
831 | for (zz = 0; zz < line; zz++) { |
---|
832 | for (yy = 0; yy < gridIn->ydim; yy++) { |
---|
833 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
834 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
835 | } |
---|
836 | } |
---|
837 | |
---|
838 | for (zz = line; zz < line+1; zz++) { |
---|
839 | |
---|
840 | // Something is really backwards here! |
---|
841 | |
---|
842 | /* |
---|
843 | zmin = gridIn->sliceOrigins[zz][2] + C5/sampleSpacing; |
---|
844 | zmax = gridIn->sliceOrigins[zz][2] + 2*C1/sampleSpacing; |
---|
845 | */ |
---|
846 | zmin = gridIn->sliceOrigins[zz][2] - C1/sampleSpacing; |
---|
847 | zmax = gridIn->sliceOrigins[zz][2] - C5/sampleSpacing; |
---|
848 | yOff = (gridIn->sliceOrigins[zz][1] - depthMap->origin[1])/res; |
---|
849 | |
---|
850 | for (yy = 0; yy < gridIn->ydim; yy++, yOff++) { |
---|
851 | |
---|
852 | // Points near the surface |
---|
853 | depthRuns = depthMap->getDepthRuns(yOff, zmin, zmax, &numRuns); |
---|
854 | if (numRuns != 0) { |
---|
855 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
856 | scanline = gridIn->getScanline(yy,zz); |
---|
857 | for (int i = 0; i < numRuns; i++) { |
---|
858 | xmin = int(depthRuns[2*i] - xOff); |
---|
859 | xmin = MAX(xmin, 0); |
---|
860 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
861 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
862 | xNewOff = xOff + xmin; |
---|
863 | buf = scanline + xmin; |
---|
864 | for (xx = xmin; xx <= xmax; xx++, xNewOff++, buf++) { |
---|
865 | resampleForCarving(depthMap, xNewOff, yOff, |
---|
866 | &depth, &confidence); |
---|
867 | updateCellForCarving(gridIn->sliceOrigins[zz][2], |
---|
868 | depth, |
---|
869 | sampleSpacing, confidence, buf); |
---|
870 | } |
---|
871 | } |
---|
872 | } |
---|
873 | |
---|
874 | oldNumRuns = numRuns; |
---|
875 | |
---|
876 | // Points in front of the surface |
---|
877 | depthRuns = depthMap->getDepthRunsUpperBound |
---|
878 | (yOff, zmin, &numRuns); |
---|
879 | |
---|
880 | int skip = 0; |
---|
881 | nothingNew = 0; |
---|
882 | if (oldNumRuns == 0 && numRuns == 0) { |
---|
883 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
884 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
885 | nothingNew = 1; |
---|
886 | skip = 1; |
---|
887 | } else if (numRuns == 0) { |
---|
888 | gridOut->putScanline(scanline,yy,zz); |
---|
889 | skip = 1; |
---|
890 | } else if (oldNumRuns == 0) { |
---|
891 | scanline = gridIn->getScanline(yy,zz); |
---|
892 | skip = 0; |
---|
893 | } |
---|
894 | |
---|
895 | if (!skip) { |
---|
896 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
897 | for (int i = 0; i < numRuns; i++) { |
---|
898 | xmin = int(depthRuns[2*i] - xOff); |
---|
899 | xmin = MAX(xmin, 0); |
---|
900 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
901 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
902 | buf = scanline + xmin; |
---|
903 | for (xx = xmin; xx <= xmax; xx++, buf++) { |
---|
904 | if (buf->totalWeight == 0) { |
---|
905 | buf->value = 0; |
---|
906 | } |
---|
907 | } |
---|
908 | } |
---|
909 | } |
---|
910 | |
---|
911 | oldNumRuns += numRuns; |
---|
912 | |
---|
913 | depthRuns = depthMap->getDepthRunsEdges |
---|
914 | (yOff, zmin, &numRuns); |
---|
915 | |
---|
916 | if (oldNumRuns == 0 && numRuns == 0) { |
---|
917 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
918 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
919 | if (nothingNew) |
---|
920 | nothingNew = 1; |
---|
921 | else |
---|
922 | nothingNew = 0; |
---|
923 | continue; |
---|
924 | } else if (numRuns == 0) { |
---|
925 | gridOut->putScanline(scanline,yy,zz); |
---|
926 | continue; |
---|
927 | } else if (oldNumRuns == 0) { |
---|
928 | scanline = gridIn->getScanline(yy,zz); |
---|
929 | } |
---|
930 | |
---|
931 | xOff = (gridIn->sliceOrigins[zz][0] - depthMap->origin[0])/res; |
---|
932 | for (int i = 0; i < numRuns; i++) { |
---|
933 | xmin = int(depthRuns[2*i] - xOff); |
---|
934 | xmin = MAX(xmin, 0); |
---|
935 | xmax = int(ceil(depthRuns[2*i+1] - xOff)); |
---|
936 | xmax = MIN(xmax, gridIn->xdim-1); |
---|
937 | xNewOff = xOff + xmin; |
---|
938 | buf = scanline + xmin; |
---|
939 | for (xx = xmin; xx <= xmax; xx++, xNewOff++, buf++) { |
---|
940 | resampleForCarving(depthMap, xNewOff, yOff, |
---|
941 | &depth, &confidence); |
---|
942 | updateCellForCarving(gridIn->sliceOrigins[zz][2], depth, |
---|
943 | sampleSpacing, confidence, buf); |
---|
944 | } |
---|
945 | } |
---|
946 | |
---|
947 | gridOut->putScanline(scanline,yy,zz); |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | for (zz = line+1; zz < gridIn->zdim; zz++) { |
---|
952 | for (yy = 0; yy < gridIn->ydim; yy++) { |
---|
953 | rleScanline = gridIn->getRLEScanline(yy, zz); |
---|
954 | gridOut->copyScanline(rleScanline, yy, zz); |
---|
955 | } |
---|
956 | } |
---|
957 | |
---|
958 | return !nothingNew; |
---|
959 | } |
---|
960 | |
---|