Example #1
0
        /*public void CutByRect (Vector3 pos, Vector3 size)
         * /// Splits all segments so that each intersection with AABB rect has a node
         * {
         *      for (int i=0; i<13; i++) //spline could be divided in 12 parts maximum
         *      {
         *              List<Vector3> newNodes = new List<Vector3>();
         *
         *              for (int n=0; n<nodes.Length-1; n++)
         *              {
         *                      //early check - if inside/outside rect
         *                      Vector3 min = Vector3.Min(nodes[n], nodes[n+1]);
         *                      Vector3 max = Vector3.Max(nodes[n], nodes[n+1]);
         *
         *                      if (max.x < pos.x  ||  min.x > pos.x+size.x ||
         *                              max.z < pos.z  ||  min.z > pos.z+size.z)
         *                                      { newNodes.Add(nodes[n+1]); continue; } //fully outside
         *                      if (min.x > pos.x  &&  max.x < pos.x+size.x &&
         *                              min.z > pos.z  &&  max.z < pos.z+size.z)
         *                                      { newNodes.Add(nodes[n+1]); continue; } //fully inside
         *
         *                      //splitting
         *                      float sp = segments[s].IntersectRect(pos, size);
         *                      if (sp < 0.0001f  ||  sp > 0.999f)
         *                              { newSegments.Add(segments[s]); continue; }  //no intersection
         *
         *                      (Segment s1, Segment s2) = segments[s].GetSplitted(sp);
         *                      newSegments.Add(s1);
         *                      newSegments.Add(s2);
         *              }
         *
         *              bool segemntsCountChanged = segments.Length != newSegments.Count;
         *              segments = newSegments.ToArray();
         *              if (!segemntsCountChanged) break; //if no nodes added - exiting 12 iterations
         *      }
         * }*/

        public void CutAA(float val, CutAxis axis)
        /// Cuts the line creating points on horizontal line with X coordinate
        {
            List <Vector3> newNodes = new List <Vector3>(capacity: nodes.Length)
            {
                nodes[0]
            };

            for (int n = 0; n < nodes.Length - 1; n++)
            {
                float pos0 = axis == CutAxis.AxisX ? nodes[n].x : nodes[n].z;
                float pos1 = axis == CutAxis.AxisX ? nodes[n + 1].x : nodes[n + 1].z;

                // early check - on one side only
                if (pos0 < val && pos1 < val)
                {
                    newNodes.Add(nodes[n + 1]); continue;
                }
                if (pos0 > val && pos1 > val)
                {
                    newNodes.Add(nodes[n + 1]); continue;
                }

                // cutting
                float percent = (pos0 - val) / (pos0 - pos1);
                newNodes.Add(new Vector3(
                                 nodes[n].x * (1 - percent) + nodes[n + 1].x * percent,
                                 nodes[n].y * (1 - percent) + nodes[n + 1].y * percent,
                                 nodes[n].z * (1 - percent) + nodes[n + 1].z * percent));

                newNodes.Add(nodes[n + 1]);
            }

            nodes = newNodes.ToArray();
        }
Example #2
0
        public Spline[] RemoveOuter(float val, CutAxis axis, CutSide side)
        /// Removes segment if any of the segment nodes is less than x
        /// Will split spline in several, returns new splitted splines
        /// Subtract a bit (or add if side is positive) from x when using together wit Cut:  +0.0001f*(int)side
        {
            List <Spline> newSplines = new List <Spline>();

            List <Vector3> currSpline = null;

            for (int n = 0; n < nodes.Length; n++)
            {
                bool isOuter = axis == CutAxis.AxisX ? nodes[n].x > val : nodes[n].z > val;
                if (side == CutSide.Negative)
                {
                    isOuter = !isOuter;
                }

                // starting new spline
                if (currSpline == null)
                {
                    if (!isOuter)
                    {
                        currSpline = new List <Vector3>();
                        currSpline.Add(nodes[n]);
                    }

                    //ignoring if under x
                }

                else
                {
                    //ending spline
                    if (isOuter)
                    {
                        newSplines.Add(new Spline(currSpline.ToArray()));
                        currSpline = null;
                    }

                    //adding node
                    else
                    {
                        currSpline.Add(nodes[n]);
                    }
                }
            }

            if (currSpline != null)
            {
                newSplines.Add(new Spline(currSpline.ToArray()));
            }

            return(newSplines.ToArray());
        }