source: nanovis/tags/1.1.1/ContourLineFilter.cpp @ 4937

Last change on this file since 4937 was 3492, checked in by ldelgass, 11 years ago

Fix camera reset for nanovis. Includes refactoring of vector/matrix classes
in nanovis to consolidate into vrmath library. Also add preliminary canonical
view control to clients for testing.

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