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(true)
35 SetFocalLength(fFocalLen
);
39 : aResetPos(0.0, 0.0, 1.0)
40 , fResetFocalLength(0.0)
41 , fResetBankAngle(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
)
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
)
74 SetVPN(aPosition
- aLookAt
);
75 SetBankAngle(fBankAngle
);
79 void Camera3D::SetLookAt(const basegfx::B3DPoint
& rNewLookAt
)
81 if ( rNewLookAt
!= aLookAt
)
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
)
98 SetVPN(aPosition
- aLookAt
);
99 SetBankAngle(fBankAngle
);
103 void Camera3D::SetBankAngle(double fAngle
)
105 basegfx::B3DVector
aDiff(aPosition
- aLookAt
);
106 basegfx::B3DVector
aPrj(aDiff
);
109 if ( aDiff
.getY() == 0 )
114 { // aPrj = Projection from aDiff on the XZ-plane
117 if ( aDiff
.getY() < 0.0 )
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
);
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()));
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
);
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
);
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
);
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
);
192 void Camera3D::SetFocalLength(double fLen
)
196 SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen
/ 35.0 * aViewWin
.W
));
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */