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

Last change on this file since 2831 was 2831, checked in by ldelgass, 13 years ago

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

  • Property svn:eol-style set to native
File size: 2.7 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-2006  Purdue Research Foundation
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 "TransferFunction.h"
18#include <memory.h>
19#include <assert.h>
20
21TransferFunction::TransferFunction(int size, float *data)
22{
23    // _size : # of slot, 4 : rgba
24    _size = size * 4;
25    _data = new float[_size];
26    memcpy(_data, data, sizeof(float) * _size);
27
28    _tex = new Texture1D(size, GL_FLOAT, GL_LINEAR, 4, data);
29    _id = _tex->id;
30}
31
32TransferFunction::~TransferFunction()
33{
34    delete [] _data;
35    delete _tex;
36}
37
38void
39TransferFunction::update(float *data)
40{
41    memcpy(_data, data, sizeof(float) * _size);
42    _tex->update(_data);
43}
44
45void
46TransferFunction::update(int size, float *data)
47{
48    // TBD..
49    //assert((size*4) == _size);
50    update(data);
51}
52
53void
54TransferFunction::sample(float fraction, float *key, int count, Vector3 *keyValue, Vector3 *ret)
55{
56    int limit = count - 1;
57    if (fraction <= key[0]) {
58        *ret = keyValue[0];
59    } else if (fraction >= key[limit]) {
60        *ret = keyValue[limit];
61    } else {
62        int n;
63        for (n = 0; n < limit; n++){
64            if (fraction >= key[n] && fraction < key[n+1]) break;
65        }
66        if (n >= limit) {
67            *ret = keyValue[limit];
68        } else {
69            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
70            ret->set(inter * (keyValue[n + 1].x - keyValue[n].x) + keyValue[n].x,
71                     inter * (keyValue[n + 1].y - keyValue[n].y) + keyValue[n].y,
72                     inter * (keyValue[n + 1].z - keyValue[n].z) + keyValue[n].z);
73        }
74    }
75}
76
77void
78TransferFunction::sample(float fraction, float *key, int count, float *keyValue, float *ret)
79{
80    int limit = count - 1;
81    if (fraction <= key[0]) {
82        *ret = keyValue[0];
83    } else if (fraction >= key[limit]) {
84        *ret = keyValue[limit];
85    } else {
86        int n;
87        for (n = 0; n < limit; n++){
88            if (fraction >= key[n] && fraction < key[n+1]) break;
89        }
90        if (n >= limit) {
91            *ret = keyValue[limit];
92        } else {
93            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
94            *ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n];
95        }
96    }
97}
Note: See TracBrowser for help on using the repository browser.