123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- // This file is part of libigl, a simple c++ geometry processing library.
- //
- // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
- //
- // 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 "sort_angles.h"
- #include <algorithm>
- template <typename DerivedM, typename DerivedR>
- IGL_INLINE void igl::sort_angles(
- const Eigen::PlainObjectBase<DerivedM>& M,
- Eigen::PlainObjectBase<DerivedR>& R) {
- const size_t num_rows = M.rows();
- const size_t num_cols = M.cols();
- assert(num_cols >= 2);
- R.resize(num_rows);
- R.setLinSpaced(num_rows, 0, num_rows-1);
- // |
- // (pi/2, pi) | (0, pi/2)
- // |
- // -------------+--------------
- // |
- // (-pi, -pi/2) | (-pi/2, 0)
- // |
- auto comp = [&](size_t i, size_t j) {
- auto yi = M(i, 0);
- auto xi = M(i, 1);
- auto yj = M(j, 0);
- auto xj = M(j, 1);
- if (xi == xj && yi == yj) {
- for (size_t idx=2; idx<num_cols; idx++) {
- auto i_val = M(i, idx);
- auto j_val = M(j, idx);
- if (i_val != j_val) {
- return i_val < j_val;
- }
- }
- // If the entire rows are equal, use the row index.
- return i < j;
- }
- if (xi >= 0 && yi >= 0) {
- if (xj >=0 && yj >= 0) {
- if (xi != xj) {
- return xi > xj;
- } else {
- return yi < yj;
- }
- } else if (xj < 0 && yj >= 0) {
- return true;
- } else if (xj < 0 && yj < 0) {
- return false;
- } else {
- return false;
- }
- } else if (xi < 0 && yi >= 0) {
- if (xj >= 0 && yj >= 0) {
- return false;
- } else if (xj < 0 && yj >= 0) {
- if (xi != xj) {
- return xi > xj;
- } else {
- return yi > yj;
- }
- } else if (xj < 0 && yj < 0) {
- return false;
- } else {
- return false;
- }
- } else if (xi < 0 && yi < 0) {
- if (xj >= 0 && yj >= 0) {
- return true;
- } else if (xj < 0 && yj >= 0) {
- return true;
- } else if (xj < 0 && yj < 0) {
- if (xi != xj) {
- return xi < xj;
- } else {
- return yi > yj;
- }
- } else {
- return true;
- }
- } else {
- if (xj >= 0 && yj >= 0) {
- return true;
- } else if (xj < 0 && yj >= 0) {
- return true;
- } else if (xj < 0 && yj < 0) {
- return false;
- } else {
- if (xi != xj) {
- return xi < xj;
- } else {
- return yi < yj;
- }
- }
- }
- };
- std::sort(R.data(), R.data() + num_rows, comp);
- }
- #ifdef IGL_STATIC_LIBRARY
- template void igl::sort_angles<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
- #endif
|