Пример #1
0
 public Interval1d IntersectionWith(ref Interval1d o)
 {
     if (o.a > b || o.b < a)
     {
         return(Interval1d.Empty);
     }
     return(new Interval1d(System.Math.Max(a, o.a), System.Math.Min(b, o.b)));
 }
Пример #2
0
        public double Dist(Interval1d o) {
			if ( b < o.a )
				return o.a - b;
			else if ( a > o.b )
				return a - o.b;
			else
				return 0;
        }
Пример #3
0
        public double SquaredDist(Interval1d o) {
			if ( b < o.a )
				return (o.a - b)*(o.a - b);
			else if ( a > o.b )
				return (a - o.b)*(a - o.b);
			else
				return 0;
        }
Пример #4
0
 public ColorMap(float[] t, Colorf[] c)
 {
     validRange = Interval1d.Empty;
     for (int i = 0; i < t.Length; ++i)
     {
         AddPoint(t[i], c[i]);
     }
 }
Пример #5
0
 public double Dist(Interval1d o)
 {
     if (b < o.a)
     {
         return(o.a - b);
     }
     else if (a > o.b)
     {
         return(a - o.b);
     }
     else
     {
         return(0);
     }
 }
Пример #6
0
 public double SquaredDist(Interval1d o)
 {
     if (b < o.a)
     {
         return((o.a - b) * (o.a - b));
     }
     else if (a > o.b)
     {
         return((a - o.b) * (a - o.b));
     }
     else
     {
         return(0);
     }
 }
        /// <summary>
        /// Calculate the two most extreme vertices along an axis, with optional transform
        /// </summary>
        public static Interval1i ExtremeVertices(DMesh3 mesh, Vector3d axis, Func <Vector3d, Vector3d> TransformF = null)
        {
            Interval1d extent  = Interval1d.Empty;
            Interval1i extreme = new Interval1i(DMesh3.InvalidID, DMesh3.InvalidID);

            if (TransformF == null)
            {
                foreach (int vid in mesh.VertexIndices())
                {
                    double t = mesh.GetVertex(vid).Dot(ref axis);
                    if (t < extent.a)
                    {
                        extent.a  = t;
                        extreme.a = vid;
                    }
                    else if (t > extent.b)
                    {
                        extent.b  = t;
                        extreme.b = vid;
                    }
                }
            }
            else
            {
                foreach (int vid in mesh.VertexIndices())
                {
                    double t = TransformF(mesh.GetVertex(vid)).Dot(ref axis);
                    if (t < extent.a)
                    {
                        extent.a  = t;
                        extreme.a = vid;
                    }
                    else if (t > extent.b)
                    {
                        extent.b  = t;
                        extreme.b = vid;
                    }
                }
            }
            return(extreme);
        }
Пример #8
0
        /// <summary>
        /// Test if segments intersect. Returns true for parallel-line overlaps.
        /// Returns same result as IntrSegment2Segment2.
        /// </summary>
        public bool Intersects(ref Segment2d seg2, double dotThresh = double.Epsilon, double intervalThresh = 0)
        {
            // see IntrLine2Line2 and IntrSegment2Segment2 for details on this code

            Vector2d diff        = seg2.Center - Center;
            double   D0DotPerpD1 = Direction.DotPerp(seg2.Direction);

            if (Math.Abs(D0DotPerpD1) > dotThresh)
            {               // Lines intersect in a single point.
                double invD0DotPerpD1 = ((double)1) / D0DotPerpD1;
                double diffDotPerpD0  = diff.DotPerp(Direction);
                double diffDotPerpD1  = diff.DotPerp(seg2.Direction);
                double s  = diffDotPerpD1 * invD0DotPerpD1;
                double s2 = diffDotPerpD0 * invD0DotPerpD1;
                return(Math.Abs(s) <= (Extent + intervalThresh) &&
                       Math.Abs(s2) <= (seg2.Extent + intervalThresh));
            }

            // Lines are parallel.
            diff.Normalize();
            double diffNDotPerpD1 = diff.DotPerp(seg2.Direction);

            if (Math.Abs(diffNDotPerpD1) <= dotThresh)
            {
                // Compute the location of segment1 endpoints relative to segment0.
                diff = seg2.Center - Center;
                double t1      = Direction.Dot(diff);
                double tmin    = t1 - seg2.Extent;
                double tmax    = t1 + seg2.Extent;
                var    extents = new Interval1d(-Extent, Extent);
                if (extents.Overlaps(new Interval1d(tmin, tmax)))
                {
                    return(true);
                }

                return(false);
            }

            // lines are parallel but not collinear
            return(false);
        }
        /// <summary>
        /// Calculate extents of mesh along an axis, with optional transform
        /// </summary>
        public static Interval1d ExtentsOnAxis(DMesh3 mesh, Vector3d axis, Func <Vector3d, Vector3d> TransformF = null)
        {
            Interval1d extent = Interval1d.Empty;

            if (TransformF == null)
            {
                foreach (Vector3d v in mesh.Vertices())
                {
                    extent.Contain(v.Dot(ref axis));
                }
            }
            else
            {
                foreach (Vector3d v in mesh.Vertices())
                {
                    Vector3d vT = TransformF(v);
                    extent.Contain(vT.Dot(ref axis));
                }
            }
            return(extent);
        }
        /// <summary>
        /// Calculate extents of mesh along an axis, with optional transform
        /// </summary>
        public static Interval1d ExtentsOnAxis(IMesh mesh, Vector3d axis, Func <Vector3d, Vector3d> TransformF = null)
        {
            Interval1d extent = Interval1d.Empty;

            if (TransformF == null)
            {
                foreach (int vid in mesh.VertexIndices())
                {
                    extent.Contain(mesh.GetVertex(vid).Dot(ref axis));
                }
            }
            else
            {
                foreach (int vid in mesh.VertexIndices())
                {
                    Vector3d vT = TransformF(mesh.GetVertex(vid));
                    extent.Contain(vT.Dot(ref axis));
                }
            }
            return(extent);
        }
