Skip to content

Commit 4b792d4

Browse files
committed
find indices methods moved to new file find.h
1 parent 4e40205 commit 4b792d4

File tree

5 files changed

+102
-69
lines changed

5 files changed

+102
-69
lines changed

src/adjustment.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "adjustment.h"
2020
#include "constants.h"
2121
#include "constraints.h"
22+
#include "find.h"
2223
#include "geometry/minrot.h"
2324
#include "global.h"
2425
#include "kernel.h"
@@ -61,7 +62,7 @@ using Geometry::Rot_ab;
6162

6263
using Matfun::null;
6364
using Matfun::is_rank_deficient;
64-
using Matfun::spfind;
65+
using Matfun::find;
6566

6667
using TextColor::black;
6768
using TextColor::blue;
@@ -284,7 +285,7 @@ void AdjustmentFramework::Jacobian(
284285
continue;
285286
}
286287

287-
VectorXidx idx = spfind( relsub.col(c).eval() );
288+
VectorXidx idx = find( relsub.col(c).eval() );
288289

289290
auto JJ = con->Jacobian( idx, l0_, l_ );
290291
const int dof = con->dof();
@@ -343,7 +344,7 @@ void AdjustmentFramework::checkConstraints(
343344
continue;
344345
}
345346

346-
const VectorXidx idx = spfind( relsub.col(c).eval() );
347+
const VectorXidx idx = find( relsub.col(c).eval() );
347348

348349
const double d = con->contradict( idx, l0_ ).norm();
349350
assert( enforced.size()==status.size() );

src/find.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
3+
* Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
#ifndef FIND_H
21+
#define FIND_H
22+
23+
#include <Eigen/Core>
24+
#include <Eigen/SparseCore>
25+
26+
27+
namespace Matfun {
28+
29+
using Eigen::ArrayXi;
30+
using Eigen::Dynamic;
31+
using Eigen::Index;
32+
using Eigen::Vector;
33+
using Eigen::SparseVector;
34+
35+
36+
//! Find indices of 1-elements in vector
37+
[[nodiscard, maybe_unused]] static ArrayXi
38+
find( const Vector<bool,Dynamic> & cond)
39+
{
40+
ArrayXi idx( cond.count() );
41+
for (int i=0, k=0; i<cond.size(); i++) {
42+
if ( cond(i) ) {
43+
idx(k++) = i;
44+
}
45+
}
46+
47+
return idx;
48+
}
49+
50+
51+
//! Find indices of nonzeros elements in sparse vector
52+
template <typename T>
53+
[[nodiscard]] static Vector<Index,Dynamic>
54+
find( const SparseVector<T> & v)
55+
{
56+
Vector<Index,Dynamic> idx( v.nonZeros() );
57+
58+
Index i=0;
59+
for ( typename SparseVector<T>::InnerIterator it(v); it; ++it) {
60+
idx(i++) = it.index();
61+
}
62+
63+
return idx;
64+
}
65+
66+
} // namespace Matfun
67+
68+
69+
// //! Matlab's find(x,1,'first')
70+
// template <typename T>
71+
// [[nodiscard]] static Index indexOf(const Eigen::Vector<T,Eigen::Dynamic> &v, const T x)
72+
// {
73+
// for ( Index i=0; i<v.size(); i++) {
74+
// if ( v(i)==x ) {
75+
// return i;
76+
// }
77+
// }
78+
// return -1;
79+
//
80+
// /* Eigen 3.4.0
81+
// auto it = std::find( v.begin(), v.end(), i);
82+
// if ( it==v.end() ) {
83+
// return -1;
84+
// }
85+
// return std::distance( v.begin(), it); */
86+
// }
87+
88+
89+
#endif // FIND_H

src/greasepad.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ HEADERS += \
5252
conncomp.h \
5353
constants.h \
5454
constraints.h \
55+
find.h \
5556
geometry/aabb.h \
5657
geometry/acute.h \
5758
geometry/conics.h \

src/matfun.h

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,8 @@
3131

3232
namespace Matfun {
3333

34-
using Eigen::ArrayXi;
35-
using Eigen::MatrixXd;
36-
using Eigen::VectorXd;
37-
using Eigen::SparseMatrix;
38-
using Eigen::SparseVector;
3934
using Eigen::Matrix3d;
40-
using Eigen::Index;
41-
using Eigen::Matrix;
42-
using Eigen::Vector;
43-
using Eigen::Dynamic;
35+
using Eigen::SparseMatrix;
4436

4537

4638
//! check if the matrix AA is rank-deficient
@@ -75,62 +67,12 @@ using Eigen::Dynamic;
7567
}
7668

7769

78-
// //! Matlab's find(x,1,'first')
79-
// template <typename T>
80-
// [[nodiscard]] static Index indexOf(const Eigen::Vector<T,Eigen::Dynamic> &v, const T x)
81-
// {
82-
// for ( Index i=0; i<v.size(); i++) {
83-
// if ( v(i)==x ) {
84-
// return i;
85-
// }
86-
// }
87-
// return -1;
88-
//
89-
// /* Eigen 3.4.0
90-
// auto it = std::find( v.begin(), v.end(), i);
91-
// if ( it==v.end() ) {
92-
// return -1;
93-
// }
94-
// return std::distance( v.begin(), it); */
95-
// }
96-
97-
9870
//! signum function with sign(0):=+1
9971
template <typename T>
10072
constexpr int sign(T val) noexcept {
10173
return (T(0) <= val) - (val < T(0));
10274
}
10375

