Skip to content

Commit e36422b

Browse files
committed
Enhance VectorSelecter and MatrixComparison
1 parent 5c1661b commit e36422b

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/matrix/operator.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,25 @@ namespace dynamicgraph {
9595
{
9696
void operator()( const Tin& m,Vector& res ) const
9797
{
98-
assert( (imin<=imax) && (imax <= m.size()) );
99-
res.resize( imax-imin );
100-
for( int i=imin;i<imax;++i ) res(i-imin)=m(i);
98+
res.resize(size);
99+
Vector::Index r=0;
100+
for (std::size_t i = 0; i < idxs.size(); ++i) {
101+
const Vector::Index& R = idxs[i].first;
102+
const Vector::Index& nr = idxs[i].second;
103+
assert( (nr>=0) && (R+nr <= m.size()) );
104+
res.segment(r,nr) = m.segment(R,nr);
105+
r += nr;
106+
}
107+
assert (r == size);
101108
}
102109

103-
int imin,imax;
104-
void setBounds( const int & m,const int & M ) { imin = m; imax = M; }
110+
typedef std::pair <Vector::Index,Vector::Index> segment_t;
111+
typedef std::vector <segment_t> segments_t;
112+
segments_t idxs;
113+
Vector::Index size;
114+
115+
void setBounds( const int & m,const int & M ) { idxs = segments_t (1, segment_t(m, M-m)); size = M-m; }
116+
void addBounds( const int & m,const int & M ) { idxs .push_back( segment_t(m, M-m)); size += M-m; }
105117

106118
void addSpecificCommands(Entity& ent,
107119
Entity::CommandMap_t& commandMap )
@@ -113,7 +125,12 @@ namespace dynamicgraph {
113125
= boost::bind( &VectorSelecter::setBounds,this,_1,_2 );
114126
doc = docCommandVoid2("Set the bound of the selection [m,M[.","int (min)","int (max)");
115127
ADD_COMMAND( "selec", makeCommandVoid2(ent,setBound,doc) );
128+
boost::function< void( const int&, const int& ) > addBound
129+
= boost::bind( &VectorSelecter::setBounds,this,_1,_2 );
130+
doc = docCommandVoid2("Add a segment to be selected [m,M[.","int (min)","int (max)");
131+
ADD_COMMAND( "addSelec", makeCommandVoid2(ent,setBound,doc) );
116132
}
133+
VectorSelecter () : size (0) {}
117134
};
118135
REGISTER_UNARY_OP( VectorSelecter,Selec_of_vector );
119136

@@ -829,14 +846,12 @@ namespace dynamicgraph {
829846
: public BinaryOpHeader <T1, T2, bool>
830847
{
831848
// TODO T1 or T2 could be a scalar type.
832-
typedef Eigen::Array<bool, T1::RowsAtCompileTime, T1::ColsAtCompileTime> Array;
833849
void operator()( const T1& a,const T2& b, bool& res ) const
834850
{
835-
Array r;
836-
if (equal) r = (a.array() <= b.array());
837-
else r = (a.array() < b.array());
838-
if (any) res = r.any();
839-
else res = r.all();
851+
if ( equal && any) res = (a.array() <= b.array()).any();
852+
else if ( equal && !any) res = (a.array() <= b.array()).all();
853+
else if (!equal && any) res = (a.array() < b.array()).any();
854+
else if (!equal && !any) res = (a.array() < b.array()).all();
840855
}
841856
virtual std::string getDocString () const
842857
{

0 commit comments

Comments
 (0)