Skip to content

Commit 3c0447f

Browse files
authored
[Type] getsub for MatSym (#5800)
* [Type] getsub for MatSym * add unit tests
1 parent 3f2478e commit 3c0447f

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

Sofa/framework/Type/src/sofa/type/MatSym.h

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ class MatSym : public VecNoInit<D * (D + 1) / 2, real>
144144
}
145145
}
146146

147+
template<Size D2>
148+
constexpr void getsub(Size a, MatSym<D2, real>& m) const requires (D2 <= D)
149+
{
150+
assert(a + D2 <= D);
151+
for (sofa::Size i = 0; i < D2; i++)
152+
{
153+
for (sofa::Size j = i; j < D2; j++)
154+
{
155+
m(i, j) = this->operator()(i + a, j + a);
156+
}
157+
}
158+
}
159+
160+
template<Size L2, Size C2>
161+
constexpr void getsub(Size L0, Size C0, Mat<L2, C2, real>& m) const requires (L2 <= D && C2 <= D)
162+
{
163+
assert(L0 + L2 <= D && C0 + C2 <= D);
164+
for (Size i = 0; i < L2; i++)
165+
{
166+
for (Size j = 0; j < C2; j++)
167+
{
168+
m(i, j) = this->operator()(i + L0, j + C0);
169+
}
170+
}
171+
}
172+
147173
/// convert to Voigt notation (supported only for D == 2 and D == 3)
148174
template<sofa::Size TD = D, typename = std::enable_if_t<TD == 3 || TD == 2> >
149175
inline Vec<NumberStoredValues, real> getVoigt() const
@@ -584,14 +610,15 @@ bool invertMatrix(MatSym<2,real>& dest, const MatSym<2,real>& from)
584610
template<sofa::Size D,class real>
585611
std::ostream& operator<<(std::ostream& o, const MatSym<D,real>& m)
586612
{
587-
o << '[' ;
613+
o << '[';
588614
for(sofa::Size i=0; i<D; i++)
589615
{
590616
for(sofa::Size j=0; j<D; j++)
591617
{
592-
o<<" "<<m(i,j);
618+
o << " " << m(i, j);
593619
}
594-
o<<" ,";
620+
if (i != D-1)
621+
o<<" ,";
595622
}
596623
o << ']';
597624
return o;

Sofa/framework/Type/test/MatSym_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,46 @@ TYPED_TEST(MatSym3x3Test, elementAccessor)
200200
ASSERT_NO_THROW (this->elementAccessor());
201201
}
202202

203+
204+
205+
TEST(MatSym_Test, getsub_3x3_2x2)
206+
{
207+
// [ 1 2 4 ]
208+
// [ 2 3 5 ]
209+
// [ 4 5 6 ]
210+
constexpr sofa::type::MatSym<3, double> mat33 {1.,2.,3.,4.,5.,6.};
211+
212+
{
213+
sofa::type::Mat<2, 2, double> mat22;
214+
mat33.getsub(0, 0, mat22);
215+
216+
EXPECT_DOUBLE_EQ(mat22(0, 0), 1_sreal);
217+
EXPECT_DOUBLE_EQ(mat22(0, 1), 2_sreal);
218+
EXPECT_DOUBLE_EQ(mat22(1, 0), 2_sreal);
219+
EXPECT_DOUBLE_EQ(mat22(1, 1), 3_sreal);
220+
}
221+
{
222+
223+
sofa::type::Mat<2, 2, double> mat22;
224+
mat33.getsub(1, 1, mat22);
225+
226+
EXPECT_DOUBLE_EQ(mat22(0, 0), 3_sreal);
227+
EXPECT_DOUBLE_EQ(mat22(0, 1), 5_sreal);
228+
EXPECT_DOUBLE_EQ(mat22(1, 0), 5_sreal);
229+
EXPECT_DOUBLE_EQ(mat22(1, 1), 6_sreal);
230+
}
231+
{
232+
233+
sofa::type::MatSym<2, double> mat22;
234+
mat33.getsub(1, mat22);
235+
236+
EXPECT_DOUBLE_EQ(mat22(0, 0), 3_sreal);
237+
EXPECT_DOUBLE_EQ(mat22(0, 1), 5_sreal);
238+
EXPECT_DOUBLE_EQ(mat22(1, 0), 5_sreal);
239+
EXPECT_DOUBLE_EQ(mat22(1, 1), 6_sreal);
240+
}
241+
}
242+
243+
244+
203245
}

0 commit comments

Comments
 (0)