-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchOctree3D.java
executable file
·191 lines (163 loc) · 5.93 KB
/
SketchOctree3D.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
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.octree.Octree;
import de.hfkbremen.algorithmiccliches.octree.OctreeEntity;
import processing.core.PApplet;
import processing.core.PVector;
import java.util.ArrayList;
public class SketchOctree3D extends PApplet {
private static final int NUMBER_OF_PARTICLES_ADDED = 10000;
private static final float OCTREE_SIZE = 100;
private final PVector mPosition = new PVector();
private MVisibleOctree mOctree;
private float mSelectRadius = 20;
private boolean showOctree = true;
private boolean useSphere = true;
private float mRotationZ = 0.1f;
private int numParticles = 1;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
textFont(createFont("Courier", 11));
mOctree = new MVisibleOctree(new PVector(-OCTREE_SIZE / 2, -OCTREE_SIZE / 2, -OCTREE_SIZE / 2), OCTREE_SIZE);
mOctree.add(new MOctreeEntity());
strokeWeight(0.25f);
}
public void draw() {
background(255);
pushMatrix();
translate(width / 2.0f, height / 2.0f, 0);
/* rotate */
if (mousePressed) {
mRotationZ += (mouseX * 0.01f - mRotationZ) * 0.05f;
} else {
mPosition.x = -(width * 0.5f - mouseX) / (width / 2.0f) * OCTREE_SIZE / 2;
mPosition.y = -(height * 0.5f - mouseY) / (height / 2.0f) * OCTREE_SIZE / 2;
}
rotateX(THIRD_PI);
rotateZ(mRotationZ);
scale(4);
/* get entities from octree */
ArrayList<OctreeEntity> mEntities;
if (useSphere) {
mEntities = mOctree.getEntitesWithinSphere(mPosition, mSelectRadius);
} else {
mEntities = mOctree.getEntitiesWithinBox(mPosition, new PVector(mSelectRadius / 2,
mSelectRadius / 2,
mSelectRadius / 2));
}
/* draw entities */
int mNumberOfPointsSelected = 0;
stroke(0, 127, 255, 127);
noFill();
if (mEntities != null) {
mNumberOfPointsSelected = mEntities.size();
for (OctreeEntity mEntity : mEntities) {
MOctreeEntity m = (MOctreeEntity) mEntity;
stroke(m.entity_color);
drawCross(mEntity.position(), 1.0f);
}
}
/* draw octree */
if (showOctree) {
stroke(0, 4);
noFill();
mOctree.draw();
}
/* draw crosshair */
stroke(255, 0, 0, 63);
noFill();
beginShape(LINES);
vertex(mPosition.x, -OCTREE_SIZE / 2, 0);
vertex(mPosition.x, OCTREE_SIZE / 2, 0);
vertex(-OCTREE_SIZE / 2, mPosition.y, 0);
vertex(OCTREE_SIZE / 2, mPosition.y, 0);
endShape();
/* draw selection sphere */
stroke(255, 0, 0, 63);
noFill();
translate(mPosition.x, mPosition.y, 0);
sphereDetail(8);
sphere(mSelectRadius);
popMatrix();
/* draw info */
fill(0);
noStroke();
text("POINTS : " + numParticles, 10, 12);
text("SELECTED : " + mNumberOfPointsSelected, 10, 24);
text("FPS : " + frameRate, 10, 36);
}
public void keyPressed() {
switch (key) {
case ' ':
for (int i = 0; i < NUMBER_OF_PARTICLES_ADDED; i++) {
MOctreeEntity mEntity = new MOctreeEntity();
mEntity.position().x = random(-OCTREE_SIZE / 2, OCTREE_SIZE / 2);
mEntity.position().y = random(-OCTREE_SIZE / 2, OCTREE_SIZE / 2);
mEntity.position().z = random(-OCTREE_SIZE / 2, OCTREE_SIZE / 2);
mOctree.add(mEntity);
}
numParticles += NUMBER_OF_PARTICLES_ADDED;
break;
case 's':
useSphere = !useSphere;
break;
case 'o':
showOctree = !showOctree;
break;
case '-':
mSelectRadius = max(mSelectRadius - 1, 2);
break;
case '+':
mSelectRadius = min(mSelectRadius + 1, OCTREE_SIZE);
break;
case 'c':
mOctree.auto_reduction(true);
mOctree.removeAll();
mOctree.auto_reduction(false);
numParticles = 0;
break;
default:
break;
}
}
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);
}
class MOctreeEntity
implements OctreeEntity {
PVector position = new PVector();
int entity_color = color(0, 127, random(0, 255), 127);
public PVector position() {
return position;
}
}
class MVisibleOctree
extends Octree {
MVisibleOctree(PVector o, float d) {
super(o, d);
}
void draw() {
drawNode(this);
}
void drawNode(Octree pOctree) {
if (pOctree.getNumChildren() > 0) {
pushMatrix();
translate(pOctree.origin().x, pOctree.origin().y, pOctree.origin().z);
box(pOctree.getNodeSize());
popMatrix();
Octree[] childNodes = pOctree.getChildren();
for (int i = 0; i < Octree.NUMBER_OF_CHILDREN; i++) {
if (childNodes[i] != null) {
drawNode(childNodes[i]);
}
}
}
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchOctree3D.class.getName()});
}
}