/// <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) { List <Point3d> P = new List <Point3d>(); if (!DA.GetDataList(0, P)) { return; } if (P == null || P.Count == 0) { return; } List <Color> colors = new List <Color>(); if (!DA.GetDataList(1, colors)) { foreach (Point3d p in P) { colors.Add(Color.Black); } } if (colors.Count < P.Count) { Color singleCol = colors[0]; colors.Clear(); foreach (Point3d p in P) { colors.Add(singleCol); } } int s = 1; DA.GetData(2, ref s); if (s < 1) { return; } _size = s; _cloud = new PointCloud(); _cloud.AddRange(P, colors); _clip = _cloud.GetBoundingBox(false); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { Random r = new Random(13); var plcnt = 1000000; var edge = 10; GetNumber gn = new GetNumber(); gn.SetCommandPrompt("How many pointsies"); gn.SetDefaultInteger(plcnt); gn.SetUpperLimit(500000001, true); gn.SetLowerLimit(1, true); gn.AcceptNothing(true); var gnrc = gn.Get(); if (gnrc == GetResult.Nothing || gnrc == GetResult.Number) { var nr = (int)gn.Number(); if (nr > 500000000) { RhinoApp.WriteLine("More than 500.000.000 points"); return(Result.Cancel); } pc = new PointCloud(); Point3dList p3dlist = new Point3dList(nr); List <System.Drawing.Color> collist = new List <System.Drawing.Color>(nr); for (int i = 0; i < nr; i++) { var d = (double)i; p3dlist.Add(new Point3d(r.NextDouble() * edge, r.NextDouble() * edge, r.NextDouble() * edge)); collist.Add(System.Drawing.Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))); } pc.AddRange(p3dlist, collist); cnd.ThePc = pc; cnd.Enabled = true; } doc.Views.Redraw(); return(Result.Success); }
private void ProcessingThread() { while (running) { double[] tmpList = new double[100]; for (int i = 0; i < 100; i++) { tmpList[i] = rnd.NextDouble(); } this.Dispatcher.BeginInvoke(new Action(() => { PointCloud.Clear(); PointCloud.AddRange(tmpList); }), System.Windows.Threading.DispatcherPriority.Background); Thread.Sleep(50); } }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { // TODO: complete command. // get picture object ObjRef objRef = null; using (var go = new GetObject()) { go.SetCommandPrompt("Select a picture object to sample"); go.GeometryFilter = ObjectType.Surface; go.Get(); if (go.CommandResult() != Result.Success) { return(go.CommandResult()); } objRef = go.Object(0); } // get rhino object RhinoObject rhinoObject = objRef.Object(); // extract bitmap texture from render material Xml RenderMaterial renderMaterial = rhinoObject.RenderMaterial; XmlDocument xml = new XmlDocument(); xml.LoadXml(renderMaterial.Xml); var node = xml.SelectSingleNode("material/simulation/Texture-1-filename"); var filePath = node.InnerText; var bitmap = new Bitmap(Image.FromFile(filePath)); // get bounding box var bbox = rhinoObject.Geometry.GetBoundingBox(Plane.WorldXY, out var worldBox); // get width / height of bbox double bboxWidth = bbox.Corner(false, true, true).DistanceTo(bbox.Corner(true, true, true)); double bboxHeight = bbox.Corner(true, false, true).DistanceTo(bbox.Corner(true, true, true)); // get width / height of image int imgWidth = bitmap.Width; int imgHeight = bitmap.Height; // Calculate stretch factor double stretchFactor = bboxWidth / imgWidth; // Test stretch factor consistency if (!(Math.Abs(imgHeight * stretchFactor - bboxHeight) < doc.ModelAbsoluteTolerance)) { RhinoApp.WriteLine("Selected Picture object is scaled non-uniform! Aborting command"); return(Result.Failure); } // get starting position var startPt = bbox.Min; // create grid of points var plane = worldBox.Plane; plane.Origin = startPt; Point3d[] points = new Point3d[imgWidth * imgHeight]; Color[] colors = new Color[imgWidth * imgHeight]; Vector3d[] normals = new Vector3d[imgWidth * imgHeight]; Vector3d normal = Vector3d.ZAxis; for (int i = 0; i < imgHeight; i++) { for (int j = 0; j < imgWidth; j++) { points[(i + 1) * j] = plane.PointAt(j * stretchFactor, i * stretchFactor); colors[(i + 1) * j] = bitmap.GetPixel(j, i); normals[(i + 1) * j] = normal; } } // create Point cloud from grid PointCloud pc = new PointCloud(); pc.AddRange(points, normals, colors); // add point cloud to doc doc.Objects.AddPointCloud(pc); // redraw doc.Views.Redraw(); // return success return(Result.Success); }
/// <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) { ///define i/o parameters PointCloud Rhino_Cloud = new PointCloud(); Double k = 6; /// read inputs if (!DA.GetData(0, ref Rhino_Cloud)) { return; } if (!DA.GetData("k", ref k)) { return; } if (Rhino_Cloud.Count <= k) { throw new Size_Exception(string.Format("Use point clouds with more than k points")); } /// interal parameters List <Vector3d> normals = new List <Vector3d>(); if (!Rhino_Cloud.ContainsNormals) { var X = Rhino_Cloud.Select(x => x.X).ToList(); var Y = Rhino_Cloud.Select(x => x.Y).ToList(); var Z = Rhino_Cloud.Select(x => x.Z).ToList(); ///2. var Matlab_X = new MWNumericArray(Rhino_Cloud.Count, 1, X.ToArray()); var Matlab_Y = new MWNumericArray(Rhino_Cloud.Count, 1, Y.ToArray()); var Matlab_Z = new MWNumericArray(Rhino_Cloud.Count, 1, Z.ToArray()); /// 3. Segmentation.segment segment_mesh = new Segmentation.segment(); var mwca = (MWCellArray)segment_mesh.G_Normals(Matlab_X, Matlab_Y, Matlab_Z, k); /// 4. MWNumericArray na0 = (MWNumericArray)mwca[1]; double[] dc0 = (double[])na0.ToVector(0); MWNumericArray na1 = (MWNumericArray)mwca[2]; double[] dc1 = (double[])na1.ToVector(0); MWNumericArray na2 = (MWNumericArray)mwca[3]; double[] dc2 = (double[])na2.ToVector(0); /// 5. var Rhino_param0 = new List <double>(dc0); var Rhino_param1 = new List <double>(dc1); var Rhino_param2 = new List <double>(dc2); Vector3d R = new Vector3d(); for (int i = 0; i < Rhino_param0.Count; i++) { R = new Vector3d(Rhino_param0[i], Rhino_param1[i], Rhino_param2[i]); normals.Add(R); } } else { normals = Rhino_Cloud.GetNormals().ToList(); } PointCloud Rhino_Cloud_out = new PointCloud(); Rhino_Cloud_out.AddRange(Rhino_Cloud.GetPoints(), normals); GH_Cloud Rhino_Cloud_out2 = new GH_Cloud(Rhino_Cloud_out); /// 6. DA.SetData(0, Rhino_Cloud_out2); }
public bool Import(string path, out PointCloud pc, bool intensity = false) { Stopwatch watch = new Stopwatch(); watch.Start(); pc = new PointCloud(); string header; if (!OpenFile(path, out header)) { return(false); } if (!ParseHeader(header)) { if (this.br != null) { this.br.Close(); } return(false); } log += string.Format("time {0}\n", watch.ElapsedMilliseconds); pc.UserDictionary.Set("width", this.width); pc.UserDictionary.Set("height", this.height); if (this.binary) { //byte[] buffer; int log_step = this.num_points / 5; //PCD_PointBuilder pbb; byte[] total_buffer = br.ReadBytes(psize * num_points); Point3d[] points = new Point3d[num_points]; Vector3d[] normals = new Vector3d[num_points]; Color[] colors = new Color[num_points]; Parallel.For(0, num_points, i => { int total_pos = i * psize; int pos = total_pos; PCD_PointBuilder pbb = new PCD_PointBuilder(); for (int k = 0; k < m_fields.Count; ++k) { m_fields[k].read(total_buffer, pos, ref pbb); pos += m_fields[k].size; points[i] = pbb.Point() * scale; normals[i] = pbb.Normal(); if (intensity) { colors[i] = Color.FromArgb(pbb.Intensity()); } else { colors[i] = Color.FromArgb(pbb.Color()); } } }); pc.AddRange(points, normals, colors); /* * for (int i = 0, j = 0; i < this.num_points; ++i, j+=psize) * { * int pos = j; * pbb = new PCD_PointBuilder(); * * for (int k = 0; k < this.fields.Count; ++k) * { * this.fields[k].read(total_buffer, pos, ref pbb); * pos += this.fields[k].size; * } * if (intensity) * pc.Add(pbb.Point() * scale, pbb.Normal(), System.Drawing.Color.FromArgb(pbb.Intensity())); * else * pc.Add(pbb.Point() * scale, pbb.Normal(), System.Drawing.Color.FromArgb(pbb.Color())); * } */ log += string.Format("time {0}\n", watch.ElapsedMilliseconds); this.br.Close(); if (use_transform) { pc.Transform(this.scan_transform); } return(true); } else { // read all data into buffer byte[] buffer = br.ReadBytes((int)br.BaseStream.Length - (int)br.BaseStream.Position); // parse buffer as ASCII text string bufferStr = System.Text.Encoding.Default.GetString(buffer); // split string buffer into list of strings List <string> lines = new List <string>(bufferStr.Split('\n')); if (lines.Count != this.num_points) { log += "Header data doesn't match up with number of data lines!\n"; } // parse points from line data for (int i = 0; i < lines.Count; ++i) { PCD_PointBuilder pbb = new PCD_PointBuilder(); string[] tokens = lines[i].Split((char[])null, StringSplitOptions.RemoveEmptyEntries); //log += lines[i]; //log += " " + tokens.Length.ToString() + "\n"; if (tokens.Length != this.m_fields.Count) { continue; } for (int j = 0; j < this.m_fields.Count; ++j) { this.m_fields[j].readASCII(tokens[j], ref pbb); } if (intensity) { pc.Add(pbb.Point() * this.scale, pbb.Normal(), System.Drawing.Color.FromArgb(pbb.Intensity())); } else { pc.Add(pbb.Point() * this.scale, pbb.Normal(), System.Drawing.Color.FromArgb(pbb.Color())); } } this.br.Close(); log += string.Format("time {0}\n", watch.ElapsedMilliseconds); if (use_transform) { pc.Transform(this.scan_transform); } log += string.Format("time {0}\n", watch.ElapsedMilliseconds); return(true); } }