// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // 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/. #include "unproject.h" #ifndef IGL_NO_OPENGL #ifndef IGL_OPENGL_4 #include #include #include "OpenGL_convenience.h" IGL_INLINE void igl::unproject( const double winX, const double winY, const double winZ, double* objX, double* objY, double* objZ) { Eigen::Vector3d obj; igl::unproject(Eigen::Vector3d(winX,winY,winZ),obj); *objX = obj(0); *objY = obj(1); *objZ = obj(2); } template IGL_INLINE void igl::unproject( const Eigen::PlainObjectBase & win, Eigen::PlainObjectBase & obj) { obj = igl::unproject(win).template cast(); } template IGL_INLINE Eigen::PlainObjectBase igl::unproject( const Eigen::PlainObjectBase & win) { using namespace Eigen; typedef typename Derivedwin::Scalar Scalar; Matrix4d MV,P; Vector4i VPi; Vector4d VPd; glGetDoublev(GL_MODELVIEW_MATRIX,MV.data()); glGetDoublev(GL_PROJECTION_MATRIX,P.data()); glGetIntegerv(GL_VIEWPORT,VPi.data()); VPd = VPi.cast(); Vector3d wind = win.template cast(); Vector3d objd = igl::unproject(wind,MV,P,VPd); return objd.template cast(); } #endif #endif template IGL_INLINE Eigen::Matrix igl::unproject( const Eigen::Matrix& win, const Eigen::Matrix& model, const Eigen::Matrix& proj, const Eigen::Matrix& viewport) { Eigen::Matrix Inverse = (proj * model).inverse(); Eigen::Matrix tmp; tmp << win, 1; tmp(0) = (tmp(0) - viewport(0)) / viewport(2); tmp(1) = (tmp(1) - viewport(1)) / viewport(3); tmp = tmp.array() * 2.0f - 1.0f; Eigen::Matrix obj = Inverse * tmp; obj /= obj(3); return obj.head(3); } #ifdef IGL_STATIC_LIBRARY #ifndef IGL_NO_OPENGL #ifndef IGL_OPENGL_4 // Explicit template instanciation template void igl::unproject, Eigen::Matrix >(Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); template void igl::unproject, Eigen::Matrix >(Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); template Eigen::PlainObjectBase > igl::unproject >(Eigen::PlainObjectBase > const&); template void igl::unproject, Eigen::Matrix >(Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); template Eigen::PlainObjectBase > igl::unproject >(Eigen::PlainObjectBase > const&); template void igl::unproject, Eigen::Matrix >(Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); template Eigen::PlainObjectBase > igl::unproject >(Eigen::PlainObjectBase > const&); template Eigen::Matrix igl::unproject(Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&); #endif template Eigen::Matrix igl::unproject(Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&); #endif #endif