source: trunk/packages/vizservers/nanovis/TransferFunction.cpp @ 3875

Last change on this file since 3875 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: 2.9 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * ColorMap.h: color map class contains an array of (RGBA) values
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
8 *           Purdue Rendering and Perceptualization Lab (PURPL)
9 *
10 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16
17#include <memory.h>
18#include <assert.h>
19
20#include <vrmath/Vector3f.h>
21
22#include "TransferFunction.h"
23
24using namespace nv;
25using namespace vrmath;
26
27TransferFunction::TransferFunction(const char *name, int size, float *data) :
28    _name(name)
29{
30    // _size : # of slot, 4 : rgba
31    _size = size * 4;
32    _data = new float[_size];
33    memcpy(_data, data, sizeof(float) * _size);
34
35    _tex = new Texture1D(size, GL_FLOAT, GL_LINEAR, 4, data);
36    _id = _tex->id();
37}
38
39TransferFunction::~TransferFunction()
40{
41    delete [] _data;
42    delete _tex;
43}
44
45void
46TransferFunction::update(float *data)
47{
48    memcpy(_data, data, sizeof(float) * _size);
49    _tex->update(_data);
50}
51
52void
53TransferFunction::update(int size, float *data)
54{
55    // TBD..
56    //assert((size*4) == _size);
57    update(data);
58}
59
60void
61TransferFunction::sample(float fraction, float *key, int count, Vector3f *keyValue, Vector3f *ret)
62{
63    int limit = count - 1;
64    if (fraction <= key[0]) {
65        *ret = keyValue[0];
66    } else if (fraction >= key[limit]) {
67        *ret = keyValue[limit];
68    } else {
69        int n;
70        for (n = 0; n < limit; n++){
71            if (fraction >= key[n] && fraction < key[n+1]) break;
72        }
73        if (n >= limit) {
74            *ret = keyValue[limit];
75        } else {
76            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
77            ret->set(inter * (keyValue[n + 1].x - keyValue[n].x) + keyValue[n].x,
78                     inter * (keyValue[n + 1].y - keyValue[n].y) + keyValue[n].y,
79                     inter * (keyValue[n + 1].z - keyValue[n].z) + keyValue[n].z);
80        }
81    }
82}
83
84void
85TransferFunction::sample(float fraction, float *key, int count, float *keyValue, float *ret)
86{
87    int limit = count - 1;
88    if (fraction <= key[0]) {
89        *ret = keyValue[0];
90    } else if (fraction >= key[limit]) {
91        *ret = keyValue[limit];
92    } else {
93        int n;
94        for (n = 0; n < limit; n++){
95            if (fraction >= key[n] && fraction < key[n+1]) break;
96        }
97        if (n >= limit) {
98            *ret = keyValue[limit];
99        } else {
100            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
101            *ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n];
102        }
103    }
104}
Note: See TracBrowser for help on using the repository browser.