Skip to content

Commit

Permalink
#16 internal calculateNFP implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fel88 committed Aug 24, 2021
1 parent 50dad9f commit 0e2a3fe
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 72 deletions.
2 changes: 1 addition & 1 deletion DeepNestConsole/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
4 changes: 3 additions & 1 deletion DeepNestConsole/DeepNestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DeepNestConsole</RootNamespace>
<AssemblyName>DeepNestConsole</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
Expand All @@ -23,6 +23,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -32,6 +33,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
128 changes: 121 additions & 7 deletions DeepNestLib/Background.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace DeepNestLib
Expand Down Expand Up @@ -351,9 +352,13 @@ public static NFP[] Process2(NFP A, NFP B, int type)
{
ret.AddPoint(new SvgPoint(item.X, item.Y));
}


foreach (var item in holesout)
{
ret.children = new List<NFP>();
if (ret.children == null)
ret.children = new List<NFP>();

ret.children.Add(new NFP());
ret.children.Last().Points = new SvgPoint[] { };
foreach (var hitem in item)
Expand Down Expand Up @@ -711,7 +716,7 @@ public static SheetPlacement placeParts(NFP[] sheets, NFP[] parts, SvgNestConfig
}
}

var clipperNfp = nfpToClipperCoordinates(nfp, config);
var clipperNfp = nfpToClipperCoordinates(nfp, config.clipperScale);

clipper.AddPaths(clipperNfp.Select(z => z.ToList()).ToList(), ClipperLib.PolyType.ptSubject, true);
}
Expand Down Expand Up @@ -1417,7 +1422,7 @@ public static NFP getHull(NFP polygon)


