-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTrapezoidSampler.cpp
181 lines (145 loc) · 5.39 KB
/
TrapezoidSampler.cpp
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
#include "TrapezoidSampler.h"
#include "Logging.h"
TrapezoidSampler* TrapezoidSampler::Instance = 0;
#include <sstream>
int ToBufferIndex(int current, int max, int start, int direction)
{
// Anti-clockwise
if (direction < 0)
{
int result = start - current;
if (result < 0)
result += max;
return result;
}
// Clockwise
int result = start + current;
if (result > max)
result -= max;
return result;
}
void TrapezoidSampler::SetSize(short rows, short cols)
{
LOG_INFO("TrapezoidSampler.SetSize: Rows = " << rows << " Cols = " << cols);
m_rows = rows;
m_cols = cols;
// Create new list of sample areas
int numLeds = rows*2 + cols*2;
m_sampleAreas.clear();
m_sampleAreas.resize(numLeds);
// I want the leds sorted from last led in strand to first led.
// in my case, the start led in this list is 32 (that's bottom middle on the frame at the back of my tv)
int i = 0;
int startLed = 32; /*hardcoded because i'm lazy*/
int direction = -1;
// Top row
for (int x=0; x<rows; x++)
{
int bufferIndex = ToBufferIndex(i++, numLeds, startLed, direction);
m_sampleAreas[bufferIndex] = TrapezoidSampleArea(x, 0);
}
// Right col
for (int y=1; y<cols+1; y++)
{
int bufferIndex = ToBufferIndex(i++, numLeds, startLed, direction);
m_sampleAreas[bufferIndex] = TrapezoidSampleArea(rows-1, y);
}
// Bottom row
for (int x=rows-1; x>=0; x--)
{
int bufferIndex = ToBufferIndex(i++, numLeds, startLed, direction);
m_sampleAreas[bufferIndex] = TrapezoidSampleArea(x, cols+1);
}
// Left col
for (int y=cols-1; y>=0; y--)
{
int bufferIndex = ToBufferIndex(i++, numLeds, startLed, direction);
m_sampleAreas[bufferIndex] = TrapezoidSampleArea(0, y);
}
}
void TrapezoidSampler::UpdatePoints(Vector2 topLeft, Vector2 topRight, Vector2 bottomRight, Vector2 bottomLeft)
{
LOG_INFO("TrapezoidSampler.UpdatePoints");
m_topLeft = topLeft;
m_topRight = topRight;
m_bottomRight = bottomRight;
m_bottomLeft = bottomLeft;
UpdateSamplerAreas();
}
Color* TrapezoidSampler::SampleFromImage(const IplImage* frame)
{
if (frame == 0)
return 0;
if (frame->imageData == 0)
return 0;
if (NumLeds() == 0)
{
LOG_WARN("TrapezoidSampler.SampleFromImage: NumLeds = 0");
return 0;
}
Vector2 frameSize = Vector2((float)frame->width, (float)frame->height);
char *frameBuffer = frame->imageData;
Color *colorBuffer = new Color[NumLeds()];
int colorIndex = 0;
for (std::vector<TrapezoidSampleArea>::iterator i=m_sampleAreas.begin(); i!=m_sampleAreas.end(); ++i)
{
TrapezoidSampleArea area = *i;
Vector2 samplePoint = area.Center() * frameSize;
int pixelOffset = (int)(samplePoint.X()) + ((int)(samplePoint.Y()) * frame->width);
pixelOffset *= 3;
// OpenCV's color format is (Green,Blue,Red)
float r = frameBuffer[pixelOffset+2]/255.0f;
float g = frameBuffer[pixelOffset+1]/255.0f;
float b = frameBuffer[pixelOffset]/255.0f;
colorBuffer[colorIndex] = Color(r, g, b);
colorIndex++;
}
return colorBuffer;
}
void TrapezoidSampler::UpdateSamplerAreas()
{
LOG_INFO("TrapezoidSampler.UpdateSamplerAreas");
// Calculate lengths of sides
float topLen = m_topLeft.Length();
float rightLen = m_topRight.Length();
float bottomLen = m_bottomRight.Length();
float leftLen = m_bottomLeft.Length();
// Calculate perspective distortion on each side based on lengths
float topPersp = min(topLen/bottomLen, 1);
float rightPersp = min(rightLen/leftLen, 1);
float bottomPersp = min(bottomLen/topLen, 1);
float leftPersp = min(leftLen/rightLen, 1);
// Minimum and maximum perspective distortions
float minSidePersp = min(leftPersp, rightPersp);
float maxSidePersp = max(leftPersp, rightPersp);
float minTopPersp = min(topPersp, bottomPersp);
float maxBottomPersp = max(topPersp, bottomPersp);
// GOOD! Now we can do some work!
// Iterate over sample area list and update their regions
for (std::vector<TrapezoidSampleArea>::iterator i=m_sampleAreas.begin(); i!=m_sampleAreas.end(); ++i)
{
TrapezoidSampleArea& area = *i;
// NormPos is the position normalized to values between 0 and 1 in the Trapezoid
Vector2 normPos, distortion;
// TopLeft
normPos = Vector2(area.GridX/(float)(m_rows), area.GridY/(float)(m_cols+2));
distortion = Vector2(lerp(minSidePersp, maxSidePersp, normPos.X()), lerp(minTopPersp, maxBottomPersp, normPos.Y()));
normPos *= distortion;
area.TopLeft = lerp4(m_topLeft, m_topRight, m_bottomRight, m_bottomLeft, normPos.X(), normPos.Y());
// TopRight
normPos = Vector2((area.GridX+1)/(float)(m_rows), area.GridY/(float)(m_cols+2));
distortion = Vector2(lerp(minSidePersp, maxSidePersp, normPos.X()), lerp(minTopPersp, maxBottomPersp, normPos.Y()));
normPos *= distortion;
area.TopRight = lerp4(m_topLeft, m_topRight, m_bottomRight, m_bottomLeft, normPos.X(), normPos.Y());
// BottomRight
normPos = Vector2((area.GridX+1)/(float)(m_rows), (area.GridY+1)/(float)(m_cols+2));
distortion = Vector2(lerp(minSidePersp, maxSidePersp, normPos.X()), lerp(minTopPersp, maxBottomPersp, normPos.Y()));
normPos *= distortion;
area.BottomRight = lerp4(m_topLeft, m_topRight, m_bottomRight, m_bottomLeft, normPos.X(), normPos.Y());
// BottomLeft
normPos = Vector2(area.GridX/(float)(m_rows), (area.GridY+1)/(float)(m_cols+2));
distortion = Vector2(lerp(minSidePersp, maxSidePersp, normPos.X()), lerp(minTopPersp, maxBottomPersp, normPos.Y()));
normPos *= distortion;
area.BottomLeft = lerp4(m_topLeft, m_topRight, m_bottomRight, m_bottomLeft, normPos.X(), normPos.Y());
}
}