e^t

我是风的歌

正在浏览 Programming 里的文章

工作是不是因为无聊?学习是不是因为无聊?介个不晓得里~~

话说卖萌可以买到妹纸吗?In urgent need ing~

有图有代码

从一个如下格式的文件中读取球的坐标和半径,然后画出来,使用了vtk库

n

x1 y1 z1 r1

x2 y2 z2 r2

xn yn zn rn

编译命令如下:

gcc example.cpp -I/usr/include/vtk-5.4 -L/usr/lib -lvtkCommon -lvtkViews -o example


#include <vtkSphereSource.h>
#include <vtkSmartPointer.h>
#include <vtkAppendPolyData.h>
#include <vtkCamera.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransform.h>
#include <vtkDepthSortPolyData.h>

vtkSmartPointer<vtkAppendPolyData> GenerateSpheres(int ntheta,int nphi)
{
    vtkSmartPointer<vtkAppendPolyData> appendData =
        vtkSmartPointer<vtkAppendPolyData>::New();

    ifstream sphereFile("spheres.3D");
    int nSphere;
    sphereFile >> nSphere;
    std::cout << "Number of spheres: " << nSphere << std::endl;
    double coordSphere[nSphere][4];
    for(int i = 0;i<nSphere;i++)
        for(int j = 0;j<4;j++)
            sphereFile >> coordSphere[i][j];

    for(int i = 0;i<nSphere;i++)
    {
        vtkSmartPointer<vtkSphereSource> sphereSource =
            vtkSmartPointer<vtkSphereSource>::New();
        sphereSource->SetThetaResolution(ntheta);
        sphereSource->SetPhiResolution(nphi);
        sphereSource->SetRadius(coordSphere[i][3]);

        sphereSource->SetCenter(coordSphere[i][0],
                coordSphere[i][1],
                coordSphere[i][2]);
        sphereSource->Update();
        appendData->AddInputConnection(sphereSource->GetOutputPort());
    }
    return appendData;
}

int main (int argc, char *argv[])
{
  int theta = 16;
  int phi = 16;

  // Generate a translucent sphere poly data set that partially overlaps:
  vtkSmartPointer<vtkAppendPolyData> translucentGeometry =
    GenerateSpheres(theta, phi);

  // generate a basic Mapper and Actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(translucentGeometry->GetOutputPort());

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->SetOpacity(1); //change to make translucent !!!
  actor->GetProperty()->SetColor(1, 0, 0);
  actor->RotateX(0); // put the objects in a position where it is easy to see
                       // different overlapping regions

  // Create the RenderWindow, Renderer and RenderWindowInteractor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the renderer, set the background and size
  renderer->AddActor(actor);
  renderer->SetBackground(1, 1, 1);
  renderWindow->SetSize(600, 400);

  // Setup view geometry
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(2.2); // so the object is larger

  int success = EXIT_SUCCESS;

  // Initialize interaction
  renderWindowInteractor->Initialize();

  // Start interaction
  renderWindowInteractor->Start();

  return success;
}

 

vtk的一些基础特性(http://apps.hi.baidu.com/share/detail/23071009):

VTk是在三维函数库OpenGL的基础上,采用面向对象的设计方法发展起来的。它有两种不同的方式:图像模型和可视化模型。

1、图像模型:支持3D几何数据绘制,3D体数据绘制,2D几何文字,图像绘制。

采用了3D图形系统简单易用的特点,同时也采用了图形用户接口的方法。整个图形模型表现3D图形系统的本质特点,主要有9类基本对象:渲染控制器,渲染窗口,渲染器,灯光,照相机,角色,特性,映射,变换。

1)渲染控制器定义与设备无关的坐标计算方法,并创建渲染窗口。

2)渲染窗口管理显示设备上的窗口,一个或多个绘制方法可在渲染窗口上创建一个场景,渲染窗口是用户图形界面,其中包括设置渲染窗口的大小,产生立体显示效果等方法。

3)渲染器是管理光源照相机和绘制对象等的位置、属性等,提供了世界坐标系,观察坐标系和现实坐标系之间的转换。

4)灯光可在场景中照亮绘制对象,可通过调用参数改变控制灯光的状态、照射角度,照射强度,颜色等,并支持点光源和平行光源。

5)照相机是定义观察者的位置,聚焦点和其他相关属性,参数可有调用者根据需要设置。

6)(相当于OpenGL的模型变换)角色代表渲染场景中的绘制对象实体,通过参数调节可以设置角色的位置,方向,渲染特性,引用,纹理映射等属性,并可对角色进行缩放。

