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

Last change on this file since 2892 was 2827, checked in by ldelgass, 12 years ago

Move Vector3/4Array typdefs to the Vector3/4.h headers and remove TypeDefs?.h.
Also misc. cleanups

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