void InitContext()
        {
            for (int i = -5; i <= 5; ++i)
            {
                Spline spline1 = new Spline();
                spline1.Add(new Base3D.Point {
                    X = 5, Y = i, Z = 0
                });
                spline1.Add(new Base3D.Point {
                    X = -5, Y = i, Z = 0
                });
                context.world.control.Add(spline1);
                Spline spline2 = new Spline();
                spline2.Add(new Base3D.Point {
                    X = i, Y = 5, Z = 0
                });
                spline2.Add(new Base3D.Point {
                    X = i, Y = -5, Z = 0
                });
                context.world.control.Add(spline2);
            }
            Spline xSpline = new Spline();

            xSpline.Matreial = new SolidMaterial(Color.DarkRed);
            xSpline.Add(new Base3D.Point {
                X = 0
            });
            xSpline.Add(new Base3D.Point {
                X = 1
            });
            context.world.control.Add(xSpline);

            Spline ySpline = new Spline();

            ySpline.Matreial = new SolidMaterial(Color.DarkBlue);
            ySpline.Add(new Base3D.Point {
                Y = 0
            });
            ySpline.Add(new Base3D.Point {
                Y = 1
            });
            context.world.control.Add(ySpline);

            Spline zSpline = new Spline();

            zSpline.Matreial = new SolidMaterial(Color.DarkGreen);
            zSpline.Add(new Base3D.Point {
                Z = 0
            });
            zSpline.Add(new Base3D.Point {
                Z = 1
            });
            context.world.control.Add(zSpline);
        }
示例#2
0
        public void InterpolationSpline()
        {
            var _interpolation = new Spline();

            _interpolation.Add(-6, 2);
            _interpolation.Add(2, -4);
            _interpolation.Add(6, 6);
            Assert.AreEqual(-5.344, _interpolation.Interpolate(0), 0.01, "Spline interpolation at x=0 failed!");
            Assert.AreEqual(0, _interpolation.Interpolate(-4.897), 0.01, "Spline interpolation at x=-4.897 failed!");
            Assert.AreEqual(0, _interpolation.Interpolate(3.928), 0.01, "Spline interpolation at x=3.928 failed!");
            Assert.AreEqual(-5.027, _interpolation.Interpolate(1), 0.01, "Spline interpolation at x=1 failed!");
        }
示例#3
0
        /// <summary>
        /// Creates a single spline from a generic curve.
        /// </summary>
        public Spline ToSpline()
        {
            // Select the curve
            AddToSelection(true);

            // Write to a pic file
            FileSystem.File tempFile = FileSystem.File.CreateTemporaryFile("pic");
            PSAutomation.ActiveModel.Export(tempFile, ExportItemsOptions.Selected);

            // Geth the data from the pic file
            List <Spline> splineList = Spline.ReadFromDUCTPictureFile(tempFile);

            // create a single spline from the data
            Spline mySpline = new Spline();

            foreach (Spline s in splineList)
            {
                foreach (SplinePoint sp in s)
                {
                    mySpline.Add(sp);
                }
            }

            // Delete the file
            tempFile.Delete();

            // Return the model
            return(mySpline);
        }
示例#4
0
        Spline GeneratePoint()
        {
            Spline spline = new Spline();

            spline.Add(new Base3D.Point());
            return(spline);
        }
示例#5
0
        public void ReverseTest2()
        {
            Spline spline = new Spline();

            spline.Add(new SplinePoint(new Point(0, 0, 0), new Vector(0.5, 0.5, 0.5), 3, new Vector(0.6, 0.6, 0.6), 5));
            spline.Add(new SplinePoint(new Point(10, 20, 30), new Vector(0, 1, 0), 3, new Vector(0, 1, 0), 5));
            spline.Add(new SplinePoint(new Point(20, 40, 50), new Vector(-1, -1, -1), 3, new Vector(-0.9, -0.9, -0.9), 5));

            spline.Reverse(1, 2);

            Assert.AreEqual(3, spline.Count);

            Assert.AreEqual((MM)0.0, spline[2].DirectionBefore.I);
            Assert.AreEqual((MM)(-1), spline[2].DirectionBefore.J);
            Assert.AreEqual((MM)0, spline[2].DirectionBefore.K);

            Assert.AreEqual((MM)0.6, spline[0].DirectionAfter.I);
            Assert.AreEqual((MM)0.6, spline[0].DirectionAfter.J);
            Assert.AreEqual((MM)0.6, spline[0].DirectionAfter.K);
        }
示例#6
0
        public Spline ToSpline()
        {
            Spline spline = new Spline();
            spline.Name = Name;

            for (int i = 0; i < Points.Count; i++)
            {
                spline.Add(Points[i].ToSplinePoint());
            }
            spline.Visible = this.Visible;
            return spline;
        }
