defer finding dialog parent until we need it
[LibreOffice.git] / svx / source / engine3d / camera3d.cxx
blobd281ae91fa915ce9508b7902005eb3487ac1b2ce
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>
22 Camera3D::Camera3D(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
23 double fFocalLen)
24 : m_fBankAngle(0)
25 , m_bAutoAdjustProjection(true)
27 SetPosition(rPos);
28 SetLookAt(rLookAt);
29 SetFocalLength(fFocalLen);
32 Camera3D::Camera3D()
33 : m_fFocalLength(35.0)
34 , m_fBankAngle(0.0)
35 , m_bAutoAdjustProjection(false)
39 // Set ViewWindow and adjust PRP
41 void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
43 Viewport3D::SetViewWindow(fX, fY, fW, fH);
44 if (m_bAutoAdjustProjection)
45 SetFocalLength(m_fFocalLength);
48 void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
50 if (rNewPos != m_aPosition)
52 m_aPosition = rNewPos;
53 SetVRP(m_aPosition);
54 SetVPN(m_aPosition - m_aLookAt);
55 SetBankAngle(m_fBankAngle);
59 void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
61 if (rNewLookAt != m_aLookAt)
63 m_aLookAt = rNewLookAt;
64 SetVPN(m_aPosition - m_aLookAt);
65 SetBankAngle(m_fBankAngle);
69 void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
70 const basegfx::B3DPoint& rNewLookAt)
72 if (rNewPos != m_aPosition || rNewLookAt != m_aLookAt)
74 m_aPosition = rNewPos;
75 m_aLookAt = rNewLookAt;
77 SetVRP(m_aPosition);
78 SetVPN(m_aPosition - m_aLookAt);
79 SetBankAngle(m_fBankAngle);
83 void Camera3D::SetBankAngle(double fAngle)
85 basegfx::B3DVector aDiff(m_aPosition - m_aLookAt);
86 basegfx::B3DVector aPrj(aDiff);
87 m_fBankAngle = fAngle;
89 if (aDiff.getY() == 0)
91 aPrj.setY(-1.0);
93 else
94 { // aPrj = Projection from aDiff on the XZ-plane
95 aPrj.setY(0.0);
97 if (aDiff.getY() < 0.0)
99 aPrj = -aPrj;
103 // Calculate from aDiff to upwards pointing View-Up-Vector
104 // duplicated line is intentional!
105 aPrj = aPrj.getPerpendicular(aDiff);
106 aPrj = aPrj.getPerpendicular(aDiff);
107 aDiff.normalize();
109 // Rotate on Z axis, to rotate the BankAngle and back
110 basegfx::B3DHomMatrix aTf;
111 const double fV(std::hypot(aDiff.getY(), aDiff.getZ()));
113 if (fV != 0.0)
115 basegfx::B3DHomMatrix aTemp;
116 const double fSin(aDiff.getY() / fV);
117 const double fCos(aDiff.getZ() / fV);
119 aTemp.set(1, 1, fCos);
120 aTemp.set(2, 2, fCos);
121 aTemp.set(2, 1, fSin);
122 aTemp.set(1, 2, -fSin);
124 aTf *= aTemp;
128 basegfx::B3DHomMatrix aTemp;
129 const double fSin(-aDiff.getX());
130 const double fCos(fV);
132 aTemp.set(0, 0, fCos);
133 aTemp.set(2, 2, fCos);
134 aTemp.set(0, 2, fSin);
135 aTemp.set(2, 0, -fSin);
137 aTf *= aTemp;
140 aTf.rotate(0.0, 0.0, m_fBankAngle);
143 basegfx::B3DHomMatrix aTemp;
144 const double fSin(aDiff.getX());
145 const double fCos(fV);
147 aTemp.set(0, 0, fCos);
148 aTemp.set(2, 2, fCos);
149 aTemp.set(0, 2, fSin);
150 aTemp.set(2, 0, -fSin);
152 aTf *= aTemp;
155 if (fV != 0.0)
157 basegfx::B3DHomMatrix aTemp;
158 const double fSin(-aDiff.getY() / fV);
159 const double fCos(aDiff.getZ() / fV);
161 aTemp.set(1, 1, fCos);
162 aTemp.set(2, 2, fCos);
163 aTemp.set(2, 1, fSin);
164 aTemp.set(1, 2, -fSin);
166 aTf *= aTemp;
169 SetVUV(aTf * aPrj);
172 void Camera3D::SetFocalLength(double fLen)
174 if (fLen < 5)
175 fLen = 5;
176 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * m_aViewWin.W));
177 m_fFocalLength = fLen;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */