kiwi.js icon indicating copy to clipboard operation
kiwi.js copied to clipboard

Problem using the kiwi for 3d geometric constraints

Open ghost opened this issue 4 years ago • 2 comments

Does someone know how to implement constraints on 2d or 3d geometries.

I have a problem on how to implement a distance constraint between two 3d vectors.

Imagine 2 points in the space

D = 10
P1 = [ 0, 0, 0 ]
P2 = [ 5, 0, 0 ]

How I can add a distance constraint between these two points.

Thanks

My code so far


// Create P1
const p1 = new Point3D(0, 0, 0);

// Create edit variables
const x = new kiwi.Variable('x');
const y = new kiwi.Variable('y');
const z = new kiwi.Variable('z');

this.kiwiSolver.addEditVariable(x, kiwi.Strength.strong);
this.kiwiSolver.addEditVariable(y, kiwi.Strength.strong);
this.kiwiSolver.addEditVariable(z, kiwi.Strength.strong);
this.kiwiSolver.suggestValue(x, v.x);
this.kiwiSolver.suggestValue(y, v.y);
this.kiwiSolver.suggestValue(z, v.z);
this.kiwiSolver.updateVariables();
p1.userData['kiwi'] = {x, y, z}

// Create P2
const p2 = new Point3D(100, 0, 0);

// Create edit variables
const x = new kiwi.Variable('x');
const y = new kiwi.Variable('y');
const z = new kiwi.Variable('z');

this.kiwiSolver.addEditVariable(x, kiwi.Strength.strong);
this.kiwiSolver.addEditVariable(y, kiwi.Strength.strong);
this.kiwiSolver.addEditVariable(z, kiwi.Strength.strong);
this.kiwiSolver.suggestValue(x, v.x);
this.kiwiSolver.suggestValue(y, v.y);
this.kiwiSolver.suggestValue(z, v.z);
this.kiwiSolver.updateVariables();
p2.userData['kiwi'] = {x, y, z}


const d = Math.sqrt(
  Math.pow((p1.kiwi.x.value() - p2.kiwi.x.value()), 2) +
  Math.pow((p1.kiwi.y.value() - p2.kiwi.y.value()), 2) +
  Math.pow((p1.kiwi.z.value() - p2.kiwi.z.value()), 2)
);
console.log({d}); // 100


// Here is the problem
const kiwi_distance_constraint = new kiwi.Constraint(
  Math.pow((p1.kiwi.x - p1.kiwi.x), 2) +
  Math.pow((p1.kiwi.y - p1.kiwi.y), 2) +
  Math.pow((p1.kiwi.z - p1.kiwi.z), 2), 
  kiwi.Operator.Eq
);
this.kiwiSolver.addConstraint(kiwi_distance_constraint);
this.kiwiSolver.updateVariables();

ghost avatar Mar 09 '20 19:03 ghost

Kiwi is linear constraint solver, so it can only handle constraints that are linear equalities or inequalities. Unfortunately, squaring a variable is not linear, so Kiwi can't handle this. You could try using a more expressive constraint solver or changing your constraint definitions so they're linear.

joshpoll avatar Oct 19 '20 15:10 joshpoll

So in geometrical constraints (just in 2D) this means..?

We cannot use kiwi.js. The Cassowary GitHub project's README gives the scope better - these are layout engines for traditional UIs.


I am looking for a CAD-like (2D) constraints engine that would work in a web app (tangents, what-not). Anyone knowing one, or also needing one, please chime in. solver-shootout

@George35mk May I suggest changing the title to "Kiwi.js ... cannot be used in geometric constraints" (or something) since it's a design decision, not really a problem. Also, I nearly missed this because was looking for 2D geometric constraints - the title actually gave me hope since it seemed to imply 2D works.

akauppi avatar Jul 05 '21 13:07 akauppi