Skip to content

Commit b38e37d

Browse files
author
Shan Huang
committed
WIP
1 parent 9d7c5e8 commit b38e37d

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

illustrationGen/illustration.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ export class PoseIllustration {
209209
let totalW = 0;
210210
let weights = {};
211211
bones.forEach(bone => {
212-
let d = bone.boneLine.getNearestPoint(point).getDistance(point);
212+
let d = MathUtils.getClosestPointOnSegment(bone.kp0.position, bone.kp1.position, point)
213+
.getDistance(point);
213214
// Absolute weight = 1 / (distance * distance)
214215
let w = 1 / (d * d);
215216
weights[bone.name] = {

illustrationGen/skeleton.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ export const allPartNames = posePartNames.concat(facePartNames);
125125
// Represents a bone formed by two part keypoints.
126126
export class Bone {
127127
constructor(kp0, kp1, skeleton, type) {
128+
console.log('constructing...');
128129
this.name = `${kp0.name}-${kp1.name}`;
129130
this.kp0 = kp0;
130131
this.kp1 = kp1;
131132
this.skeleton = skeleton;
132133
this.type = type;
133-
this.boneLine = new paper.default.Path(kp0.position, kp1.position);
134134
this.boneColor = ColorUtils.fromStringHash(this.name);
135135
this.boneColor.saturation += 0.5;
136136
};
@@ -142,11 +142,12 @@ export class Bone {
142142
let dir = this.kp1.position.subtract(this.kp0.position).normalize();
143143
let n = dir.clone();
144144
n.angle += 90;
145-
let closestP = this.boneLine.getNearestPoint(p);
145+
let closestP = MathUtils.getClosestPointOnSegment(this.kp0.position, this.kp1.position, p);
146146
let v = p.subtract(closestP);
147147
let dirProjD = v.dot(dir);
148148
let dirProjN = v.dot(n);
149-
let anchorPerc = closestP.subtract(this.kp0.position).length / this.boneLine.length;
149+
let d = this.kp0.position.subtract(this.kp1.position).length;
150+
let anchorPerc = closestP.subtract(this.kp0.position).length / d;
150151
return {
151152
transform: new paper.default.Point(dirProjD, dirProjN),
152153
anchorPerc: anchorPerc,
@@ -282,7 +283,7 @@ export class Skeleton {
282283
let leftLowerLipBottom0 = getKeyPointFromSVG(skeletonGroup, 'leftLowerLipBottom0');
283284
let leftLowerLipBottom1 = getKeyPointFromSVG(skeletonGroup, 'leftLowerLipBottom1');
284285
let lowerLipBottomMid = getKeyPointFromSVG(skeletonGroup, 'lowerLipBottomMid');
285-
286+
286287
this.bLeftShoulderRightShoulder = new Bone(leftShoulder, rightShoulder, this, 'body');
287288
this.bRightShoulderRightHip = new Bone(rightShoulder, rightHip, this, 'body');
288289
this.bLeftHipRightHip = new Bone(leftHip, rightHip, this, 'body');
@@ -364,6 +365,7 @@ export class Skeleton {
364365
this.bRightMouthCornerRightLowerLipBottom0 = new Bone(rightMouthCorner, rightLowerLipBottom0, this, 'face');
365366
this.bRightLowerLipBottom0RightLowerLipBottom1 = new Bone(rightLowerLipBottom0, rightLowerLipBottom1, this, 'face');
366367
this.bRightLowerLipBottom1LowerLipBottomMid = new Bone(rightLowerLipBottom1, lowerLipBottomMid, this, 'face');
368+
console.log('Done');
367369

368370
this.faceBones = [
369371
// Face
@@ -589,7 +591,9 @@ export class Skeleton {
589591
let minDistance = Infinity;
590592
let boneGroup = this.boneGroups[boneGroupKey];
591593
boneGroup.forEach(bone => {
592-
minDistance = Math.min(minDistance, bone.boneLine.getNearestPoint(point).getDistance(point));
594+
let d = MathUtils.getClosestPointOnSegment(bone.kp0.position, bone.kp1.position, point)
595+
.getDistance(point);
596+
minDistance = Math.min(minDistance, d);
593597
});
594598
minDistances[boneGroupKey] = minDistance;
595599
});

static_image.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function setupGui() {
218218
const imageControls = gui.addFolder('Image');
219219
imageControls.open();
220220
gui.add(guiState, 'sourceImage', Object.keys(sourceImages)).onChange(() => testImageAndEstimatePoses());
221-
gui.add(guiState, 'avatarSVG', Object.keys(avatarSvgs)).onChange(() => parseSVG(avatarSvgs[guiState.avatarSVG]));
221+
gui.add(guiState, 'avatarSVG', Object.keys(avatarSvgs)).onChange(() => loadSVG(avatarSvgs[guiState.avatarSVG]));
222222

223223
const debugControls = gui.addFolder('Debug controls');
224224
debugControls.open();
@@ -251,17 +251,24 @@ export async function bindPage() {
251251
});
252252

253253
setupGui(posenet);
254-
await parseSVG(Object.values(avatarSvgs)[0]);
254+
setStatusText('Loading SVG file...');
255+
await loadSVG(Object.values(avatarSvgs)[0]);
255256
}
256257

257258
window.onload = bindPage;
258-
FileUtils.setDragDropHandler(parseSVG);
259+
FileUtils.setDragDropHandler(loadSVG);
259260

260261
// Target is SVG string or path
261-
async function parseSVG(target) {
262+
async function loadSVG(target) {
263+
let t = new Date();
262264
let svgScope = await SVGUtils.importSVG(target);
265+
console.log('Loading AVatar dt0: ', new Date() - t);
263266
skeleton = new Skeleton(svgScope);
267+
console.log('Loading AVatar dt1: ', new Date() - t);
264268
illustration = new PoseIllustration(canvasScope);
269+
console.log('Loading AVatar dt2: ', new Date() - t);
265270
illustration.bindSkeleton(skeleton, svgScope);
266-
testImageAndEstimatePoses(posenet);
271+
console.log('Loading AVatar dt3: ', new Date() - t);
272+
testImageAndEstimatePoses();
273+
console.log('Loading AVatar dt4: ', new Date() - t);
267274
}

utils/mathUtils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export class MathUtils {
5656
}
5757
}
5858

59+
static getClosestPointOnSegment(p0, p1, p) {
60+
let d = p1.subtract(p0);
61+
let c = p.subtract(p0).dot(d) / (d.dot(d));
62+
if (c >= 1) {
63+
return p1.clone();
64+
} else if (c <= 0) {
65+
return p0.clone();
66+
} else {
67+
return p0.add(d.multiply(c));
68+
}
69+
}
70+
5971
// Check if v0 and v1 are collinear.
6072
// Returns true if cosine of the angle between v0 and v1 is within threshold to 1.
6173
static isCollinear(v0, v1, threshold = 0.01) {

0 commit comments

Comments
 (0)