Пример #11
0
        public virtual int[] AddTriangleStrip(IList <Frame3f> frames, IList <Interval1d> spans, int group_id = -1)
        {
            int N = frames.Count;

            if (N != spans.Count)
            {
                throw new Exception("MeshEditor.AddTriangleStrip: spans list is not the same size!");
            }

            int[] new_tris = new int[2 * (N - 1)];

            int prev_a = -1, prev_b = -1;
            int i = 0, ti = 0;

            for (i = 0; i < N; ++i)
            {
                Frame3f    f    = frames[i];
                Interval1d span = spans[i];

                Vector3d va = f.Origin + (float)span.a * f.Y;
                Vector3d vb = f.Origin + (float)span.b * f.Y;

                // [TODO] could compute normals here...

                int a = Mesh.AppendVertex(va);
                int b = Mesh.AppendVertex(vb);

                if (prev_a != -1)
                {
                    new_tris[ti++] = Mesh.AppendTriangle(prev_a, b, prev_b);
                    new_tris[ti++] = Mesh.AppendTriangle(prev_a, a, b);
                }
                prev_a = a; prev_b = b;
            }

            return(new_tris);
        }
Пример #12
0
        int split_tri_set_midpoint(int[] triangles, Vector3d[] centers, int iStart, int iCount, int depth, int minTriCount,
                                   boxes_set tris, boxes_set nodes, out AxisAlignedBox3f box)
        {
            box = AxisAlignedBox3f.Empty;
            int iBox = -1;

            if (iCount < minTriCount)
            {
                // append new triangles box
                iBox = tris.iBoxCur++;
                tris.box_to_index.insert(tris.iIndicesCur, iBox);

                tris.index_list.insert(iCount, tris.iIndicesCur++);
                for (int i = 0; i < iCount; ++i)
                {
                    tris.index_list.insert(triangles[iStart + i], tris.iIndicesCur++);
                    box.Contain(mesh.GetTriBounds(triangles[iStart + i]));
                }

                tris.box_centers.insert(box.Center, iBox);
                tris.box_extents.insert(box.Extents, iBox);

                return(-(iBox + 1));
            }

            //compute interval along an axis and find midpoint
            int        axis     = depth % 3;
            Interval1d interval = Interval1d.Empty;

            for (int i = 0; i < iCount; ++i)
            {
                interval.Contain(centers[iStart + i][axis]);
            }
            double midpoint = interval.Center;

            int n0, n1;

            if (Math.Abs(interval.a - interval.b) > MathUtil.ZeroTolerance)
            {
                // we have to re-sort the centers & triangles lists so that centers < midpoint
                // are first, so that we can recurse on the two subsets. We walk in from each side,
                // until we find two out-of-order locations, then we swap them.
                int l = 0;
                int r = iCount - 1;
                while (l < r)
                {
                    // [RMS] is <= right here? if v.axis == midpoint, then this loop
                    //   can get stuck unless one of these has an equality test. But
                    //   I did not think enough about if this is the right thing to do...
                    while (centers[iStart + l][axis] <= midpoint)
                    {
                        l++;
                    }
                    while (centers[iStart + r][axis] > midpoint)
                    {
                        r--;
                    }
                    if (l >= r)
                    {
                        break;      //done!
                    }
                    //swap
                    Vector3d tmpc = centers[iStart + l]; centers[iStart + l] = centers[iStart + r];  centers[iStart + r] = tmpc;
                    int      tmpt = triangles[iStart + l]; triangles[iStart + l] = triangles[iStart + r]; triangles[iStart + r] = tmpt;
                }

                n0 = l;
                n1 = iCount - n0;
                Debug.Assert(n0 >= 1 && n1 >= 1);
            }
            else
            {
                // interval is near-empty, so no point trying to do sorting, just split half and half
                n0 = iCount / 2;
                n1 = iCount - n0;
            }

            // create child boxes
            AxisAlignedBox3f box1;
            int child0 = split_tri_set_midpoint(triangles, centers, iStart, n0, depth + 1, minTriCount, tris, nodes, out box);
            int child1 = split_tri_set_midpoint(triangles, centers, iStart + n0, n1, depth + 1, minTriCount, tris, nodes, out box1);

            box.Contain(box1);

            // append new box
            iBox = nodes.iBoxCur++;
            nodes.box_to_index.insert(nodes.iIndicesCur, iBox);

            nodes.index_list.insert(child0, nodes.iIndicesCur++);
            nodes.index_list.insert(child1, nodes.iIndicesCur++);

            nodes.box_centers.insert(box.Center, iBox);
            nodes.box_extents.insert(box.Extents, iBox);

            return(iBox);
        }
