コード例 #1
0
ファイル: RenderEngine.cs プロジェクト: Sidd710/tempRepo
        internal static List <SlicePolyLine3D> GetZIntersections(List <Triangle> triangles, float zcur)
        {
            try
            {
                SlicePolyLine3D s3d      = null;
                var             lstlines = new List <SlicePolyLine3D>();

                for (var triangleIndex = 0; triangleIndex < triangles.Count; triangleIndex++)
                {
                    var triangle = triangles[triangleIndex];
                    s3d = triangle.IntersectZPlane(zcur);
                    if (s3d != null)
                    {
                        s3d.Normal             = triangle.Normal;
                        s3d.TriangleConnection = triangle.Index;
                        lstlines.Add(s3d);
                    }
                }
                return(lstlines);
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #2
0
 public static List <SlicePolyLine3D> GetZIntersections(List <Triangle> triangles, float zcur)
 {
     try
     {
         SlicePolyLine3D s3d      = null;
         var             lstlines = new List <SlicePolyLine3D>();
         foreach (var triangle in triangles)
         {
             s3d = triangle.IntersectZPlane(zcur);
             if (s3d != null)
             {
                 lstlines.Add(s3d);
             }
         }
         return(lstlines);
     }
     catch (Exception)
     {
         return(null);
     }
 }
コード例 #3
0
        public static void RenderSliceToPolygons(Slices.Slice slice, Slices.RenderSliceInfo renderSlice)
        {
            var pixelValues = new byte[renderSlice.PrinterResolutionX * renderSlice.PrinterResolutionY];
            var pixelLength = pixelValues.Length;
            //        Array.Clear(pixelValues, 0, pixelLength);

            int          beginPixel      = 0;
            int          beginPixelOfRow = 0;
            int          endPixel        = 0;
            int          endPixelOfRow   = 0;
            SlicePoint2D p1;
            SlicePoint2D p2;

            //model points
            if (slice.ModelPoints.Count % 2 == 0)
            {
                for (int cnt = 0; cnt < slice.ModelPoints.Count; cnt += 2)      // increment by 2
                {
                    p1 = slice.ModelPoints[cnt];
                    p2 = slice.ModelPoints[cnt + 1];
                    if (p1.X > p2.X)     //flip over
                    {
                        var pTemp = p1;
                        p1 = p2;
                        p2 = pTemp;
                    }
                    p1.X++;

                    if (p1.X <= p2.X)
                    {
                        if (p2.Y >= 0 && p1.Y >= 0)
                        {
                            beginPixel      = (p1.Y * renderSlice.PrinterResolutionX + p1.X);
                            beginPixelOfRow = (((p1.Y) * renderSlice.PrinterResolutionX));
                            if (p1.X < 0)
                            {
                                beginPixel = (p1.Y * renderSlice.PrinterResolutionX);
                            }

                            //get end pixel of row
                            endPixel      = ((p2.Y * renderSlice.PrinterResolutionX + p2.X));
                            endPixelOfRow = (((p1.Y + 1) * renderSlice.PrinterResolutionX)) - 1;

                            if (endPixel > endPixelOfRow)
                            {
                                endPixel = endPixelOfRow;
                            }

                            if (pixelValues.Length - 1 >= beginPixel && beginPixel >= 0)
                            {
                                pixelValues[beginPixel] = 255;
                            }

                            if (p2.X < 0)
                            {
                                endPixel = ((p2.Y * renderSlice.PrinterResolutionX));
                            }

                            //fill color between begin and endpoints
                            if (pixelValues.Length - 1 >= endPixel && endPixel >= 0)
                            {
                                pixelValues[endPixel] = 255;

                                if (beginPixel < endPixel)
                                {
                                    for (var betweenPixel = beginPixel + 1; betweenPixel < endPixel; betweenPixel++)
                                    {
                                        if (betweenPixel >= 0)
                                        {
                                            pixelValues[betweenPixel] = 255;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (slice.SupportPoints.Count % 2 == 0)                          // is even
            {
                for (int cnt = 0; cnt < slice.SupportPoints.Count; cnt += 2) // increment by 2
                {
                    p1 = slice.SupportPoints[cnt];
                    p2 = slice.SupportPoints[cnt + 1];
                    if (p1.X > p2.X)     //flip over
                    {
                        var pTemp = p1;
                        p1 = p2;
                        p2 = pTemp;
                    }
                    p1.X++;

                    if (p1.X <= p2.X)
                    {
                        if (p2.Y >= 0 && p1.Y >= 0)
                        {
                            beginPixel      = (p1.Y * renderSlice.PrinterResolutionX + p1.X);
                            beginPixelOfRow = (((p1.Y) * renderSlice.PrinterResolutionX));
                            if (p1.X < 0)
                            {
                                beginPixel = (p1.Y * renderSlice.PrinterResolutionX);
                            }

                            //get end pixel of row
                            endPixel      = ((p2.Y * renderSlice.PrinterResolutionX + p2.X));
                            endPixelOfRow = (((p1.Y + 1) * renderSlice.PrinterResolutionX));

                            if (endPixel > endPixelOfRow)
                            {
                                endPixel = endPixelOfRow;
                            }

                            if (pixelValues.Length - 1 >= beginPixel && beginPixel >= 0)
                            {
                                pixelValues[beginPixel] = 255;
                            }

                            if (p2.X < 0)
                            {
                                endPixel = ((p2.Y * renderSlice.PrinterResolutionX));
                            }

                            if (pixelValues.Length - 1 >= endPixel && endPixel >= 0)
                            {
                                pixelValues[endPixel] = 255;

                                if (beginPixel < endPixel)
                                {
                                    for (var betweenPixel = beginPixel + 1; betweenPixel < endPixel; betweenPixel++)
                                    {
                                        if (betweenPixel >= 0)
                                        {
                                            pixelValues[betweenPixel] = 255;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            else      // flag error
            {
                Console.WriteLine("Row y=" + " odd # of points = " + slice.SupportPoints.Count + " - Model may have holes");
            }

            //convert pixel values to polylines
            for (var pixelRowIndex = 0; pixelRowIndex < renderSlice.PrinterResolutionY; pixelRowIndex++)
            {
                var beginRowIndex       = renderSlice.PrinterResolutionX * pixelRowIndex;
                var startOfPolygon      = false;
                var startOfPolygonIndex = 0;
                var endOfPolygonIndex   = 0;

                for (var pixelColumnIndex = 0; pixelColumnIndex < renderSlice.PrinterResolutionX; pixelColumnIndex++)
                {
                    if (pixelValues[beginRowIndex + pixelColumnIndex] == 255)
                    {
                        if (!startOfPolygon)
                        {
                            startOfPolygon      = true;
                            startOfPolygonIndex = pixelColumnIndex;
                        }
                    }
                    else if (pixelValues[beginRowIndex + pixelColumnIndex] == 0)
                    {
                        if (startOfPolygon)
                        {
                            endOfPolygonIndex = pixelColumnIndex;
                            startOfPolygon    = false;

                            //add polygon to return list
                            var slicePolygon = new SlicePolyLine3D();
                            slicePolygon.Points.Add(new Vector3(startOfPolygonIndex, pixelRowIndex, renderSlice.SliceHeight));
                            slicePolygon.Points.Add(new Vector3(endOfPolygonIndex, pixelRowIndex, renderSlice.SliceHeight));
                            SimulationPolyLines.Add(slicePolygon);
                        }
                    }
                }
            }



            lock (_totalProcessedSlicesLock)
            {
                TotalProcessedSlices++;
            }

            if (TotalProcessedSlices == TotalAmountSlices)
            {
                if (SimulationPrintJobCompleted != null)
                {
                    //convert polygons to vectors
                    STLModel           = new STLModel3D(STLModel3D.TypeObject.Model, true);
                    STLModel.Triangles = new TriangleInfoList();

                    var triangleIndex = 0;
                    var arrayIndex    = 0;
                    foreach (var polyLine in SimulationPolyLines)
                    {
                        if (polyLine != null)
                        {
                            var polygonCube = new Shapes.Cube(polyLine.Points[1].X - polyLine.Points[0].X + 1, polyLine.Points[1].Y - polyLine.Points[0].Y + 1, 1);
                            //Console.WriteLine(polygonCube.Center);
                            for (var cubeVertexIndex = 0; cubeVertexIndex < polygonCube.VertexArray.Length; cubeVertexIndex += 3)
                            {
                                var triangle = new Triangle();
                                triangle.Normal = polygonCube.VertexArray[cubeVertexIndex].Normal;

                                triangle.Vectors[0].Position = new Vector3(-48, -20, 0);
                                triangle.Vectors[0].Normal   = triangle.Vectors[1].Normal = triangle.Vectors[2].Normal = triangle.Normal;
                                triangle.Vectors[0].Color    = triangle.Vectors[1].Color = triangle.Vectors[2].Color = new Byte4()
                                {
                                    A = 128, B = 255
                                };
                                triangle.Vectors[1].Position = new Vector3(-48, -20, 0);
                                triangle.Vectors[2].Position = new Vector3(-48, -20, 0);


                                triangle.Vectors[0].Position += polygonCube.VertexArray[cubeVertexIndex].Position / 20;
                                triangle.Vectors[1].Position += polygonCube.VertexArray[cubeVertexIndex + 1].Position / 20;
                                triangle.Vectors[2].Position += polygonCube.VertexArray[cubeVertexIndex + 2].Position / 20;

                                //add 0 point
                                triangle.Vectors[0].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);
                                triangle.Vectors[1].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);
                                triangle.Vectors[2].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);

                                if (triangleIndex > 33333)
                                {
                                    IsReadyForRendering = true;
                                    break;
                                    triangleIndex = 0;
                                    arrayIndex++;
                                    STLModel.Triangles.Add(new List <Triangle>());
                                }

                                triangleIndex++;
                                STLModel.Triangles[arrayIndex].Add(triangle);
                            }
                        }
                    }

                    IsReadyForRendering = true;
                    SimulationPrintJobCompleted(null, null);
                }
            }
        }