| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2005 Imetric 3D GmbH * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "ViewProj.h" |
| 27 |
|
| 28 |
using namespace Base; |
| 29 |
|
| 30 |
ViewProjMethod::ViewProjMethod() = default; |
| 31 |
|
| 32 |
bool ViewProjMethod::isValid() const |
| 33 |
{ |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
/*! Calculate the composed projection matrix which is a product of |
| 38 |
* projection matrix multiplied with input transformation matrix. |
| 39 |
*/ |
| 40 |
Matrix4D ViewProjMethod::getComposedProjectionMatrix() const |
| 41 |
{ |
| 42 |
Matrix4D mat = getProjectionMatrix(); |
| 43 |
|
| 44 |
// Compose the object transform, if defined |
| 45 |
if (hasTransform) { |
| 46 |
mat = mat * transform; |
| 47 |
} |
| 48 |
|
| 49 |
return mat; |
| 50 |
} |
| 51 |
|
| 52 |
/*! |
| 53 |
* \brief This method applies an additional transformation to the input points |
| 54 |
* passed with the () operator. |
| 55 |
* \param mat |
| 56 |
*/ |
| 57 |
void ViewProjMethod::setTransform(const Base::Matrix4D& mat) |
| 58 |
{ |
| 59 |
transform = mat; |
| 60 |
hasTransform = (mat != Base::Matrix4D()); |
| 61 |
} |
| 62 |
|
| 63 |
void ViewProjMethod::transformInput(const Base::Vector3f& src, Base::Vector3f& dst) const |
| 64 |
{ |
| 65 |
dst = src; |
| 66 |
if (hasTransform) { |
| 67 |
transform.multVec(dst, dst); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void ViewProjMethod::transformInput(const Base::Vector3d& src, Base::Vector3d& dst) const |
| 72 |
{ |
| 73 |
dst = src; |
| 74 |
if (hasTransform) { |
| 75 |
transform.multVec(dst, dst); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
//----------------------------------------------------------------------------- |
| 80 |
|
| 81 |
ViewProjMatrix::ViewProjMatrix(const Matrix4D& rclMtx) |
| 82 |
: _clMtx(rclMtx) |
| 83 |
{ |
| 84 |
double m30 = _clMtx[3][0]; |
| 85 |
double m31 = _clMtx[3][1]; |
| 86 |
double m32 = _clMtx[3][2]; |
| 87 |
double m33 = _clMtx[3][3]; |
| 88 |
isOrthographic = (m30 == 0.0 && m31 == 0.0 && m32 == 0.0 && m33 == 1.0); |
| 89 |
|
| 90 |
// Only for orthographic projection mode we can compute a single |
| 91 |
// matrix performing all steps. |
| 92 |
// For perspective projection the scaling and translation must |
| 93 |
// be done afterwards because it depends on the input point. |
| 94 |
if (isOrthographic) { |
| 95 |
// Scale from [-1,1] to [0,1] |
| 96 |
// As done in OpenInventor sources (see SbDPViewVolume::projectToScreen) |
| 97 |
_clMtx.scale(0.5, 0.5, 0.5); |
| 98 |
_clMtx.move(0.5, 0.5, 0.5); |
| 99 |
} |
| 100 |
|
| 101 |
_clMtxInv = _clMtx; |
| 102 |
_clMtxInv.inverseGauss(); |
| 103 |
} |
| 104 |
|
| 105 |
Matrix4D ViewProjMatrix::getProjectionMatrix() const |
| 106 |
{ |
| 107 |
// Return the same matrix as passed to the constructor |
| 108 |
Matrix4D mat(_clMtx); |
| 109 |
if (isOrthographic) { |
| 110 |
mat.move(-0.5, -0.5, -0.5); |
| 111 |
mat.scale(2.0, 2.0, 2.0); |
| 112 |
} |
| 113 |
|
| 114 |
return mat; |
| 115 |
} |
| 116 |
|
| 117 |
template<typename Vec> |
| 118 |
void perspectiveTransform(const Base::Matrix4D& mat, Vec& pnt) |
| 119 |
{ |
| 120 |
double m30 = mat[3][0]; |
| 121 |
double m31 = mat[3][1]; |
| 122 |
double m32 = mat[3][2]; |
| 123 |
double m33 = mat[3][3]; |
| 124 |
double ww |
| 125 |
= (static_cast<double>(pnt.x) * m30 + static_cast<double>(pnt.y) * m31 |
| 126 |
+ static_cast<double>(pnt.z) * m32 + m33); |
| 127 |
|
| 128 |
mat.multVec(pnt, pnt); |
| 129 |
pnt /= static_cast<typename Vec::num_type>(ww); |
| 130 |
} |
| 131 |
|
| 132 |
Vector3f ViewProjMatrix::operator()(const Vector3f& inp) const |
| 133 |
{ |
| 134 |
Vector3f src; |
| 135 |
transformInput(inp, src); |
| 136 |
|
| 137 |
Vector3f dst; |
| 138 |
if (!isOrthographic) { |
| 139 |
dst = src; |
| 140 |
perspectiveTransform<Vector3f>(_clMtx, dst); |
| 141 |
dst.Set(0.5F * dst.x + 0.5F, 0.5F * dst.y + 0.5F, 0.5F * dst.z + 0.5F); |
| 142 |
} |
| 143 |
else { |
| 144 |
_clMtx.multVec(src, dst); |
| 145 |
} |
| 146 |
|
| 147 |
return dst; |
| 148 |
} |
| 149 |
|
| 150 |
Vector3d ViewProjMatrix::operator()(const Vector3d& inp) const |
| 151 |
{ |
| 152 |
Vector3d src; |
| 153 |
transformInput(inp, src); |
| 154 |
|
| 155 |
Vector3d dst; |
| 156 |
if (!isOrthographic) { |
| 157 |
dst = src; |
| 158 |
perspectiveTransform<Vector3d>(_clMtx, dst); |
| 159 |
dst.Set(0.5 * dst.x + 0.5, 0.5 * dst.y + 0.5, 0.5 * dst.z + 0.5); |
| 160 |
} |
| 161 |
else { |
| 162 |
_clMtx.multVec(src, dst); |
| 163 |
} |
| 164 |
|
| 165 |
return dst; |
| 166 |
} |
| 167 |
|
| 168 |
Vector3f ViewProjMatrix::inverse(const Vector3f& src) const |
| 169 |
{ |
| 170 |
Vector3f dst; |
| 171 |
if (!isOrthographic) { |
| 172 |
dst.Set(2.0F * src.x - 1.0F, 2.0F * src.y - 1.0F, 2.0F * src.z - 1.0F); |
| 173 |
perspectiveTransform<Vector3f>(_clMtxInv, dst); |
| 174 |
} |
| 175 |
else { |
| 176 |
_clMtxInv.multVec(src, dst); |
| 177 |
} |
| 178 |
|
| 179 |
return dst; |
| 180 |
} |
| 181 |
|
| 182 |
Vector3d ViewProjMatrix::inverse(const Vector3d& src) const |
| 183 |
{ |
| 184 |
Vector3d dst; |
| 185 |
if (!isOrthographic) { |
| 186 |
dst.Set(2.0 * src.x - 1.0, 2.0 * src.y - 1.0, 2.0 * src.z - 1.0); |
| 187 |
perspectiveTransform<Vector3d>(_clMtxInv, dst); |
| 188 |
} |
| 189 |
else { |
| 190 |
_clMtxInv.multVec(src, dst); |
| 191 |
} |
| 192 |
|
| 193 |
return dst; |
| 194 |
} |
| 195 |
|
| 196 |
// ---------------------------------------------------------------------------- |
| 197 |
|
| 198 |
ViewOrthoProjMatrix::ViewOrthoProjMatrix(const Matrix4D& rclMtx) |
| 199 |
: _clMtx(rclMtx) |
| 200 |
, _clMtxInv(rclMtx) |
| 201 |
{ |
| 202 |
_clMtxInv.inverse(); |
| 203 |
} |
| 204 |
|
| 205 |
Matrix4D ViewOrthoProjMatrix::getProjectionMatrix() const |
| 206 |
{ |
| 207 |
return _clMtx; |
| 208 |
} |
| 209 |
|
| 210 |
Vector3f ViewOrthoProjMatrix::operator()(const Vector3f& rclPt) const |
| 211 |
{ |
| 212 |
return Vector3f(_clMtx * rclPt); |
| 213 |
} |
| 214 |
|
| 215 |
Vector3d ViewOrthoProjMatrix::operator()(const Vector3d& rclPt) const |
| 216 |
{ |
| 217 |
return Vector3d(_clMtx * rclPt); |
| 218 |
} |
| 219 |
|
| 220 |
Vector3f ViewOrthoProjMatrix::inverse(const Vector3f& rclPt) const |
| 221 |
{ |
| 222 |
return Vector3f(_clMtxInv * rclPt); |
| 223 |
} |
| 224 |
|
| 225 |
Vector3d ViewOrthoProjMatrix::inverse(const Vector3d& rclPt) const |
| 226 |
{ |
| 227 |
return Vector3d(_clMtxInv * rclPt); |
| 228 |
} |