// returns clipper nfp. Remember that clipper nfp are a list of polygons, not a tree!
public static IntPoint[][] nfpToClipperCoordinates(NFP nfp, SvgNestConfig config)
public static IntPoint[][] nfpToClipperCoordinates(NFP nfp, double clipperScale = 10000000)
{

List<IntPoint[]> clipperNfp = new List<IntPoint[]>();
Expand All @@ -1432,7 +1437,7 @@ public static IntPoint[][] nfpToClipperCoordinates(NFP nfp, SvgNestConfig config
nfp.children[j].reverse();
}
//var childNfp = SvgNest.toClipperCoordinates(nfp.children[j]);
var childNfp = _Clipper.ScaleUpPaths(nfp.children[j], config.clipperScale);
var childNfp = _Clipper.ScaleUpPaths(nfp.children[j], clipperScale);
clipperNfp.Add(childNfp);
}
}
Expand All @@ -1447,7 +1452,7 @@ public static IntPoint[][] nfpToClipperCoordinates(NFP nfp, SvgNestConfig config

// clipper js defines holes based on orientation

var outerNfp = _Clipper.ScaleUpPaths(nfp, config.clipperScale);
var outerNfp = _Clipper.ScaleUpPaths(nfp, clipperScale);

//var cleaned = ClipperLib.Clipper.CleanPolygon(outerNfp, 0.00001*config.clipperScale);

Expand All @@ -1462,7 +1467,7 @@ public static IntPoint[][] innerNfpToClipperCoordinates(NFP[] nfp, SvgNestConfig
List<IntPoint[]> clipperNfp = new List<IntPoint[]>();
for (var i = 0; i < nfp.Count(); i++)
{
var clip = nfpToClipperCoordinates(nfp[i], config);
var clip = nfpToClipperCoordinates(nfp[i], config.clipperScale);
clipperNfp.AddRange(clip);
//clipperNfp = clipperNfp.Concat(new[] { clip }).ToList();
}
Expand All @@ -1471,6 +1476,107 @@ public static IntPoint[][] innerNfpToClipperCoordinates(NFP[] nfp, SvgNestConfig
}

static object lockobj = new object();

public static NFP[] NewMinkowskiSum(NFP pattern, NFP path, int type, bool useChilds = false, bool takeOnlyBiggestArea = true)
{
var key = pattern.source + ";" + path.source + ";" + pattern.rotation + ";" + path.rotation;
bool cacheAllow = type != 1;
if (cacheProcess.ContainsKey(key) && cacheAllow)
{
return cacheProcess[key];
}

var ac = _Clipper.ScaleUpPaths(pattern, 10000000);
List<List<IntPoint>> solution = null;
if (useChilds)
{
var bc = Background.nfpToClipperCoordinates(path, 10000000);
for (var i = 0; i < bc.Length; i++)
{
for (int j = 0; j < bc[i].Length; j++)
{
bc[i][j].X *= -1;
bc[i][j].Y *= -1;
}
}

solution = ClipperLib.Clipper.MinkowskiSum(new List<IntPoint>(ac), new List<List<IntPoint>>(bc.Select(z => z.ToList())), true);
}
else
{
var bc = _Clipper.ScaleUpPaths(path, 10000000);
for (var i = 0; i < bc.Length; i++)
{
bc[i].X *= -1;
bc[i].Y *= -1;
}
solution = Clipper.MinkowskiSum(new List<IntPoint>(ac), new List<IntPoint>(bc), true);
}
NFP clipperNfp = null;

double? largestArea = null;
int largestIndex = -1;

for (int i = 0; i < solution.Count(); i++)
{
var n = toNestCoordinates(solution[i].ToArray(), 10000000);
var sarea = Math.Abs(GeometryUtil.polygonArea(n));
if (largestArea == null || largestArea < sarea)
{
clipperNfp = n;
largestArea = sarea;
largestIndex = i;
}
}
if (!takeOnlyBiggestArea)
{
for (int j = 0; j < solution.Count; j++)
{
if (j == largestIndex) continue;
var n = toNestCoordinates(solution[j].ToArray(), 10000000);
if (clipperNfp.children == null)
clipperNfp.children = new List<NFP>();
clipperNfp.children.Add(n);
}
}
for (var i = 0; i < clipperNfp.Length; i++)
{

clipperNfp[i].x *= -1;
clipperNfp[i].y *= -1;
clipperNfp[i].x += pattern[0].x;
clipperNfp[i].y += pattern[0].y;

}
if (clipperNfp.children != null)
foreach (var nFP in clipperNfp.children)
{
for (int j = 0; j < nFP.Length; j++)
{

nFP.Points[j].x *= -1;
nFP.Points[j].y *= -1;
nFP.Points[j].x += pattern[0].x;
nFP.Points[j].y += pattern[0].y;
}
}
var res = new[] { clipperNfp };
if (cacheAllow)
{
cacheProcess.Add(key, res);
}
return res;
}
public static void ExecuteSTA(Action act)
{
if (!Debugger.IsAttached) return;
Thread thread = new Thread(() => { act(); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}

public static bool UseExternalDll = true;
public static NFP getOuterNfp(NFP A, NFP B, int type, bool inside = false)//todo:?inside def?
{
NFP[] nfp = null;
Expand Down Expand Up @@ -1507,7 +1613,15 @@ public static NFP getOuterNfp(NFP A, NFP B, int type, bool inside = false)//todo
{
lock (lockobj)
{
nfp = Process2(A, B, type);
if (UseExternalDll)
{
nfp = Process2(A, B, type);

}
else
{
nfp = NewMinkowskiSum(B, A, type, true, takeOnlyBiggestArea: false);
}
}
}
else
Expand Down
8 changes: 5 additions & 3 deletions DeepNestLib/DeepNestLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DeepNestLib</RootNamespace>
<AssemblyName>DeepNestLib</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,10 +30,11 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="IxMilia.Dxf, Version=0.7.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IxMilia.Dxf.0.7.2\lib\net35\IxMilia.Dxf.dll</HintPath>
<Reference Include="IxMilia.Dxf, Version=0.7.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IxMilia.Dxf.0.7.5\lib\net45\IxMilia.Dxf.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
31 changes: 29 additions & 2 deletions DeepNestLib/NFP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace DeepNestLib
{
Expand All @@ -20,7 +21,7 @@ public NFP()
{
Points = new SvgPoint[] { };
}

public string Name { get; set; }
public void AddPoint(SvgPoint point)
{
Expand All @@ -38,6 +39,32 @@ public void reverse()
Points = Points.Reverse().ToArray();
}

public StringBuilder GetXml()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\"?>");
sb.AppendLine("<root>");
sb.AppendLine("<region>");
foreach (var item in Points)
{
sb.AppendLine($"<point x=\"{item.x}\" y=\"{item.y}\"/>");
}
sb.AppendLine("</region>");
if (children != null)
foreach (var item in children)
{
sb.AppendLine("<region>");
foreach (var citem in item.Points)
{
sb.AppendLine($"<point x=\"{citem.x}\" y=\"{citem.y}\"/>");
}
sb.AppendLine("</region>");
}

sb.AppendLine("</root>");

return sb;
}
public double x { get; set; }
public double y { get; set; }

Expand Down Expand Up @@ -105,7 +132,7 @@ public int id
Id = value;
}
}

public double? offsetx;
public double? offsety;
public int? source = null;
Expand Down
2 changes: 1 addition & 1 deletion DeepNestLib/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="IxMilia.Dxf" version="0.7.2" targetFramework="net40" />
<package id="IxMilia.Dxf" version="0.7.5" targetFramework="net45" />
</packages>
6 changes: 5 additions & 1 deletion DeepNestPort/DeepNestPort.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DeepNestPort</RootNamespace>
<AssemblyName>DeepNestPort</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -22,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>true</UseVSHostingProcess>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -31,6 +33,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="ObjectListView, Version=2.9.1.1072, Culture=neutral, PublicKeyToken=b1c5bf581481bcd4, processorArchitecture=MSIL">
Expand Down Expand Up @@ -95,6 +98,7 @@
<EmbeddedResource Include="QntDialog.resx">
<DependentUpon>QntDialog.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.config" />
<None Include="dxfs\_1.dxf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Loading

0 comments on commit 0e2a3fe

Please sign in to comment.