7)属性是说明几何物体的一些特性,实现三维图形真实感。

8)映射指定了渲染数据和图形库中基本图元之间的关系,一个或多个角色可以使用相同的映射,有多个参数可对其进行控制。

9)变换是一个放置4×4变换矩阵的堆栈,可以进行各种操作。

2、可视化模型:利用数据流程模型,由过程对象和数据对象组成。过程对象包括可视化流程的模块及算法,数据对象包括数据处理及当数据在网络中流动时对数据进行的操作。过程对象又分为:源对象,过滤器,映射。源对象是可视化流程的起点,源对象包括从文件中读入及程序内部产生的数据,过滤器接收从源对象来的输入数据,处理数据及输出数据,映射指定了基本图元与数据之间的接口,接收过滤输出的数据,并把数据映射为基本图元。

 

References:

[1] http://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/CorrectlyRenderingTranslucentGeometry

[2] The official vtk documentation

没事儿熟悉一下C语言,林火模型其实物理学家用来研究SOC(self-organized criticality),也就是自组织临界性的玩意儿。说到其使用价值,把一个森林简化到这种地步,值得怀疑啊。下面用了狗血的周期边界条件(敢问编过科学计算程序的人,谁没有啃过面包圈?!),初始条件是全部是森林(100*100的网格)。图一表示几个闪电打下了之后,森林鸭梨很大,纷纷dead;图二是图一中的强大火势快没了,因为森林已经七零八落了;图三说明森林恢复力惊人,继续和大火做起了永不停息的斗争……

下面是C语言代码,直接gcc就行了。上面的图我用Mathematica画的。差点忘了,秀一下森林人口变化(刚开始是一万):

森林人口

森林人口

顺便表示对wordpress排版无力吐槽……


#include <stdio.h>

#include <stdlib.h>

#define p_fire 0.001

#define p_grow 0.02

#define ISFIRE 10

#define ISNULL 0

#define ISTREE 1

double ran1()

{

    return 1.0*rand()/RAND_MAX;

}

int main(int argc, const char *argv[])

