OpenMesh
OpenMesh/Apps/Unsupported/Streaming-qt4/Server/VDPMServerSession.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_APP_VDPMSTREAMING_SERVER_VDPMSERVERSESSION_HH
00043 #define OPENMESH_APP_VDPMSTREAMING_SERVER_VDPMSERVERSESSION_HH
00044 
00045 #include <QTcpSocket>
00046 #include <QThread>
00047 #include <QDataStream>
00048 #include <QTimer>
00049 #include <iostream>
00050 // #include <QObject>
00051 #include <OpenMesh/Core/Geometry/VectorT.hh>
00052 #include <OpenMesh/Core/Geometry/Plane3d.hh>
00053 #include <OpenMesh/Tools/Utils/Timer.hh>
00054 #include <OpenMesh/Tools/VDPM/StreamingDef.hh>
00055 #include <OpenMesh/Tools/VDPM/VHierarchyNodeIndex.hh>
00056 #include <OpenMesh/Tools/VDPM/ViewingParameters.hh>
00057 #include <OpenMesh/Tools/VDPM/VHierarchy.hh>
00058 #include <OpenMesh/Tools/VDPM/VFront.hh>
00059 #include <OpenMesh/Apps/VDProgMesh/Streaming/Server/ServerSideVDPM.hh>
00060 #include <OpenMesh/Tools/VDPM/VHierarchyWindow.hh>
00061 
00062 
00063 using OpenMesh::VDPM::VDPMStreamingPhase;
00064 using OpenMesh::VDPM::kBaseMesh;
00065 using OpenMesh::VDPM::kVSplits;
00066 
00067 using OpenMesh::VDPM::VHierarchyWindow;
00068 using OpenMesh::VDPM::VHierarchyNodeIndex;
00069 using OpenMesh::VDPM::VHierarchyNodeHandle;
00070 using OpenMesh::VDPM::ViewingParameters;
00071 
00072 using OpenMesh::VDPM::set_debug_print;
00073 
00074 class VDPMServerSession : public QThread
00075 {
00076 
00077   Q_OBJECT
00078 
00079 public:
00080 
00081   VDPMServerSession(QTcpSocket* _socket, QObject *parent=0, const char *name=0)
00082   {
00083     socket_ = _socket;
00084 
00085     set_debug_print(true);
00086 
00087     streaming_phase_       = kBaseMesh;
00088     transmission_complete_ = false;
00089 
00090     connect(socket_, SIGNAL(connected()), this, SLOT(socketConnected()));
00091     QTcpSocket::connect(socket_, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
00092     //connect(this, SIGNAL(connectionClosed()), SLOT(deleteLater()));
00093     QTcpSocket::connect(socket_, SIGNAL(connectionClosed()), this, SLOT(delayedCloseFinished()));
00094 
00096 //     setSocket(sock);
00097 
00098     qStatisticsTimer_ = new QTimer(this);
00099     QTcpSocket::connect(qStatisticsTimer_, SIGNAL(timeout()), this, SLOT(print_statistics()));
00100 
00101     mem_file = fopen("mem.txt", "w");
00102     
00103     start();
00104   }
00105 
00106   ~VDPMServerSession()
00107   {
00108     fclose(mem_file);
00109   }
00110 
00111 //   void run()
00112 //   {
00113 //     exec();
00114 //   }
00115 
00116 private:  
00117 
00118   VDPMStreamingPhase  streaming_phase_;
00119   bool                transmission_complete_;
00120   
00121   QTcpSocket*         socket_;
00122 
00123 private:
00124 
00125   void sendBaseMeshToClient();
00126   void send_vsplit_packets();
00127   void readBaseMeshRequestFromClient();
00128   void readViewingParametersFromClient();
00129 
00130   void PrintOutVFront();
00131 
00132 private slots:
00133 
00134   void socketConnected()
00135   {
00136     std::cout << "socket is connected" << std::endl;
00137   }
00138 
00139   void socketReadyRead()
00140   {
00141     if (streaming_phase_ == kBaseMesh)
00142     {
00143       readBaseMeshRequestFromClient();      
00144     }
00145     else if (streaming_phase_ == kVSplits)
00146     {
00147       readViewingParametersFromClient();
00148     }
00149   }
00150 
00151   void print_statistics()
00152   {
00153     //std::cout << memory_requirements(true) << " " << memory_requirements(false) << std::endl;
00154   }
00155 
00156 private:  
00157   unsigned short                tree_id_bits_;    // obsolete
00158   ServerSideVDPM*               vdpm_;
00159   VHierarchy*                   vhierarchy_;
00160   VHierarchyWindow              vhwindow_;
00161   ViewingParameters             viewing_parameters_;
00162   float                         kappa_square_;
00163   VHierarchyNodeHandleContainer vsplits_;
00164 
00165 private:
00166   bool outside_view_frustum(const OpenMesh::Vec3f &pos, float radius);
00167   bool oriented_away(float sin_square, float distance_square, float product_value);
00168   bool screen_space_error(float mue_square, float sigma_square, float distance_square, float product_value);
00169 
00170   void adaptive_refinement();
00171   void sequential_refinement();
00172   bool qrefine(VHierarchyNodeHandle _node_handle);
00173   void force_vsplit(VHierarchyNodeHandle node_handle);
00174   void vsplit(VHierarchyNodeHandle _node_handle);
00175   VHierarchyNodeHandle active_ancestor_handle(VHierarchyNodeIndex &node_index);
00176   void stream_vsplits();
00177 
00178 public:
00179   bool set_vdpm(const char _vdpm_name[256]);
00180   unsigned int  memory_requirements_using_window(bool _estimate);
00181   unsigned int  memory_requirements_using_vfront();
00182 
00183   // for example
00184 private:
00185   QTimer  *qStatisticsTimer_;
00186   FILE    *mem_file;
00187 };
00188 
00189 #endif //OPENMESH_APP_VDPMSTREAMING_SERVER_VDPMSERVERSESSION_HH defined