104-
105-
//! Matlab's find
106-
[[nodiscard,maybe_unused]] static ArrayXi
107-
find( const Vector<bool,Dynamic> & cond)
108-
{
109-
ArrayXi idx( cond.count() );
110-
for (int i=0, k=0; i<cond.size(); i++) {
111-
if ( cond(i) ) {
112-
idx(k++) = i;
113-
}
114-
}
115-
116-
return idx;
117-
}
118-
119-
120-
//! find indices of sparse vectors
121-
template <typename T>
122-
[[nodiscard,maybe_unused]] static Vector<Index,Dynamic> spfind( const SparseVector<T> & v)
123-
{
124-
Vector<Index,Dynamic> idx( v.nonZeros() );
125-
126-
Index i=0;
127-
for ( typename SparseVector<T>::InnerIterator it(v); it; ++it) {
128-
idx(i++) = it.index();
129-
}
130-
131-
return idx;
132-
}
133-
13476
} // namespace Matfun
13577

13678
#endif // MATFUN_H

src/state.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "conncomp.h"
3636
#include "constants.h"
3737
#include "constraints.h"
38+
#include "find.h"
3839
#include "geometry/acute.h"
3940
#include "global.h"
4041
#include "mainscene.h"
@@ -98,7 +99,6 @@ using Graph::IncidenceMatrix;
9899
using Matfun::find;
99100
using Matfun::sign;
100101
using Matfun::unique;
101-
using Matfun::spfind;
102102

103103
using TextColor::black;
104104
using TextColor::blue;
@@ -690,7 +690,7 @@ void impl::lookOutForParallelism(const Index c)
690690
const SparseMatrix WW = (Adj*Adj).eval();
691691

692692
// case: straight line segment connects two segments
693-
const Vector<Index,Dynamic> idx = spfind( Adj.col(c).eval() ); // neighbors of c
693+
const Vector<Index,Dynamic> idx = find( Adj.col(c).eval() ); // neighbors of c
694694

695695
// Check all pairs of direct neighbors.
696696
for ( Index i=0; i<idx.rows(); i++) {
@@ -715,7 +715,7 @@ void impl::lookOutForParallelism(const Index c)
715715

716716

717717
// Find walks of length 2 between the vertices of the graph.
718-
const Vector<Index,Dynamic> nbs = spfind( WW.col(c).eval() );
718+
const Vector<Index,Dynamic> nbs = find( WW.col(c).eval() );
719719
for ( const auto a : nbs) { // Index n=0; n<nbs.rows()-1; n++) {
720720
// a is a walk of length 2 away from c.
721721
if ( !Adj.isSet(a,c) ) {
@@ -733,11 +733,11 @@ void impl::lookOutForCopunctuality( const Index c )
733733
{
734734
const SparseMatrix WW = (Adj*Adj).eval();
735735

736-
const Vector<Index,Dynamic> nbs = spfind( WW.col(c).eval() );
736+
const Vector<Index,Dynamic> nbs = find( WW.col(c).eval() );
737737
for ( const auto a : nbs) {
738738
// a and c are adjacent and have at least one common neighbor.
739739
// Find all common neighbors of a and c.
740-
const Vector<Index,Dynamic> nbnb = spfind( Adj.col(a).eval() ); // neighbors of a.
740+
const Vector<Index,Dynamic> nbnb = find( Adj.col(a).eval() ); // neighbors of a.
741741
for ( const auto b : nbnb ) {
742742
if ( !Adj.isSet( b,c ) ) {
743743
continue; // b is not a common neighbor of a and c.
@@ -1124,7 +1124,7 @@ void impl::remove_constraint( const Index i )
11241124

11251125
if ( m_constr.at(i)->isInstanceOf<Parallel>() )
11261126
{
1127-
const Vector<Index,Dynamic> idx = spfind( Rel.col(i).eval() );
1127+
const Vector<Index,Dynamic> idx = find( Rel.col(i).eval() );
11281128
Q_ASSERT_X( idx.size() == 2, Q_FUNC_INFO,
11291129
QStringLiteral("parallel with %1 entities")
11301130
.arg( QString::number(idx.size())).toUtf8() );
@@ -1332,7 +1332,7 @@ void impl::replaceGraphics() {
13321332
for (Index c=0; c<m_constr.length(); c++)
13331333
{
13341334
bool modified = false;
1335-
const Vector<Index,Dynamic> idx = spfind( Rel.col(c).eval() );
1335+
const Vector<Index,Dynamic> idx = find( Rel.col(c).eval() );
13361336

13371337
if ( m_constr.at(c).use_count()==1 ) {
13381338
modified = true; // actually not modified, but added

0 commit comments

Comments
 (0)