fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / engine3d / viewpt3d2.cxx
blob084f6550d1a9f5a9408b3b5c7511bf7627405aaa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svx/viewpt3d.hxx>
21 #include <basegfx/range/b3drange.hxx>
23 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(0),
36 fWRatio (1.0),
37 fHRatio (1.0)
39 aViewWin.X = -1; aViewWin.Y = -1;
40 aViewWin.W = 2; aViewWin.H = 2;
43 // Set ViewWindow (in View coordinates)
45 void Viewport3D::SetViewWindow(double fX, double fY, double fW, double fH)
47 aViewWin.X = fX;
48 aViewWin.Y = fY;
49 if ( fW > 0 ) aViewWin.W = fW;
50 else aViewWin.W = 1.0;
51 if ( fH > 0 ) aViewWin.H = fH;
52 else aViewWin.H = 1.0;
54 fWRatio = aDeviceRect.GetWidth() / aViewWin.W;
55 fHRatio = aDeviceRect.GetHeight() / aViewWin.H;
58 // Returns observer position (PRP) in world coordinates
60 const basegfx::B3DPoint& Viewport3D::GetViewPoint()
62 MakeTransform();
64 return aViewPoint;
67 // Calculate View transformations matrix
69 void Viewport3D::MakeTransform(void)
71 if ( !bTfValid )
73 double fV, fXupVp, fYupVp;
74 aViewPoint = aVRP + aVPN * aPRP.getZ();
76 // Reset to Identity matrix
77 aViewTf.identity();
79 // shift in the origin
80 aViewTf.translate(-aVRP.getX(), -aVRP.getY(), -aVRP.getZ());
82 // fV = Length of the projection of aVPN on the yz plane:
83 fV = aVPN.getYZLength();
85 if ( fV != 0 )
87 basegfx::B3DHomMatrix aTemp;
88 const double fSin(aVPN.getY() / fV);
89 const double fCos(aVPN.getZ() / fV);
90 aTemp.set(2, 2, fCos);
91 aTemp.set(1, 1, fCos);
92 aTemp.set(2, 1, fSin);
93 aTemp.set(1, 2, -fSin);
94 aViewTf *= aTemp;
98 basegfx::B3DHomMatrix aTemp;
99 const double fSin(-aVPN.getX());
100 const double fCos(fV);
101 aTemp.set(2, 2, fCos);
102 aTemp.set(0, 0, fCos);
103 aTemp.set(0, 2, fSin);
104 aTemp.set(2, 0, -fSin);
105 aViewTf *= aTemp;
108 // Convert X- and Y- coordinates of the view up vector to the
109 // (preliminary) view coordinate system.
110 fXupVp = aViewTf.get(0, 0) * aVUV.getX() + aViewTf.get(0, 1) * aVUV.getY() + aViewTf.get(0, 2) * aVUV.getZ();
111 fYupVp = aViewTf.get(1, 0) * aVUV.getX() + aViewTf.get(1, 1) * aVUV.getY() + aViewTf.get(1, 2) * aVUV.getZ();
112 fV = sqrt(fXupVp * fXupVp + fYupVp * fYupVp);
114 if ( fV != 0 )
116 basegfx::B3DHomMatrix aTemp;
117 const double fSin(fXupVp / fV);
118 const double fCos(fYupVp / fV);
119 aTemp.set(1, 1, fCos);
120 aTemp.set(0, 0, fCos);
121 aTemp.set(1, 0, fSin);
122 aTemp.set(0, 1, -fSin);
123 aViewTf *= aTemp;
126 bTfValid = sal_True;
130 void Viewport3D::SetDeviceWindow(const Rectangle& rRect)
132 long nNewW = rRect.GetWidth();
133 long nNewH = rRect.GetHeight();
134 long nOldW = aDeviceRect.GetWidth();
135 long nOldH = aDeviceRect.GetHeight();
137 switch ( eAspectMapping )
139 double fRatio, fTmp;
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 if ( nOldW > 0 && nOldH > 0 )
148 fRatio = (double) nNewW / nOldW;
149 aViewWin.X *= fRatio;
150 aViewWin.W *= fRatio;
151 fRatio = (double) nNewH / nOldH;
152 aViewWin.Y *= fRatio;
153 aViewWin.H *= fRatio;
154 break;
156 case AS_HOLD_X:
157 // Adapt view height to view width
158 fRatio = (double) nNewH / nNewW;
159 fTmp = aViewWin.H;
160 aViewWin.H = aViewWin.W * fRatio;
161 aViewWin.Y = aViewWin.Y * aViewWin.H / fTmp;
162 break;
164 case AS_HOLD_Y:
165 // Adapt view width to view height
166 fRatio = (double) nNewW / nNewH;
167 fTmp = aViewWin.W;
168 aViewWin.W = aViewWin.H * fRatio;
169 aViewWin.X = aViewWin.X * aViewWin.W / fTmp;
170 break;
171 default: break;
173 fWRatio = nNewW / aViewWin.W;
174 fHRatio = nNewH / aViewWin.H;
176 aDeviceRect = rRect;
179 // Set View Reference Point
181 void Viewport3D::SetVRP(const basegfx::B3DPoint& rNewVRP)
183 aVRP = rNewVRP;
184 bTfValid = sal_False;
187 // Set View Plane Normal
189 void Viewport3D::SetVPN(const basegfx::B3DVector& rNewVPN)
191 aVPN = rNewVPN;
192 aVPN.normalize();
193 bTfValid = sal_False;
196 // Set View Up Vector
198 void Viewport3D::SetVUV(const basegfx::B3DVector& rNewVUV)
200 aVUV = rNewVUV;
201 bTfValid = sal_False;
204 // Set Center Of Projection
206 void Viewport3D::SetPRP(const basegfx::B3DPoint& rNewPRP)
208 aPRP = rNewPRP;
209 aPRP.setX(0.0);
210 aPRP.setY(0.0);
211 bTfValid = sal_False;
214 // Set View Plane Distance
216 void Viewport3D::SetVPD(double fNewVPD)
218 fVPD = fNewVPD;
219 bTfValid = sal_False;
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */