xref: /trunk/main/tools/inc/tools/b3dtrans.hxx (revision 8b851043)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _B3D_B3DTRANS_HXX
25 #define _B3D_B3DTRANS_HXX
26 
27 // Zu verwendender DephRange des Z-Buffers
28 #define ZBUFFER_DEPTH_RANGE			((double)(256L * 256L * 256L))
29 
30 #include <basegfx/matrix/b3dhommatrix.hxx>
31 #include <basegfx/range/b3drange.hxx>
32 #include <tools/gen.hxx>
33 #include <basegfx/matrix/b2dhommatrix.hxx>
34 #include <basegfx/point/b2dpoint.hxx>
35 #include <tools/toolsdllapi.h>
36 
37 // Vorausdeklarationen
38 
39 /*************************************************************************
40 |*
41 |* Unterstuetzte Methoden, um das Seitenverhaeltnis einzuhalten
42 |*
43 \************************************************************************/
44 
45 enum Base3DRatio
46 {
47 	Base3DRatioGrow = 1,
48 	Base3DRatioShrink,
49 	Base3DRatioMiddle
50 };
51 
52 /*************************************************************************
53 |*
54 |* Typ der Projektion
55 |*
56 \************************************************************************/
57 
58 enum Base3DProjectionType
59 {
60 	Base3DProjectionTypeParallel = 1,
61 	Base3DProjectionTypePerspective
62 };
63 
64 /*************************************************************************
65 |*
66 |* Transformationen fuer alle 3D Ausgaben
67 |*
68 \************************************************************************/
69 
70 class TOOLS_DLLPUBLIC B3dTransformationSet
71 {
72 private:
73 	// Object Matrix Object -> World
74 	basegfx::B3DHomMatrix			maObjectTrans;
75 	basegfx::B3DHomMatrix			maInvObjectTrans;
76 
77 	// Orientation Matrix
78 	basegfx::B3DHomMatrix			maOrientation;
79 	basegfx::B3DHomMatrix			maInvOrientation;
80 
81 	// Projection Matrix
82 	basegfx::B3DHomMatrix			maProjection;
83 	basegfx::B3DHomMatrix			maInvProjection;
84 
85 	// Texture Matrices
86 	basegfx::B2DHomMatrix			maTexture;
87 
88 	// Speziell zum Umwandeln von Punkten Objekt -> Device
89 	basegfx::B3DHomMatrix			maObjectToDevice;
90 
91 	// Transponierte Inverse fuer Vectortransformationen
92 	basegfx::B3DHomMatrix			maInvTransObjectToEye;
93 
94 	// Transformation World->View
95 	basegfx::B3DHomMatrix			maMatFromWorldToView;
96 	basegfx::B3DHomMatrix			maInvMatFromWorldToView;
97 
98 	// Parameters for ViewportTransformation
99 	basegfx::B3DVector			maScale;
100 	basegfx::B3DVector			maTranslate;
101 
102 	// ViewPlane DeviceRectangle (vom Benutzer gesetzt)
103 	double							mfLeftBound;
104 	double							mfRightBound;
105 	double							mfBottomBound;
106 	double							mfTopBound;
107 
108 	// Near and far clipping planes
109 	double							mfNearBound;
110 	double							mfFarBound;
111 
112 	// Seitenverhaeltnis der 3D Abbildung (Y / X)
113 	// default ist 1:1 -> 1.0
114 	// Deaktivieren mit 0.0 als Wert
115 	double							mfRatio;
116 
117 	// Der gesetzte Ausgabebereich (in logischen Koordinaten)
118 	// und der dazugehoerige sichtbare Bereich
119 	Rectangle						maViewportRectangle;
120 	Rectangle						maVisibleRectangle;
121 
122 	// Die tatsaechlich von CalcViewport gesetzten Abmessungen
123 	// des sichtbaren Bereichs (in logischen Koordinaten)
124 	Rectangle						maSetBound;
125 
126 	// Methode zur Aufrechterhaltung des Seitenverhaeltnisses
127 	// default ist Base3DRatioGrow
128 	Base3DRatio						meRatio;
129 
130 	// Flags
131 	unsigned						mbPerspective				: 1;
132 	unsigned						mbWorldToViewValid			: 1;
133 	unsigned						mbInvTransObjectToEyeValid	: 1;
134 	unsigned						mbObjectToDeviceValid		: 1;
135 	unsigned						mbProjectionValid			: 1;
136 
137 public:
138 	B3dTransformationSet();
139 	virtual ~B3dTransformationSet();
140 
141 	// Zurueck auf Standard
142 	void Reset();
143 
144 	// ObjectTrans
145 	void SetObjectTrans(const basegfx::B3DHomMatrix& rObj);
GetObjectTrans()146 	const basegfx::B3DHomMatrix& GetObjectTrans() { return maObjectTrans; }
GetInvObjectTrans()147 	const basegfx::B3DHomMatrix& GetInvObjectTrans() { return maInvObjectTrans; }
148 
149 	// Orientation
150 	void SetOrientation(
151 		basegfx::B3DPoint aVRP = basegfx::B3DPoint(0.0,0.0,1.0),
152 		basegfx::B3DVector aVPN = basegfx::B3DVector(0.0,0.0,1.0),
153 		basegfx::B3DVector aVUP = basegfx::B3DVector(0.0,1.0,0.0));
154 	void SetOrientation(basegfx::B3DHomMatrix& mOrient);
GetOrientation()155 	const basegfx::B3DHomMatrix& GetOrientation() { return maOrientation; }
GetInvOrientation()156 	const basegfx::B3DHomMatrix& GetInvOrientation() { return maInvOrientation; }
157 
158 	// Projection
159 	void SetProjection(const basegfx::B3DHomMatrix& mProject);
160 	const basegfx::B3DHomMatrix& GetProjection();
161 	const basegfx::B3DHomMatrix& GetInvProjection();
162 
163 	// Texture
164 	void SetTexture(const basegfx::B2DHomMatrix& rTxt);
GetTexture()165 	const basegfx::B2DHomMatrix& GetTexture() { return maTexture; }
166 
167 	// Seitenverhaeltnis und Modus zu dessen Aufrechterhaltung
GetRatio()168 	double GetRatio() { return mfRatio; }
169 	void SetRatio(double fNew=1.0);
GetRatioMode()170 	Base3DRatio GetRatioMode() { return meRatio; }
171 	void SetRatioMode(Base3DRatio eNew=Base3DRatioGrow);
172 
173 	// Parameter der ViewportTransformation
174 	void SetDeviceRectangle(double fL=-1.0, double fR=1.0, double fB=-1.0, double fT=1.0, sal_Bool bBroadCastChange=sal_True);
175 	void SetDeviceVolume(const basegfx::B3DRange& rVol, sal_Bool bBroadCastChange=sal_True);
176 	void GetDeviceRectangle(double &fL, double &fR, double& fB, double& fT);
177 	basegfx::B3DRange GetDeviceVolume();
GetDeviceRectangleWidth() const178 	double GetDeviceRectangleWidth() const { return mfRightBound - mfLeftBound; }
GetDeviceRectangleHeight() const179 	double GetDeviceRectangleHeight() const { return mfTopBound - mfBottomBound; }
180 	void SetFrontClippingPlane(double fF=0.0);
GetFrontClippingPlane()181 	double GetFrontClippingPlane() { return mfNearBound; }
182 	void SetBackClippingPlane(double fB=1.0);
GetBackClippingPlane()183 	double GetBackClippingPlane() { return mfFarBound; }
184 	void SetPerspective(sal_Bool bNew);
GetPerspective()185 	sal_Bool GetPerspective() { return mbPerspective; }
186 	void SetViewportRectangle(Rectangle& rRect, Rectangle& rVisible);
SetViewportRectangle(Rectangle & rRect)187 	void SetViewportRectangle(Rectangle& rRect) { SetViewportRectangle(rRect, rRect); }
GetViewportRectangle()188 	const Rectangle& GetViewportRectangle() { return maViewportRectangle; }
189 	void CalcViewport();
190 
191 	// Spezielle Matritzen anfordern
192 	basegfx::B3DHomMatrix GetMatFromObjectToView();
193 
194 	// Transponierte Inverse fuer Vectortransformationen
195 	const basegfx::B3DHomMatrix& GetInvTransObjectToEye();
196 
197 	// Speziell zum Umwandeln von Punkten Objekt -> Device
198 	const basegfx::B3DHomMatrix& GetObjectToDevice();
199 
200 	// Speziell zum Umwandeln von Punkten World -> View
201 	const basegfx::B3DHomMatrix& GetMatFromWorldToView();
202 	const basegfx::B3DHomMatrix& GetInvMatFromWorldToView();
203 
204 	// Bounds des Viewports lesen
205 	const Rectangle& GetLogicalViewportBounds();
206 	const basegfx::B3DVector& GetScale();
207 	const basegfx::B3DVector& GetTranslate();
208 
209 	// Direkter Zugriff auf verschiedene Transformationen
210 	const basegfx::B3DPoint WorldToEyeCoor(const basegfx::B3DPoint& rVec);
211 	const basegfx::B3DPoint EyeToWorldCoor(const basegfx::B3DPoint& rVec);
212 	const basegfx::B3DPoint EyeToViewCoor(const basegfx::B3DPoint& rVec);
213 	const basegfx::B3DPoint ViewToEyeCoor(const basegfx::B3DPoint& rVec);
214 	const basegfx::B3DPoint WorldToViewCoor(const basegfx::B3DPoint& rVec);
215 	const basegfx::B3DPoint ViewToWorldCoor(const basegfx::B3DPoint& rVec);
216 	const basegfx::B3DPoint DeviceToViewCoor(const basegfx::B3DPoint& rVec);
217 	const basegfx::B3DPoint ViewToDeviceCoor(const basegfx::B3DPoint& rVec);
218 	const basegfx::B3DPoint ObjectToWorldCoor(const basegfx::B3DPoint& rVec);
219 	const basegfx::B3DPoint WorldToObjectCoor(const basegfx::B3DPoint& rVec);
220 	const basegfx::B3DPoint ObjectToViewCoor(const basegfx::B3DPoint& rVec);
221 	const basegfx::B3DPoint ViewToObjectCoor(const basegfx::B3DPoint& rVec);
222 	const basegfx::B3DPoint ObjectToEyeCoor(const basegfx::B3DPoint& rVec);
223 	const basegfx::B3DPoint EyeToObjectCoor(const basegfx::B3DPoint& rVec);
224 	const basegfx::B3DPoint DeviceToEyeCoor(const basegfx::B3DPoint& rVec);
225 	const basegfx::B3DPoint EyeToDeviceCoor(const basegfx::B3DPoint& rVec);
226 
227 	const basegfx::B3DPoint InvTransObjectToEye(const basegfx::B3DPoint& rVec);
228 	const basegfx::B2DPoint TransTextureCoor(const basegfx::B2DPoint& rVec);
229 
230 	static void Frustum(
231 		basegfx::B3DHomMatrix& rTarget,
232 		double fLeft = -1.0, double fRight = 1.0,
233 		double fBottom = -1.0, double fTop = 1.0,
234 		double fNear = 0.001, double fFar = 1.0);
235 	static void Ortho(
236 		basegfx::B3DHomMatrix& rTarget,
237 		double fLeft = -1.0, double fRight = 1.0,
238 		double fBottom = -1.0, double fTop = 1.0,
239 		double fNear = 0.0, double fFar = 1.0);
240 	static void Orientation(
241 		basegfx::B3DHomMatrix& rTarget,
242 		basegfx::B3DPoint aVRP = basegfx::B3DPoint(0.0,0.0,1.0),
243 		basegfx::B3DVector aVPN = basegfx::B3DVector(0.0,0.0,1.0),
244 		basegfx::B3DVector aVUP = basegfx::B3DVector(0.0,1.0,0.0));
245 
246 protected:
247 	void PostSetObjectTrans();
248 	void PostSetOrientation();
249 	void PostSetProjection();
250 	void PostSetTexture();
251 	void PostSetViewport();
252 
253 	void CalcMatObjectToDevice();
254 	void CalcMatFromWorldToView();
255 	void CalcMatInvTransObjectToEye();
256 
257 	virtual void DeviceRectangleChange();
258 };
259 
260 /*************************************************************************
261 |*
262 |* Viewport fuer B3D
263 |*
264 |* Verwendet wird hier ein vereinfachtes System, bei dem der abzubildende
265 |* Punkt durch VRP repraesentiert wird
266 |*
267 \************************************************************************/
268 
269 class TOOLS_DLLPUBLIC B3dViewport : public B3dTransformationSet
270 {
271 private:
272 	basegfx::B3DPoint				aVRP;			// View Reference Point
273 	basegfx::B3DVector			aVPN;			// View Plane Normal
274 	basegfx::B3DVector			aVUV;			// View Up Vector
275 
276 public:
277 	B3dViewport();
278 	virtual ~B3dViewport();
279 
280 	void SetVRP(const basegfx::B3DPoint& rNewVRP);
281 	void SetVPN(const basegfx::B3DVector& rNewVPN);
282 	void SetVUV(const basegfx::B3DVector& rNewVUV);
283 	void SetViewportValues(
284 		const basegfx::B3DPoint& rNewVRP,
285 		const basegfx::B3DVector& rNewVPN,
286 		const basegfx::B3DVector& rNewVUV);
287 
GetVRP() const288 	const basegfx::B3DPoint&	GetVRP() const	{ return aVRP; }
GetVPN() const289 	const basegfx::B3DVector&	GetVPN() const	{ return aVPN; }
GetVUV() const290 	const basegfx::B3DVector&	GetVUV() const	{ return aVUV; }
291 
292 protected:
293 	void CalcOrientation();
294 };
295 
296 /*************************************************************************
297 |*
298 |* Kamera fuer B3D
299 |*
300 \************************************************************************/
301 
302 class TOOLS_DLLPUBLIC B3dCamera : public B3dViewport
303 {
304 private:
305 	basegfx::B3DPoint		aPosition;
306 	basegfx::B3DPoint		aCorrectedPosition;
307 	basegfx::B3DVector	aLookAt;
308 	double					fFocalLength;
309 	double					fBankAngle;
310 
311 	unsigned				bUseFocalLength			: 1;
312 
313 public:
314 	B3dCamera(
315 		const basegfx::B3DPoint& rPos = basegfx::B3DPoint(0.0, 0.0, 1.0),
316 		const basegfx::B3DVector& rLkAt = basegfx::B3DVector(0.0, 0.0, 0.0),
317 		double fFocLen = 35.0, double fBnkAng = 0.0,
318 		sal_Bool bUseFocLen = sal_False);
319 	virtual ~B3dCamera();
320 
321 	// Positionen
322 	void SetPosition(const basegfx::B3DPoint& rNewPos);
GetPosition() const323 	const basegfx::B3DPoint& GetPosition() const { return aPosition; }
324 	void SetLookAt(const basegfx::B3DVector& rNewLookAt);
GetLookAt() const325 	const basegfx::B3DVector& GetLookAt() const { return aLookAt; }
326 	void SetPositionAndLookAt(const basegfx::B3DPoint& rNewPos, const basegfx::B3DVector& rNewLookAt);
327 
328 	// Brennweite in mm
329 	void SetFocalLength(double fLen);
GetFocalLength() const330 	double GetFocalLength() const { return fFocalLength; }
331 
332 	// Neigung links/rechts
333 	void SetBankAngle(double fAngle);
GetBankAngle() const334 	double GetBankAngle() const { return fBankAngle; }
335 
336 	// FocalLength Flag
337 	void SetUseFocalLength(sal_Bool bNew);
GetUseFocalLength() const338 	sal_Bool GetUseFocalLength() const { return (sal_Bool)bUseFocalLength; }
339 
340 protected:
341 	void CalcNewViewportValues();
342 	sal_Bool CalcFocalLength();
343 
344 	virtual void DeviceRectangleChange();
345 };
346 
347 
348 #endif          // _B3D_B3DTRANS_HXX
349