2008年5月23日星期五

GSL笔记20080523

最近的一个程序要用到一些矩阵和向量的操作,主程序是用c写的,自己实现一些矩阵的操作倒也不算困难,但是有些运算就不太好实现了,而且质量也难以保证。在google上搜索了一番,于是就发现了GSL(GNU Scientific Library),GNU的好东西还真不少。

很快的翻了一下gsl的手册,写的比较详细,再配合它的例子,一般的使用应该不成问题。
照葫芦画瓢,写了一个求逆矩阵的程序,当然都是数值运算的,好在这次的程序没有符号运算。

#include
#include

int
main (void)
{
double a_data[] = { 0.3, 0, 0, 0,
0, 0.3, 0, 0,
0, 0, 0.3, 0,
0, 0, 0, 0.3 };

double b_data[] = { 1.0, 2.0, 3.0, 4.0 };

gsl_matrix_view m
= gsl_matrix_view_array(a_data, 4, 4);

gsl_vector_view b
= gsl_vector_view_array(b_data, 4);

gsl_vector *x = gsl_vector_alloc (4);
gsl_matrix *mi= gsl_matrix_alloc (4,4);

int s;

gsl_permutation * p = gsl_permutation_alloc (4);

gsl_linalg_LU_decomp (&m.matrix, p, &s);

gsl_linalg_LU_invert (&m.matrix, p, mi);

printf ("inv=\n");
gsl_matrix_fprintf (stdout, mi, "%f");

gsl_permutation_free (p);
return 0;
}


permutation没有查到准确的意思,从手册和例子中的用法猜测,意思大概是行列式前面的符号。
gsl中的线性代数部分在操作矩阵的时候,都是把矩阵分解成某种乘积形式,然后再做运算,上面程序用的是LU分解把m分解成一个下三角阵和上三角阵的乘积,之后再求逆。

2008年5月10日星期六

ADT, queue.h

以前写C的代码的时候,所有的ADT都是自己去实现,那真是一个累啊,无奈自己能力有限,要花费大量的时间来调试ADT部分的代码。终于,某天,在某论坛,看到某高人的一句话…解放了,直接用bsd的queue.h就好了。

能用的还有linux的list.h,不过我在osx上测试没有成功,总是说指针类型不匹配,就算是在linux上也要修改一下,里面已经说了,"don't include kernel headers in userspace"。

2008年5月2日星期五

Duncan提供的几个nCloth的tips

Here are a few points on breakable nCloth:
-If you select verts, edges or faces when making a tearable constraint then it can only tear at those regions, which is more efficient.

-The detach component code internally detachs based on vertices, not edges, so the tearable surface constraint will give "ragged" edges. You can make it more efficient by carefully modelling your own detach edges. Basically initially create your model with all the edges you wish to be breakable being border edges. 

-For large rigid chunks you may wish to use separate objects, each with its own nCloth. Constrain the chunks together with a component to component constraint with glueStrength set and the weld method. Then combine, merge vertex, smooth normals and possibly extrude downstream of the nCloth nodes. This allows you to use rigidity on the nCloth nodes(otherwise you need to use high bend/stretch levels which is less efficient). You can also turn off self collision on the nCloth nodes, because they are internally rigid. If instead you were using one cloth node for all the chunks then you would need self collision so that the chunks could intercollide.

-There is bend stiffness on the constraint node, which can be used for glass shatter sorts of effects. However this requires that you have edge nComponents. If you use standard breakable surface setup with edge components it gets extremely slow to use the constraint bend stiffness for a complex surface. We are currently looking at fixing this problem, as it makes it hard to do rigid shatter effects.

Duncan