Пример #13
0
        public override DeformInfo Apply(Frame3f vNextPos)
        {
            Interval1d edgeRangeSqr = Interval1d.Empty;

            int N = Curve.VertexCount;

            if (N > NewV.size)
            {
                NewV.resize(N);
            }
            if (N > ModifiedV.Length)
            {
                ModifiedV = new BitArray(2 * N);
            }

            // clear modified
            ModifiedV.SetAll(false);

            bool   bSmooth = (SmoothAlpha > 0 && SmoothIterations > 0);
            double r2      = Radius * Radius;

            // deform pass
            if (DeformF != null)
            {
                for (int i = 0; i < N; ++i)
                {
                    Vector3d v  = Curve[i];
                    double   d2 = (v - vPreviousPos.Origin).LengthSquared;
                    if (d2 < r2)
                    {
                        double t = WeightFunc(Math.Sqrt(d2), Radius);

                        Vector3d vNew = DeformF(i, t);

                        if (bSmooth == false)
                        {
                            if (i > 0)
                            {
                                edgeRangeSqr.Contain(vNew.DistanceSquared(Curve[i - 1]));
                            }
                            if (i < N - 1)
                            {
                                edgeRangeSqr.Contain(vNew.DistanceSquared(Curve[i + 1]));
                            }
                        }

                        NewV[i]      = vNew;
                        ModifiedV[i] = true;
                    }
                }
            }
            else
            {
                // anything?
            }

            // smooth pass
            if (bSmooth)
            {
                for (int j = 0; j < SmoothIterations; ++j)
                {
                    int iStart = (Curve.Closed) ? 0 : 1;
                    int iEnd   = (Curve.Closed) ? N : N - 1;
                    for (int i = iStart; i < iEnd; ++i)
                    {
                        Vector3d v  = (ModifiedV[i]) ? NewV[i] : Curve[i];
                        double   d2 = (v - vPreviousPos.Origin).LengthSquared;
                        if (ModifiedV[i] || d2 < r2)            // always smooth any modified verts
                        {
                            double a = SmoothAlpha * WeightFunc(Math.Sqrt(d2), Radius);

                            int      iPrev = (i == 0) ? N - 1 : i - 1;
                            int      iNext = (i + 1) % N;
                            Vector3d vPrev = (ModifiedV[iPrev]) ? NewV[iPrev] : Curve[iPrev];
                            Vector3d vNext = (ModifiedV[iNext]) ? NewV[iNext] : Curve[iNext];
                            Vector3d c     = (vPrev + vNext) * 0.5f;
                            NewV[i]      = (1 - a) * v + (a) * c;
                            ModifiedV[i] = true;

                            if (i > 0)
                            {
                                edgeRangeSqr.Contain(NewV[i].DistanceSquared(Curve[i - 1]));
                            }
                            if (i < N - 1)
                            {
                                edgeRangeSqr.Contain(NewV[i].DistanceSquared(Curve[i + 1]));
                            }
                        }
                    }
                }
            }

            // bake
            for (int i = 0; i < N; ++i)
            {
                if (ModifiedV[i])
                {
                    Curve[i] = NewV[i];
                }
            }

            return(new DeformInfo()
            {
                minEdgeLenSqr = edgeRangeSqr.a, maxEdgeLenSqr = edgeRangeSqr.b
            });
        }
Пример #14
0
 public ColorMap()
 {
     validRange = Interval1d.Empty;
 }
Пример #15
0
 public Interval1d(Interval1d copy)
 {
     a = copy.a; b = copy.b;
 }
Пример #16
0
 public void Set(Interval1d o)
 {
     a = o.a; b = o.b;
 }
Пример #17
0
 public Interval1d IntersectionWith(ref Interval1d o)
 {
     if (o.a > b || o.b < a)
         return Interval1d.Empty;
     return new Interval1d(Math.Max(a, o.a), Math.Min(b, o.b));
 }
Пример #18
0
 public ScalarMap()
 {
     validRange = Interval1d.Empty;
 }
Пример #19
0
 public bool Overlaps(Interval1d o)
 {
     return(!(o.a > b || o.b < a));
 }
Пример #20
0
 public Intersector1(Interval1d u, Interval1d v)
 {
     U = u;
     V = v;
 }
Пример #21
0
 public Intersector1(double u0, double u1, double v0, double v1)
 {
     // [TODO] validate 0 < 1
     U = new Interval1d(u0, u1);
     V = new Interval1d(v0, v1);
 }