Subdivide() public static method

public static Subdivide ( Vector3 path, int subdivisions ) : Vector3[]
path Vector3
subdivisions int
return Vector3[]
		public override void Apply (Path p) {
			if (!useRaycasting && !useGraphRaycasting) return;

			var points = p.vectorPath;

			if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count-1])) {
				// A very common case is that there is a straight line to the target.
				var s = p.vectorPath[0];
				var e = p.vectorPath[p.vectorPath.Count-1];
				points.ClearFast();
				points.Add(s);
				points.Add(e);
			} else {
				int iterations = iterationsByQuality[(int)quality];
				for (int it = 0; it < iterations; it++) {
					if (it != 0) {
						Polygon.Subdivide(points, buffer, 3);
						Memory.Swap(ref buffer, ref points);
						buffer.ClearFast();
						points.Reverse();
					}

					points = quality >= Quality.High ? ApplyDP(p, points) : ApplyGreedy(p, points);
				}
				if ((iterations % 2) == 0) points.Reverse();
			}

			p.vectorPath = points;
		}
Ejemplo n.º 2
0
        public override void Apply(Path p)
        {
            if (!useRaycasting && !useGraphRaycasting)
            {
                return;
            }

            var points = p.vectorPath;

            cachedFilter.path = p;

            // Use the same graph mask as the path.
            // We don't want to use the tag mask or other options for this though since then the linecasting will be will confused.
            cachedNNConstraint.graphMask = p.nnConstraint.graphMask;

            if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count - 1], cachedFilter.cachedDelegate, cachedNNConstraint))
            {
                // A very common case is that there is a straight line to the target.
                var s = p.vectorPath[0];
                var e = p.vectorPath[p.vectorPath.Count - 1];
                points.ClearFast();
                points.Add(s);
                points.Add(e);
            }
            else
            {
                int iterations = iterationsByQuality[(int)quality];
                for (int it = 0; it < iterations; it++)
                {
                    if (it != 0)
                    {
                        Polygon.Subdivide(points, buffer, 3);
                        Memory.Swap(ref buffer, ref points);
                        buffer.ClearFast();
                        points.Reverse();
                    }

                    points = quality >= Quality.High ? ApplyDP(p, points, cachedFilter.cachedDelegate, cachedNNConstraint) : ApplyGreedy(p, points, cachedFilter.cachedDelegate, cachedNNConstraint);
                }
                if ((iterations % 2) == 0)
                {
                    points.Reverse();
                }
            }

            p.vectorPath = points;
        }
        // Token: 0x060026B8 RID: 9912 RVA: 0x001AC284 File Offset: 0x001AA484
        public override void Apply(Path p)
        {
            if (!this.useRaycasting && !this.useGraphRaycasting)
            {
                return;
            }
            List <Vector3> list = p.vectorPath;

            if (this.ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count - 1]))
            {
                Vector3 item  = p.vectorPath[0];
                Vector3 item2 = p.vectorPath[p.vectorPath.Count - 1];
                list.ClearFast <Vector3>();
                list.Add(item);
                list.Add(item2);
            }
            else
            {
                int num = RaycastModifier.iterationsByQuality[(int)this.quality];
                for (int i = 0; i < num; i++)
                {
                    if (i != 0)
                    {
                        Polygon.Subdivide(list, RaycastModifier.buffer, 3);
                        Memory.Swap <List <Vector3> >(ref RaycastModifier.buffer, ref list);
                        RaycastModifier.buffer.ClearFast <Vector3>();
                        list.Reverse();
                    }
                    list = ((this.quality >= RaycastModifier.Quality.High) ? this.ApplyDP(p, list) : this.ApplyGreedy(p, list));
                }
                if (num % 2 == 0)
                {
                    list.Reverse();
                }
            }
            p.vectorPath = list;
        }
        public List <Vector3> SmoothSimple(List <Vector3> path)
        {
            if (path.Count < 2)
            {
                return(path);
            }

            List <Vector3> subdivided;

            if (uniformLength)
            {
                // Clamp to a small value to avoid the path being divided into a huge number of segments
                maxSegmentLength = Mathf.Max(maxSegmentLength, 0.005f);

                float pathLength = 0;
                for (int i = 0; i < path.Count - 1; i++)
                {
                    pathLength += Vector3.Distance(path[i], path[i + 1]);
                }

                int estimatedNumberOfSegments = Mathf.FloorToInt(pathLength / maxSegmentLength);
                // Get a list with an initial capacity high enough so that we can add all points
                subdivided = ListPool <Vector3> .Claim(estimatedNumberOfSegments + 2);

                float distanceAlong = 0;

                // Sample points every [maxSegmentLength] world units along the path
                for (int i = 0; i < path.Count - 1; i++)
                {
                    var start = path[i];
                    var end   = path[i + 1];

                    float length = Vector3.Distance(start, end);

                    while (distanceAlong < length)
                    {
                        subdivided.Add(Vector3.Lerp(start, end, distanceAlong / length));
                        distanceAlong += maxSegmentLength;
                    }

                    distanceAlong -= length;
                }

                // Make sure we get the exact position of the last point
                subdivided.Add(path[path.Count - 1]);
            }
            else
            {
                subdivisions = Mathf.Max(subdivisions, 0);

                if (subdivisions > 10)
                {
                    Debug.LogWarning("Very large number of subdivisions. Cowardly refusing to subdivide every segment into more than " + (1 << subdivisions) + " subsegments");
                    subdivisions = 10;
                }

                int steps = 1 << subdivisions;
                subdivided = ListPool <Vector3> .Claim((path.Count - 1) *steps + 1);

                Polygon.Subdivide(path, subdivided, steps);
            }

            if (strength > 0)
            {
                for (int it = 0; it < iterations; it++)
                {
                    Vector3 prev = subdivided[0];

                    for (int i = 1; i < subdivided.Count - 1; i++)
                    {
                        Vector3 tmp = subdivided[i];

                        // prev is at this point set to the value that subdivided[i-1] had before this loop started
                        // Move the point closer to the average of the adjacent points
                        subdivided[i] = Vector3.Lerp(tmp, (prev + subdivided[i + 1]) / 2F, strength);

                        prev = tmp;
                    }
                }
            }

            return(subdivided);
        }
