vgl_compute_similarity_3d.hxx
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_compute_similarity_3d.hxx
2 #ifndef vgl_compute_similarity_3d_hxx_
3 #define vgl_compute_similarity_3d_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
10 #include <vnl/vnl_double_4.h>
11 #include <vnl/algo/vnl_svd.h>
12 #include <vnl/algo/vnl_determinant.h>
13 #include <vnl/vnl_matrix.h>
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #include <cassert>
18 #include "vgl_compute_rigid_3d.h"
19 
20 template <class T>
22 vgl_compute_similarity_3d(std::vector<vgl_point_3d<T> > const& points1,
23  std::vector<vgl_point_3d<T> > const& points2)
24 : points1_(points1),
25  points2_(points2)
26 {
27  assert(points1.size() == points2.size());
28 }
29 
30 template <class T>
33 {
34  points1_.push_back(p1);
35  points2_.push_back(p2);
36 }
37 
38 
39 template <class T>
41 {
42  points1_.clear();
43  points2_.clear();
44 }
45 
46 
47 //: center all the points at the origin, and return the applied translation
48 template <class T>
50 center_points(std::vector<vgl_point_3d<T> >& pts,
51  vgl_vector_3d<T>& t) const
52 {
53  t.set(0,0,0);
54  vgl_point_3d<T> origin(0,0,0);
55  for (unsigned i=0; i<pts.size(); ++i)
56  {
57  t += origin - pts[i];
58  }
59  t /= pts.size();
60  for (unsigned i=0; i<pts.size(); ++i)
61  {
62  pts[i] += t;
63  }
64 }
65 
66 
67 //: normalize the scale of the points, and return the applied scale
68 // The average distance from the origin will be sqrt(3)
69 template <class T>
71 scale_points(std::vector<vgl_point_3d<T> >& pts,
72  T& s) const
73 {
74  s = 0.0;
75  vgl_point_3d<T> origin(0,0,0);
76  for (unsigned i=0; i<pts.size(); ++i)
77  {
78  s += (pts[i]-origin).length();
79  }
80  s = std::sqrt(3.0)*pts.size()/s;
81  for (unsigned i=0; i<pts.size(); ++i)
82  {
83  vgl_point_3d<T>& p = pts[i];
84  p.set(p.x()*s, p.y()*s, p.z()*s);
85  }
86 }
87 
88 
89 template <class T>
91 {
92  vgl_vector_3d<T> t1, t2;
93  std::vector<vgl_point_3d<T> > pts1(points1_), pts2(points2_);
94  center_points(pts1, t1);
95  center_points(pts2, t2);
96 
97  T s1, s2;
98  scale_points(pts2, s2);
99  scale_points(pts1, s1);
100  scale_ = s1/s2;
101 
102  vgl_compute_rigid_3d<T> rigid_comp(pts1, pts2);
103  rigid_comp.estimate();
104 
105  rotation_ = rigid_comp.rotation();
106  translation_ = (scale_*(rotation_*t1) - t2);
107 
108  return true;
109 }
110 
111 //--------------------------------------------------------------------------
112 #undef VGL_COMPUTE_SIMILARITY_3D_INSTANTIATE
113 #define VGL_COMPUTE_SIMILARITY_3D_INSTANTIATE(T) \
114 template class vgl_compute_similarity_3d<T >
115 
116 #endif // vgl_compute_similarity_3d_hxx_
void center_points(std::vector< vgl_point_3d< T > > &pts, vgl_vector_3d< T > &t) const
center all the points at the origin, and return the applied translation.
void add_points(vgl_point_3d< T > const &p1, vgl_point_3d< T > const &p2)
add a pair of points to point sets.
Compute a similarity transformation between two corresponding sets of 3D points.
double length(v const &a)
Return the length of a vector.
Definition: vgl_vector_2d.h:94
Type & z()
Definition: vgl_point_3d.h:73
vgl_compute_similarity_3d()=default
bool estimate()
estimates the similarity transformation from the stored points.
Compute a rigid transformation between two corresponding sets of 3D points.
const vgl_rotation_3d< T > & rotation() const
Access the estimated rotation.
bool estimate()
estimates the rigid transformation from the stored points.
Type & x()
Definition: vgl_point_3d.h:71
Direction vector in Euclidean 3D space, templated by type of element.
Definition: vgl_fwd.h:13
void clear()
clear internal data.
void set(T vx, T vy, T vz)
Assignment.
Definition: vgl_vector_3d.h:60
void set(Type px, Type py, Type pz)
Set x, y and z.
Definition: vgl_point_3d.h:81
void scale_points(std::vector< vgl_point_3d< T > > &pts, T &s) const
normalize the scale of the points, and return the applied scale.
The similarity transform that normalizes a 3-d point set.
Type & y()
Definition: vgl_point_3d.h:72