summaryrefslogtreecommitdiff
path: root/include/tools/b3dtrans.hxx
blob: c0747cfca174ffbdadce042078bb258640da4328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_TOOLS_B3DTRANS_HXX
#define INCLUDED_TOOLS_B3DTRANS_HXX

#define ZBUFFER_DEPTH_RANGE         (double(256L * 256L * 256L))

#include <config_options.h>
#include <basegfx/matrix/b3dhommatrix.hxx>
#include <tools/gen.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <basegfx/point/b3dpoint.hxx>
#include <basegfx/vector/b3dvector.hxx>
#include <tools/toolsdllapi.h>

/// Transformation sets for 3D output
class SAL_WARN_UNUSED UNLESS_MERGELIBS(TOOLS_DLLPUBLIC) B3dTransformationSet
{
private:
    // Object Matrix Object -> World
    basegfx::B3DHomMatrix maObjectTrans;
    basegfx::B3DHomMatrix maInvObjectTrans;

    // Orientation Matrix
    basegfx::B3DHomMatrix maOrientation;
    basegfx::B3DHomMatrix maInvOrientation;

    // Projection Matrix
    basegfx::B3DHomMatrix maProjection;
    basegfx::B3DHomMatrix maInvProjection;

    // Texture Matrices
    basegfx::B2DHomMatrix maTexture;

    // Parameters for ViewportTransformation
    basegfx::B3DVector    maScale;
    basegfx::B3DVector    maTranslate;

    // ViewPlane DeviceRectangle (user-defined)
    double                mfLeftBound;
    double                mfRightBound;
    double                mfBottomBound;
    double                mfTopBound;

    // Aspect ratio of 3D transformation (Y / X)
    // default: 1:1 -> 1.0
    // Disable with value 0.0
    double                mfRatio;

    // Viewport area in logical coordinates
    tools::Rectangle             maViewportRectangle;
    // Visible area within viewport
    tools::Rectangle             maVisibleRectangle;

    // Actual coordinates as set by CalcViewport
    // of visible viewport area (logical coordinates)
    tools::Rectangle             maSetBound;

    // Flags
    bool mbPerspective              : 1;
    bool mbProjectionValid          : 1;

public:
    B3dTransformationSet();
    virtual ~B3dTransformationSet();

    B3dTransformationSet(B3dTransformationSet const &) = default;
    B3dTransformationSet(B3dTransformationSet &&) = default;
    B3dTransformationSet & operator =(B3dTransformationSet const &) = default;
    B3dTransformationSet & operator =(B3dTransformationSet &&) = default;

    void Reset();

    /** Set the orientation

        @param vVRP the View Reference Point (VRP)
        @param vVPN the View Plane Normal (VPN)
        @param vVUP the View Up Plane (VUP)
    */
    void SetOrientation(
        const basegfx::B3DPoint& rVRP = basegfx::B3DPoint(0.0,0.0,1.0),
        const basegfx::B3DVector& rVPN = basegfx::B3DVector(0.0,0.0,1.0),
        const basegfx::B3DVector& rVUP = basegfx::B3DVector(0.0,1.0,0.0));

    // Projection
    void SetProjection(const basegfx::B3DHomMatrix& mProject);
    const basegfx::B3DHomMatrix& GetProjection();

    // Texture

    // aspect ratio accessors and the defined method of keeping defined aspect ratio
    double GetRatio() const { return mfRatio; }
    void SetRatio(double fNew);

    // Parameters of ViewportTransformation
    void SetDeviceRectangle(double fL=-1.0, double fR=1.0,
                            double fB=-1.0, double fT=1.0);
    double GetDeviceRectangleWidth() const { return mfRightBound - mfLeftBound; }

    void SetPerspective(bool bNew);

    void SetViewportRectangle(tools::Rectangle const & rRect, tools::Rectangle const & rVisible);
    void SetViewportRectangle(tools::Rectangle const & rRect) { SetViewportRectangle(rRect, rRect); }

    void CalcViewport();

    // Direct accessors for miscellaneous transformations
    basegfx::B3DPoint WorldToEyeCoor(const basegfx::B3DPoint& rVec);
    basegfx::B3DPoint EyeToWorldCoor(const basegfx::B3DPoint& rVec);

    static void Frustum(
        basegfx::B3DHomMatrix& rTarget,
        double fLeft = -1.0, double fRight = 1.0,
        double fBottom = -1.0, double fTop = 1.0,
        double fNear = 0.001, double fFar = 1.0);
    static void Ortho(
        basegfx::B3DHomMatrix& rTarget,
        double fLeft = -1.0, double fRight = 1.0,
        double fBottom = -1.0, double fTop = 1.0,
        double fNear = 0.0, double fFar = 1.0);
    static void Orientation(
        basegfx::B3DHomMatrix& rTarget,
        const basegfx::B3DPoint& aVRP = basegfx::B3DPoint(0.0,0.0,1.0),
        basegfx::B3DVector aVPN = basegfx::B3DVector(0.0,0.0,1.0),
        basegfx::B3DVector aVUP = basegfx::B3DVector(0.0,1.0,0.0));

protected:
    void PostSetObjectTrans();
    void PostSetOrientation();
    void PostSetProjection();

    virtual void DeviceRectangleChange();
};

/** Viewport for B3D

    Uses a simplified model, in which a point is described using a View
    Reference Point (VRP).
*/
class SAL_WARN_UNUSED UNLESS_MERGELIBS(TOOLS_DLLPUBLIC) B3dViewport : public B3dTransformationSet
{
private:
    basegfx::B3DPoint           aVRP;   // View Reference Point
    basegfx::B3DVector          aVPN;   // View Plane Normal
    basegfx::B3DVector          aVUV;   // View Up Vector

public:
    B3dViewport();
    virtual ~B3dViewport() override;

    B3dViewport(B3dViewport const &) = default;
    B3dViewport(B3dViewport &&) = default;
    B3dViewport & operator =(B3dViewport const &) = default;
    B3dViewport & operator =(B3dViewport &&) = default;

    void SetVUV(const basegfx::B3DVector& rNewVUV);
    void SetViewportValues(
        const basegfx::B3DPoint& rNewVRP,
        const basegfx::B3DVector& rNewVPN,
        const basegfx::B3DVector& rNewVUV);

    const basegfx::B3DPoint&    GetVRP() const  { return aVRP; }
    const basegfx::B3DVector&   GetVPN() const  { return aVPN; }
    const basegfx::B3DVector&   GetVUV() const  { return aVUV; }

protected:
    void CalcOrientation();
};

// B3D camera

class SAL_WARN_UNUSED UNLESS_MERGELIBS(TOOLS_DLLPUBLIC) B3dCamera final : public B3dViewport
{
public:
    B3dCamera(
        const basegfx::B3DPoint& rPos = basegfx::B3DPoint(0.0, 0.0, 1.0),
        const basegfx::B3DVector& rLkAt = basegfx::B3DVector(0.0, 0.0, 0.0),
        double fFocLen = 35.0, double fBnkAng = 0.0);
    virtual ~B3dCamera() override;

    B3dCamera(B3dCamera const &) = default;
    B3dCamera(B3dCamera &&) = default;
    B3dCamera & operator =(B3dCamera const &) = default;
    B3dCamera & operator =(B3dCamera &&) = default;

private:
    void CalcNewViewportValues();
    void CalcFocalLength();

    virtual void DeviceRectangleChange() override;

    basegfx::B3DPoint       aPosition;
    basegfx::B3DVector  aLookAt;
    double                  fFocalLength;
    double                  fBankAngle;

};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */