Пример #1
0
        //This method cuts the inputcurve into segments
        public static List <Curve> cutCurve(Curve curve, Plane plane, List <Plane> cuttingPlanes, Axis axis)
        {
            Rhino.RhinoApp.WriteLine("jee");
            //Initializes the list where the cutted curves are added
            List <Curve> cutCurveList = new List <Curve>();
            //Transform curve to the local coordinates
            Transform localCoordinates = Transform.PlaneToPlane(plane, Plane.WorldXY);

            curve.Transform(localCoordinates);

            List <double> parameters        = new List <double>();
            List <Curve>  validCurves       = new List <Curve>();
            List <Curve>  invalidCurves     = new List <Curve>();
            List <Curve>  straightCurveList = new List <Curve>();
            Curve         remainingCurve    = curve;

            foreach (Plane cutPlane in cuttingPlanes)
            {
                parameters.Clear();
                validCurves.Clear();
                invalidCurves.Clear();
                straightCurveList.Clear();
                Rhino.Geometry.Intersect.CurveIntersections intersections = Rhino.Geometry.Intersect.Intersection.CurvePlane(remainingCurve, cutPlane, 0.001);

                if (intersections != null && intersections.Count % 2 == 0)
                {
                    foreach (Rhino.Geometry.Intersect.IntersectionEvent intEvent in intersections)
                    {
                        parameters.Add(intEvent.ParameterA);
                    }

                    Curve[] curveList = remainingCurve.Split(parameters);
                    foreach (Curve cuttedCurve in curveList)
                    {
                        if (compareCurve(cuttedCurve, axis, cutPlane))
                        {
                            validCurves.Add(cuttedCurve);
                        }
                        else
                        {
                            invalidCurves.Add(cuttedCurve);
                        }
                    }



                    Point3d[] intPoints = sortPoints(parameters, remainingCurve, axis);

                    for (int i = 0; i < intPoints.Count(); i += 2)
                    {
                        Curve straightCurve = new Line(intPoints[i], intPoints[i + 1]).ToNurbsCurve();
                        straightCurveList.Add(straightCurve);
                        //Rhino.RhinoApp.WriteLine(intersections[i+1].ParameterA.ToString());
                        //Rhino.RhinoApp.WriteLine(straightCurve.ToString());

                        Curve[] joinedCurves = Curve.JoinCurves(validCurves.ToArray().Union(new Curve[] { straightCurve }));

                        foreach (Curve testCurve in joinedCurves)
                        {
                            if (testCurve.IsClosed)
                            {
                                cutCurveList.Add(testCurve);
                                break;
                            }
                        }
                    }


                    remainingCurve = Curve.JoinCurves(invalidCurves.Union(straightCurveList))[0];
                }
            }
            cutCurveList.Add(remainingCurve);

            return(cutCurveList);
        }
        //This method cuts the inputcurve into segments
        public static List <Curve> cutCurve(Curve curve, Plane plane, Axis axis)
        {
            //Finds the min and max range where the cuts are made
            Tuple <Point3d, Point3d> minAndMax = getMinAndMax(curve, plane);

            //Creates the cutting planes
            List <Plane> cuttingPlanes = getCuttingPlanes(minAndMax.Item1, minAndMax.Item2, plane, axis);

            //Initializes the list where the cutted curves are added
            List <Curve> cutCurveList = new List <Curve>();
            //Transform curve to the local coordinates
            Transform localCoordinates = Transform.PlaneToPlane(plane, Plane.WorldXY);

            curve.Transform(localCoordinates);


            List <double> parameters = new List <double>(); // Curve parameters that intersect with the cutting plane
            // Curves that come out of the splitting process as curves that are not part of that particular joining cycle
            List <Curve> validCurves = new List <Curve>();
            // Curves that come out of the splitting process as curves that are not part of that particular joining cycle
            List <Curve> invalidCurves     = new List <Curve>();
            List <Curve> straightCurveList = new List <Curve>(); // curves that connect the splitted parts in the main geometry

            Curve[]      remainingCurves = { curve };            //the curves that still need to be split
            List <Curve> remainingTemp   = new List <Curve>();


            foreach (Plane cutPlane in cuttingPlanes)
            {
                remainingTemp.Clear();
                foreach (Curve remainingCurve in remainingCurves)
                {
                    //Initialize the lists for this cycle
                    parameters.Clear();
                    validCurves.Clear();
                    invalidCurves.Clear();
                    straightCurveList.Clear();
                    Rhino.Geometry.Intersect.CurveIntersections intersections = Rhino.Geometry.Intersect.Intersection.CurvePlane(remainingCurve, cutPlane, 0.001);

                    if (intersections != null && intersections.Count % 2 == 0)
                    {
                        //gets parameters on the curve form the intersection events
                        foreach (Rhino.Geometry.Intersect.IntersectionEvent intEvent in intersections)
                        {
                            parameters.Add(intEvent.ParameterA);
                        }


                        //Checks if curve is higher or lower than the cutting plane and saves the curves in to different lists
                        Curve[] curveList = remainingCurve.Split(parameters);
                        foreach (Curve cuttedCurve in curveList)
                        {
                            if (compareCurve(cuttedCurve, axis, cutPlane))
                            {
                                validCurves.Add(cuttedCurve);
                            }
                            else
                            {
                                invalidCurves.Add(cuttedCurve);
                            }
                        }


                        //sorts the intersection points from lowest to highest according to axis selected
                        Point3d[] intPoints = sortPoints(parameters, remainingCurve, axis);

                        //creates lines between the points
                        for (int i = 0; i < intPoints.Count(); i += 2)
                        {
                            Curve straightCurve = new Line(intPoints[i], intPoints[i + 1]).ToNurbsCurve();
                            straightCurveList.Add(straightCurve);
                        }

                        //Creates closed curves from the curves that are lower than the cutting plane
                        Curve[] joinedCurves = Curve.JoinCurves(validCurves.ToArray().Union(straightCurveList));
                        if (findClosedCurve(joinedCurves).Item1)
                        {
                            cutCurveList.AddRange(findClosedCurve(joinedCurves).Item2);
                        }



                        //join the remaining curves to form a closed curve for later splitting
                        Curve[] joinedRemainingCurves = Curve.JoinCurves(invalidCurves.Union(straightCurveList));
                        if (findClosedCurve(joinedRemainingCurves).Item1)
                        {
                            remainingTemp.AddRange(findClosedCurve(joinedRemainingCurves).Item2);
                        }
                    }
                }
                // changes the list into array
                if (remainingTemp.Count != 0)
                {
                    remainingCurves = remainingTemp.ToArray();
                }
            }
            cutCurveList.AddRange(remainingCurves);

            return(cutCurveList);
        }
