Skip to content

Instantly share code, notes, and snippets.

@derMart
Created October 19, 2018 11:41
Show Gist options
  • Save derMart/cc0690d01d6e34eed9df44ef6fc21e81 to your computer and use it in GitHub Desktop.
Save derMart/cc0690d01d6e34eed9df44ef6fc21e81 to your computer and use it in GitHub Desktop.
Thin Plate Spline Problem
Following Control Points JSON array:
[{"x1":260.21111988766125,"y1":310.4417412673348,"x2":298.33182733573295,"y2":291.0939741615935},{"x1":235.67262594347676,"y1":295.70781112866644,"x2":278.784541199405,"y2":281.6143898884996},{"x1":325.105415130756,"y1":281.73224504125875,"x2":348.87073227718565,"y2":265.7221456659335},{"x1":293.19995611724335,"y1":296.0328242935015,"x2":323.8909821749851,"y2":276.9965776970716},{"x1":272.9408021765662,"y1":280.81137440759977,"x2":307.42368964204695,"y2":266.1229369126426},{"x1":270.7740477444044,"y1":245.11409513781314,"x2":301.8474635988654,"y2":238.24180669837813},{"x1":311.3465244865483,"y1":273.4444093382673,"x2":338.06461610540646,"y2":258.79216005827334},{"x1":293.25412497804064,"y1":273.7694225030943,"x2":322.99138008326383,"y2":259.6503760975967},{"x1":318.3884763910774,"y1":320.67965595929815,"x2":342.86104178785206,"y2":295.84356825797295},{"x1":345.6895822362422,"y1":328.1549587502408,"x2":364.81743183176945,"y2":301.66375419050695},{"x1":307.9338862559275,"y1":337.90535369494177,"x2":335.7513535831127,"y2":308.84314522046924},{"x1":373.53237668950817,"y1":332.2176233105289,"x2":385.1706568887479,"y2":306.7172090420421},{"x1":395.09158328949553,"y1":319.65044760401224,"x2":402.6312146857466,"y2":298.59682986671163},{"x1":98.99104791995451,"y1":342.78055116727876,"x2":185.10072274713397,"y2":338.87864092476855},{"x1":144.926241881691,"y1":191.9744426891324,"x2":183.04448939363147,"y2":216.30622221818692},{"x1":120.65859224153175,"y1":156.65634544497104,"x2":152.93286876171953,"y2":195.81359151071769},{"x1":190.96977356504095,"y1":107.36268211328067,"x2":200.05197882470097,"y2":122.34681339481585},{"x1":181.54439178515682,"y1":104.32922590823802,"x2":187.5751730535579,"y2":122.90443599895576},{"x1":269.62295945234337,"y1":110.82948920470984,"x2":286.6228881416063,"y2":107.22130025347843},{"x1":571.3435141302664,"y1":282.8697911179728,"x2":538.5986024578838,"y2":304.6894049992168},{"x1":535.5920659996714,"y1":220.14225030702306,"x2":535.3922724833715,"y2":253.17901692695563},{"x1":450.5740389679452,"y1":261.09390907478206,"x2":452.428484387713,"y2":260.2015766005447},{"x1":464.6579427770237,"y1":438.5510970684899,"x2":440.9972209998251,"y2":388.10626146078795}]
Insert into this code with sW=640 and sH=520:
function testTPS(controlPoints, sW, sH) {
var tps = new ThinPlateSpline();
var trans = [];
for(var i = 0; i < controlPoints.length; i++) {
var p = controlPoints[i];
trans.push([[p.x1, p.y1], [p.x2, p.y2]]);
}
tps.push_points(trans);
tps.solve();
var points = [];
var steps = 1;
var stepX = sW / steps;
var stepY = sH / steps;
for(var y = 0; y <= steps; y++) {
for(var x = 0; x <= steps; x++) {
var xT = x * stepX;
var yT = y * stepY;
var p = tps.transform([xT, yT], false);
var invP = tps.transform(p, true);
points.push([xT, invP[0], yT, invP[1]]);
}
}
console.log('ControlPoints', controlPoints);
console.log('Points:', points);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment