2014-01-29 19:09:30 -08:00
|
|
|
|
/*
|
2014-02-15 18:06:03 -08:00
|
|
|
|
Copyright (c) 2014, Lars Brubaker
|
2014-01-29 19:09:30 -08:00
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
2015-04-08 15:20:10 -07:00
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
2015-04-08 15:20:10 -07:00
|
|
|
|
list of conditions and the following disclaimer.
|
2014-01-29 19:09:30 -08:00
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
2015-04-08 15:20:10 -07:00
|
|
|
|
and/or other materials provided with the distribution.
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
2015-04-08 15:20:10 -07:00
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
2014-01-29 19:09:30 -08:00
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2017-07-18 18:15:10 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-07-09 16:48:12 -07:00
|
|
|
|
using System.Diagnostics;
|
2017-07-18 18:15:10 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
using ClipperLib;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.Image;
|
2018-07-13 07:58:48 -07:00
|
|
|
|
using MatterHackers.Agg.Image.ThresholdFunctions;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.Agg.ImageProcessing;
|
|
|
|
|
|
using MatterHackers.MarchingSquares;
|
2018-06-20 17:16:38 -07:00
|
|
|
|
using MatterHackers.MatterControl.DesignTools;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.PolygonMesh;
|
|
|
|
|
|
using MatterHackers.VectorMath;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
2015-04-08 15:20:10 -07:00
|
|
|
|
using Polygon = List<IntPoint>;
|
|
|
|
|
|
|
|
|
|
|
|
using Polygons = List<List<IntPoint>>;
|
|
|
|
|
|
|
|
|
|
|
|
public static class CreateDiscreteMeshes
|
|
|
|
|
|
{
|
2017-07-18 18:15:10 -07:00
|
|
|
|
public static List<Mesh> SplitVolumesIntoMeshes(Mesh meshToSplit, CancellationToken cancellationToken, Action<double, string> reportProgress)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2019-01-11 16:49:34 -08:00
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
//Stopwatch maxProgressReport = Stopwatch.StartNew();
|
|
|
|
|
|
//List<Mesh> discreetVolumes = new List<Mesh>();
|
|
|
|
|
|
//HashSet<Face> facesThatHaveBeenAdded = new HashSet<Face>();
|
|
|
|
|
|
//Mesh meshFromCurrentVolume = null;
|
|
|
|
|
|
//Stack<Face> attachedFaces = new Stack<Face>();
|
|
|
|
|
|
//int faceCount = meshToSplit.Faces.Count;
|
|
|
|
|
|
//for (int faceIndex = 0; faceIndex < faceCount; faceIndex++)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (reportProgress != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (maxProgressReport.ElapsedMilliseconds > 200)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// reportProgress(faceIndex / (double)faceCount, "Merging Mesh Edges");
|
|
|
|
|
|
// maxProgressReport.Restart();
|
|
|
|
|
|
// if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// Face currentFace = meshToSplit.Faces[faceIndex];
|
|
|
|
|
|
// // If this face as not been added to any volume, create a new volume and add all of the attached faces.
|
|
|
|
|
|
// if (!facesThatHaveBeenAdded.Contains(currentFace))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// attachedFaces.Push(currentFace);
|
|
|
|
|
|
// meshFromCurrentVolume = new Mesh();
|
|
|
|
|
|
|
|
|
|
|
|
// while (attachedFaces.Count > 0)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Face faceToAdd = attachedFaces.Pop();
|
|
|
|
|
|
// foreach (IVertex attachedVertex in faceToAdd.Vertices())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// foreach (Face faceAttachedToVertex in attachedVertex.ConnectedFaces())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (!facesThatHaveBeenAdded.Contains(faceAttachedToVertex))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // mark that this face has been taken care of
|
|
|
|
|
|
// facesThatHaveBeenAdded.Add(faceAttachedToVertex);
|
|
|
|
|
|
// // add it to the list of faces we need to walk
|
|
|
|
|
|
// attachedFaces.Push(faceAttachedToVertex);
|
|
|
|
|
|
|
|
|
|
|
|
// // Add a new face to the new mesh we are creating.
|
|
|
|
|
|
// var faceVertices = new List<IVertex>();
|
|
|
|
|
|
// foreach (FaceEdge faceEdgeToAdd in faceAttachedToVertex.FaceEdges())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var newVertex = meshFromCurrentVolume.CreateVertex(faceEdgeToAdd.FirstVertex.Position, CreateOption.CreateNew, SortOption.WillSortLater);
|
|
|
|
|
|
// faceVertices.Add(newVertex);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// meshFromCurrentVolume.CreateFace(faceVertices.ToArray(), CreateOption.CreateNew);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// discreetVolumes.Add(meshFromCurrentVolume);
|
|
|
|
|
|
// meshFromCurrentVolume = null;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (reportProgress != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// double progress = faceIndex / (double)meshToSplit.Faces.Count;
|
|
|
|
|
|
// reportProgress(progress, "Split Into Meshes");
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
//return discreetVolumes;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool PointInPolygon(Polygon polygon, IntPoint testPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
int numPoints = polygon.Count;
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
for (int i = 0; i < numPoints; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
int prevIndex = i - 1;
|
|
|
|
|
|
if (prevIndex < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
prevIndex += numPoints;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((((polygon[i].Y <= testPosition.Y) && (testPosition.Y < polygon[prevIndex].Y))
|
|
|
|
|
|
|| ((polygon[prevIndex].Y <= testPosition.Y) && (testPosition.Y < polygon[i].Y)))
|
|
|
|
|
|
&& (testPosition.X - polygon[i].X < (polygon[prevIndex].X - polygon[i].X) * (testPosition.Y - polygon[i].Y) / (polygon[prevIndex].Y - polygon[i].Y)))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = !result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static private void GetAreasRecursive(PolyNode polyTreeForPlate, Polygons discreteAreas)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!polyTreeForPlate.IsHole)
|
|
|
|
|
|
{
|
|
|
|
|
|
discreteAreas.Add(polyTreeForPlate.Contour);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (PolyNode child in polyTreeForPlate.Childs)
|
|
|
|
|
|
{
|
|
|
|
|
|
GetAreasRecursive(child, discreteAreas);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static public PolyTree FindDistictObjectBounds(ImageBuffer image)
|
|
|
|
|
|
{
|
2018-06-20 17:16:38 -07:00
|
|
|
|
var intensity = new MapOnMaxIntensity();
|
|
|
|
|
|
MarchingSquaresByte marchingSquaresData = new MarchingSquaresByte(image, intensity.ZeroColor, intensity.Threshold, 0);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
marchingSquaresData.CreateLineSegments();
|
|
|
|
|
|
Polygons lineLoops = marchingSquaresData.CreateLineLoops(1);
|
|
|
|
|
|
|
|
|
|
|
|
if (lineLoops.Count == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create a bounding polygon to clip against
|
|
|
|
|
|
IntPoint min = new IntPoint(long.MaxValue, long.MaxValue);
|
|
|
|
|
|
IntPoint max = new IntPoint(long.MinValue, long.MinValue);
|
|
|
|
|
|
foreach (Polygon polygon in lineLoops)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (IntPoint point in polygon)
|
|
|
|
|
|
{
|
|
|
|
|
|
min.X = Math.Min(point.X - 10, min.X);
|
|
|
|
|
|
min.Y = Math.Min(point.Y - 10, min.Y);
|
|
|
|
|
|
max.X = Math.Max(point.X + 10, max.X);
|
|
|
|
|
|
max.Y = Math.Max(point.Y + 10, max.Y);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Polygon boundingPoly = new Polygon();
|
|
|
|
|
|
boundingPoly.Add(min);
|
|
|
|
|
|
boundingPoly.Add(new IntPoint(min.X, max.Y));
|
|
|
|
|
|
boundingPoly.Add(max);
|
|
|
|
|
|
boundingPoly.Add(new IntPoint(max.X, min.Y));
|
|
|
|
|
|
|
|
|
|
|
|
// now clip the polygons to get the inside and outside polys
|
|
|
|
|
|
Clipper clipper = new Clipper();
|
|
|
|
|
|
clipper.AddPaths(lineLoops, PolyType.ptSubject, true);
|
|
|
|
|
|
clipper.AddPath(boundingPoly, PolyType.ptClip, true);
|
|
|
|
|
|
|
|
|
|
|
|
PolyTree polyTreeForPlate = new PolyTree();
|
|
|
|
|
|
clipper.Execute(ClipType.ctIntersection, polyTreeForPlate);
|
|
|
|
|
|
|
|
|
|
|
|
return polyTreeForPlate;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|