♻️ Reworked sampling/anchor code
This commit is contained in:
parent
118677aa22
commit
829baa469b
4 changed files with 80 additions and 47 deletions
|
@ -106,6 +106,37 @@ Pattern.prototype.sampleParts = function() {
|
|||
return parts;
|
||||
};
|
||||
|
||||
Pattern.prototype.sampleRun = function(parts, anchors, l, extraClass = false) {
|
||||
this.draft();
|
||||
for (let i in this.parts) {
|
||||
let anchor = false;
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
if (this.parts[i].points.anchor) {
|
||||
if (typeof anchors[i] === "undefined")
|
||||
anchors[i] = this.parts[i].points.anchor;
|
||||
else {
|
||||
if (!anchors[i].sitsOn(this.parts[i].points.anchor)) {
|
||||
dx = this.parts[i].points.anchor.dx(anchors[i]);
|
||||
dy = this.parts[i].points.anchor.dy(anchors[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let j in this.parts[i].paths) {
|
||||
parts[i].paths[j + "_" + l] = this.parts[i].paths[j]
|
||||
.clone()
|
||||
.attr("class", "sample sample-" + l, true);
|
||||
if (this.parts[i].points.anchor)
|
||||
parts[i].paths[j + "_" + l] = parts[i].paths[j + "_" + l].translate(
|
||||
dx,
|
||||
dy
|
||||
);
|
||||
if (extraClass !== false)
|
||||
parts[i].paths[j + "_" + l].attributes.add("class", extraClass);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles option sampling
|
||||
*/
|
||||
|
@ -127,32 +158,7 @@ Pattern.prototype.sampleOption = function(optionName) {
|
|||
debugStyle("info", "🔬 Sample run"),
|
||||
`Sampling option ${optionName} with value ${round(val)}`
|
||||
);
|
||||
this.draft();
|
||||
for (let i in this.parts) {
|
||||
let anchor = false;
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
if (this.parts[i].points.anchor) {
|
||||
if (typeof anchors[i] === "undefined")
|
||||
anchors[i] = this.parts[i].points.anchor;
|
||||
else {
|
||||
if (!anchors[i].sitsOn(this.parts[i].points.anchor)) {
|
||||
dx = this.parts[i].points.anchor.dx(anchors[i]);
|
||||
dy = this.parts[i].points.anchor.dy(anchors[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let j in this.parts[i].paths) {
|
||||
parts[i].paths[j + "_" + l] = this.parts[i].paths[j]
|
||||
.clone()
|
||||
.attr("class", "sample-" + l, true);
|
||||
if (this.parts[i].points.anchor)
|
||||
parts[i].paths[j + "_" + l] = parts[i].paths[j + "_" + l].translate(
|
||||
dx,
|
||||
dy
|
||||
);
|
||||
}
|
||||
}
|
||||
this.sampleRun(parts, anchors, l);
|
||||
val += step;
|
||||
}
|
||||
this.parts = parts;
|
||||
|
@ -164,6 +170,7 @@ Pattern.prototype.sampleOption = function(optionName) {
|
|||
* Handles measurement sampling
|
||||
*/
|
||||
Pattern.prototype.sampleMeasurement = function(measurementName) {
|
||||
let anchors = {};
|
||||
let parts = this.sampleParts();
|
||||
let val = this.settings.measurements[measurementName];
|
||||
if (val === undefined) throw "Cannot sample a measurement that is undefined";
|
||||
|
@ -175,14 +182,7 @@ Pattern.prototype.sampleMeasurement = function(measurementName) {
|
|||
debugStyle("info", "🔬 Sample run"),
|
||||
`Sampling measurement ${measurementName} with value ${round(val)}`
|
||||
);
|
||||
this.draft();
|
||||
for (let i in this.parts) {
|
||||
for (let j in this.parts[i].paths) {
|
||||
parts[i].paths[j + "_" + l] = this.parts[i].paths[j]
|
||||
.clone()
|
||||
.attr("class", "sample-" + l, true);
|
||||
}
|
||||
}
|
||||
this.sampleRun(parts, anchors, l);
|
||||
val += step;
|
||||
}
|
||||
this.parts = parts;
|
||||
|
@ -194,22 +194,15 @@ Pattern.prototype.sampleMeasurement = function(measurementName) {
|
|||
* Handles models sampling
|
||||
*/
|
||||
Pattern.prototype.sampleModels = function(models, focus = false) {
|
||||
let anchors = {};
|
||||
let parts = this.sampleParts();
|
||||
let count = 0;
|
||||
for (let l in models) {
|
||||
count++;
|
||||
this.settings.measurements = models[l];
|
||||
this.debug(debugStyle("info", "🔬 Sample run"), `Sampling model ${l}`);
|
||||
this.draft();
|
||||
for (let i in this.parts) {
|
||||
for (let j in this.parts[i].paths) {
|
||||
parts[i].paths[j + "_" + count] = this.parts[i].paths[j]
|
||||
.clone()
|
||||
.attr("class", "sample sample-" + count, true);
|
||||
if (l === focus)
|
||||
parts[i].paths[j + "_" + count].attr("class", "sample-focus");
|
||||
}
|
||||
}
|
||||
let className = l === focus ? "sample-focus" : "";
|
||||
this.sampleRun(parts, anchors, count, className);
|
||||
}
|
||||
this.parts = parts;
|
||||
|
||||
|
|
|
@ -702,3 +702,24 @@ it("Should trim a path when a curves overlap", () => {
|
|||
expect(test.ops[2].to.x).to.equal(50);
|
||||
expect(test.ops[2].to.y).to.equal(11.01);
|
||||
});
|
||||
|
||||
it("Should translate a path", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
pattern.parts.a = new pattern.Part();
|
||||
let a = pattern.parts.a;
|
||||
a.points.A = new a.Point(0, 0);
|
||||
a.points.B = new a.Point(100, 100);
|
||||
a.points.C = new a.Point(0, 100);
|
||||
a.points.D = new a.Point(100, 0);
|
||||
|
||||
let base = new a.Path()
|
||||
.move(a.points.A)
|
||||
.curve(a.points.B, a.points.C, a.points.D);
|
||||
let test = base.translate(10, 20);
|
||||
|
||||
expect(test.ops.length).to.equal(2);
|
||||
expect(test.ops[0].to.x).to.equal(10);
|
||||
expect(test.ops[0].to.y).to.equal(20);
|
||||
expect(test.ops[1].to.x).to.equal(110);
|
||||
expect(test.ops[1].to.y).to.equal(20);
|
||||
});
|
||||
|
|
|
@ -171,9 +171,9 @@ it("Should sample models with focus", () => {
|
|||
expect(pattern.parts.a.paths.test_1.attributes.get("class")).to.equal(
|
||||
"sample sample-1 sample-focus"
|
||||
);
|
||||
expect(pattern.parts.b.paths.test_2.attributes.get("class")).to.equal(
|
||||
"sample sample-2"
|
||||
);
|
||||
expect(
|
||||
pattern.parts.b.paths.test_2.attributes.get("class")
|
||||
).to.equalIgnoreSpaces("sample sample-2");
|
||||
});
|
||||
|
||||
it("Should register a hook via on", () => {
|
||||
|
|
|
@ -50,12 +50,24 @@ it("Should check points for equality", () => {
|
|||
expect(a).to.deep.equal(b);
|
||||
});
|
||||
|
||||
it("Should flip point around X value zero", () => {
|
||||
let result = new Point(2, 4).flipX();
|
||||
expect(result.x).to.equal(-2);
|
||||
expect(result.y).to.equal(4);
|
||||
});
|
||||
|
||||
it("Should flip point around X value", () => {
|
||||
let result = new Point(2, 4).flipX(new Point(-20, 19));
|
||||
expect(result.x).to.equal(-42);
|
||||
expect(result.y).to.equal(4);
|
||||
});
|
||||
|
||||
it("Should flip point around Y value zero", () => {
|
||||
let result = new Point(2, 4).flipY();
|
||||
expect(result.x).to.equal(2);
|
||||
expect(result.y).to.equal(-4);
|
||||
});
|
||||
|
||||
it("Should flip point around Y value", () => {
|
||||
let result = new Point(2, 4).flipY(new Point(12, -14));
|
||||
expect(result.x).to.equal(2);
|
||||
|
@ -190,3 +202,10 @@ it("Should clone a point", () => {
|
|||
expect(p2.sitsOn(p1)).to.equal(true);
|
||||
expect(p2.attributes.get("class")).to.equal("something else");
|
||||
});
|
||||
|
||||
it("Should translate a point", () => {
|
||||
let p1 = new Point(10, 20);
|
||||
let p2 = p1.translate(15, 50);
|
||||
expect(p2.x).to.equal(25);
|
||||
expect(p2.y).to.equal(70);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue