# DLineate

## 2D Geometric Constraint Solver

I first started using SolveSpace for my CAD Software , but I soon found that it did not successfully solve for all of the situations that I desired it to. I looked into alternatives, but it seems there aren’t many open source constraint solvers (or at least not when I looked in early 2020).

After a lot of research, I found in-depth descriptions for two approaches to constraint solvers:

1. Using a non-linear algebraic solver to solve a system of equations generated from the provided constraints.
2. A graph based approach with research papers and a book published by professors at Purdue.

If the above link to the Purdue publication is not accessible, try the internet archive version

The graph based approach appealed to me because it simplifies the problem set by dividing it into smaller clusters and then solving the constraints in the clusters in groups of three.

## The Basic Algorithm

When elements are added to the system, the solver will break these down into simpler pieces – just points and lines. With just points and lines to work with, constraints can also be simplified to a combination of distance and angle constraints.

With the smaller problem set, there are fewer combinations of equations that need to be solved for. It is feasible to write specific solutions to those smaller systems of equations. From this point in the approach, the actual constraint solving is trivial. The complexity becomes finding clusters of these elements and constraints that can be solved easily.

Elements are represented as nodes and constraints as edges to generate a graph of the sketch to solve. The algorithm then starts creating clusters. Each cluster is bootstrapped with two elements connected by one constraint. The cluster is expanded by adding to it any other element that has two constraints connecting it to any two other elements in the cluster. When no other elements can be added to the cluster, it is considered complete. If there are any elements remaining that are not part of a cluster, the algorithm continues by starting a new cluster.

Each cluster can easily be solved by starting with the first constraint and two elements added to the cluster. The graph is walked in the order that the elements were added to the cluster. Each element encountered is solved using constraints in the cluster connecting the element to other solved elements.

Once all clusters are solved, the clusters are merged. This is easily done by rotating and translating clusters as a whole to match elements that are shared between the clusters. When all clusters are merged, all of the constraints are solved.

## Examples

### Square

A simple example is defining and solving for a square. Setting up the sketch looks like this:

``````	sketch := dlineate.NewSketch()
// Add elements -- include some error to see the solver work
l1 := sketch.AddLine(0.1, -0.2, 1.1, 0.1)
l2 := sketch.AddLine(1.01, 0.2, 1.1, 0.9)
l3 := sketch.AddLine(1.1, 1.2, 0.1, 1.1)
l4 := sketch.AddLine(-0.1, 1.2, 0.1, 0.1)

// Solve
err := sketch.Solve()
``````

The resulting graph and clusters look like this:

The final solution looks like this:

### Pentagon

To extend on the square example, we can define a pentagon.

``````	sketch := dlineate.NewSketch()

// Add elements -- points are reasonably close as if drawn by a human
l1 := sketch.AddLine(0.0, 0.0, 3.13, 0.0)
l2 := sketch.AddLine(3.13, 0.0, 5.14, 2.27)
l3 := sketch.AddLine(5.14, 2.27, 2.28, 4.72)
l4 := sketch.AddLine(2.28, 4.72, -1.04, 3.56)
l5 := sketch.AddLine(-1.04, 3.56, 0.0, 0.0)

// Bottom of pentagon starts at origin and aligns with x axis

// line points are coincident

// 108 degrees between lines (skip 2 to not over constrain)

// 4 unit length on lines (skip 1 to not over constrain)

// Solve
err := sketch.Solve()
``````

The resulting graph and clusters look like this:

The final solution looks like this (gray lines represent the X and Y axes):

### A More Realistic Example

To solve for something more realistic, I took a look at the engineering drawings for the Curta Type II. In one of the drawings, there is a feature that is basically a cylindrical hole with a bottleneck added to it. This sketch represents the profile of the cut for that feature.

``````	sketch := dlineate.NewSketch()

line1 := sketch.AddLine(6, 0, 9.4, 0)
line2 := sketch.AddLine(9.4, 0, 9.4, -1)
line3 := sketch.AddLine(9.4, -1, 8.1, -2)
arc1 := sketch.AddArc(8.5, -2.4, 8.1, -2, 8.1, -2.7)
line4 := sketch.AddLine(8.1, -2.7, 9.4, -3.8)
line5 := sketch.AddLine(9.4, -3.8, 9.4, -8)
line6 := sketch.AddLine(9.4, -8, 6, -8)
line7 := sketch.AddLine(6, -8, 6, 0)
arc2 := sketch.AddArc(6, 0, 6, 3, 8.1, -2.1)
arc3 := sketch.AddArc(6, -5, 6, -8.3, 8, -2.5)

// Constrain offset line to position the rest

// line points are coincident

// line1 constraints

// line2 constraints

// line3 constraints

// arc1 constraints

// line5 constraints

// line6 constraints

// line7 constraints

// arc2 constraints

// arc3 constraints