Skip to content
57 changes: 27 additions & 30 deletions CockroachGH/Cleaning/CloudBoxCrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ public class CloudBoxCrop : GH_Component {

public CloudBoxCrop()
: base("CloudBoxCrop", "CloudBoxCrop",
"CloudBoxCrop",
"Cockroach", "Crop") {
"Crop a PointCloud with one or multiple Boxes, returning either the inside or the outside of the Box(es) as a new PointCloud.",
"Cockroach", "Crop") {
}
public override GH_Exposure Exposure => GH_Exposure.quarternary;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);
pManager.AddBrepParameter("Boxes", "B", "Boxes",GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I","Get Outside part of box", GH_ParamAccess.item, false);
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The PointCloud to crop.", GH_ParamAccess.item);
pManager.AddBrepParameter("Boxes", "B", "The Box(es) to crop PointCloud with.",GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I","If set to True, will get the outside part of the Box.", GH_ParamAccess.item, false);
pManager[2].Optional = true;
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {

pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);

pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The cropped PointCloud.", GH_ParamAccess.item);
}

protected override void SolveInstance(IGH_DataAccess DA) {
Expand All @@ -43,21 +41,16 @@ protected override void SolveInstance(IGH_DataAccess DA) {

PointCloud c = CropBox(cgh.Value, boxes, inverse, true);


DA.SetData(0, new GH_Cloud(c));


}


public PointCloud CropBox(PointCloud cloud, List<Brep> boxes, bool inverse = false, bool parallel = false) {


bool[] flags = new bool[cloud.Count];

foreach (var brep in boxes) {


foreach (var brep in boxes)
{
brep.Surfaces[0].TryGetPlane(out Plane plane);
Box box = new Box(plane, brep);

Expand All @@ -74,23 +67,22 @@ public PointCloud CropBox(PointCloud cloud, List<Brep> boxes, bool inverse = fal
Point3d Max = b.PointAt(1, 1, 1);
//Rhino.RhinoDoc.ActiveDoc.Objects.AddBox(b);

System.Threading.Tasks.Parallel.For(0, cloudCopy.Count, i => {

System.Threading.Tasks.Parallel.For(0, cloudCopy.Count, i =>
{
if (flags[i]) return;
var p = cloudCopy[i].Location;
bool flag = (Min.X < p.X) && (Max.X > p.X) && (Min.Y < p.Y) && (Max.Y > p.Y) && (Min.Z < p.Z) && (Max.Z > p.Z);
if (flag) flags[i] = true;

});
}



int count = 0;
List<int> idList = new List<int>();
for (int i = 0; i < cloud.Count; i++) {
for (int i = 0; i < cloud.Count; i++)
{
bool f = inverse ? !flags[i] : flags[i];
if (f) {
if (f)
{
idList.Add(i);
count++;
}
Expand All @@ -100,22 +92,28 @@ public PointCloud CropBox(PointCloud cloud, List<Brep> boxes, bool inverse = fal
Point3d[] points = new Point3d[count];
Vector3d[] normals = new Vector3d[count];
Color[] colors = new Color[count];
double[] pvalues = new double[count];

System.Threading.Tasks.Parallel.For(0, idArray.Length, i => {


System.Threading.Tasks.Parallel.For(0, idArray.Length, i =>
{
int id = idArray[i];
var p = cloud[(int)id];
points[i] = p.Location;
normals[i] = p.Normal;
colors[i] = p.Color;

pvalues[i] = p.PointValue;
});

PointCloud croppedCloud = new PointCloud();
croppedCloud.AddRange(points, normals, colors);


if (cloud.ContainsPointValues)
{
croppedCloud.AddRange(points, normals, colors, pvalues);
}
else
{
croppedCloud.AddRange(points, normals, colors);
}



//for (int i = 0; i < cloud.Count; i++) {
Expand All @@ -129,7 +127,6 @@ public PointCloud CropBox(PointCloud cloud, List<Brep> boxes, bool inverse = fal
// }
//}
return croppedCloud;

}

//public void CropCloud(PointCloud cloud, ref bool[] flags, Box box, bool inverse = false, bool parallel = false) {
Expand Down
56 changes: 26 additions & 30 deletions CockroachGH/Cleaning/CloudMeshCrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ public class CloudMeshCrop : GH_Component {

public CloudMeshCrop()
: base("CloudMeshCrop", "CloudMeshCrop",
"CloudMeshCrop",
"Cockroach", "Crop") {
"Crop a PointCloud with one or multiple Meshes, returning either the inside or the outside of the Mesh(es) as a new PointCloud.",
"Cockroach", "Crop") {
}
public override GH_Exposure Exposure => GH_Exposure.quarternary;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);
pManager.AddMeshParameter("Meshes", "M", "Meshes", GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I","Get Outside part of box", GH_ParamAccess.item, false);
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The PointCloud to crop.", GH_ParamAccess.item);
pManager.AddMeshParameter("Meshes", "M", "The Mesh(es) to crop the PointCloud with.", GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I", "If set to True, will get the outside part of the Mesh.", GH_ParamAccess.item, false);
pManager[2].Optional = true;
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {

pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);

pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The cropped PointCloud.", GH_ParamAccess.item);
}

protected override void SolveInstance(IGH_DataAccess DA) {
Expand All @@ -41,19 +39,15 @@ protected override void SolveInstance(IGH_DataAccess DA) {
bool inverse = false;
DA.GetData(2, ref inverse);


PointCloud c = CropCloud(cgh.Value, meshes,inverse);

DA.SetData(0, new GH_Cloud(c));


}


public PointCloud CropCloud(PointCloud cloud, List<Mesh> M, bool inverse = false) {

RTree rTree = Rhino.Geometry.RTree.CreatePointCloudTree(cloud);


bool[] flags = new bool[cloud.Count];

Expand All @@ -64,9 +58,10 @@ public PointCloud CropCloud(PointCloud cloud, List<Mesh> M, bool inverse = false
var boxSearchData = new BoxSearchData();
rTree.Search(bBox, BoundingBoxCallback, boxSearchData);

foreach (int id in boxSearchData.Ids) {

if (m.IsPointInside(cloud[id].Location, 0, true)) {
foreach (int id in boxSearchData.Ids)
{
if (m.IsPointInside(cloud[id].Location, 0, true))
{
flags[id] = true;
}
}
Expand All @@ -75,9 +70,11 @@ public PointCloud CropCloud(PointCloud cloud, List<Mesh> M, bool inverse = false

int count = 0;
List<int> idList = new List<int>();
for (int i = 0; i < cloud.Count; i++) {
for (int i = 0; i < cloud.Count; i++)
{
bool f = inverse ? !flags[i] : flags[i];
if (f) {
if (f)
{
idList.Add(i);
count++;
}
Expand All @@ -87,21 +84,27 @@ public PointCloud CropCloud(PointCloud cloud, List<Mesh> M, bool inverse = false
Point3d[] points = new Point3d[count];
Vector3d[] normals = new Vector3d[count];
Color[] colors = new Color[count];
double[] pvalues = new double[count];

System.Threading.Tasks.Parallel.For(0, idArray.Length, i => {


System.Threading.Tasks.Parallel.For(0, idArray.Length, i =>
{
int id = idArray[i];
var p = cloud[(int)id];
points[i] = p.Location;
normals[i] = p.Normal;
colors[i] = p.Color;

pvalues[i] = p.PointValue;
});

PointCloud croppedCloud = new PointCloud();
croppedCloud.AddRange(points, normals, colors);

if (cloud.ContainsPointValues)
{
croppedCloud.AddRange(points, normals, colors, pvalues);
}
else
{
croppedCloud.AddRange(points, normals, colors);
}

//for (int i = 0; i < f.Length; i++) {
// bool flag = inverse ? !f[i] : f[i];
Expand Down Expand Up @@ -132,13 +135,6 @@ public BoxSearchData() {
public List<int> Ids { get; set; }
}








public override Guid ComponentGuid {
get { return new Guid("98aaf1ed-7834-41c8-8fd6-edf88cd44ba1"); }
}
Expand Down
55 changes: 30 additions & 25 deletions CockroachGH/Cleaning/CloudPolygonCrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ public class CloudPolygonCrop : GH_Component {

public CloudPolygonCrop()
: base("CloudPolygonCrop", "CloudPolygonCrop",
"CloudPolygonCrop",
"Cockroach", "Crop") {
"Crop a PointCloud with one or multiple closed Polylines, returning either the inside or the outside of the Polyline(s) as a new PointCloud.",
"Cockroach", "Crop") {
}
public override GH_Exposure Exposure => GH_Exposure.quarternary;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);
pManager.AddCurveParameter("Polylines", "P", "Closed Polylines",GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I","Get Outside part of box", GH_ParamAccess.item, false);
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The PointCloud to crop.", GH_ParamAccess.item);
pManager.AddCurveParameter("Polylines", "P", "Closed Polylines to crop the PointCloud with.",GH_ParamAccess.list);
pManager.AddBooleanParameter("Inverse","I", "If set to True, will get the outside part of the Polygon(s).", GH_ParamAccess.item, false);
pManager[2].Optional = true;
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {

pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "PointCloud", GH_ParamAccess.item);
pManager.AddParameter(new Param_Cloud(), "PointCloud", "C", "The cropped PointCloud.", GH_ParamAccess.item);

}

Expand Down Expand Up @@ -72,55 +72,60 @@ public PointCloud CropCloud(PointCloud cloud, List<Curve> curves, bool inverse =
cloud_.Transform(xform);
var input = Geometry.RhinClip.PolylineToIntPoint(polyline, scale);

System.Threading.Tasks.Parallel.For(0, cloud.Count, i => {
// for (int i = 0; i < cloud_.Count; i++) {

System.Threading.Tasks.Parallel.For(0, cloud.Count, i =>
{
if (flags[i]) return;

Point3d p = new Point3d(cloud_[i].Location);
var inputP = new IntPoint(p.X * scale, p.Y * scale);

bool flag = Clipper642.Clipper.PointInPolygon(inputP, input) != 0;//;: Clipper642.Clipper.PointInPolygon(inputP, input) == 0;
if (flag)
flags[i] = true;
// }
if (flag) flags[i] = true;
});



}


int count = 0;
List<int> idList = new List<int>();
//System.Threading.Tasks.Parallel.For(0, cloud.Count, i => {
for (int i = 0; i < cloud.Count; i++) {
bool f = inverse ? !flags[i] : flags[i];
if (f) {
idList.Add(i);
count++;
}
for (int i = 0; i < cloud.Count; i++)
{
bool f = inverse ? !flags[i] : flags[i];
if (f)
{
idList.Add(i);
count++;
}
}
//});

int[] idArray = idList.ToArray();
Point3d[] points = new Point3d[count];
Vector3d[] normals = new Vector3d[count];
Color[] colors = new Color[count];
double[] pvalues = new double[count];

System.Threading.Tasks.Parallel.For(0, idArray.Length, i => {


System.Threading.Tasks.Parallel.For(0, idArray.Length, i =>
{
int id = idArray[i];
var p = cloud[(int)id];
points[i] = p.Location;
normals[i] = p.Normal;
colors[i] = p.Color;

pvalues[i] = p.PointValue;
});

PointCloud croppedCloud = new PointCloud();
croppedCloud.AddRange(points, normals, colors);
if (cloud.ContainsPointValues)
{
croppedCloud.AddRange(points, normals, colors, pvalues);
}
else
{
croppedCloud.AddRange(points, normals, colors);
}

//PointCloud croppedCloud = new PointCloud();
//for (int i = 0; i < cloud.Count; i++) {
Expand All @@ -133,8 +138,8 @@ public PointCloud CropCloud(PointCloud cloud, List<Curve> curves, bool inverse =
// }
// }
//}
return croppedCloud;

return croppedCloud;
}


Expand Down
Loading