{

    const int NX = 100;

    const int NY = 100;

    int forest[NX][NY];

    int forest_n[NX][NY];

    int i,j;

    int ide,idw,jdn,jds,sum;

    int n;

    FILE *pFile;

    pFile = fopen("forestfire.dat","w");

    // initialization

    for(i=0;i

        for(j=0;j

            forest[i][j]=1;

            //if(ran1() < 0.818) forest[i][j] = 0;

            //else forest[i][j] = 1;

    // save the initial forest

    for(i=0;i

        for(j=0;j

            fprintf(pFile,"%2d",forest[i][j]);

        fprintf(pFile,"\n");

    }

    for(n=1;n

    {

        for(i=0;i

        {

            for(j=0;j

            {

                ide = i + 1 > NX - 1 ? 0 : i + 1;

                idw = i - 1 < 0 ? NX - 1 : i - 1;

                jdn = j + 1 > NY - 1 ? 0 : j + 1;

                jds = j - 1 < 0 ? NY - 1 : j - 1;

                sum = forest[i][jdn] + forest[i][jds] + forest[ide][j] + forest[idw][j] +

                    forest[ide][jdn] + forest[ide][jds] + forest[idw][jdn] + forest[idw][jds];

                if(forest[i][j] == ISTREE)

                    if(sum>10) // neighbour is on fire!

                        forest_n[i][j] = ISFIRE;

                    else // no neighbour is on fire, lightning sets in however

                        forest_n[i][j] = (ran1()                else if(forest[i][j] == ISNULL) // empty cell has a chance to grow new tree

                        forest_n[i][j] = (ran1()                else // with fire, the tree is dead

                        forest_n[i][j] = ISNULL;

            }

        }

        // update

        for(i=0;i

            for(j=0;j

                forest[i][j]=forest_n[i][j];

        // store data

        for(i=0;i

            for(j=0;j

                fprintf(pFile,"%2d ",forest[i][j]);

            fprintf(pFile,"\n");

        }

    }

    fclose(pFile);

    return 0;

}

Learning C/C++

抢沙发

This post contains some useful links for c/c++ learning~~

1. Understanding the “extern” key word in C

http://www.geeksforgeeks.org/archives/840

(more to be added)

 

—-use profiler

[1]: g++ -o sortrand -pg main.cpp
[2]: ./sortrand
[3]: gprof sortrand.exe
—-显示库中的函数: ar t name.a

—-useful settings of output format in C++
cout.setf(ios_base::scientific,ios_base::floatfield);
cout.setf(ios_base::left,ios_base::adjustfield);
cout.width(0);恢复缺省设置

Recently I updated to ubuntu 11.10 – for no good reason. Now I think about it, the reason should be I forgot the golden maxima “don’t ‘fix’ it unless it is broken”.

After getting lost and tortured by the fancy interface, and found there was no way to roll back by package uninstallation. I reinstalled the system, and during various configurations later, BTW, I screwed up my Python installation and resintalled the os one more time. One thing especially lucky is I have my /home directory on a different hard disk sector – thank God I did that!

However, today after booting my ubuntu, I found I could no longer connect to the internet – although it was supposed to (I can connect from my Windows 7), many googling on my phone brought me the following solution.

sudo vi /etc/network/interfaces
# add the following two lines
auto eth0
iface eth0 inet dhcp
# quit vi and…
sudo /etc/init.d/networking restart

You can check the following reference for more explanation
http://lotphelp.com/lotp/ubuntu-network-suddenly-becomes-unreachable

And one more time, remember that:
don’t ‘fix’ it unless it is broken

我是新月,我要实现“七月流火”的相~

numerical_and_exact

absolute error

一个Poisson方程的简单测试,考虑到使用了30*30的网格,这个结果还是可以接受的。

函数库:明尼苏达大学一个牛教授的Sparsekit

初等几何

Oxyrhynchus papyrus (From Wikipedia)

任何喜欢数学的人都会对初等几何青眼有加,尤其是“初等的思想”解决高

等问题的感觉并不亚于一把薄薄的柳叶飞刀摧毁一个重甲机器人。可惜真

正理解刀、会用刀的人无比稀有——江湖中如此,学院中更如此!

顺便推荐一个站点:

http://www.its.caltech.edu/~mamikon/calculus.html

各种分割和等价。或者用我喜欢的词汇:乾坤大挪移和四两拨千斤……

Cleve Moler

Matlab

以前只是听说他是Fortran->Matlab的人,不过不知道他是Caltech和

Stanford的学生,更不知道他是SIAM的主席。对这个老爷子,相信无数

人是充满感激和敬意的,想象一下没有Matlab的世界吧~

Matlab官网上看到的视频:

http://www.mathworks.com/company/aboutus/founders/clevemoler.html

老爷子提到的几个有趣的事情:

  1. Matlab版本一是~2000行Fortran组成的,作者为此学习了parser的书写
  2. Matlab版本一有80个函数,现在有~8000个函数……,最初的图形是*号构成的,学 过C的应该很熟悉
  3. Matlab的logo是老爷子博士论文的一个解,L形状薄膜的第一个特征函数
  4. 1979老爷子上课让学生用Matlab,一半来自科学的学生很不爽,估计因为他们学不到算法,一半来自工程的学生很爽,因为他们要成为工程师而非科学家~
  5. Matlab最著名的函数是什么——不要一脸茫然哦,当然是/操作,这个原型也是老爷子写的。

很喜欢看到专业人士门槛的降低,这样有人会不高兴,但是更多的人会很高兴!

 

1. Cygwin Assembler Error

My situation: I’ve configure my gVim to compile all programming languages I commonly use by pressing one key (F3 for object file, F4 for executable for example). But recently (under Win 7) this handy shortcut stopped working, with the recurring and annoying error message:
Assembler messaes: FATAL can’t create ***, no such file or directory
As always, I quickly began to blame Windows 7 — “what’s the problem with you, I can’t make modifications of my own computer?”
It was Google, instead of the madness, gave me the answer.
According to my understanding: Cygwin is more like a smaller ecosystem residing inside Windows, rather than an installed software. Being a ported version of Linux, it certainly has a stronger notion of user privilege or authorization control. my gVim was configured to call gcc-4 for compilation, and gcc requires a location for storing temporary files, which it should have the right to modify. The directory I had used was C:/tmp, which is on the system disk and the path interpreted by Cygwin is /cygdrive/c/tmp — not quite ‘natural’, I can only say.
So the solution is:
export TEMP=/tmp
Again, something still confusing is my original configuration actually worked for quite a while, and broke down all of a sudden — I can only attribute this to my own Windows’ long existing unknown problem.

Change Matlab Directory
I often close my Matlab with several files opening, so they are automatically opened the next time I start Matlab. There is a small problem: the default directory Matlab opens is its installation directory, not the one containing the opened scripts.

Normally I would use cd command or the file explore, or just run it… Today a much faster trick came to me accidently. It is:

  1. Set a breakpoint in your editor – Matlab will ask you if you want to switch directory.
  2. Click yes.
  3. Unset the breakpoint.