-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlueNoisePopulate2D.cs
164 lines (140 loc) · 4.98 KB
/
BlueNoisePopulate2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using System.Drawing;
using Rhino;
using System.Net;
using System.IO;
using Grasshopper.Kernel.Data;
using Rhino.Geometry;
using System.Runtime.InteropServices;
namespace PointGenerator
{
internal static class UnsafeNativeMethods
{
const string _dllLocation = "LowDiscBlueNoise.dll";
[DllImport(_dllLocation)]
public static extern void initSamplers();
[DllImport(_dllLocation)]
public static extern int ldbnBNOT(int samples);
[DllImport(_dllLocation)]
public static extern IntPtr getPointAt(int i);
[DllImport(_dllLocation)]
public static extern void clearSamplers();
}
public class Populate2D : GH_Component
{
public Populate2D()
: base("Blue Noise Populate 2D", "Blue Noise 2D", "Populate a 2-Dimensional region with points using a low discrepancy blue noise sampling algorithm. For more information visit: https://projet.liris.cnrs.fr/ldbn/", "Vector", "Grid")
{
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddIntegerParameter("Approximate Number of Points", "N", "Approximate Number of Points", GH_ParamAccess.item, 200);
pManager.AddIntervalParameter("Domain", "X", "Domain of the X-axis of the point field", GH_ParamAccess.item, new Interval(0.0, 1.0));
pManager.AddIntervalParameter("Domain", "Y", "Domain of the Y-axis of the point field", GH_ParamAccess.item, new Interval(0.0, 1.0));
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddPointParameter("Points", "P", "Points", GH_ParamAccess.list);
}
public static void InitSamplers()
{
UnsafeNativeMethods.initSamplers();
}
public static void ClearSamplers()
{
UnsafeNativeMethods.clearSamplers();
}
public static int GeneratePts(int samples)
{
return UnsafeNativeMethods.ldbnBNOT(samples);
}
public static Point3d getPointAt(int i)
{
IntPtr ptr = UnsafeNativeMethods.getPointAt(i);
double[] pt = new double[2];
Marshal.Copy(ptr, pt, 0, 2);
if (pt.Length < 0) pt = new double[2] { 0.0, 0.0 };
return new Point3d(pt[0], pt[1], 0.0);
}
public override void RemovedFromDocument(GH_Document document)
{
ClearSamplers();
base.RemovedFromDocument(document);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
int samples = 200;
Interval mi_x = new Interval();
Interval mi_y = new Interval();
if (
DA.GetData<int>(0, ref samples) &&
DA.GetData<Interval>(1, ref mi_x) &&
DA.GetData<Interval>(2, ref mi_y)
)
{
Interval nx = new Interval();
Interval ny = new Interval();
double sf = 1.0;
if(mi_x.Length >= mi_y.Length)
{
nx = new Interval(0.0, 1.0);
ny = new Interval(0.0, mi_y.Length / mi_x.Length);
sf = mi_x.Length;
}
else
{
ny = new Interval(0.0, 1.0);
nx = new Interval(0.0, mi_x.Length / mi_y.Length);
sf = mi_y.Length;
}
List<Point3d> points = new List<Point3d>();
InitSamplers();
int pointCount = GeneratePts(samples);
double m_x = nx.Length;
double m_y = ny.Length;
Point3d p = Point3d.Origin;
for (int i = 0; i < pointCount - 1; i++)
{
p = getPointAt(i);
if(nx.IncludesParameter(p.X) && ny.IncludesParameter(p.Y))
{
p *= sf;
p.X += mi_x.T0;
p.Y += mi_y.T0;
points.Add(p);
}
}
DA.SetDataList(0, points);
}
}
public override Guid ComponentGuid
{
get
{
return new Guid("{5d406e66-51a3-4b24-92ff-3ae1840609eb}");
}
}
public override GH_Exposure Exposure
{
get
{
return GH_Exposure.secondary;
}
}
protected override Bitmap Icon
{
get
{
return PointGenerator.Properties.Resources.Icon_BlueNoise_2D;
}
}
}
}