Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <svx/viewpt3d.hxx>
21 : #include <basegfx/range/b3drange.hxx>
22 :
23 0 : Viewport3D::Viewport3D() :
24 : aVRP(0, 0, 5),
25 : aVPN(0, 0, 1),
26 : aVUV(0, 1, 1),
27 : aPRP(0, 0, 2),
28 : fVPD(-3),
29 : fNearClipDist (0.0),
30 : fFarClipDist (0.0),
31 : eProjection(PR_PERSPECTIVE),
32 : eAspectMapping(AS_NO_MAPPING),
33 : aDeviceRect(Point(0,0), Size(-1,-1)),
34 : aViewPoint (0, 0, 5000),
35 : bTfValid(false),
36 : fWRatio (1.0),
37 0 : fHRatio (1.0)
38 : {
39 0 : aViewWin.X = -1; aViewWin.Y = -1;
40 0 : aViewWin.W = 2; aViewWin.H = 2;
41 0 : }
42 :
43 : // Set ViewWindow (in View coordinates)
44 :
45 0 : void Viewport3D::SetViewWindow(double fX, double fY, double fW, double fH)
46 : {
47 0 : aViewWin.X = fX;
48 0 : aViewWin.Y = fY;
49 0 : if ( fW > 0 ) aViewWin.W = fW;
50 0 : else aViewWin.W = 1.0;
51 0 : if ( fH > 0 ) aViewWin.H = fH;
52 0 : else aViewWin.H = 1.0;
53 :
54 0 : fWRatio = aDeviceRect.GetWidth() / aViewWin.W;
55 0 : fHRatio = aDeviceRect.GetHeight() / aViewWin.H;
56 0 : }
57 :
58 : // Returns observer position (PRP) in world coordinates
59 :
60 0 : const basegfx::B3DPoint& Viewport3D::GetViewPoint()
61 : {
62 0 : MakeTransform();
63 :
64 0 : return aViewPoint;
65 : }
66 :
67 : // Calculate View transformations matrix
68 :
69 0 : void Viewport3D::MakeTransform(void)
70 : {
71 0 : if ( !bTfValid )
72 : {
73 : double fV, fXupVp, fYupVp;
74 0 : aViewPoint = aVRP + aVPN * aPRP.getZ();
75 :
76 : // Reset to Identity matrix
77 0 : aViewTf.identity();
78 :
79 : // shift in the origin
80 0 : aViewTf.translate(-aVRP.getX(), -aVRP.getY(), -aVRP.getZ());
81 :
82 : // fV = Length of the projection of aVPN on the yz plane:
83 0 : fV = aVPN.getYZLength();
84 :
85 0 : if ( fV != 0 )
86 : {
87 0 : basegfx::B3DHomMatrix aTemp;
88 0 : const double fSin(aVPN.getY() / fV);
89 0 : const double fCos(aVPN.getZ() / fV);
90 0 : aTemp.set(2, 2, fCos);
91 0 : aTemp.set(1, 1, fCos);
92 0 : aTemp.set(2, 1, fSin);
93 0 : aTemp.set(1, 2, -fSin);
94 0 : aViewTf *= aTemp;
95 : }
96 :
97 : {
98 0 : basegfx::B3DHomMatrix aTemp;
99 0 : const double fSin(-aVPN.getX());
100 0 : const double fCos(fV);
101 0 : aTemp.set(2, 2, fCos);
102 0 : aTemp.set(0, 0, fCos);
103 0 : aTemp.set(0, 2, fSin);
104 0 : aTemp.set(2, 0, -fSin);
105 0 : aViewTf *= aTemp;
106 : }
107 :
108 : // Convert X- and Y- coordinates of the view up vector to the
109 : // (preliminary) view coordinate system.
110 0 : fXupVp = aViewTf.get(0, 0) * aVUV.getX() + aViewTf.get(0, 1) * aVUV.getY() + aViewTf.get(0, 2) * aVUV.getZ();
111 0 : fYupVp = aViewTf.get(1, 0) * aVUV.getX() + aViewTf.get(1, 1) * aVUV.getY() + aViewTf.get(1, 2) * aVUV.getZ();
112 0 : fV = sqrt(fXupVp * fXupVp + fYupVp * fYupVp);
113 :
114 0 : if ( fV != 0 )
115 : {
116 0 : basegfx::B3DHomMatrix aTemp;
117 0 : const double fSin(fXupVp / fV);
118 0 : const double fCos(fYupVp / fV);
119 0 : aTemp.set(1, 1, fCos);
120 0 : aTemp.set(0, 0, fCos);
121 0 : aTemp.set(1, 0, fSin);
122 0 : aTemp.set(0, 1, -fSin);
123 0 : aViewTf *= aTemp;
124 : }
125 :
126 0 : bTfValid = true;
127 : }
128 0 : }
129 :
130 0 : void Viewport3D::SetDeviceWindow(const Rectangle& rRect)
131 : {
132 0 : long nNewW = rRect.GetWidth();
133 0 : long nNewH = rRect.GetHeight();
134 0 : long nOldW = aDeviceRect.GetWidth();
135 0 : long nOldH = aDeviceRect.GetHeight();
136 :
137 0 : switch ( eAspectMapping )
138 : {
139 : double fRatio, fTmp;
140 :
141 : // Mapping, without changing the real size of the objects in the
142 : // Device Window
143 : case AS_HOLD_SIZE:
144 : // When the Device is invalid (w, h = -1), adapt the View
145 : // with AsHoldX
146 0 : if ( nOldW > 0 && nOldH > 0 )
147 : {
148 0 : fRatio = (double) nNewW / nOldW;
149 0 : aViewWin.X *= fRatio;
150 0 : aViewWin.W *= fRatio;
151 0 : fRatio = (double) nNewH / nOldH;
152 0 : aViewWin.Y *= fRatio;
153 0 : aViewWin.H *= fRatio;
154 0 : break;
155 : }
156 : case AS_HOLD_X:
157 : // Adapt view height to view width
158 0 : fRatio = (double) nNewH / nNewW;
159 0 : fTmp = aViewWin.H;
160 0 : aViewWin.H = aViewWin.W * fRatio;
161 0 : aViewWin.Y = aViewWin.Y * aViewWin.H / fTmp;
162 0 : break;
163 :
164 : case AS_HOLD_Y:
165 : // Adapt view width to view height
166 0 : fRatio = (double) nNewW / nNewH;
167 0 : fTmp = aViewWin.W;
168 0 : aViewWin.W = aViewWin.H * fRatio;
169 0 : aViewWin.X = aViewWin.X * aViewWin.W / fTmp;
170 0 : break;
171 0 : default: break;
172 : }
173 0 : fWRatio = nNewW / aViewWin.W;
174 0 : fHRatio = nNewH / aViewWin.H;
175 :
176 0 : aDeviceRect = rRect;
177 0 : }
178 :
179 : // Set View Reference Point
180 :
181 0 : void Viewport3D::SetVRP(const basegfx::B3DPoint& rNewVRP)
182 : {
183 0 : aVRP = rNewVRP;
184 0 : bTfValid = false;
185 0 : }
186 :
187 : // Set View Plane Normal
188 :
189 0 : void Viewport3D::SetVPN(const basegfx::B3DVector& rNewVPN)
190 : {
191 0 : aVPN = rNewVPN;
192 0 : aVPN.normalize();
193 0 : bTfValid = false;
194 0 : }
195 :
196 : // Set View Up Vector
197 :
198 0 : void Viewport3D::SetVUV(const basegfx::B3DVector& rNewVUV)
199 : {
200 0 : aVUV = rNewVUV;
201 0 : bTfValid = false;
202 0 : }
203 :
204 : // Set Center Of Projection
205 :
206 0 : void Viewport3D::SetPRP(const basegfx::B3DPoint& rNewPRP)
207 : {
208 0 : aPRP = rNewPRP;
209 0 : aPRP.setX(0.0);
210 0 : aPRP.setY(0.0);
211 0 : bTfValid = false;
212 0 : }
213 :
214 : // Set View Plane Distance
215 :
216 0 : void Viewport3D::SetVPD(double fNewVPD)
217 : {
218 0 : fVPD = fNewVPD;
219 0 : bTfValid = false;
220 0 : }
221 :
222 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|