1 // Copyright (c) 2007, 2008 libmv authors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20 
21 #include "libmv/multiview/test_data_sets.h"
22 
23 #include <cmath>
24 
25 #include "libmv/numeric/numeric.h"
26 #include "libmv/multiview/projection.h"
27 #include "libmv/multiview/fundamental.h"
28 
29 namespace libmv {
30 
TwoRealisticCameras(bool same_K)31 TwoViewDataSet TwoRealisticCameras(bool same_K) {
32   TwoViewDataSet d;
33 
34   d.K1 << 320,   0, 160,
35             0, 320, 120,
36             0,   0,   1;
37   if (same_K) {
38     d.K2 = d.K1;
39   } else {
40     d.K2 << 360,   0, 170,
41               0, 360, 110,
42               0,   0,   1;
43   }
44   d.R1 = RotationAroundZ(-0.1);
45   d.R2 = RotationAroundX(-0.1);
46   d.t1 << 1, 1, 10;
47   d.t2 << -2, -1, 10;
48   P_From_KRt(d.K1, d.R1, d.t1, &d.P1);
49   P_From_KRt(d.K2, d.R2, d.t2, &d.P2);
50 
51   FundamentalFromProjections(d.P1, d.P2, &d.F);
52 
53   d.X.resize(3, 30);
54   d.X.setRandom();
55 
56   Project(d.P1, d.X, &d.x1);
57   Project(d.P2, d.X, &d.x2);
58 
59   return d;
60 }
61 
nViewDatasetConfigator(int fx,int fy,int cx,int cy,double distance,double jitter_amount)62 nViewDatasetConfigator::nViewDatasetConfigator(int fx ,  int fy,
63                                                int cx,   int cy,
64                                                double distance,
65                                                double jitter_amount) {
66   _fx = fx;
67   _fy = fy;
68   _cx = cx;
69   _cy = cy;
70   _dist = distance;
71   _jitter_amount = jitter_amount;
72 }
73 
NRealisticCamerasFull(int nviews,int npoints,const nViewDatasetConfigator config)74 NViewDataSet NRealisticCamerasFull(int nviews, int npoints,
75                                    const nViewDatasetConfigator config) {
76   NViewDataSet d;
77   d.n = nviews;
78   d.K.resize(nviews);
79   d.R.resize(nviews);
80   d.t.resize(nviews);
81   d.C.resize(nviews);
82   d.x.resize(nviews);
83   d.x_ids.resize(nviews);
84 
85   d.X.resize(3, npoints);
86   d.X.setRandom();
87   d.X *= 0.6;
88 
89   Vecu all_point_ids(npoints);
90   for (size_t j = 0; j < npoints; ++j)
91     all_point_ids[j] = j;
92 
93   for (size_t i = 0; i < nviews; ++i) {
94     Vec3 camera_center, t, jitter, lookdir;
95 
96     double theta = i * 2 * M_PI / nviews;
97     camera_center << sin(theta), 0.0, cos(theta);
98     camera_center *= config._dist;
99     d.C[i] = camera_center;
100 
101     jitter.setRandom();
102     jitter *= config._jitter_amount / camera_center.norm();
103     lookdir = -camera_center + jitter;
104 
105     d.K[i] << config._fx, 0,          config._cx,
106                0,         config._fy, config._cy,
107                0,         0,          1;
108     d.R[i] = LookAt(lookdir);
109     d.t[i] = -d.R[i] * camera_center;
110     d.x[i] = Project(d.P(i), d.X);
111     d.x_ids[i] = all_point_ids;
112   }
113   return d;
114 }
115 
116 
NRealisticCamerasSparse(int nviews,int npoints,float view_ratio,unsigned min_projections,const nViewDatasetConfigator config)117 NViewDataSet NRealisticCamerasSparse(int nviews, int npoints,
118                                      float view_ratio, unsigned min_projections,
119                                      const nViewDatasetConfigator config) {
120   assert(view_ratio <= 1.0);
121   assert(view_ratio > 0.0);
122   assert(min_projections <= npoints);
123   NViewDataSet d;
124   d.n = nviews;
125   d.K.resize(nviews);
126   d.R.resize(nviews);
127   d.t.resize(nviews);
128   d.C.resize(nviews);
129   d.x.resize(nviews);
130   d.x_ids.resize(nviews);
131 
132   d.X.resize(3, npoints);
133   d.X.setRandom();
134   d.X *= 0.6;
135 
136   Mat visibility(nviews, npoints);
137   visibility.setZero();
138   Mat randoms(nviews, npoints);
139   randoms.setRandom();
140   randoms = (randoms.array() + 1)/2.0;
141   unsigned num_visibles = 0;
142   for (size_t i = 0; i < nviews; ++i) {
143     num_visibles = 0;
144     for (size_t j = 0; j < npoints; j++) {
145       if (randoms(i, j) <= view_ratio) {
146         visibility(i, j) = true;
147         num_visibles++;
148       }
149     }
150     if (num_visibles < min_projections) {
151       unsigned num_projections_to_add = min_projections - num_visibles;
152       for (size_t j = 0; j < npoints && num_projections_to_add > 0; ++j) {
153         if (!visibility(i, j)) {
154           num_projections_to_add--;
155         }
156       }
157       num_visibles += num_projections_to_add;
158     }
159     d.x_ids[i].resize(num_visibles);
160     d.x[i].resize(2, num_visibles);
161   }
162 
163   size_t j_visible = 0;
164   Vec3 X;
165   for (size_t i = 0; i < nviews; ++i) {
166     Vec3 camera_center, t, jitter, lookdir;
167 
168     double theta = i * 2 * M_PI / nviews;
169     camera_center << sin(theta), 0.0, cos(theta);
170     camera_center *= config._dist;
171     d.C[i] = camera_center;
172 
173     jitter.setRandom();
174     jitter *= config._jitter_amount / camera_center.norm();
175     lookdir = -camera_center + jitter;
176 
177     d.K[i] << config._fx, 0,          config._cx,
178                0,         config._fy, config._cy,
179                0,         0,          1;
180     d.R[i] = LookAt(lookdir);
181     d.t[i] = -d.R[i] * camera_center;
182     j_visible = 0;
183     for (size_t j = 0; j < npoints; j++) {
184       if (visibility(i, j)) {
185         X =  d.X.col(j);
186         d.x[i].col(j_visible) = Project(d.P(i), X);
187         d.x_ids[i][j_visible] = j;
188         j_visible++;
189       }
190     }
191   }
192   return d;
193 }
194 
195 
196 }  // namespace libmv
197