28 #ifndef MACH_EPS_DOUBLE
29 #define MACH_EPS_DOUBLE 2e-16
31 #ifndef MACH_EPS_FLOAT
32 #define MACH_EPS_FLOAT 2e-7
41 template<
class ElementType >
42 static std::vector< ElementType >
43 unique(
const std::vector< ElementType > &v)
45 std::vector< ElementType > ret;
46 size_t nb_e = v.size();
51 for(
size_t i = 1; i < nb_e; ++i)
53 bool is_unique =
true;
54 for(
size_t j = 0; j < ret.size(); j++)
71 template<
typename ElementType,
int DIM >
75 ElementType result = std::numeric_limits< ElementType >::min();
76 for(
size_t i = 0; i < DIM; ++i)
85 template<
typename ElementType >
87 maximum(
const std::vector< ElementType > &v)
89 ElementType result = std::numeric_limits< ElementType >::min();
90 const size_t dim = v.size();
91 for(
size_t i = 0; i < dim; ++i)
101 template<
typename ElementType,
int DIM >
105 ElementType result = std::numeric_limits< ElementType >::max();
106 for(
size_t i = 0; i < DIM; ++i)
115 template<
typename ElementType >
119 ElementType result = std::numeric_limits< ElementType >::max();
120 const size_t dim = v.size();
121 for(
size_t i = 0; i < dim; ++i)
131 template<
typename ElementType,
int DIM >
135 ElementType result = 0;
136 for(
size_t i = 0; i < DIM; ++i)
141 return result /
static_cast< ElementType
>(DIM);
145 template<
typename ElementType >
147 mean(
const std::vector< ElementType > &v)
149 ElementType result = 0;
150 const size_t dim = v.size();
151 for(
size_t i = 0; i < dim; ++i)
156 return result /
static_cast< ElementType
>(dim);
161 template<
typename ElementType >
163 mean2(
const std::vector< ElementType > &v)
165 ElementType result = 0;
166 const size_t dim = v.size();
167 for(
size_t i = 0; i < dim; ++i)
169 result += v[i] * v[i];
172 return result /
static_cast< ElementType
>(dim);
177 template<
typename ElementType >
179 mean4(
const std::vector< ElementType > &v)
181 ElementType result = 0;
182 const size_t dim = v.size();
183 for(
size_t i = 0; i < dim; ++i)
185 result += v[i] * v[i] * v[i] * v[i];
188 return result /
static_cast< ElementType
>(dim);
193 template<
typename ElementType >
197 ElementType result = 0;
198 const size_t dim = v.size();
199 for(
size_t i = 0; i < dim; ++i)
201 result += sqrt(v[i]);
204 return result /
static_cast< ElementType
>(dim);
209 template<
typename ElementType >
213 ElementType result = 0;
214 const size_t dim = v.size();
215 for(
size_t i = 0; i < dim; ++i)
217 result += sqrt(sqrt(v[i]));
220 return result /
static_cast< ElementType
>(dim);
225 template<
typename ElementType,
int DIM >
229 ElementType result = 0;
230 ElementType sum_weights = 0;
231 for(
size_t i = 0; i < DIM; ++i)
233 result += v[i] * weights[i];
234 sum_weights += weights[i];
237 return result / sum_weights;
241 template<
typename ElementType >
244 const std::vector< ElementType > &weights)
246 ElementType result = 0;
247 ElementType sum_weights = 0;
248 const size_t dim = ((v.size() <= weights.size()) ? v.size() : weights.size());
249 for(
size_t i = 0; i < dim; ++i)
251 result += v[i] * weights[i];
252 sum_weights += weights[i];
255 return result / sum_weights;
264 template<
class ElementType >
268 std::sort(v.begin(), v.end());
269 if(v.size() % 2 == 0)
271 return static_cast< ElementType
>(
272 (v[v.size() / 2] + v[(v.size() / 2) - 1]) /
277 return v[v.size() / 2];
281 template<
class ElementType >
285 std::sort(v.begin(), v.end());
290 return v[(int)floor(k * (
float)v.size())];
298 bool operator()(
const unsigned int a,
const unsigned int b)
const
320 inline std::vector< unsigned int >
323 std::vector< unsigned int > vi(v.size());
324 for(
size_t i = 0; i < v.size(); i++)
328 std::sort(vi.begin(), vi.end(),
IndexCmp< std::vector< T > & >(v));
331 template<
class ElementType >
334 const std::vector< ElementType > &weights)
337 double sumw = 0.0, csumw = 0.0;
338 size_t i, nb_e = weights.size();
339 for(i = 0; i < nb_e; ++i)
341 sumw += (double)weights[i];
343 for(i = 0; i < nb_e; ++i)
345 csumw += (double)weights[vi[i]] / (
double)sumw;
352 template<
class ElementType >
355 ElementType mean_value,
356 bool unbiased =
true)
358 ElementType result = 0;
359 size_t nb_e = v.size();
362 std::cerr <<
"Vector::Stats::variance: at least 2 elements are needed for "
363 "computing the unbiased variance."
368 for(
size_t i = 0; i < nb_e; ++i)
370 result += (v[i] - mean_value) * (v[i] - mean_value);
373 result /= (
static_cast< ElementType
>(nb_e) - 1);
375 result /=
static_cast< ElementType
>(nb_e);
380 template<
class ElementType >
383 ElementType mean_value,
384 ElementType biased_variance_value)
386 ElementType result = 0;
387 size_t nb_e = v.size();
388 for(
size_t i = 0; i < nb_e; ++i)
390 result += (v[i] - mean_value) * (v[i] - mean_value) * (v[i] - mean_value);
392 result /=
static_cast< ElementType
>(nb_e);
393 result /= sqrt(biased_variance_value * biased_variance_value *
394 biased_variance_value +
395 static_cast< float >(1e-30));
399 template<
class ElementType >
402 ElementType mean_value,
403 ElementType biased_variance_value)
405 ElementType result = 0;
406 size_t nb_e = v.size();
407 for(
size_t i = 0; i < nb_e; ++i)
409 result += (v[i] - mean_value) * (v[i] - mean_value) * (v[i] - mean_value) *
412 result /= (nb_e * biased_variance_value * biased_variance_value +
413 static_cast< float >(1e-30));
419 template<
typename ElementType,
int DIM >
424 for(
int i = 0; i < DIM; ++i)
429 template<
typename ElementType >
432 const std::vector< ElementType > &v2)
435 size_t nb_e = v1.size();
439 for(
size_t i = 0; i < nb_e; ++i)
444 template<
typename GeometryTraits >
445 static typename GeometryTraits::Scalar
448 const GeometryTraits >)
450 return gt.dot_product(v1, v2);
458 template<
typename ElementType,
int DIM >
459 static std::vector< ElementType >
467 std::vector< ElementType > res(nb_e);
469 for(
size_t i = 0; i < nb_e; ++i)
470 res[i] = v1[(1 + i) % nb_e] * v2[(2 + i) % nb_e] -
471 v1[(2 + i) % nb_e] * v2[(1 + i) % nb_e];
477 template<
typename ElementType >
478 static std::vector< ElementType >
480 const std::vector< ElementType > &v2)
484 if((nb_e != v1.size()) || (nb_e != v2.size()))
487 std::vector< ElementType > res(nb_e);
489 for(
size_t i = 0; i < nb_e; ++i)
490 res[i] = v1[(1 + i) % nb_e] * v2[(2 + i) % nb_e] -
491 v1[(2 + i) % nb_e] * v2[(1 + i) % nb_e];
496 template<
typename GeometryTraits >
500 const GeometryTraits >)
502 return gt.cross_product(v1, v2);
506 template<
typename ElementType,
int DIM >
507 static std::vector< ElementType >
508 sub(
const ElementType v1[DIM],
const ElementType v2[DIM])
510 std::vector< ElementType > res(DIM);
512 for(
size_t i = 0; i < DIM; ++i)
513 res[i] = v1[i] - v2[i];
519 template<
typename ElementType >
520 static std::vector< ElementType >
521 sub(
const std::vector< ElementType > &v1,
const std::vector< ElementType > &v2)
523 size_t nb_e = v1.size();
527 std::vector< ElementType > res(nb_e);
529 for(
size_t i = 0; i < nb_e; ++i)
530 res[i] = v1[i] - v2[i];
536 template<
typename GeometryTraits >
540 const GeometryTraits >)
542 return gt.sub_p(p1, p2);
546 template<
typename ElementType,
int DIM >
547 static std::vector< ElementType >
548 add(
const ElementType v1[DIM],
const ElementType v2[DIM])
550 std::vector< ElementType > res(DIM);
552 for(
size_t i = 0; i < DIM; ++i)
553 res[i] = v1[i] + v2[i];
559 template<
typename ElementType >
560 static std::vector< ElementType >
561 add(
const std::vector< ElementType > &v1,
const std::vector< ElementType > &v2)
563 size_t nb_e = v1.size();
567 std::vector< ElementType > res(nb_e);
569 for(
size_t i = 0; i < nb_e; ++i)
570 res[i] = v1[i] + v2[i];
576 template<
typename GeometryTraits >
580 const GeometryTraits >)
582 return gt.add_pv(p, v);
586 template<
typename ElementType,
int DIM >
592 std::vector< ElementType > sub_res(Vector::sub< ElementType, DIM >(v1, v2));
593 return sqrt(dot_product< ElementType >(sub_res, sub_res));
597 template<
typename ElementType,
int DIM >
601 return sqrt(dot_product< ElementType, DIM >(v, v));
605 template<
typename GeometryTraits >
609 const GeometryTraits >)
611 return gt.length(p1, p2);
615 template<
typename ElementType >
618 const std::vector< ElementType > &v2)
620 std::vector< ElementType > sub_res(
sub(v1, v2));
621 return sqrt(dot_product< ElementType >(sub_res, sub_res));
625 template<
typename GeometryTraits >
633 template<
typename ElementType >
637 return sqrt(dot_product< ElementType >(v, v));
643 template<
typename GeometryTraits >
647 return gt.normalize(v);
651 template<
typename ElementType >
654 const std::vector< ElementType > &v2
677 template<
typename GeometryTraits >
684 std::vector< typename GeometryTraits::Scalar >{v1[0], v1[1], v1[2]},
685 std::vector< typename GeometryTraits::Scalar >{v2[0], v2[1], v2[2]});
689 template<
typename ElementType >
692 const std::vector< ElementType > &p2,
693 const std::vector< ElementType > &p3)
695 return are_collinear< ElementType >(sub< ElementType >(p2, p1),
696 sub< ElementType >(p1, p3));
699 template<
typename GeometryTraits >
706 std::vector< typename GeometryTraits::Scalar >{p1[0], p1[1], p1[2]},
707 std::vector< typename GeometryTraits::Scalar >{p2[0], p2[1], p2[2]},
708 std::vector< typename GeometryTraits::Scalar >{p3[0], p3[1], p3[2]});
712 template<
typename ElementType >
713 static std::vector< ElementType >
714 scalar_mult(
const std::vector< ElementType > &v1, ElementType coef)
716 size_t nb_e = v1.size();
718 std::vector< ElementType > res(nb_e);
720 for(
size_t i = 0; i < nb_e; ++i)
721 res[i] = v1[i] * coef;
726 template<
typename GeometryTraits >
730 const GeometryTraits >)
732 return gt.scalar_mult(v, coef);
736 template<
typename ElementType,
int DIM >
739 const ElementType v2[DIM])
741 double cosphi = Vector::dot_product< ElementType, DIM >(v1, v2);
745 return std::atan2(sinphi, cosphi);
747 template<
typename ElementType,
int DIM >
750 const ElementType v2[DIM])
753 (Vector::get_angle_from_unit_vectors< ElementType, DIM >(v1, v2)));
757 template<
typename ElementType >
760 const std::vector< ElementType > &v2)
762 double cosphi = Vector::dot_product< ElementType >(v1, v2);
766 return std::atan2(sinphi, cosphi);
768 template<
typename ElementType >
771 const std::vector< ElementType > &v2)
773 return rad2deg((Vector::get_angle_from_unit_vectors< ElementType >(v1, v2)));
777 template<
class ElementType >
780 const std::vector< ElementType > &v2)
782 std::vector< ElementType > copy_v1(v1);
783 double lv1 = std::sqrt(
dot_product(copy_v1, copy_v1));
787 copy_v1 = std::vector< ElementType >(copy_v1.size(), 0);
789 std::vector< ElementType > copy_v2(v2);
791 double lv2 = std::sqrt(
dot_product(copy_v2, copy_v2));
795 copy_v2 = std::vector< ElementType >(copy_v2.size(), 0);
799 template<
typename ElementType >
802 const std::vector< ElementType > &v2)
805 (Vector::get_angle_from_non_unit_vectors< ElementType >(v1, v2)));
807 template<
class ElementType >
810 const std::vector< ElementType > &p1,
811 const std::vector< ElementType > &p2,
812 const std::vector< ElementType > &p3)
814 std::vector< ElementType > v1 =
sub(p1, p2);
815 std::vector< ElementType > v2 =
sub(p3, p2);
819 template<
typename ElementType >
822 const std::vector< ElementType > &p1,
823 const std::vector< ElementType > &p2,
824 const std::vector< ElementType > &p3)
826 return rad2deg((Vector::get_angle_from3positions< ElementType >(p1, p2, p3)));
831 template<
typename ElementType >
834 const std::vector< ElementType >
836 const std::vector< ElementType >
839 const std::vector< ElementType > &new_norm,
841 std::vector< ElementType >
843 std::vector< ElementType >
849 std::vector< ElementType > old_norm =
cross_product(old_u, old_v);
851 static_cast< ElementType
>(
dot_product(old_norm, new_norm));
858 std::vector< ElementType > perp_old =
860 std::vector< ElementType > dperp =
865 dperp,
static_cast< ElementType
>(
dot_product(new_u, perp_old))));
869 dperp,
static_cast< ElementType
>(
dot_product(new_v, perp_old))));
876 template<
typename ElementType >
879 const std::vector< ElementType > &old_v,
883 const std::vector< ElementType > &new_u,
884 const std::vector< ElementType > &new_v,
886 ElementType &new_kuv,
890 std::vector< ElementType > r_new_u, r_new_v;
893 ElementType u1 =
static_cast< ElementType
>(
dot_product(r_new_u, old_u));
894 ElementType v1 =
static_cast< ElementType
>(
dot_product(r_new_u, old_v));
895 ElementType u2 =
static_cast< ElementType
>(
dot_product(r_new_v, old_u));
896 ElementType v2 =
static_cast< ElementType
>(
dot_product(r_new_v, old_v));
897 new_ku = old_ku * u1 * u1 + old_kuv * (2.0f * u1 * v1) + old_kv * v1 * v1;
898 new_kuv = old_ku * u1 * u2 + old_kuv * (u1 * v2 + u2 * v1) + old_kv * v1 * v2;
899 new_kv = old_ku * u2 * u2 + old_kuv * (2.0f * u2 * v2) + old_kv * v2 * v2;
907 template<
typename CoordinateType,
size_t N >
908 static std::vector< CoordinateType >
909 covar(
const CoordinateType point_coords[3 * N])
911 std::vector< CoordinateType > cd(9);
914 for(
size_t r = 0; r < N; r++)
915 cd[0] += point_coords[r * 3] * point_coords[r * 3];
916 for(
size_t j = 0; j <= 1; j++)
918 cd[1 + j * 3] = 0.0f;
919 for(
size_t r = 0; r < N; r++)
921 cd[1 + j * 3] += point_coords[1 + r * 3] * point_coords[j + r * 3];
924 for(
size_t j = 0; j <= 2; j++)
926 cd[2 + j * 3] = 0.0f;
927 for(
size_t r = 0; r < N; r++)
929 cd[2 + j * 3] += point_coords[2 + r * 3] * point_coords[j + r * 3];
941 template<
typename CoordinateType >
942 static std::vector< CoordinateType >
943 covar(
const std::vector< CoordinateType > &point_coords)
945 std::vector< CoordinateType > cd(9);
946 const size_t n = point_coords.size() / 3;
948 for(
size_t r = 0; r < n; r++)
949 cd[0] += point_coords[r * 3] * point_coords[r * 3];
950 for(
size_t j = 0; j <= 1; j++)
952 cd[1 + j * 3] = 0.0f;
953 for(
size_t r = 0; r < n; r++)
955 cd[1 + j * 3] += point_coords[1 + r * 3] * point_coords[j + r * 3];
958 for(
size_t j = 0; j <= 2; j++)
960 cd[2 + j * 3] = 0.0f;
961 for(
size_t r = 0; r < n; r++)
963 cd[2 + j * 3] += point_coords[2 + r * 3] * point_coords[j + r * 3];
974 template<
typename ElementType,
int DIM = 3 >
977 ElementType pp_matrix[][DIM],
981 for(
int i = 0; i < DIM; ++i)
982 for(j = 0; j < DIM; ++j)
983 pp_matrix[i][j] = coeff * p_vector[i] * p_vector[j];
989 template<
typename ElementType,
int DIM = 3 >
992 const ElementType pp_matrix[DIM][DIM],
993 const ElementType in_vertex[DIM],
995 ElementType out_vertex[DIM]
1001 for(
int i = 0; i < DIM; ++i)
1004 for(j = 0; j < DIM; ++j)
1005 out_vertex[i] += pp_matrix[i][j] * in_vertex[j];
1012 template<
typename ElementType,
int DIM,
int N >
1015 const ElementType pp_matrix[DIM][DIM],
1018 in_vertices[DIM][N],
1019 ElementType out_vertices[DIM]
1024 for(
int i = 0; i < DIM; ++i)
1026 for(
int n = 0; n < N; ++n)
1028 out_vertices[i][n] = 0;
1029 for(j = 0; j < DIM; ++j)
1030 out_vertices[i][n] += pp_matrix[i][j] * in_vertices[j][n];
1036 template<
typename ElementType,
int DIM >
1038 add(
const ElementType p_matrix[][DIM], ElementType p_matrix_sum[][DIM])
1041 for(
int i = 0; i < DIM; ++i)
1042 for(j = 0; j < DIM; ++j)
1043 p_matrix_sum[i][j] += p_matrix[i][j];
1046 template<
typename ElementType,
int DIM >
1051 for(
int i = 0; i < DIM; ++i)
1052 for(j = 0; j < DIM; ++j)