00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00119 #ifndef __VCGLIB_SHOT
00120 #define __VCGLIB_SHOT
00121
00122 #include <vcg/space/point2.h>
00123 #include <vcg/space/point3.h>
00124 #include <vcg/math/similarity.h>
00125 #include <vcg/math/camera.h>
00126
00127 namespace vcg{
00128
00129 template <class S, class RotationType = Matrix44<S> >
00130 class Shot {
00131 public:
00132 typedef Camera<S> CameraType;
00133 typedef S ScalarType;
00134
00135 template <class ScalarType, class RotoType >
00136 class ReferenceFrame {
00137 friend class Shot<ScalarType, RotoType>;
00138 RotoType rot;
00139 Point3<S> tra;
00140 public:
00141 void SetIdentity(){ rot.SetIdentity(); tra = Point3<S>(0.0,0.0,0.0);}
00142 void SetTra(const Point3<S> & tr) {tra = tr;}
00143 void SetRot(const RotoType & rt) {rot = rt;}
00144 Point3<ScalarType> Tra() const { return tra;}
00145 RotoType Rot() const { return rot;}
00146 };
00147
00148 Camera<S> Intrinsics;
00149 ReferenceFrame<S,RotationType> Extrinsics;
00150
00151
00152 Shot(Camera<S> c)
00153 {
00154 Intrinsics = c;
00155 Extrinsics.SetIdentity();
00156 }
00157
00158 Shot()
00159 {
00160 Extrinsics.SetIdentity();
00161 }
00162
00163
00165 vcg::Point3<S> Axis(const int & i)const;
00166
00168 const vcg::Point3<S> GetViewPoint()const;
00170 void SetViewPoint(const vcg::Point3<S> & viewpoint);
00171
00173 void LookAt(const vcg::Point3<S> & point,const vcg::Point3<S> & up);
00174
00176 void LookAt(const S & eye_x,const S & eye_y,const S & eye_z,
00177 const S & at_x,const S & at_y,const S & at_z,
00178 const S & up_x,const S & up_y,const S & up_z);
00179
00181 void LookTowards(const vcg::Point3<S> & z_dir,const vcg::Point3<S> & up);
00182
00184 vcg::Point3<S> ConvertWorldToCameraCoordinates(const vcg::Point3<S> & p) const;
00185
00187 vcg::Point3<S> ConvertCameraToWorldCoordinates(const vcg::Point3<S> & p) const;
00188
00191 vcg::Point3<S> ConvertCameraToWorldCoordinates_Substitute(const vcg::Point3<S> & p) const;
00192
00194 vcg::Point2<S> Project(const vcg::Point3<S> & p) const;
00195
00197 vcg::Point3<S> UnProject(const vcg::Point2<S> & p, const S & d) const;
00198
00201 vcg::Point3<S> UnProject_Substitute(const vcg::Point2<S> & p, const S & d) const;
00202
00204 S Depth(const vcg::Point3<S> & p)const;
00205
00206
00207
00208 public:
00209
00210
00211
00212
00213 Matrix44<S> GetExtrinsicsToWorldMatrix() const {
00214 Matrix44<S> rotM ;
00215 Extrinsics.rot.ToMatrix(rotM);
00216 return Matrix44<S>().SetTranslate(Extrinsics.tra) * rotM.transpose();
00217 }
00218
00219
00220
00221
00222 Matrix44<S> GetWorldToExtrinsicsMatrix() const {
00223 Matrix44<S> rotM ;
00224 Extrinsics.rot.ToMatrix(rotM);
00225 return rotM * Matrix44<S>().SetTranslate(-Extrinsics.tra) ;
00226 }
00227
00228
00229
00230
00231 void MultMatrix( vcg::Matrix44<S> m44)
00232 {
00233 Extrinsics.tra = m44 * Extrinsics.tra;
00234 m44[0][3] = m44[1][3] = m44[2][3] = 0.0;
00235 Extrinsics.rot = m44 * Extrinsics.rot ;
00236 }
00237
00238
00239
00240
00241 void MultSimilarity( const Similarity<S> & s){ MultMatrix(s.Matrix());}
00242
00243 bool IsValid() const
00244 {
00245 return Intrinsics.PixelSizeMm[0]>0 && Intrinsics.PixelSizeMm[1]>0;
00246 }
00247
00248 };
00249
00250
00251
00252
00254 template <class S, class RotationType>
00255 const vcg::Point3<S> Shot<S,RotationType>::GetViewPoint() const
00256 {
00257 return Extrinsics.tra;
00258 }
00260 template <class S, class RotationType>
00261 void Shot<S,RotationType>::SetViewPoint(const vcg::Point3<S> & viewpoint)
00262 {
00263 Extrinsics.SetTra( viewpoint );
00264 }
00265
00266
00268 template <class S, class RotationType>
00269 vcg::Point3<S> Shot<S,RotationType>::Axis(const int & i) const
00270 {
00271 vcg::Matrix44<S> m;
00272 Extrinsics.rot.ToMatrix(m);
00273 vcg::Point3<S> aa = m.GetRow3(i);
00274 return aa;
00275 }
00276
00278 template <class S, class RotationType>
00279 void Shot<S,RotationType>::LookAt(const vcg::Point3<S> & z_dir,const vcg::Point3<S> & up)
00280 {
00281 LookTowards(z_dir-GetViewPoint(),up);
00282 }
00283
00285 template <class S, class RotationType>
00286 void Shot<S,RotationType>::LookAt(const S & eye_x, const S & eye_y, const S & eye_z,
00287 const S & at_x, const S & at_y, const S & at_z,
00288 const S & up_x,const S & up_y,const S & up_z)
00289 {
00290 SetViewPoint(Point3<S>(eye_x,eye_y,eye_z));
00291 LookAt(Point3<S>(at_x,at_y,at_z),Point3<S>(up_x,up_y,up_z));
00292 }
00293
00295 template <class S, class RotationType>
00296 void Shot<S,RotationType>::LookTowards(const vcg::Point3<S> & z_dir,const vcg::Point3<S> & up)
00297 {
00298 vcg::Point3<S> x_dir = up ^-z_dir;
00299 vcg::Point3<S> y_dir = -z_dir ^x_dir;
00300
00301 Matrix44<S> m;
00302 m.SetIdentity();
00303 *(vcg::Point3<S> *)&m[0][0] = x_dir/x_dir.Norm();
00304 *(vcg::Point3<S> *)&m[1][0] = y_dir/y_dir.Norm();
00305 *(vcg::Point3<S> *)&m[2][0] = -z_dir/z_dir.Norm();
00306
00307 Extrinsics.rot.FromMatrix(m);
00308 }
00309
00310
00311
00313 template <class S, class RotationType>
00314 vcg::Point3<S> Shot<S,RotationType>::ConvertWorldToCameraCoordinates(const vcg::Point3<S> & p) const
00315 {
00316 Matrix44<S> rotM;
00317 Extrinsics.rot.ToMatrix(rotM);
00318 vcg::Point3<S> cp = rotM * (p - GetViewPoint() );
00319 cp[2]=-cp[2];
00320 return cp;
00321 }
00322
00324 template <class S, class RotationType>
00325 vcg::Point3<S> Shot<S,RotationType>::ConvertCameraToWorldCoordinates(const vcg::Point3<S> & p) const
00326 {
00327 Matrix44<S> rotM;
00328 vcg::Point3<S> cp = p;
00329 cp[2]=-cp[2];
00330 Extrinsics.rot.ToMatrix(rotM);
00331 cp = rotM.transpose() * cp + GetViewPoint();
00332 return cp;
00333 }
00334
00337 template <class S, class RotationType>
00338 vcg::Point3<S> Shot<S,RotationType>::ConvertCameraToWorldCoordinates_Substitute(const vcg::Point3<S> & p) const
00339 {
00340 Matrix44<S> rotM;
00341 vcg::Point3<S> cp = p;
00342 cp[2]=-cp[2];
00343 Extrinsics.rot.ToMatrix(rotM);
00344 cp = Inverse(rotM) * cp + GetViewPoint();
00345 return cp;
00346 }
00347
00349 template <class S, class RotationType>
00350 vcg::Point2<S> Shot<S,RotationType>::Project(const vcg::Point3<S> & p) const
00351 {
00352 Point3<S> cp = ConvertWorldToCameraCoordinates(p);
00353 Point2<S> pp = Intrinsics.Project(cp);
00354 Point2<S> vp = Intrinsics.LocalToViewportPx(pp);
00355 return vp;
00356 }
00357
00359 template <class S, class RotationType>
00360 vcg::Point3<S> Shot<S,RotationType>::UnProject(const vcg::Point2<S> & p, const S & d) const
00361 {
00362 Point2<S> lp = Intrinsics.ViewportPxToLocal(p);
00363 Point3<S> cp = Intrinsics.UnProject(lp,d);
00364 Point3<S> wp = ConvertCameraToWorldCoordinates(cp);
00365 return wp;
00366 }
00367
00370 template <class S, class RotationType>
00371 vcg::Point3<S> Shot<S,RotationType>::UnProject_Substitute(const vcg::Point2<S> & p, const S & d) const
00372 {
00373 Point2<S> lp = Intrinsics.ViewportPxToLocal(p);
00374 Point3<S> cp = Intrinsics.UnProject(lp,d);
00375 Point3<S> wp = ConvertCameraToWorldCoordinates_Substitute(cp);
00376 return wp;
00377 }
00378
00380 template <class S, class RotationType>
00381 S Shot<S,RotationType>::Depth(const vcg::Point3<S> & p)const
00382 {
00383 return ConvertWorldToCameraCoordinates(p).Z();
00384 }
00385
00386
00387
00388
00389
00390
00391
00392 class Shotf: public Shot<float>{};
00393 class Shotd: public Shot<double>{};
00394
00395
00396 }
00397
00398 #endif
00399
00400
00401
00402