bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / engine3d / camera3d.cxx
blobef2e4d711c9f1cc9c6df24bbaa66cd0ad70529b8
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(true)
32 SetVPD(0);
33 SetPosition(rPos);
34 SetLookAt(rLookAt);
35 SetFocalLength(fFocalLen);
38 Camera3D::Camera3D()
39 : aResetPos(0.0, 0.0, 1.0)
40 , fResetFocalLength(0.0)
41 , fResetBankAngle(0.0)
42 , fFocalLength(35.0)
43 , fBankAngle(0.0)
44 , bAutoAdjustProjection(false)
48 // Set default values for reset
50 void Camera3D::SetDefaults(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
51 double fFocalLen, double fBankAng)
53 aResetPos = rPos;
54 aResetLookAt = rLookAt;
55 fResetFocalLength = fFocalLen;
56 fResetBankAngle = fBankAng;
59 // Set ViewWindow and adjust PRP
61 void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
63 Viewport3D::SetViewWindow(fX, fY, fW, fH);
64 if ( bAutoAdjustProjection )
65 SetFocalLength(fFocalLength);
68 void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
70 if ( rNewPos != aPosition )
72 aPosition = rNewPos;
73 SetVRP(aPosition);
74 SetVPN(aPosition - aLookAt);
75 SetBankAngle(fBankAngle);
79 void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
81 if ( rNewLookAt != aLookAt )
83 aLookAt = rNewLookAt;
84 SetVPN(aPosition - aLookAt);
85 SetBankAngle(fBankAngle);
89 void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
90 const basegfx::B3DPoint& rNewLookAt)
92 if ( rNewPos != aPosition || rNewLookAt != aLookAt )
94 aPosition = rNewPos;
95 aLookAt = rNewLookAt;
97 SetVRP(aPosition);
98 SetVPN(aPosition - aLookAt);
99 SetBankAngle(fBankAngle);
103 void Camera3D::SetBankAngle(double fAngle)
105 basegfx::B3DVector aDiff(aPosition - aLookAt);
106 basegfx::B3DVector aPrj(aDiff);
107 fBankAngle = fAngle;
109 if ( aDiff.getY() == 0 )
111 aPrj.setY(-1.0);
113 else
114 { // aPrj = Projection from aDiff on the XZ-plane
115 aPrj.setY(0.0);
117 if ( aDiff.getY() < 0.0 )
119 aPrj = -aPrj;
123 // Calculate from aDiff to uppwards pointing View-Up-Vector
124 // duplicated line is intentional!
125 aPrj = aPrj.getPerpendicular(aDiff);
126 aPrj = aPrj.getPerpendicular(aDiff);
127 aDiff.normalize();
129 // Rotate on Z axis, to rotate the BankAngle and back
130 basegfx::B3DHomMatrix aTf;
131 const double fV(sqrt(aDiff.getY() * aDiff.getY() + aDiff.getZ() * aDiff.getZ()));
133 if ( fV != 0.0 )
135 basegfx::B3DHomMatrix aTemp;
136 const double fSin(aDiff.getY() / fV);
137 const double fCos(aDiff.getZ() / fV);
139 aTemp.set(1, 1, fCos);
140 aTemp.set(2, 2, fCos);
141 aTemp.set(2, 1, fSin);
142 aTemp.set(1, 2, -fSin);
144 aTf *= aTemp;
148 basegfx::B3DHomMatrix aTemp;
149 const double fSin(-aDiff.getX());
150 const double fCos(fV);
152 aTemp.set(0, 0, fCos);
153 aTemp.set(2, 2, fCos);
154 aTemp.set(0, 2, fSin);
155 aTemp.set(2, 0, -fSin);
157 aTf *= aTemp;
160 aTf.rotate(0.0, 0.0, fBankAngle);
163 basegfx::B3DHomMatrix aTemp;
164 const double fSin(aDiff.getX());
165 const double fCos(fV);
167 aTemp.set(0, 0, fCos);
168 aTemp.set(2, 2, fCos);
169 aTemp.set(0, 2, fSin);
170 aTemp.set(2, 0, -fSin);
172 aTf *= aTemp;
175 if ( fV != 0.0 )
177 basegfx::B3DHomMatrix aTemp;
178 const double fSin(-aDiff.getY() / fV);
179 const double fCos(aDiff.getZ() / fV);
181 aTemp.set(1, 1, fCos);
182 aTemp.set(2, 2, fCos);
183 aTemp.set(2, 1, fSin);
184 aTemp.set(1, 2, -fSin);
186 aTf *= aTemp;
189 SetVUV(aTf * aPrj);
192 void Camera3D::SetFocalLength(double fLen)
194 if ( fLen < 5 )
195 fLen = 5;
196 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * aViewWin.W));
197 fFocalLength = fLen;
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */