Skip to content

Commit 74d8d71

Browse files
committed
visit -> behavior api
1 parent fa9957b commit 74d8d71

14 files changed

Lines changed: 116 additions & 83 deletions

File tree

examples/basics/CajasOrientadas/CajasOrientadas.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setup() {
3939
cajas[i] = new Node(caja(random(10, 40), random(10, 40), random(10, 40)));
4040
cajas[i].togglePicking(Node.SHAPE | Node.AXES);
4141
cajas[i].enableHint(Node.AXES | Node.BULLSEYE);
42-
scene.setVisit(cajas[i], (Node node) -> {
42+
scene.addBehavior(cajas[i], (Node node) -> {
4343
Vector to = Vector.subtract(esfera.position(), node.position());
4444
node.setOrientation(Quaternion.from(Vector.plusJ, to));
4545
});

examples/basics/SceneBuffers/SceneBuffers.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Scene Buffers.
3-
* by Jean Pierre Charalambos.
3+
* by Sebastian Chaparro & Jean Pierre Charalambos.
44
*
55
* This example displays the scene front and back buffers.
66
*

examples/demos/FlockOfBoids/Boid.pde

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Boid {
1313

1414
Boid(Vector inPos) {
1515
node = new Node(this::display);
16-
node.setVisit(scene, this::behavior);
16+
startAnimation();
1717
position = new Vector();
1818
position.set(inPos);
1919
node.setPosition(new Vector(position.x(), position.y(), position.z()));
@@ -22,7 +22,15 @@ class Boid {
2222
neighborhoodRadius = 100;
2323
}
2424

25-
public void display(PGraphics pg) {
25+
void startAnimation() {
26+
node.setBehavior(scene, this::behavior);
27+
}
28+
29+
void stopAnimation() {
30+
scene.resetBehavior(node);
31+
}
32+
33+
void display(PGraphics pg) {
2634
pg.pushStyle();
2735
// uncomment to draw boid axes
2836
//Scene.drawAxes(pg, 10);
@@ -152,4 +160,4 @@ class Boid {
152160
if (position.z() < 0)
153161
position.setZ(flockDepth);
154162
}
155-
}
163+
}

examples/demos/FlockOfBoids/FlockOfBoids.pde

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ int flockWidth = 1280;
3636
int flockHeight = 720;
3737
int flockDepth = 600;
3838
boolean avoidWalls = true;
39+
boolean animate = true;
3940

4041
int initBoidNum = 400; // amount of boids to start the program with
4142
ArrayList<Boid> flock;
@@ -150,8 +151,15 @@ void mouseWheel(MouseEvent event) {
150151
void keyPressed() {
151152
switch (key) {
152153
case 'a':
153-
//for (Boid boid : flock)
154-
//boid.task.toggle();
154+
animate = !animate;
155+
for (Boid boid : flock) {
156+
if (animate) {
157+
boid.startAnimation();
158+
}
159+
else {
160+
boid.stopAnimation();
161+
}
162+
}
155163
break;
156164
case 's':
157165
if (scene.eye().reference() == null)

examples/demos/PostEffects/PostEffects.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Post Effects.
3-
* by Ivan Castellanos and Jean Pierre Charalambos.
3+
* by Ivan Castellanos & Jean Pierre Charalambos.
44
*
55
* This example is an adaption of Neil Mendoza great openFrameworks
66
* ofxPostProcessing addon (http://www.neilmendoza.com/ofxpostprocessing/)

examples/demos/ViewFrustumCulling/ViewFrustumCulling.pde

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
* This example illustrates a basic view frustum culling implementation which
66
* is performed by analytically solving the frustum plane equations.
77
*
8-
* A customized traversal rendering algorithm is implemented by overriding the
9-
* node visit() method to clip an octree against the camera's viewing frustum.
10-
* A second viewer displays an external view of the main frustum scene (using
11-
* the main scene eye BOUNDS hint) and the clipped octree.
8+
* A customized traversal rendering algorithm is implemented by adding a custom
9+
* node behavior for the main scene to clip an octree against the camera's viewing
10+
* frustum. A second viewer displays an external view of the main frustum scene
11+
* (using the main scene eye BOUNDS hint) and the clipped octree.
1212
*
1313
* Press the space-bar to change the scene type: PERSPECTIVE or ORTHOGRAPHIC.
1414
*/
@@ -67,7 +67,7 @@ Node node(Node parent, Vector vector) {
6767
pg.box(a, b, c);
6868
});
6969
// register the culling method only at the main scene
70-
mainScene.setVisit(node, this::cull);
70+
mainScene.addBehavior(node, this::cull);
7171
return node;
7272
}
7373

src/nub/core/Graph.java

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class Graph {
129129
protected static HashSet<Node> _interpolators = new HashSet<Node>();
130130

131131
// Custom render
132-
protected HashMap<Integer, BiConsumer<Graph, Node>> _functors;
132+
protected HashMap<Integer, BiConsumer<Graph, Node>> _behaviors;
133133

134134
// offscreen
135135
protected int _upperLeftCornerX, _upperLeftCornerY;
@@ -385,7 +385,7 @@ protected void _init(Object context, int width, int height, Node eye, Type type)
385385
_irays = _i1rays;
386386
// dummy
387387
_orays = _i2rays;
388-
_functors = new HashMap<Integer, BiConsumer<Graph, Node>>();
388+
_behaviors = new HashMap<Integer, BiConsumer<Graph, Node>>();
389389
if (eye == null) {
390390
throw new RuntimeException("Error eye shouldn't be null");
391391
}
@@ -1634,7 +1634,6 @@ public void fit(Node node) {
16341634
* @see #fitFOV()
16351635
* @see #fitFOV(float)
16361636
*/
1637-
//TODO needs testing with flock
16381637
public void fit(Node node, float duration) {
16391638
if (duration <= 0) {
16401639
_eye.set(node);
@@ -2518,11 +2517,11 @@ public void closeContext() {
25182517

25192518
/**
25202519
* Renders the node tree onto the {@link #context()} from the {@link #eye()} viewpoint.
2521-
* Calls {@link #setVisit(Node, BiConsumer)} on each visited node (refer to the {@link Node} documentation).
2520+
* Calls {@link #addBehavior(Node, BiConsumer)} on each visited node (refer to the {@link Node} documentation).
25222521
* Same as {@code render(null)}.
25232522
*
25242523
* @see #render(Node)
2525-
* @see #setVisit(Node, BiConsumer)
2524+
* @see #addBehavior(Node, BiConsumer)
25262525
* @see Node#cull
25272526
* @see Node#bypass()
25282527
* @see Node#setShape(Consumer)
@@ -2541,10 +2540,11 @@ public void render() {
25412540
* within {@link #openContext()} and {@link #closeContext()}.
25422541
*
25432542
* <p>
2544-
* Note that the rendering algorithm calls {@link #setVisit(Node, BiConsumer)} on each visited node
2543+
* Note that the rendering algorithm executes the custom behavior (set with
2544+
* {@link #addBehavior(Node, BiConsumer)}) on each rendered node.
25452545
* (refer to the {@link Node} documentation).
25462546
*
2547-
* @see #setVisit(Node, BiConsumer)
2547+
* @see #addBehavior(Node, BiConsumer)
25482548
* @see Node#cull
25492549
* @see Node#bypass()
25502550
* @see Node#setShape(Consumer)
@@ -2575,7 +2575,8 @@ public void render(Node subtree) {
25752575
}
25762576

25772577
/**
2578-
* Sets a custom node visit for the {@link #render()} algorithm.
2578+
* Adds a custom node behavior to be executed for this scene
2579+
* {@link #render()} algorithm.
25792580
* <p>
25802581
* Bypassing the node rendering and/or performing hierarchical culling, i.e.,
25812582
* culling of the node and its children, should be done here.
@@ -2584,57 +2585,57 @@ public void render(Node subtree) {
25842585
* {@code
25852586
* Graph scene = new Graph(context, width, height);
25862587
* Node space = new Node();
2587-
* public void visit(Graph graph, Node node) {
2588+
* public void behavior(Graph graph, Node node) {
25882589
* if (graph.cullingCondition) {
25892590
* node.cull = true;
25902591
* }
25912592
* else if (bypassCondition) {
25922593
* node.bypass();
25932594
* }
25942595
* }
2595-
* scene.setVisit(space, visit);
2596+
* scene.addBehavior(space, behavior);
25962597
* }
25972598
* </pre>
25982599
* Note that the graph culling condition may be set from
25992600
* {@link #ballVisibility(Vector, float)} or {@link #boxVisibility(Vector, Vector)}.
26002601
*
2601-
* @see #setVisit(Node, Consumer)
2602-
* @see #resetVisit(Node)
2602+
* @see #addBehavior(Node, Consumer)
2603+
* @see #resetBehavior(Node)
26032604
* @see #render(Node)
2604-
* @see Node#setVisit(Graph, BiConsumer)
2605-
* @see Node#setVisit(Graph, Consumer)
2605+
* @see Node#setBehavior(Graph, BiConsumer)
2606+
* @see Node#setBehavior(Graph, Consumer)
26062607
* @see Node#bypass()
26072608
* @see Node#cull
26082609
*/
2609-
public void setVisit(Node node, BiConsumer<Graph, Node> functor) {
2610-
_functors.put(node.id(), functor);
2610+
public void addBehavior(Node node, BiConsumer<Graph, Node> behavior) {
2611+
_behaviors.put(node.id(), behavior);
26112612
}
26122613

26132614
/**
2614-
* Same as {@code setVisit(node, (g, n) -> functor.accept(n))}.
2615+
* Same as {@code setBehavior(node, (g, n) -> behavior.accept(n))}.
26152616
*
2616-
* @see #setVisit(Node, BiConsumer)
2617-
* @see #resetVisit(Node)
2617+
* @see #addBehavior(Node, BiConsumer)
2618+
* @see #resetBehavior(Node)
26182619
* @see #render(Node)
2619-
* @see Node#setVisit(Graph, BiConsumer)
2620-
* @see Node#setVisit(Graph, Consumer)
2620+
* @see Node#setBehavior(Graph, BiConsumer)
2621+
* @see Node#setBehavior(Graph, Consumer)
26212622
* @see Node#bypass()
26222623
* @see Node#cull
26232624
*/
2624-
public void setVisit(Node node, Consumer<Node> functor) {
2625-
setVisit(node, (g, n) -> functor.accept(n));
2625+
public void addBehavior(Node node, Consumer<Node> behavior) {
2626+
addBehavior(node, (g, n) -> behavior.accept(n));
26262627
}
26272628

26282629
/**
2629-
* Resets the node custom visit set with {@link #setVisit(Node, BiConsumer)}.
2630+
* Resets the node custom behavior which is set with {@link #addBehavior(Node, BiConsumer)}.
26302631
*
2631-
* @see #setVisit(Node, BiConsumer)
2632+
* @see #addBehavior(Node, BiConsumer)
26322633
* @see #render(Node)
26332634
* @see Node#bypass()
26342635
* @see Node#cull
26352636
*/
2636-
public void resetVisit(Node node) {
2637-
_functors.remove(node.id());
2637+
public void resetBehavior(Node node) {
2638+
_behaviors.remove(node.id());
26382639
}
26392640

26402641
/**
@@ -2644,10 +2645,16 @@ protected void _render(Node node) {
26442645
_matrixHandler.pushMatrix();
26452646
node._execute(this);
26462647
_matrixHandler.applyTransformation(node);
2647-
// TODO should go before pushMatrix ???
2648-
BiConsumer<Graph, Node> functor = _functors.get(node.id());
2649-
if (functor != null) {
2650-
functor.accept(this, node);
2648+
// TODO ordering of operations is a bit experimental.
2649+
// For instance should the visits go before pushMatrix?
2650+
// I believe it belongs here, i.e., current node culling
2651+
// condition may require local geometry operations.
2652+
// On the oder hand, timing stuff (node_execute) may only
2653+
// be executed once it's known for sure the node is not
2654+
// culled :-/
2655+
BiConsumer<Graph, Node> behavior = _behaviors.get(node.id());
2656+
if (behavior != null) {
2657+
behavior.accept(this, node);
26512658
}
26522659
if (!node.cull) {
26532660
if (node._bypass != _frameCount) {

src/nub/core/Interpolator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected Vector _translation() {
104104
return _node.reference() == _keyFrame.reference() ? _keyFrame.position() :
105105
_node.reference() == null ? _keyFrame.worldPosition() : _node.reference().location(_keyFrame.worldPosition());
106106
// perhaps less efficient but simpler equivalent form:
107-
// return _node.reference() == null ? _keyFrame.position() : _node.reference().location(_keyFrame.position());
107+
// return _node.reference() == null ? _keyFrame.worldPosition() : _node.reference().location(_keyFrame.worldPosition());
108108
}
109109

110110
/**
@@ -115,7 +115,7 @@ protected Quaternion _rotation() {
115115
return _node.reference() == _keyFrame.reference() ? _keyFrame.orientation() :
116116
_node.reference() == null ? _keyFrame.worldOrientation() : _node.reference().displacement(_keyFrame.worldOrientation());
117117
// perhaps less efficient but simpler equivalent form:
118-
// return _node.reference() == null ? _keyFrame.orientation() : _node.reference().displacement(_keyFrame.orientation());
118+
// return _node.reference() == null ? _keyFrame.worldOrientation() : _node.reference().displacement(_keyFrame.worldOrientation());
119119
}
120120

121121
/**
@@ -126,7 +126,7 @@ protected float _scaling() {
126126
return _node.reference() == _keyFrame.reference() ? _keyFrame.magnitude() :
127127
_node.reference() == null ? _keyFrame.worldMagnitude() : _node.reference().displacement(_keyFrame.worldMagnitude());
128128
// perhaps less efficient but simpler equivalent form:
129-
// return _node.reference() == null ? _keyFrame.magnitude() : _node.reference().displacement(_keyFrame.magnitude());
129+
// return _node.reference() == null ? _keyFrame.worldMagnitude() : _node.reference().displacement(_keyFrame.worldMagnitude());
130130
}
131131
}
132132

0 commit comments

Comments
 (0)