Simplify sparsity structure

This commit is contained in:
Irlan 2019-05-10 10:51:05 -03:00
parent 389a45bf9e
commit 8d0295f5b7
2 changed files with 14 additions and 89 deletions

View File

@ -28,8 +28,6 @@ struct b3RowValue
{ {
u32 column; u32 column;
b3Mat33 value; b3Mat33 value;
b3RowValue* prev;
b3RowValue* next; b3RowValue* next;
}; };
@ -46,40 +44,15 @@ struct b3RowValueList
void PushFront(b3RowValue* link) void PushFront(b3RowValue* link)
{ {
link->prev = NULL;
link->next = head; link->next = head;
if (head)
{
head->prev = link;
}
head = link; head = link;
++count; ++count;
} }
void PushAfter(b3RowValue* prev, b3RowValue* link)
{
link->prev = prev;
if (prev->next == NULL)
{
link->next = NULL;
}
else
{
link->next = prev->next;
prev->next->prev = link;
}
prev->next = link;
++count;
}
b3RowValue* head; b3RowValue* head;
u32 count; u32 count;
}; };
// A sparse symmetric matrix. // A sparse symmetric matrix.
// Each row is a list of non-zero elements in the row. // Each row is a list of non-zero elements in the row.
// The total matrix capacity is bounded by // The total matrix capacity is bounded by
@ -225,24 +198,15 @@ inline const b3Mat33& b3SparseSymMat33::operator()(u32 i, u32 j) const
B3_ASSERT(i < rowCount); B3_ASSERT(i < rowCount);
B3_ASSERT(j < rowCount); B3_ASSERT(j < rowCount);
// Ensure i, and j is on the upper triangle
if (i > j) if (i > j)
{ {
b3Swap(i, j); b3Swap(i, j);
} }
b3RowValueList* vs = rows + i; b3RowValueList* vs = rows + i;
for (b3RowValue* v = vs->head; v; v = v->next) for (b3RowValue* v = vs->head; v; v = v->next)
{ {
u32 column = v->column; if (v->column == j)
if (column < j)
{
break;
}
if (column == j)
{ {
return v->value; return v->value;
} }
@ -256,56 +220,27 @@ inline b3Mat33& b3SparseSymMat33::operator()(u32 i, u32 j)
B3_ASSERT(i < rowCount); B3_ASSERT(i < rowCount);
B3_ASSERT(j < rowCount); B3_ASSERT(j < rowCount);
// Ensure i, and j is on the upper triangle
if (i > j) if (i > j)
{ {
b3Swap(i, j); b3Swap(i, j);
} }
b3RowValueList* vs = rows + i; b3RowValueList* vs = rows + i;
for (b3RowValue* v = vs->head; v; v = v->next) for (b3RowValue* v = vs->head; v; v = v->next)
{ {
u32 column = v->column; if (v->column == j)
if (column < j)
{
break;
}
if (column == j)
{ {
return v->value; return v->value;
} }
} }
b3RowValue* v1 = (b3RowValue*)b3Alloc(sizeof(b3RowValue)); b3RowValue* v = (b3RowValue*)b3Alloc(sizeof(b3RowValue));
v1->column = j; v->column = j;
v1->value.SetZero(); v->value.SetZero();
b3RowValue* v0 = nullptr; vs->PushFront(v);
for (b3RowValue* v = vs->head; v; v = v->next) return v->value;
{
u32 column = v->column;
if (column > j)
{
v0 = v;
break;
}
}
if (v0 == nullptr)
{
vs->PushFront(v1);
}
else
{
vs->PushAfter(v0, v1);
}
return v1->value;
} }
inline void b3SparseSymMat33::operator+=(const b3SparseSymMat33& m) inline void b3SparseSymMat33::operator+=(const b3SparseSymMat33& m)
@ -389,16 +324,14 @@ inline void b3Mul(b3SparseSymMat33& out, float32 s, const b3SparseSymMat33& B)
return; return;
} }
for (u32 i = 0; i < B.rowCount; ++i) out = B;
{
b3RowValueList* vs = B.rows + i;
for (b3RowValue* vB = vs->head; vB; vB = vB->next) for (u32 i = 0; i < out.rowCount; ++i)
{ {
u32 j = vB->column; b3RowValueList* vs = out.rows + i;
b3Mat33 b = vB->value; for (b3RowValue* v = vs->head; v; v = v->next)
{
out(i, j) = s * b; v->value = s * v->value;
} }
} }
} }

View File

@ -99,15 +99,7 @@ inline const b3Mat33& b3SparseSymMat33View::operator()(u32 i, u32 j) const
for (u32 c = 0; c < vs->count; ++c) for (u32 c = 0; c < vs->count; ++c)
{ {
b3ArrayRowValue* rv = vs->values + c; b3ArrayRowValue* rv = vs->values + c;
if (rv->column == j)
u32 column = rv->column;
if (column < j)
{
break;
}
if (column == j)
{ {
return rv->value; return rv->value;
} }