Пример #3
0
        public static Line PickLine(this List <Line> lines, string method, Random random = null, List <Curve> roads = null, Curve originalBound = null)
        {
            if (lines.Count == 0)
            {
                return(new Line());
            }

            //====================================================================//

            else if (method == "random") //selects line randomly
            {
                Line line = lines[random.Next(lines.Count)];
                return(line);
            }

            //====================================================================//

            else if (method == "shortest") //selects shortest line
            {
                return(lines.OrderBy(x => x.Length).ToList()[0]);
            }

            //====================================================================//

            else if (method == "boundary") //selects line on boundary
            {
                List <Line> posLines = new List <Line>();

                foreach (Line l in lines)
                {
                    isc.CurveIntersections i = isc.Intersection.CurveCurve(originalBound, l.ToNurbsCurve(), Tolerance.Distance, Tolerance.Distance);
                    if (i.Count > 0)
                    {
                        isc.IntersectionEvent ie = i[0];
                        if (ie.IsOverlap)
                        {
                            posLines.Add(l);
                        }
                    }
                }
                return(posLines[random.Next(posLines.Count)]);
            }

            //====================================================================//

            else if (method == "roads") //select line on road
            {
                List <Line> posLines = new List <Line>();

                foreach (Line l in lines)
                {
                    foreach (Curve road in roads)
                    {
                        isc.CurveIntersections i = isc.Intersection.CurveCurve(road, l.ToNurbsCurve(), Tolerance.Distance, Tolerance.Distance);
                        if (i.Count > 0)
                        {
                            isc.IntersectionEvent ie = i[0];
                            if (ie.IsOverlap)
                            {
                                posLines.Add(l);
                                goto nextLine;
                            }
                        }
                    }
                    nextLine :;
                }
                if (posLines.Count != 0)
                {
                    return(posLines[random.Next(posLines.Count)]);
                }
                else
                {
                    return(new Line());
                }
            }

            //====================================================================//

            else if (method == "boundary first") //selects line on boundary first and then randomly is there are none. TODO: Make it work. :)
            {
                List <Line> posLines = new List <Line>();

                foreach (Line l in lines)
                {
                    isc.CurveIntersections i = isc.Intersection.CurveCurve(originalBound, l.ToNurbsCurve(), Tolerance.Distance, Tolerance.Distance);
                    if (i.Count > 0)
                    {
                        isc.IntersectionEvent ie = i[0];
                        if (ie.IsOverlap)
                        {
                            posLines.Add(l);
                        }
                    }
                }
                if (posLines.Count == 0)
                {
                    return(lines.PickLine("random", random, roads, originalBound));
                }

                return(posLines[random.Next(posLines.Count)]);
            }

            //====================================================================//

            else if (method == "longest") //returns longest line
            {
                return(lines.OrderBy(x => x.Length).ToList()[lines.Count - 1]);
            }

            //====================================================================//

            else // TODO: Fix issue with try/catch in PlaceHouseRow.
            {
                throw new NotImplementedException("The methods yo can choose from are 'shortest', 'longest', 'random', 'boundary' and 'boundary first'.");
            }
        }