|
| 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 | + |
0 commit comments