Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <svx/camera3d.hxx>
21 : #include <tools/stream.hxx>
22 :
23 0 : Camera3D::Camera3D(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
24 : double fFocalLen, double fBankAng) :
25 : aResetPos(rPos),
26 : aResetLookAt(rLookAt),
27 : fResetFocalLength(fFocalLen),
28 : fResetBankAngle(fBankAng),
29 : fBankAngle(fBankAng),
30 0 : bAutoAdjustProjection(sal_True)
31 : {
32 0 : SetVPD(0);
33 0 : SetPosition(rPos);
34 0 : SetLookAt(rLookAt);
35 0 : SetFocalLength(fFocalLen);
36 0 : }
37 :
38 0 : Camera3D::Camera3D()
39 : {
40 0 : basegfx::B3DPoint aVector3D(0.0 ,0.0 ,1.0);
41 0 : Camera3D(aVector3D, basegfx::B3DPoint());
42 0 : }
43 :
44 : // Set default values for reset
45 :
46 0 : void Camera3D::SetDefaults(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
47 : double fFocalLen, double fBankAng)
48 : {
49 0 : aResetPos = rPos;
50 0 : aResetLookAt = rLookAt;
51 0 : fResetFocalLength = fFocalLen;
52 0 : fResetBankAngle = fBankAng;
53 0 : }
54 :
55 : // Set ViewWindow and adjust PRP
56 :
57 0 : void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
58 : {
59 0 : Viewport3D::SetViewWindow(fX, fY, fW, fH);
60 0 : if ( bAutoAdjustProjection )
61 0 : SetFocalLength(fFocalLength);
62 0 : }
63 :
64 0 : void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
65 : {
66 0 : if ( rNewPos != aPosition )
67 : {
68 0 : aPosition = rNewPos;
69 0 : SetVRP(aPosition);
70 0 : SetVPN(aPosition - aLookAt);
71 0 : SetBankAngle(fBankAngle);
72 : }
73 0 : }
74 :
75 0 : void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
76 : {
77 0 : if ( rNewLookAt != aLookAt )
78 : {
79 0 : aLookAt = rNewLookAt;
80 0 : SetVPN(aPosition - aLookAt);
81 0 : SetBankAngle(fBankAngle);
82 : }
83 0 : }
84 :
85 0 : void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
86 : const basegfx::B3DPoint& rNewLookAt)
87 : {
88 0 : if ( rNewPos != aPosition || rNewLookAt != aLookAt )
89 : {
90 0 : aPosition = rNewPos;
91 0 : aLookAt = rNewLookAt;
92 :
93 0 : SetVRP(aPosition);
94 0 : SetVPN(aPosition - aLookAt);
95 0 : SetBankAngle(fBankAngle);
96 : }
97 0 : }
98 :
99 0 : void Camera3D::SetBankAngle(double fAngle)
100 : {
101 0 : basegfx::B3DVector aDiff(aPosition - aLookAt);
102 0 : basegfx::B3DVector aPrj(aDiff);
103 0 : fBankAngle = fAngle;
104 :
105 0 : if ( aDiff.getY() == 0 )
106 : {
107 0 : aPrj.setY(-1.0);
108 : }
109 : else
110 : { // aPrj = Projection from aDiff on the XZ-plane
111 0 : aPrj.setY(0.0);
112 :
113 0 : if ( aDiff.getY() < 0.0 )
114 : {
115 0 : aPrj = -aPrj;
116 : }
117 : }
118 :
119 : // Calculate from aDiff to uppwards pointing View-Up-Vector
120 0 : aPrj = aPrj.getPerpendicular(aDiff);
121 0 : aPrj = aPrj.getPerpendicular(aDiff);
122 0 : aDiff.normalize();
123 :
124 : // Rotate on Z axis, to rotate the BankAngle and back
125 0 : basegfx::B3DHomMatrix aTf;
126 0 : const double fV(sqrt(aDiff.getY() * aDiff.getY() + aDiff.getZ() * aDiff.getZ()));
127 :
128 0 : if ( fV != 0.0 )
129 : {
130 0 : basegfx::B3DHomMatrix aTemp;
131 0 : const double fSin(aDiff.getY() / fV);
132 0 : const double fCos(aDiff.getZ() / fV);
133 :
134 0 : aTemp.set(1, 1, fCos);
135 0 : aTemp.set(2, 2, fCos);
136 0 : aTemp.set(2, 1, fSin);
137 0 : aTemp.set(1, 2, -fSin);
138 :
139 0 : aTf *= aTemp;
140 : }
141 :
142 : {
143 0 : basegfx::B3DHomMatrix aTemp;
144 0 : const double fSin(-aDiff.getX());
145 0 : const double fCos(fV);
146 :
147 0 : aTemp.set(0, 0, fCos);
148 0 : aTemp.set(2, 2, fCos);
149 0 : aTemp.set(0, 2, fSin);
150 0 : aTemp.set(2, 0, -fSin);
151 :
152 0 : aTf *= aTemp;
153 : }
154 :
155 0 : aTf.rotate(0.0, 0.0, fBankAngle);
156 :
157 : {
158 0 : basegfx::B3DHomMatrix aTemp;
159 0 : const double fSin(aDiff.getX());
160 0 : const double fCos(fV);
161 :
162 0 : aTemp.set(0, 0, fCos);
163 0 : aTemp.set(2, 2, fCos);
164 0 : aTemp.set(0, 2, fSin);
165 0 : aTemp.set(2, 0, -fSin);
166 :
167 0 : aTf *= aTemp;
168 : }
169 :
170 0 : if ( fV != 0.0 )
171 : {
172 0 : basegfx::B3DHomMatrix aTemp;
173 0 : const double fSin(-aDiff.getY() / fV);
174 0 : const double fCos(aDiff.getZ() / fV);
175 :
176 0 : aTemp.set(1, 1, fCos);
177 0 : aTemp.set(2, 2, fCos);
178 0 : aTemp.set(2, 1, fSin);
179 0 : aTemp.set(1, 2, -fSin);
180 :
181 0 : aTf *= aTemp;
182 : }
183 :
184 0 : SetVUV(aTf * aPrj);
185 0 : }
186 :
187 0 : void Camera3D::SetFocalLength(double fLen)
188 : {
189 0 : if ( fLen < 5 )
190 0 : fLen = 5;
191 0 : SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * aViewWin.W));
192 0 : fFocalLength = fLen;
193 0 : }
194 :
195 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|