1 | |
---|
2 | /* |
---|
3 | * @note : I refer to 'http://www.codeproject.com/useritems/ScoOterVisualizationPart2.asp' |
---|
4 | * @note : for this class |
---|
5 | */ |
---|
6 | |
---|
7 | #include "ContourLineFilter.h" |
---|
8 | #include <R2/graphics/R2VertexBuffer.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <memory.h> |
---|
11 | #include "Vector4.h" |
---|
12 | #include "Vector3.h" |
---|
13 | |
---|
14 | ContourLineFilter::ContourLineFilter() |
---|
15 | : _colorMap(0) |
---|
16 | { |
---|
17 | } |
---|
18 | |
---|
19 | void ContourLineFilter::clear() |
---|
20 | { |
---|
21 | |
---|
22 | ContourLineFilter::ContourLineList::iterator iter; |
---|
23 | for (iter = _lines.begin(); iter != _lines.end(); ++iter) |
---|
24 | { |
---|
25 | delete (*iter); |
---|
26 | } |
---|
27 | |
---|
28 | _lines.clear(); |
---|
29 | } |
---|
30 | |
---|
31 | R2Geometry* ContourLineFilter::create(float min, float max, int linecount, Vector3* vertices, int width, int height) |
---|
32 | { |
---|
33 | _lines.clear(); |
---|
34 | |
---|
35 | float transtion = (max - min) / (linecount + 1); |
---|
36 | |
---|
37 | float val; |
---|
38 | int totalNumOfPoints = 0, numOfPoints; |
---|
39 | for (int i = 1; i <= linecount; ++i) |
---|
40 | { |
---|
41 | val = min + i * transtion; |
---|
42 | |
---|
43 | ContourLine* c = new ContourLine(val); |
---|
44 | if(numOfPoints = c->createLine(width, height, vertices)) |
---|
45 | { |
---|
46 | totalNumOfPoints += numOfPoints; |
---|
47 | _lines.push_back(c); |
---|
48 | } |
---|
49 | else |
---|
50 | delete c; |
---|
51 | } |
---|
52 | |
---|
53 | Vector3* vertexSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints); |
---|
54 | Vector3* colorSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints); |
---|
55 | |
---|
56 | ContourLineFilter::ContourLineList::iterator iter; |
---|
57 | int index = 0, colorIndex = 0; |
---|
58 | for (iter = _lines.begin(); iter != _lines.end(); ++iter, ++colorIndex) |
---|
59 | { |
---|
60 | std::list<Vector3>& lines = (*iter)->_points; |
---|
61 | std::list<Vector3>::iterator iter2; |
---|
62 | for (iter2 = lines.begin(); iter2 != lines.end(); ++iter2, ++index) |
---|
63 | { |
---|
64 | if (_colorMap && (colorIndex < _colorMap->size())) |
---|
65 | { |
---|
66 | colorSet[index] = _colorMap->at(colorIndex); |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | colorSet[index].set((*iter)->_value, (*iter)->_value, (*iter)->_value); |
---|
71 | } |
---|
72 | vertexSet[index] = (*iter2); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | R2VertexBuffer* vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3, totalNumOfPoints, |
---|
77 | totalNumOfPoints * sizeof(Vector3), vertexSet, false); |
---|
78 | R2VertexBuffer* colorBuffer = new R2VertexBuffer(R2VertexBuffer::COLOR4, totalNumOfPoints, |
---|
79 | totalNumOfPoints * sizeof(Vector3), colorSet, false); |
---|
80 | R2Geometry* geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0); |
---|
81 | |
---|
82 | clear(); |
---|
83 | |
---|
84 | return geometry; |
---|
85 | } |
---|
86 | |
---|
87 | R2Geometry* ContourLineFilter::create(float min, float max, int linecount, Vector4* vertices, int width, int height) |
---|
88 | { |
---|
89 | _lines.clear(); |
---|
90 | |
---|
91 | float transtion = (max - min) / (linecount + 1); |
---|
92 | |
---|
93 | float val; |
---|
94 | int totalNumOfPoints = 0, numOfPoints; |
---|
95 | for (int i = 1; i <= linecount; ++i) |
---|
96 | { |
---|
97 | val = min + i * transtion; |
---|
98 | |
---|
99 | ContourLine* c = new ContourLine(val); |
---|
100 | if(numOfPoints = c->createLine(width, height, vertices)) |
---|
101 | { |
---|
102 | totalNumOfPoints += numOfPoints; |
---|
103 | _lines.push_back(c); |
---|
104 | } |
---|
105 | else |
---|
106 | delete c; |
---|
107 | } |
---|
108 | |
---|
109 | Vector3* vertexSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints); |
---|
110 | Vector3* colorSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints); |
---|
111 | |
---|
112 | ContourLineFilter::ContourLineList::iterator iter; |
---|
113 | int index = 0, colorIndex = 0; |
---|
114 | for (iter = _lines.begin(); iter != _lines.end(); ++iter, ++colorIndex) |
---|
115 | { |
---|
116 | std::list<Vector3>& lines = (*iter)->_points; |
---|
117 | std::list<Vector3>::iterator iter2; |
---|
118 | for (iter2 = lines.begin(); iter2 != lines.end(); ++iter2, ++index) |
---|
119 | { |
---|
120 | if (_colorMap && (colorIndex < _colorMap->size())) |
---|
121 | { |
---|
122 | colorSet[index] = _colorMap->at(colorIndex); |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | colorSet[index].set((*iter)->_value, (*iter)->_value, (*iter)->_value); |
---|
127 | } |
---|
128 | vertexSet[index] = (*iter2); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | R2VertexBuffer* vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3, totalNumOfPoints, |
---|
133 | totalNumOfPoints * sizeof(Vector3), vertexSet, false); |
---|
134 | R2VertexBuffer* colorBuffer = new R2VertexBuffer(R2VertexBuffer::COLOR4, totalNumOfPoints, |
---|
135 | totalNumOfPoints * sizeof(Vector3), colorSet, false); |
---|
136 | R2Geometry* geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0); |
---|
137 | |
---|
138 | clear(); |
---|
139 | |
---|
140 | return geometry; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | ContourLineFilter::ContourLine::ContourLine(float value) |
---|
145 | : _value(value) |
---|
146 | { |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | int ContourLineFilter::ContourLine::createLine(int width, int height, Vector3* vertices) |
---|
151 | { |
---|
152 | _points.clear(); |
---|
153 | |
---|
154 | int hl = height - 1; |
---|
155 | int wl = width - 1; |
---|
156 | int index1, index2, index3, index4; |
---|
157 | for (int i = 0; i < hl; ++i) |
---|
158 | { |
---|
159 | for (int j = 0; j < wl; ++j) |
---|
160 | { |
---|
161 | index1 = j + i * width; |
---|
162 | index2 = j + 1 + i * width; |
---|
163 | index3 = j + 1 + (i + 1) * width; |
---|
164 | index4 = j + (i + 1) * width; |
---|
165 | |
---|
166 | if (isValueWithIn(vertices[index1].y, vertices[index2].y)) getContourPoint(index1, index2, vertices, width); |
---|
167 | if (isValueWithIn(vertices[index2].y, vertices[index3].y)) getContourPoint(index2, index3, vertices, width); |
---|
168 | if (isValueWithIn(vertices[index3].y, vertices[index1].y)) getContourPoint(index3, index1, vertices, width); |
---|
169 | |
---|
170 | if (isValueWithIn(vertices[index1].y, vertices[index3].y)) getContourPoint(index1, index3, vertices, width); |
---|
171 | if (isValueWithIn(vertices[index3].y, vertices[index4].y)) getContourPoint(index3, index4, vertices, width); |
---|
172 | if (isValueWithIn(vertices[index4].y, vertices[index1].y)) getContourPoint(index4, index1, vertices, width); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | return _points.size(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | int ContourLineFilter::ContourLine::createLine(int width, int height, Vector4* vertices) |
---|
181 | { |
---|
182 | _points.clear(); |
---|
183 | |
---|
184 | int hl = height - 1; |
---|
185 | int wl = width - 1; |
---|
186 | int index1, index2, index3, index4; |
---|
187 | for (int i = 0; i < hl; ++i) |
---|
188 | { |
---|
189 | for (int j = 0; j < wl; ++j) |
---|
190 | { |
---|
191 | index1 = j + i * width; |
---|
192 | index2 = j + 1 + i * width; |
---|
193 | index3 = j + 1 + (i + 1) * width; |
---|
194 | index4 = j + (i + 1) * width; |
---|
195 | |
---|
196 | if (isValueWithIn(vertices[index1].y, vertices[index2].y)) getContourPoint(index1, index2, vertices, width); |
---|
197 | if (isValueWithIn(vertices[index2].y, vertices[index3].y)) getContourPoint(index2, index3, vertices, width); |
---|
198 | if (isValueWithIn(vertices[index3].y, vertices[index1].y)) getContourPoint(index3, index1, vertices, width); |
---|
199 | |
---|
200 | if (isValueWithIn(vertices[index1].y, vertices[index3].y)) getContourPoint(index1, index3, vertices, width); |
---|
201 | if (isValueWithIn(vertices[index3].y, vertices[index4].y)) getContourPoint(index3, index4, vertices, width); |
---|
202 | if (isValueWithIn(vertices[index4].y, vertices[index1].y)) getContourPoint(index4, index1, vertices, width); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | return _points.size(); |
---|
207 | } |
---|
208 | |
---|
209 | bool ContourLineFilter::ContourLine::isValueWithIn(float val1, float Val2) |
---|
210 | { |
---|
211 | return (_value >= val1 && _value <= Val2) || (_value >= Val2 && _value <= val1); |
---|
212 | } |
---|
213 | |
---|
214 | void ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1, int vertexIndex2, Vector3* vertices, int width) |
---|
215 | { |
---|
216 | float diff = vertices[vertexIndex2].y - vertices[vertexIndex1].y; |
---|
217 | float t = 0.0; |
---|
218 | if (diff != 0) |
---|
219 | { |
---|
220 | t = (_value - vertices[vertexIndex1].y) / diff; |
---|
221 | } |
---|
222 | |
---|
223 | Vector3 p; |
---|
224 | p.x = vertices[vertexIndex1].x + t * (vertices[vertexIndex2].x - vertices[vertexIndex1].x); |
---|
225 | p.y = vertices[vertexIndex1].y + t * (vertices[vertexIndex2].y - vertices[vertexIndex1].y); |
---|
226 | p.z = vertices[vertexIndex1].z + t * (vertices[vertexIndex2].z - vertices[vertexIndex1].z); |
---|
227 | |
---|
228 | _points.push_back(p); |
---|
229 | } |
---|
230 | |
---|
231 | void ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1, int vertexIndex2, Vector4* vertices, int width) |
---|
232 | { |
---|
233 | float diff = vertices[vertexIndex2].y - vertices[vertexIndex1].y; |
---|
234 | float t = 0.0; |
---|
235 | if (diff != 0) |
---|
236 | { |
---|
237 | t = (_value - vertices[vertexIndex1].y) / diff; |
---|
238 | } |
---|
239 | |
---|
240 | Vector3 p; |
---|
241 | p.x = vertices[vertexIndex1].x + t * (vertices[vertexIndex2].x - vertices[vertexIndex1].x); |
---|
242 | p.y = vertices[vertexIndex1].y + t * (vertices[vertexIndex2].y - vertices[vertexIndex1].y); |
---|
243 | p.z = vertices[vertexIndex1].z + t * (vertices[vertexIndex2].z - vertices[vertexIndex1].z); |
---|
244 | |
---|
245 | _points.push_back(p); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | void ContourLineFilter::setColorMap(Vector3Array* colorMap) |
---|
250 | { |
---|
251 | if (colorMap == _colorMap) return; |
---|
252 | |
---|
253 | if (colorMap && _colorMap) |
---|
254 | { |
---|
255 | if (colorMap->size() != _colorMap->size()) |
---|
256 | { |
---|
257 | _colorMap->resize(_colorMap->size()); |
---|
258 | } |
---|
259 | _colorMap->assign(colorMap->begin(), colorMap->end()); |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | delete _colorMap; |
---|
264 | |
---|
265 | if (colorMap && colorMap->size()) |
---|
266 | { |
---|
267 | _colorMap = new Vector3Array(colorMap->size()); |
---|
268 | _colorMap->assign(colorMap->begin(), colorMap->end()); |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | _colorMap = 0; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|