OpenMesh
OpenMesh/Apps/Unsupported/Streaming-qt4/Client/VDPMClientViewerWidget.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 #ifndef OPENMESH_APPS_VDPMSTREAMING_CLIENT_VDPMCLIENTVIEWERWIDGET_HH
00043 #define OPENMESH_APPS_VDPMSTREAMING_CLIENT_VDPMCLIENTVIEWERWIDGET_HH
00044 
00045 //== INCLUDES =================================================================
00046 
00047 #include <QTimer>
00048 #include <QTcpSocket>
00049 #include <QDataStream>
00050 #include <iostream>
00051 #include <string>
00052 
00053 #include <OpenMesh/Core/IO/MeshIO.hh>
00054 #include <OpenMesh/Core/Geometry/Plane3d.hh>
00055 #include <OpenMesh/Tools/Utils/Timer.hh>
00056 #include <OpenMesh/Tools/VDPM/StreamingDef.hh>
00057 #include <OpenMesh/Tools/VDPM/ViewingParameters.hh>
00058 #include <OpenMesh/Tools/VDPM/VHierarchy.hh>
00059 #include <OpenMesh/Tools/VDPM/VFront.hh>
00060 
00061 #include <OpenMesh/Apps/VDProgMesh/Streaming/Client/MeshViewerWidgetT.hh>
00062 #include <OpenMesh/Apps/VDProgMesh/Streaming/Client/MyMesh.hh>
00063 //#include <OpenMesh/Apps/VDProgMesh/Streaming/Client/VDPMClientSession.hh>
00064 
00065 
00066 
00067 typedef MeshViewerWidgetT<MyMesh>                 MeshViewerWidget;
00068 
00069 
00070 
00071 using OpenMesh::VDPM::VDPMStreamingPhase;
00072 using OpenMesh::VDPM::kVSplitHeader;
00073 using OpenMesh::VDPM::kVSplits;
00074 using OpenMesh::VDPM::kBaseMesh;
00075 
00076 using OpenMesh::VDPM::Plane3d;
00077 
00078 using OpenMesh::VDPM::VFront;
00079 using OpenMesh::VDPM::VHierarchy;
00080 using OpenMesh::VDPM::VHierarchyNodeIndex;
00081 using OpenMesh::VDPM::VHierarchyNodeHandle;
00082 using OpenMesh::VDPM::ViewingParameters;
00083 using OpenMesh::VDPM::set_debug_print;
00084 
00085 
00086 //== CLASS DEFINITION =========================================================
00087 
00088 
00089 class VDPMClientViewerWidget : public MeshViewerWidget
00090 {
00091 
00092   Q_OBJECT
00093 
00094 public:
00095   VDPMClientViewerWidget(QWidget *_parent=0, const char *_name=0)
00096     : MeshViewerWidget(_parent, _name)
00097   {
00098     set_debug_print(true);
00099     adaptive_mode_ = false;
00100     
00101     qSessionTimer_ = new QTimer(this);
00102     qSocket_ = new QTcpSocket(this);
00103     streaming_phase_ = kBaseMesh;
00104     session_running_ = false;
00105 
00106     
00107     connect(qSessionTimer_, SIGNAL(timeout()),
00108       this, SLOT(session_timer_check()));
00109 
00110     connect(qSessionTimer_, SIGNAL(timeout()),
00111       this, SLOT(socketReadyRead()));
00112     
00113     // connect signal-slots about QSocket
00114     connect(qSocket_, SIGNAL(connected()), 
00115             this, SLOT(socketConnected()));
00116 
00117     connect(qSocket_, SIGNAL(connectionClosed()), 
00118             this, SLOT(socketConnectionClosed()));
00119 
00120     connect(qSocket_, SIGNAL(readyRead()), 
00121             this, SLOT(socketReadyRead()));
00122 
00123     connect(qSocket_, SIGNAL(error( QAbstractSocket::SocketError )), 
00124             this, SLOT(socketError( QAbstractSocket::SocketError )));
00125 
00126 
00127     look_around_mode_ = false;
00128     frame_ = 0;
00129     n_viewpoints_ = 60;
00130 
00131     global_timer_.reset();
00132     global_timer_.start();
00133     render_timer_.reset();
00134     refinement_timer_.reset();
00135     session_timer_.reset();
00136 
00137     qAnimationTimer_ = new QTimer(this);
00138    
00139     connect(qAnimationTimer_, SIGNAL(timeout()),
00140       this, SLOT(look_around()));
00141     //connect(qAnimationTimer_, SIGNAL(timeout()),
00142     //  this, SLOT(print_statistics()));
00143 
00144 
00145     uplink_file = fopen("uplink.txt", "w");
00146     downlink_file = fopen("downlink.txt", "w");
00147 
00148     render_file = fopen("render.txt", "w");
00149     refinement_file = fopen("refinement.txt", "w");
00150     session_file = fopen("session.txt", "w");
00151 
00152     vd_streaming_ = true;
00153     max_transmitted_datasize_ = 0;
00154     transmitted_datasize_ = 0;    
00155   }
00156 
00157   ~VDPMClientViewerWidget()
00158   {
00159     fclose(uplink_file);
00160     fclose(downlink_file);
00161 
00162     fclose(render_file);
00163     fclose(refinement_file);
00164     fclose(session_file);
00165   }
00166 
00167 
00168   void connectToServer( std::string& _server_name, 
00169                         int _port= VDPM_STREAMING_PORT )
00170   {
00171     qSocket_->connectToHost( _server_name.c_str(), _port );
00172   }
00173 
00174   void openBaseMesh( std::string& _base_mesh )
00175   {
00176     open_vd_base_mesh( _base_mesh.c_str() );
00177     std::cout << "spm file: " << _base_mesh << std::endl;
00178   }
00179 
00180 // socket related slots
00181 private slots:
00182 
00183   void closeConnection()
00184   {
00185     close();
00186     if (qSocket_->state() == QAbstractSocket::ClosingState)     // we have a delayed close.
00187     {      
00188       connect(this, SIGNAL(delayedCloseFinished()), SLOT(socketClosed()));
00189     }
00190     else                                 // the qSocket is closed.
00191     {      
00192       socketClosed();
00193     }
00194   }
00195 
00196   void socketReadyRead()
00197   {
00198     switch( streaming_phase_)
00199     {
00200       case kVSplits:      receive_vsplit_packets(); break;
00201       case kVSplitHeader: receive_vsplit_header();  break;
00202       case kBaseMesh:     receive_base_mesh();      break;
00203     }
00204 
00205   }
00206 
00207   void socketConnected()
00208   {
00209     std::cout << "Connected to server" << std::endl;
00210   }
00211 
00212   void socketConnectionClosed()
00213   {
00214     std::cout << "Connection closed by the server" << std::endl;
00215   }
00216 
00217   void socketClosed()
00218   {
00219     std::cout << "Connection closed" << std::endl;
00220   }
00221 
00222   void socketError(QAbstractSocket::SocketError e)
00223   {
00224     std::cout << "Error number " << e << " occurred" << std::endl;
00225   }
00226 
00227   void look_around();
00228   void print_statistics();
00229 
00230 
00231   void session_timer_check()
00232   {
00233     std::cout << "Session Timer works" << std::endl;
00234   }
00235 
00236 // for view-dependent PM 
00237 private:
00238   VHierarchy          vhierarchy_;
00239   //unsigned char       tree_id_bits_;
00240   VFront              vfront_;
00241   ViewingParameters   viewing_parameters_;
00242   float               kappa_square_;
00243   bool                adaptive_mode_;
00244 
00245   unsigned int        n_base_vertices_;
00246   unsigned int        n_base_edges_;
00247   unsigned int        n_base_faces_;
00248   unsigned int        n_details_;
00249 
00250 private:
00251 
00252   bool outside_view_frustum(const OpenMesh::Vec3f &pos, float radius);
00253   bool oriented_away(float sin_square, float distance_square, 
00254                      float product_value);
00255   bool screen_space_error(float mue_square, float sigma_square, 
00256                           float distance_square, float product_value);
00257   void update_viewing_parameters();
00258 
00259   virtual void keyPressEvent(QKeyEvent *_event);
00260 
00261 protected:
00262 
00264   virtual void draw_scene(const std::string& _draw_mode);
00265 
00266 
00267 public:
00268 
00269   void open_vd_prog_mesh(const char* _filename);
00270   
00271   unsigned int  num_base_vertices() const     { return  n_base_vertices_; }
00272   unsigned int  num_base_edges() const        { return  n_base_edges_; }
00273   unsigned int  num_base_faces() const        { return  n_base_faces_; }
00274   unsigned int  num_details() const           { return  n_details_; }  
00275 
00276   void adaptive_refinement();   
00277   bool qrefine(VHierarchyNodeHandle _node_handle);              
00278   void force_vsplit(VHierarchyNodeHandle _node_handle);
00279   bool ecol_legal(VHierarchyNodeHandle _parent_handle, MyMesh::HalfedgeHandle& v0v1);
00280 
00281   void get_active_cuts(VHierarchyNodeHandle _node_handle, MyMesh::VertexHandle &vl, MyMesh::VertexHandle &vr);
00282   void vsplit(VHierarchyNodeHandle _node_handle, MyMesh::VertexHandle vl, MyMesh::VertexHandle vr);
00283   void ecol(VHierarchyNodeHandle _parent_handle, const MyMesh::HalfedgeHandle& v0v1);
00284 
00285         void init_vfront(); 
00286 
00287   // streaming realted functions
00288 private:
00289   QTimer              *qSessionTimer_;
00290   QTcpSocket          *qSocket_;
00291   QString             qFilename_;
00292   bool                session_running_;
00293   VDPMStreamingPhase  streaming_phase_;
00294   unsigned int        n_vsplit_packets_;
00295 
00296 public:
00297   void connect_to_server();
00298   bool request_base_mesh();
00299   bool receive_base_mesh();
00300   void send_viewing_information();
00301   void receive_vsplit_header();
00302   void receive_vsplit_packets();
00303   void open_vd_base_mesh(const char* _filename);
00304   void update_vhierarchy(
00305     const OpenMesh::Vec3f     &_pos,              // 3D position of v0
00306     const VHierarchyNodeIndex &_v,                // vhierarchy index of v1
00307     const VHierarchyNodeIndex &_fund_lcut_index,  // vhierarchy index of fundamental lcut
00308     const VHierarchyNodeIndex &_fund_rcut_index,  // vhierarchy index of fundamental rcut
00309     const float               _radius[2],         // radius of lchild & rchild
00310     const OpenMesh::Vec3f     _normal[2],         // normal of lchild & rchild
00311     const float               _sin_square[2],     // sin_square of lchild & rchild
00312     const float               _mue_square[2],     // mue_square of lchild & rchild
00313     const float               _sigma_square[2]    // sigma_square of lchild & rchild
00314     );
00315 
00316    
00317   // for example
00318 private:
00319   QTimer              *qAnimationTimer_;
00320   QString             qCameraFileName_;
00321   MyMesh::Point       bbMin_, bbMax_;
00322   unsigned int        frame_;
00323   int                 max_transmitted_datasize_;
00324   int                 transmitted_datasize_;
00325   bool                vd_streaming_;
00326 
00327   unsigned int        nth_viewpoint_;
00328   unsigned int        n_viewpoints_;
00329   bool                look_around_mode_;
00330   GLdouble            reserved_modelview_matrix_[16];
00331   GLdouble            reserved_projection_matrix_[16];
00332 
00333   FILE  *uplink_file;
00334   FILE  *downlink_file;
00335   FILE  *render_file;
00336   FILE  *refinement_file;
00337   FILE  *session_file;
00338 
00339 public:
00340   void save_screen(bool _flag);
00341   void save_views();
00342   void load_views(const char *camera_filename);
00343   void screen_capture(const char *_filename);
00344   void current_max_resolution();
00345 
00346   OpenMesh::Utils::Timer  global_timer_;
00347   OpenMesh::Utils::Timer  render_timer_;
00348   OpenMesh::Utils::Timer  refinement_timer_;
00349   OpenMesh::Utils::Timer  session_timer_;
00350 
00351 
00352 
00353 #ifdef EXAMPLE_CREATION
00354   void increase_max_descendents(const VHierarchyNodeIndex &node_index);
00355   void increase_cur_descendents(VHierarchyNodeHandle _node_handle);
00356   void __add_children(const VHierarchyNodeIndex &node_index, bool update_current = true);
00357   void mesh_coloring();
00358 #endif
00359 };
00360 
00361 #endif //OPENMESH_APPS_VDPMSTREAMING_CLIENT_VDPMCLIENTVIEWERWIDGET_HH defined