Repository navigation
Expand file tree
/
Copy pathGradientDensity.java
More file actions
131 lines (101 loc) · 3.29 KB
/
Copy pathGradientDensity.java
File metadata and controls
131 lines (101 loc) · 3.29 KB
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
package clojure2d.java;
import fastmath.vector.Vec4;
import net.jafama.FastMath;
import fastmath.java.PrimitiveMath;
import java.awt.Color;
import clojure.lang.IFn;
import clojure2d.java.reconstruction.AFilter;
public class GradientDensity {
public int w,h,size;
public AFilter f;
private int wdec, hdec;
public double[] cnt;
public GradientDensity(int w, int h, AFilter f) {
this.f = f;
this.w = w;
this.h = h;
wdec = w - 1;
hdec = h - 1;
size = w * h;
cnt = new double[size];
}
public GradientDensity(int w, int h) {
this(w, h, null);
}
public void addPixel(double px, double py) {
if(f==null) {
set(px,py);
} else {
setFiltered(px,py);
}
}
public void addPixel(double px, double py, double v) {
if(f==null) {
set(px,py,v);
} else {
setFiltered(px,py,v);
}
}
// pbrt way
public void setFiltered(double fx, double fy, double v) {
int p0x = Math.max((int)FastMath.ceil(fx-0.5-f.radius), 0);
int p0y = Math.max((int)FastMath.ceil(fy-0.5-f.radius), 0);
int p1x = Math.min((int)FastMath.floor(fx-0.5+f.radius)+1, wdec);
int p1y = Math.min((int)FastMath.floor(fy-0.5+f.radius)+1, hdec);
int diffx = p1x-p0x;
int diffy = p1y-p0y;
if (diffx>0 && diffy>0) {
for (int y=p0y; y<p1y; y++) {
int yy = Math.min((int)FastMath.floor(FastMath.abs((y-fy)*f.iradius16)), 15) << 4;
for (int x=p0x; x<p1x; x++) {
int xx = Math.min((int)FastMath.floor(FastMath.abs((x-fx)*f.iradius16)), 15);
set(x, y, v*f.filterTable[yy+xx]);
}
}
}
}
public void setFiltered(double fx, double fy) {
setFiltered(fx,fy,1.0);
}
public void set(double x, double y, double v) {
int xx = (int)(x+0.5);
int yy = (int)(y+0.5);
if(xx>=0 && xx<w && yy>=0 && yy<h) {
cnt[yy*w+xx]+=v;
}
}
public void set(double x, double y) {
set(x,y,1.0);
}
public void set(int x, int y, double weight) {
cnt[y*w+x] += weight;
}
public class Config {
public double mx, lmx;
public boolean is_log;
public IFn gradient;
public Config(boolean is_log_, IFn gradient_) {
is_log = is_log_;
gradient = gradient_;
mx = Double.MIN_VALUE;
for (int i=0; i<size; i++) {
if (cnt[i]>mx) mx=cnt[i];
}
lmx = 1.0 / FastMath.log1p(mx);
mx = 1.0 / mx;
}
}
public void toPixels(int[] res, int start, int end, Config conf) {
for (int i=start; i<end; i++) {
double hit = Math.max(0,cnt[i]);
double pos = conf.is_log ? FastMath.log(hit+1.0) * conf.lmx : hit * conf.mx;
Pixels.setColor(res, i, (Vec4)conf.gradient.invoke(pos));
}
}
public void toPixels(int[] res, Config conf) {
toPixels(res, 0, cnt.length, conf);
}
public void merge(GradientDensity d) {
for(int i=0; i<size; i++) { cnt[i] += d.cnt[i]; }
}
}