Ejemplo n.º 5
0
        // Token: 0x060026C3 RID: 9923 RVA: 0x001ACF04 File Offset: 0x001AB104
        public List <Vector3> SmoothSimple(List <Vector3> path)
        {
            if (path.Count < 2)
            {
                return(path);
            }
            List <Vector3> list;

            if (this.uniformLength)
            {
                this.maxSegmentLength = Mathf.Max(this.maxSegmentLength, 0.005f);
                float num = 0f;
                for (int i = 0; i < path.Count - 1; i++)
                {
                    num += Vector3.Distance(path[i], path[i + 1]);
                }
                list = ListPool <Vector3> .Claim(Mathf.FloorToInt(num / this.maxSegmentLength) + 2);

                float num2 = 0f;
                for (int j = 0; j < path.Count - 1; j++)
                {
                    Vector3 a    = path[j];
                    Vector3 b    = path[j + 1];
                    float   num3 = Vector3.Distance(a, b);
                    while (num2 < num3)
                    {
                        list.Add(Vector3.Lerp(a, b, num2 / num3));
                        num2 += this.maxSegmentLength;
                    }
                    num2 -= num3;
                }
                list.Add(path[path.Count - 1]);
            }
            else
            {
                this.subdivisions = Mathf.Max(this.subdivisions, 0);
                if (this.subdivisions > 10)
                {
                    Debug.LogWarning("Very large number of subdivisions. Cowardly refusing to subdivide every segment into more than " + (1 << this.subdivisions) + " subsegments");
                    this.subdivisions = 10;
                }
                int num4 = 1 << this.subdivisions;
                list = ListPool <Vector3> .Claim((path.Count - 1) *num4 + 1);

                Polygon.Subdivide(path, list, num4);
            }
            if (this.strength > 0f)
            {
                for (int k = 0; k < this.iterations; k++)
                {
                    Vector3 a2 = list[0];
                    for (int l = 1; l < list.Count - 1; l++)
                    {
                        Vector3 vector = list[l];
                        list[l] = Vector3.Lerp(vector, (a2 + list[l + 1]) / 2f, this.strength);
                        a2      = vector;
                    }
                }
            }
            return(list);
        }