IGL_INLINE void igl::barycentric_coordinates( const Eigen::MatrixBase<DerivedP> & P, const Eigen::MatrixBase<DerivedA> & A, const Eigen::MatrixBase<DerivedB> & B, const Eigen::MatrixBase<DerivedC> & C, Eigen::PlainObjectBase<DerivedL> & L) { using namespace Eigen; #ifndef NDEBUG const int DIM = P.cols(); assert(A.cols() == DIM && "corners must be in same dimension as query"); assert(B.cols() == DIM && "corners must be in same dimension as query"); assert(C.cols() == DIM && "corners must be in same dimension as query"); assert(P.rows() == A.rows() && "Must have same number of queries as corners"); assert(A.rows() == B.rows() && "Corners must be same size"); assert(A.rows() == C.rows() && "Corners must be same size"); #endif // http://gamedev.stackexchange.com/a/23745 typedef Eigen::Array< typename DerivedP::Scalar, DerivedP::RowsAtCompileTime, DerivedP::ColsAtCompileTime> ArrayS; typedef Eigen::Array< typename DerivedP::Scalar, DerivedP::RowsAtCompileTime, 1> VectorS; const ArrayS v0 = B.array() - A.array(); const ArrayS v1 = C.array() - A.array(); const ArrayS v2 = P.array() - A.array(); VectorS d00 = (v0*v0).rowwise().sum(); VectorS d01 = (v0*v1).rowwise().sum(); VectorS d11 = (v1*v1).rowwise().sum(); VectorS d20 = (v2*v0).rowwise().sum(); VectorS d21 = (v2*v1).rowwise().sum(); VectorS denom = d00 * d11 - d01 * d01; L.resize(P.rows(),3); L.col(1) = (d11 * d20 - d01 * d21) / denom; L.col(2) = (d00 * d21 - d01 * d20) / denom; L.col(0) = 1.0f -(L.col(1) + L.col(2)).array(); }
void ba81NormalQuad::layer::detectTwoTier(Eigen::ArrayBase<T1> ¶m, Eigen::MatrixBase<T2> &mean, Eigen::MatrixBase<T3> &cov) { if (mean.rows() < 3) return; std::vector<int> orthogonal; Eigen::Matrix<Eigen::DenseIndex, Eigen::Dynamic, 1> numCov((cov.array() != 0.0).matrix().colwise().count()); std::vector<int> candidate; for (int fx=0; fx < numCov.rows(); ++fx) { if (numCov(fx) == 1) candidate.push_back(fx); } if (candidate.size() > 1) { std::vector<bool> mask(numItems()); for (int cx=candidate.size() - 1; cx >= 0; --cx) { std::vector<bool> loading(numItems()); for (int ix=0; ix < numItems(); ++ix) { loading[ix] = param(candidate[cx], itemsMap[ix]) != 0; } std::vector<bool> overlap(loading.size()); std::transform(loading.begin(), loading.end(), mask.begin(), overlap.begin(), std::logical_and<bool>()); if (std::find(overlap.begin(), overlap.end(), true) == overlap.end()) { std::transform(loading.begin(), loading.end(), mask.begin(), mask.begin(), std::logical_or<bool>()); orthogonal.push_back(candidate[cx]); } } } std::reverse(orthogonal.begin(), orthogonal.end()); if (orthogonal.size() == 1) orthogonal.clear(); if (orthogonal.size() && orthogonal[0] != mean.rows() - int(orthogonal.size())) { mxThrow("Independent specific factors must be given after general dense factors"); } numSpecific = orthogonal.size(); if (numSpecific) { Sgroup.assign(numItems(), 0); for (int ix=0; ix < numItems(); ix++) { for (int dx=orthogonal[0]; dx < mean.rows(); ++dx) { if (param(dx, itemsMap[ix]) != 0) { Sgroup[ix] = dx - orthogonal[0]; continue; } } } //Eigen::Map< Eigen::ArrayXi > foo(Sgroup.data(), param.cols()); //mxPrintMat("sgroup", foo); } }
Eigen::Matrix<typename Derived::Scalar, 3, 3> rpy2rotmat(const Eigen::MatrixBase<Derived>& rpy) { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>, 3); auto rpy_array = rpy.array(); auto s = rpy_array.sin(); auto c = rpy_array.cos(); Eigen::Matrix<typename Derived::Scalar, 3, 3> R; R.row(0) << c(2) * c(1), c(2) * s(1) * s(0) - s(2) * c(0), c(2) * s(1) * c(0) + s(2) * s(0); R.row(1) << s(2) * c(1), s(2) * s(1) * s(0) + c(2) * c(0), s(2) * s(1) * c(0) - c(2) * s(0); R.row(2) << -s(1), c(1) * s(0), c(1) * c(0); return R; }
IGL_INLINE void igl::normalize_row_sums( const Eigen::MatrixBase<DerivedA>& A, Eigen::MatrixBase<DerivedB> & B) { #ifndef NDEBUG // loop over rows for(int i = 0; i < A.rows();i++) { typename DerivedB::Scalar sum = A.row(i).sum(); assert(sum != 0); } #endif B = (A.array().colwise() / A.rowwise().sum().array()).eval(); }
IGL_INLINE Scalar igl::random_search( const std::function< Scalar (DerivedX &) > f, const Eigen::MatrixBase<DerivedLB> & LB, const Eigen::MatrixBase<DerivedUB> & UB, const int iters, DerivedX & X) { Scalar min_f = std::numeric_limits<Scalar>::max(); const int dim = LB.size(); assert(UB.size() == dim && "UB should match LB size"); for(int iter = 0;iter<iters;iter++) { const DerivedX R = DerivedX::Random(dim).array()*0.5+0.5; DerivedX Xr = LB.array() + R.array()*(UB-LB).array(); const Scalar fr = f(Xr); if(fr<min_f) { min_f = fr; X = Xr; } } return min_f; }
inline bool isFucked(const Eigen::MatrixBase<Derived>& x) { return !((x.array() == x.array())).all() && !( (x - x).array() == (x - x).array()).all(); }
inline bool is_nan(const Eigen::MatrixBase<Derived>& x) { return ((x.array() == x.array())).all(); }
void operator()(Eigen::MatrixBase<Derived> &inout) const { inout = 1.0 / (1.0 + (-1.0 * inout.array()).exp()); }
void log_activation(const Eigen::MatrixBase<T>& inputs, Eigen::VectorXd& activation) { activation = inputs.array().log().matrix(); }