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/viewpt3d.hxx>
21 #include <basegfx/range/b3drange.hxx>
23 Viewport3D::Viewport3D() :
31 eProjection(PR_PERSPECTIVE
),
32 eAspectMapping(AS_NO_MAPPING
),
33 aDeviceRect(Point(0,0), Size(-1,-1)),
34 aViewPoint (0, 0, 5000),
39 aViewWin
.X
= -1; aViewWin
.Y
= -1;
40 aViewWin
.W
= 2; aViewWin
.H
= 2;
43 // Set ViewWindow (in View coordinates)
45 void Viewport3D::SetViewWindow(double fX
, double fY
, double fW
, double fH
)
49 if ( fW
> 0 ) aViewWin
.W
= fW
;
50 else aViewWin
.W
= 1.0;
51 if ( fH
> 0 ) aViewWin
.H
= fH
;
52 else aViewWin
.H
= 1.0;
54 fWRatio
= aDeviceRect
.GetWidth() / aViewWin
.W
;
55 fHRatio
= aDeviceRect
.GetHeight() / aViewWin
.H
;
58 // Returns observer position (PRP) in world coordinates
60 const basegfx::B3DPoint
& Viewport3D::GetViewPoint()
67 // Calculate View transformations matrix
69 void Viewport3D::MakeTransform(void)
73 double fV
, fXupVp
, fYupVp
;
74 aViewPoint
= aVRP
+ aVPN
* aPRP
.getZ();
76 // Reset to Identity matrix
79 // shift in the origin
80 aViewTf
.translate(-aVRP
.getX(), -aVRP
.getY(), -aVRP
.getZ());
82 // fV = Length of the projection of aVPN on the yz plane:
83 fV
= aVPN
.getYZLength();
87 basegfx::B3DHomMatrix aTemp
;
88 const double fSin(aVPN
.getY() / fV
);
89 const double fCos(aVPN
.getZ() / fV
);
90 aTemp
.set(2, 2, fCos
);
91 aTemp
.set(1, 1, fCos
);
92 aTemp
.set(2, 1, fSin
);
93 aTemp
.set(1, 2, -fSin
);
98 basegfx::B3DHomMatrix aTemp
;
99 const double fSin(-aVPN
.getX());
100 const double fCos(fV
);
101 aTemp
.set(2, 2, fCos
);
102 aTemp
.set(0, 0, fCos
);
103 aTemp
.set(0, 2, fSin
);
104 aTemp
.set(2, 0, -fSin
);
108 // Convert X- and Y- coordinates of the view up vector to the
109 // (preliminary) view coordinate system.
110 fXupVp
= aViewTf
.get(0, 0) * aVUV
.getX() + aViewTf
.get(0, 1) * aVUV
.getY() + aViewTf
.get(0, 2) * aVUV
.getZ();
111 fYupVp
= aViewTf
.get(1, 0) * aVUV
.getX() + aViewTf
.get(1, 1) * aVUV
.getY() + aViewTf
.get(1, 2) * aVUV
.getZ();
112 fV
= sqrt(fXupVp
* fXupVp
+ fYupVp
* fYupVp
);
116 basegfx::B3DHomMatrix aTemp
;
117 const double fSin(fXupVp
/ fV
);
118 const double fCos(fYupVp
/ fV
);
119 aTemp
.set(1, 1, fCos
);
120 aTemp
.set(0, 0, fCos
);
121 aTemp
.set(1, 0, fSin
);
122 aTemp
.set(0, 1, -fSin
);
130 void Viewport3D::SetDeviceWindow(const Rectangle
& rRect
)
132 long nNewW
= rRect
.GetWidth();
133 long nNewH
= rRect
.GetHeight();
134 long nOldW
= aDeviceRect
.GetWidth();
135 long nOldH
= aDeviceRect
.GetHeight();
137 switch ( eAspectMapping
)
141 // Mapping, without changing the real size of the objects in the
144 // When the Device is invalid (w, h = -1), adapt the View
146 if ( nOldW
> 0 && nOldH
> 0 )
148 fRatio
= (double) nNewW
/ nOldW
;
149 aViewWin
.X
*= fRatio
;
150 aViewWin
.W
*= fRatio
;
151 fRatio
= (double) nNewH
/ nOldH
;
152 aViewWin
.Y
*= fRatio
;
153 aViewWin
.H
*= fRatio
;
157 // Adapt view height to view width
158 fRatio
= (double) nNewH
/ nNewW
;
160 aViewWin
.H
= aViewWin
.W
* fRatio
;
161 aViewWin
.Y
= aViewWin
.Y
* aViewWin
.H
/ fTmp
;
165 // Adapt view width to view height
166 fRatio
= (double) nNewW
/ nNewH
;
168 aViewWin
.W
= aViewWin
.H
* fRatio
;
169 aViewWin
.X
= aViewWin
.X
* aViewWin
.W
/ fTmp
;
173 fWRatio
= nNewW
/ aViewWin
.W
;
174 fHRatio
= nNewH
/ aViewWin
.H
;
179 // Set View Reference Point
181 void Viewport3D::SetVRP(const basegfx::B3DPoint
& rNewVRP
)
184 bTfValid
= sal_False
;
187 // Set View Plane Normal
189 void Viewport3D::SetVPN(const basegfx::B3DVector
& rNewVPN
)
193 bTfValid
= sal_False
;
196 // Set View Up Vector
198 void Viewport3D::SetVUV(const basegfx::B3DVector
& rNewVUV
)
201 bTfValid
= sal_False
;
204 // Set Center Of Projection
206 void Viewport3D::SetPRP(const basegfx::B3DPoint
& rNewPRP
)
211 bTfValid
= sal_False
;
214 // Set View Plane Distance
216 void Viewport3D::SetVPD(double fNewVPD
)
219 bTfValid
= sal_False
;
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */