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(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 : : fResetFocalLength(0.0)
40 : , fResetBankAngle(0.0)
41 : , fFocalLength(0.0)
42 : , fBankAngle(0.0)
43 0 : , bAutoAdjustProjection(false)
44 : {
45 0 : basegfx::B3DPoint aVector3D(0.0 ,0.0 ,1.0);
46 0 : Camera3D(aVector3D, basegfx::B3DPoint());
47 0 : }
48 :
49 : // Set default values for reset
50 :
51 0 : void Camera3D::SetDefaults(const basegfx::B3DPoint& rPos, const basegfx::B3DPoint& rLookAt,
52 : double fFocalLen, double fBankAng)
53 : {
54 0 : aResetPos = rPos;
55 0 : aResetLookAt = rLookAt;
56 0 : fResetFocalLength = fFocalLen;
57 0 : fResetBankAngle = fBankAng;
58 0 : }
59 :
60 : // Set ViewWindow and adjust PRP
61 :
62 0 : void Camera3D::SetViewWindow(double fX, double fY, double fW, double fH)
63 : {
64 0 : Viewport3D::SetViewWindow(fX, fY, fW, fH);
65 0 : if ( bAutoAdjustProjection )
66 0 : SetFocalLength(fFocalLength);
67 0 : }
68 :
69 0 : void Camera3D::SetPosition(const basegfx::B3DPoint& rNewPos)
70 : {
71 0 : if ( rNewPos != aPosition )
72 : {
73 0 : aPosition = rNewPos;
74 0 : SetVRP(aPosition);
75 0 : SetVPN(aPosition - aLookAt);
76 0 : SetBankAngle(fBankAngle);
77 : }
78 0 : }
79 :
80 0 : void Camera3D::SetLookAt(const basegfx::B3DPoint& rNewLookAt)
81 : {
82 0 : if ( rNewLookAt != aLookAt )
83 : {
84 0 : aLookAt = rNewLookAt;
85 0 : SetVPN(aPosition - aLookAt);
86 0 : SetBankAngle(fBankAngle);
87 : }
88 0 : }
89 :
90 0 : void Camera3D::SetPosAndLookAt(const basegfx::B3DPoint& rNewPos,
91 : const basegfx::B3DPoint& rNewLookAt)
92 : {
93 0 : if ( rNewPos != aPosition || rNewLookAt != aLookAt )
94 : {
95 0 : aPosition = rNewPos;
96 0 : aLookAt = rNewLookAt;
97 :
98 0 : SetVRP(aPosition);
99 0 : SetVPN(aPosition - aLookAt);
100 0 : SetBankAngle(fBankAngle);
101 : }
102 0 : }
103 :
104 0 : void Camera3D::SetBankAngle(double fAngle)
105 : {
106 0 : basegfx::B3DVector aDiff(aPosition - aLookAt);
107 0 : basegfx::B3DVector aPrj(aDiff);
108 0 : fBankAngle = fAngle;
109 :
110 0 : if ( aDiff.getY() == 0 )
111 : {
112 0 : aPrj.setY(-1.0);
113 : }
114 : else
115 : { // aPrj = Projection from aDiff on the XZ-plane
116 0 : aPrj.setY(0.0);
117 :
118 0 : if ( aDiff.getY() < 0.0 )
119 : {
120 0 : aPrj = -aPrj;
121 : }
122 : }
123 :
124 : // Calculate from aDiff to uppwards pointing View-Up-Vector
125 : // duplicated line is intentional!
126 0 : aPrj = aPrj.getPerpendicular(aDiff);
127 0 : aPrj = aPrj.getPerpendicular(aDiff);
128 0 : aDiff.normalize();
129 :
130 : // Rotate on Z axis, to rotate the BankAngle and back
131 0 : basegfx::B3DHomMatrix aTf;
132 0 : const double fV(sqrt(aDiff.getY() * aDiff.getY() + aDiff.getZ() * aDiff.getZ()));
133 :
134 0 : if ( fV != 0.0 )
135 : {
136 0 : basegfx::B3DHomMatrix aTemp;
137 0 : const double fSin(aDiff.getY() / fV);
138 0 : const double fCos(aDiff.getZ() / fV);
139 :
140 0 : aTemp.set(1, 1, fCos);
141 0 : aTemp.set(2, 2, fCos);
142 0 : aTemp.set(2, 1, fSin);
143 0 : aTemp.set(1, 2, -fSin);
144 :
145 0 : aTf *= aTemp;
146 : }
147 :
148 : {
149 0 : basegfx::B3DHomMatrix aTemp;
150 0 : const double fSin(-aDiff.getX());
151 0 : const double fCos(fV);
152 :
153 0 : aTemp.set(0, 0, fCos);
154 0 : aTemp.set(2, 2, fCos);
155 0 : aTemp.set(0, 2, fSin);
156 0 : aTemp.set(2, 0, -fSin);
157 :
158 0 : aTf *= aTemp;
159 : }
160 :
161 0 : aTf.rotate(0.0, 0.0, fBankAngle);
162 :
163 : {
164 0 : basegfx::B3DHomMatrix aTemp;
165 0 : const double fSin(aDiff.getX());
166 0 : const double fCos(fV);
167 :
168 0 : aTemp.set(0, 0, fCos);
169 0 : aTemp.set(2, 2, fCos);
170 0 : aTemp.set(0, 2, fSin);
171 0 : aTemp.set(2, 0, -fSin);
172 :
173 0 : aTf *= aTemp;
174 : }
175 :
176 0 : if ( fV != 0.0 )
177 : {
178 0 : basegfx::B3DHomMatrix aTemp;
179 0 : const double fSin(-aDiff.getY() / fV);
180 0 : const double fCos(aDiff.getZ() / fV);
181 :
182 0 : aTemp.set(1, 1, fCos);
183 0 : aTemp.set(2, 2, fCos);
184 0 : aTemp.set(2, 1, fSin);
185 0 : aTemp.set(1, 2, -fSin);
186 :
187 0 : aTf *= aTemp;
188 : }
189 :
190 0 : SetVUV(aTf * aPrj);
191 0 : }
192 :
193 0 : void Camera3D::SetFocalLength(double fLen)
194 : {
195 0 : if ( fLen < 5 )
196 0 : fLen = 5;
197 0 : SetPRP(basegfx::B3DPoint(0.0, 0.0, fLen / 35.0 * aViewWin.W));
198 0 : fFocalLength = fLen;
199 0 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|