libidxgui
/home/rex/ebltrunk/tools/libidxgui/include/win3d.h
00001 /***************************************************************************
00002  *   Copyright (C) 2011 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef _WIN3D_H_
00034 #define _WIN3D_H_
00035 
00036 #include <QPixmap>
00037 #include <QColor>
00038 #include <QWidget>
00039 #include <QtGui>
00040 #include <QResizeEvent>
00041 #include <math.h>
00042 #include <iostream>
00043 
00044 #include "defines_idxgui.h"
00045 #include "libidx.h"
00046 #include "scroll_box0.h"
00047 #include "defines.h"
00048 #include "win.h"
00049 
00050 #ifdef __GUI3D__
00051 #include "qgl.h"
00052 #include "qglpainter.h"
00053 #include "qglbuilder.h"
00054 #include "qglcamera.h"
00055 #include "qgltexture2d.h"
00056 #include "qglview.h"
00057 #include "qglsphere.h"
00058 #include "qglcylinder.h"
00059 #include "qglcolormaterial.h"
00060 #include "qglbuilder.h"
00061 #endif
00062 
00063 using namespace std;
00064 
00065 namespace ebl {
00066 
00067 #ifdef __GUI3D__
00068   
00069   class text3d {
00070   public:
00072     text3d(const char *s, float x, float y, float z,
00073            int r = 255, int g = 255, int b = 255, int a = 255);
00074     virtual ~text3d();
00076     std::string describe();
00077     // members
00078     QString s;
00079     float x, y, z;
00080     QColor col;
00081   };
00082   
00083   class sphere3d {
00084   public:
00087     sphere3d(float x, float y, float z, float radius,
00088              int r = 255, int g = 255, int b = 255, int a = 255);
00089     virtual ~sphere3d();
00091     std::string describe();
00092     // members
00093     float x, y, z, radius;
00094     QGLSceneNode *node;
00095     QColor col;
00096   };
00097   
00098   class cylinder3d {
00099   public:
00104     cylinder3d(float x, float y, float z, float length, float top_radius,
00105                float base_radius, float a1, float a2, int r = 255,
00106                int g = 255, int b = 255, int a = 255, bool tops = false);
00107     virtual ~cylinder3d();
00109     std::string describe();
00110     // members
00111     float x, y, z; 
00112     float a1, a2; 
00113     float top_radius, base_radius;
00114     bool tops;
00115     QGLSceneNode *node;
00116     QColor col;
00117   };
00118   
00119   class line3d { //: public QGLSceneNode {
00120   public:
00125     line3d(float x, float y, float z, float x1, float y1, float z1,
00126            int r = 255, int g = 255, int b = 255, int a = 255);
00127     virtual ~line3d();
00129     std::string describe();
00130   /* protected: */
00131   /*   virtual void drawGeometry(QGLPainter *painter); */
00132     // members
00133   public:
00134     float x, y, z; 
00135     float x1, y1, z1; 
00136     QGLSceneNode *node;
00137     QColor col;
00138   };
00139   
00141   // win3d
00142   
00143   class IDXGUIEXPORT win3d : public QGLWidget, public win {
00144     Q_OBJECT
00145       
00146   public:
00147     // constructors ////////////////////////////////////////////////////////////
00148     
00149     win3d(uint wid, const char *wname = NULL, uint height = 0, uint width = 0);
00150     virtual ~win3d();
00151 
00152     // window matters //////////////////////////////////////////////////////////
00153     
00154     virtual void show();
00157     virtual void set_wupdate(bool ud);
00161     virtual void resize_window(uint h, uint w, bool force = false);
00164     virtual void update_window(bool activate = false);
00165 
00166     // objects adding //////////////////////////////////////////////////////////
00167     
00169     void add_sphere(float x, float y, float z, float radius,
00170                     const char *label = NULL,
00171                     int r = 255, int g = 255, int b = 255, int a = 255);
00176     void add_cylinder(float x, float y, float z, float length, float top_radius,
00177                       float base_radius, float a1, float a2,
00178                       const char *label = NULL,
00179                       int r = 255, int g = 255, int b = 255, int a = 255,
00180                       bool tops = false); 
00182     void add_text(float x, float y, float z, const char *s,
00183                   int r = 255, int g = 255, int b = 255, int a = 255);
00185     void add_line(float x, float y, float z, float x1, float y1, float z1,
00186                    const char *s, int r = 255, int g = 255, int b = 255,
00187                    int a = 255);
00188 
00189     // clear methods ///////////////////////////////////////////////////////////
00190 
00193     virtual void clear();
00194     void clear_spheres();
00195     void clear_cylinders();
00196     void clear_texts();
00197     void clear_lines();
00198 
00200     // event methods
00201   protected:
00203     virtual void initializeGL();
00205     void resizeGL(int width, int height);
00207     virtual void paintGL();
00209     void paint_text();
00211     void paint_spheres(QGLPainter *painter);
00213     void paint_cylinders(QGLPainter *painter);
00215     void paint_lines(QGLPainter *painter);
00216 
00217   protected:
00219     // event methods
00220 
00221     void wheelEvent(QWheelEvent *event);
00222     void mousePressEvent(QMouseEvent *event);
00223     void mouseMoveEvent(QMouseEvent *event);
00224     //    void mouseReleaseEvent(QMouseEvent *event);
00225     void keyPressEvent(QKeyEvent *event);
00226     void keyReleaseEvent(QKeyEvent *event);
00227 
00228     /* void setupViewport(int width, int height); */
00229     //       void paintEvent(QPaintEvent *);
00230 
00231     GLfloat rotationX;
00232     GLfloat rotationY;
00233     GLfloat rotationZ;
00234     GLfloat scaling;
00235     QPoint lastPos;
00236 
00237     //  QSize sizeHint() const;
00238      int xRotation() const { return xRot; }
00239      int yRotation() const { return yRot; }
00240      int zRotation() const { return zRot; }
00241 
00242  int xRot;
00243      int yRot;
00244      int zRot;
00245     double               pixmapScale;
00246 
00247     int height; 
00248     int width; 
00249      
00250      
00251  public slots:
00252      void setXRotation(int angle);
00253      void setYRotation(int angle);
00254      void setZRotation(int angle);
00255 
00256     
00257   private:
00258     vector<text3d*> texts;
00259     vector<sphere3d*> spheres;
00260     vector<cylinder3d*> cylinders;
00261     vector<line3d*> lines;
00262   };
00263 
00264 #else // No 3D available, just define a dummy class
00265 
00266   class IDXGUIEXPORT win3d : public win {
00267   public:
00268     win3d(uint wid, const char *wname = NULL, uint height = 0, uint width = 0) {
00269       eblerror("install Qt3d required"); }
00270   };
00271   
00272 #endif
00273 
00274 } // namespace ebl {
00275 
00276 #endif /* _WIN3D_H_ */