Exemple #1
0
 public DVectorArray3(int nCount = 0)
 {
     vector = new DVector <T>();
     if (nCount > 0)
     {
         vector.resize(nCount * 3);
     }
 }
Exemple #2
0
        /// <summary>
        /// resize the list-of-lists
        /// </summary>
        public void Resize(int new_size)
        {
            int cur_size = list_heads.size;

            if (new_size > cur_size)
            {
                list_heads.resize(new_size);
                for (int k = cur_size; k < new_size; ++k)
                {
                    list_heads[k] = Null;
                }
            }
        }
Exemple #3
0
        public MeshTriInfoCache(DMesh3 mesh)
        {
            int NT = mesh.TriangleCount;

            Centroids = new DVector <Vector3d>(); Centroids.resize(NT);
            Normals   = new DVector <Vector3d>(); Normals.resize(NT);
            Areas     = new DVector <double>(); Areas.resize(NT);
            gParallel.ForEach(mesh.TriangleIndices(), (tid) => {
                Vector3d c, n; double a;
                mesh.GetTriInfo(tid, out n, out a, out c);
                Centroids[tid] = c;
                Normals[tid]   = n;
                Areas[tid]     = a;
            });
        }
 protected virtual void InitializeVertexBufferForPass()
 {
     if (vBufferV.size < mesh.MaxVertexID)
     {
         vBufferV.resize(mesh.MaxVertexID + mesh.MaxVertexID / 5);
     }
     if (vModifiedV.Length < mesh.MaxVertexID)
     {
         vModifiedV = new BitArray(2 * mesh.MaxVertexID);
     }
     else
     {
         vModifiedV.SetAll(false);
     }
 }
Exemple #5
0
        public void EnableVertexColors(Vector3f initial_color)
        {
            if (HasVertexColors)
            {
                return;
            }
            colors = new DVector <float>();
            int NV = MaxVertexID;

            colors.resize(3 * NV);
            for (int i = 0; i < NV; ++i)
            {
                int vi = 3 * i;
                colors[vi]     = initial_color.x;
                colors[vi + 1] = initial_color.y;
                colors[vi + 2] = initial_color.z;
            }
        }
        // TODO: parallel version, cache tri normals
        void Compute_FaceAvg_AreaWeighted()
        {
            int NV = Mesh.MaxVertexID;

            if (NV != Normals.size)
            {
                Normals.resize(NV);
            }

            for (int i = 0; i < NV; ++i)
            {
                Normals[i] = Vector3d.Zero;
            }

            var Normals_lock = new SpinLock();

            gParallel.ForEach(Mesh.TriangleIndices(), (ti) =>
            {
                Index3i tri = Mesh.GetTriangle(ti);
                Vector3d va = Mesh.GetVertex(tri.a);
                Vector3d vb = Mesh.GetVertex(tri.b);
                Vector3d vc = Mesh.GetVertex(tri.c);
                Vector3d N  = MathUtil.Normal(ref va, ref vb, ref vc);
                double a    = MathUtil.Area(ref va, ref vb, ref vc);
                bool taken  = false;
                Normals_lock.Enter(ref taken);
                Normals[tri.a] += a * N;
                Normals[tri.b] += a * N;
                Normals[tri.c] += a * N;
                Normals_lock.Exit();
            });

            gParallel.BlockStartEnd(0, NV - 1, (vi_start, vi_end) =>
            {
                for (int vi = vi_start; vi <= vi_end; vi++)
                {
                    if (Normals[vi].LengthSquared > MathUtil.ZeroTolerancef)
                    {
                        Normals[vi] = Normals[vi].Normalized;
                    }
                }
            });
        }
        // TODO: parallel version, cache tri normals
        void Compute_FaceAvg_AreaWeighted()
        {
            int NV = Mesh.MaxVertexID;

            if (NV != Normals.size)
            {
                Normals.resize(NV);
            }
            for (int i = 0; i < NV; ++i)
            {
                Normals[i] = Vector3d.Zero;
            }

            int NT = Mesh.MaxTriangleID;

            for (int ti = 0; ti < NT; ++ti)
            {
                if (Mesh.IsTriangle(ti) == false)
                {
                    continue;
                }
                Index3i  tri = Mesh.GetTriangle(ti);
                Vector3d va  = Mesh.GetVertex(tri.a);
                Vector3d vb  = Mesh.GetVertex(tri.b);
                Vector3d vc  = Mesh.GetVertex(tri.c);
                Vector3d N   = MathUtil.Normal(va, vb, vc);
                double   a   = MathUtil.Area(va, vb, vc);
                Normals[tri.a] += a * N;
                Normals[tri.b] += a * N;
                Normals[tri.c] += a * N;
            }

            for (int vi = 0; vi < NV; ++vi)
            {
                if (Normals[vi].LengthSquared > MathUtil.ZeroTolerancef)
                {
                    Normals[vi].Normalize();
                }
            }
        }
 public void trim(int maxIndex)
 {
     free_indices = new DVector <int>();
     ref_counts.resize(maxIndex);
     used_count = maxIndex;
 }
Exemple #9
0
 public void Resize(int count)
 {
     vector.resize(3 * count);
 }
