source: nanovis/trunk/ContourLineFilter.cpp @ 6632

Last change on this file since 6632 was 3611, checked in by ldelgass, 11 years ago

Use nv namespace for classes in nanovis rather than prefixing class names with
Nv (still need to convert shader classes).

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