Performance improvements :)
Autor: Francisco Fernandes | Publicado: 2012-08-04T19:21:00-03:00

Hi all! 

For the first time, I accomplished good performance improvement in a code after refactoring and commenting the code. This kind of practice really helps to clarify an implementation. 

My code was very "dirty", almost unreadable, so I rewrote all the comments, hoping that it could help anyone to understand what's going on in the scene. While rewriting, some major errors in the link cell algorithm was corrected. For instance, I had the grid cell operation retrieving the upper-right neighborhood of a cell, instead of the lower-right:&nbsp; this was driving me crazy.

Anyway, most of the particles retrivement algorithm was slow due to unnecessary I/O operations in the annotations. So, to make things faster, one class is working as a particles manager (KisSandPaintOp), where it takes care of the interaction between the image properties and the particle architeture (grids, neighborhoods and the particles themselves).
The KisSandPaintOp reads and writes all the particles from the annotation ans save them as the SandBrush add new particles.


There are some other things that is missing, but is easy to be done:

brush widget update: have to add the "mouse particle" 
settings, mostly the physical properties like mass, friction and 
dissipation. The grid customization had to be done again, since I 
removed while fixing the grid retrieve in the neighboorhood.


brush default settings: the brush is begins with all its values set to zero, so I need to initialize them in a default value


This was the good news of this week. Along with that, I made a list of the main flaws in the brush behaviour. I have some ideas how to fixing each one,
but, like most of the previous ideas, I'm taking sometime to study the best ones:


dynamic pouring fix: the mouse velocity and acceleration are taken as parameters to modify the added particles position. However, when the movement is in one direction only (X or Y) the grains form a line, yielding an undesirable result (see the picture below). I'm applying the mouse velocity and acceleration directly in the particle pixel position, and update them at each call of the paintAt() method of the SandBrush.



Note the lines drawn in the X/Y directions. The mouse movement in this type of situation has to act in a more "random" way.


sand depletion operation: the sand depletion (amount of sand for each brush stroke) is decreasing too fast. I need to find a more reasonable asymptotic function to do the depletion, with the brush radius as parameter. As bigger the radius is, more fast the sand should This is not a high priority feature, but it's nice to have a more real feeling of the painting.


particle positions update in the spread operation: I implemented the main step of the spread operation (the final&nbsp; step of the brush function). As the following video shows, the correct grid position is retrieved allong with its particles to do the spread operation. However, for a reason I can't identify, the positions are not updated: 
I'm not deleting the previous positions yet, just trying to paint the 
new ones, which does not happen. And to make things worse, the selected 
particles have their color modified to blue, something that I'm not 
doing.


Anyway, just a few more steps and the spread operation will be done. The main properties of the brush is in this spread operation so&nbsp; accomplish the desired behaviour depends on this step. Fortunately, most of the necessary functions for this operations are working properly so I'm with good expectations of the results. Let's see how it will go!

Till next time!