Skip to content

Commit

Permalink
Introduce ObservablePoint and ObservableFrame to render better SheetN…
Browse files Browse the repository at this point in the history
…fp and FinalNfp re fel88/DeepNestPort#16 (comment)
  • Loading branch information
9swampy committed Sep 2, 2021
1 parent 0fb6f75 commit 3fedc2c
Show file tree
Hide file tree
Showing 11 changed files with 1,745 additions and 32 deletions.
11 changes: 11 additions & 0 deletions DeepNestLib/INfp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ public interface INfp : IMinMaxXY, IPolygon, IPlacement

NFP GetHull();

/// <summary>
/// Shifts the polygon and all it's children iteratively by the specified PartPlacement(X,Y) offset.
/// </summary>
/// <param name="shift">The amount to shift.</param>
/// <returns>A partial clone of the polygon.</returns>
INfp Shift(IPartPlacement shift);

/// <summary>
/// Shifts the polygon and all it's children iteratively by the specified X,Y offset.
/// </summary>
/// <param name="x">Distance to shift on X axis.</param>
/// <param name="y">Distance to shift on Y axis.</param>
/// <returns>A partial clone of the polygon.</returns>
INfp Shift(double x, double y);

/// <summary>
Expand Down
13 changes: 2 additions & 11 deletions DeepNestLib/NFP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,22 +540,13 @@ public string ToOpenScadPolygon()
return resultBuilder.ToString();
}

/// <summary>
/// Shifts the polygon and all it's children iteratively by the specified PartPlacement(X,Y) offset.
/// </summary>
/// <param name="shift"></param>
/// <returns></returns>
/// <inheritdoc />
public INfp Shift(IPartPlacement shift)
{
return Shift(shift.X, shift.Y);
}

/// <summary>
/// Shifts the polygon and all it's children iteratively by the specified X,Y offset.
/// </summary>
/// <param name="x">Distance to shift on X axis.</param>
/// <param name="y">Distance to shift on Y axis.</param>
/// <returns>A partial clone of the polygon.</returns>
/// <inheritdoc />
public INfp Shift(double x, double y)
{
NFP shifted = new NFP();
Expand Down
24 changes: 13 additions & 11 deletions DeepNestLib/Placement/PartPlacementWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,23 @@ public InnerFlowResult ProcessPart(INfp inputPart, int inputPartIndex)
if (Placements.Count == 0)
{
this.VerboseLog("First placement, put it on the bottom left corner. . .");

// first placement, put it on the bottom left corner
for (int j = 0; j < SheetNfp.Length; j++)
var processedPartOrigin = processedPart[0];
for (int nfpCandidateIndex = 0; nfpCandidateIndex < SheetNfp.Length; nfpCandidateIndex++)
{
for (int k = 0; k < SheetNfp[j].Length; k++)
var nfpCandidate = SheetNfp[nfpCandidateIndex];
for (int pointIndex = 0; pointIndex < nfpCandidate.Points.Length; pointIndex++)
{
var nfpPoint = nfpCandidate.Points[pointIndex];
var proposedX = nfpPoint.X - processedPartOrigin.X;
var proposedY = nfpPoint.Y - processedPartOrigin.Y;
if (position == null ||
((SheetNfp[j][k].X - processedPart[0].X) < position.X) ||
(
GeometryUtil.AlmostEqual(SheetNfp[j][k].X - processedPart[0].X, position.X)
&& ((SheetNfp[j][k].Y - processedPart[0].Y) < position.Y)))
proposedX < position.X ||
(GeometryUtil.AlmostEqual(proposedX, position.X) && proposedY < position.Y))
{
position = new PartPlacement(processedPart)
{
X = SheetNfp[j][k].X - processedPart[0].X,
Y = SheetNfp[j][k].Y - processedPart[0].Y,
X = proposedX,
Y = proposedY,
Id = processedPart.Id,
Rotation = processedPart.Rotation,
Source = processedPart.Source,
Expand All @@ -206,6 +207,7 @@ public InnerFlowResult ProcessPart(INfp inputPart, int inputPartIndex)
// console.log(sheetNfp);
}

SheetNfp = new SheetNfp(SheetNfp.Items, Sheet, processedPart.Shift(position));
AddPlacement(inputPart, processedPart, position, inputPartIndex);
}
else if (SheetNfp != null && SheetNfp.CanAcceptPart)
Expand Down Expand Up @@ -429,7 +431,7 @@ public InnerFlowResult ProcessPart(INfp inputPart, int inputPartIndex)

if (position != null)
{
FinalNfp = new NfpCandidateList(finalNfp.ToArray(), Sheet, new NFP(processedPart, WithChildren.Included).Shift(position));
FinalNfp = new NfpCandidateList(finalNfp.ToArray(), Sheet, processedPart.Shift(position));
AddPlacement(inputPart, processedPart, position, inputPartIndex);
if (position.MergedLength.HasValue)
{
Expand Down
Loading

0 comments on commit 3fedc2c

Please sign in to comment.