Saturday, August 14, 2010

Nice syntax highlighting in Blogger

There is an easy way to make source code snippets look great on your blog. You have to change your template once and after that you can just surround your code with a simple tag. One thing you must not forget to do is to "escape" your source code so it only contains valid html code. Manually this means replacing any less than sign with appropriate html code.

Everything is nicely explained here:

syntax highlighting tutorial

and here is a direct link to a site to escape your html

quick escape

Sunday, August 8, 2010

Reading a vtk file with multiple scalar fields

Sometimes I come across data files in .vtk format. One of these files stores multiple scalar values in a single file, where the scalar are stored as structured points. For example I received a file containing quantA, quantB and quantC. I would like to read in these files and convert them to something else. So the main goal was to obtain arrays with the 3 scalar fields. The code is not yet complete but the reading is quite involved and it took me a lot of time to patch the required information together from all different examples and the DOxygen online manual of VTK (which I have come to dislike to a great extend). Perhaps the VTK books would have helped me out here but I am not willing to buy them just for this single application of VTK.

Okay here we go:
//First you have to create a reader object:
vtkSmartPointer < vtkstructuredpointsreader> reader = vtkSmartPointer < vtkstructuredpointsreader>::New();
//Now we can get the number of scalars in the file
vtkIdType numScal=reader->GetNumberOfScalarsInFile();
std::cout << "number of scalars in file: " << numScal << std::endl;
//Show which scalars we have
int i;
for(i=0;i< numScalComp;i++){
  std::cout << "Scalar "<< i <<": " << reader->GetScalarsNameInFile(i)<< std::endl;
//To read a certain scalar field we have to tell the reader which one we want
//we do that by name in this example we take the third one
reader->Update();  //I think this actually makes the reader do something
//the rest is what vtk people call setting up the pipeline
//To get to the point data we need a few intermediate steps and objects
vtkStructuredPoints* structuredPoints = reader->GetOutput();
vtkPointData *pd=structuredPoints->GetPointData();
vtkDataArray *scalars ;
//here the same name as selected in the reader must be used!!
//I tried this line with the SetScalarsName call to the reader and
//that doesn't work.
//Now we can access the points for example as
vtkIdType numPoints = structuredPoints->GetNumberOfPoints();
for(i=0;i< numPoints;i++){
   std::cout<< i<< ": " << scalars->GetComponent(i,0)<< std::endl;
The next step is to write a .vtk file from my own data. The mayavi2 and ParaView programs look quite nice for imaging of 3d data.