bump product version to 6.1.0.2
[LibreOffice.git] / svx / source / engine3d / camera3d.cxx
blobb9ea65e563134bb51d1fac7f761411fe9b2a3493
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) :
25 aResetPos(rPos),
26 aResetLookAt(rLookAt),
27 fBankAngle(0),
28 bAutoAdjustProjection(true)
30 SetPosition(rPos);
31 SetLookAt(rLookAt);
32 SetFocalLength(fFocalLen);
35 Camera3D::Camera3D()
36 : aResetPos(0.0, 0.0, 1.0)
37 , fFocalLength(35.0)
38 , fBankAngle(0.0)
39 , bAutoAdjustProjection(false)
43 // Set default values for reset
45 void Camera3D::SetDefaults(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt)
47 aResetPos = rPos;
48 aResetLookAt = rLookAt;
51 // Set ViewWindow and adjust PRP
53 void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
55 Viewport3D::SetViewWindow(fX, fY, fW, fH);
56 if ( bAutoAdjustProjection )
57 SetFocalLength(fFocalLength);
60 void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
62 if ( rNewPos != aPosition )
64 aPosition = rNewPos;
65 SetVRP(aPosition);
66 SetVPN(aPosition - aLookAt);
67 SetBankAngle(fBankAngle);
71 void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
73 if ( rNewLookAt != aLookAt )
75 aLookAt = rNewLookAt;
76 SetVPN(aPosition - aLookAt);
77 SetBankAngle(fBankAngle);
81 void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
82 const basegfx::B3DPoint& rNewLookAt)
84 if ( rNewPos != aPosition || rNewLookAt != aLookAt )
86 aPosition = rNewPos;
87 aLookAt = rNewLookAt;
89 SetVRP(aPosition);
90 SetVPN(aPosition - aLookAt);
91 SetBankAngle(fBankAngle);
95 void Camera3D::SetBankAngle(double fAngle)
97 basegfx::B3DVector aDiff(aPosition - aLookAt);
98 basegfx::B3DVector aPrj(aDiff);
99 fBankAngle = fAngle;
101 if ( aDiff.getY() == 0 )
103 aPrj.setY(-1.0);
105 else
106 { // aPrj = Projection from aDiff on the XZ-plane
107 aPrj.setY(0.0);
109 if ( aDiff.getY() < 0.0 )
111 aPrj = -aPrj;
115 // Calculate from aDiff to upwards pointing View-Up-Vector
116 // duplicated line is intentional!
117 aPrj = aPrj.getPerpendicular(aDiff);
118 aPrj = aPrj.getPerpendicular(aDiff);
119 aDiff.normalize();
121 // Rotate on Z axis, to rotate the BankAngle and back
122 basegfx::B3DHomMatrix aTf;
123 const double fV(sqrt(aDiff.getY() * aDiff.getY() + aDiff.getZ() * aDiff.getZ()));
125 if ( fV != 0.0 )
127 basegfx::B3DHomMatrix aTemp;
128 const double fSin(aDiff.getY() / fV);
129 const double fCos(aDiff.getZ() / fV);
131 aTemp.set(1, 1, fCos);
132 aTemp.set(2, 2, fCos);
133 aTemp.set(2, 1, fSin);
134 aTemp.set(1, 2, -fSin);
136 aTf *= aTemp;
140 basegfx::B3DHomMatrix aTemp;
141 const double fSin(-aDiff.getX());
142 const double fCos(fV);
144 aTemp.set(0, 0, fCos);
145 aTemp.set(2, 2, fCos);
146 aTemp.set(0, 2, fSin);
147 aTemp.set(2, 0, -fSin);
149 aTf *= aTemp;
152 aTf.rotate(0.0, 0.0, fBankAngle);
155 basegfx::B3DHomMatrix aTemp;
156 const double fSin(aDiff.getX());
157 const double fCos(fV);
159 aTemp.set(0, 0, fCos);
160 aTemp.set(2, 2, fCos);
161 aTemp.set(0, 2, fSin);
162 aTemp.set(2, 0, -fSin);
164 aTf *= aTemp;
167 if ( fV != 0.0 )
169 basegfx::B3DHomMatrix aTemp;
170 const double fSin(-aDiff.getY() / fV);
171 const double fCos(aDiff.getZ() / fV);
173 aTemp.set(1, 1, fCos);
174 aTemp.set(2, 2, fCos);
175 aTemp.set(2, 1, fSin);
176 aTemp.set(1, 2, -fSin);
178 aTf *= aTemp;
181 SetVUV(aTf * aPrj);
184 void Camera3D::SetFocalLength(double fLen)
186 if ( fLen < 5 )
187 fLen = 5;
188 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * aViewWin.W));
189 fFocalLength = fLen;
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */