public static IPrimitive Convert(PolygonMesh.Mesh simpleMesh, MaterialAbstract partMaterial = null) { List <IPrimitive> renderCollection = new List <IPrimitive>(); if (partMaterial == null) { partMaterial = new SolidMaterial(new RGBA_Floats(.9, .2, .1), .01, 0.0, 2.0); } int index = 0; Vector3[] triangle = new Vector3[3]; foreach (PolygonMesh.Face face in simpleMesh.Faces) { foreach (PolygonMesh.Vertex vertex in face.Vertices()) { triangle[index++] = vertex.Position; if (index == 3) { index = 0; renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], partMaterial)); } } } return(BoundingVolumeHierarchy.CreateNewHierachy(renderCollection)); }
public static IPrimitive ConvertUnoptomized(PolygonMesh.Mesh simpleMesh) { List <IPrimitive> renderCollection = new List <IPrimitive>(); //SolidMaterial redStuff = new SolidMaterial(new RGBA_Floats(.9, .2, .1), .01, 0.0, 2.0); SolidMaterial mhBlueStuff = new SolidMaterial(new RGBA_Floats(0, .32, .58), .01, 0.0, 2.0); int index = 0; Vector3[] triangle = new Vector3[3]; //PolygonMesh.Mesh simpleMesh = PolygonMesh.Processors.StlProcessing.Load("complex.stl"); //PolygonMesh.Mesh simpleMesh = PolygonMesh.Processors.StlProcessing.Load("Spider With Base.stl"); foreach (PolygonMesh.Face face in simpleMesh.Faces) { foreach (PolygonMesh.Vertex vertex in face.Vertices()) { triangle[index++] = vertex.Position; if (index == 3) { index = 0; renderCollection.Add(new TriangleShape(triangle[0], triangle[1], triangle[2], mhBlueStuff)); } } } return(new UnboundCollection(renderCollection)); }
public void CorrectRayOnCircle() { var testPartPath = TestContext.CurrentContext.ResolveProjectPath(4, "examples", "RayTracerTest"); var testPart = Path.Combine(testPartPath, "circle_100x100_centered.stl"); PolygonMesh.Mesh simpleMesh = StlProcessing.Load(testPart, CancellationToken.None); var bvhCollection = MeshToBVH.Convert(simpleMesh); var scene = new Scene(); scene.shapes.Add(bvhCollection); RayTracer raytracer = new RayTracer() { AntiAliasing = AntiAliasing.None, MultiThreaded = false, }; int samples = 40; var advance = MathHelper.Tau / samples; TestSingleAngle(scene, raytracer, advance, 15); for (int i = 0; i < samples; i++) { TestSingleAngle(scene, raytracer, advance, i); } }
public static PolygonMesh.Mesh CreateCylinder(Cylinder.CylinderPrimitive cylinderToMeasure) { PolygonMesh.Mesh cylinder = new PolygonMesh.Mesh(); List <Vertex> bottomVerts = new List <Vertex>(); List <Vertex> topVerts = new List <Vertex>(); int count = 20; for (int i = 0; i < count; i++) { Vector2 bottomRadialPos = Vector2.Rotate(new Vector2(cylinderToMeasure.Radius1, 0), MathHelper.Tau * i / 20); Vertex bottomVertex = cylinder.CreateVertex(new Vector3(bottomRadialPos.x, bottomRadialPos.y, -cylinderToMeasure.Height / 2)); bottomVerts.Add(bottomVertex); Vector2 topRadialPos = Vector2.Rotate(new Vector2(cylinderToMeasure.Radius1, 0), MathHelper.Tau * i / 20); Vertex topVertex = cylinder.CreateVertex(new Vector3(topRadialPos.x, topRadialPos.y, cylinderToMeasure.Height / 2)); topVerts.Add(topVertex); } cylinder.ReverseFaceEdges(cylinder.CreateFace(bottomVerts.ToArray())); cylinder.CreateFace(topVerts.ToArray()); for (int i = 0; i < count - 1; i++) { cylinder.CreateFace(new Vertex[] { topVerts[i], bottomVerts[i], bottomVerts[i + 1], topVerts[i + 1] }); } cylinder.CreateFace(new Vertex[] { topVerts[count - 1], bottomVerts[count - 1], bottomVerts[0], topVerts[0] }); return(cylinder); }
private void AddTestStl() { Stopwatch loadTime = new Stopwatch(); loadTime.Start(); PolygonMesh.Mesh simpleMesh = StlProcessing.Load("Simple.stl"); //PolygonMesh.Mesh simpleMesh = StlProcessing.Load("Complex.stl"); //PolygonMesh.Mesh simpleMesh = StlProcessing.Load("bunny.stl"); //PolygonMesh.Mesh simpleMesh = StlProcessing.Load("bunny_binary.stl"); loadTime.Stop(); timingStrings.Add("Time to load STL {0:0.0}s".FormatWith(loadTime.Elapsed.TotalSeconds)); Stopwatch bvhTime = new Stopwatch(); bvhTime.Start(); IPrimitive bvhCollection = MeshToBVH.Convert(simpleMesh); bvhTime.Stop(); timingStrings.Add("Time to create BVH {0:0.0}s".FormatWith(bvhTime.Elapsed.TotalSeconds)); renderCollection.Add(bvhCollection); }
public PolygonMesh.Mesh GetMesh() { if (polygonMesh == null) { polygonMesh = MatterHackers.PolygonMesh.Processors.StlProcessing.Load(sourceFileName); } return(polygonMesh); }
public PolygonMesh.Mesh CsgToMeshRecursive(Difference objectToProcess) { PolygonMesh.Mesh primary = CsgToMeshRecursive((dynamic)objectToProcess.Primary); foreach (CsgObject objectToOutput in objectToProcess.AllSubtracts) { PolygonMesh.Mesh remove = CsgToMeshRecursive((dynamic)objectToOutput); primary = PolygonMesh.Csg.CsgOperations.Subtract(primary, remove); } return(primary); }
public PolygonMesh.Mesh CsgToMeshRecursive(Union objectToProcess) { PolygonMesh.Mesh primary = CsgToMeshRecursive((dynamic)objectToProcess.AllObjects[0]); for (int i = 1; i < objectToProcess.AllObjects.Count; i++) { PolygonMesh.Mesh add = CsgToMeshRecursive((dynamic)objectToProcess.AllObjects[i]); primary = PolygonMesh.Csg.CsgOperations.Union(primary, add); } return(primary); }
private void LoadStl_Click(object sender, EventArgs e) { OpenFileDialogParams opeParams = new OpenFileDialogParams("STL Files|*.stl"); FileDialog.OpenFileDialog(opeParams, (openParams) => { var streamToLoadFrom = File.Open(openParams.FileName, FileMode.Open); if (streamToLoadFrom != null) { var loadedFileName = openParams.FileName; meshToRender = StlProcessing.Load(streamToLoadFrom); ImageBuffer plateInventory = new ImageBuffer((int)(300 * 8.5), 300 * 11, 32, new BlenderBGRA()); Graphics2D plateGraphics = plateInventory.NewGraphics2D(); plateGraphics.Clear(RGBA_Bytes.White); double inchesPerMm = 0.0393701; double pixelsPerInch = 300; double pixelsPerMm = inchesPerMm * pixelsPerInch; AxisAlignedBoundingBox aabb = meshToRender.GetAxisAlignedBoundingBox(); Vector2 lowerLeftInMM = new Vector2(-aabb.minXYZ.x, -aabb.minXYZ.y); Vector3 centerInMM = (aabb.maxXYZ - aabb.minXYZ) / 2; Vector2 offsetInMM = new Vector2(20, 30); { RectangleDouble bounds = new RectangleDouble(offsetInMM.x * pixelsPerMm, offsetInMM.y * pixelsPerMm, (offsetInMM.x + aabb.maxXYZ.x - aabb.minXYZ.x) * pixelsPerMm, (offsetInMM.y + aabb.maxXYZ.y - aabb.minXYZ.y) * pixelsPerMm); bounds.Inflate(3 * pixelsPerMm); RoundedRect rect = new RoundedRect(bounds, 3 * pixelsPerMm); plateGraphics.Render(rect, RGBA_Bytes.LightGray); Stroke rectOutline = new Stroke(rect, .5 * pixelsPerMm); plateGraphics.Render(rectOutline, RGBA_Bytes.DarkGray); } OrthographicZProjection.DrawTo(plateGraphics, meshToRender, lowerLeftInMM + offsetInMM, pixelsPerMm); plateGraphics.DrawString(Path.GetFileName(openParams.FileName), (offsetInMM.x + centerInMM.x) * pixelsPerMm, (offsetInMM.y - 10) * pixelsPerMm, 50, Agg.Font.Justification.Center); //ImageBuffer logoImage = new ImageBuffer(); //ImageIO.LoadImageData("Logo.png", logoImage); //plateGraphics.Render(logoImage, (plateInventory.Width - logoImage.Width) / 2, plateInventory.Height - logoImage.Height - 10 * pixelsPerMm); //ImageIO.SaveImageData("plate Inventory.jpeg", plateInventory); } }); }
public static PolygonMesh.Mesh CreateBox(AxisAlignedBoundingBox aabb) { PolygonMesh.Mesh cube = new PolygonMesh.Mesh(); Vertex[] verts = new Vertex[8]; //verts[0] = cube.CreateVertex(new Vector3(-1, -1, 1)); //verts[1] = cube.CreateVertex(new Vector3(1, -1, 1)); //verts[2] = cube.CreateVertex(new Vector3(1, 1, 1)); //verts[3] = cube.CreateVertex(new Vector3(-1, 1, 1)); //verts[4] = cube.CreateVertex(new Vector3(-1, -1, -1)); //verts[5] = cube.CreateVertex(new Vector3(1, -1, -1)); //verts[6] = cube.CreateVertex(new Vector3(1, 1, -1)); //verts[7] = cube.CreateVertex(new Vector3(-1, 1, -1)); verts[0] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z)); verts[1] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z)); verts[2] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z)); verts[3] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z)); verts[4] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z)); verts[5] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z)); verts[6] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z)); verts[7] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z)); // front cube.CreateFace(new Vertex[] { verts[0], verts[1], verts[2], verts[3] }); // left cube.CreateFace(new Vertex[] { verts[4], verts[0], verts[3], verts[7] }); // right cube.CreateFace(new Vertex[] { verts[1], verts[5], verts[6], verts[2] }); // back cube.CreateFace(new Vertex[] { verts[4], verts[7], verts[6], verts[5] }); // top cube.CreateFace(new Vertex[] { verts[3], verts[2], verts[6], verts[7] }); // bottom cube.CreateFace(new Vertex[] { verts[4], verts[5], verts[1], verts[0] }); return(cube); }
public static PolygonMesh.Mesh CreateBox(AxisAlignedBoundingBox aabb) { PolygonMesh.Mesh cube = new PolygonMesh.Mesh(); Vertex[] verts = new Vertex[8]; //verts[0] = cube.CreateVertex(new Vector3(-1, -1, 1)); //verts[1] = cube.CreateVertex(new Vector3(1, -1, 1)); //verts[2] = cube.CreateVertex(new Vector3(1, 1, 1)); //verts[3] = cube.CreateVertex(new Vector3(-1, 1, 1)); //verts[4] = cube.CreateVertex(new Vector3(-1, -1, -1)); //verts[5] = cube.CreateVertex(new Vector3(1, -1, -1)); //verts[6] = cube.CreateVertex(new Vector3(1, 1, -1)); //verts[7] = cube.CreateVertex(new Vector3(-1, 1, -1)); verts[0] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z)); verts[1] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.z)); verts[2] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z)); verts[3] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.maxXYZ.z)); verts[4] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z)); verts[5] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.minXYZ.y, aabb.minXYZ.z)); verts[6] = cube.CreateVertex(new Vector3(aabb.maxXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z)); verts[7] = cube.CreateVertex(new Vector3(aabb.minXYZ.x, aabb.maxXYZ.y, aabb.minXYZ.z)); // front cube.CreateFace(new Vertex[] { verts[0], verts[1], verts[2], verts[3] }); // left cube.CreateFace(new Vertex[] { verts[4], verts[0], verts[3], verts[7] }); // right cube.CreateFace(new Vertex[] { verts[1], verts[5], verts[6], verts[2] }); // back cube.CreateFace(new Vertex[] { verts[4], verts[7], verts[6], verts[5] }); // top cube.CreateFace(new Vertex[] { verts[3], verts[2], verts[6], verts[7] }); // bottom cube.CreateFace(new Vertex[] { verts[4], verts[5], verts[1], verts[0] }); return cube; }
public PolygonMesh.Mesh CsgToMeshRecursive(TransformBase objectToProcess) { PolygonMesh.Mesh mesh = CsgToMeshRecursive((dynamic)objectToProcess.ObjectToTransform); mesh.Transform(objectToProcess.ActiveTransform); return(mesh); }
public PolygonMesh.Mesh GetMesh() { if (polygonMesh == null) { polygonMesh = MatterHackers.PolygonMesh.Processors.StlProcessing.Load(sourceFileName); } return polygonMesh; }
public Mesh(MatterHackers.PolygonMesh.Mesh polygonMesh, string name = "") : base(name) { this.polygonMesh = polygonMesh; }
public static PolygonMesh.Mesh CreateCylinder(Cylinder.CylinderPrimitive cylinderToMeasure) { PolygonMesh.Mesh cylinder = new PolygonMesh.Mesh(); List<Vertex> bottomVerts = new List<Vertex>(); List<Vertex> topVerts = new List<Vertex>(); int count = 20; for (int i = 0; i < count; i++) { Vector2 bottomRadialPos = Vector2.Rotate(new Vector2(cylinderToMeasure.Radius1, 0), MathHelper.Tau * i / 20); Vertex bottomVertex = cylinder.CreateVertex(new Vector3(bottomRadialPos.x, bottomRadialPos.y, -cylinderToMeasure.Height / 2)); bottomVerts.Add(bottomVertex); Vector2 topRadialPos = Vector2.Rotate(new Vector2(cylinderToMeasure.Radius1, 0), MathHelper.Tau * i / 20); Vertex topVertex = cylinder.CreateVertex(new Vector3(topRadialPos.x, topRadialPos.y, cylinderToMeasure.Height / 2)); topVerts.Add(topVertex); } cylinder.ReverseFaceEdges(cylinder.CreateFace(bottomVerts.ToArray())); cylinder.CreateFace(topVerts.ToArray()); for (int i = 0; i < count - 1; i++) { cylinder.CreateFace(new Vertex[] { topVerts[i], bottomVerts[i], bottomVerts[i + 1], topVerts[i + 1] }); } cylinder.CreateFace(new Vertex[] { topVerts[count - 1], bottomVerts[count - 1], bottomVerts[0], topVerts[0] }); return cylinder; }