KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
edge_chamfer.h
Go to the documentation of this file.
1 /* $Id: edge_chamfer.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_CHAMFER_CPP
21 #define KJB_CHAMFER_CPP
22 
23 #include <boost/shared_ptr.hpp>
24 #include <g/g_chamfer.h>
25 #include <edge_cpp/edge.h>
26 #include <m_cpp/m_matrix.h>
27 #include <vector>
28 #include <string>
29 
30 #include <l_cpp/l_serialization.h>
31 #include <l_cpp/l_int_matrix.h>
32 
33 #ifdef KJB_HAVE_BST_SERIAL
34 #include <boost/serialization/access.hpp>
35 #endif
36 
37 namespace kjb
38 {
39 
41 {
42  /*
43  * TODO:
44  * Make it possible to use existing framebuffer objects in this (shared_pointers?)
45  * If reduce_module is already loaded before declaring this object, don't re-load it (singleton object).
46  */
47 #ifdef KJB_HAVE_BST_SERIAL
48  friend class boost::serialization::access;
49 #endif
50 
51  typedef Chamfer_transform Self;
52 public:
54  m_size(0),
55  m_edges(),
56  m_num_rows(0),
57  m_num_cols(0),
58  m_distances(),
59  m_edge_map(0)
60  {}
61 
62  Chamfer_transform(const Edge_set_ptr edges, int size = 3) :
63  m_size(size),
64  m_edges(edges),
65  m_num_rows(edges->num_rows()),
66  m_num_cols(edges->num_cols()),
67  m_distances(), // this might not be necessary and could be made switchable
68  m_edge_map(0)
69  {
70  kjb_c::Matrix* c_distances = 0;
71  const kjb_c::Edge_point*** c_edge_map = NULL;
72 
73  chamfer_transform_2(
74  edges->c_ptr(),
75  edges->num_rows(),
76  edges->num_cols(),
77  size,
78  &c_distances,
79  &c_edge_map);
80 
81  m_distances = Matrix(c_distances);
82 
83  // CONVERT DOUBLE-POINTER TO VECTOR OF VECTORS
84 
85  // create array of rows
86  m_edge_map = std::vector<std::vector<const kjb_c::Edge_point*> >(m_num_rows);
87  for(int row = 0; row < m_num_rows; row++)
88  {
89  // create array of columns
90  m_edge_map[row] = std::vector<const kjb_c::Edge_point*>(m_num_cols);
91  // populate column
92  for(int col = 0; col < m_num_cols; col++)
93  {
94  m_edge_map[row][col] = c_edge_map[row][col];
95  }
96  }
97 
98  // free c-style 2D array.
99  kjb_c::free_2D_ptr_array((void***) c_edge_map);
100  }
101 
102  Chamfer_transform(const std::string& fname) :
103  m_size(0),
104  m_edges(),
105  m_num_rows(0),
106  m_num_cols(0),
107  m_distances(),
108  m_edge_map(0)
109  {
110  // use serialization framework to load from file
111  load(*this, fname);
112  }
113 
114  Chamfer_transform(const Self& other);
115 
117  {
118  }
119 
120  Self& operator=(const Self& other)
121  {
122  Self tmp(other);
123  swap(tmp);
124  return *this;
125  }
126 
127  void swap(Self& other)
128  {
129  using std::swap;
130 
131  swap(m_size, other.m_size);
132  swap(m_edges, other.m_edges);
133  swap(m_num_rows, other.m_num_rows);
134  swap(m_num_cols, other.m_num_cols);
135  m_distances.swap(other.m_distances);
136  m_edge_map.swap(other.m_edge_map);
137  }
138 
139  const kjb_c::Edge_point& nearest_edge(int row, int col)
140  {
141  assert(row >= 0);
142  assert(row < m_num_rows);
143  assert(col >= 0);
144  assert(col < m_num_cols);
145 
146  return *m_edge_map[row][col];
147  }
148 
149  double nearest_distance(int row, int col)
150  {
151  assert(row >= 0);
152  assert(row < m_num_rows);
153  assert(col >= 0);
154  assert(col < m_num_cols);
155 
156  return m_distances(row, col);
157  }
158 
159  int get_num_rows() const { return m_num_rows; }
160  int get_num_cols() const { return m_num_cols; }
161 
162  const std::vector<std::vector<const kjb_c::Edge_point*> >& edge_map() const { return m_edge_map; }
163  const Matrix& distance_map() const { return m_distances; }
164 
170  std::vector<Int_matrix> position_map() const
171  {
172  std::vector<Int_matrix> result(2);
173  position_map(result[0], result[1]);
174  return result;
175  }
176 
182  void position_map(Int_matrix& row_positions, Int_matrix& col_positions) const
183  {
184  row_positions = col_positions = Int_matrix(m_num_rows, m_num_cols);
185 
186  for(int row = 0; row < m_num_rows; row++)
187  for(int col = 0; col < m_num_cols; col++)
188  {
189  row_positions(row, col) = (*m_edge_map[row][col]).row;
190  col_positions(row, col) = (*m_edge_map[row][col]).col;
191  }
192  }
193 
194  size_t get_num_points() const
195  {
196  return m_edges->get_total_edge_points();
197  }
198 private:
199  int m_size;
200 
201  // these edges may be owned by a number of other objects,
202  // and we don't want to rely on the fact that they won't be
203  // freed while this object exists.
204  Edge_set_ptr m_edges;
205 
206  int m_num_rows;
207  int m_num_cols;
208 
209  Matrix m_distances;
210  std::vector<std::vector<const kjb_c::Edge_point*> > m_edge_map;
211 
212 #ifdef KJB_HAVE_BST_SERIAL
213  template <class Archive>
214  void serialize(Archive& /* ar */, const unsigned int /* version */)
215  {
217 // ar & m_size;
218 // ar & m_edges;
219 // ar & m_num_rows;
220 // ar & m_num_cols;
221 //
222 // ar & m_distances;
223 // ar & m_edge_map;
224  }
225 #endif
226 };
227 
228 typedef boost::shared_ptr<Chamfer_transform> Chamfer_transform_ptr;
229 
230 inline void swap(Chamfer_transform& op1, Chamfer_transform& op2)
231 {
232  op1.swap(op2);
233 }
234 
235 }
236 #endif
Chamfer_transform()
Definition: edge_chamfer.h:53
Definition for the Int_matrix class, a thin wrapper on the KJB Int_matrix struct and its related func...
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
~Chamfer_transform()
Definition: edge_chamfer.h:116
int get_num_cols() const
Definition: edge_chamfer.h:160
Definition: edge_chamfer.h:40
int get_num_rows() const
Definition: edge_chamfer.h:159
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
#define KJB_THROW(ex)
Definition: l_exception.h:46
Chamfer_transform(const Edge_set_ptr edges, int size=3)
Definition: edge_chamfer.h:62
Self & operator=(const Self &other)
Definition: edge_chamfer.h:120
void swap(Self &other)
Definition: edge_chamfer.h:127
const Matrix & distance_map() const
Definition: edge_chamfer.h:163
Chamfer_transform(const std::string &fname)
Definition: edge_chamfer.h:102
boost::shared_ptr< Edge_set > Edge_set_ptr
Definition: edge.h:613
void load(Edge_set &edges, const std::string &fname)
Definition: edge.h:603
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
void position_map(Int_matrix &row_positions, Int_matrix &col_positions) const
Definition: edge_chamfer.h:182
std::vector< Int_matrix > position_map() const
Definition: edge_chamfer.h:170
const kjb_c::Edge_point & nearest_edge(int row, int col)
Definition: edge_chamfer.h:139
size_t get_num_points() const
Definition: edge_chamfer.h:194
Object thrown when attempting to use unimplemented functionality.
Definition: l_exception.h:281
edges
Definition: APPgetLargeConnectedEdges.m:85
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
void swap(Matrix &other)
Swap the representations of two matrices.
Definition: m_matrix.h:532
double nearest_distance(int row, int col)
Definition: edge_chamfer.h:149
boost::shared_ptr< Chamfer_transform > Chamfer_transform_ptr
Definition: edge_chamfer.h:228
const std::vector< std::vector< const kjb_c::Edge_point * > > & edge_map() const
Definition: edge_chamfer.h:162