-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchDiffusionLimitedAggregationWithIsoSurfaces.java
executable file
·245 lines (209 loc) · 8.25 KB
/
SketchDiffusionLimitedAggregationWithIsoSurfaces.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.isosurface.MetaBall;
import de.hfkbremen.algorithmiccliches.isosurface.MetaBallManager;
import de.hfkbremen.algorithmiccliches.octree.Octree;
import de.hfkbremen.algorithmiccliches.octree.OctreeEntity;
import processing.core.PApplet;
import processing.core.PVector;
import teilchen.BasicParticle;
import teilchen.util.Overlap;
import java.util.ArrayList;
public class SketchDiffusionLimitedAggregationWithIsoSurfaces extends PApplet {
private static final int NUMBER_OF_PARTICLES_UNATTACHED = 200;
private static final int NUMBER_OF_MAX_PARTICLES = 1000;
private static final int SPHERE_DETAIL = 8;
private final float mOctreeSize = 150;
private Octree mOctree;
private float mRotationZ = 0.1f;
private MetaBallManager mMetaBallManager;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
textFont(createFont("Courier", 11));
strokeWeight(0.25f);
mOctree = new Octree(new PVector(-mOctreeSize / 2, -mOctreeSize / 2, -mOctreeSize / 2), mOctreeSize);
mMetaBallManager = new MetaBallManager();
mMetaBallManager.dimension.set(mOctreeSize, mOctreeSize, mOctreeSize);
final int mIsoSurfaceResolution = 60;
mMetaBallManager.resolution.set(mIsoSurfaceResolution, mIsoSurfaceResolution, mIsoSurfaceResolution);
mMetaBallManager.position.set(-mOctreeSize / 2, -mOctreeSize / 2, -mOctreeSize / 2);
for (int i = 0; i < 270; i += 16) {
float x = sin(radians(i)) * 50;
float y = cos(radians(i)) * 50;
float r = 2.5f + sin(radians(i));
addInitialParticle(r, x, y, 0);
}
}
public void draw() {
/* move particles */
for (OctreeEntity oe : mOctree.entities()) {
BrownianParticle bp = (BrownianParticle) oe;
bp.move();
}
/* teleport */
float mBoxSize = mOctreeSize / 2;
for (OctreeEntity oe : mOctree.entities()) {
BrownianParticle bp = (BrownianParticle) oe;
if (bp.position().x > mBoxSize) {
bp.position().x = -mBoxSize;
}
if (bp.position().y > mBoxSize) {
bp.position().y = -mBoxSize;
}
if (bp.position().z > mBoxSize) {
bp.position().z = -mBoxSize;
}
if (bp.position().x < -mBoxSize) {
bp.position().x = mBoxSize;
}
if (bp.position().y < -mBoxSize) {
bp.position().y = mBoxSize;
}
if (bp.position().z < -mBoxSize) {
bp.position().z = mBoxSize;
}
}
/* maintain particle number */
final ArrayList<BrownianParticle> mAttachedParticles = new ArrayList<>();
for (OctreeEntity oe : mOctree.entities()) {
BrownianParticle bp = (BrownianParticle) oe;
if (bp.attached()) {
mAttachedParticles.add(bp);
}
}
int mNumberOfUnattachedParticles = mOctree.entities().size() - mAttachedParticles.size();
if (mNumberOfUnattachedParticles < NUMBER_OF_PARTICLES_UNATTACHED && mOctree.entities()
.size() < NUMBER_OF_MAX_PARTICLES) {
addBrownianParticle();
}
/* resolve overlap */
Overlap.resolveOverlap(mAttachedParticles);
/* --- */
background(255);
lights();
pushMatrix();
translate(width / 2.0f, height / 2.0f, 0);
/* rotate */
mRotationZ += 1.0f / frameRate * 0.1f;
rotateX(THIRD_PI);
rotateZ(mRotationZ);
scale(4);
/* metaball */
if (keyPressed) {
mMetaBallManager.clear();
for (BrownianParticle bp : mAttachedParticles) {
mMetaBallManager.add(new MetaBall(bp.position(), 5, bp.radius()));
}
final ArrayList<PVector> myData = mMetaBallManager.createSurface();
/* draw */
fill(255, 127, 0);
noStroke();
beginShape(TRIANGLES);
for (PVector aMyData : myData) {
vertex(aMyData.x, aMyData.y, aMyData.z);
}
endShape();
}
/* draw unattached */
// noFill();
// stroke(0);
// for (OctreeEntity oe : mOctree.entities()) {
// BrownianParticle bp = (BrownianParticle) oe;
// if (!bp.attached()) {
// drawCross(bp.position(), bp.radius());
// }
// }
noStroke();
sphereDetail(4);
for (OctreeEntity oe : mOctree.entities()) {
BrownianParticle bp = (BrownianParticle) oe;
if (!bp.attached()) {
fill(bp.entity_color);
pushMatrix();
translate(bp.position().x, bp.position().y, bp.position().z);
sphere(bp.radius() * 1.1f);
popMatrix();
}
}
/* draw attached */
if (!keyPressed) {
noStroke();
sphereDetail(SPHERE_DETAIL);
for (BrownianParticle bp : mAttachedParticles) {
fill(bp.entity_color);
pushMatrix();
translate(bp.position().x, bp.position().y, bp.position().z);
sphere(bp.radius() * 1.1f);
popMatrix();
}
}
popMatrix();
/* draw info */
fill(0);
noStroke();
text("TOTAL : " + mOctree.entities().size(), 10, 12);
text("ATTACHED : " + mAttachedParticles.size(), 10, 24);
text("FPS : " + frameRate, 10, 36);
}
private void addInitialParticle(float r, float x, float y, float z) {
BrownianParticle p = new BrownianParticle(r);
p.position().set(x, y, z);
p.attach(true);
mOctree.add(p);
}
private void drawCross(PVector v, float pRadius) {
line(v.x - pRadius, v.y, v.z, v.x + pRadius, v.y, v.z);
line(v.x, v.y - pRadius, v.z, v.x, v.y + pRadius, v.z);
line(v.x, v.y, v.z - pRadius, v.x, v.y, v.z + pRadius);
}
private void addBrownianParticle() {
BrownianParticle mEntity = new BrownianParticle(random(0.5f, 2.5f));
mEntity.position().x = random(-mOctreeSize / 2, mOctreeSize / 2);
mEntity.position().y = random(-mOctreeSize / 2, mOctreeSize / 2);
mEntity.position().z = random(-mOctreeSize / 2, mOctreeSize / 2);
mOctree.add(mEntity);
}
private class BrownianParticle extends BasicParticle implements OctreeEntity {
private static final float SPEED = 2;
private static final float SELECT_RADIUS = 20;
int entity_color = color(191);
private boolean mAttached = false;
BrownianParticle(float pRadius) {
radius(pRadius);
}
void move() {
if (!mAttached) {
position().x += random(-SPEED, SPEED);
position().y += random(-SPEED, SPEED);
position().z += random(-SPEED, SPEED);
attach();
}
}
void attach(boolean pAttachState) {
mAttached = pAttachState;
}
boolean attach() {
ArrayList<OctreeEntity> mEntities = mOctree.getEntitesWithinSphere(position(), SELECT_RADIUS);
if (mEntities != null) {
for (OctreeEntity mEntity : mEntities) {
BrownianParticle m = (BrownianParticle) mEntity;
if (m != this & m.attached()) { // only attach to still particles?
float mDistance = PVector.dist(position(), m.position());
if (mDistance < (radius() + m.radius())) {
mAttached = true;
return true;
}
}
}
}
return false;
}
boolean attached() {
return mAttached;
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchDiffusionLimitedAggregationWithIsoSurfaces.class.getName()});
}
}