FiberVISH 0.2
Fish - The Fiber Bundle API for the Vish Visualization Shell
Median.hpp
1#ifndef EYE_SHADERS_MEDIAN_HPP
2#define EYE_SHADERS_MEDIAN_HPP
3
4#include <shrimp/ColorTypes.hpp>
5
6#include "shadersDllApi.h"
7
12extern shaders_API constinit const char Median_GLSL[];
13
14
15#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b);
16#define mn3(a, b, c) s2(a, b); s2(a, c);
17#define mx3(a, b, c) s2(b, c); s2(a, c);
18
19#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges
20#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges
21#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges
22#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges
23
24
25/*
263x3 Median
27Morgan McGuire and Kyle Whitson
28http://graphics.cs.williams.edu
29https://casual-effects.com/research/McGuire2008Median/median.pix
30
31
32Copyright (c) Morgan McGuire and Williams College, 2006
33All rights reserved.
34
35Redistribution and use in source and binary forms, with or without
36modification, are permitted provided that the following conditions are
37met:
38
39Redistributions of source code must retain the above copyright notice,
40this list of conditions and the following disclaimer.
41
42Redistributions in binary form must reproduce the above copyright
43notice, this list of conditions and the following disclaimer in the
44documentation and/or other materials provided with the distribution.
45
46THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
49A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57*/
58template <class T>
59inline T median(std::array<T,9> v)
60{
61T temp;
62
63 // Starting with a subset of size 6, remove the min and max each time
64 mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);
65 mnmx5(v[1], v[2], v[3], v[4], v[6]);
66 mnmx4(v[2], v[3], v[4], v[7]);
67 mnmx3(v[3], v[4], v[8]);
68
69 return v[4];
70}
71
72#undef s2
73#undef mn3
74#undef mx3
75#undef mnmx3
76#undef mnmx4
77#undef mnmx5
78#undef mnmx6
79
80
81
82
83
84
122#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b);
123#define t2(a, b) s2(v[a], v[b]);
124#define t24(a, b, c, d, e, f, g, h) t2(a, b); t2(c, d); t2(e, f); t2(g, h);
125#define t25(a, b, c, d, e, f, g, h, i, j) t24(a, b, c, d, e, f, g, h); t2(i, j);
126
127template <class T>
128inline T median(std::array<T,25> & v)
129{
130T temp;
131
132 t25(0, 1, 3, 4, 2, 4, 2, 3, 6, 7);
133 t25(5, 7, 5, 6, 9, 7, 1, 7, 1, 4);
134 t25(12, 13, 11, 13, 11, 12, 15, 16, 14, 16);
135 t25(14, 15, 18, 19, 17, 19, 17, 18, 21, 22);
136 t25(20, 22, 20, 21, 23, 24, 2, 5, 3, 6);
137 t25(0, 6, 0, 3, 4, 7, 1, 7, 1, 4);
138 t25(11, 14, 8, 14, 8, 11, 12, 15, 9, 15);
139 t25(9, 12, 13, 16, 10, 16, 10, 13, 20, 23);
140 t25(17, 23, 17, 20, 21, 24, 18, 24, 18, 21);
141 t25(19, 22, 8, 17, 9, 18, 0, 18, 0, 9);
142 t25(10, 19, 1, 19, 1, 10, 11, 20, 2, 20);
143 t25(2, 11, 12, 21, 3, 21, 3, 12, 13, 22);
144 t25(4, 22, 4, 13, 14, 23, 5, 23, 5, 14);
145 t25(15, 24, 6, 24, 6, 15, 7, 16, 7, 19);
146 t25(3, 11, 5, 17, 11, 17, 9, 17, 4, 10);
147 t25(6, 12, 7, 14, 4, 6, 4, 7, 12, 14);
148 t25(10, 14, 6, 7, 10, 12, 6, 10, 6, 17);
149 t25(12, 17, 7, 17, 7, 10, 12, 18, 7, 12);
150 t24(10, 18, 12, 20, 10, 20, 10, 12);
151
152 return v[12];
153}
154
155#undef s2
156#undef t2
157#undef t25
158#undef t24
159
160
161#endif // EYE_SHADERS_MEDIAN_HPP