bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / engine3d / camera3d.cxx
blob533b456de315171400860c605c1e7bce99626a92
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/camera3d.hxx>
21 #include <tools/stream.hxx>
23 Camera3D::Camera3D(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
24 double fFocalLen, double fBankAng) :
25 aResetPos(rPos),
26 aResetLookAt(rLookAt),
27 fResetFocalLength(fFocalLen),
28 fResetBankAngle(fBankAng),
29 fBankAngle(fBankAng),
30 bAutoAdjustProjection(sal_True)
32 SetVPD(0);
33 SetPosition(rPos);
34 SetLookAt(rLookAt);
35 SetFocalLength(fFocalLen);
38 Camera3D::Camera3D()
40 basegfx::B3DPoint aVector3D(0.0 ,0.0 ,1.0);
41 Camera3D(aVector3D, basegfx::B3DPoint());
44 // Set default values for reset
46 void Camera3D::SetDefaults(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
47 double fFocalLen, double fBankAng)
49 aResetPos = rPos;
50 aResetLookAt = rLookAt;
51 fResetFocalLength = fFocalLen;
52 fResetBankAngle = fBankAng;
55 // Set ViewWindow and adjust PRP
57 void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
59 Viewport3D::SetViewWindow(fX, fY, fW, fH);
60 if ( bAutoAdjustProjection )
61 SetFocalLength(fFocalLength);
64 void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
66 if ( rNewPos != aPosition )
68 aPosition = rNewPos;
69 SetVRP(aPosition);
70 SetVPN(aPosition - aLookAt);
71 SetBankAngle(fBankAngle);
75 void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
77 if ( rNewLookAt != aLookAt )
79 aLookAt = rNewLookAt;
80 SetVPN(aPosition - aLookAt);
81 SetBankAngle(fBankAngle);
85 void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
86 const basegfx::B3DPoint& rNewLookAt)
88 if ( rNewPos != aPosition || rNewLookAt != aLookAt )
90 aPosition = rNewPos;
91 aLookAt = rNewLookAt;
93 SetVRP(aPosition);
94 SetVPN(aPosition - aLookAt);
95 SetBankAngle(fBankAngle);
99 void Camera3D::SetBankAngle(double fAngle)
101 basegfx::B3DVector aDiff(aPosition - aLookAt);
102 basegfx::B3DVector aPrj(aDiff);
103 fBankAngle = fAngle;
105 if ( aDiff.getY() == 0 )
107 aPrj.setY(-1.0);
109 else
110 { // aPrj = Projection from aDiff on the XZ-plane
111 aPrj.setY(0.0);
113 if ( aDiff.getY() < 0.0 )
115 aPrj = -aPrj;
119 // Calculate from aDiff to uppwards pointing View-Up-Vector
120 // duplicated line is intentional!
121 aPrj = aPrj.getPerpendicular(aDiff);
122 aPrj = aPrj.getPerpendicular(aDiff);
123 aDiff.normalize();
125 // Rotate on Z axis, to rotate the BankAngle and back
126 basegfx::B3DHomMatrix aTf;
127 const double fV(sqrt(aDiff.getY() * aDiff.getY() + aDiff.getZ() * aDiff.getZ()));
129 if ( fV != 0.0 )
131 basegfx::B3DHomMatrix aTemp;
132 const double fSin(aDiff.getY() / fV);
133 const double fCos(aDiff.getZ() / fV);
135 aTemp.set(1, 1, fCos);
136 aTemp.set(2, 2, fCos);
137 aTemp.set(2, 1, fSin);
138 aTemp.set(1, 2, -fSin);
140 aTf *= aTemp;
144 basegfx::B3DHomMatrix aTemp;
145 const double fSin(-aDiff.getX());
146 const double fCos(fV);
148 aTemp.set(0, 0, fCos);
149 aTemp.set(2, 2, fCos);
150 aTemp.set(0, 2, fSin);
151 aTemp.set(2, 0, -fSin);
153 aTf *= aTemp;
156 aTf.rotate(0.0, 0.0, fBankAngle);
159 basegfx::B3DHomMatrix aTemp;
160 const double fSin(aDiff.getX());
161 const double fCos(fV);
163 aTemp.set(0, 0, fCos);
164 aTemp.set(2, 2, fCos);
165 aTemp.set(0, 2, fSin);
166 aTemp.set(2, 0, -fSin);
168 aTf *= aTemp;
171 if ( fV != 0.0 )
173 basegfx::B3DHomMatrix aTemp;
174 const double fSin(-aDiff.getY() / fV);
175 const double fCos(aDiff.getZ() / fV);
177 aTemp.set(1, 1, fCos);
178 aTemp.set(2, 2, fCos);
179 aTemp.set(2, 1, fSin);
180 aTemp.set(1, 2, -fSin);
182 aTf *= aTemp;
185 SetVUV(aTf * aPrj);
188 void Camera3D::SetFocalLength(double fLen)
190 if ( fLen < 5 )
191 fLen = 5;
192 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * aViewWin.W));
193 fFocalLength = fLen;
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */