~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/core/StelProjector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream)
  • mto: (11.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20090313200722-gbgujsmzsa8a02ty
Import upstream version 0.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2003 Fabien Chereau
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "StelTranslator.hpp"
 
21
 
 
22
#include "StelProjector.hpp"
 
23
#include "StelProjectorClasses.hpp"
 
24
 
 
25
 
 
26
#include <QDebug>
 
27
#include <QString>
 
28
 
 
29
const QString StelProjector::maskTypeToString(StelProjectorMaskType type)
 
30
{
 
31
        if (type == MaskDisk )
 
32
                return "disk";
 
33
        else
 
34
                return "none";
 
35
}
 
36
 
 
37
StelProjector::StelProjectorMaskType StelProjector::stringToMaskType(const QString &s)
 
38
{
 
39
        if (s=="disk")
 
40
                return MaskDisk;
 
41
        return MaskNone;
 
42
}
 
43
 
 
44
void StelProjector::init(const StelProjectorParams& params)
 
45
{
 
46
        maskType = (StelProjectorMaskType)params.maskType;
 
47
        zNear = params.zNear;
 
48
        oneOverZNearMinusZFar = 1./(zNear-params.zFar);
 
49
        viewportXywh = params.viewportXywh;
 
50
        viewportCenter = params.viewportCenter;
 
51
        gravityLabels = params.gravityLabels;
 
52
        flipHorz = params.flipHorz ? -1. : 1.;
 
53
        flipVert = params.flipVert ? -1. : 1.;
 
54
        viewportFovDiameter = params.viewportFovDiameter;
 
55
        pixelPerRad = 0.5 * viewportFovDiameter / fovToViewScalingFactor(params.fov*(M_PI/360.0));
 
56
        //Q_ASSERT(params.fov<=getMaxFov());
 
57
}
 
58
 
 
59
QString StelProjector::getHtmlSummary() const
 
60
{
 
61
        return QString("<h3>%1</h3><p>%2</p><b>%3</b>%4").arg(getNameI18()).arg(getDescriptionI18()).arg(q_("Maximum FOV: ")).arg(getMaxFov())+QChar(0x00B0);
 
62
}
 
63
 
 
64
/*************************************************************************
 
65
 Return a convex polygon on the sphere which includes the viewport in the 
 
66
 current frame
 
67
*************************************************************************/
 
68
StelGeom::ConvexPolygon StelProjector::getViewportConvexPolygon(double marginX, double marginY) const
 
69
{
 
70
        Vec3d e0, e1, e2, e3;
 
71
        bool ok = unProject(0-marginX,0-marginY,e0);
 
72
        ok &= unProject(getViewportWidth()+marginX,0-marginY,e1);
 
73
        ok &= unProject(getViewportWidth()+marginX,getViewportHeight()+marginY,e2);
 
74
        ok &= unProject(0-marginX,getViewportHeight()+marginY,e3);
 
75
        if (!ok)
 
76
        {
 
77
                // Special case for handling degenerated cases, use full sky.
 
78
                //qDebug() << "!ok";
 
79
                return StelGeom::ConvexPolygon::fullSky();
 
80
        }
 
81
        e0.normalize();
 
82
        e1.normalize();
 
83
        e2.normalize();
 
84
        e3.normalize();
 
85
        StelGeom::ConvexPolygon res = needGlFrontFaceCW() ? StelGeom::ConvexPolygon(e1, e0, e3, e2) : StelGeom::ConvexPolygon(e0, e1, e2, e3);
 
86
        if (res.checkValid())
 
87
                return res;
 
88
        //qDebug() << "!valid";
 
89
        return StelGeom::ConvexPolygon::fullSky(); 
 
90
}
 
91
 
 
92
 
 
93
// Return a Halfspace containing the whole viewport
 
94
StelGeom::HalfSpace StelProjector::getBoundingHalfSpace() const
 
95
{
 
96
        StelGeom::HalfSpace result;
 
97
        
 
98
        bool ok = unProject(viewportXywh[0]+0.5*viewportXywh[2], viewportXywh[1]+0.5*viewportXywh[3], result.n);
 
99
        Q_ASSERT(ok);   // The central point should be at a valid position by definition
 
100
        
 
101
        // Now need to determine the aperture
 
102
        Vec3d e0,e1,e2,e3,e4,e5;
 
103
        const Vec4i& vp = viewportXywh;
 
104
        ok &= unProject(vp[0],vp[1],e0);
 
105
        ok &= unProject(vp[0]+vp[2],vp[1],e1);
 
106
        ok &= unProject(vp[0]+vp[2],vp[1]+vp[3],e2);
 
107
        ok &= unProject(vp[0],vp[1]+vp[3],e3);
 
108
        ok &= unProject(vp[0],vp[1]+vp[3]/2,e4);
 
109
        ok &= unProject(vp[0]+vp[2],vp[1]+vp[3]/2,e5);
 
110
        if (!ok)
 
111
        {
 
112
                // Some points were in invalid positions
 
113
                result.d = -1.;
 
114
                return result;
 
115
        }
 
116
        result.d = result.n*e0;
 
117
        double h = result.n*e1;
 
118
        if (result.d > h)
 
119
                result.d=h;
 
120
        h = result.n*e2;
 
121
        if (result.d > h)
 
122
                result.d=h;
 
123
        h = result.n*e3;
 
124
        if (result.d > h)
 
125
                result.d=h;
 
126
        h = result.n*e4;
 
127
        if (result.d > h)
 
128
                result.d=h;
 
129
        h = result.n*e5;
 
130
        if (result.d > h)
 
131
                result.d=h;
 
132
        return result;
 
133
}
 
134
 
 
135
/*************************************************************************
 
136
 Project the vector v from the viewport frame into the current frame 
 
137
*************************************************************************/
 
138
bool StelProjector::unProject(double x, double y, Vec3d &v) const
 
139
{
 
140
        v[0] = flipHorz * (x - viewportCenter[0]) / pixelPerRad;
 
141
        v[1] = flipVert * (y - viewportCenter[1]) / pixelPerRad;
 
142
        v[2] = 0;
 
143
        const bool rval = backward(v);
 
144
        // Even when the reprojected point comes from a region of the screen,
 
145
        // where nothing is projected to (rval=false), we finish reprojecting.
 
146
        // This looks good for atmosphere rendering, and it helps avoiding
 
147
        // discontinuities when dragging around with the mouse.
 
148
 
 
149
        // We need no matrix inversion because we always work with orthogonal matrices (where the transposed is the inverse).
 
150
        //v.transfo4d(inverseModelViewMatrix);
 
151
        x = v[0] - modelViewMatrix.r[12];
 
152
        y = v[1] - modelViewMatrix.r[13];
 
153
        const double z = v[2] - modelViewMatrix.r[14];
 
154
        v[0] = modelViewMatrix.r[0]*x + modelViewMatrix.r[1]*y + modelViewMatrix.r[2]*z;
 
155
        v[1] = modelViewMatrix.r[4]*x + modelViewMatrix.r[5]*y + modelViewMatrix.r[6]*z;
 
156
        v[2] = modelViewMatrix.r[8]*x + modelViewMatrix.r[9]*y + modelViewMatrix.r[10]*z;
 
157
 
 
158
        return rval;
 
159
}