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