Пример #1
0
        public static Mesh GenerateMesh(this PixelGrid2D pixelGrid, Color trueColor, Color falseColor)
        {
            var m     = new Mesh();
            var sizeU = pixelGrid.PixelSize[0] / 2;
            var sizeV = pixelGrid.PixelSize[1] / 2;

            var pts = new Point3d[] {
                new Point3d(-sizeU, -sizeV, 0),
                new Point3d(-sizeU, sizeV, 0),
                new Point3d(sizeU, sizeV, 0),
                new Point3d(sizeU, -sizeV, 0)
            };

            var pln = pixelGrid.Plane;

            for (var i = 0; i < pixelGrid.Count; i++)
            {
                var pt = pixelGrid.PointAt(i);
                pln.Origin = pt;

                var p3fs = new List <Point3f>();
                foreach (var ptd in pts)
                {
                    var worldpt = pln.PointAt(ptd.X, ptd.Y, ptd.Z);
                    p3fs.Add(new Point3f((float)worldpt.X, (float)worldpt.Y, (float)worldpt.Z));
                }

                // try to use unique vertices
                var cCount = m.Vertices.Count;
                m.Vertices.AddVertices(p3fs);
                var iFaceIndex = m.Faces.Count;
                var facenormal = Vector3d.CrossProduct(p3fs[1] - p3fs[0], p3fs[2] - p3fs[0]);
                if (Vector3d.VectorAngle(facenormal, pln.Normal) > Math.PI / 2)
                {
                    m.Faces.AddFace(cCount + 3, cCount + 2, cCount + 1, cCount);
                }
                else
                {
                    m.Faces.AddFace(cCount, cCount + 1, cCount + 2, cCount + 3);
                }

                if (pixelGrid[i] == true)
                {
                    m.VertexColors.Add(trueColor);  m.VertexColors.Add(trueColor);  m.VertexColors.Add(trueColor);  m.VertexColors.Add(trueColor);
                }
                else
                {
                    m.VertexColors.Add(falseColor);  m.VertexColors.Add(falseColor);  m.VertexColors.Add(falseColor); m.VertexColors.Add(falseColor);
                }
                m.FaceNormals.SetFaceNormal(iFaceIndex, pln.Normal);
                m.Normals.ComputeNormals();
                m.Compact();
            }
            return(m);
        }
Пример #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var bb = DA.Fetch <Rectangle3d>("BoundingBox");
            var x  = DA.Fetch <double>("SizeX");
            var y  = DA.Fetch <double>("SizeY");

            var vg = new PixelGrid2D(bb, new Point2d(x, y));

            if (!vg.IsValid)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Result was an invalid grid");
            }

            DA.SetData(0, new GH_PixelGrid((PixelGrid2D)vg.Clone()));
        }
        private void AddPoint3d(PixelGrid2D pg, Point3d pt)
        {
            pg.Plane.RemapToPlaneSpace(pt, out var p);
            var ix = (p.X - pg.BBox.X.Min - pg.PixelSize.X / 2) / pg.PixelSize.X;
            var iy = (p.Y - pg.BBox.Y.Min - pg.PixelSize.Y / 2) / pg.PixelSize.Y;


            var pti = pg.ClosestPoint(pt);

            pti = new Point2i(ix, iy);
            if (new Point2i(0, 0) > pti || pti >= pg.SizeUV)
            {
                return;
            }
            pg[pti] = true;
        }
        private void AddCurve(PixelGrid2D pg, Curve c)
        {
            var projection = Transform.PlanarProjection(pg.Plane);

            c.Transform(projection);

            if (c.IsClosed)
            {
                var bb = c.GetBoundingBox(pg.Plane);
                PixelGrid2D.EnumeratePixels testDelB
                    = delegate(int i) {
                    };

                for (var i = 0; i < pg.Count; i++)
                {
                    var pt = pg.PointAt(i);
                    var pc = c.Contains(pt, pg.Plane);
                    if (pc.HasFlag(PointContainment.Inside) || pc.HasFlag(PointContainment.Coincident))
                    {
                        pg.SetValue(i, true);
                    }
                }
            }
        }