-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow.pde
More file actions
55 lines (49 loc) · 2.16 KB
/
Flow.pde
File metadata and controls
55 lines (49 loc) · 2.16 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
int gridWidth = camWidth/gridTileSize;
int gridHeight = camHeight/gridTileSize;
PVector gridSize = new PVector(gridWidth,gridHeight);
PImage flowmap;
PVector averageFlow;
float flowLinger = .5;
void initializeFlowMap()
{
flowmap = new PImage(gridWidth,gridHeight);
flowmap.loadPixels();
for (int i=0; i<flowmap.pixels.length; i++)
{
flowmap.pixels[i] = vectorToColor(HalfColor);
}
flowmap.updatePixels();
averageFlow = vector(0);
}
void updateFlowMap(float fillPercent)
{
averageFlow = cv.flow.getAverageFlowInRegion(0,0, camWidth,camHeight);
int randMax = gridTileSize * int((1/fillPercent)-1);
flowmap.loadPixels();
for (int y=gridTileSize; y<camHeight-gridTileSize; y+=gridTileSize)
{
for (int x=gridTileSize; x<camWidth-gridTileSize; x+=gridTileSize)
{
PVector flow = cv.flow.getAverageFlowInRegion(x,y, gridTileSize,gridTileSize);
flow = PVector.mult( flow, 16 ); // Get the average flow in this grid square
PVector cameraCoord = new PVector(x,y);
int gridIndex = cameraCoordToGridIndex(cameraCoord); // Translate camera coordinates to grid index
PVector currentFlow = colorToVector(flowmap.pixels[gridIndex]); // Sample flowmap
// float linger = flowLinger / max(flow.mag(), 1);
flowAttenuation(currentFlow, flow, flowLinger ); // Calculate resultant flow
flowmap.pixels[gridIndex] = vectorToColor(currentFlow); // Update flowmap value
x += int(random(0,randMax)); // Add random to grid increment to reduce updates
}
}
flowmap.updatePixels();
}
void flowAttenuation(PVector currentFlow, PVector momentFlow, float lingerFactor)
{
subInPlace(currentFlow, HalfColor); // Normalize currentFlow from the range 0-255 to the range -127-127
multInPlace( currentFlow, lingerFactor ); // Persist the flowmap by linger factor
addInPlace( currentFlow, PVector.mult(momentFlow,1-lingerFactor) ); // Factor in the momentary flow
// result = PVector.add( PVector.mult(result,lingerFactor), PVector.mult(momentFlow,1-lingerFactor) );
// currentFlow = PVector.add( PVector.mult(currentFlow,(1-flowLinger)*mag), PVector.mult(flow,flowLinger*(1-mag)) );
addInPlace(currentFlow, HalfColor);
constrainInPlace(currentFlow, Zero,FullColor);
}