70001725 Custom Point Cloud Object

Article 70001725
Type HowTo
Product Engine
Version 7
Date Added 3/30/2020 12:00:00 AM
Fixed 8.8005.0.1 (4/2/2020 12:00:00 AM)
Submitted by Vissarion GiorSpiros

Summary

How to make my own custom point cloud object

Solution

In version 8005.0.1 vdPointCloud was improved in order to accept custom user data and support multi color
See the following code
Run the static method Custom_PointCLoud.test to add it to a vdDocument.

    //a class that override the standard gPoint with some additional properties
    public class ColoredPoint : gPoint, IvdProxySerializer
    {
        Color mcolor = Color.Empty;
        public Color Color { get { return mcolor; } set { mcolor = value; } }
        public ColoredPoint()
        {

        }
        public ColoredPoint(double _x, double _y, double _z, Color _color) : base(_x, _y, _z)
        {
            mcolor = _color;
        }
        #region IFromString Members //used for serialize - deserialize
        /// 
        /// Sets the x,y,z,r,g,b values of the point from a given string.
        /// 
        /// A string representing a point with the following syntax: x,y,z,r,g,b .
        public override void FromString(string str)
        {
            if (str == null) return;
            string[] items = str.Split(new char[] { ',' });
            if (items.Length > 0) x = Double.Parse(items[0], VectorDraw.Serialize.Activator.GetNumberFormat());
            if (items.Length > 1) y = Double.Parse(items[1], VectorDraw.Serialize.Activator.GetNumberFormat());
            if (items.Length > 2) z = Double.Parse(items[2], VectorDraw.Serialize.Activator.GetNumberFormat());
            if (items.Length > 5) mcolor = Color.FromArgb(int.Parse(items[3]), int.Parse(items[4]), int.Parse(items[5]));
        }
        /// 
        /// Gets a System.String that represents the coordinates of the gPoint Object using the format parameter for visualization.
        /// 
        /// A parameter used to specify the format the coordinates should be displayed.
        /// A string representing the coordinates of the gPoint object.
        public override string ToString(string format)
        {

            return VectorDraw.Serialize.Serializer.DoubleToString(x, format)
                + "," + VectorDraw.Serialize.Serializer.DoubleToString(y, format)
                + "," + VectorDraw.Serialize.Serializer.DoubleToString(z, format)
                + "," + mcolor.R.ToString()
                + "," + mcolor.G.ToString()
                + "," + mcolor.B.ToString()
                ;
        }
        /// 
        /// Gets a System.String that represents the coordinates of the gPoint Object using the format parameter for visualization.
        /// 
        /// The Serializer object where the object is going to be saved.
        /// 
        public override string ToString(VectorDraw.Serialize.Serializer serializer)
        {
            return ToString(serializer.DoublePrecisionFormat);
        }
        #endregion
        /// 
        /// Gets a System.String that represents the coordinates of the gPoint Object.
        /// 
        public override string ToString()
        {
            return ToString("0.00000000");
        }
        public override object Clone()
        {
            ColoredPoint ret = new ColoredPoint();
            ret.CopyFrom(this);
            ret.Color = Color;
            return ret;
        }
    }
    //a collection of ColoredPoint type
    public class ColoredPoints : vdArray<ColoredPoint>, ICloneable, IvdProxySerializer
    {
        public ColoredPoints() { }//a default constructor must exist and must be public in order to be serialized / deserialized
        public override void Serialize(Serializer serializer)
        {
            base.Serialize(serializer);
        }
        public override bool DeSerialize(DeSerializer deserializer, string fieldname, object value)
        {
            return base.DeSerialize(deserializer, fieldname, value);
        }

        public object Clone()
        {
            ColoredPoints ret = new ColoredPoints();
            foreach (ColoredPoint item in this)
            {
                ret.AddItem(item.Clone() as ColoredPoint);
            }
            return ret;
        }
    }
    public class Custom_PointCLoud : vdPointCloud, IvdProxyFigure
    {
        //a test function that creates a new Custom_PointCLoud and add it to the document
        public static void test(vdDocument doc)
        {
            doc.ProxyClasses.Add(typeof(Custom_PointCLoud));
            doc.ProxyClasses.Add(typeof(ColoredPoint));
            doc.ProxyClasses.Add(typeof(ColoredPoints));
            //create a test of pointCloud with random points
            int size = 1000;
            //prompts to set the number of points.Enter for default 1000000 points
            doc.Prompt("Number of points <" + size.ToString() + ">:");
            doc.ActionUtility.SetAcceptedStringValues(null, size);
            StatusCode scode = doc.ActionUtility.getUserInt(out size);
            doc.Prompt(null);
            if (scode != StatusCode.Success) return;
            size = Math.Abs(size);
            if (size == 0) size = 1000;
            ColoredPoints pts = new ColoredPoints();
            int OneSize = (int)Math.Sqrt(size);
            int colorindex = 0;
            //add some points with diferent colors
            for (int i = 0; i < OneSize; i++)
            {
                for (int j = 0; j < OneSize; j++)
                {
                    pts.AddItem(new ColoredPoint(i, j, 0, doc.Palette[colorindex % 255].SystemColor));
                    colorindex++;
                }
            }
            //Create a new pointCloud with calculated points
            Custom_PointCLoud mpts = new Custom_PointCLoud();
            mpts.SetUnRegisterDocument(doc);
            mpts.setDocumentDefaults();

            mpts.nodes = pts;
            //add the pointCloud to the model entities collection
            doc.Model.Entities.AddItem(mpts);
            //send a redraw
            doc.Redraw(true);
        }
        public Custom_PointCLoud()
        {

        }
        //basic field properties
        ColoredPoints nodes = new ColoredPoints();
        public ColoredPoints Nodes { get { return nodes; } set { nodes = value; } }
        public override void MatchProperties(vdPrimary _from, vdDocument thisdocument)
        {
            if (this == _from) return;
            base.MatchProperties(_from, thisdocument);
            Custom_PointCLoud from = _from as Custom_PointCLoud;
            if (from == null) return;
            nodes = from.nodes.Clone() as ColoredPoints;
        }
        //always draw as a single pixel
        public override int PointSize { get => 1; }
        public override int Count => nodes.Count;
        public override void GetPointAt(int index, ref double x, ref double y, ref double z)
        {
            ColoredPoint pt = nodes[index];
            x = pt.x;
            y = pt.y;
            z = pt.z;
        }
        public override bool GetColorAt(int index, ref byte red, ref byte green, ref byte blue)
        {
            ColoredPoint pt = nodes[index];
            red = pt.Color.R;
            green = pt.Color.G;
            blue = pt.Color.B;
            return true;
        }
        public override void Serialize(Serializer serializer)
        {
            base.Serialize(serializer);
            serializer.Serialize(nodes, "nodes");
        }
        public override bool DeSerialize(DeSerializer deserializer, string fieldname, object value)
        {
            if (base.DeSerialize(deserializer, fieldname, value)) return true;
            else if (fieldname == "nodes") nodes = (ColoredPoints)value;
            else return false;
            return true;
        }
    }

Send comments on this topic.