Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_persp_mes_list.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2000-19 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_persp_mes_list.h"
17 
18 const double sd_pos = 0.01;
19 const double sd_ang = 0.001;
20 const double sd_sxy = 0.1;
21 const double sd_st = 0.1;
22 const double sd_uv0 = 0.1;
23 const double sd_K1 = 0.0001;
24 const double sd_K2 = 0.00001;
25 const double sd_P1 = 0.0001;
26 const double sd_P2 = 0.00001;
27 
28 //---------------------------------------------------------------------------
29 a_persp_mes_list::a_persp_mes_list(const std::string file)
30 {
31  file_ = file;
32  std::ifstream in(file.c_str());
33  if (in)
34  in >> *this;
35  else
36  throw file_error();
37 }
38 //---------------------------------------------------------------------------
40 {
41  for (std::vector<a_unwarp *>::iterator w = cam_unwarp_.begin(); w < cam_unwarp_.end(); w++)
42  delete (*w);
43  cam_unwarp_.clear();
44  for (std::vector<a_intern *>::iterator i = cam_intern_.begin(); i < cam_intern_.end(); i++)
45  delete (*i);
46  cam_intern_.clear();
47  for (std::vector<a_pos2 *>::iterator p = pos_.begin(); p < pos_.end(); p++)
48  delete (*p);
49  pos_.clear();
50  for (std::vector<a_persp_mes *>::iterator m = measures_.begin(); m < measures_.end(); m++)
51  delete (*m);
52  measures_.clear();
53 }
54 //---------------------------------------------------------------------------
55 double a_persp_mes_list::getX(int ai) const
56 {
57  int nc = cam_unwarp_.size();
58  int np = pos_.size();
59  if (ai<6*np)
60  {
61  a_pos2 * pos = pos_[ai/6];
62  switch (ai%6)
63  {
64  case 0: return pos->ox()/sd_pos;
65  case 1: return pos->oy()/sd_pos;
66  case 2: return pos->oz()/sd_pos;
67  case 3: return pos->ax()/sd_ang;
68  case 4: return pos->ay()/sd_ang;
69  case 5: return pos->az()/sd_ang;
70  }
71  }
72  else if (ai<6*np+5*nc)
73  {
74  ai -= 6*np;
75  a_intern * intern = cam_intern_[ai/5];
76  a_unwarp * unwarp;
77  switch (ai%5)
78  {
79  case 0: return intern->sx()/sd_sxy;
80  case 1: return intern->sy()/sd_sxy;
81  case 2: return intern->stheta()/sd_st;
82  case 3: return intern->u0()/sd_uv0;
83  case 4: return intern->v0()/sd_uv0;
84  }
85  }
86  else
87  {
88  ai -= 6*np+5*nc;
89  a_unwarp * unwarp = cam_unwarp_[ai/4];
90  switch (ai%4)
91  {
92  case 0: return unwarp->K1()/sd_K1;
93  case 1: return unwarp->K2()/sd_K2;
94  case 2: return unwarp->P1()/sd_P1;
95  case 3: return unwarp->P2()/sd_P2;
96  }
97  }
98  return 0.; // just for the sake of eliminating compiler warning
99 }
100 //---------------------------------------------------------------------------
101 void a_persp_mes_list::setX(int ai, double val)
102 {
103  int nc = cam_unwarp_.size();
104  int np = pos_.size();
105  if (ai<6*np)
106  {
107  a_pos2 * pos = pos_[ai/6];
108  switch (ai%6)
109  {
110  case 0: pos->ox(val*sd_pos); break;
111  case 1: pos->oy(val*sd_pos); break;
112  case 2: pos->oz(val*sd_pos); break;
113  case 3: pos->ax(val*sd_ang); break;
114  case 4: pos->ay(val*sd_ang); break;
115  case 5: pos->az(val*sd_ang);
116  }
117  }
118  else if (ai<6*np+5*nc)
119  {
120  ai -= 6*np;
121  a_intern * intern = cam_intern_[ai/5];
122  a_unwarp * unwarp;
123  switch (ai%5)
124  {
125  case 0: intern->sx(val*sd_sxy); break;
126  case 1: intern->sy(val*sd_sxy); break;
127  case 2: intern->stheta(val*sd_st); break;
128  case 3:
129  intern->u0(val*sd_uv0);
130  unwarp = cam_unwarp_[ai/5];
131  unwarp->u0(val*sd_uv0);
132  break;
133  case 4:
134  intern->v0(val*sd_uv0);
135  unwarp = cam_unwarp_[ai/5];
136  unwarp->v0(val*sd_uv0);
137  }
138  }
139  else
140  {
141  ai -= 6*np+5*nc;
142  a_unwarp * unwarp = cam_unwarp_[ai/4];
143  switch (ai%4)
144  {
145  case 0: unwarp->K1(val*sd_K1); break;
146  case 1: unwarp->K2(val*sd_K2); break;
147  case 2: unwarp->P1(val*sd_P1); break;
148  case 3: unwarp->P2(val*sd_P2);
149  }
150  }
151 }
152 //---------------------------------------------------------------------------
154 {
155  double d = 0;
156  for (int i = 0; i < measures_.size(); i++)
157  {
158  a_persp_mes * meas = measures_[i];
159  a_unwarp * unwarp = cam_unwarp_[meas->cam()];
160  a_intern * intern = cam_intern_[meas->cam()];
161  a_pos2 * pos = pos_[meas->pos()];
162  d += meas->residual(unwarp,intern,pos);
163  }
164  return d;
165 }
166 //---------------------------------------------------------------------------
167 std::istream& operator>> (std::istream& i, a_persp_mes_list& l)
168 {
169  int n_cam,n_pos,n_meas;
170  i >> n_cam >> n_pos >> n_meas;
171  for (int j = 0; j < n_cam; j++)
172  {
173  auto unwarp = new a_unwarp;
174  i >> *unwarp;
175  l.cam_unwarp_.push_back(unwarp);
176  auto intern = new a_intern;
177  i >> *intern;
178  l.cam_intern_.push_back(intern);
179  }
180  for (int j = 0; j < n_pos; j++)
181  {
182  auto pos = new a_pos2;
183  i >> *pos;
184  l.pos_.push_back(pos);
185  }
186  for (int j = 0; j < n_meas; j++)
187  {
188  auto meas = new a_persp_mes;
189  l.measures_.push_back(meas);
190  i >> *meas;
191  }
192  return i;
193 }
194 //---------------------------------------------------------------------------
195 std::ostream& operator<< (std::ostream& o, const a_persp_mes_list& l)
196 {
197  o << l.cam_unwarp_.size() << " " << l.pos_.size() << " " << l.measures_.size() << std::endl;
198  for (int i = 0; i < l.cam_unwarp_.size(); i++)
199  {
200  o << *(l.cam_unwarp_[i]);
201  o << *(l.cam_intern_[i]);
202  }
203  for (int i = 0; i < l.pos_.size(); i++)
204  o << *(l.pos_[i]);
205  for (int i = 0; i < l.measures_.size(); i++)
206  o << *(l.measures_[i]);
207  return o;
208 }
std::istream & operator>>(std::istream &i, a_persp_mes_list &l)
const double sd_K1
const double sd_uv0
const double sd_st
const double sd_P2
const double sd_P1
const double sd_pos
std::ostream & operator<<(std::ostream &o, const a_persp_mes_list &l)
const double sd_K2
const double sd_ang
const double sd_sxy
intern calibration matrix
Definition: a_intern.h:29
void sy(double v)
Definition: a_intern.h:36
void sx(double v)
Definition: a_intern.h:35
void v0(double v)
Definition: a_intern.h:39
void u0(double v)
Definition: a_intern.h:38
void stheta(double v)
Definition: a_intern.h:37
a_persp_mes_list
void setX(int ai, double val)
a_persp_mes_list(const std::string name)
double getX(int ai) const
std::vector< a_persp_mes * > measures_
std::vector< a_pos2 * > pos_
std::vector< a_intern * > cam_intern_
std::vector< a_unwarp * > cam_unwarp_
a_persp_mes
Definition: a_persp_mes.h:33
void pos(int pos)
Definition: a_persp_mes.h:39
void cam(int cam)
Definition: a_persp_mes.h:38
double residual(a_unwarp *, a_intern *, a_pos2 *)
Definition: a_persp_mes.cxx:47
a position matrix
Definition: a_pos2.h:29
void ay(double v)
Definition: a_pos2.h:51
void ox(double v)
Definition: a_pos2.h:43
void oz(double v)
Definition: a_pos2.h:47
void ax(double v)
Definition: a_pos2.h:49
void az(double v)
Definition: a_pos2.h:53
void oy(double v)
Definition: a_pos2.h:45
void K2(double v)
Definition: a_unwarp.h:33
void P1(double v)
Definition: a_unwarp.h:34
void u0(double v)
Definition: a_unwarp.h:30
void P2(double v)
Definition: a_unwarp.h:35
void v0(double v)
Definition: a_unwarp.h:31
void K1(double v)
Definition: a_unwarp.h:32
int val