Friday, August 31, 2012

Refactoring and dependency fixes

As I was working on the solver improvements, I decided to move the solver methods to a new class that would contain only the solvers and the related methods, and this lead to some interesting changes.

First of all, we have several unrelated 'Solver'classes. We have the GeometricSolver and derived classes, which are used with the Hinter, and the actual Solver which is for now in the Sketch2DSolver class. I've split these two and the new Solvers will all be in the SolverModule class. The constraints will be in a separate module, the ConstraintsModule.

After making these changes there were several problems caused by circular dependencies, mainly because helper methods and utility classes were using business logic layer methods. To solve these issues I used a great tool, NDepend.

It allows all kinds of queries related to assemblies/methods/types that are using and being used by a certain assembly/method/type and creates all sorts of useful graphs.
Here's for example a graph of direct and indirect callers for the new ConstraintsModule:


The dependency errors are fixed now, so I'll go back to working on the Solver, but after I'm done with that I'll do some more modules refactoring and ensure that everything is in the correct class, remove unused code and references.

Wednesday, August 22, 2012

Updating the tangent to circle constraint

As mentioned in yesterday's post, I'm updating the constraints so that their calculation in the solver is faster and the partial derivatives are calculated instead of being approximated as they are now.

A line tangent to circle constraint is equivalent to a point to line constraint, where the point is the center of the circle and, following the demonstration here we have the following formula for the constraint function:



For the partial derivative calculation we have two cases: where the variable doesn't appear in the denominator, and when it does. For the first case, the result is obvious. For the second case, we need to use these three formulas:




After applying these, we obtain:


which can be further simplified by using the old dx and dy notations:



the formula becomes:


and the sign is given by the value of the module evaluation (if the value inside the absolute module is negative, the entire expression is multiplied by -1 because we've been differentiating an absolute value.

With some small modifications, the same formula will be used for the radius tangent constraint and after a structure update for the point to line constraint, it will be used there, as well.

Tuesday, August 21, 2012

Improving the Solver

I am working on improving the Solver and investigating ways to replace (at least in some cases) the current algorithm which is based on the BFGS method with an algorithm based on the Gauss-Newton algorithm which should be easier to compute.

I'm also updating the error functions to simplify the calculations and adding the calculated values for the first derivative for each function: each of the point's coordinate is a variable for the Jacobian matrix and for each method we'll have several expressions to evaluate depending on the coordinate. I'll explain this in more detail when the algorithm will be finalized.

I've also added two helper classes for vector and matrix calculations - these should make the algorithm a lot easier to understand and make debugging a lot easier.