/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ /**************************************************************************** History $Log: not supported by cvs2svn $ Revision 1.18 2005/12/12 16:53:43 callieri corrected UnProject, it's necessary also a ZDepth value to perform inverse projection Revision 1.17 2005/12/07 10:57:52 callieri added commodity function ProjectWorldtoViewport() to obtain directly pixel indices without calling two separate function of two different objects Revision 1.16 2005/12/02 16:14:35 callieri in Shot::Axis changed Row3 to GetRow3 . row3 was the old method name of Matrix44 Revision 1.15 2005/12/01 01:03:37 cignoni Removed excess ';' from end of template functions, for gcc compiling Revision 1.14 2005/11/23 14:18:35 ganovelli added access to similarity (just for symmetry with Camera() ) Revision 1.13 2005/11/23 11:58:52 ganovelli Empty constructor added, untemplated class Shotf and Shotd added usage: Shotf myShot; corrected member access rights Revision 1.12 2005/07/11 13:12:35 cignoni small gcc-related compiling issues (typenames,ending cr, initialization order) Revision 1.11 2005/01/22 11:20:20 ponchio <...Point3.h> -> <...point3.h> Revision 1.10 2005/01/05 13:26:15 ganovelli corretto cambiamento di sistema di rif. Revision 1.9 2004/12/15 18:45:50 tommyfranken *** empty log message *** Revision 1.4 2004/10/07 14:41:31 fasano Little fix on ViewPoint() method Revision 1.3 2004/10/07 14:24:53 ganovelli added LookAt,LookToward Revision 1.2 2004/10/05 19:04:25 ganovelli version 5-10-2004 in progress Revision 1.1 2004/09/15 22:58:05 ganovelli re-creation Revision 1.2 2004/09/06 21:41:30 ganovelli *** empty log message *** Revision 1.1 2004/09/03 13:01:51 ganovelli creation ****************************************************************************/ #ifndef __VCGLIB_SHOT #define __VCGLIB_SHOT // #include // #include // #include #include #include #include #include namespace vcg{ template class Shot { public: typedef Camera CameraType; typedef S ScalarType; protected: enum { NOTVALID_BIT = 0x1, CREATED_EMPTY= 0x2 }; char flags; Camera & camera; // the camera that shot vcg::Similarity > similarity; // the position from where it did it public: Shot( Camera & c):camera(c){similarity.SetIdentity();flags=0;} Shot():camera(*new vcg::Camera()){similarity.SetIdentity();flags|=CREATED_EMPTY;} ~Shot(){if(flags&CREATED_EMPTY) delete &camera;} bool IsValid(){ return (flags& NOTVALID_BIT)==0;} void SetValid(bool v){ if(!v) flags|=NOTVALID_BIT; else flags&=~NOTVALID_BIT;} Camera & Camera(){return camera;}; /// access to similarity vcg::Similarity > & Similarity(){return similarity;} /// take the i-th axis of the coordinate system of the camera vcg::Point3 Axis(const int & i)const; /// take the viewpoint const vcg::Point3 ViewPoint()const; /// set the viewpoint void SetViewPoint(const vcg::Point3 & viewpoint); /// look at void LookAt(const vcg::Point3 & z_dir,const vcg::Point3 & up); /// look at (same as opengl) void LookAt(const S & eye_x,const S & eye_y,const S & eye_z,const S & at_x,const S & at_y,const S & at_z, const S & up_x,const S & up_y,const S & up_z); /// look towards void LookTowards(const vcg::Point3 & z_dir,const vcg::Point3 & up); /// convert a 3d point in camera coordinates vcg::Point3 ConvertToCameraCoordinates(const vcg::Point3 & p) const; /// convert a 3d point in camera coordinates vcg::Point3 ConvertToWorldCoordinates(const vcg::Point3 & p) const; /// project onto the camera plane vcg::Point2 Project(const vcg::Point3 & p) const; /// inverse projection from camera plane + Zdepth to original 3D vcg::Point3 UnProject(const vcg::Point2 & p, const S & d) const; /// take the distance from the point p and the plane parallel to the camera plane and passing through the view /// point. The would be z depth S Depth(const vcg::Point3 & p)const; /// project a 3d point from 3D WORLD to 2D VIEWPORT vcg::Point2 ProjectWorldtoViewport(const vcg::Point3 & p) const; }; // end class definition template const vcg::Point3 Shot::ViewPoint() const { //Matrix44 m = similarity.Matrix(); //return Point3(m[0][3],m[1][3],m[2][3]); return -similarity.tra; } template vcg::Point3 Shot::Axis(const int & i) const { vcg::Matrix44 m; similarity.rot.ToMatrix(m); vcg::Point3 aa = m.GetRow3(i); return aa; } template void Shot::SetViewPoint(const vcg::Point3 & viewpoint){ similarity.tra = -viewpoint; } template void Shot::LookAt(const vcg::Point3 & z_dir,const vcg::Point3 & up){ LookTowards(z_dir-ViewPoint(),up); } template void Shot::LookAt(const S & eye_x,const S & eye_y,const S & eye_z,const S & at_x,const S & at_y,const S & at_z, const S & up_x,const S & up_y,const S & up_z){ SetViewPoint(Point3(eye_x,eye_y,eye_z)); LookAt(Point3(at_x,at_y,at_z),Point3(up_x,up_y,up_z)); } template void Shot::LookTowards(const vcg::Point3 & z_dir,const vcg::Point3 & up){ vcg::Point3 x_dir = up ^-z_dir ; vcg::Point3 y_dir = -z_dir ^x_dir ; Matrix44 m; m.SetIdentity(); *(vcg::Point3 *)&m[0][0] = x_dir/x_dir.Norm(); *(vcg::Point3 *)&m[1][0] = y_dir/y_dir.Norm(); *(vcg::Point3 *)&m[2][0] = -z_dir/z_dir.Norm(); similarity.rot.FromMatrix(m); } template vcg::Point3 Shot::ConvertToCameraCoordinates(const vcg::Point3 & p) const{ Matrix44 rotM; similarity.rot.ToMatrix(rotM); vcg::Point3 cp = rotM * (p+similarity.tra); // note: the camera reference system is right handed cp[2]=-cp[2]; return cp; } template vcg::Point3 Shot::ConvertToWorldCoordinates(const vcg::Point3 & p) const{ Matrix44 rotM; vcg::Point3 cp = p; // note: the World reference system is left handed cp[2]=-cp[2]; similarity.rot.ToMatrix(rotM); cp = Inverse(rotM) * cp-similarity.tra; return cp; } template vcg::Point2 Shot::Project(const vcg::Point3 & p) const{ return camera.Project(ConvertToCameraCoordinates(p)); } template vcg::Point3 Shot::UnProject(const vcg::Point2 & p, const S & d) const{ Point2 tp = camera.ViewportToLocal(p); vcg::Point3 q = camera.UnProject(tp, d); return ConvertToWorldCoordinates(q); } template S Shot::Depth(const vcg::Point3 & p)const { return ConvertToCameraCoordinates(p).Z(); } template vcg::Point2 Shot::ProjectWorldtoViewport(const vcg::Point3 & p)const { return camera.LocalToViewport( camera.Project( ConvertToCameraCoordinates(p) ) ); } class Shotf: public Shot{}; class Shotd: public Shot{}; } // end name space #endif