Tuesday, May 8, 2012

Changes to the Solver

As I've mentioned in the previous post, the Solver used global objects to keep the references to the shapes that it was using in the solver method, updating them on the fly and sometimes leaving there NaN values.

I've modified the solver to use only the data that was passed in the parameters, fixing two issues this way: the points don't get to have NaN values for the coordinates and subsequent calls to Solve don't influence each other.

The new parameters list contains double values, which are the coordinates of the points and the constraints list contains the indexes in the parameters list, instead of actual values. Let's say we have two parallel lines, with the node indexes as shown:


The paramaters list will contain the coordinates for nodes 1,2,4 and 5:
parameters content: [ 3.0   6.0   1.0   1.0   8.0   5.0   5.0   0.0 ]
                    [ P1.x  P1.y  P2.x  P2.y  P4.x  P4.y  P5.x  P5.y]
indexes:            [  0     1     2     3     4     5     6     7  ]
The indexes will be used in the constraints list. To define that the line P1P2 is parallel to P4P5 we add a new constraint wich has two lines, defined by the point indexes:

L1 = new Line { P1 = new Point(0,1), P2 = new Point(2,3) }
L2 = new Line { P1 = new Point(4,5), P2 = new Point(6,7) }

When the constraint is calculated, the parameters list is passed - this makes it easier to debug and test the constraints code.

Using this structure, whenever the point position changes, the parameters value is changed, but the constraint stays the same and will always use the updated version of the coordinate.

If a solutions is found, the coordinates are automatically updated by updating the node, using an additional mapping between the parameters indexes and the shape index.

This new implementation replaces the old populate methods which were called for each free points number with one loading of the parameters and constraint build per action and doesn't require unmaping.

I'm testing this for parallel and perpendicular constraints on lines and updating the unit tests. As soon as I'm finished I'll commit the changes.

No comments: