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