Exemple #10
0
 public DVectorArray2(int nCount = 0)
 {
     vector = new DVector <T>();
     vector.resize(nCount * 2);
 }
        protected virtual void do_split(Line2d line, bool insert_edges, int insert_gid)
        {
            if (EdgeSigns.Length < Graph.MaxVertexID)
            {
                EdgeSigns.resize(Graph.MaxVertexID);
            }

            foreach (int vid in Graph.VertexIndices())
            {
                EdgeSigns[vid] = line.WhichSide(Graph.GetVertex(vid), OnVertexTol);
            }


            hits.Clear();
            foreach (int eid in Graph.EdgeIndices())
            {
                Index2i ev    = Graph.GetEdgeV(eid);
                var     signs = new Index2i(EdgeSigns[ev.a], EdgeSigns[ev.b]);
                if (signs.a * signs.b > 0)
                {
                    continue;                       // both positive or negative, ignore
                }

                var hit = new edge_hit()
                {
                    hit_eid = eid, vtx_signs = signs, hit_vid = -1
                };
                Vector2d a = Graph.GetVertex(ev.a);
                Vector2d b = Graph.GetVertex(ev.b);

                // parallel-edge case (both are zero)
                if (signs.a == signs.b)
                {
                    if (a.DistanceSquared(b) > MathUtil.Epsilon)
                    {
                        // we need to somehow not insert a new segment for this span below.
                        // so, insert two hit points for the ray-interval, with same eid.
                        // This will result in this span being skipped by the same-eid test below
                        // *however*, if other edges self-intersect w/ this segment, this will *not work*
                        // and duplicate edges will be inserted
                        hit.hit_vid = ev.a;
                        hit.line_t  = line.Project(a);
                        hits.Add(hit);
                        hit.hit_vid = ev.b;
                        hit.line_t  = line.Project(b);
                        hits.Add(hit);
                    }
                    else
                    {
                        // degenerate edge - fall through to a == 0 case below
                        signs.b = 1;
                    }
                }

                if (signs.a == 0)
                {
                    hit.hit_pos = a;
                    hit.hit_vid = ev.a;
                    hit.line_t  = line.Project(a);
                }
                else if (signs.b == 0)
                {
                    hit.hit_pos = b;
                    hit.hit_vid = ev.b;
                    hit.line_t  = line.Project(b);
                }
                else
                {
                    var intr = new IntrLine2Segment2(line, new Segment2d(a, b));
                    if (intr.Find() == false)
                    {
                        throw new Exception("GraphSplitter2d.Split: signs are different but ray did not it?");
                    }

                    if (intr.IsSimpleIntersection)
                    {
                        hit.hit_pos = intr.Point;
                        hit.line_t  = intr.Parameter;
                    }
                    else
                    {
                        throw new Exception("GraphSplitter2d.Split: got parallel edge case!");
                    }
                }
                hits.Add(hit);
            }

            // sort by increasing ray-t
            hits.Sort((hit0, hit1) => { return(hit0.line_t.CompareTo(hit1.line_t)); });

            // insert segments between successive intersection points
            int N = hits.Count;

            for (int i = 0; i < N - 1; ++i)
            {
                int j = i + 1;
                // note: skipping parallel segments depends on this eid == eid test (see above)
                if (hits[i].line_t == hits[j].line_t || hits[i].hit_eid == hits[j].hit_eid)
                {
                    continue;
                }

                int vi = hits[i].hit_vid;
                int vj = hits[j].hit_vid;
                if (vi == vj && vi >= 0)
                {
                    continue;
                }

                if (vi >= 0 && vj >= 0)
                {
                    int existing = Graph.FindEdge(vi, vj);
                    if (existing >= 0)
                    {
                        continue;
                    }
                }

                if (vi == -1)
                {
                    DGraph2.EdgeSplitInfo split;
                    var result = Graph.SplitEdge(hits[i].hit_eid, out split);
                    if (result != MeshResult.Ok)
                    {
                        throw new Exception("GraphSplitter2d.Split: first edge split failed!");
                    }

                    vi = split.vNew;
                    Graph.SetVertex(vi, hits[i].hit_pos);
                    edge_hit tmp = hits[i]; tmp.hit_vid = vi; hits[i] = tmp;
                }

                if (vj == -1)
                {
                    DGraph2.EdgeSplitInfo split;
                    var result = Graph.SplitEdge(hits[j].hit_eid, out split);
                    if (result != MeshResult.Ok)
                    {
                        throw new Exception("GraphSplitter2d.Split: second edge split failed!");
                    }

                    vj = split.vNew;
                    Graph.SetVertex(vj, hits[j].hit_pos);
                    edge_hit tmp = hits[j]; tmp.hit_vid = vj; hits[j] = tmp;
                }

                // check if we actually want to add this segment
                if (InsideTestF != null)
                {
                    Vector2d midpoint = 0.5 * (Graph.GetVertex(vi) + Graph.GetVertex(vj));
                    if (InsideTestF(midpoint) == false)
                    {
                        continue;
                    }
                }

                if (insert_edges)
                {
                    Graph.AppendEdge(vi, vj, insert_gid);
                }
            }
        }
 public StandardSculptCurveDeformation()
 {
     NewV = new DVector <Vector3d>();
     NewV.resize(256);
     ModifiedV = new BitArray(256);
 }
        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
            });
        }