vgl_norm_trans_3d.hxx
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_norm_trans_3d.hxx
2 #ifndef vgl_norm_trans_3d_hxx_
3 #define vgl_norm_trans_3d_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include "vgl_norm_trans_3d.h"
9 #include <vgl/vgl_point_3d.h>
10 #include <vnl/vnl_vector_fixed.h>
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 //--------------------------------------------------------------
16 //
17 
18 //: Default constructor
19 template <class T>
21 {
22 }
23 
24 //: Copy constructor
25 template <class T>
27 : vgl_h_matrix_3d<T>(M)
28 {
29 }
30 
31 
32 //: Constructor from std::istream
33 template <class T>
35 : vgl_h_matrix_3d<T>(s)
36 {
37 }
38 
39 //: Constructor from file
40 template <class T>
42 : vgl_h_matrix_3d<T>(filename)
43 {
44 }
45 
46 //--------------------------------------------------------------
47 //: Constructor
48 template <class T>
49 vgl_norm_trans_3d<T>::vgl_norm_trans_3d(vnl_matrix_fixed<T,4,4> const& M)
50 : vgl_h_matrix_3d<T>(M)
51 {
52 }
53 
54 //--------------------------------------------------------------
55 //: Constructor
56 template <class T>
58 : vgl_h_matrix_3d<T>(H)
59 {
60 }
61 
62 //: Destructor
63 template <class T>
65 
66 // == OPERATIONS ==
67 //----------------------------------------------------------------
68 // Get the normalizing transform for a set of points
69 // 1) Compute the center of gravity and form the normalizing
70 // transformation matrix
71 // 2) Transform the point set to a temporary collection
72 // 3) Compute the average point radius
73 // 4) Complete the normalizing transform
74 template <class T>
76 compute_from_points(std::vector<vgl_homg_point_3d<T> > const& points)
77 {
78  T cx, cy, cz, radius;
79  this->center_of_mass(points, cx, cy, cz);
81  std::vector<vgl_homg_point_3d<T> > temp;
82  for (typename std::vector<vgl_homg_point_3d<T> >::const_iterator
83  pit = points.begin(); pit != points.end(); pit++)
84  {
85  vgl_homg_point_3d<T> p((*this)(*pit));
86  temp.push_back(p);
87  }
88  //Points might be coincident
89  if (!this->scale_xyzroot2(temp, radius))
90  return false;
91  vgl_h_matrix_3d<T>::set_scale(T(1)/radius);
92  return true;
93 }
94 
95 //-------------------------------------------------------------------
96 // Find the center of a point cloud
97 //
98 template <class T>
100 center_of_mass(std::vector<vgl_homg_point_3d<T> > const& in,
101  T& cx, T& cy, T& cz)
102 {
103  T cog_x = 0;
104  T cog_y = 0;
105  T cog_z = 0;
106  T cog_count = 0;
107  T tol = 1e-06f;
108  unsigned n = in.size();
109  for (unsigned i = 0; i < n; ++i)
110  {
111  if (in[i].ideal(tol))
112  continue;
113  vgl_point_3d<T> p(in[i]);
114  T x = p.x();
115  T y = p.y();
116  T z = p.z();
117  cog_x += x;
118  cog_y += y;
119  cog_z += z;
120  ++cog_count;
121  }
122  if (cog_count > 0)
123  {
124  cog_x /= cog_count;
125  cog_y /= cog_count;
126  cog_z /= cog_count;
127  }
128  //output result
129  cx = cog_x;
130  cy = cog_y;
131  cz = cog_z;
132 }
133 
134 //-------------------------------------------------------------------
135 // Find the mean distance of the input pointset. Assumed to have zero mean
136 //
137 template <class T>
139 scale_xyzroot2(std::vector<vgl_homg_point_3d<T> > const& in, T& radius)
140 {
141  T magnitude = T(0);
142  int numfinite = 0;
143  T tol = T(1e-06);
144  radius = T(0);
145  for (unsigned i = 0; i < in.size(); ++i)
146  {
147  if (in[i].ideal(tol))
148  continue;
149  vgl_point_3d<T> p(in[i]);
150  vnl_vector_fixed<T, 3> v(p.x(), p.y(), p.z());
151  magnitude += v.magnitude();
152  ++numfinite;
153  }
154 
155  if (numfinite > 0)
156  {
157  radius = magnitude / numfinite;
158  return radius>=tol;
159  }
160  return false;
161 }
162 
163 //----------------------------------------------------------------------------
164 #undef VGL_NORM_TRANS_3D_INSTANTIATE
165 #define VGL_NORM_TRANS_3D_INSTANTIATE(T) \
166 template class vgl_norm_trans_3d<T >
167 
168 #endif // vgl_norm_trans_3d_hxx_
vgl_h_matrix_3d & set_scale(T scale)
compose the current transform with a uniform scaling transformation, S.
vgl_h_matrix_3d & set_translation(T tx, T ty, T tz)
set H[0][3] = tx, H[1][3] = ty, and H[2][3] = tz, other elements unaltered.
vgl_norm_trans_3d()
Default constructor.
Represents a homogeneous 3D point.
Definition: vgl_fwd.h:9
Type & z()
Definition: vgl_point_3d.h:73
~vgl_norm_trans_3d()
Destructor.
#define v
Definition: vgl_vector_2d.h:74
bool compute_from_points(std::vector< vgl_homg_point_3d< T > > const &points)
compute the normalizing transform.
a point in 3D nonhomogeneous space
A class to hold a 3-d projective transformation matrix and to perform common operations using it e....
Definition: vgl_algo_fwd.h:12
Type & x()
Definition: vgl_point_3d.h:71
vgl_h_matrix_3d & set_identity()
initialize the transformation to identity.
static bool scale_xyzroot2(std::vector< vgl_homg_point_3d< T > > const &in, T &radius)
static void center_of_mass(std::vector< vgl_homg_point_3d< T > > const &points, T &cx, T &cy, T &cz)
The similarity transform that normalizes a 3-d point set.
Type & y()
Definition: vgl_point_3d.h:72