Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature implementation Rotation along path and path context with toPoints and toKeyPoints #566

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 102 additions & 14 deletions packages/maker.js/src/core/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ namespace MakerJs.chain {
/**
* @private
*/
function removeDuplicateEnds(endless: boolean, points: IPoint[]) {
function removeDuplicateEnds(endless: boolean, points: IWalkChainPoints[]) {
if (!endless || points.length < 2) return;
if (measure.isPointEqual(points[0], points[points.length - 1], .00001)) {
if (measure.isPointEqual(points[0].point, points[points.length - 1].point, .00001)) {
points.pop();
}
}
Expand All @@ -575,8 +575,40 @@ namespace MakerJs.chain {
* @param maxPoints Maximum number of points to retrieve.
* @returns Array of points which are on the chain spread at a uniform interval.
*/
export function toPoints(chainContext: IChain, distanceOrDistances: number | number[], maxPoints?: number): IPoint[] {
var result: IPoint[] = [];
export function toPoints(chainContext: IChain, distanceOrDistances: number | number[], maxPoints?: number): IPoint[];

/**
* Get points along a chain of paths.
*
* @param chainContext Chain of paths to get points from.
* @param distance Numeric distance along the chain between points, or numeric array of distances along the chain between each point.
* @param callback Callback function when points are found.
* @param maxPoints Maximum number of points to retrieve.
* @returns Array of points which are on the chain spread at a uniform interval.
*/
export function toPoints(chainContext: IChain, distanceOrDistances: number | number[], callback: IChainPointsCallback, maxPoints?: number): IPoint[];

export function toPoints(chainContext: IChain, distanceOrDistances: number | number[], ... args: any): IPoint[] {

var maxPoints: number;
var callback: IChainPointsCallback;

switch (args.length) {
case 1:
if (typeof args[0] === 'function') {
callback = args[0];
} else {
maxPoints = args[0];
}
break;

case 2:
callback = args[0];
maxPoints = args[1];
break;
}

var result: IWalkChainPoints[] = [];
var di = 0;
var t = 0;
var distanceArray: number[];
Expand All @@ -595,18 +627,32 @@ namespace MakerJs.chain {
if (link.reversed) {
r = 1 - r;
}
let chainPoint: IWalkChainPoints = {
point: point.add(point.middle(wp.pathContext, r), wp.offset),
danmarshall marked this conversation as resolved.
Show resolved Hide resolved
pathContext: wp.pathContext,
modelContext: wp.modelContext,
pathId: wp.pathId,
layer: wp.layer,
offset: wp.offset,
route: wp.route,
routeKey: wp.routeKey,
endPoints: link.endPoints
};
result.push(chainPoint);

result.push(point.add(point.middle(wp.pathContext, r), wp.offset));

if (maxPoints && result.length >= maxPoints) return result;
if (maxPoints && result.length >= maxPoints) {
if (callback) callback(result);
return result.map(x => x.point);
}

var distance: number;
if (distanceArray) {
distance = distanceArray[di];
di++;

if (di > distanceArray.length) {
return result;
if (callback) callback(result);
return result.map(x => x.point);
}

} else {
Expand All @@ -615,12 +661,12 @@ namespace MakerJs.chain {

t += distance;
}

t -= len;
}

removeDuplicateEnds(chainContext.endless, result);
return result;
if (callback) callback(result);
return result.map(x => x.point);
}

/**
Expand All @@ -630,8 +676,35 @@ namespace MakerJs.chain {
* @param maxArcFacet The maximum length between points on an arc or circle.
* @returns Array of points which are on the chain.
*/
export function toKeyPoints(chainContext: IChain, maxArcFacet?: number): IPoint[] {
var result: IPoint[] = [];
export function toKeyPoints(chainContext: IChain, maxArcFacet?: number): IPoint[];
noobd3v marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get key points (a minimal a number of points) along a chain of paths.
*
* @param chainContext Chain of paths to get points from.
* @param maxArcFacet The maximum length between points on an arc or circle.
* @param callback Callback function when points are found.
* @returns Array of points which are on the chain.
*/
export function toKeyPoints(chainContext: IChain, callback: IWalkChainPoints, maxArcFacet?: number): IPoint[];
export function toKeyPoints(chainContext: IChain, ... args: any): IPoint[] {
var maxArcFacet: number;
var callback: IChainPointsCallback;
switch (args.length) {
case 1:
if (typeof args[0] === 'function') {
callback = args[0];
} else {
maxArcFacet = args[0];
}
break;

case 2:
callback = args[0];
maxArcFacet = args[1];
break;
}
var result: IWalkChainPoints[] = [];

for (var i = 0; i < chainContext.links.length; i++) {
var link = chainContext.links[i];
Expand All @@ -646,12 +719,27 @@ namespace MakerJs.chain {
}

var offsetPathPoints = keyPoints.map(p => point.add(p, wp.offset));
result.push.apply(result, offsetPathPoints);

var chainPoints = offsetPathPoints.map(p=> {
return {
point: p,
pathContext: wp.pathContext,
modelContext: wp.modelContext,
pathId: wp.pathId,
layer: wp.layer,
offset: wp.offset,
route: wp.route,
routeKey: wp.routeKey,
endPoints: link.endPoints
}
});
result.push.apply(result, chainPoints);
}
}

removeDuplicateEnds(chainContext.endless, result);
return result;
if (callback) callback(result);
return result.map(x=> x.point);
}

}
14 changes: 11 additions & 3 deletions packages/maker.js/src/core/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ namespace MakerJs.layout {
* @param reversed Flag to travel along the chain in reverse. Default is false.
* @param contain Flag to contain the children layout within the length of the chain. Default is false.
* @param rotate Flag to rotate the child to mitered angle. Default is true.
* @param rotateAlongPath Flag to rotate the child along the layout path. Default is false. Works only if rotate is set true.
* @returns The parentModel, for cascading.
*/
export function childrenOnChain(parentModel: IModel, onChain: IChain, baseline = 0, reversed = false, contain = false, rotated = true) {
export function childrenOnChain(parentModel: IModel, onChain: IChain, baseline = 0, reversed = false, contain = false, rotated = true, rotateAlongPath = false) {
var result = getChildPlacement(parentModel, baseline);
var cpa = result.cpa;

Expand All @@ -206,8 +207,15 @@ namespace MakerJs.layout {
relatives.shift();
}

var alongPathAngles: number[] = [];
//chain.toPoints always follows the chain in its order, from beginning to end. This is why we needed to contort the points input
points = chain.toPoints(onChain, relatives);
points = chain.toPoints(onChain, relatives,
(chainPoints)=> {
for(let i = 0; i < chainPoints.length; i++)
{
alongPathAngles.push(angle.ofPointInDegrees(chainPoints[i].endPoints[0], chainPoints[i].endPoints[1]));
}
});

if (points.length < cpa.length) {
//add last point of chain, since our distances exceeded the chain
Expand All @@ -231,7 +239,7 @@ namespace MakerJs.layout {

if (cpa.length > 1) {
cpa.forEach((cp, i) => {
cp.angle = angles[i];
cp.angle = rotateAlongPath ? alongPathAngles[i+1] : angles[i];
cp.origin = points[i];
});
} else {
Expand Down
16 changes: 16 additions & 0 deletions packages/maker.js/src/core/maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,22 @@ namespace MakerJs {
alternateDirection?: boolean;
}

/**
* A point reference in a path.
*/
export interface IWalkChainPoints extends IWalkPath{
danmarshall marked this conversation as resolved.
Show resolved Hide resolved
point: IPoint;
endPoints: IPoint[];
}

/**
* Callback signature for chain.toPoints().
*/
export interface IChainPointsCallback {

(chainPoints: IWalkChainPoints[]): void;
}

/**
* Reference to a model within a model.
*/
Expand Down