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

Last change on this file since 3492 was 3492, checked in by ldelgass, 6 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: 2.8 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-2012  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 vrmath;
25
26TransferFunction::TransferFunction(int size, float *data)
27{
28    // _size : # of slot, 4 : rgba
29    _size = size * 4;
30    _data = new float[_size];
31    memcpy(_data, data, sizeof(float) * _size);
32
33    _tex = new Texture1D(size, GL_FLOAT, GL_LINEAR, 4, data);
34    _id = _tex->id();
35}
36
37TransferFunction::~TransferFunction()
38{ 
39    delete [] _data;
40    delete _tex; 
41}
42
43void
44TransferFunction::update(float *data)
45{
46    memcpy(_data, data, sizeof(float) * _size);
47    _tex->update(_data);
48}
49
50void
51TransferFunction::update(int size, float *data)
52{
53    // TBD..
54    //assert((size*4) == _size);
55    update(data);
56}
57
58void
59TransferFunction::sample(float fraction, float *key, int count, Vector3f *keyValue, Vector3f *ret)
60{
61    int limit = count - 1;
62    if (fraction <= key[0]) {
63        *ret = keyValue[0];
64    } else if (fraction >= key[limit]) {
65        *ret = keyValue[limit];
66    } else {
67        int n;
68        for (n = 0; n < limit; n++){
69            if (fraction >= key[n] && fraction < key[n+1]) break;
70        }
71        if (n >= limit) {
72            *ret = keyValue[limit];
73        } else {
74            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
75            ret->set(inter * (keyValue[n + 1].x - keyValue[n].x) + keyValue[n].x,
76                     inter * (keyValue[n + 1].y - keyValue[n].y) + keyValue[n].y,
77                     inter * (keyValue[n + 1].z - keyValue[n].z) + keyValue[n].z);
78        }
79    }
80}
81
82void
83TransferFunction::sample(float fraction, float *key, int count, float *keyValue, float *ret)
84{
85    int limit = count - 1;
86    if (fraction <= key[0]) {
87        *ret = keyValue[0];
88    } else if (fraction >= key[limit]) {
89        *ret = keyValue[limit];
90    } else {
91        int n;
92        for (n = 0; n < limit; n++){
93            if (fraction >= key[n] && fraction < key[n+1]) break;
94        }
95        if (n >= limit) {
96            *ret = keyValue[limit];
97        } else {
98            float inter = (fraction - key[n]) / (key[n + 1] - key[n]);
99            *ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n];
100        }
101    }
102}
Note: See TracBrowser for help on using the repository browser.