Home · All Namespaces · All Classes · Functions · Coding Style · Plugins · File Structure

core/StelProjector.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2003 Fabien Chereau
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #ifndef _STELPROJECTOR_HPP_
00021 #define _STELPROJECTOR_HPP_
00022 
00023 #include "StelProjectorType.hpp"
00024 #include "VecMath.hpp"
00025 #include "StelSphereGeometry.hpp"
00026 
00034 class StelProjector
00035 {
00036 public:
00037     friend class StelPainter;
00038     friend class StelCore;
00039     
00042     enum StelProjectorMaskType
00043     {
00044         MaskNone,   
00045         MaskDisk    
00046     };
00047     
00050     struct StelProjectorParams
00051     {
00052         StelProjectorParams() : viewportXywh(0), fov(60.), gravityLabels(false), maskType(MaskNone), viewportCenter(0., 0.), flipHorz(false), flipVert(false) {;}
00053         Vector4<int> viewportXywh;     
00054         double fov;                    
00055         bool gravityLabels;            
00056         StelProjectorMaskType maskType;    
00057         double zNear, zFar;            
00058         Vec2d viewportCenter;          
00059         double viewportFovDiameter;    
00060         bool flipHorz, flipVert;       
00061     };
00062     
00064     virtual ~StelProjector() {;}
00065     
00067     // Methods which must be reimplemented by all instance of StelProjector
00069     virtual QString getNameI18() const = 0;
00071     virtual QString getDescriptionI18() const {return "No description";}
00073     QString getHtmlSummary() const;
00075     virtual double getMaxFov() const = 0;   
00082     virtual bool forward(Vec3d &v) const = 0;
00084     virtual bool backward(Vec3d &v) const = 0;
00086     virtual double deltaZoom(double fov) const = 0;
00087 
00091     bool intersectViewportDiscontinuity(const Vec3d& p1, const Vec3d& p2) const
00092     {
00093         if (hasDiscontinuity()==false)
00094             return false;
00095         return intersectViewportDiscontinuityInternal(modelViewMatrix*p1, modelViewMatrix*p2);
00096     }
00097     
00099     virtual double fovToViewScalingFactor(double fov) const = 0;
00101     virtual double viewScalingFactorToFov(double vsf) const = 0;
00102     
00106     bool getFlagGravityLabels() const { return gravityLabels; }
00107 
00109     const Vec4i& getViewport(void) const {return viewportXywh;}
00110 
00112     Vec2d getViewportCenter(void) const
00113     {
00114         return Vec2d(viewportCenter[0]-viewportXywh[0],viewportCenter[1]-viewportXywh[1]);
00115     }
00116     
00118     int getViewportPosX(void) const {return viewportXywh[0];}
00120     int getViewportPosY(void) const {return viewportXywh[1];}
00122     int getViewportWidth(void) const {return viewportXywh[2];}
00124     int getViewportHeight(void) const {return viewportXywh[3];}
00125     
00131     StelGeom::ConvexPolygon getViewportConvexPolygon(double marginX=0., double marginY=0.) const;
00132 
00134     StelGeom::HalfSpace getBoundingHalfSpace() const;
00135     
00137     double getPixelPerRadAtCenter(void) const {return pixelPerRad;}
00138     
00140     double getFov() const {return 360./M_PI*viewScalingFactorToFov(0.5*viewportFovDiameter/pixelPerRad);}
00141     
00143     bool needGlFrontFaceCW(void) const {return (flipHorz*flipVert < 0.0);}
00144     
00146     // Full projection methods
00149     bool checkInViewport(const Vec3d& pos) const
00150     {
00151         return (pos[1]>=viewportXywh[1] && pos[0]>=viewportXywh[0] &&
00152             pos[1]<=(viewportXywh[1] + viewportXywh[3]) && pos[0]<=(viewportXywh[0] + viewportXywh[2]));
00153     }
00154 
00157     Vec3d viewPortIntersect(const Vec3d& p1, const Vec3d& p2) const
00158     {
00159         Vec3d v1=p1;
00160         Vec3d v2=p2;
00161         Vec3d v;
00162         for (int i=0;i<8;++i)
00163         {
00164             v=(v1+v2)*0.5;
00165             if (!checkInViewport(v))
00166                 v2=v;
00167             else
00168                 v1=v;
00169         }
00170         return v;
00171     }
00172         
00177     bool project(const Vec3d& v, Vec3d& win) const
00178     {
00179         // really important speedup:
00180         win[0] = modelViewMatrix.r[0]*v[0] + modelViewMatrix.r[4]*v[1]
00181                 + modelViewMatrix.r[8]*v[2] + modelViewMatrix.r[12];
00182         win[1] = modelViewMatrix.r[1]*v[0] + modelViewMatrix.r[5]*v[1]
00183                 + modelViewMatrix.r[9]*v[2] + modelViewMatrix.r[13];
00184         win[2] = modelViewMatrix.r[2]*v[0] + modelViewMatrix.r[6]*v[1]
00185                 + modelViewMatrix.r[10]*v[2] + modelViewMatrix.r[14];
00186         const bool rval = forward(win);
00187         // very important: even when the projected point comes from an
00188         // invisible region of the sky (rval=false), we must finish
00189         // reprojecting, so that OpenGl can successfully eliminate
00190         // polygons by culling.
00191         win[0] = viewportCenter[0] + flipHorz * pixelPerRad * win[0];
00192         win[1] = viewportCenter[1] + flipVert * pixelPerRad * win[1];
00193         win[2] = (win[2] - zNear) * oneOverZNearMinusZFar;
00194         return rval;
00195     }
00196 
00201     bool projectCheck(const Vec3d& v, Vec3d& win) const {return (project(v, win) && checkInViewport(win));}
00202 
00207     bool unProject(const Vec3d& win, Vec3d& v) const {return unProject(win[0], win[1], v);}
00208     bool unProject(double x, double y, Vec3d& v) const;
00209 
00216     bool projectLineCheck(const Vec3d& v1, Vec3d& win1, const Vec3d& v2, Vec3d& win2) const
00217         {return project(v1, win1) && project(v2, win2) && (checkInViewport(win1) || checkInViewport(win2));}
00218 
00220     const Mat4d& getModelViewMatrix() const {return modelViewMatrix;}
00221     
00224     static const QString maskTypeToString(StelProjectorMaskType type);
00226     static StelProjectorMaskType stringToMaskType(const QString &s);
00227     
00229     StelProjectorMaskType getMaskType(void) const {return maskType;}
00230     
00231 protected:
00233     StelProjector(const Mat4d& modelViewMat) : modelViewMatrix(modelViewMat) {;}
00235     virtual bool hasDiscontinuity() const =0;
00239     virtual bool intersectViewportDiscontinuityInternal(const Vec3d& p1, const Vec3d& p2) const = 0;
00240 private:
00242     void init(const StelProjectorParams& param);
00243     
00244     StelProjectorMaskType maskType;    // The current projector mask
00245     double zNear, oneOverZNearMinusZFar;  // Near and far clipping planes
00246     Vec4i viewportXywh;     // Viewport parameters
00247     Vec2d viewportCenter;          // Viewport center in screen pixel
00248     double viewportFovDiameter;    // diameter of the FOV disk in pixel
00249     double pixelPerRad;            // pixel per rad at the center of the viewport disk
00250     double flipHorz,flipVert;      // Whether to flip in horizontal or vertical directions
00251     bool gravityLabels;            // should label text align with the horizon?
00252     Mat4d modelViewMatrix;         // openGL MODELVIEW Matrix
00253 };
00254 
00255 #endif // _STELPROJECTOR_HPP_

Generated on Mon Mar 9 16:16:15 2009 for Stellarium by  doxygen 1.5.5