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
,
24 double fFocalLen
, double fBankAng
) :
26 aResetLookAt(rLookAt
),
27 fResetFocalLength(fFocalLen
),
28 fResetBankAngle(fBankAng
),
30 bAutoAdjustProjection(sal_True
)
35 SetFocalLength(fFocalLen
);
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
)
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
)
70 SetVPN(aPosition
- aLookAt
);
71 SetBankAngle(fBankAngle
);
75 void Camera3D::SetLookAt(const basegfx::B3DPoint
& rNewLookAt
)
77 if ( rNewLookAt
!= aLookAt
)
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
)
94 SetVPN(aPosition
- aLookAt
);
95 SetBankAngle(fBankAngle
);
99 void Camera3D::SetBankAngle(double fAngle
)
101 basegfx::B3DVector
aDiff(aPosition
- aLookAt
);
102 basegfx::B3DVector
aPrj(aDiff
);
105 if ( aDiff
.getY() == 0 )
110 { // aPrj = Projection from aDiff on the XZ-plane
113 if ( aDiff
.getY() < 0.0 )
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
);
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()));
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
);
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
);
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
);
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
);
188 void Camera3D::SetFocalLength(double fLen
)
192 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen
/ 35.0 * aViewWin
.W
));
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */