source: trunk/packages/vizservers/nanovis/ContourLineFilter.cpp @ 1984

Last change on this file since 1984 was 1984, checked in by gah, 13 years ago

Clean up debugging/printing traces

File size: 8.4 KB
Line 
1
2/*
3 * @note : I refer to 'http://www.codeproject.com/useritems/ScoOterVisualizationPart2.asp'
4 * @note : for this class
5 */
6
7#include <string>
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
15ContourLineFilter::ContourLineFilter()
16    : _colorMap(0), _top(false)
17{
18}
19
20void
21ContourLineFilter::clear()
22{
23       
24    ContourLineFilter::ContourLineList::iterator iter;
25    for (iter = _lines.begin(); iter != _lines.end(); ++iter) {
26        delete (*iter);
27    }
28    _lines.clear();     
29}
30
31R2Geometry*
32ContourLineFilter::create(float min, float max, int linecount,
33                          Vector3* vertices, int width, int height)
34{
35    _lines.clear();
36   
37    float transtion = (max - min) / (linecount + 1);
38    float val;
39    int totalPoints = 0, numPoints;
40    for (int i = 1; i <= linecount; ++i) {
41        val = min + i * transtion;
42       
43        ContourLine* c = new ContourLine(val);
44        numPoints = c->createLine(width, height, vertices, _top);
45        if (numPoints != 0) {
46            totalPoints += numPoints;
47            _lines.push_back(c);
48        } else {
49            delete c;
50        }
51    }
52    Vector3* vertexSet = (Vector3 *)malloc(sizeof(Vector3) * totalPoints);
53    Vector3* colorSet = NULL;
54    if (_colorMap) {
55        colorSet = (Vector3 *)malloc(sizeof(Vector3) * totalPoints);
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);
69        }
70    }
71    R2VertexBuffer* vertexBuffer;
72    vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3, totalPoints,
73        totalPoints * sizeof(Vector3), vertexSet, false);
74    R2VertexBuffer* colorBuffer = NULL;
75    if (_colorMap) {
76        colorBuffer  = new R2VertexBuffer(R2VertexBuffer::COLOR4, totalPoints,
77                totalPoints * sizeof(Vector3), colorSet, false);
78    }
79    R2Geometry* geometry;
80    geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0);
81    clear();
82    return geometry;
83}
84
85R2Geometry*
86ContourLineFilter::create(float min, float max, int linecount,
87                          Vector4* vertices, int width, int height)
88{
89    _lines.clear();
90
91    float transtion = (max - min) / (linecount + 1);
92
93    float val;
94    int totalPoints = 0, numPoints;
95    for (int i = 1; i <= linecount; ++i) {
96        val = min + i * transtion;
97       
98        ContourLine* c = new ContourLine(val);
99        numPoints = c->createLine(width, height, vertices, _top);
100        if (numPoints != 0) {
101            totalPoints += numPoints;
102            _lines.push_back(c);
103        } else {
104            delete c;
105        }
106    }
107    Vector3* vertexSet = (Vector3 *)malloc(sizeof(Vector3) * totalPoints);
108    Vector3* colorSet  = (Vector3 *)malloc(sizeof(Vector3) * totalPoints);
109       
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);
123        }
124    }
125    R2VertexBuffer* vertexBuffer;
126    vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3, totalPoints,
127        totalPoints * sizeof(Vector3), vertexSet, false);
128    R2VertexBuffer* colorBuffer;
129    colorBuffer = new R2VertexBuffer(R2VertexBuffer::COLOR4, totalPoints,
130        totalPoints * sizeof(Vector3), colorSet, false);
131    R2Geometry* geometry;
132    geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0);
133    clear();
134    return geometry;
135}
136
137
138ContourLineFilter::ContourLine::ContourLine(float value)
139    : _value(value)
140{
141}
142
143
144int
145ContourLineFilter::ContourLine::createLine(int width, int height,
146                                           Vector3* vertices, bool top)
147{
148    _points.clear();
149
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))
161                getContourPoint(index1, index2, vertices, width, top);
162            if (isValueWithIn(vertices[index2].y, vertices[index3].y))
163                getContourPoint(index2, index3, vertices, width, top);
164            if (isValueWithIn(vertices[index3].y, vertices[index1].y))
165                getContourPoint(index3, index1, vertices, width, top);
166           
167            if (isValueWithIn(vertices[index1].y, vertices[index3].y))
168                getContourPoint(index1, index3, vertices, width, top);
169            if (isValueWithIn(vertices[index3].y, vertices[index4].y))
170                getContourPoint(index3, index4, vertices, width, top);
171            if (isValueWithIn(vertices[index4].y, vertices[index1].y))
172                getContourPoint(index4, index1, vertices, width, top);
173        }
174    }
175    return _points.size();
176}
177
178
179int
180ContourLineFilter::ContourLine::createLine(int width, int height,
181                                           Vector4* vertices, bool top)
182{
183    _points.clear();
184
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))
196                getContourPoint(index1, index2, vertices, width, top);
197            if (isValueWithIn(vertices[index2].y, vertices[index3].y))
198                getContourPoint(index2, index3, vertices, width, top);
199            if (isValueWithIn(vertices[index3].y, vertices[index1].y))
200                getContourPoint(index3, index1, vertices, width, top);
201           
202            if (isValueWithIn(vertices[index1].y, vertices[index3].y))
203                getContourPoint(index1, index3, vertices, width, top);
204            if (isValueWithIn(vertices[index3].y, vertices[index4].y))
205                getContourPoint(index3, index4, vertices, width, top);
206            if (isValueWithIn(vertices[index4].y, vertices[index1].y))
207                getContourPoint(index4, index1, vertices, width, top);
208        }
209    }
210   
211    return _points.size();
212}
213
214
215void
216ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1,
217        int vertexIndex2, Vector3* vertices, int width, bool top)
218{
219    float diff = vertices[vertexIndex2].y - vertices[vertexIndex1].y;
220    float t = 0.0;
221    if (diff != 0) {
222            t = (_value - vertices[vertexIndex1].y) / diff;
223    }
224
225    Vector3 p;
226    p.x = vertices[vertexIndex1].x + t *
227        (vertices[vertexIndex2].x - vertices[vertexIndex1].x);
228
229    if (top)
230    {
231        p.y = 1.0f;
232    }
233    else
234    {
235    p.y = vertices[vertexIndex1].y + t *
236        (vertices[vertexIndex2].y - vertices[vertexIndex1].y);
237    }
238
239    p.z = vertices[vertexIndex1].z + t *
240        (vertices[vertexIndex2].z - vertices[vertexIndex1].z);
241    _points.push_back(p);
242}
243
244void
245ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1,
246        int vertexIndex2, Vector4* vertices, int width, bool top)
247{
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    }
253
254    Vector3 p;
255    p.x = vertices[vertexIndex1].x +
256        t * (vertices[vertexIndex2].x - vertices[vertexIndex1].x);
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
267    p.z = vertices[vertexIndex1].z +
268        t * (vertices[vertexIndex2].z - vertices[vertexIndex1].z);
269    _points.push_back(p);
270}
271
272
273void
274ContourLineFilter::setColorMap(Vector3Array* colorMap)
275{
276    if (colorMap == _colorMap) {
277        return;
278    }
279    if (colorMap && _colorMap) {
280        if (colorMap->size() != _colorMap->size()) {
281            _colorMap->resize(_colorMap->size());
282        }
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;
292        }
293    }
294}
Note: See TracBrowser for help on using the repository browser.