Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / engine3d / viewpt3d2.cxx
blob660af6adfbe646b6d2ed79e2db1aed7005e4bcd5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 Viewport3D::Viewport3D() :
23 aVRP(0, 0, 5),
24 aVPN(0, 0, 1),
25 aVUV(0, 1, 1),
26 aPRP(0, 0, 2),
27 eProjection(ProjectionType::Perspective),
28 aDeviceRect(Point(0,0), Size(-1,-1)),
29 aViewPoint (0, 0, 5000),
30 bTfValid(false)
32 aViewWin.X = -1; aViewWin.Y = -1;
33 aViewWin.W = 2; aViewWin.H = 2;
36 // Set ViewWindow (in View coordinates)
38 void Viewport3D::SetViewWindow(double fX, double fY, double fW, double fH)
40 aViewWin.X = fX;
41 aViewWin.Y = fY;
42 if ( fW > 0 ) aViewWin.W = fW;
43 else aViewWin.W = 1.0;
44 if ( fH > 0 ) aViewWin.H = fH;
45 else aViewWin.H = 1.0;
48 // Returns observer position (PRP) in world coordinates
50 const basegfx::B3DPoint& Viewport3D::GetViewPoint()
52 // Calculate View transformations matrix
53 if ( !bTfValid )
55 double fV, fXupVp, fYupVp;
56 aViewPoint = aVRP + aVPN * aPRP.getZ();
58 // Reset to Identity matrix
59 aViewTf.identity();
61 // shift in the origin
62 aViewTf.translate(-aVRP.getX(), -aVRP.getY(), -aVRP.getZ());
64 // fV = Length of the projection of aVPN on the yz plane:
65 fV = aVPN.getYZLength();
67 if ( fV != 0 )
69 basegfx::B3DHomMatrix aTemp;
70 const double fSin(aVPN.getY() / fV);
71 const double fCos(aVPN.getZ() / fV);
72 aTemp.set(2, 2, fCos);
73 aTemp.set(1, 1, fCos);
74 aTemp.set(2, 1, fSin);
75 aTemp.set(1, 2, -fSin);
76 aViewTf *= aTemp;
80 basegfx::B3DHomMatrix aTemp;
81 const double fSin(-aVPN.getX());
82 const double fCos(fV);
83 aTemp.set(2, 2, fCos);
84 aTemp.set(0, 0, fCos);
85 aTemp.set(0, 2, fSin);
86 aTemp.set(2, 0, -fSin);
87 aViewTf *= aTemp;
90 // Convert X- and Y- coordinates of the view up vector to the
91 // (preliminary) view coordinate system.
92 fXupVp = aViewTf.get(0, 0) * aVUV.getX() + aViewTf.get(0, 1) * aVUV.getY() + aViewTf.get(0, 2) * aVUV.getZ();
93 fYupVp = aViewTf.get(1, 0) * aVUV.getX() + aViewTf.get(1, 1) * aVUV.getY() + aViewTf.get(1, 2) * aVUV.getZ();
94 fV = sqrt(fXupVp * fXupVp + fYupVp * fYupVp);
96 if ( fV != 0 )
98 basegfx::B3DHomMatrix aTemp;
99 const double fSin(fXupVp / fV);
100 const double fCos(fYupVp / fV);
101 aTemp.set(1, 1, fCos);
102 aTemp.set(0, 0, fCos);
103 aTemp.set(1, 0, fSin);
104 aTemp.set(0, 1, -fSin);
105 aViewTf *= aTemp;
108 bTfValid = true;
110 return aViewPoint;
113 void Viewport3D::SetDeviceWindow(const tools::Rectangle& rRect)
115 aDeviceRect = rRect;
118 // Set View Reference Point
120 void Viewport3D::SetVRP(const basegfx::B3DPoint& rNewVRP)
122 aVRP = rNewVRP;
123 bTfValid = false;
126 // Set View Plane Normal
128 void Viewport3D::SetVPN(const basegfx::B3DVector& rNewVPN)
130 aVPN = rNewVPN;
131 aVPN.normalize();
132 bTfValid = false;
135 // Set View Up Vector
137 void Viewport3D::SetVUV(const basegfx::B3DVector& rNewVUV)
139 aVUV = rNewVUV;
140 bTfValid = false;
143 // Set Center Of Projection
145 void Viewport3D::SetPRP(const basegfx::B3DPoint& rNewPRP)
147 aPRP = rNewPRP;
148 aPRP.setX(0.0);
149 aPRP.setY(0.0);
150 bTfValid = false;
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */