-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewProj.cpp
231 lines (194 loc) · 6.25 KB
/
ViewProj.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
/***************************************************************************
* Copyright (c) 2005 Imetric 3D GmbH *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#include "ViewProj.h"
using namespace Base;
ViewProjMethod::ViewProjMethod()
: hasTransform(false)
{
}
/*! Calculate the composed projection matrix which is a product of
* projection matrix multiplied with input transformation matrix.
*/
Matrix4D ViewProjMethod::getComposedProjectionMatrix (void) const
{
Matrix4D mat = getProjectionMatrix();
// Compose the object transform, if defined
if (hasTransform) {
mat = mat * transform;
}
return mat;
}
/*!
* \brief This method applies an additional transformation to the input points
* passed with the () operator.
* \param mat
*/
void ViewProjMethod::setTransform(const Base::Matrix4D& mat)
{
transform = mat;
hasTransform = (mat != Base::Matrix4D());
}
void ViewProjMethod::transformInput(const Base::Vector3f& src, Base::Vector3f& dst) const
{
dst = src;
if (hasTransform) {
transform.multVec(dst, dst);
}
}
void ViewProjMethod::transformInput(const Base::Vector3d& src, Base::Vector3d& dst) const
{
dst = src;
if (hasTransform) {
transform.multVec(dst, dst);
}
}
//-----------------------------------------------------------------------------
ViewProjMatrix::ViewProjMatrix (const Matrix4D &rclMtx)
: _clMtx(rclMtx)
{
double m30 = _clMtx[3][0];
double m31 = _clMtx[3][1];
double m32 = _clMtx[3][2];
double m33 = _clMtx[3][3];
isOrthographic = (m30 == 0 && m31 == 0 && m32 == 0 && m33 == 1);
// Only for orthographic projection mode we can compute a single
// matrix performing all steps.
// For perspective projection the scaling and translation must
// be done afterwards because it depends on the input point.
if (isOrthographic) {
// Scale from [-1,1] to [0,1]
// As done in OpenInventor sources (see SbDPViewVolume::projectToScreen)
_clMtx.scale(0.5, 0.5, 0.5);
_clMtx.move(0.5, 0.5, 0.5);
}
_clMtxInv = _clMtx;
_clMtxInv.inverseGauss();
}
ViewProjMatrix::~ViewProjMatrix()
{
}
Matrix4D ViewProjMatrix::getProjectionMatrix (void) const
{
// Return the same matrix as passed to the constructor
Matrix4D mat(_clMtx);
if (isOrthographic) {
mat.move(-0.5, -0.5, -0.5);
mat.scale(2.0, 2.0, 2.0);
}
return mat;
}
template<typename Vec>
void perspectiveTransform(const Base::Matrix4D& mat, Vec& pnt)
{
double m30 = mat[3][0];
double m31 = mat[3][1];
double m32 = mat[3][2];
double m33 = mat[3][3];
double w = (pnt.x * m30 + pnt.y * m31 + pnt.z * m32 + m33);
mat.multVec(pnt, pnt);
pnt /= w;
}
Vector3f ViewProjMatrix::operator()(const Vector3f& inp) const
{
Vector3f src;
transformInput(inp, src);
Vector3f dst;
if (!isOrthographic) {
dst = src;
perspectiveTransform<Vector3f>(_clMtx, dst);
dst.Set(0.5*dst.x+0.5, 0.5*dst.y+0.5, 0.5*dst.z+0.5);
}
else {
_clMtx.multVec(src, dst);
}
return dst;
}
Vector3d ViewProjMatrix::operator()(const Vector3d& inp) const
{
Vector3d src;
transformInput(inp, src);
Vector3d dst;
if (!isOrthographic) {
dst = src;
perspectiveTransform<Vector3d>(_clMtx, dst);
dst.Set(0.5*dst.x+0.5, 0.5*dst.y+0.5, 0.5*dst.z+0.5);
}
else {
_clMtx.multVec(src, dst);
}
return dst;
}
Vector3f ViewProjMatrix::inverse (const Vector3f& src) const
{
Vector3f dst;
if (!isOrthographic) {
dst.Set(2.0*src.x-1.0, 2.0*src.y-1.0, 2.0*src.z-1.0);
perspectiveTransform<Vector3f>(_clMtxInv, dst);
}
else {
_clMtxInv.multVec(src, dst);
}
return dst;
}
Vector3d ViewProjMatrix::inverse (const Vector3d& src) const
{
Vector3d dst;
if (!isOrthographic) {
dst.Set(2.0*src.x-1.0, 2.0*src.y-1.0, 2.0*src.z-1.0);
perspectiveTransform<Vector3d>(_clMtxInv, dst);
}
else {
_clMtxInv.multVec(src, dst);
}
return dst;
}
// ----------------------------------------------------------------------------
ViewOrthoProjMatrix::ViewOrthoProjMatrix (const Matrix4D &rclMtx)
: _clMtx(rclMtx)
{
_clMtxInv = _clMtx;
_clMtxInv.inverse();
}
ViewOrthoProjMatrix::~ViewOrthoProjMatrix()
{
}
Matrix4D ViewOrthoProjMatrix::getProjectionMatrix (void) const
{
return _clMtx;
}
Vector3f ViewOrthoProjMatrix::operator()(const Vector3f &rclPt) const
{
return Vector3f(_clMtx * rclPt);
}
Vector3d ViewOrthoProjMatrix::operator()(const Vector3d &rclPt) const
{
return Vector3d(_clMtx * rclPt);
}
Vector3f ViewOrthoProjMatrix::inverse (const Vector3f &rclPt) const
{
return Vector3f(_clMtxInv * rclPt);
}
Vector3d ViewOrthoProjMatrix::inverse (const Vector3d &rclPt) const
{
return Vector3d(_clMtxInv * rclPt);
}