示例#7
0
        public void BezierCurveTest()
        {
            Point startPoint   = new Point();
            Point startControl = new Point(1, 2, 3);
            Point endPoint     = new Point(4, 5, 6);
            Point endControl   = new Point(3, 3, 4);

            SplinePoint splineStartPoint = new SplinePoint(startPoint, null, startControl);
            SplinePoint splineEndPoint   = new SplinePoint(endPoint, endControl, null);
            Spline      spline           = new Spline();

            spline.Add(splineStartPoint);
            spline.Add(splineEndPoint);

            CubicBezier splineAsBezier = spline.GetBezierCurve(0);

            Assert.AreEqual(startPoint, splineAsBezier.StartPoint);
            Assert.AreEqual(startControl, splineAsBezier.StartControlPoint);
            Assert.AreEqual(endPoint, splineAsBezier.EndPoint);
            Assert.AreEqual(endControl, splineAsBezier.EndControlPoint);
        }
示例#8
0
        public Spline ToSpline()
        {
            Spline spline = new Spline();

            spline.Name = Name;

            for (int i = 0; i < Points.Count; i++)
            {
                spline.Add(Points[i].ToSplinePoint());
            }
            spline.Visible = this.Visible;
            return(spline);
        }
示例#9
0
        private void clearAndDrawSpline(IEnumerable <CubicBezier> curves, bool colorize)
        {
            try
            {
                // Spline throws exceptions sometimes, so wrap it in a try/catch
                _spline.Clear();
                foreach (CubicBezier c in curves)
                {
                    _spline.Add(c);
                }
            }
            catch (Exception e)
            {
                _log.error(e);
                clearDrawing();
                return;
            }

            if (_spline.Length == 0)
            {
                clearDrawing();
            }
            else
            {
                using (DrawingContext ctx = _drawing.Open())
                {
                    clearDrawing(ctx);
                    int   nPoints = (int)Math.Round(_spline.Length / SPLINE_POINT_DISTANCE);
                    float ratio   = 1f / (nPoints - 1);
                    for (int i = 0; i < nPoints; i++)
                    {
                        float            u     = i * ratio;
                        Spline.SamplePos pos   = _spline.GetSamplePosition(u);
                        VECTOR           p     = _spline.Curves[pos.Index].Sample(pos.Time);
                        Brush            brush = colorize ? _partBrushes[pos.Index % _partBrushes.Length] : _partBrushes[0];
                        drawPoint(ctx, p, brush, DEFAULT_POINT_RADIUS);
                    }
                }
            }
        }
        /// <summary>
        /// Extracts Polylines from a DUCT picture file and returns them as a list.
        /// </summary>
        /// <param name="file">DUCT picture file.</param>
        /// <returns>List of polylines.</returns>
        public static List <Polyline> ReadFromDUCTPictureFile(File file)
        {
            List <Polyline> polylines = new List <Polyline>();

            string CurCult = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;

            try
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");

                string strBuff = "";
                using (
                    System.IO.FileStream FS = new System.IO.FileStream(file.Path,
                                                                       System.IO.FileMode.Open,
                                                                       System.IO.FileAccess.Read))
                {
                    using (System.IO.StreamReader SR = new System.IO.StreamReader(FS))
                    {
                        strBuff = SR.ReadToEnd();
                        SR.Close();
                    }
                    FS.Close();
                }
                strBuff = strBuff.Replace(Strings.Chr(9).ToString(), "");
                strBuff = strBuff.Replace(Strings.Chr(13).ToString(), "");
                string[] strSpl = strBuff.Split(Strings.Chr(10));

                List <List <int> > instructions = new List <List <int> >();
                List <Point>       points       = new List <Point>();

                // Check to see if the file is in inches or mm
                bool isInInches = strSpl[1].Trim().ToLower().EndsWith("inches");

                // Ignore the header lines just concentrate on the juicy bits
                for (int lineIndex = 5; lineIndex <= strSpl.Length - 1; lineIndex++)
                {
                    // Collect up all the numeric values on the line
                    List <double> elementsOnLine = new List <double>();
                    foreach (string stringElement in strSpl[lineIndex].Split(' '))
                    {
                        double value = 0;
                        if (double.TryParse(stringElement,
                                            NumberStyles.Any,
                                            CultureInfo.InvariantCulture,
                                            out value))
                        {
                            elementsOnLine.Add(value);
                        }
                    }
                    if (elementsOnLine.Count == 2)
                    {
                        // If there are two values then it is an instruction line
                        instructions.Add(new List <int>
                        {
                            Convert.ToInt32(elementsOnLine[0]),
                            Convert.ToInt32(elementsOnLine[1])
                        });
                    }
                    else if (elementsOnLine.Count >= 3)
                    {
                        // Otherwise it is a point data line
                        if (isInInches)
                        {
                            points.Add(new Point(elementsOnLine[0] * 25.4, elementsOnLine[1] * 25.4, elementsOnLine[2] * 25.4));
                        }
                        else
                        {
                            points.Add(new Point(elementsOnLine[0], elementsOnLine[1], elementsOnLine[2]));
                        }
                    }
                }

                // So we have all the data, now we need to populate the polyline and spline lists

                // This keeps track of which point we are looking at for the current instruction
                int pointIndex = 0;

                for (int instructionIndex = 0; instructionIndex <= instructions.Count - 1; instructionIndex++)
                {
                    if ((instructions[instructionIndex][0] & 1024) == 1024)
                    {
                        // Bezier curves
                        Spline spline     = new Spline();
                        int    pixelIndex = 1;
                        if ((instructions[instructionIndex][0] & 24) == 24)
                        {
                            pixelIndex = -1;
                        }
                        while (pixelIndex < instructions[instructionIndex][1])
                        {
                            if (pixelIndex == -1)
                            {
                                // First pixel only has tangency out
                                Vector directionOut = points[pointIndex + pixelIndex + 1] - points[pointIndex + pixelIndex];
                                double distanceOut  = directionOut.Magnitude;
                                directionOut.Normalize();
                                spline.Add(new SplinePoint(points[pointIndex + pixelIndex],
                                                           directionOut,
                                                           distanceOut,
                                                           directionOut,
                                                           distanceOut));
                                pixelIndex += 3;
                            }
                            else if (instructions[instructionIndex][1] - pixelIndex == 1)
                            {
                                // Last pixel only has tangency in
                                Vector directionIn = points[pointIndex + pixelIndex] - points[pointIndex + pixelIndex - 1];
                                double distanceIn  = directionIn.Magnitude;
                                directionIn.Normalize();
                                spline.Add(new SplinePoint(points[pointIndex + pixelIndex],
                                                           directionIn,
                                                           distanceIn,
                                                           directionIn,
                                                           distanceIn));
                                pixelIndex += 3;
                            }
                            else
                            {
                                // Pixel has tangency in and out
                                Vector directionIn = points[pointIndex + pixelIndex] - points[pointIndex + pixelIndex - 1];
                                double distanceIn  = directionIn.Magnitude;
                                directionIn.Normalize();
                                Vector directionOut = points[pointIndex + pixelIndex + 1] - points[pointIndex + pixelIndex];
                                double distanceOut  = directionOut.Magnitude;
                                directionOut.Normalize();
                                spline.Add(new SplinePoint(points[pointIndex + pixelIndex],
                                                           directionIn,
                                                           distanceIn,
                                                           directionOut,
                                                           distanceOut));
                                pixelIndex += 3;
                            }
                        }
                        if ((instructions[instructionIndex][0] & 24) == 0)
                        {
                            // Starting a new section
                            Polyline polyline = new Polyline(spline, 0.01);
                            polylines.Add(polyline);
                        }
                        else
                        {
                            // Continuing the last section
                            Polyline polyline = new Polyline(spline, 0.01);

                            // Add all points apart from the first one
                            for (int i = 1; i <= polyline.Count - 1; i++)
                            {
                                polylines[polylines.Count - 1].Add(polyline[i]);
                            }
                        }
                        pointIndex += instructions[instructionIndex][1];
                    }
                    else if ((instructions[instructionIndex][0] & 512) == 512)
                    {
                        // Conic arcs
                        Spline intermediateCurve = new Spline();
                        Spline spline            = new Spline();
                        int    pixelIndex        = 0;
                        if ((instructions[instructionIndex][0] & 24) == 24)
                        {
                            pixelIndex = -1;
                        }
                        while (pixelIndex < instructions[instructionIndex][1])
                        {
                            intermediateCurve.Add(new SplinePoint(points[pointIndex + pixelIndex]));

                            // If there are three points on the curve
                            if (intermediateCurve.Count == 3)
                            {
                                intermediateCurve.FreeTangentsAndMagnitudes();
                                if (spline.Count != 0)
                                {
                                    // If the spline curve already has points then dont add the first one, just set the output tangent and magnitude
                                    spline.Last().MagnitudeAfter = intermediateCurve[0].MagnitudeAfter;
                                    spline.Last().DirectionAfter = intermediateCurve[0].DirectionAfter;
                                }
                                else
                                {
                                    // else add first point
                                    spline.Add(intermediateCurve[0]);
                                }

                                // add second and third point
                                spline.Add(intermediateCurve[1]);
                                spline.Add(intermediateCurve[2]);
                                var p = intermediateCurve[2];

                                // reset intermediate curve and add last point
                                intermediateCurve.Clear();
                                intermediateCurve.Add(p.Clone());
                            }

                            pixelIndex += 1;
                        }
                        if ((instructions[instructionIndex][0] & 24) == 0)
                        {
                            // Starting a new section
                            Polyline polyline = new Polyline(spline, 0.01);
                            polylines.Add(polyline);
                        }
                        else
                        {
                            // Continuing the last section
                            Polyline polyline = new Polyline(spline, 0.01);

                            // Add all points apart from the first one
                            for (int i = 1; i <= polyline.Count - 1; i++)
                            {
                                polylines[polylines.Count - 1].Add(polyline[i]);
                            }
                        }
                        pointIndex += instructions[instructionIndex][1];
                    }
                    else
                    {
                        // Polylines
                        if ((instructions[instructionIndex][0] & 24) == 0)
                        {
                            // Starting a new section
                            Polyline polyline =
                                new Polyline(
                                    new List <Point>(points.GetRange(pointIndex, instructions[instructionIndex][1]).ToArray()));
                            polylines.Add(polyline);
                        }
                        else
                        {
                            // Continuing the last section
                            polylines[polylines.Count - 1].AddRange(
                                points.GetRange(pointIndex, instructions[instructionIndex][1]).ToArray());
                        }
                        pointIndex += instructions[instructionIndex][1];
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(CurCult);
            }

            return(polylines);
        }
示例#11
0
文件: Program.cs 项目: nopcoder/rx
        public static Chart Lines3D()
            {
                var series1 = new Spline();
                var series2 = new Spline();
                var series3 = new Spline();

                // imperative
                var random = new Random();
                for (var pointIndex = 0; pointIndex < 10; pointIndex++)
                {
                    series1.Add(random.Next(45, 95));
                    series2.Add(random.Next(5, 75));
                    series3.Add(random.Next(2, 50));
                }

                var area = new System.Linq.Charting.ChartArea
                {
                    Area3DStyle =
                    {
                        Enable3D = true
                    ,
                        Inclination = 38
                    ,
                        Rotation = 9
                    ,
                        Perspective = 10
                    ,
                        PointDepth = 200
                    ,
                        LightStyle = System.Windows.Forms.DataVisualization.Charting.LightStyle.Realistic
                    ,
                        IsRightAngleAxes = false
                    ,
                        WallWidth = 0
                    }
                ,
                    AxisX = { IsMarginVisible = true }
                ,
                    Series = { series1, series2, series3 }
                };

                var chart = new System.Linq.Charting.Chart
                {
                    ChartAreas = { area }
                ,
                    Titles = { new System.Windows.Forms.DataVisualization.Charting.Title{ Text = "Three 3D Lines"
					                          , Font = new Font(FontFamily.GenericSansSerif, 20) 
					                          }
			                        }
                ,
                    Dock = DockStyle.Fill
                };
                return chart;

            }
示例#12
0
        internal void AddSplinePoint()
        {
            Spline spline = EditorData.EditorLogic.CurrentSpline;

            if (spline == null)
            {
                MessageBox.Show("Select a spline to add points to");
            }
            else
            {
                SplinePoint newSplinePoint = new SplinePoint();
                if (spline.Count > 1)
                {
                    SplinePoint pointBefore = spline[spline.Count - 1];

                    newSplinePoint.Time = pointBefore.Time + 1;

                    if (spline.Count == 1)
                    {
                        newSplinePoint.Position    = pointBefore.Position;
                        newSplinePoint.Position.X += 30 / SpriteManager.Camera.PixelsPerUnitAt(newSplinePoint.Position.Z);
                    }
                    else
                    {
                        SplinePoint pointBeforePointBefore = spline[spline.Count - 2];

                        Vector3 difference = pointBefore.Position - pointBeforePointBefore.Position;

                        if (difference == Vector3.Zero)
                        {
                            newSplinePoint.Position    = pointBefore.Position;
                            newSplinePoint.Position.X += 30 / Camera.Main.PixelsPerUnitAt(0);
                        }
                        else
                        {
                            newSplinePoint.Position = pointBefore.Position + difference;
                        }
                    }
                }
                else
                {
                    newSplinePoint.Position.X = SpriteManager.Camera.X;
                    newSplinePoint.Position.Y = SpriteManager.Camera.Y;

                    if (spline.Count > 0)
                    {
                        newSplinePoint.Time = spline[0].Time + 1;

                        newSplinePoint.Position    = spline[0].Position;
                        newSplinePoint.Position.X += 30 / Camera.Main.PixelsPerUnitAt(0);
                    }
                }
                spline.Add(newSplinePoint);
                spline.CalculateVelocities();
                spline.CalculateAccelerations();
                spline.CalculateDistanceTimeRelationships(.1f);

                GuiData.PropertyGrid.Refresh();
                GuiData.SplineListDisplay.UpdateToList();
            }
            //if (AfterNewPointAdded != null)
            //{
            //    AfterNewPointAdded(this);
            //}
        }