Skip to content

Commit 43282e0

Browse files
committed
Added experimental blurring/averaging
Added Gaussian(), Mean(), Median() functions to Voxels class. Consider experimental for now. We may remove them again, if we deem them not robust enough for engineering applications.
1 parent 5410b67 commit 43282e0

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// SPDX-License-Identifier: CC0-1.0
3+
//
4+
// This example code file is released to the public under Creative Commons CC0.
5+
// See https://creativecommons.org/publicdomain/zero/1.0/legalcode
6+
//
7+
// To the extent possible under law, LEAP 71 has waived all copyright and
8+
// related or neighboring rights to this PicoGK example code file.
9+
//
10+
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
11+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16+
// THE SOFTWARE.
17+
//
18+
19+
using PicoGK;
20+
using System.Numerics;
21+
22+
namespace PicoGKExamples
23+
{
24+
///////////////////////////////////////////////////////////////////////////
25+
// Below is a static class that implements a single static function
26+
// that can be called from Library::Go()
27+
28+
/// <summary>
29+
/// This function tests the EXPERIMENTAL blurring and averaging functions
30+
/// introduced in PicoGK 1.5.
31+
///
32+
/// We are still debating whether to keep them in, as they are potentially
33+
/// not suitable for engineering applications.
34+
///
35+
/// hey work reasonably well, but are mostly "visual" functions that
36+
/// require trial and error to get what you want.
37+
///
38+
/// They are quite sensitive to voxel sizes.
39+
///
40+
/// They also seem to create artifacts at higher size parameters.
41+
///
42+
/// DoubleOffset, TripleOffset are (slower) alternatives.
43+
///
44+
/// </summary>
45+
46+
class BlurringAndAveraging
47+
{
48+
public static void Task()
49+
{
50+
try
51+
{
52+
// Create a mesh from an existing STL file
53+
Mesh msh = Mesh.mshFromStlFile(
54+
Path.Combine( Utils.strPicoGKSourceCodeFolder(),
55+
"Examples/Testfiles/Teapot.stl"));
56+
57+
// Add it to the viewer (moving it 20mm to the side)
58+
Library.oViewer().Add(new Voxels(msh));
59+
60+
float fStart = 0.1f;
61+
float fEnd = 1.0f;
62+
float fSteps = 5;
63+
float fDiff = (fEnd - fStart)/fSteps;
64+
65+
Library.oViewer().SetGroupMaterial(0, "AAAAAA", 0.2f, 0.5f);
66+
Library.oViewer().SetGroupMaterial(1, "FF0000", 0.2f, 0.5f);
67+
Library.oViewer().SetGroupMaterial(2, "00FF00", 0.2f, 0.5f);
68+
Library.oViewer().SetGroupMaterial(3, "0000FF", 0.2f, 0.5f);
69+
70+
{
71+
float fOffset = 0f;
72+
for (float f=fStart; f<=fEnd; f+=fDiff)
73+
{
74+
Voxels vox = new(msh);
75+
vox.Gaussian(f);
76+
77+
Mesh mshVoxelized = new(vox);
78+
Library.oViewer().Add(mshVoxelized.mshCreateTransformed( Vector3.One,
79+
new Vector3(fOffset, 20, 0)),
80+
1);
81+
fOffset += 20f;
82+
}
83+
}
84+
85+
{
86+
float fOffset = 0f;
87+
for (float f=fStart; f<=fEnd; f+=fDiff)
88+
{
89+
Voxels vox = new(msh);
90+
vox.Median(f);
91+
92+
Mesh mshVoxelized = new(vox);
93+
Library.oViewer().Add(mshVoxelized.mshCreateTransformed( Vector3.One,
94+
new Vector3(fOffset, 40, 0)),
95+
2);
96+
fOffset += 20f;
97+
}
98+
}
99+
100+
{
101+
float fOffset = 0f;
102+
for (float f=fStart; f<=fEnd; f+=fDiff)
103+
{
104+
Voxels vox = new(msh);
105+
vox.Mean(f);
106+
107+
Mesh mshVoxelized = new(vox);
108+
Library.oViewer().Add(mshVoxelized.mshCreateTransformed( Vector3.One,
109+
new Vector3(fOffset, 60, 0)),
110+
3);
111+
fOffset += 20f;
112+
}
113+
}
114+
115+
116+
}
117+
118+
catch (Exception e)
119+
{
120+
Library.Log($"Failed to run example: \n{e.Message}"); ;
121+
}
122+
}
123+
}
124+
}
125+

PicoGK_Voxels.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,33 @@ public void DoubleOffset( float fDist1MM,
199199
public void TripleOffset( float fDistMM)
200200
=> _TripleOffset(m_hThis, fDistMM);
201201

202+
/// <summary>
203+
/// Applies a Gaussian Blur to the voxel field with the specified size
204+
/// EXPERIMENTAL - We may remove this function again, if we determine
205+
/// it isn't suitable for engineering applications
206+
/// </summary>
207+
/// <param name="fDistMM">The size of the Gaussian kernel applied</param>
208+
public void Gaussian(float fSizeMM)
209+
=> _Gaussian(m_hThis, fSizeMM);
210+
211+
/// <summary>
212+
/// Applies a median avergage to the voxel field with the specified size
213+
/// EXPERIMENTAL - We may remove this function again, if we determine
214+
/// it isn't suitable for engineering applications
215+
/// </summary>
216+
/// <param name="fDistMM">The size of the median average kernel applied</param>
217+
public void Median(float fSizeMM)
218+
=> _Median(m_hThis, fSizeMM);
219+
220+
/// <summary>
221+
/// Applies a mean avergage to the voxel field with the specified size
222+
/// EXPERIMENTAL - We may remove this function again, if we determine
223+
/// it isn't suitable for engineering applications
224+
/// </summary>
225+
/// <param name="fDistMM">The size of the mean average kernel applied</param>
226+
public void Mean(float fSizeMM)
227+
=> _Mean(m_hThis, fSizeMM);
228+
202229
/// <summary>
203230
/// Renders a mesh into the voxel field, combining it with
204231
/// the existing content

PicoGK__Interop.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ private static extern void _DoubleOffset( IntPtr hThis,
272272
private static extern void _TripleOffset( IntPtr hThis,
273273
float fOffset);
274274

275+
[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Voxels_Gaussian")]
276+
private static extern void _Gaussian( IntPtr hThis,
277+
float fDistance);
278+
279+
[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Voxels_Median")]
280+
private static extern void _Median( IntPtr hThis,
281+
float fDistance);
282+
283+
[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Voxels_Mean")]
284+
private static extern void _Mean( IntPtr hThis,
285+
float fDistance);
286+
275287
[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Voxels_RenderMesh")]
276288
private static extern void _RenderMesh( IntPtr hThis,
277289
IntPtr hMesh);

0 commit comments

Comments
 (0)