-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMScan.cc
2707 lines (2253 loc) · 70.2 KB
/
MMScan.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//############################################################
//
// MMScan.cc
//
// Jeremy Ginsberg
//
// Stores all information pertaining to a 3DScanners ModelMaker H
// scan "conglomerate"... this contains many small scan fragments,
// each of which can be characterized by a single viewing direction.
//
// Data structures revamped starting rev1.35 to better encapsulate the
// idea of a collection of scan fragments.
//
//
// $Date: 2001/07/02 17:56:26 $
// $Revision: 1.66 $
//
//############################################################
#include "MMScan.h"
#include "MeshTransport.h"
#include "TriMeshUtils.h"
#include "KDindtree.h"
#include "ColorUtils.h"
#include "Progress.h"
#include "VertexFilter.h"
#include "Random.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "sys/stat.h"
#include "plvScene.h"
#define STRIPE_PTS 312 // max number of points in a given stripe
#define SUBSAMPS 6 // number of resolutions to create
MMScan::MMScan()
{
haveScanDir = false;
isDirty_mem = false;
isDirty_disk = false;
scans.clear();
}
MMScan::~MMScan()
{
while (kdtree.size()) {
delete (kdtree.back());
kdtree.pop_back();
}
scans.clear();
}
static bool
getXformFilename (const char* meshName, char* xfName)
{
if (meshName == NULL || meshName[0] == 0)
return FALSE;
strcpy (xfName, meshName);
char* pExt = strrchr (xfName, '.');
if (pExt == NULL)
pExt = xfName + strlen (xfName);
strcpy (pExt, ".xf");
return TRUE;
}
void
MMScan::absorb_xforms(char *basename)
{
char buffer[PATH_MAX];
strcpy(buffer, basename);
char *pExt = strrchr(buffer, '.');
if (pExt == NULL)
pExt = buffer + strlen(buffer);
for (int i = 0; i < scans.size(); i++) {
char temp[15];
if (i < 10) sprintf(temp, "_00%d.xf", i);
else if (i < 100) sprintf(temp, "_0%d.xf", i);
else sprintf(temp, "_%d.xf", i);
strcpy(pExt, temp);
TbObj tempObj;
Xform<float> newXF;
tempObj.readXform(buffer);
newXF = tempObj.getXform();
cout << "Filename sought: " << buffer << endl;
cout << newXF << endl;
for (int j = 0; j < scans[i].meshes.size(); j++) {
for (int k = 0; k < scans[i].meshes[j].vtx.size(); k++) {
newXF(scans[i].meshes[j].vtx[k]);
}
}
}
computeBBox();
}
int
MMScan::write_xform_ply(int whichRes, char *directory,
crope &confLines, bool applyXform)
{
if (!load_resolution(whichRes)) {
cerr << "MMScan::write_xform_ply couldn't load desired resolution"
<< endl;
return 0;
}
if (scans[0].meshes[whichRes].tris.size() == 0) {
// we don't have triangles; only t-strips... do something about this
for (int i = 0; i < scans.size(); i++) {
mmResLevel& res = scans[i].meshes[whichRes];
triangulate(&scans[0] + i, &res, 1 << whichRes);
}
}
int numFragsSaved = 0;
// get basename for ply files
char buffer[PATH_MAX], *slash;
strcpy(buffer, get_name().c_str());
for (slash = buffer; *slash; slash++)
if (*slash == '/') *slash = '_';
char *pExt = strrchr(buffer, '.');
if (pExt == NULL)
pExt = buffer + strlen(buffer);
/*
okay so we don't really want confidence
// build confidence if we don't have it
if (scans[0].meshes[0].confidence.size() == 0)
calcConfidence();
*/
vector<Pnt3> scanVtx;
Pnt3 axis;
float rotAngle, quat[4];
Xform<float> xf, invXF;
// stuff for global alignment
Xform<float> meshXF;
Xform<float> comboXF;
float comboTrans[3];
meshXF = this->getXform();
// set up conf strings
//confLines = new char *[scans.size()];
// save each scan to a separate file
for(int i = 0; i < scans.size(); i++) {
// discard absurdly tiny meshes
if (scans[i].stripes.size() < 3) {
cout << "DISCARDING MESH WITH < 3 STRIPES " << endl;
}
else {
//cout << "Scan # " << i << ":" << endl;
if (i < 10) sprintf(pExt, "_00%d.ply", i);
else if (i < 100) sprintf(pExt, "_0%d.ply", i);
else sprintf(pExt, "_%d.ply", i);
#if 0
// An effort to combat flipped view direction
Pnt3 nrm(0,0,0);
for(int j = 0; j < scans[i].meshes[whichRes].nrm.size(); j++) {
nrm[0] += scans[i].meshes[whichRes].nrm[j*3+0];
nrm[1] += scans[i].meshes[whichRes].nrm[j*3+1];
nrm[2] += scans[i].meshes[whichRes].nrm[j*3+2];
}
if (dot(nrm, scans[i].viewDir) < 0)
scans[i].viewDir = -scans[i].viewDir;
#endif
// calculate transformation necessary to align viewDir with Z-axis
xf.identity();
axis.set(-1 * scans[i].viewDir[1], scans[i].viewDir[0], 0);
axis.normalize();
rotAngle = M_PI + acos(-1 * scans[i].viewDir[2]);
//cout << "rotating " << rotAngle << " rads around " << axis << endl;
xf.rot(rotAngle, axis[0], axis[1], axis[2]);
//cout << scans[i].num_vertices(whichRes) << " points" << endl;
invXF = xf;
invXF.invert();
comboXF = meshXF * invXF;
comboXF.toQuaternion(quat);
comboXF.getTranslation(comboTrans);
if (applyXform) {
char line[PATH_MAX + 100];
// save transformation to conf string
sprintf(line, "{bmesh %s %g %g %g %g %g %g %g}",
buffer, comboTrans[0], comboTrans[1], comboTrans[2],
-1 * quat[1], -1 * quat[2], -1 * quat[3], quat[0]);
//confLines[numFragsSaved] = new char[strlen(buffer)];
//strcpy(confLines[numFragsSaved], buffer);
if (!confLines.empty()) {
confLines += " ";
}
confLines += line;
}
scanVtx.reserve(scans[i].meshes[whichRes].vtx.size());
// apply transformation to points
for(int k = 0; k < scans[i].meshes[whichRes].vtx.size(); k++) {
Pnt3 pt = scans[i].meshes[whichRes].vtx[k];
if (applyXform)
xf(pt);
scanVtx.push_back(pt);
}
char full_path[PATH_MAX];
sprintf(full_path, "%s/%s", directory, buffer);
write_ply_file(full_path, scanVtx,
scans[i].meshes[whichRes].tris, false
);
scanVtx.clear();
// write xf
strcpy (full_path + strlen(full_path) - 3, "xf");
cout << "Writing " << full_path << " ... " << flush;
ofstream xffile (full_path);
if (applyXform)
xffile << comboXF;
else
xffile << meshXF;
if (xffile.fail()) {
cerr << "Scan " << buffer
<< " failed to write xform!" << endl;
break;
}
numFragsSaved++;
}
}
return numFragsSaved;
}
MeshTransport*
MMScan::mesh(bool perVertex, bool stripped,
ColorSource color, int colorSize)
{
if (stripped && !perVertex) {
cerr << "No t-strips without per-vertex properties";
return NULL;
}
int resNum = current_resolution_index();
if (!resolutions[resNum].in_memory) load_resolution(resNum);
int numVerts = num_vertices(resNum);
MeshTransport* mt = new MeshTransport;
for (int i = 0; i < scans.size(); i++) {
if (scans[i].isVisible) {
mmResLevel *res = &(scans[i].meshes[0]) + resNum;
mt->setVtx (&res->vtx, MeshTransport::share);
if (perVertex) {
mt->setNrm (&res->nrm, MeshTransport::share);
} else {
return NULL;
/* no support now
if (!res->triNrm.size()) {
if (!res->tris.size()) strips_to_tris(res->tstrips, res->tris);
calcTriNormals (res->tris, res->triNrm);
}
mt->setNrm (&res->triNrm, MeshTransport::share);
*/
}
if (stripped) {
if (!res->tstrips.size()) {
int subSamp = 1 << resNum;
make_tstrips(&scans[0] + i, res, subSamp);
}
mt->setTris (&res->tstrips, MeshTransport::share);
} else { // regular triangles
if (!res->tris.size() && res->tstrips.size())
strips_to_tris(res->tstrips, res->tris);
mt->setTris (&res->tris, MeshTransport::share);
}
if (color != colorNone)
setMTColor (mt, i, perVertex, color, colorSize);
}
}
return mt;
}
void
MMScan::setMTColor (MeshTransport* mt, int iScan, bool perVertex,
ColorSource source, int colorsize)
{
int resNum = current_resolution_index();
mmResLevel* res = &(scans[iScan].meshes[resNum]);
/* don't know how to check ahead of time yet
if ((!conf_col) && (vtxIntensity.size() == 0) && (colorsize != 3))
return false; // if we have no intensity data for some unknown reason
// and we're not looking for per-scan color or confidence
*/
vector<uchar>* colors = new vector<uchar>;
mt->setColor (colors, MeshTransport::steal);
if (perVertex)
colors->reserve (res->vtx.size() * colorsize);
else return;
/* no support now
colors->reserve (num_tris(resNum) / 3 * colorsize);
*/
int j;
switch (source) {
case colorConf:
// confidence zone
if (perVertex) {
// per-vertex confidence zone
if (res->confidence.size() == 0) return;
for (j = 0; j < res->confidence.size(); j++)
pushColor(*colors, colorsize, res->confidence[j]);
}
else {
return;
/* no support now
// per-face confidence zone
if (res->confidence.size() == 0) return;
for (j = 0; j < res->tris.size(); j+=3)
pushColor(*colors, colorsize,
res->confidence[res->tris[j+0]],
res->confidence[res->tris[j+1]],
res->confidence[res->tris[j+2]]);
*/
}
break; // end confidence color zone
case colorTrue:
// MMScan doesn't have true-color information, so it interprets this
// flag as a request for per-fragment coloring.
// begin per-scan coloring to replace diffuse color
pushColor(*colors, colorsize, scans[iScan].falseColor);
break;
case colorIntensity:
default:
// intensity coloring
// on filtered vertex sets, i'm not sure how the intensity
// is going to be derived, so check and see where that data
// is coming from if you're concerned.
if (perVertex) {
// per-vertex intensities
if (res->intensity.size() == 0) return;
for (j = 0; j < res->vtx.size(); j++)
pushColor(*colors, colorsize, res->intensity[j]);
}
else {
// per-face intensities
return;
/* no support now
if (res->intensity.size() == 0) return;
for (j = 0; j < res->tris.size(); j+=3)
pushColor(*colors, colorsize,
res->intensity[res->tris[j+0]],
res->intensity[res->tris[j+1]],
res->intensity[res->tris[j+2]]);
*/
} // end intensity-coloring zone
} // end switch
}
crope
MMScan::getInfo(void)
{
char buf[1000];
sprintf(buf, "MMScan: %d scans, %d stripes, %d points,"
" %d resolutions\n\n",
num_scans(), num_stripes(), num_vertices(0), num_resolutions());
return crope (buf) + RigidScan::getInfo();
}
int
MMScan::num_tris(int res)
{
if (scans.size() == 0) return 0;
int numResolutions = scans[0].meshes.size();
if (numResolutions == 0) return 0;
if ((res >= numResolutions) || (res < 0)) return 0;
int numTris = 0;
for (int i = 0; i < scans.size(); i++) {
if (scans[i].isVisible)
numTris += scans[i].meshes[res].tris.size() / 3;
}
return numTris;
}
int
MMScan::num_stripes()
{
int sum = 0;
for (int i = 0; i < scans.size(); i++) {
sum += scans[i].stripes.size();
}
return sum;
}
int
MMScan::num_vertices(void)
{
int total = 0;
int resNum = current_resolution_index();
for (int i = 0; i < scans.size(); i++) {
if (scans[i].isVisible) {
if ((resNum < scans[i].meshes.size()) && (resNum >= 0))
total += scans[i].meshes[resNum].vtx.size();
}
}
return total;
}
int
MMScan::num_vertices(int resNum)
{
int total = 0;
for (int i = 0; i < scans.size(); i++) {
if (scans[i].isVisible)
if ((resNum < scans[i].meshes.size()) && (resNum >= 0))
total += scans[i].meshes[resNum].vtx.size();
}
return total;
}
int
MMScan::create_resolution_absolute(int budget, Decimator dec)
{
if (budget == 0) return 0;
int resNum = 0; // always decimate from the master copy, at least for now
int totalTris = num_tris(resNum);
int decimatedTris = 0;
mmResLevel *resolutions = new mmResLevel[scans.size()];
mmResLevel *newRes;
cerr << "Target size = " << budget << ": decimating... ";
for (int i = 0; i < scans.size(); i++) {
// check to see if we have triangles
mmResLevel *res = &(scans[i].meshes[0]) + resNum;
if (!res->tris.size()) strips_to_tris(res->tstrips, res->tris);
cerr << "mesh tris = " << scans[i].meshes[resNum].num_tris() << endl;;
int fragBudget = budget * ((double)(scans[i].meshes[resNum].num_tris())
/ totalTris);
fprintf(stderr, "Mesh #%d:\t%d tris...\t", i, fragBudget);
newRes = resolutions + i;
quadric_simplify(scans[i].meshes[0].vtx, scans[i].meshes[0].tris,
newRes->vtx, newRes->tris,
fragBudget, PLACE_OPTIMAL, 0, 1000);
if (newRes->tris.size()/3 != fragBudget)
fprintf(stderr, "ended up with %ld tris\n", newRes->tris.size()/3);
else fprintf(stderr, "done\n");
decimatedTris += newRes->tris.size() / 3;
getVertexNormals(newRes->vtx, newRes->tris, false, newRes->nrm, true);
}
insert_resolution(decimatedTris, get_name(), true, true);
int iPos = findLevelForRes(decimatedTris);
for (int i = 0; i < scans.size(); i++) {
// STL Update
scans[i].meshes.insert(scans[i].meshes.begin() + iPos, resolutions[i]);
}
delete[] resolutions;
return decimatedTris;
}
void
MMScan::computeBBox (void)
{
bbox.clear();
int resNum = current_resolution_index();
for (int k = 0; k < scans.size(); k++) {
if (scans[k].isVisible) {
for (int i = 0; i < scans[k].meshes[resNum].num_vertices(); i++) {
bbox.add(scans[k].meshes[resNum].vtx[i]);
}
}
}
if (scans.size() == 0) {
rot_ctr = Pnt3();
} else {
rot_ctr = bbox.center();
}
}
void
MMScan::flipNormals (void)
{
for (int i = 0; i < scans.size(); i++) {
for (int j = 0; j < scans[i].meshes.size(); j++) {
// triangle normals
for (int k = 0; k < scans[i].meshes[j].tris.size(); k+=3) {
swap(scans[i].meshes[j].tris[k], scans[i].meshes[j].tris[k+1]);
}
// tstrip normals
flip_tris(scans[i].meshes[j].tstrips, true);
// per-vertex normals
for (int k = 0; k < scans[i].meshes[j].nrm.size(); k++) {
scans[i].meshes[j].nrm[k] *= -1;
}
}
for (int j = 0; j < scans[i].stripes.size(); j++) {
// this isn't completely effective; causes z-axis align problems
scans[i].stripes[j].sensorVec *= -1;
if (scans[i].stripes[j].scanDir == 1) scans[i].stripes[j].scanDir = 2;
else scans[i].stripes[j].scanDir = 1;
}
scans[i].viewDir *= -1;
}
}
void
MMScan::flipNormals (int scanNum)
{
for (int j = 0; j < scans[scanNum].meshes.size(); j++) {
// triangle normals
for (int k = 0; k < scans[scanNum].meshes[j].tris.size(); k+=3) {
swap(scans[scanNum].meshes[j].tris[k],
scans[scanNum].meshes[j].tris[k+1]);
}
// tstrip normals
flip_tris(scans[scanNum].meshes[j].tstrips, true);
// per-vertex normals
for (int k = 0; k < scans[scanNum].meshes[j].nrm.size(); k++) {
scans[scanNum].meshes[j].nrm[k] *= -1;
}
}
for (int j = 0; j < scans[scanNum].stripes.size(); j++) {
// sensor vectors (so that it will triangulate flipped next time
scans[scanNum].stripes[j].sensorVec *= -1;
if (scans[scanNum].stripes[j].scanDir == 1)
scans[scanNum].stripes[j].scanDir = 2;
else scans[scanNum].stripes[j].scanDir = 1;
}
scans[scanNum].viewDir *= -1;
}
bool
MMScan::isValidPoint(int scanNum, int stripeNum, int colNum)
// returns value of bitmap of specified stripe in specified column
// of a specified scan
{
if (scanNum >= scans.size()) return false;
if (stripeNum >= scans[scanNum].stripes.size()) return false;
if (colNum > STRIPE_PTS) return false;
return (scans[scanNum].stripes[stripeNum].validMap[colNum / 8]
& (1 << (colNum % 8)));
}
static void
addTri(vector<int> &tris, int v1, int v2, int v3, char scanDir)
{
// if it's normal or if we don't know the scan direction...
if ((scanDir == 1) || (scanDir == 0)) {
tris.push_back(v1);
tris.push_back(v2);
tris.push_back(v3);
}
// otherwise flip the beans out of it
else if (scanDir == 2) {
tris.push_back(v2);
tris.push_back(v1);
tris.push_back(v3);
}
}
void
MMScan::calcScanDir(void)
{
Pnt3 scanDir, stripeDir, crossDir;
int numPts, i, j, k;
// loop through all scans
for (k = 0; k < scans.size(); k++) {
if (scans[k].indices.size() == 0) calcIndices();
mmScanFrag *scan = &scans[0] + k;
mmResLevel *mesh = &(scan->meshes[0]);
// stop before last stripe since we look ahead one stripe
for (j = 0; j < scans[k].stripes.size() - 1; j++) {
scanDir.set(0,0,0);
stripeDir.set(0,0,0);
numPts = 0;
for (i = 0; i < STRIPE_PTS - 1; i++) {
// first get scan direction
if (isValidPoint(k,j,i) && isValidPoint(k,j+1,i)) {
scanDir += mesh->vtx[scan->indices[i + (j+1) * STRIPE_PTS]];
scanDir -= mesh->vtx[scan->indices[i + j * STRIPE_PTS]];
numPts++;
}
}
// then get stripe direction
stripeDir += mesh->vtx[scan->stripes[j].startIdx +
scan->stripes[j].numPoints - 1];
stripeDir -= mesh->vtx[scan->stripes[j].startIdx];
if (numPts == 0) {
// not enough information to determine scanning direction
scan->stripes[j].scanDir = 0;
cout << "couldn't obtain scan direction for a stripe" << endl;
}
else {
scanDir /= numPts;
scanDir.normalize();
stripeDir.normalize();
// figure out where the scan direction is expected to be
crossDir = cross(scan->stripes[j].sensorVec, stripeDir);
// if the actual doesn't match expected, we need to flip the triangles
// by marking the scanDir flag as 2 instead of 1
if (dot(crossDir, scanDir) > 0) scan->stripes[j].scanDir = 1;
else scan->stripes[j].scanDir = 2;
}
}
// here we are in the last stripe of a scan, so set it's direction to 0
scan->stripes[j].scanDir = 0;
}
/*
cout << "stripe " << j << endl << " stripe dir: " << stripeDir << endl;
cout << " sensor vec: " << stripes[j].sensorVec << endl;
cout << " cross vec: " << crossDir << endl;
cout << " scan dir: " << scanDir << endl;
cout << " num pts : " << numPts << endl;
if (stripes[j].scanDir == 1) cout << "normal" << endl;
else if (stripes[j].scanDir == 2) cout << "flipped" << endl;
else cout << "fucked!!" << endl;
*/
haveScanDir = true;
}
void
MMScan::calcIndices()
{
int i, j, k, count;
fprintf(stderr, "Calculating triangulation indices...\n");
for (k = 0; k < scans.size(); k++) {
mmScanFrag *scan = &(scans[0]) + k;
scan->indices.clear();
scan->indices.reserve(scan->stripes.size() * STRIPE_PTS);
count = 0;
for (i = 0; i < scan->stripes.size(); i++) {
for (j = 0; j < STRIPE_PTS; j++) {
if (isValidPoint(k,i,j)) {
scan->indices.push_back(count++);
}
else
scan->indices.push_back(-1);
}
}
}
}
/* not needed anymore
void
MMScan::calcTriNormals(const vector<int> &tris, vector<short> &triNrm)
{
int i, j, k;
for (i = 0; i < scans.size(); i++) {
for (j = 0; j < scans[i].meshes.size(); j++) {
mmResLevel *mesh = &(scans[i].meshes[j]);
mesh->triNrm.clear();
mesh->triNrm.reserve(3 * mesh->num_tris());
for(k = 0; k < mesh->tris.size(); k +=3) {
Pnt3 n;
n = mesh->nrm[mesh->tris[k+0]] +
mesh->nrm[mesh->tris[k+1]] +
mesh->nrm[mesh->tris[k+2]];
n /= 3.0;
n *= 32767;
mesh->triNrm.push_back(n[0]);
mesh->triNrm.push_back(n[1]);
mesh->triNrm.push_back(n[2]);
}
}
}
}
*/
static Random rnd;
bool
MMScan::load_resolution (int iRes)
{
if (resolutions[iRes].in_memory)
return true;
int subSamp = 1 << iRes;
Progress progress(scans.size(), "%s: build mesh",
name.c_str());
int numTris = 0;
cerr << name << ": build mesh (~" << resolutions[iRes].abs_resolution
<< ") from " << scans.size() << " fragments: " << flush;
int i;
for (i = 0; i < scans.size(); i++) {
progress.update(i);
mmScanFrag *scan = &scans[0] + i;
mmResLevel& res = scan->meshes[iRes];
triangulate(scan, &res, subSamp);
/* if (subSamp == 1) {
make_tstrips(scan, res.tstrips);
numTris += count_tris(res.tstrips);
}
else {
triangulate(scan, &res, subSamp);
tris_to_strips(res.vtx.size(), res.tris, res.tstrips);*/
res.nrm.reserve(scan->meshes[0].nrm.size() / subSamp);
getVertexNormals (res.vtx, res.tris, false, res.nrm);
numTris += res.tris.size()/3;
// }
}
cerr << " done." << endl;
progress.update(i);
resolutions[iRes].abs_resolution = numTris;
resolutions[iRes].in_memory = true;
return true;
}
void
MMScan::triangulate(mmScanFrag *scan, mmResLevel *res, int subSamp)
{
int i,j,ii,jj;
int in1,in2,in3,in4,vin1,vin2,vin3,vin4;
int count;
int numStripes = scan->stripes.size();
// clear the list of triangles
res->tris.clear();
// i don't know what ltMin does. kari+curless do it, so i will too
int ltMin = (numStripes-1) - ((numStripes-1)/subSamp)*subSamp;
if (scan->indices.size() == 0) calcIndices();
if (!haveScanDir) calcScanDir();
// create a list saying whether a vertex is going to be used
// int *vert_index = new int[vtx.size()];
// for (i = 0; i < vtx.size(); i++)
// vert_index[i] = -1;
int *vert_remap;
bool alreadySubSampled = (res->vtx.size() > 0);
if (subSamp != 1) {
// see which vertices will be used in the new mesh
vert_remap = new int[scan->num_vertices(0)];
if (!alreadySubSampled) {
res->vtx.reserve(scan->num_vertices(0) / subSamp);
res->confidence.reserve(scan->meshes[0].confidence.size() / subSamp);
res->intensity.reserve(scan->meshes[0].intensity.size() / subSamp);
}
count = 0;
for (j = ltMin; j < numStripes; j += subSamp) {
for (i = 0; i < STRIPE_PTS; i += subSamp) {
in1 = scan->indices[i + j * STRIPE_PTS];
if (in1 >= 0) {
// vert_index[in1] = count;
vert_remap[in1] = count;
if (!alreadySubSampled) {
res->vtx.push_back(scan->meshes[0].vtx[in1]);
if (scan->meshes[0].confidence.size())
res->confidence.push_back(scan->meshes[0].confidence[in1]);
if (scan->meshes[0].intensity.size())
res->intensity.push_back(scan->meshes[0].intensity[in1]);
}
count++;
}
}
}
}
else count = scan->num_vertices(0);
//if (subSamp == 1) fprintf(stderr, "%d verts... ", count);
int max_tris = count * 6;
res->tris.reserve(max_tris);
// create the triangles
for (j = ltMin; j < numStripes - subSamp; j += subSamp) {
// if (scan->stripes[j].scanDir != scan->stripes[j+subSamp].scanDir) {
// cout << "not triangulating across a gap" << endl;
// continue;
// }
for (i = 0; i < STRIPE_PTS - subSamp; i += subSamp) {
ii = (i + subSamp) % STRIPE_PTS;
jj = (j + subSamp) % numStripes;
// count the number of good vertices
// 2 3
// 1 4
in1 = scan->indices[ i + j * STRIPE_PTS];
in2 = scan->indices[ i + jj * STRIPE_PTS];
in3 = scan->indices[ii + jj * STRIPE_PTS];
in4 = scan->indices[ii + j * STRIPE_PTS];
count = (in1 >= 0) + (in2 >= 0) + (in3 >= 0) + (in4 >=0);
// note about vin_x vs. in_x :
// vin's should be used if new vertex lists are being generated.
// on subsamp == 1, we aren't remapping. otherwise, we are.
if (in1 >= 0) {
if (subSamp == 1) vin1 = in1;
else vin1 = vert_remap[in1];
}
if (in2 >= 0) {
if (subSamp == 1) vin2 = in2;
else vin2 = vert_remap[in2];
}
if (in3 >= 0) {
if (subSamp == 1) vin3 = in3;
else vin3 = vert_remap[in3];
}
if (in4 >= 0) {
if (subSamp == 1) vin4 = in4;
else vin4 = vert_remap[in4];
}
// these are squared distances, let's not forget, scaled by subSamp
float maxLen1 = 1.0 * 1.0 * subSamp * subSamp;
float maxLen2 = 1.25 * 1.25 * subSamp * subSamp;
if (count == 4) { // all 4 vertices okay, so make 2 tris
bool badTri = false;
// check to make sure that stripe edges aren't too long
if (dist2(res->vtx[vin2], res->vtx[vin3]) > maxLen1) badTri = true;
if (dist2(res->vtx[vin1], res->vtx[vin4]) > maxLen1) badTri = true;
// check to make sure that inter-stripe distances aren't too long
if (dist2(res->vtx[vin1], res->vtx[vin2]) > maxLen2) badTri = true;
if (dist2(res->vtx[vin3], res->vtx[vin4]) > maxLen2) badTri = true;
if (!badTri) {
// compute lengths of cross-edges
float len1 = dist(res->vtx[vin1], res->vtx[vin3]);
float len2 = dist(res->vtx[vin2], res->vtx[vin4]);
if (len1 < len2) {
addTri(res->tris, vin2, vin1, vin3, scan->stripes[j].scanDir);
addTri(res->tris, vin1, vin4, vin3, scan->stripes[j].scanDir);
} else {
addTri(res->tris, vin2, vin1, vin4, scan->stripes[j].scanDir);
addTri(res->tris, vin2, vin4, vin3, scan->stripes[j].scanDir);
}
}
}
else if (count == 3) { // only 3 vertices okay, so make 1 tri
if (in1 == -1) {
if ((dist2(res->vtx[vin2], res->vtx[vin3]) < maxLen1) &&
(dist2(res->vtx[vin3], res->vtx[vin4]) < maxLen2))
addTri(res->tris, vin2, vin4, vin3, scan->stripes[j].scanDir);
} else if (in2 == -1) {
if ((dist2(res->vtx[vin1], res->vtx[vin4]) < maxLen1) &&
(dist2(res->vtx[vin3], res->vtx[vin4]) < maxLen2))
addTri(res->tris, vin1, vin4, vin3, scan->stripes[j].scanDir);
} else if (in3 == -1) {
if ((dist2(res->vtx[vin1], res->vtx[vin4]) < maxLen1) &&
(dist2(res->vtx[vin1], res->vtx[vin2]) < maxLen2))
addTri(res->tris, vin2, vin1, vin4, scan->stripes[j].scanDir);
} else { // in4 == -1
if ((dist2(res->vtx[vin2], res->vtx[vin3]) < maxLen1) &&
(dist2(res->vtx[vin1], res->vtx[vin2]) < maxLen2))
addTri(res->tris, vin2, vin1, vin3, scan->stripes[j].scanDir);
}
}
}
}
// old solution before properly using vin's
/*
if (subSamp != 1) {
// re-map triangle indices
for (i = 0; i < res->tris.size(); i++)
res->tris[i] = vert_remap[res->tris[i]];
}
*/
if (subSamp != 1)
delete[] vert_remap;
// I originally called this with the last 2 parameters: 50, 8
// but that seems to be quite incorrect. changed on 10/19/98
// don't call anymore
// remove_stepedges(res->vtx, res->tris, 4, 90);
cerr << "." << flush;
}
void
MMScan::make_tstrips(mmScanFrag *scan, mmResLevel *res, int subSamp)
{
int i,j,ii,jj;
int in1,in2,in3,in4;
int vin1, vin2, vin3, vin4;
int count;
int numStripes = scan->stripes.size();
int numStrips = 0;
bool newStrip = true;
int inc, iStart, iFinish;
res->tstrips.clear();
// i don't know what ltMin does. kari+curless do it, so i will too
int ltMin = (numStripes-1) - ((numStripes-1)/subSamp)*subSamp;
// this flag is used to differentiate the 2 kinds of diagonals which occur
bool whichDiag = true;
if (scan->indices.size() == 0) calcIndices();
if (!haveScanDir) calcScanDir();
bool alreadySubSampled = (res->vtx.size() > 0);
int *vert_remap;
if (subSamp != 1) {
// see which vertices will be used in the new mesh
vert_remap = new int[scan->num_vertices(0)];
if (!alreadySubSampled) {
res->vtx.reserve(scan->num_vertices(0) / subSamp);
res->confidence.reserve(scan->meshes[0].confidence.size() / subSamp);
res->intensity.reserve(scan->meshes[0].intensity.size() / subSamp);
}
count = 0;
for (j = ltMin; j < numStripes; j += subSamp) {
for (i = 0; i < STRIPE_PTS; i += subSamp) {
in1 = scan->indices[i + j * STRIPE_PTS];