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

Last change on this file since 3464 was 3463, checked in by ldelgass, 11 years ago

Begin process of renaming R2 library

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