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 #ifndef INCLUDED_TOOLS_B3DTRANS_HXX
21 #define INCLUDED_TOOLS_B3DTRANS_HXX
22 
23 #define ZBUFFER_DEPTH_RANGE         (double(256L * 256L * 256L))
24 
25 #include <basegfx/matrix/b3dhommatrix.hxx>
26 #include <tools/gen.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/point/b3dpoint.hxx>
29 #include <basegfx/vector/b3dvector.hxx>
30 #include <tools/toolsdllapi.h>
31 
32 /// Transformation sets for 3D output
33 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC B3dTransformationSet
34 {
35 private:
36     // Object Matrix Object -> World
37     basegfx::B3DHomMatrix maObjectTrans;
38     basegfx::B3DHomMatrix maInvObjectTrans;
39 
40     // Orientation Matrix
41     basegfx::B3DHomMatrix maOrientation;
42     basegfx::B3DHomMatrix maInvOrientation;
43 
44     // Projection Matrix
45     basegfx::B3DHomMatrix maProjection;
46     basegfx::B3DHomMatrix maInvProjection;
47 
48     // Texture Matrices
49     basegfx::B2DHomMatrix maTexture;
50 
51     // Parameters for ViewportTransformation
52     basegfx::B3DVector    maScale;
53     basegfx::B3DVector    maTranslate;
54 
55     // ViewPlane DeviceRectangle (user-defined)
56     double                mfLeftBound;
57     double                mfRightBound;
58     double                mfBottomBound;
59     double                mfTopBound;
60 
61     // Aspect ratio of 3D transformation (Y / X)
62     // default: 1:1 -> 1.0
63     // Disable with value 0.0
64     double                mfRatio;
65 
66     // Viewport area in logical coordinates
67     tools::Rectangle             maViewportRectangle;
68     // Visible area within viewport
69     tools::Rectangle             maVisibleRectangle;
70 
71     // Actual coordinates as set by CalcViewport
72     // of visible viewport area (logical coordinates)
73     tools::Rectangle             maSetBound;
74 
75     // Flags
76     bool mbPerspective              : 1;
77     bool mbProjectionValid          : 1;
78 
79 public:
80     B3dTransformationSet();
81     virtual ~B3dTransformationSet();
82 
83     B3dTransformationSet(B3dTransformationSet const &) = default;
84     B3dTransformationSet(B3dTransformationSet &&) = default;
85     B3dTransformationSet & operator =(B3dTransformationSet const &) = default;
86     B3dTransformationSet & operator =(B3dTransformationSet &&) = default;
87 
88     void Reset();
89 
90     /** Set the orientation
91 
92         @param vVRP the View Reference Point (VRP)
93         @param vVPN the View Plane Normal (VPN)
94         @param vVUP the View Up Plane (VUP)
95     */
96     void SetOrientation(
97         const basegfx::B3DPoint& rVRP = basegfx::B3DPoint(0.0,0.0,1.0),
98         const basegfx::B3DVector& rVPN = basegfx::B3DVector(0.0,0.0,1.0),
99         const basegfx::B3DVector& rVUP = basegfx::B3DVector(0.0,1.0,0.0));
100 
101     // Projection
102     void SetProjection(const basegfx::B3DHomMatrix& mProject);
103     const basegfx::B3DHomMatrix& GetProjection();
104 
105     // Texture
106 
107     // aspect ratio accessors and the defined method of keeping defined aspect ratio
GetRatio() const108     double GetRatio() const { return mfRatio; }
109     void SetRatio(double fNew);
110 
111     // Parameters of ViewportTransformation
112     void SetDeviceRectangle(double fL=-1.0, double fR=1.0,
113                             double fB=-1.0, double fT=1.0);
GetDeviceRectangleWidth() const114     double GetDeviceRectangleWidth() const { return mfRightBound - mfLeftBound; }
115 
116     void SetPerspective(bool bNew);
117 
118     void SetViewportRectangle(tools::Rectangle const & rRect, tools::Rectangle const & rVisible);
SetViewportRectangle(tools::Rectangle const & rRect)119     void SetViewportRectangle(tools::Rectangle const & rRect) { SetViewportRectangle(rRect, rRect); }
120 
121     void CalcViewport();
122 
123     // Direct accessors for miscellaneous transformations
124     basegfx::B3DPoint WorldToEyeCoor(const basegfx::B3DPoint& rVec);
125     basegfx::B3DPoint EyeToWorldCoor(const basegfx::B3DPoint& rVec);
126 
127     static void Frustum(
128         basegfx::B3DHomMatrix& rTarget,
129         double fLeft = -1.0, double fRight = 1.0,
130         double fBottom = -1.0, double fTop = 1.0,
131         double fNear = 0.001, double fFar = 1.0);
132     static void Ortho(
133         basegfx::B3DHomMatrix& rTarget,
134         double fLeft = -1.0, double fRight = 1.0,
135         double fBottom = -1.0, double fTop = 1.0,
136         double fNear = 0.0, double fFar = 1.0);
137     static void Orientation(
138         basegfx::B3DHomMatrix& rTarget,
139         const basegfx::B3DPoint& aVRP = basegfx::B3DPoint(0.0,0.0,1.0),
140         basegfx::B3DVector aVPN = basegfx::B3DVector(0.0,0.0,1.0),
141         basegfx::B3DVector aVUP = basegfx::B3DVector(0.0,1.0,0.0));
142 
143 protected:
144     void PostSetObjectTrans();
145     void PostSetOrientation();
146     void PostSetProjection();
147 
148     virtual void DeviceRectangleChange();
149 };
150 
151 /** Viewport for B3D
152 
153     Uses a simplified model, in which a point is described using a View
154     Reference Point (VRP).
155 */
156 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC B3dViewport : public B3dTransformationSet
157 {
158 private:
159     basegfx::B3DPoint           aVRP;   // View Reference Point
160     basegfx::B3DVector          aVPN;   // View Plane Normal
161     basegfx::B3DVector          aVUV;   // View Up Vector
162 
163 public:
164     B3dViewport();
165     virtual ~B3dViewport() override;
166 
167     B3dViewport(B3dViewport const &) = default;
168     B3dViewport(B3dViewport &&) = default;
169     B3dViewport & operator =(B3dViewport const &) = default;
170     B3dViewport & operator =(B3dViewport &&) = default;
171 
172     void SetVUV(const basegfx::B3DVector& rNewVUV);
173     void SetViewportValues(
174         const basegfx::B3DPoint& rNewVRP,
175         const basegfx::B3DVector& rNewVPN,
176         const basegfx::B3DVector& rNewVUV);
177 
GetVRP() const178     const basegfx::B3DPoint&    GetVRP() const  { return aVRP; }
GetVPN() const179     const basegfx::B3DVector&   GetVPN() const  { return aVPN; }
GetVUV() const180     const basegfx::B3DVector&   GetVUV() const  { return aVUV; }
181 
182 protected:
183     void CalcOrientation();
184 };
185 
186 // B3D camera
187 
188 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC B3dCamera final : public B3dViewport
189 {
190 public:
191     B3dCamera(
192         const basegfx::B3DPoint& rPos = basegfx::B3DPoint(0.0, 0.0, 1.0),
193         const basegfx::B3DVector& rLkAt = basegfx::B3DVector(0.0, 0.0, 0.0),
194         double fFocLen = 35.0, double fBnkAng = 0.0);
195     virtual ~B3dCamera() override;
196 
197     B3dCamera(B3dCamera const &) = default;
198     B3dCamera(B3dCamera &&) = default;
199     B3dCamera & operator =(B3dCamera const &) = default;
200     B3dCamera & operator =(B3dCamera &&) = default;
201 
202 private:
203     void CalcNewViewportValues();
204     void CalcFocalLength();
205 
206     virtual void DeviceRectangleChange() override;
207 
208     basegfx::B3DPoint       aPosition;
209     basegfx::B3DVector  aLookAt;
210     double                  fFocalLength;
211     double                  fBankAngle;
212 
213 };
214 
215 #endif
216 
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
218