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

Last change on this file since 1258 was 1053, checked in by gah, 16 years ago

fixes to make compile under gcc-4.3

File size: 8.8 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   
39    float val;
40    int totalNumOfPoints = 0, numOfPoints;
41    for (int i = 1; i <= linecount; ++i) {
42        val = min + i * transtion;
43       
44        ContourLine* c = new ContourLine(val);
45        numOfPoints = c->createLine(width, height, vertices, _top);
46        if (numOfPoints != 0) {
47            totalNumOfPoints += numOfPoints;
48            _lines.push_back(c);
49        } else {
50            delete c;
51        }
52    }
53   
54    Vector3* vertexSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints);
55    Vector3* colorSet = 0;
56    if (_colorMap) {
57        colorSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints);
58    }
59   
60    ContourLineFilter::ContourLineList::iterator iter;
61    unsigned int index = 0, colorIndex = 0;
62    for (iter = _lines.begin(); iter != _lines.end(); ++iter, ++colorIndex) {
63        std::list<Vector3>& lines = (*iter)->_points;
64        std::list<Vector3>::iterator iter2;
65        for (iter2 = lines.begin(); iter2 != lines.end(); ++iter2, ++index) {
66            if (_colorMap && (colorIndex < _colorMap->size())) {
67                colorSet[index] = _colorMap->at(colorIndex);
68            } else {
69                //colorSet[index].set((*iter)->_value, (*iter)->_value, (*iter)->_value);
70            }
71            vertexSet[index] = (*iter2);
72        }
73    }
74   
75    R2VertexBuffer* vertexBuffer;
76    vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3,
77                                      totalNumOfPoints,
78                                      totalNumOfPoints * sizeof(Vector3),
79                                      vertexSet, false);
80    R2VertexBuffer* colorBuffer = 0;
81    R2Geometry* geometry = 0;
82    if (_colorMap) {
83        colorBuffer  = new R2VertexBuffer(R2VertexBuffer::COLOR4,
84                                          totalNumOfPoints,
85                                          totalNumOfPoints * sizeof(Vector3),
86                                          colorSet, false);
87    }
88
89    geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0);
90    clear();
91    return geometry;
92}
93
94R2Geometry*
95ContourLineFilter::create(float min, float max, int linecount,
96                          Vector4* vertices, int width, int height)
97{
98    _lines.clear();
99
100    float transtion = (max - min) / (linecount + 1);
101
102    float val;
103    int totalNumOfPoints = 0, numOfPoints;
104    for (int i = 1; i <= linecount; ++i) {
105        val = min + i * transtion;
106       
107        ContourLine* c = new ContourLine(val);
108        numOfPoints = c->createLine(width, height, vertices, _top);
109        if (numOfPoints != 0) {
110            totalNumOfPoints += numOfPoints;
111            _lines.push_back(c);
112        } else {
113            delete c;
114        }
115    }
116    Vector3* vertexSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints);
117    Vector3* colorSet = (Vector3*) malloc(sizeof(Vector3) * totalNumOfPoints);
118       
119    ContourLineFilter::ContourLineList::iterator iter;
120    unsigned int index = 0, colorIndex = 0;
121    for (iter = _lines.begin(); iter != _lines.end(); ++iter, ++colorIndex) {
122        std::list<Vector3>& lines = (*iter)->_points;
123        std::list<Vector3>::iterator iter2;
124        for (iter2 = lines.begin(); iter2 != lines.end(); ++iter2, ++index) {
125            if (_colorMap && (colorIndex < _colorMap->size())) {
126                colorSet[index] = _colorMap->at(colorIndex);
127            } else {
128                colorSet[index].set((*iter)->_value, (*iter)->_value,
129                                    (*iter)->_value);
130            }
131            vertexSet[index] = (*iter2);
132        }
133    }
134    R2VertexBuffer* vertexBuffer;
135    vertexBuffer = new R2VertexBuffer(R2VertexBuffer::POSITION3,
136                                      totalNumOfPoints,
137                                      totalNumOfPoints * sizeof(Vector3),
138                                      vertexSet, false);
139    R2VertexBuffer* colorBuffer;
140    colorBuffer = new R2VertexBuffer(R2VertexBuffer::COLOR4, totalNumOfPoints,
141                                     totalNumOfPoints * sizeof(Vector3),
142                                     colorSet, false);
143    R2Geometry* geometry;
144    geometry = new R2Geometry(R2Geometry::LINES, vertexBuffer, colorBuffer, 0);
145    clear();
146    return geometry;
147}
148
149
150ContourLineFilter::ContourLine::ContourLine(float value)
151    : _value(value)
152{
153}
154
155
156int
157ContourLineFilter::ContourLine::createLine(int width, int height,
158                                           Vector3* vertices, bool top)
159{
160    _points.clear();
161
162    int hl = height - 1;
163    int wl = width - 1;
164    int index1, index2, index3, index4;
165    for (int i = 0; i < hl; ++i) {
166        for (int j = 0; j < wl; ++j) {
167            index1 = j + i * width;
168            index2 = j + 1 + i * width;
169            index3 = j + 1 + (i + 1) * width;
170            index4 = j + (i + 1) * width;
171           
172            if (isValueWithIn(vertices[index1].y, vertices[index2].y))
173                getContourPoint(index1, index2, vertices, width, top);
174            if (isValueWithIn(vertices[index2].y, vertices[index3].y))
175                getContourPoint(index2, index3, vertices, width, top);
176            if (isValueWithIn(vertices[index3].y, vertices[index1].y))
177                getContourPoint(index3, index1, vertices, width, top);
178           
179            if (isValueWithIn(vertices[index1].y, vertices[index3].y))
180                getContourPoint(index1, index3, vertices, width, top);
181            if (isValueWithIn(vertices[index3].y, vertices[index4].y))
182                getContourPoint(index3, index4, vertices, width, top);
183            if (isValueWithIn(vertices[index4].y, vertices[index1].y))
184                getContourPoint(index4, index1, vertices, width, top);
185        }
186    }
187    return _points.size();
188}
189
190
191int
192ContourLineFilter::ContourLine::createLine(int width, int height,
193                                           Vector4* vertices, bool top)
194{
195    _points.clear();
196
197    int hl = height - 1;
198    int wl = width - 1;
199    int index1, index2, index3, index4;
200    for (int i = 0; i < hl; ++i) {
201        for (int j = 0; j < wl; ++j) {
202            index1 = j + i * width;
203            index2 = j + 1 + i * width;
204            index3 = j + 1 + (i + 1) * width;
205            index4 = j + (i + 1) * width;
206           
207            if (isValueWithIn(vertices[index1].y, vertices[index2].y))
208                getContourPoint(index1, index2, vertices, width, top);
209            if (isValueWithIn(vertices[index2].y, vertices[index3].y))
210                getContourPoint(index2, index3, vertices, width, top);
211            if (isValueWithIn(vertices[index3].y, vertices[index1].y))
212                getContourPoint(index3, index1, vertices, width, top);
213           
214            if (isValueWithIn(vertices[index1].y, vertices[index3].y))
215                getContourPoint(index1, index3, vertices, width, top);
216            if (isValueWithIn(vertices[index3].y, vertices[index4].y))
217                getContourPoint(index3, index4, vertices, width, top);
218            if (isValueWithIn(vertices[index4].y, vertices[index1].y))
219                getContourPoint(index4, index1, vertices, width, top);
220        }
221    }
222   
223    return _points.size();
224}
225
226bool
227ContourLineFilter::ContourLine::isValueWithIn(float val1, float Val2)
228{
229    return ((_value >= val1 && _value <= Val2) ||
230            (_value >= Val2 && _value <= val1));
231}
232
233void
234ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1,
235        int vertexIndex2, Vector3* vertices, int width, bool top)
236{
237    float diff = vertices[vertexIndex2].y - vertices[vertexIndex1].y;
238    float t = 0.0;
239    if (diff != 0) {
240            t = (_value - vertices[vertexIndex1].y) / diff;
241    }
242
243    Vector3 p;
244    p.x = vertices[vertexIndex1].x + t *
245        (vertices[vertexIndex2].x - vertices[vertexIndex1].x);
246
247    if (top)
248    {
249        p.y = 1.0f;
250    }
251    else
252    {
253    p.y = vertices[vertexIndex1].y + t *
254        (vertices[vertexIndex2].y - vertices[vertexIndex1].y);
255    }
256
257    p.z = vertices[vertexIndex1].z + t *
258        (vertices[vertexIndex2].z - vertices[vertexIndex1].z);
259    _points.push_back(p);
260}
261
262void
263ContourLineFilter::ContourLine::getContourPoint(int vertexIndex1,
264        int vertexIndex2, Vector4* vertices, int width, bool top)
265{
266    float diff = vertices[vertexIndex2].y - vertices[vertexIndex1].y;
267    float t = 0.0;
268    if (diff != 0) {
269        t = (_value - vertices[vertexIndex1].y) / diff;
270    }
271
272    Vector3 p;
273    p.x = vertices[vertexIndex1].x +
274        t * (vertices[vertexIndex2].x - vertices[vertexIndex1].x);
275    if (top)
276    {
277        p.y = 1.0f;
278    }
279    else
280    {
281        p.y = vertices[vertexIndex1].y +
282            t * (vertices[vertexIndex2].y - vertices[vertexIndex1].y);
283    }
284
285    p.z = vertices[vertexIndex1].z +
286        t * (vertices[vertexIndex2].z - vertices[vertexIndex1].z);
287    _points.push_back(p);
288}
289
290
291void
292ContourLineFilter::setColorMap(Vector3Array* colorMap)
293{
294    if (colorMap == _colorMap) {
295        return;
296    }
297    if (colorMap && _colorMap) {
298        if (colorMap->size() != _colorMap->size()) {
299            _colorMap->resize(_colorMap->size());
300        }
301        _colorMap->assign(colorMap->begin(), colorMap->end());
302    } else {
303        delete _colorMap;
304       
305        if (colorMap && colorMap->size()) {     
306            _colorMap = new Vector3Array(colorMap->size());
307            _colorMap->assign(colorMap->begin(), colorMap->end());
308        } else {
309            _colorMap = 0;
310        }
311    }
312}
Note: See TracBrowser for help on using the repository browser.