-
Notifications
You must be signed in to change notification settings - Fork 13
/
m3c2.cpp
1514 lines (1384 loc) · 79.7 KB
/
m3c2.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
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
//**********************************************************************
//* This file is a part of the CANUPO project, a set of programs for *
//* classifying automatically 3D point clouds according to the local *
//* multi-scale dimensionality at each point. *
//* *
//* Author & Copyright: Nicolas Brodu <nicolas.brodu@numerimoire.net> *
//* *
//* This project is free software; you can redistribute it and/or *
//* modify it under the terms of the GNU Lesser General Public *
//* License as published by the Free Software Foundation; either *
//* version 2.1 of the License, or (at your option) any later version. *
//* *
//* This library is distributed in the hope that it will be useful, *
//* but WITHOUT ANY WARRANTY; without even the implied warranty of *
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
//* Lesser General Public License for more details. *
//* *
//* You should have received a copy of the GNU Lesser General Public *
//* License along with this library; if not, write to the Free *
//* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
//* MA 02110-1301 USA *
//* *
//**********************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <functional>
#include <limits>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/tokenizer.hpp>
#include <boost/math/special_functions/erf.hpp>
#include <boost/math/distributions/normal.hpp>
#include <boost/format.hpp>
#define FLOAT_TYPE double
#include "points.hpp"
#include "svd.hpp"
#include <string.h>
#include <stdlib.h>
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace std;
using namespace boost;
const char* all_result_formats[] = {"diff", "dev1", "dev2", "shift1", "shift2", "c1", "c2", "n1", "n2", "sn1", "sn2", "np1", "np2", "diff_bsdev", "shift1_bsdev", "shift2_bsdev", "ksi1", "ksi2", "n1angle_bs", "n2angle_bs", "diff_ci_low", "diff_ci_high", "diff_sig", "normal_dev1", "normal_dev2", "ns1", "ns2", "c0"};
const int nresformats = 28;
const char* default_result_formats[] = {"c1","n1","diff","diff_sig"};
const int num_default_result_formats = 4;
int help(const char* errmsg = 0) {
cout << "\
m3c2 normal_scale(s) : [cylinder_base : [cylinder_length : ]] p1.xyz[:p1reduced.xyz] p2.xyz[:p2reduced.xyz] cores.xyz extpts.xyz result.txt[,format[:result2.txt,format...]] [opt_flags [extra_info]]\n\
input: normal_scale(s) # The scale at which to compute the normal. If multiple scales\n\
# are given the one at which the cloud looks most 2D is used.\n\
# The syntax minscale:increment:maxscale is also accepted.\n\
# Use : to indicate the end of the list of scales\n\
input: cylinder_base # Optional. If given, the search cylinder for projecting the core\n\
# points in the normal direction is build with this base diameter\n\
# Default is to use the minimal scale given above.\n\
input: cylinder_length # Optional. If given, the search cylinder for projecting the core\n\
# extends up to that distance in length.\n\
# Default is to use the maximal scale given above.\n\
input: p1.xyz # first whole raw point cloud to process\n\
input: p2.xyz # second whole raw point cloud to process, possibly the same as p1\n\
# if you only care for the normal computation and core point shifting.\n\
input: p1reduced.xyz # Optional: use this subsampled cloud for performing the normal\n\
# computations instead of p1.xyz. The projection is still performed\n\
# with the full-resolution cloud, but you may drastically accelerate\n\
# the computations by using a subsampled cloud if the scale at which\n\
# the normals are computed is very large.\n\
input: p2.xyz # second whole raw point cloud to process, possibly the same as p1\n\
# if you only care for the normal computation and core point shifting.\n\
input: p2reduced.xyz # Optional: See p1reduced.xyz.\n\
input: cores.xyz # points near which to do the computation. It is not necessary that these\n\
# points match entries in either cloud. A regular grid is OK for example.\n\
# You can also take exactly the same file, or put more core points than\n\
# data points, the core points need only lie in the same region as data.\n\
# Core points are shifted toward the surface along the local normal\n\
# before doing the difference. The goal is to monitor the surface\n\
# evolution between p1 and p2.\n\
input: extpts.xyz # set of points on the exterior of the objets in the scene, toward wich\n\
# to orient normals. The closest point in the set is used to orient the\n\
# normal a each core point. The exterior points shall be exterior to\n\
# both p1 and p2...\n\
outputs: result.txt[,v1,v2,v3:res2.txt,v4,...]\n\
# A file containing as many result lines as core points\n\
# Each line contains space separated output variables, that can be\n\
# specified in order by their names below (ex: c1,diff).\n\
# Several output files may be specified separated by ':',\n\
# each with their own optional comma separated variables\n\
# specification (see the syntax above)\n\
# The default is to use c1,n1,diff,diff_sig\n\
# Available output variables are:\n\
# - c1.x c1.y c1.z: core point coordinates, shifted to surface of p1.\n\
# The surface is defined by a mean position (default) or by a median position (option \"q\").\n\
# Use the format name \"c1\", all three entries are then given.\n\
# - c2.x c2.y c2.z: core point coordinates, shifted to surface of p2.\n\
# - c0.x c0.y c0.z: original core point coordinates.\n\
# - n1.x n1.y n1.z: surface normal vector coordinates for p1\n\
# Use the format name \"n1\", all three entries are then given.\n\
# - n2.x n2.y n2.z: surface normal vector coordinates for p2\n\
# - sn1 sn2: scales at which the normals were computed in p1 and p2\n\
# - ns1 ns2: number of points in the neighborhood sphere at the sn1,sn2 scale.\n\
# - np1 np2: number of points in the cylinders\n\
# - diff: point cloud difference. \n\
# value of the signed distance c2 - c1, possibly averaged\n\
# by the statistical bootstrapping technique.\n\
# - diff_ci_low: lower bound of the confidence interval for the diff values, at 95% confidence level (default, see the c and g flags). The default is to estimate the confidence interval using the Bias-Corrected-accelerated (BCa) technique when bootstrapping is active, or to use a normality assumption otherwise.\n\
# - diff_ci_high: higher bound of the confidence interval for the diff values. See diff_ci_low.\n\
# - diff_sig: A boolean (0 or 1) indicating whether the diff value is outside the confidence interval AND whether there were enough points for estimating that interval itself. See the c, g, and p flags).\n\
# - shift1 shift2: signed distance the core points were shifted along the normals. This value is possibly averaged by bootstrap.\n\
# - dev1 dev2: standard deviation of the distance between: the points\n\
# surrounding c1 and c2 in the cylinder; and the\n\
# \"surface\" planes defined by the normals at c1 and c2.\n\
# This value is possibly averaged by bootstrap.\n\
# When option \"q\" is activated the interquartile ranges are used instead of standard deviations.\n\
# - diff_bsdev: deviation of the diff value, estimated by bootstrapping.\n\
# - shift1_bsdev shift2_bsdev: deviation of the shift values, estimated by bootstrapping.\n\
# - normal_dev1 normal_dev2: standard deviation of the points around the plane corresponding to the normal, including all points within neighborhood at the normal scale sn1/sn2\n\
# - ksi1 ksi2: a shortcut for sn1/dev1 and sn2/dev2\n\
# - n1angle_bs n2angle_bs: average angle (in degrees) between the normals and their mean value during normal bootstrapping. This can be seen as a kind of mean angular deviation around the normal, and is an indicator of how the normal is stable at that point. Requires the \"n\" flag.\n\
input: opt_flags # Optional flags. Some may be combined together. Ex: \"hqb\".\n\
# Available flags are:\n\
# h: make the resulting normals purely Horizontal (null z component).\n\
# v: make the resulting normals purely Vertical (so, all normals are either +z or -z depending on the reference points).\n\
# 1: Shift the core point on both clouds using the normal computed on the first cloud\n\
# The default is to shift the core point for each cloud using the normal computed on that cloud.\n\
# 2: Shift the core point on both clouds using the normal computed on the second cloud.\n\
# m: Shift the core point on both clouds using the mean of both normals.\n\
# b: Number of bootstrap iterations for the standard deviation around the shifted position of the core points (default is 0, see the f flag). 0 disables this bootstrapping\n\
# n: Number of bootstrap iterations for the estimation of the normals. 0 (the default) disables this bootstrapping. This option is useless when the normal is fixed to be vertical.\n\
# c: Confidence interval (in %) for setting the significance bounds. Default is 95. This option is only effective when computing the diff_ci_xxx parameters.\n\
# p: Minimal number of points that shall be in the cylinders (the np1 and np2 values), below which the the diff is considered not significant (the diff_sig value is set to 0). Default is 10.\n\
# q: Use the interquartile range and the median instead of standard deviation and mean, in order to define the new core point. The confidence intervals, if requested, are computed using the quantiles set by the c flag, either on the bootstrapped distribution (if available) or on a distribution of combinations of diffs between the clouds (can actually be slower and less reliable than bootstrap, check it!)\n\
# e: provide the standard deviation of the Error on the point positions\n\
# in p1 and p2 as extra info. This is a parameter provided by the device\n\
# used for measuring p1 and p2. It is incorporated in the bootstrapping.\n\
# k: Value for the ksi parameter (default is 0) for selecting the scale at which the normal is computed. The smallest scale satisfying ksi > this_value will be used. See the paper for what ksi means. Use a value of 0 to disable this parameter. In that case, the scale at which the cloud looks most 2D is used instead. Note: The value of ksi computed at the normal selection stage may differ from the final value given by the ksi1/2 result specifiers in case of normal bootstrapping. Note 2: For contrieved geometries, it is a good idea to double-check the ksi values and the selected scales if something goes amiss (ksi may increase at small scales). Note3: Using this selection mode takes some extra cpu.\n\
# f: (default, no need to specify) Fast-but-not-too-wrong estimator for the confidence intervals. This is Fast-and-exact only when the same normal is used, when each cloud is totally independant, and the points distances to their planes are distributed according to a Gaussian in each cylinder. These assumptions may fail, in which case use either the bootstrap technique (recommended) or maintain a Gaussian assumption and allow for normals to differ (g experimental flag, not recommended)\n\
# g: EXPERIMENTAL. Assume a normal (Gaussian) distribution of the point distances around the mean shift(1/2) values for estimating the confidence interval of the diff values, but allow the normals to differ. The worst case relies on monte-carlo sampling of the joint distribution, which may be slower and less precise than boostrapping. This option dos not take into account the e flag.\n\
# w: show extra warnings.\n\
input: extra_info # Extra parameters for the \"e\", \"s\", \"b\", \"n\", \"c\", \"k\" and \"p\" flags,\n\
# given in the same order as these flags were specified.\n\
# Ex: m3c2 (all other opts) ehb 1e-2 1000\n\
# The flags are \"e\", \"h\" and \"b\". \"h\" has no extra parameter.\n\
# So, in this order: e=1e-2 and b=1000\n\
"<<endl;
if (errmsg) cout << "Error: " << errmsg << endl;
return 0;
}
void mean_dev(double* values, int num, double& mean, double& dev) {
if (num<1) {mean = dev = numeric_limits<double>::quiet_NaN(); return;}
double sum = 0, ssq = 0;
for (int i=0; i<num; ++i) {
sum += values[i];
ssq += values[i] * values[i];
}
mean = sum / num;
if (num>1) dev = sqrt( (ssq - mean*mean*num)/(num-1.0) );
else dev = 0;
}
double mean(double* values, int num) {
if (num<1) {return numeric_limits<double>::quiet_NaN();}
double sum = 0;
for (int i=0; i<num; ++i) sum += values[i];
return sum / num;
}
// median: common definition using mid-point average in the even case
double median(double* values, int num) {
if (num<1) return numeric_limits<double>::quiet_NaN();
int nd2 = num/2;
double med = values[nd2];
if (num%2==0) { // even case
med = (med + values[nd2-1]) * 0.5;
}
return med;
}
// interquartile range
// there are several ways to compute it, with no standard
// commonly accepted definition. Use that of mathworld
double interquartile(double* values, int num) {
int num_pts_each_half = (num+1)/2;
int offset_second_half = num/2;
double q1 = median(values, num_pts_each_half);
double q3 = median(values + offset_second_half, num_pts_each_half);
return q3 - q1;
}
vector<uint32_t> randtable;
int randtableidx = 0;
static const int rand_table_size = 0x8000; // 32768
void randinit() {
randtable.resize(rand_table_size);
boost::mt11213b rng;
for (uint32_t& x : randtable) x = (uint32_t)rng();
}
int randint(const int nint) {
randtableidx = (randtableidx+1) & 0x7FFF;
return randtable[randtableidx] % nint;
}
void resample(const vector<double>& original, vector<double>& resampled) {
const int nint = original.size();
for (double& sample : resampled) sample = original[randint(nint)];
}
inline double inverse_normal_cdf(double p) {
// use non-inf arithmetic, faster
if (p<=0) return -numeric_limits<double>::max();
if (p>=1) return numeric_limits<double>::max();
// try {
return 1.4142135623730950488016887242096981 * boost::math::erf_inv(2.*p-1.);
// } catch(...) {
// cout << "inverse_normal_cdf error: p=" << p << endl;
// return 0;
// }
}
inline double normal_cumulative(double x) {
return 0.5 * (1. + boost::math::erf(x*0.7071067811865475244008443621048490));
}
/*
boost::mt11213b rng;
void resample(const vector<double>& original, vector<double>& resampled) {
// resampling with replacement
boost::uniform_int<int> int_dist(0, original.size()-1);
boost::variate_generator<boost::mt11213b&, boost::uniform_int<int> > randint(rng, int_dist);
for (double& sample : resampled) sample = original[randint()];
}
*/
inline double nan_is_0(double x) {
return isfinite(x)?x:0;
}
int main(int argc, char** argv) {
if (argc<8) return help();
int separator = 0;
for (int i=1; i<argc; ++i) if (!strcmp(":",argv[i])) {
separator = i;
break;
}
if (separator==0) return help();
// get all unique scales from large to small, for later processing of neighborhoods
typedef set<double, greater<double> > ScaleSet;
ScaleSet scales;
for (int i=1; i<separator; ++i) {
// perhaps it has the minscale:increment:maxscale syntax
char* col1 = strchr(argv[i],':');
char* col2 = strrchr(argv[i],':');
if (col1==0 || col2==0 || col1==col2) {
double scale = atof(argv[i]);
if (scale<=0) return help("Invalid scale");
scales.insert(scale);
} else {
*col1++=0;
double minscale = atof(argv[i]);
*col2++=0;
double increment = atof(col1);
double maxscale = atof(col2);
if (minscale<=0 || maxscale<=0) return help("Invalid scale range");
bool validRange = false;
if ((minscale - maxscale) * increment > 0) return help("Invalid range specification");
if (minscale<=maxscale) for (double scale = minscale; scale < maxscale*(1-1e-6); scale += increment) {
validRange = true;
scales.insert(scale);
} else for (double scale = minscale; scale > maxscale*(1+1e-6); scale += increment) {
validRange = true;
scales.insert(scale);
}
// compensate roundoff errors for loop bounds
scales.insert(minscale); scales.insert(maxscale);
if (!validRange) return help("Invalid range specification");
}
}
vector<double> scalesvec(scales.begin(), scales.end());
int nscales = scalesvec.size();
if (scales.empty()) return help();
double cylinder_base = scalesvec.back(); // smallest
double cylinder_length = scalesvec[0]; // largest
int separator_next = 0;
for (int i=separator+1; i<argc; ++i) if (!strcmp(":",argv[i])) {
separator_next = i;
break;
}
if (separator_next!=0) {
if (separator_next != separator+2) return help();
cylinder_base = atof(argv[separator+1]);
separator = separator_next;
separator_next = 0;
for (int i=separator+1; i<argc; ++i) if (!strcmp(":",argv[i])) {
separator_next = i;
break;
}
if (separator_next!=0) {
if (separator_next != separator+2) return help();
cylinder_length = atof(argv[separator+1]);
separator = separator_next;
}
}
// cylinder is split in segments using small balls, covering up the whole length
// Analysis of the volume of the balls outside the cylinder wrt the number of balls
// and their diameters required for covering the cylinder shows a minimum
// Use twice the length as we also look for negative shifts
int num_cyl_balls = floor(2.*cylinder_length*sqrt(2.)/cylinder_base);
double cyl_section_length = 2.*cylinder_length/num_cyl_balls;
double cyl_ball_radius = 0.5*sqrt(cyl_section_length*cyl_section_length + cylinder_base*cylinder_base);
double cylinder_base_radius_sq = cylinder_base * cylinder_base * 0.25;
if (argc<separator+6) return help();
cout << "Scale" << (scales.size()==1?"":"s") << " for normals computation:";
for (ScaleSet::iterator it = scales.begin(); it!=scales.end(); ++it) {
cout << " " << *it;
}
cout << endl;
cout << "Base of projection/search cylinder: " << cylinder_base << endl;
cout << "Length of projection/search cylinder: " << cylinder_length << endl;
string p1fname = argv[separator+1];
string p1reducedfname;
int p1redpos = p1fname.find(':');
if (p1redpos>=0) {
p1reducedfname = p1fname.substr(p1redpos+1);
p1fname = p1fname.substr(0,p1redpos);
}
string p2fname = argv[separator+2];
string p2reducedfname;
int p2redpos = p2fname.find(':');
if (p2redpos>=0) {
p2reducedfname = p2fname.substr(p2redpos+1);
p2fname = p2fname.substr(0,p2redpos);
}
string corefname = argv[separator+3];
string extptsfname = argv[separator+4];
string resultarg = argv[separator+5];
vector<string> result_filenames;
vector<vector<string> > result_formats;
bool compute_normal_angles = false, compute_shift_bsdev = false;
double n1angle_bs = 0, n2angle_bs = 0;
// set to true below by default
// disabled below if another option is set
bool fast_ci = true;
bool compute_normal_plane_dev = false;
char_separator<char> colsep(":");
char_separator<char> commasep(",");
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
tokenizer resfile_tokenizer(resultarg, colsep);
for (tokenizer::iterator ftokit = resfile_tokenizer.begin(); ftokit != resfile_tokenizer.end(); ++ftokit) {
// get each file specification
tokenizer spec_tokenizer(*ftokit, commasep);
tokenizer::iterator stokit = spec_tokenizer.begin();
if (stokit == spec_tokenizer.end()) return help("Invalid result file format");
// first token is the file name
result_filenames.push_back(*stokit);
vector<string> formats;
// next are the format specifications
for(++stokit; stokit!=spec_tokenizer.end(); ++stokit) {
string format = *stokit;
bool found = false;
for (int i=0; i<nresformats; ++i) if (format==all_result_formats[i]) {
found = true;
break;
}
if (!found) return help(("Invalid result file format: "+format).c_str());
formats.push_back(format);
}
if (formats.empty()) {
for (int i=0; i<num_default_result_formats; ++i) formats.push_back(default_result_formats[i]);
}
for (auto format : formats) {
if (format=="ksi1" || format=="ksi2" || format=="normal_dev1" || format=="normal_dev2") compute_normal_plane_dev = true;
if (format=="n1angle_bs" || format=="n2angle_bs") compute_normal_angles = true;
if (format=="shift1_bsdev" || format=="shift2_bsdev") compute_shift_bsdev = true;
}
result_formats.push_back(formats);
}
bool force_horizontal = false;
bool force_vertical = false;
bool use_median = false;
bool shift_first = false;
bool shift_second = false;
bool shift_mean = false;
double pos_dev = 0;
double max_core_shift_distance = numeric_limits<double>::max();
int num_bootstrap_iter = 1, num_normal_bootstrap_iter = 1;
double confidence_interval_percent = 95.;
bool normal_ci = false;
bool use_BCa = false;
int num_pt_sig = 10;
double ksi_autoscale = 0;
bool warnings = false;
int np_prod_max = 10000;
if (argc>separator+6) {
int extra_info_idx = separator+6;
for (char* opt=argv[separator+6]; *opt!=0; ++opt) {
switch(*opt) {
case '1': shift_first = true; break;
case '2': shift_second = true; break;
case 'm': shift_mean = true; break;
case 'q': use_median = true; break;
case 'h': force_horizontal = true; break;
case 'v': force_vertical = true; break;
case 'w': warnings = true; break;
case 'e': if (++extra_info_idx<argc) {
pos_dev = atof(argv[extra_info_idx]); break;
} else return help("Missing value for the e flag");
case 'b': if (++extra_info_idx<argc) {
use_BCa = true;
num_bootstrap_iter = atoi(argv[extra_info_idx]); break;
} else return help("Missing value for the b flag");
case 'n': if (++extra_info_idx<argc) {
num_normal_bootstrap_iter = atoi(argv[extra_info_idx]); break;
} else return help("Missing value for the n flag");
case 'c': if (++extra_info_idx<argc) {
confidence_interval_percent = atof(argv[extra_info_idx]); break;
} else return help("Missing value for the c flag");
case 'k': if (++extra_info_idx<argc) {
ksi_autoscale = atof(argv[extra_info_idx]); break;
} else return help("Missing value for the c flag");
case 'f': fast_ci = true; break;
case 'g': normal_ci = true; break;
case 'p': if (++extra_info_idx<argc) {
num_pt_sig = atoi(argv[extra_info_idx]); break;
} else return help("Missing value for the p flag");
/* case 's': if (extra_info_idx+3<argc) {
systematic_error.x = atof(argv[++extra_info_idx]);
systematic_error.y = atof(argv[++extra_info_idx]);
systematic_error.z = atof(argv[++extra_info_idx]);
break;
} else return help("Missing value for the s flag"); break;
*/
default: return help((string("Unrecognised optional flag: ")+opt).c_str());
}
}
}
if (shift_first && shift_second) return help("Conflicting 1 and 2 options for shifting core points.");
if (shift_first && shift_mean) return help("Conflicting 1 and m options for shifting core points.");
if (shift_mean && shift_second) return help("Conflicting 2 and m options for shifting core points.");
if (force_horizontal && force_vertical) return help("Cannot force normals to be both horizontal and vertical!");
if (num_bootstrap_iter<=0) num_bootstrap_iter = 1;
if (num_bootstrap_iter==1) {
if (compute_shift_bsdev) return help("Error: cannot compute the shift bootstrap deviation without bootstrapping, set the b flag.");
if (use_BCa && !normal_ci) {
// too experimental, do not set any confidence interval then
// normal_ci = true;
// use_BCa = false;
return help("Error: confidence interval cannot be computed with bootstrap and only one iteration.");
}
}
// honor the g flag
if (normal_ci && !use_median) use_BCa = false;
// disable the fast estimator if another technique is specified
if (use_BCa || normal_ci) fast_ci = false;
if (fast_ci && use_median) return help("Estimating confidence interval when using median/interquartile ranges needs bootstrapping. Please set the b flag");
if (num_normal_bootstrap_iter<=0) num_normal_bootstrap_iter = 1;
if (compute_normal_angles && (num_normal_bootstrap_iter==1) && warnings) cout << "Warning: cannot compute the normal angle deviations without bootstraping the normals, set the n flag." << endl;
if (confidence_interval_percent<0 || confidence_interval_percent>100) {
return help("Invalid confidence interval for the c flag, shall be between 0 and 100");
}
double z_low = 0., z_high = 0.;
double cdf_low = 0., cdf_high = 0.;
if (fast_ci || use_BCa || normal_ci) {
cdf_low = 0.5 - confidence_interval_percent/200.;
cdf_high = 0.5 + confidence_interval_percent/200.;
z_low = inverse_normal_cdf(cdf_low);
z_high = inverse_normal_cdf(cdf_high);
}
vector<double> *bs_dist = 0;
if (use_BCa) bs_dist = new vector<double>(num_bootstrap_iter);
boost::mt11213b rng;
boost::normal_distribution<double> poserr_dist(0, pos_dev);
boost::variate_generator<boost::mt11213b&, boost::normal_distribution<double> > poserr_rand(rng, poserr_dist);
randinit();
cout << "Loading cloud 1: " << p1fname << endl;
PointCloud<Point> p1, p1reduced;
p1.load_txt(p1fname);
if (!p1reducedfname.empty()) {
cout << "Loading subsampled cloud 1: " << p1reducedfname << endl;
if (p1reduced.load_txt(p1reducedfname)==0) {
cout << "Bad or empty subsampled cloud 1: " << p1reducedfname << endl;
return -1;
}
}
cout << "Loading cloud 2: " << p2fname << endl;
PointCloud<Point> p2, p2reduced;
p2.load_txt(p2fname);
if (!p2reducedfname.empty()) {
cout << "Loading subsampled cloud 2: " << p2reducedfname << endl;
if (p2reduced.load_txt(p2reducedfname)==0) {
cout << "Bad or empty subsampled cloud 2: " << p2reducedfname << endl;
return -1;
}
}
cout << "Loading core points: " << corefname << endl;
FILE* corepointsfile = fopen(corefname.c_str(), "r");
if (!corepointsfile) {cerr << "Could not load file: " << corefname << endl; return 1;}
char* line = 0; size_t linelen = 0; int num_read = 0;
int linenum = 0;
vector<Point> corepoints;
while ((num_read = getline(&line, &linelen, corepointsfile)) != -1) {
++linenum;
if (linelen==0 || line[0]=='#') continue;
Point point;
int i = 0;
for (char* x = line; *x!=0;) {
double value = fast_atof_next_token(x);
if (i<Point::dim) point[i++] = value;
else break;
}
if (i<3) {cerr << "Error in the core points file" << corefname << " line " << (linenum+1) << endl; continue;}
corepoints.push_back(point);
}
fclose(corepointsfile);
cout << "Loading external reference points: " << extptsfname << endl;
FILE* refpointsfile = fopen(extptsfname.c_str(), "r");
if (!refpointsfile) {std::cerr << "Could not load file: " << extptsfname << std::endl; return 1;}
linenum = 0;
vector<Point> refpoints;
while ((num_read = getline(&line, &linelen, refpointsfile)) != -1) {
++linenum;
if (linelen==0 || line[0]=='#') continue;
Point point;
int i = 0;
for (char* x = line; *x!=0;) {
double value = fast_atof_next_token(x);
if (i<Point::dim) point[i++] = value;
else break;
}
if (i<3) return help(str(boost::format("Error in the reference points file line %d") % (linenum+1)).c_str());
refpoints.push_back(point);
}
fclose(refpointsfile);
if (refpoints.empty()) return help("Please provide at least one reference point");
for (int i = separator+6; i<argc; ++i) {
if (i==separator+6) cout << "Options given:";
cout << " " << argv[i];
if (i==argc-1) cout << endl;
}
cout << "Computing result files: " << endl;
for (int i=0; i<(int)result_filenames.size(); ++i) {
cout << " " << result_filenames[i] << ":";
vector<string>& formats = result_formats[i];
for (int j=0; j<(int)formats.size(); ++j) cout << " " << formats[j];
cout << endl;
}
map<string, string> formats_disp_map;
for (int i=0; i<(int)result_filenames.size(); ++i) {
for (auto f : result_formats[i]) formats_disp_map[f] = f;
}
formats_disp_map["c1"] = "c1.x c1.y c1.z";
formats_disp_map["c2"] = "c2.x c2.y c2.z";
formats_disp_map["n1"] = "n1.x n1.y n1.z";
formats_disp_map["n2"] = "n2.x n2.y n2.z";
formats_disp_map["c0"] = "c0.x c0.y c0.z";
vector<ofstream*> resultfiles(result_filenames.size());
for (int i=0; i<(int)result_filenames.size(); ++i) {
resultfiles[i] = new ofstream(result_filenames[i].c_str());
// add the variables as a comment for matlab/octave
// but no space between # and the first variable for cloud compare
*resultfiles[i] << "#";
vector<string>& formats = result_formats[i];
for (int j=0; j<(int)formats.size(); ++j) {
if (j>0) *resultfiles[i] << " ";
*resultfiles[i] << formats_disp_map[formats[j]];
}
*resultfiles[i] << endl;
resultfiles[i]->precision(20);
}
// parameters and files loaded, now the real work
cout << "Percent complete: 0" << flush;
double core_global_diff_mean = 0;
double core_global_diff_min = numeric_limits<double>::max();
double core_global_diff_max = -numeric_limits<double>::max();
int num_nan_diff = 0;
int num_nan_c1 = 0;
int num_nan_c2 = 0;
// for each core point
int nextpercentcomplete = 5;
for (int ptidx = 0; ptidx < (int)corepoints.size(); ++ptidx) {
int percentcomplete = ((ptidx+1) * 100) / corepoints.size();
if (percentcomplete>=nextpercentcomplete) {
if (percentcomplete>=nextpercentcomplete) {
nextpercentcomplete+=5;
if (percentcomplete % 10 == 0) cout << percentcomplete << flush;
else if (percentcomplete % 5 == 0) cout << "." << flush;
}
}
// first extract the neighbors on which to do the normal computations
// these are fixed for the whole bootstrapping. Technically we should apply
// the pos_dev and resampling before looking for neighbors in order to
// ensure proper computation at the exact selected scale. In practice
// the difference does not matter (we're at scale +- pos_dev instead of scale)
// and the computations should be much faster!
vector<DistPoint<Point> > neighbors_1, neighbors_2;
vector<int> neigh_num_1(nscales,0), neigh_num_2(nscales,0);
vector<Point> neighsums_1, neighsums_2;
if (!force_vertical)
// Scales are sorted from max to lowest
for (int scaleidx = 0; scaleidx<nscales; ++scaleidx) {
// Neighborhood search only on max radius
if (scaleidx==0) {
// we have all neighbors, unsorted, but with distances computed already
// use scales = diameters, not radius
if (!shift_second) {
if (p1reducedfname.empty())
p1.findNeighbors(back_inserter(neighbors_1), corepoints[ptidx], scalesvec[scaleidx] * 0.5);
else
p1reduced.findNeighbors(back_inserter(neighbors_1), corepoints[ptidx], scalesvec[scaleidx] * 0.5);
// Sort the neighbors from closest to farthest, so we can process all lower scales easily
if (nscales>1) sort(neighbors_1.begin(), neighbors_1.end());
neigh_num_1[scaleidx] = neighbors_1.size();
// pre-compute cumulated sums
// so we might as well share the intermediates to lower levels
neighsums_1.resize(neighbors_1.size());
if (!neighbors_1.empty()) neighsums_1[0] = *neighbors_1[0].pt;
for (int i=1; i<(int)neighbors_1.size(); ++i) neighsums_1[i] = neighsums_1[i-1] + *neighbors_1[i].pt;
}
if (!shift_first) {
if (p2reducedfname.empty())
p2.findNeighbors(back_inserter(neighbors_2), corepoints[ptidx], scalesvec[scaleidx] * 0.5);
else
p2reduced.findNeighbors(back_inserter(neighbors_2), corepoints[ptidx], scalesvec[scaleidx] * 0.5);
// Sort the neighbors from closest to farthest, so we can process all lower scales easily
if (nscales>1) sort(neighbors_2.begin(), neighbors_2.end());
neigh_num_2[scaleidx] = neighbors_2.size();
neighsums_2.resize(neighbors_2.size());
if (!neighbors_2.empty()) neighsums_2[0] = *neighbors_2[0].pt;
for (int i=1; i<(int)neighbors_2.size(); ++i) neighsums_2[i] = neighsums_2[i-1] + *neighbors_2[i].pt;
}
}
// lower scale : restrict previously found neighbors to the new distance
else {
double radiussq = scalesvec[scaleidx] * scalesvec[scaleidx] * 0.25;
// dicho search might be faster than sequencially from the vector end if there are many points
int dichofirst = 0;
int dicholast = neighbors_1.size();
int dichomed;
if (!shift_second) {
while (true) {
dichomed = (dichofirst + dicholast) / 2;
if (dichomed==dichofirst) break;
if (radiussq==neighbors_1[dichomed].distsq) break;
if (radiussq<neighbors_1[dichomed].distsq) { dicholast = dichomed; continue;}
dichofirst = dichomed;
}
// dichomed is now the last index with distance below or equal to requested radius
neigh_num_1[scaleidx] = dichomed+1;
}
// same for cloud 2
if (!shift_first) {
dichofirst = 0;
dicholast = neighbors_2.size();
while (true) {
dichomed = (dichofirst + dicholast) / 2;
if (dichomed==dichofirst) break;
if (radiussq==neighbors_2[dichomed].distsq) break;
if (radiussq<neighbors_2[dichomed].distsq) { dicholast = dichomed; continue;}
dichofirst = dichomed;
}
neigh_num_2[scaleidx] = dichomed+1;
}
}
}
// The most planar scale is only computed once if needed
// bootstrapping is then done on that scale for the normal computations
int normal_scale_idx_1 = 0, normal_scale_idx_2 = 0;
int ref12_idx_begin = 0;
int ref12_idx_end = 2;
if (!compute_normal_plane_dev) {
if (shift_first) ref12_idx_end = 1;
if (shift_second) ref12_idx_begin = 1;
}
if (ksi_autoscale>0 || (nscales>1 && !force_vertical)) {
double svalues[3]; double eigenvectors[9];
// avoid code dup below
// but some dup in bootstrapping as I'm lazy to get rid of it
int* normal_scale_idx_ref[2] = {&normal_scale_idx_1, &normal_scale_idx_2};
vector<DistPoint<Point> >* neighbors_ref[2] = {&neighbors_1, &neighbors_2};
vector<int>* neigh_num_ref[2] = {&neigh_num_1, &neigh_num_2};
vector<Point>* neighsums_ref[2] = {&neighsums_1, &neighsums_2};
// loop on both pt sets, unless shift1/2 specified
for (int ref12_idx = ref12_idx_begin; ref12_idx < ref12_idx_end; ++ref12_idx) {
vector<DistPoint<Point> >& neighbors = *neighbors_ref[ref12_idx];
double maxbarycoord = -numeric_limits<double>::max();
// init to largest scale in case ksi condition is never verified below
if (ksi_autoscale>0) *normal_scale_idx_ref[ref12_idx] = 0;
for (int sidx=0; sidx<nscales; ++sidx) {
int npts = (*neigh_num_ref[ref12_idx])[sidx];
if (npts>=3) {
// use the pre-computed sums to get the average point
Point avg = (*neighsums_ref[ref12_idx])[npts-1] / npts;
// compute PCA on the neighbors at this scale
// a copy is needed as LAPACK destroys the matrix, and the center changes anyway
// => cannot keep the points from one scale to the lower, need to rebuild the matrix
vector<double> A(npts * 3);
for (int i=0; i<npts; ++i) {
// A is column-major
A[i] = neighbors[i].pt->x - avg.x;
A[i+npts] = neighbors[i].pt->y - avg.y;
A[i+npts*2] = neighbors[i].pt->z - avg.z;
}
if (ksi_autoscale>0) {
vector<double> Acopy = A;
svd(npts, 2, &Acopy[0], &svalues[0], false, &eigenvectors[0]);
Point e1(eigenvectors[0], eigenvectors[3], eigenvectors[6]);
Point e2(eigenvectors[1], eigenvectors[4], eigenvectors[7]);
Point normal = e1.cross(e2);
double avg_dist_to_plane = 0.;
double ssq_dist_to_plane = 0.;
for (int i=0; i<npts; ++i) {
// A is now centered on 0, plane goes on 0
Point a(A[i], A[i+npts], A[i+npts*2]);
// so dist is easy to compute
double d = a.dot(normal);
avg_dist_to_plane += d;
ssq_dist_to_plane += d*d;
}
avg_dist_to_plane /= npts;
ssq_dist_to_plane = (ssq_dist_to_plane - npts * avg_dist_to_plane * avg_dist_to_plane) / (npts-1.);
ssq_dist_to_plane = max(0.,ssq_dist_to_plane);
// when ssq_dist_to_plane==0, estimate is infinite
// which means all scales match, so end up with the lowest one
if (ssq_dist_to_plane==0) *normal_scale_idx_ref[ref12_idx] = sidx;
else {
double ksi = scalesvec[sidx] / sqrt(ssq_dist_to_plane);
if (ksi > ksi_autoscale) *normal_scale_idx_ref[ref12_idx] = sidx;
}
} else {
svd(npts, 3, &A[0], &svalues[0]);
// The most 2D scale. For the criterion for how "2D" a scale is, see canupo
// Ideally first and second eigenvalue are equal
// convert to percent variance explained by each dim
double totalvar = 0;
for (int i=0; i<3; ++i) {
// singular values are squared roots of eigenvalues
svalues[i] = svalues[i] * svalues[i]; // / (neighbors.size() - 1);
totalvar += svalues[i];
}
for (int i=0; i<3; ++i) svalues[i] /= totalvar;
// ideally, 2D means first and second entries are both 1/2 and third is 0
// convert to barycentric coordinates and take the coefficient of the 2D
// corner as a quality measure.
// Use barycentric coordinates : a for 1D, b for 2D and c for 3D
// Formula on wikipedia page for barycentric coordinates
// using directly the triangle in %variance space, they simplify a lot
//double c = 1 - a - b; // they sum to 1
// a = svalues[0] - svalues[1];
double b = 2 * svalues[0] + 4 * svalues[1] - 2;
if (b > maxbarycoord) {
maxbarycoord = b;
*normal_scale_idx_ref[ref12_idx] = sidx;
}
}
}
}
} //ref12 loop
}
// closest ref point is also shared for all bootstrap iterations for efficiency
// non-empty set => valid index
int nearestidx = -1;
double mindist = numeric_limits<double>::max();
for (int i=0; i<(int)refpoints.size(); ++i) {
double d = dist2(refpoints[i],corepoints[ptidx]);
if (d<mindist) {
mindist = d;
nearestidx = i;
}
}
Point& refpt = refpoints[nearestidx];
Point deltaref = refpt - corepoints[ptidx];
if (force_horizontal) deltaref.z = 0;
deltaref.normalize();
Point normal_1, normal_2;
vector<Point> *normal_bs_sample1 = 0;
vector<Point> *normal_bs_sample2 = 0;
if (compute_normal_angles) {
normal_bs_sample1 = new vector<Point>(num_normal_bootstrap_iter);
normal_bs_sample2 = new vector<Point>(num_normal_bootstrap_iter);
}
double normal_dev1 = 0, normal_dev2 = 0;
// largest possible A, reused at each bootstrap step
vector<double> A1(neigh_num_1[0] * 3);
vector<double> A2(neigh_num_2[0] * 3);
vector<double>* A_ref[2] = {&A1, &A2};
// lapack destroys the matrix, we need it for compute_normal_plane_dev
vector<double> A1copy(0), A2copy(0);
if (compute_normal_plane_dev) {
A1copy.resize(A1.size());
A2copy.resize(A2.size());
}
vector<double>* Acopy_ref[2] = {&A1copy, &A2copy};
// We have all core point neighbors at all scales in each data set
// and the correct scales for the computation
// Now bootstrapping...
for (int n_bootstrap_iter = 0; n_bootstrap_iter < num_normal_bootstrap_iter; ++n_bootstrap_iter) {
Point normal_bs_1, normal_bs_2;
int npts_scale0_bs_1, npts_scale0_bs_2;
double svalues[3];
double eigenvectors[9];
vector<Point> resampled_neighbors_1, resampled_neighbors_2;
// avoid code dup below
int* npts_scale0_bs_ref[2] = {&npts_scale0_bs_1, &npts_scale0_bs_2};
int* normal_scale_idx_ref[2] = {&normal_scale_idx_1, &normal_scale_idx_2};
Point* normal_ref[2] = {&normal_1, &normal_2};
Point* normal_bs_ref[2] = {&normal_bs_1, &normal_bs_2};
vector<DistPoint<Point> >* neighbors_ref[2] = {&neighbors_1, &neighbors_2};
vector<int>* neigh_num_ref[2] = {&neigh_num_1, &neigh_num_2};
vector<Point>* resampled_neighbors_ref[2] = {&resampled_neighbors_1, &resampled_neighbors_2};
double* normal_dev_ref[2] = {&normal_dev1, &normal_dev2};
int npts_scaleN_1 = 0, npts_scaleN_2 = 0;
int* npts_scaleN_ref[2] = {&npts_scaleN_1, &npts_scaleN_2};
// loop on both pt sets
for (int ref12_idx = ref12_idx_begin; ref12_idx < ref12_idx_end; ++ref12_idx) {
Point& normal = *normal_bs_ref[ref12_idx];
// vertical normals need none of the SVD business
if (force_vertical) {
normal.z = (deltaref.z<0) ? -1 : 1;
// but they may require a deviation around the plane
if (!compute_normal_plane_dev) continue;
}
vector<DistPoint<Point> >& neighbors = *neighbors_ref[ref12_idx];
int normal_sidx = *normal_scale_idx_ref[ref12_idx];
int npts_scale_base = (*neigh_num_ref[ref12_idx])[normal_sidx];
Point avg = 0;
vector<double>& A = *A_ref[ref12_idx];
vector<double>& Acopy = *Acopy_ref[ref12_idx];
int& npts_scaleN = *npts_scaleN_ref[ref12_idx];
npts_scaleN = 0;
double radiussq = scalesvec[normal_sidx] * scalesvec[normal_sidx] * 0.25;
Point bspt;
for (int i=0; i<npts_scale_base; ++i) {
Point* pt = neighbors[i].pt;
if (num_normal_bootstrap_iter>1) {
//int selectedidx = randint();
int selectedidx = randint(npts_scale_base);
pt = neighbors[selectedidx].pt;
// add some gaussian noise with dev specified by the user on each coordinate
if (pos_dev>0 && !force_vertical) {
bspt = *pt;
bspt.x += poserr_rand(); //poserr_dist(rng);
bspt.y += poserr_rand();
bspt.z += poserr_rand();
pt = &bspt;
}
}
// filter only the points within normal scale for the normal
// computation below
if ((corepoints[ptidx] - *pt).norm2()>=radiussq) continue;
avg += *pt;
A[npts_scaleN] = pt->x;
A[npts_scaleN+npts_scale_base] = pt->y;
A[npts_scaleN+npts_scale_base*2] = pt->z;
++npts_scaleN;
}
// need to reshape A, colomn-major for Fortran :(
if (npts_scaleN<npts_scale_base) {
for (int i=0; i<npts_scaleN; ++i) A[i+npts_scaleN] = A[i+npts_scale_base];
for (int i=0; i<npts_scaleN; ++i) A[i+npts_scaleN*2] = A[i+npts_scale_base*2];
}
// now finish the averaging
avg /= npts_scaleN;
for (int i=0; i<npts_scaleN; ++i) {
A[i] -= avg.x;
A[i+npts_scaleN] -= avg.y;
A[i+npts_scaleN*2] -= avg.z;
}
if (compute_normal_plane_dev) for (int i=0; i<npts_scaleN*3; ++i) Acopy [i] = A[i];
if (!force_vertical) {
if (force_horizontal) {
if (npts_scaleN<2 && n_bootstrap_iter==0) {
if (warnings) cout << "Warning: Invalid core point / data file / scale combination: less than 2 points at max scale for core point " << (ptidx+1) << " in data set " << ref12_idx+1 << endl;
} else {
// column-wise A: no need to reshape so as to skip z
// SVD decomposition handled by LAPACK
svd(npts_scaleN, 2, &A[0], &svalues[0], false, &eigenvectors[0]);
// The total least squares solution in the horizontal plane
// is given by the singular vector with minimal singular value
int mins = 0; if (svalues[1]<svalues[0]) mins = 1;
normal = Point(eigenvectors[mins], eigenvectors[2+mins], 0);
normal.normalize();
}
} else {
if (npts_scaleN<3 && n_bootstrap_iter==0) {
if (warnings) cout << "Warning: Invalid core point / data file / scale combination: less than 3 points at max scale for core point " << (ptidx+1) << " in data set " << ref12_idx+1 << endl;
} else {
svd(npts_scaleN, 3, &A[0], &svalues[0], false, &eigenvectors[0]);
//std::cout << svalues[0] << " " << svalues[1] << " " << svalues[2] << std::endl;
// column-major matrix, eigenvectors as rows
Point e1(eigenvectors[0], eigenvectors[3], eigenvectors[6]);
Point e2(eigenvectors[1], eigenvectors[4], eigenvectors[7]);
normal = e1.cross(e2);
}
}
// normal orientation... simple with external help
if (normal.dot(deltaref)<0) normal *= -1;
}
}
// replace the local iteration value for the options "1", "2", and "m"
if (shift_first) normal_bs_2 = normal_bs_1;
if (shift_second) normal_bs_1 = normal_bs_2;
if (shift_mean) {
if (normal_bs_1.norm2()==0) {
if (warnings) cout << "Warning: null normal on Cloud 1 for core point " << (ptidx+1) << endl;
normal_bs_1 = normal_bs_2;
}
if (normal_bs_2.norm2()==0) {
if (warnings) cout << "Warning: null normal on Cloud 2 for core point " << (ptidx+1) << endl;
normal_bs_2 = normal_bs_1;
}
normal_bs_1 = (normal_bs_1 + normal_bs_2) * 0.5;
normal_bs_2 = normal_bs_1;
}
else {