1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
,
26 aResetLookAt(rLookAt
),
28 bAutoAdjustProjection(true)
32 SetFocalLength(fFocalLen
);
36 : aResetPos(0.0, 0.0, 1.0)
39 , bAutoAdjustProjection(false)
43 // Set default values for reset
45 void Camera3D::SetDefaults(const basegfx::B3DPoint
& rPos
, const basegfx::B3DPoint
& rLookAt
)
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
)
66 SetVPN(aPosition
- aLookAt
);
67 SetBankAngle(fBankAngle
);
71 void Camera3D::SetLookAt(const basegfx::B3DPoint
& rNewLookAt
)
73 if ( rNewLookAt
!= aLookAt
)
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
)
90 SetVPN(aPosition
- aLookAt
);
91 SetBankAngle(fBankAngle
);
95 void Camera3D::SetBankAngle(double fAngle
)
97 basegfx::B3DVector
aDiff(aPosition
- aLookAt
);
98 basegfx::B3DVector
aPrj(aDiff
);
101 if ( aDiff
.getY() == 0 )
106 { // aPrj = Projection from aDiff on the XZ-plane
109 if ( aDiff
.getY() < 0.0 )
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
);
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()));
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
);
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
);
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
);
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
);
184 void Camera3D::SetFocalLength(double fLen
)
188 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen
/ 35.0 * aViewWin
.W
));
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */