Changeset 953


Ignore:
Timestamp:
Mar 12, 2008, 3:48:06 PM (16 years ago)
Author:
gah
Message:

remove warnings from compile

Location:
trunk/vizservers/nanovis
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/vizservers/nanovis/BucketSort.cpp

    r821 r953  
    33using namespace PCA;
    44
    5 void BucketSort::init()
     5void
     6BucketSort::init()
    67{
    7         _count = 0;
    8         memset(_buffer, 0, sizeof(ClusterList*) * _size);
     8    _count = 0;
     9    memset(_buffer, 0, sizeof(ClusterList*) * _size);
    910}
    1011
    11 void BucketSort::sort(ClusterAccel* clusterAccel, const Mat4x4& cameraMat, int level)
     12void
     13BucketSort::sort(ClusterAccel* clusterAccel, const Mat4x4& cameraMat, int level)
    1214{
    13         if (clusterAccel == 0) return;
     15    if (clusterAccel == 0) {
     16        return;
     17    }
     18    Cluster* cluster = clusterAccel->startPointerCluster[level - 1];
     19    Cluster* c = &(cluster[0]);
     20    Cluster* end = &(cluster[clusterAccel->numOfClusters[level - 1] - 1]);
    1421
    15         Cluster* cluster = clusterAccel->startPointerCluster[level - 1];
    16         Cluster* c = &(cluster[0]);
    17         Cluster* end = &(cluster[clusterAccel->numOfClusters[level - 1] - 1]);
     22    Vector3 pos;
     23    for (; c <= end; c = (Cluster*) ((int) c + sizeof(Cluster))) {
     24        pos.transform(c->centroid, cameraMat);
     25        addBucket(c, pos.length()*_invMaxLength);
     26    }
     27}       
    1828
    19         Vector3 pos;
    20         int count = clusterAccel->numOfClusters[level - 1];
    21        
    22         for (; c <= end; c = (Cluster*) ((int) c + sizeof(Cluster)))
    23         {
    24                 pos.transform(c->centroid, cameraMat);
    25                 addBucket(c, pos.length()*_invMaxLength);
    26         }
    27 }       
    28 
    29 void BucketSort::addBucket(Cluster* cluster, float ratio)
     29void
     30BucketSort::addBucket(Cluster* cluster, float ratio)
    3031{
    31         int index = (int) (ratio * _size);
    32         ClusterList* c = &(_memChunck[_count++]);
    33         c->data = cluster;
    34         c->next =_buffer[index];
    35         _buffer[index] = c;
     32    int index = (int) (ratio * _size);
     33    ClusterList* c = &(_memChunck[_count++]);
     34    c->data = cluster;
     35    c->next =_buffer[index];
     36    _buffer[index] = c;
    3637}
    3738
  • trunk/vizservers/nanovis/ContourLineFilter.cpp

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

    r931 r953  
    5151
    5252
    53 static void saveFloatGradients(float *gradients, int *sizes)
     53#ifdef notused
     54static void
     55saveFloatGradients(float *gradients, int *sizes)
    5456{
    5557    char *filename;
     
    6163        exit(1);
    6264    }
    63 
    6465    if (fwrite(gradients, 3 * sizes[0] * sizes[1] * sizes[2] * sizeof(float),
    6566               1, fp) != 1) {
     
    6768        exit(1);
    6869    }
    69 
    7070    fclose(fp);
    7171}
    72 
    73 
    74 int getNextPowerOfTwo(int n)
     72#endif
     73
     74int
     75getNextPowerOfTwo(int n)
    7576{
    7677    int i;
  • trunk/vizservers/nanovis/Grid.cpp

    r932 r953  
    2222    glPushMatrix();
    2323    glEnable(GL_DEPTH_TEST);
     24    glDisable(GL_TEXTURE_2D);
     25    glEnable(GL_BLEND);
     26
    2427#ifdef notdef
    2528    glEnable(GL_LINE_SMOOTH);
     
    4851    glEnd();
    4952
    50     glDisable(GL_TEXTURE_2D);
    5153    glLineWidth(1.0f);
    52     glEnable(GL_BLEND);
    5354    glColor4f(_majorColor.red, _majorColor.green, _majorColor.blue,
    5455              _majorColor.alpha);
  • trunk/vizservers/nanovis/NvCamera.cpp

    r881 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    1516
    1617#include <stdio.h>
     18#include <GL/glu.h>
    1719#include "NvCamera.h"
    1820
    1921NvCamera::NvCamera(int startx, int starty, int w, int h,
    20                 double loc_x, double loc_y, double loc_z,
    21                 double target_x, double target_y, double target_z,
    22                 int angle_x, int angle_y, int angle_z):
    23      startX(startx),
    24      startY(starty),
    25          width(w),
    26          height(h),
    27          location(Vector3(loc_x, loc_y, loc_z)),
    28          target(Vector3(target_x, target_y, target_z)),
    29          angle(Vector3(angle_x, angle_y, angle_z)) { }
    30 
    31 NvCamera::~NvCamera(){} 
    32 
    33 void NvCamera::move(double loc_x, double loc_y, double loc_z)
     22                   double loc_x, double loc_y, double loc_z,
     23                   double target_x, double target_y, double target_z,
     24                   int angle_x, int angle_y, int angle_z):
     25    location(Vector3(loc_x, loc_y, loc_z)),
     26    target(Vector3(target_x, target_y, target_z)),
     27    angle(Vector3(angle_x, angle_y, angle_z)),
     28    width(w),
     29    height(h),
     30    startX(startx),
     31    startY(starty)
    3432{
    35   location = Vector3(loc_x, loc_y, loc_z);
     33    /*empty*/
    3634}
    3735
    38 void NvCamera::aim(double target_x, double target_y, double target_z)
     36NvCamera::~NvCamera()
     37{
     38    /*empty*/
     39}       
     40
     41void
     42NvCamera::move(double loc_x, double loc_y, double loc_z)
    3943{
    40   target = Vector3(target_x, target_y, target_z);
     44    location = Vector3(loc_x, loc_y, loc_z);
    4145}
    4246
    43 void NvCamera::rotate(double angle_x, double angle_y, double angle_z)
     47void
     48NvCamera::aim(double target_x, double target_y, double target_z)
    4449{
    45   angle = Vector3(angle_x, angle_y, angle_z);
     50    target = Vector3(target_x, target_y, target_z);
    4651}
    4752
    48 void NvCamera::activate(){
    49   //fprintf(stderr, "camera: %d, %d\n", width, height);
    50   glViewport(startX, startY, width, height);
    51   glMatrixMode(GL_PROJECTION);
    52   glLoadIdentity();
    53   gluPerspective(30, (GLdouble)(width - startX)/(GLdouble)(height - startY), 0.1, 50.0);
    54 
    55   glMatrixMode(GL_MODELVIEW);
    56   glLoadIdentity();
    57 
    58   gluLookAt(location.x, location.y, location.z,
    59             target.x, target.y, target.z,
    60             0., 1., 0.);
    61 
    62    glRotated(angle.x, 1., 0., 0.);
    63    glRotated(angle.y, 0., 1., 0.);
    64    glRotated(angle.z, 0., 0., 1.);
     53void
     54NvCamera::rotate(double angle_x, double angle_y, double angle_z)
     55{
     56    angle = Vector3(angle_x, angle_y, angle_z);
    6557}
    6658
    67 void NvCamera::set_screen_size(int sx, int sy, int w, int h){
    68   width = w; height = h;
    69   startX = sx; startY = sy;
     59void
     60NvCamera::activate()
     61{
     62    //fprintf(stderr, "camera: %d, %d\n", width, height);
     63    glViewport(startX, startY, width, height);
     64    glMatrixMode(GL_PROJECTION);
     65    glLoadIdentity();
     66    gluPerspective(30, (GLdouble)(width - startX)/(GLdouble)(height - startY),
     67        0.1, 50.0);
     68
     69    glMatrixMode(GL_MODELVIEW);
     70    glLoadIdentity();
     71
     72    gluLookAt(location.x, location.y, location.z,
     73              target.x, target.y, target.z,
     74              0., 1., 0.);
     75
     76    glRotated(angle.x, 1., 0., 0.);
     77    glRotated(angle.y, 0., 1., 0.);
     78    glRotated(angle.z, 0., 0., 1.);
    7079}
     80
     81void
     82NvCamera::set_screen_size(int sx, int sy, int w, int h)
     83{
     84    width = w, height = h;
     85    startX = sx, startY = sy;
     86}
  • trunk/vizservers/nanovis/NvCamera.h

    r881 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    1718#define _CAMERA_H_
    1819
    19 
    20 #include <GL/glu.h>
    2120#include "Vector3.h"
    2221
    23 class NvCamera{
     22class NvCamera {
    2423
    2524public:
    26         Vector3 location;       //Location of the camera in the scene
    27         Vector3 target;         //Location the camera is looking at.
    28                                 //location and target: two points define the line-of-sight     
    29         Vector3 angle;          //rotation angles of camera along x, y, z
    30         int width;      //screen size
    31         int height;     //screen size
     25    Vector3 location;           //Location of the camera in the scene
     26    Vector3 target;             //Location the camera is looking at. 
     27                                //location and target: two points define the
     28                                //line-of-sight
     29    Vector3 angle;              //rotation angles of camera along x, y, z
     30    int width;                  //screen size
     31    int height;                 //screen size
    3232    int startX;
    3333    int startY;
    3434
    35         ~NvCamera();
    36         NvCamera(int startx, int starty, int w, int h,
    37                 double loc_x, double loc_y, double loc_z,
    38                 double target_x, double target_y, double target_z,
    39                 int angle_x, int angle_y, int angle_z);
    40         void move(double loc_x, double loc_y, double loc_z);    //move location of camera
    41         void aim(double target_x, double target_y, double target_z); //change target point
    42         void rotate(double angle_x, double angle_y, double angle_z); //change target point
    43         void activate();//make the camera setting active, this has to be called before drawing things.
    44         void set_screen_size(int startx, int starty, int w, int h);
     35    ~NvCamera();
     36    NvCamera(int startx, int starty, int w, int h,
     37             double loc_x, double loc_y, double loc_z,
     38             double target_x, double target_y, double target_z,
     39             int angle_x, int angle_y, int angle_z);
     40    void move(double loc_x, double loc_y, double loc_z); //move location of camera
     41    void aim(double target_x, double target_y, double target_z); //change target point
     42    void rotate(double angle_x, double angle_y, double angle_z); //change target point
     43    void activate(); //make the camera setting active, this has to be called
     44                     //before drawing things.
     45    void set_screen_size(int startx, int starty, int w, int h);
    4546};
    4647
  • trunk/vizservers/nanovis/NvParticleAdvectionShader.cpp

    r851 r953  
    33#include "NvParticleAdvectionShader.h"
    44
    5 NvParticleAdvectionShader::NvParticleAdvectionShader()
    6 : _scale(1.0f, 1.0f, 1.0f), _velocityVolumeID(0), _max(1.0f), _timeStep(0.005f)
     5NvParticleAdvectionShader::NvParticleAdvectionShader() :
     6    _velocityVolumeID(0),
     7    _scale(1.0f, 1.0f, 1.0f),
     8    _max(1.0f),
     9    _timeStep(0.005f)
    710{
    811    init();
  • trunk/vizservers/nanovis/NvZincBlendeReconstructor.cpp

    r806 r953  
    3030{
    3131    Vector3 origin, delta;
    32     double temp;
    33     int width = 0, height = 0, depth = 0;
    34     void* data = NULL;
    35 
    3632
    3733    std::ifstream stream;
     
    4945    ZincBlendeVolume* volume = 0;
    5046    Vector3 origin, delta;
    51     double temp;
    5247    int width = 0, height = 0, depth = 0;
    5348    void* data = NULL;
     
    8277
    8378
    84     if (version == 1)
    85     {
     79    if (version == 1) {
     80        float dummy;
     81
    8682        sscanf(buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4],&width, &height, &depth);
    8783        getLine(stream);
    8884        sscanf(buff, "%s%f%f%f", str[0], &(origin.x), &(origin.y), &(origin.z));
    8985        getLine(stream);
    90         sscanf(buff, "%s%f%f%f", str[0], &(delta.x), &temp, &temp);
    91         getLine(stream);
    92         sscanf(buff, "%s%f%f%f", str[0], &temp, &(delta.y), &temp);
    93         getLine(stream);
    94         sscanf(buff, "%s%f%f%f", str[0], &temp, &temp, &(delta.z));
     86        sscanf(buff, "%s%f%f%f", str[0], &(delta.x), &dummy, &dummy);
     87        getLine(stream);
     88        sscanf(buff, "%s%f%f%f", str[0], &dummy, &(delta.y), &dummy);
     89        getLine(stream);
     90        sscanf(buff, "%s%f%f%f", str[0], &dummy, &dummy, &(delta.z));
    9591        do {
    9692            getLine(stream);
     
    224220
    225221
    226 ZincBlendeVolume* NvZincBlendeReconstructor::buildUp(const Vector3& origin, const Vector3& delta, int width, int height, int depth, void* data)
     222ZincBlendeVolume*
     223NvZincBlendeReconstructor::buildUp(const Vector3& origin, const Vector3& delta,
     224        int width, int height, int depth, void* data)
    227225{
    228226    ZincBlendeVolume* zincBlendeVolume = NULL;
     
    239237    int index;
    240238
     239    nzero_min = 0.0f;           /* Suppress compiler warning. */
    241240    vmin = vmax = srcPtr->atom;
    242241
     
    407406    ZincBlendeVolume* volume = 0;
    408407    Vector3 origin, delta;
    409     double temp;
    410408    int width = 0, height = 0, depth = 0;
    411409    void* data = NULL;
     
    439437    if (version == 1)
    440438    {
     439        float dummy;
     440
    441441        sscanf(buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4],&width, &height, &depth);
    442442        getLine(stream);
    443443        sscanf(buff, "%s%f%f%f", str[0], &(origin.x), &(origin.y), &(origin.z));
    444444        getLine(stream);
    445         sscanf(buff, "%s%f%f%f", str[0], &(delta.x), &temp, &temp);
    446         getLine(stream);
    447         sscanf(buff, "%s%f%f%f", str[0], &temp, &(delta.y), &temp);
    448         getLine(stream);
    449         sscanf(buff, "%s%f%f%f", str[0], &temp, &temp, &(delta.z));
     445        sscanf(buff, "%s%f%f%f", str[0], &(delta.x), &dummy, &dummy);
     446        getLine(stream);
     447        sscanf(buff, "%s%f%f%f", str[0], &dummy, &(delta.y), &dummy);
     448        getLine(stream);
     449        sscanf(buff, "%s%f%f%f", str[0], &dummy, &dummy, &(delta.z));
    450450        do {
    451451            getLine(stream);
  • trunk/vizservers/nanovis/PlaneRenderer.cpp

    r755 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    1819
    1920PlaneRenderer::PlaneRenderer(CGcontext _context, int _width, int _height):
    20   n_planes(0),
    21   g_context(_context),
    22   render_width(_width),
    23   render_height(_height),
    24   active_plane(-1)
     21    active_plane(-1),
     22    n_planes(0),
     23    render_width(_width),
     24    render_height(_height),
     25    g_context(_context)
    2526{
    26   plane.clear();
    27   tf.clear();
    28   init_shaders();
     27    plane.clear();
     28    tf.clear();
     29    init_shaders();
     30}
     31
     32PlaneRenderer::~PlaneRenderer()
     33{
     34    /*empty*/
     35}
     36
     37//initialize the render shader
     38void
     39PlaneRenderer::init_shaders()
     40{
     41    //plane rendering shader
     42    R2string path = R2FilePath::getInstance()->getPath("one_plane.cg");
     43    m_fprog = loadProgram(g_context, CG_PROFILE_FP30, CG_SOURCE, path);
     44    m_data_param = cgGetNamedParameter(m_fprog, "data");
     45    m_tf_param = cgGetNamedParameter(m_fprog, "tf");
     46    m_render_param = cgGetNamedParameter(m_fprog, "render_param");
    2947}
    3048
    3149
    32 PlaneRenderer::~PlaneRenderer(){}
     50int
     51PlaneRenderer::add_plane(Texture2D* _p, TransferFunction* _tf)
     52{
     53    int ret = n_planes;
    3354
    34 //initialize the render shader
    35 void PlaneRenderer::init_shaders(){
    36  
    37   //plane rendering shader
    38   R2string path = R2FilePath::getInstance()->getPath("one_plane.cg");
    39   m_fprog = loadProgram(g_context, CG_PROFILE_FP30, CG_SOURCE, path);
    40   m_data_param = cgGetNamedParameter(m_fprog, "data");
    41   m_tf_param = cgGetNamedParameter(m_fprog, "tf");
    42   m_render_param = cgGetNamedParameter(m_fprog, "render_param");
    43 }
     55    plane.push_back(_p);
     56    tf.push_back(_tf);
    4457
     58    if(ret==0)
     59        active_plane = ret;
    4560
    46 int PlaneRenderer::add_plane(Texture2D* _p, TransferFunction* _tf){
    47 
    48   int ret = n_planes;
    49 
    50   plane.push_back(_p);
    51   tf.push_back(_tf);
    52 
    53   if(ret==0)
    54     active_plane = ret;
    55 
    56   n_planes++;
    57   return ret;
     61    n_planes++;
     62    return ret;
    5863}
    5964
    6065void
    6166PlaneRenderer::remove_plane(int index) {
    62   vector<Texture2D*>::iterator piter = plane.begin()+index;
    63   vector<TransferFunction*>::iterator tfiter = tf.begin()+index;
     67    vector<Texture2D*>::iterator piter = plane.begin()+index;
     68    vector<TransferFunction*>::iterator tfiter = tf.begin()+index;
    6469
    65   plane.erase(piter);
    66   tf.erase(tfiter);
     70    plane.erase(piter);
     71    tf.erase(tfiter);
    6772
    68   n_planes--;
     73    n_planes--;
    6974}
    7075
    7176
    72 void PlaneRenderer::render(){
    73   if (n_planes == 0)
    74     return;
     77void
     78PlaneRenderer::render()
     79{
     80    if (n_planes == 0)
     81        return;
    7582
    76   glEnable(GL_TEXTURE_2D);
    77   glEnable(GL_BLEND);
     83    glEnable(GL_TEXTURE_2D);
     84    glEnable(GL_BLEND);
    7885
    79   glViewport(0, 0, render_width, render_height);
    80   glMatrixMode(GL_PROJECTION);
    81   glLoadIdentity();
    82   gluOrtho2D(0, render_width, 0, render_height);
    83   glMatrixMode(GL_MODELVIEW);
    84   glLoadIdentity();
     86    glViewport(0, 0, render_width, render_height);
     87    glMatrixMode(GL_PROJECTION);
     88    glLoadIdentity();
     89    gluOrtho2D(0, render_width, 0, render_height);
     90    glMatrixMode(GL_MODELVIEW);
     91    glLoadIdentity();
    8592
    86   //glColor3f(1.,1.,1.);         //MUST HAVE THIS LINE!!!
     93    //glColor3f(1.,1.,1.);         //MUST HAVE THIS LINE!!!
    8794
    88   //if no active plane
    89   if(active_plane == -1)
    90     return;
     95    //if no active plane
     96    if(active_plane == -1)
     97        return;
    9198
    92   activate_shader(active_plane);
    93   glBegin(GL_QUADS);
    94   glTexCoord2f(0, 0); glVertex2f(0, 0);
    95   glTexCoord2f(1, 0); glVertex2f(render_width, 0);
    96   glTexCoord2f(1, 1); glVertex2f(render_width, render_height);
    97   glTexCoord2f(0, 1); glVertex2f(0, render_height);
    98   glEnd();
    99   deactivate_shader();
     99    activate_shader(active_plane);
     100    glBegin(GL_QUADS);
     101    glTexCoord2f(0, 0); glVertex2f(0, 0);
     102    glTexCoord2f(1, 0); glVertex2f(render_width, 0);
     103    glTexCoord2f(1, 1); glVertex2f(render_width, render_height);
     104    glTexCoord2f(0, 1); glVertex2f(0, render_height);
     105    glEnd();
     106    deactivate_shader();
    100107
    101108}
    102109
    103 void PlaneRenderer::activate_shader(int index){
     110void
     111PlaneRenderer::activate_shader(int index)
     112{
    104113
    105   cgGLSetTextureParameter(m_data_param, plane[index]->id);
    106   cgGLSetTextureParameter(m_tf_param, tf[index]->id);
    107   cgGLEnableTextureParameter(m_data_param);
    108   cgGLEnableTextureParameter(m_tf_param);
     114    cgGLSetTextureParameter(m_data_param, plane[index]->id);
     115    cgGLSetTextureParameter(m_tf_param, tf[index]->id);
     116    cgGLEnableTextureParameter(m_data_param);
     117    cgGLEnableTextureParameter(m_tf_param);
    109118
    110   cgGLSetParameter4f(m_render_param, 0., 0., 0., 0.);
     119    cgGLSetParameter4f(m_render_param, 0., 0., 0., 0.);
    111120
    112   cgGLBindProgram(m_fprog);
    113   cgGLEnableProfile(CG_PROFILE_FP30);
     121    cgGLBindProgram(m_fprog);
     122    cgGLEnableProfile(CG_PROFILE_FP30);
    114123}
    115124
    116125
    117 void PlaneRenderer::deactivate_shader(){
    118   cgGLDisableProfile(CG_PROFILE_FP30);
    119   cgGLDisableTextureParameter(m_data_param);
    120   cgGLDisableTextureParameter(m_tf_param);
     126void
     127PlaneRenderer::deactivate_shader()
     128{
     129    cgGLDisableProfile(CG_PROFILE_FP30);
     130    cgGLDisableTextureParameter(m_data_param);
     131    cgGLDisableTextureParameter(m_tf_param);
    121132}
    122133
    123134void
    124  PlaneRenderer::set_active_plane(int index) {
    125   active_plane = index;
     135PlaneRenderer::set_active_plane(int index)
     136{
     137    active_plane = index;
    126138}
    127139
    128140void
    129 PlaneRenderer::set_screen_size(int w, int h) {
    130   render_width = w;
    131   render_height = h;
     141PlaneRenderer::set_screen_size(int w, int h)
     142{
     143    render_width = w;
     144    render_height = h;
    132145}
  • trunk/vizservers/nanovis/PlaneRenderer.h

    r455 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    3637
    3738private:
    38   vector <Texture2D*> plane;     //array of volumes
    39   vector <TransferFunction*> tf; //array of corresponding transfer functions
    40   int active_plane;              //the active plane, only one is rendered
    41   int n_planes;
     39    vector <Texture2D*> plane;  // Array of volumes
     40    vector <TransferFunction*> tf; // Array of corresponding transfer functions
     41    int active_plane;           // The active plane, only one is rendered
     42    int n_planes;
    4243
    43   int render_width;     //render size
    44   int render_height;   
     44    int render_width;   //render size
     45    int render_height; 
    4546
    46   //cg related
    47   CGcontext g_context;          //the Nvidia cg context
    48   CGprogram m_fprog;
    49   CGparameter m_data_param;
    50   CGparameter m_tf_param;
    51   CGparameter m_render_param;
     47    //cg related
     48    CGcontext g_context;        // The Nvidia cg context
     49    CGprogram m_fprog;
     50    CGparameter m_data_param;
     51    CGparameter m_tf_param;
     52    CGparameter m_render_param;
    5253
    53   void init_shaders();
    54   void activate_shader(int plane_index);
    55   void deactivate_shader();
     54    void init_shaders();
     55    void activate_shader(int plane_index);
     56    void deactivate_shader();
    5657
    5758public:
    58   PlaneRenderer(CGcontext _context, int width, int height);
    59   ~PlaneRenderer();
     59    PlaneRenderer(CGcontext _context, int width, int height);
     60    ~PlaneRenderer();
    6061
    61   int add_plane(Texture2D* _p, TransferFunction* _tf);
    62                                                 //add a plane and its transfer function
    63                                                 //we require a transfer function when a
    64                                                 //plane is added.
    65   void remove_plane(int index);
    66   void set_active_plane(int index);             //set the active plane to be rendered
    67   void set_screen_size(int w, int h);           //change the rendering size
    68   void render();
     62    int add_plane(Texture2D* _p, TransferFunction* _tf);
     63    // Add a plane and its transfer function. We require a transfer function
     64    // when a plane is added.
     65    void remove_plane(int index);
     66    void set_active_plane(int index); //set the active plane to be rendered
     67    void set_screen_size(int w, int h); //change the rendering size
     68    void render();
    6969};
    7070
  • trunk/vizservers/nanovis/PointSet.cpp

    r827 r953  
    44#include <stdio.h>
    55
    6 PointSet::PointSet()
    7 : _visible(false), _cluster(0), _sortLevel(4), _min(0.0f), _max(1.0f)
     6PointSet::PointSet() :
     7    _sortLevel(4),
     8    _cluster(0),
     9    _max(1.0f),
     10    _min(0.0f),
     11    _visible(false)
    812{
     13    /*empty*/
    914}
    1015
  • trunk/vizservers/nanovis/PointSet.h

    r827 r953  
     1
    12#ifndef __POINT_SET_H__
    23#define __POINT_SET_H__
  • trunk/vizservers/nanovis/PointSetRenderer.cpp

    r915 r953  
     1
    12#include "Nv.h"
    23#include <GL/gl.h>
     
    1718    _shader = new PointShader();
    1819    R2string path = R2FilePath::getInstance()->getPath("particle2.bmp");
    19     if (path.getLength() == 0)
    20     {
     20    if (path.getLength() == 0) {
    2121        printf("ERROR : pointset file not found - %s\n", (const char*) path);
    2222        fflush(stdout);
     
    2828
    2929    unsigned char* bytes = (unsigned char*) image->getImageBuffer();
    30     if (bytes)
    31     {
    32         for (int y = 0; y < image->getHeight(); ++y)
    33            for (int x = 0; x < image->getWidth(); ++x, bytes +=4)
    34            {
     30    if (bytes) {
     31        for (unsigned int y = 0; y < image->getHeight(); ++y) {
     32            for (unsigned int x = 0; x < image->getWidth(); ++x, bytes +=4) {
    3533                bytes[3] =  (bytes[0] == 0)? 0 : 255;
    36            }
     34            }
     35        }
    3736    }
    3837
    39     if (image)
    40     {
    41         _pointTexture = new Texture2D(image->getWidth(), image->getHeight(), GL_UNSIGNED_BYTE, GL_LINEAR,   
    42                                 4, (float*) image->getImageBuffer());
    43     }
    44     else
    45     {
     38    if (image) {
     39        _pointTexture = new Texture2D(image->getWidth(), image->getHeight(),
     40                                      GL_UNSIGNED_BYTE, GL_LINEAR,   
     41                                      4, (float*) image->getImageBuffer());
     42    } else {
    4643        printf("fail to load image [%s]\n", "particles2.bmp");
    4744    }
    4845
    4946    delete loader;
    50 
    5147    _bucketSort = new PCA::BucketSort(1024);
    5248}
  • trunk/vizservers/nanovis/R2/include/R2/R2Fonts.h

    r603 r953  
    99class R2Fonts : public R2Object {
    1010
    11         struct R2FontAttributes {
    12                 R2string _fontName;
    13                 R2int32 _textureWidth;
    14                 R2int32 _textureHeight;
    15                 R2int32 _fontHeight;
    16                 R2uint32 _fontTextureID;
    17                 R2uint32 _displayLists;
    18                
    19                 struct R2CharInfo {
    20                         R2float _left;
    21                         R2float _right;
    22                         R2float _top;
    23                         R2float _bottom;
    24                         R2bool _valid;
    25                         R2float _width;
    26                        
    27                 };
     11    struct R2FontAttributes {
     12        R2string _fontName;
     13        R2int32 _textureWidth;
     14        R2int32 _textureHeight;
     15        R2int32 _fontHeight;
     16        R2uint32 _fontTextureID;
     17        R2uint32 _displayLists;
     18       
     19        struct R2CharInfo {
     20            R2float _left;
     21            R2float _right;
     22            R2float _top;
     23            R2float _bottom;
     24            R2bool _valid;
     25            R2float _width;
     26           
     27        };
     28        R2CharInfo _chars[256];
     29    };
     30   
     31    typedef std::vector<R2FontAttributes>  R2FontVector;
    2832
    29                 R2CharInfo _chars[256];
    30         };
    31 
    32         typedef std::vector<R2FontAttributes>  R2FontVector;
    33        
    34         R2FontVector    _fonts;
    35 
    36         /**
    37          * @brief current font index
    38          */
    39         R2int32 _fontIndex;
    40        
    41         /**
    42          * @brief screen width
    43          */
    44         R2int32 _screenWidth;
    45 
    46         /**
    47          * @brief screen height
    48          */
    49         R2int32 _screenHeight;
    50 
     33    R2FontVector        _fonts;
     34   
     35    /**
     36     * @brief current font index
     37     */
     38    R2int32 _fontIndex;
     39   
     40    /**
     41     * @brief screen width
     42     */
     43    R2int32 _screenWidth;
     44   
     45    /**
     46     * @brief screen height
     47     */
     48    R2int32 _screenHeight;
     49   
    5150private :
    5251    ~R2Fonts();
    53 
    54 //private :
     52   
     53    //private :
    5554public :
    56         /**
    57         * @brief set projection to orthographic
    58         */
    59         void begin();
    60 
    61         /**
    62         * @brief reset projection matrix
    63         */
    64         void end();
    65 
    66         /**
    67         * @brief initialize R2FontAttributes
    68         */
    69         void initializeFont(R2FontAttributes& attr);
    70 
    71         /**
    72         * @brief load font data
    73         */
    74         R2bool loadFont(const char* fontName, const char* fontFileName, R2FontAttributes& sFont);
    75 
     55    /**
     56    * @brief set projection to orthographic
     57    */
     58    void begin();
     59   
     60    /**
     61    * @brief reset projection matrix
     62    */
     63    void end();
     64   
     65    /**
     66    * @brief initialize R2FontAttributes
     67    */
     68    void initializeFont(R2FontAttributes& attr);
     69   
     70    /**
     71    * @brief load font data
     72    */
     73    R2bool loadFont(const char* fontName, const char* fontFileName, R2FontAttributes& sFont);
     74   
    7675public :
    77         /**
    78          * @brief constructor
    79          */
    80         R2Fonts();
    81 
    82         /**
    83          * @brief
    84          */
    85         void addFont(const char* fontName, const char* fontFileName);
    86 
    87         /**
    88          * @brief
    89          */
    90         void setFont(const char* fontName);
    91 
    92 
    93        
    94         /**
    95          *
    96          */
    97         void draw(const char* pString, ...) const;
    98 
    99         /**
    100          *
    101          */
    102         void resize(R2int32 width, R2int32 height);
    103 
    104         /**
    105          * @brief return font height
    106          */
    107         R2int32 getFontHeight() const;
    108 
     76    /**
     77     * @brief constructor
     78     */
     79    R2Fonts();
     80   
     81    /**
     82     * @brief
     83     */
     84    void addFont(const char* fontName, const char* fontFileName);
     85   
     86    /**
     87     * @brief
     88     */
     89    void setFont(const char* fontName);
     90   
     91    /**
     92     *
     93     */
     94    void draw(const char* pString, ...) const;
     95   
     96    /**
     97     *
     98     */
     99    void resize(R2int32 width, R2int32 height);
     100   
     101    /**
     102     * @brief return font height
     103     */
     104    R2int32 getFontHeight() const;
     105   
    109106};
    110107
    111108inline R2int32 R2Fonts::getFontHeight() const
    112109{
    113         return _fonts[_fontIndex]._fontHeight;
     110    return _fonts[_fontIndex]._fontHeight;
    114111}
    115112
  • trunk/vizservers/nanovis/R2/src/R2Fonts.cpp

    r929 r953  
    77const int c_nFileMagicHeader = 6666;
    88
    9 R2Fonts::R2Fonts() : _fontIndex(-1), _screenWidth(512), _screenHeight(512)
     9R2Fonts::R2Fonts() :
     10    _fontIndex(-1),
     11    _screenWidth(512),
     12    _screenHeight(512)
    1013{
    1114}
     
    2326{
    2427    for (unsigned index = 0; index < _fonts.size(); ++index) {
    25         if (_fonts[index]._fontName == fontName) {
    26             _fontIndex = index;
    27             break;
    28         }
     28        if (_fonts[index]._fontName == fontName) {
     29            _fontIndex = index;
     30            break;
     31        }
    2932    }
    3033}
     
    5255    if (_fontIndex != -1) {
    5356        int length = strlen(szVargsBuffer);
    54        
     57       
    5558        glListBase(_fonts[_fontIndex]._displayLists);
    5659        glCallLists(length, GL_UNSIGNED_BYTE,
    57                     reinterpret_cast<const GLvoid*>(szVargsBuffer));
    58     }
    59 }
    60 
    61 void R2Fonts::begin()
     60                    reinterpret_cast<const GLvoid*>(szVargsBuffer));
     61    }
     62}
     63
     64void
     65R2Fonts::begin()
    6266{
    6367    glEnable(GL_TEXTURE_2D);
     
    8084}
    8185
    82 void R2Fonts::end()
     86void
     87R2Fonts::end()
    8388{
    8489    glBindTexture(GL_TEXTURE_2D, 0);
     
    9398}
    9499
    95 void R2Fonts::initializeFont(R2FontAttributes& attr)
     100void
     101R2Fonts::initializeFont(R2FontAttributes& attr)
    96102{
    97103    attr._displayLists = glGenLists(256);
     
    99105    R2int32 index;
    100106    for (index = 0; index < 256; ++index) {
    101         R2FontAttributes::R2CharInfo& charInfo = attr._chars[index];
    102         glNewList(attr._displayLists + index, GL_COMPILE);
    103         if (charInfo._valid) {
    104             glBegin(GL_TRIANGLE_STRIP);
    105            
    106             glTexCoord2f(charInfo._left, charInfo._top );
    107             glVertex2i(0.0f, 0.0f);
    108            
    109             glTexCoord2f(charInfo._left, charInfo._bottom);
    110             glVertex2i(0.0f,  attr._fontHeight);
    111            
    112             glTexCoord2f(charInfo._right, charInfo._top);
    113             glVertex2i(charInfo._width, 0.0f);
    114            
    115             glTexCoord2f(charInfo._right,  charInfo._bottom);
    116             glVertex2i(charInfo._width, attr._fontHeight);
    117            
    118             glEnd( );
     107        R2FontAttributes::R2CharInfo& charInfo = attr._chars[index];
     108        glNewList(attr._displayLists + index, GL_COMPILE);
     109        if (charInfo._valid) {
     110            glBegin(GL_TRIANGLE_STRIP);
     111           
     112            glTexCoord2f(charInfo._left, charInfo._top );
     113            glVertex2i(0.0f, 0.0f);
     114           
     115            glTexCoord2f(charInfo._left, charInfo._bottom);
     116            glVertex2i(0.0f,  attr._fontHeight);
     117           
     118            glTexCoord2f(charInfo._right, charInfo._top);
     119            glVertex2i(charInfo._width, 0.0f);
     120           
     121            glTexCoord2f(charInfo._right,  charInfo._bottom);
     122            glVertex2i(charInfo._width, attr._fontHeight);
     123           
     124            glEnd( );
    119125            glTranslatef(charInfo._width, 0.0f, 0.0f);
    120         }
    121         glEndList();
     126        }
     127        glEndList();
    122128    }
    123129}
     
    125131R2bool
    126132R2Fonts::loadFont(const char* fontName, const char* fontFileName,
    127                   R2FontAttributes& sFont)
     133                  R2FontAttributes& sFont)
    128134{
    129135    R2bool bSuccess = false;
    130    
    131136    R2string path = R2FilePath::getInstance()->getPath(fontFileName);
    132137   
    133138    std::ifstream fsInput((const char*) path, std::ios::binary);
    134139    if (fsInput) {
    135         sFont._fontName = fontName;
    136        
     140        sFont._fontName = fontName;
     141       
    137142        // make sure this file is the correct type by checking the header
    138143        unsigned int uiFileId = 0;
     
    143148            uiTextureWidth = uiTextureHeight = uiFontHeight = 0;
    144149            fsInput.read(reinterpret_cast<char*>(&sFont._textureWidth),
    145                         sizeof(unsigned int));
     150                        sizeof(unsigned int));
    146151            fsInput.read(reinterpret_cast<char*>(&sFont._textureHeight),
    147                         sizeof(unsigned int));
     152                        sizeof(unsigned int));
    148153            fsInput.read(reinterpret_cast<char*>(&sFont._fontHeight),
    149                         sizeof(unsigned int));
     154                        sizeof(unsigned int));
    150155
    151156            // read dimensions for each charactor in 256-char ASCII chart
     
    155160                // top
    156161                fsInput.read(reinterpret_cast<char*>(&uiSize),
    157                              sizeof(unsigned int));
     162                             sizeof(unsigned int));
    158163                sFont._chars[i]._top = static_cast<float>(uiSize) / sFont._textureHeight;
    159164                // left
     
    198203            glBindTexture(GL_TEXTURE_2D, sFont._fontTextureID);
    199204            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
    200                         sFont._textureWidth, sFont._textureHeight, 0,
    201                         GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pTexMap);
     205                        sFont._textureWidth, sFont._textureHeight, 0,
     206                        GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pTexMap);
    202207            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    203208            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  • trunk/vizservers/nanovis/R2/src/R2string.cpp

    r835 r953  
    22#include <string.h>
    33
    4 R2string::R2string() : _string(NULL), _length(0)
     4R2string::R2string() :
     5    _string(NULL),
     6    _length(0)
    57{
    68}
     
    810R2string::R2string(const char* str) : _string(NULL), _length(0)
    911{
    10         if (str != NULL)
    11         {
    12                 set(str, (R2int32) strlen(str));
    13         }
     12    if (str != NULL) {
     13        set(str, (R2int32) strlen(str));
     14    }
    1415}
    1516
    1617R2string::R2string(const R2string& string) : _string(NULL)
    1718{
    18         set(string._string, string._length);
     19    set(string._string, string._length);
    1920}
    2021
     
    2627R2string& R2string::operator=(const R2string& string)
    2728{
    28         set(string._string, string._length);
     29    set(string._string, string._length);
    2930
    30         return *this;
     31    return *this;
    3132}
    3233
    3334R2string& R2string::operator=(const char* string)
    3435{
    35         set(string, strlen(string));
     36    set(string, strlen(string));
    3637
    37         return *this;
     38    return *this;
    3839}
    3940
    4041R2string operator+(const R2string& string1, const R2string& string2)
    4142{
    42         R2string ret;
    43         ret._length = string1._length + string2._length;
    44         ret._string = new R2char[ret._length + 1];
    45        
    46         strcpy(ret._string, string1._string);
    47         strcpy(ret._string + string1._length, string2._string);
    48        
    49         return ret;
     43    R2string ret;
     44    ret._length = string1._length + string2._length;
     45    ret._string = new R2char[ret._length + 1];
     46   
     47    strcpy(ret._string, string1._string);
     48    strcpy(ret._string + string1._length, string2._string);
     49   
     50    return ret;
    5051}
    5152
     
    5354R2string operator+(const R2string& string1, const char* string2)
    5455{
    55         R2string ret;
    56         ret._length = string1._length + strlen(string2);
    57         ret._string = new R2char[ret._length + 1];
    58        
    59         strcpy(ret._string, string1._string);
    60         strcpy(ret._string + string1._length, string2);
    61        
    62         return ret;
     56    R2string ret;
     57    ret._length = string1._length + strlen(string2);
     58    ret._string = new R2char[ret._length + 1];
     59   
     60    strcpy(ret._string, string1._string);
     61    strcpy(ret._string + string1._length, string2);
     62   
     63    return ret;
    6364}
  • trunk/vizservers/nanovis/RenderVertexArray.cpp

    r850 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    4142
    4243RenderVertexArray::RenderVertexArray(int nverts, GLint size, GLenum type) :
    43     m_nverts(nverts), m_size(size), m_usage(GL_STREAM_COPY), m_type(type)
     44    m_usage(GL_STREAM_COPY),
     45    m_nverts(nverts),
     46    m_size(size),
     47    m_type(type)
    4448{
    4549    switch(m_type) {
    46         case GL_HALF_FLOAT_NV:
    47             m_bytes_per_component = 2; break;
    48         case GL_FLOAT:
    49             m_bytes_per_component = sizeof(float); break;
    50         default:
    51             fprintf(stderr, "Error: unsupported RenderVertexArray type\n");
    52             return;
     50    case GL_HALF_FLOAT_NV:
     51        m_bytes_per_component = 2; break;
     52    case GL_FLOAT:
     53        m_bytes_per_component = sizeof(float); break;
     54    default:
     55        fprintf(stderr, "Error: unsupported RenderVertexArray type\n");
     56        return;
    5357    }
    5458
     
    5660    glGenBuffersARB(1, &m_buffer);
    5761    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, m_buffer);
    58     glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT, m_nverts*m_size*m_bytes_per_component, 0, m_usage); // undefined data
     62    glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT,
     63        m_nverts*m_size*m_bytes_per_component, 0, m_usage); // undefined data
    5964    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
    6065
    6166    // set equivalent image format
    6267    switch(m_size) {
    63         case 1:
    64             m_format = GL_LUMINANCE; break;
    65         case 3:
    66             m_format = GL_RGB; break;
    67         case 4:
    68             m_format = GL_RGBA; break;
    69         default:
    70             fprintf(stderr, "Error: unsupported RenderVertexArray size\n");
    71             return;
     68    case 1:
     69        m_format = GL_LUMINANCE; break;
     70    case 3:
     71        m_format = GL_RGB; break;
     72    case 4:
     73        m_format = GL_RGBA; break;
     74    default:
     75        fprintf(stderr, "Error: unsupported RenderVertexArray size\n");
     76        return;
    7277    }
    7378}
     
    8388    // load data to buffer object
    8489    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, m_buffer);
    85     glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT, m_nverts*m_size*m_bytes_per_component, data, m_usage);
     90    glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT,
     91                    m_nverts*m_size*m_bytes_per_component, data, m_usage);
    8692    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
    8793}
     
    104110    // bind buffer object to vertex array
    105111    glBindBufferARB(GL_ARRAY_BUFFER, m_buffer);
    106     //glVertexAttribPointerARB(index, m_size, m_type, GL_FALSE, 0, 0);          //doesn't work
     112    //glVertexAttribPointerARB(index, m_size, m_type, GL_FALSE, 0, 0);          //doesn't work
    107113    glVertexPointer(m_size, m_type, 0, 0);
    108114
  • trunk/vizservers/nanovis/RenderVertexArray.h

    r226 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    2021
    2122class RenderVertexArray {
    22 public:
    23   RenderVertexArray(int nverts, GLint size, GLenum type = GL_FLOAT);
    24   ~RenderVertexArray();
    25 
    26   void LoadData(void *data);                // load vertex data from memory
    27   void Read(/*GLenum buffer,*/ int w, int h);   // read vertex data from frame buffer
    28   void SetPointer(GLuint index);
    29 
    30 private:
    3123    GLenum m_usage;     // vbo usage flag
    3224    GLuint m_buffer;
     
    3729    GLenum m_type;      // FLOAT or HALF_FLOAT
    3830    int m_bytes_per_component;
     31
     32public:
     33    RenderVertexArray(int nverts, GLint size, GLenum type = GL_FLOAT);
     34    ~RenderVertexArray();
     35
     36    void LoadData(void *data);  // load vertex data from memory
     37    void Read(/*GLenum buffer,*/ int w, int h);   // read vertex data from
     38                                                  // frame buffer
     39    void SetPointer(GLuint index);
    3940};
    4041
  • trunk/vizservers/nanovis/ScreenSnapper.cpp

    r406 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
    34 * ScreenSnapper.h: ScreenSnapper class. It captures the render result
    4  *                      and stores it in an array of chars or floats
    5  *                      depending on chosen format.
     5 *                      and stores it in an array of chars or floats
     6 *                      depending on chosen format.
    67 *
    78 * ======================================================================
     
    2021#include "ScreenSnapper.h"
    2122
    22 ScreenSnapper::ScreenSnapper(int w, int h, NVISdatatype type, int channel_per_pixel){
    23   width = w;
    24   height = h;
     23ScreenSnapper::ScreenSnapper(int w, int h, NVISdatatype type,
     24                             int channel_per_pixel)
     25{
     26    width = w;
     27    height = h;
    2528
    26   assert(type == NVIS_FLOAT || type == NVIS_UNSIGNED_BYTE); //only allow two types
    27   data_type = type;
     29    //only allow two types
     30    assert(type == NVIS_FLOAT || type == NVIS_UNSIGNED_BYTE);
     31    data_type = type;
    2832 
    29   assert(channel_per_pixel == 3 || channel_per_pixel == 4); //only allow RGB or RGBA
    30   n_channels_per_pixel = channel_per_pixel;
     33    //only allow RGB or RGBA
     34    assert(channel_per_pixel == 3 || channel_per_pixel == 4);
     35    n_channels_per_pixel = channel_per_pixel;
    3136
    32   if(type == NVIS_FLOAT)
    33      data = new float[w*h*channel_per_pixel];
    34   else if(type == NVIS_UNSIGNED_BYTE)
    35      data = new unsigned char[w*h*channel_per_pixel];
     37    if(type == NVIS_FLOAT)
     38        data = new float[w*h*channel_per_pixel];
     39    else if(type == NVIS_UNSIGNED_BYTE)
     40        data = new unsigned char[w*h*channel_per_pixel];
    3641 
    37   assert(data!=0);
    38   reset(0);     //reset data
     42    assert(data!=0);
     43    reset(0);   //reset data
    3944}
    4045
    41 ScreenSnapper::~ScreenSnapper(){
    42   if(data_type == NVIS_FLOAT)
    43     delete[] (float*)data;
    44   else if(data_type == NVIS_UNSIGNED_BYTE)
    45     delete[] (unsigned char*)data;
     46ScreenSnapper::~ScreenSnapper()
     47{
     48    if(data_type == NVIS_FLOAT)
     49        delete[] (float*)data;
     50    else if(data_type == NVIS_UNSIGNED_BYTE)
     51        delete[] (unsigned char*)data;
    4652}
    4753
    48 void ScreenSnapper::reset(char c){
    49   int size;
    50   if(data_type == NVIS_FLOAT)
    51     size = sizeof(float)*width*height*n_channels_per_pixel; 
    52   else if(data_type == NVIS_UNSIGNED_BYTE)
    53     size = sizeof(unsigned char)*width*height*n_channels_per_pixel; 
     54void
     55ScreenSnapper::reset(char c)
     56{
     57    unsigned int elemSize;
     58    switch (data_type) {
     59    case NVIS_FLOAT:
     60        elemSize = sizeof(float);
     61        break;
    5462
    55   memset(data, size, c);
     63    case NVIS_UNSIGNED_BYTE:
     64        elemSize = sizeof(unsigned char);
     65        break;
     66
     67    default:
     68        assert(0);
     69        break;
     70    }
     71    unsigned int size;
     72    size = elemSize * width * height * n_channels_per_pixel;
     73    memset(data, size, c);
    5674}
    5775
    58 void ScreenSnapper::capture(){
    59 
    60   if(data_type == NVIS_FLOAT){
    61     if(n_channels_per_pixel==3)
    62       glReadPixels(0, 0, width, height, GL_RGB, GL_FLOAT, data);
    63     else if(n_channels_per_pixel==4)
    64       glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data);
    65   }
    66   else if(data_type == NVIS_UNSIGNED_BYTE){
    67     if(n_channels_per_pixel==3)
    68       glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    69     else if(n_channels_per_pixel==4)
    70       glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
    71   }
    72 
    73   assert(glGetError()==0);
     76void
     77ScreenSnapper::capture()
     78{
     79    if(data_type == NVIS_FLOAT){
     80        if(n_channels_per_pixel==3)
     81            glReadPixels(0, 0, width, height, GL_RGB, GL_FLOAT, data);
     82        else if(n_channels_per_pixel==4)
     83            glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data);
     84    }
     85    else if(data_type == NVIS_UNSIGNED_BYTE){
     86        if(n_channels_per_pixel==3)
     87            glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
     88        else if(n_channels_per_pixel==4)
     89            glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
     90    }
     91    assert(glGetError()==0);
    7492}
    7593
    76 void ScreenSnapper::print(){
    77  for(int i=0; i<width*height; i++){
    78   if(data_type == NVIS_FLOAT){
    79     if(n_channels_per_pixel==3)
    80       fprintf(stderr, "(%f %f %f) ",
    81                       ((float*)data)[3*i],
    82                       ((float*)data)[3*i+1],
    83                       ((float*)data)[3*i+2]);
    84     else if(n_channels_per_pixel==4)
    85       fprintf(stderr, "(%f %f %f %f) ",
    86                       ((float*)data)[4*i],
    87                       ((float*)data)[4*i+1],
    88                       ((float*)data)[4*i+2],
    89                       ((float*)data)[4*i+3]);
    90   }
    91   else if(data_type == NVIS_UNSIGNED_BYTE){
    92     if(n_channels_per_pixel==3)
    93       fprintf(stderr, "(%d %d %d) ",
    94                       ((unsigned char*)data)[3*i],
    95                       ((unsigned char*)data)[3*i+1],
    96                       ((unsigned char*)data)[3*i+2]);
    97     else if(n_channels_per_pixel==4)
    98       fprintf(stderr, "(%d %d %d %d) ",
    99                       ((unsigned char*)data)[4*i],
    100                       ((unsigned char*)data)[4*i+1],
    101                       ((unsigned char*)data)[4*i+2],
    102                       ((unsigned char*)data)[4*i+3]);
    103   }
    104  }
     94void
     95ScreenSnapper::print()
     96{
     97    for(int i=0; i<width*height; i++){
     98        if(data_type == NVIS_FLOAT){
     99            if(n_channels_per_pixel==3)
     100                fprintf(stderr, "(%f %f %f) ",
     101                        ((float*)data)[3*i],
     102                        ((float*)data)[3*i+1],
     103                        ((float*)data)[3*i+2]);
     104            else if(n_channels_per_pixel==4)
     105                fprintf(stderr, "(%f %f %f %f) ",
     106                        ((float*)data)[4*i],
     107                        ((float*)data)[4*i+1],
     108                        ((float*)data)[4*i+2],
     109                        ((float*)data)[4*i+3]);
     110        }
     111        else if(data_type == NVIS_UNSIGNED_BYTE){
     112            if(n_channels_per_pixel==3)
     113                fprintf(stderr, "(%d %d %d) ",
     114                        ((unsigned char*)data)[3*i],
     115                        ((unsigned char*)data)[3*i+1],
     116                        ((unsigned char*)data)[3*i+2]);
     117            else if(n_channels_per_pixel==4)
     118                fprintf(stderr, "(%d %d %d %d) ",
     119                        ((unsigned char*)data)[4*i],
     120                        ((unsigned char*)data)[4*i+1],
     121                        ((unsigned char*)data)[4*i+2],
     122                        ((unsigned char*)data)[4*i+3]);
     123        }
     124    }
    105125}
    106126
  • trunk/vizservers/nanovis/Sphere.cpp

    r406 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    1819
    1920Sphere::Sphere(float x, float y, float z,
    20                 float r, float g, float b,
    21                 float _radius,
    22                 int _stack,
    23                 int _slice):
    24         Renderable(Vector3(x, y, z)),
    25         radius(_radius),
    26         stack(_stack),
    27         slice(_slice),
    28         color(Color(r,g,b))
     21               float r, float g, float b,
     22               float _radius,
     23               int _stack,
     24               int _slice):
     25    Renderable(Vector3(x, y, z)),
     26    radius(_radius),
     27    color(Color(r,g,b)),
     28    stack(_stack),
     29    slice(_slice)
    2930{
    30   boundary = BoundBox(x-r, y-r, z-r, x+r, y+r, z+r);
     31    boundary = BoundBox(x-r, y-r, z-r, x+r, y+r, z+r);
    3132}
    3233
    33 Sphere::~Sphere(){}
    34 
    35 void Sphere::render(){}
    36 
    37 void Sphere::draw(GLUquadric* quad){
    38   glColor3f(color.R, color.G, color.B);
    39 
    40   glMatrixMode(GL_MODELVIEW);
    41   glPushMatrix();
    42   glTranslatef(location.x, location.y, location.z);
    43 
    44   //draw it
    45   gluSphere(quad, radius, stack, slice);
    46 
    47   glPopMatrix();
     34Sphere::~Sphere() {
     35    /*empty*/
    4836}
    4937
    50 void Sphere::set_horizontal_res(int _slice) {slice=_slice;}
    51 void Sphere::set_vertical_res(int _stack) {stack=_stack;}
     38void
     39Sphere::render()
     40{
     41    /*empty*/
     42}
    5243
     44void
     45Sphere::draw(GLUquadric* quad)
     46{
     47    glColor3f(color.R, color.G, color.B);
     48   
     49    glMatrixMode(GL_MODELVIEW);
     50    glPushMatrix();
     51    glTranslatef(location.x, location.y, location.z);
     52   
     53    //draw it
     54    gluSphere(quad, radius, stack, slice);
     55   
     56    glPopMatrix();
     57}
     58
     59void
     60Sphere::set_horizontal_res(int _slice) {
     61    slice=_slice;
     62}
     63
     64void
     65Sphere::set_vertical_res(int _stack) {
     66    stack=_stack;
     67}
     68
  • trunk/vizservers/nanovis/Sphere.h

    r401 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    2526
    2627public:
    27         float radius;
    28         Color color;
    29         int stack;
    30         int slice;
     28    float radius;
     29    Color color;
     30    int stack;
     31    int slice;
    3132
    32         ~Sphere();
    33         Sphere(float x, float y, float z, float r, float g, float b, float _radius, int _stack, int _slice);
    34         void set_vertical_res(int _stack);
    35         void set_horizontal_res(int _slice);
    36        
    37         //display the sphere
    38         void draw(GLUquadric* q);
    39         void render();
     33    ~Sphere();
     34    Sphere(float x, float y, float z, float r, float g, float b, float _radius,
     35           int _stack, int _slice);
     36    void set_vertical_res(int _stack);
     37    void set_horizontal_res(int _slice);
     38       
     39    //display the sphere
     40    void draw(GLUquadric* q);
     41    void render();
    4042};
    4143
  • trunk/vizservers/nanovis/Texture2D.cpp

    r825 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    2324Texture2D::Texture2D(){}
    2425
    25 Texture2D::Texture2D(int width, int height, GLuint type=GL_FLOAT, GLuint interp=GL_LINEAR, int n=4, float* data = 0)
     26Texture2D::Texture2D(int width, int height, GLuint type=GL_FLOAT,
     27        GLuint interp=GL_LINEAR, int n=4, float* data = 0)
    2628{
    27         assert(type == GL_UNSIGNED_BYTE || type == GL_FLOAT|| type ==GL_UNSIGNED_INT);
    28         assert(interp == GL_LINEAR || interp == GL_NEAREST);
    29        
    30         this->width = width;
    31         this->height = height;
    32         this->type = type;
    33         this->interp_type = interp;
    34         this->n_components = n;
     29    assert(type == GL_UNSIGNED_BYTE ||
     30           type == GL_FLOAT ||
     31           type ==GL_UNSIGNED_INT);
     32    assert(interp == GL_LINEAR || interp == GL_NEAREST);
     33       
     34    this->width = width;
     35    this->height = height;
     36    this->type = type;
     37    this->interp_type = interp;
     38    this->n_components = n;
    3539
    36         this->id = 0;
     40    this->id = 0;
    3741
    38         if(data != 0)
    39           initialize(data);
     42    if(data != 0)
     43        initialize(data);
    4044}
    4145
    42 GLuint Texture2D::initialize(float *data)
     46GLuint
     47Texture2D::initialize(float *data)
    4348{
    44         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     49    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    4550
    46         glGenTextures(1, &id);
    47         glBindTexture(GL_TEXTURE_2D, id);
    48         assert(id!=-1);
     51    glGenTextures(1, &id);
     52    glBindTexture(GL_TEXTURE_2D, id);
     53    assert(id!=-1);
    4954
    50         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    51         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    52        
    53         if(interp_type == GL_LINEAR){
    54           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    55           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    56         }
    57         else{
    58           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    59           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    60         }
    61 
    62         //to do: add handling to more formats
    63         if(type==GL_FLOAT){
    64           switch(n_components){
    65             #ifdef NV40
    66                 case 1:
    67                         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height,  0, GL_LUMINANCE, GL_FLOAT, data);
    68                         break;
    69                 case 2:
    70                         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    71                         break;
    72                 case 3:
    73                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, width, height, 0, GL_RGB, GL_FLOAT, data);
    74                         break;
    75                 case 4:
    76                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, data);
    77                         break;
    78             #else
    79                 case 1:
    80                         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_FLOAT, data);
    81                         break;
    82                 case 2:
    83                         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    84                         break;
    85                 case 3:
    86                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, data);
    87                         break;
    88                 case 4:
    89                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, data);
    90                         break;
    91             #endif
    92                 default:
    93                         break;
    94           }
    95         }
    96     else
    97     {
    98         int comp[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
    99                 glTexImage2D(GL_TEXTURE_2D, 0, comp[n_components], width, height, 0, GL_RGBA, type, data);
     55    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     56    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     57       
     58    if(interp_type == GL_LINEAR){
     59        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     60        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     61    } else {
     62        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     63        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    10064    }
    10165
    102         assert(glGetError()==0);
    103         return id;
     66    //to do: add handling to more formats
     67    if (type==GL_FLOAT) {
     68        switch(n_components){
     69#ifdef NV40
     70        case 1:
     71            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height,
     72                         0, GL_LUMINANCE, GL_FLOAT, data);
     73            break;
     74        case 2:
     75            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA16F_ARB, width,
     76                         height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
     77            break;
     78        case 3:
     79            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, width, height, 0,
     80                         GL_RGB, GL_FLOAT, data);
     81            break;
     82        case 4:
     83            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0,
     84                         GL_RGBA, GL_FLOAT, data);
     85            break;
     86#else
     87        case 1:
     88            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
     89                         GL_LUMINANCE, GL_FLOAT, data);
     90            break;
     91        case 2:
     92            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height,
     93                         0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
     94            break;
     95        case 3:
     96            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
     97                         GL_FLOAT, data);
     98            break;
     99        case 4:
     100            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
     101                         GL_FLOAT, data);
     102            break;
     103#endif
     104        default:
     105            break;
     106        }
     107    } else {
     108        int comp[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
     109        glTexImage2D(GL_TEXTURE_2D, 0, comp[n_components], width, height, 0,
     110                     GL_RGBA, type, data);
     111    }
     112    assert(glGetError()==0);
     113    return id;
    104114}
    105115
    106116
    107 void Texture2D::activate()
     117void
     118Texture2D::activate()
    108119{
    109   glBindTexture(GL_TEXTURE_2D, id);
    110   glEnable(GL_TEXTURE_2D);
     120    glBindTexture(GL_TEXTURE_2D, id);
     121    glEnable(GL_TEXTURE_2D);
    111122}
    112123
    113124
    114 void Texture2D::deactivate()
     125void
     126Texture2D::deactivate()
    115127{
    116   glDisable(GL_TEXTURE_2D);             
     128    glDisable(GL_TEXTURE_2D);           
    117129}
    118130
     
    120132Texture2D::~Texture2D()
    121133{
    122   glDeleteTextures(1, &id);
     134    glDeleteTextures(1, &id);
    123135}
    124136
    125137
    126 void Texture2D::check_max_size(){
    127   GLint max = 0;
    128   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
    129        
    130   fprintf(stderr, "max texture size: %d\n", max);
     138void
     139Texture2D::check_max_size(){
     140    GLint max = 0;
     141    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
     142       
     143    fprintf(stderr, "max texture size: %d\n", max);
    131144}
    132145
    133 void Texture2D::check_max_unit(){
    134   int max;
    135   glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
     146void
     147Texture2D::check_max_unit(){
     148    int max;
     149    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
    136150
    137   fprintf(stderr, "max texture units: %d.\n", max);
     151    fprintf(stderr, "max texture units: %d.\n", max);
    138152}
    139153
  • trunk/vizservers/nanovis/transfer-function/ColorGradientGLUTWindow.cpp

    r383 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    3334int cm_editState;
    3435
    35 namespace ColorGradientWindow
    36 {
    37         int cm_numOfPoints;
    38 
    39         bool cm_gvIsDragging=false;
    40         bool cm_gvScaleDragging=false;
    41 
    42         double distanceThreshold;
    43         double gv_X, gv_Y;
    44         double gv_DX, gv_DY;
    45         double gv_lastX, gv_lastY;
     36namespace ColorGradientWindow {
     37    int cm_numOfPoints;
     38
     39    bool cm_gvIsDragging=false;
     40    bool cm_gvScaleDragging=false;
     41
     42    double distanceThreshold;
     43    double gv_X, gv_Y;
     44    double gv_DX, gv_DY;
     45    double gv_lastX, gv_lastY;
    4646};
    4747
    4848using namespace ColorGradientWindow;
    4949
    50 
    51 
    5250//extern UnstructuredVolumeRenderer *uvol;
    5351
    5452ColorGradientGLUTWindow::ColorGradientGLUTWindow()
    5553{
    56         map = new ColorGradient();
    57 }
    58 
    59 
    60 ColorGradientGLUTWindow::~ColorGradientGLUTWindow(){ }
    61 
    62 
    63 void ColorGradientGLUTWindow::cmInit(int main_win_x, int main_win_y)
    64 {
    65         mapOutput=(float*)malloc(3*sizeof(float)*mapNumOfOutput);
    66         cm_winx=main_win_x;
    67         //cm_winy=main_win_y/3;
    68         cm_winy=100;
    69         cm_unitWidth=tf_winx*0.95;
    70         map=new ColorGradient();
    71         cm_editState=0;
    72         cm_gvIsDragging=false;
    73         cm_gvScaleDragging=false;
    74         cm_gvSelectedPoint=0;
    75         cm_numOfPoints=2;
    76         gv_X=0;
    77         gv_Y=0;
    78         gv_DX=0;
    79         gv_DY=0;
    80         gv_lastX=0;
    81         gv_lastY=0;
    82         distanceThreshold=64;
    83 
    84         if(mapOutput==0)
    85                 fprintf(stderr, "NULL");
    86 
    87         //initialize mapOutput
    88         int i=0;
    89         for (i=0; i<mapNumOfOutput; i++){
    90                 color_table[i][0]=0;
    91         }
    92 }
    93 
    94 
    95 
    96 void ColorGradientGLUTWindow::createGLUIWidgets(){
    97         cm_glui = GLUI_Master.create_glui_subwindow(colorMapWindow, GLUI_SUBWINDOW_BOTTOM);
    98         pan=cm_glui->add_panel("buttons", GLUI_PANEL_NONE);
    99         cm_glui->set_main_gfx_window(colorMapWindow);
    100         cm_glui->add_column_to_panel(pan, false );
    101         cm_glui->add_statictext_to_panel(pan,"Color Controls:");
    102         cm_glui->add_column_to_panel(pan,false );
    103         cm_glui->add_button_to_panel(pan,"Add", 1, changeState);
    104         cm_glui->add_column_to_panel(pan, false );
    105         cm_glui->add_button_to_panel(pan, "Edit", 2, changeState);
    106         cm_glui->add_column_to_panel(pan, false );
    107         cm_glui->add_button_to_panel(pan, "Remove", 3, changeState);
     54    map = new ColorGradient();
     55}
     56
     57
     58ColorGradientGLUTWindow::~ColorGradientGLUTWindow()
     59{
     60}
     61
     62void
     63ColorGradientGLUTWindow::cmInit(int main_win_x, int main_win_y)
     64{
     65    mapOutput=(float*)malloc(3*sizeof(float)*mapNumOfOutput);
     66    cm_winx=main_win_x;
     67    //cm_winy=main_win_y/3;
     68    cm_winy=100;
     69    cm_unitWidth=tf_winx*0.95;
     70    map=new ColorGradient();
     71    cm_editState=0;
     72    cm_gvIsDragging=false;
     73    cm_gvScaleDragging=false;
     74    cm_gvSelectedPoint=0;
     75    cm_numOfPoints=2;
     76    gv_X=0;
     77    gv_Y=0;
     78    gv_DX=0;
     79    gv_DY=0;
     80    gv_lastX=0;
     81    gv_lastY=0;
     82    distanceThreshold=64;
     83
     84    if(mapOutput==0)
     85        fprintf(stderr, "NULL");
     86
     87    //initialize mapOutput
     88    int i=0;
     89    for (i=0; i<mapNumOfOutput; i++){
     90        color_table[i][0]=0;
     91    }
     92}
     93
     94
     95
     96void
     97ColorGradientGLUTWindow::createGLUIWidgets()
     98{
     99    cm_glui = GLUI_Master.create_glui_subwindow(colorMapWindow,
     100                                                GLUI_SUBWINDOW_BOTTOM);
     101    pan=cm_glui->add_panel("buttons", GLUI_PANEL_NONE);
     102    cm_glui->set_main_gfx_window(colorMapWindow);
     103    cm_glui->add_column_to_panel(pan, false );
     104    cm_glui->add_statictext_to_panel(pan,"Color Controls:");
     105    cm_glui->add_column_to_panel(pan,false );
     106    cm_glui->add_button_to_panel(pan,"Add", 1, changeState);
     107    cm_glui->add_column_to_panel(pan, false );
     108    cm_glui->add_button_to_panel(pan, "Edit", 2, changeState);
     109    cm_glui->add_column_to_panel(pan, false );
     110    cm_glui->add_button_to_panel(pan, "Remove", 3, changeState);
    108111}
    109112
     
    111114void update_tf_texture();
    112115
    113 void ColorGradientGLUTWindow::changeState(int arg){
    114         switch (arg){
    115         case 1: //add color
    116                 cm_editState=1;
    117                 break;
    118         case 2:
    119                 //cm_editState=2;
    120                 ColorPalette::cpInit();
    121                 update_tf_texture();
    122                 break;
    123         case 3:
    124                 cm_editState=3;
    125                 if (cm_gvSelectedPoint!=0)
    126                         map->removeKey(cm_gvSelectedPoint->x);
    127                 cm_editState=0;
    128                 update_tf_texture();
    129                 break;
    130         case 4:
    131                 cm_editState=4;
    132                 break;
    133 
    134         default:
    135                 break;
    136         }
    137 }
    138 
    139 
    140 
    141 void ColorGradientGLUTWindow::cmDisplay()
    142 {
    143         glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
    144         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    145 
    146 
    147         int i;
    148 
    149         if(map == NULL){
    150                 fprintf(stderr, "ColorGradient Window : forgot to bind the colorMap\n");
    151                 return;
    152         }
    153 
    154 
    155         Color* leftColor=map->keyColors;
    156         Color* rightColor;
    157         ControlPoint* leftPoint=map->keyList;
    158         ControlPoint* rightPoint;
    159         double left_x, right_x;
    160         GLfloat left_color[3];
    161         GLfloat right_color[3];
    162 
    163 
    164         //draw "rainbow"
    165         glShadeModel(GL_SMOOTH);
    166         glDisable(GL_DEPTH_TEST);
    167        
    168         glBegin(GL_QUADS);
    169         for(i=0; i<map->numOfKeys-1; i++){
    170                         left_x=leftPoint->x;
    171                         leftColor->GetRGB(left_color);
    172 
    173                         rightColor=leftColor->next;
    174                         rightPoint=leftPoint->next;
    175                         right_x=rightPoint->x;
    176                         rightColor->GetRGB(right_color);
    177 
    178                                 //printf("right=(%g, %g, %g)\n", right_color[0], right_color[1], right_color[2]);
    179                                 glColor3f(left_color[0], left_color[1], left_color[2]);
    180                                 glVertex2d(left_x, 0);
    181                                 glVertex2d(left_x, cm_winy-10);
    182 
    183                                 //printf("left=(%g, %g, %g)\n", left_color[0], left_color[1], left_color[2]);
    184                                 glColor3f(right_color[0], right_color[1], right_color[2]);     
    185                                 glVertex2d(right_x, cm_winy-10);
    186                                 glVertex2d(right_x, 0);
    187                        
    188                         leftColor=leftColor->next;
    189                         leftPoint=leftPoint->next;
    190 
    191         }
    192         glEnd();
    193         glColor3f(0, 0, 0);
    194 
    195 
    196         //draw bar
    197         glLineWidth(1.0);
    198         glBegin(GL_LINES);
    199                 glVertex2d(15, cm_winy-1);
    200                 glVertex2d(15+tf_unitWidth, cm_winy-1);
    201         glEnd();
    202 
    203 
    204         i=0;
    205         Color* curColor=map->keyColors;
    206         ControlPoint* curKey=map->keyList;
    207         for (i=0; i<map->numOfKeys ; i++){
    208                 //glColor3d(curColor->R, curColor->G, curColor->B);
    209                 glColor3d(0.0, 0.0, 0.0);
    210                 curKey->glDraw_2();
    211                 curColor=curColor->next;
    212                 curKey=curKey->next;
    213         }
    214 
    215 
    216         glColor3d(0.0, 0.0, 0.0);
    217         glutSwapBuffers();
    218 }
    219 
    220 
    221 
    222 
    223 void ColorGradientGLUTWindow::cmReshape(int x, int y)
    224 {
    225         //printf("cmReshape");
    226         cm_winx=x;
    227         cm_winy=100;
    228        
    229 
    230         double scale=((double)tf_winx*0.95)/((double)cm_unitWidth);
    231         cm_unitWidth=tf_winx*0.95;
    232         map->scaleKeys(scale, cm_unitWidth);
    233 
    234         glViewport( 0, 0, x, y );
    235 
    236         glMatrixMode( GL_PROJECTION );
    237         glLoadIdentity();
    238 
    239         glMatrixMode(GL_MODELVIEW);
    240         glLoadIdentity();
    241 
    242         //Set up most convenient projection
    243         gluOrtho2D(0, x, 0, y);
    244         glutPostRedisplay();
    245 }
    246 
    247 
    248 
    249 
    250 void ColorGradientGLUTWindow::cmDestroy(){}
    251 
    252 
    253 
    254 void ColorGradientGLUTWindow::cmKeyboard(unsigned char key, int x, int y){
    255         switch(key)
    256         {   
    257         case 'p':
    258                 //printPoints();
    259                 break;
    260         case 'c':
    261                 printInterpolation();
    262                 break;
    263         default:
    264                 break;
    265     }
    266 }
    267 
    268 
    269 
    270 void ColorGradientGLUTWindow::cmIdle(){ }
    271 
    272 
    273 
    274 void ColorGradientGLUTWindow::cmMouse(int button, int state, int x, int y)
    275 {       
    276         //printf("cm mouse\n");
    277 
     116void
     117ColorGradientGLUTWindow::changeState(int arg)
     118{
     119    switch (arg){
     120    case 1: //add color
     121        cm_editState=1;
     122        break;
     123    case 2:
     124        //cm_editState=2;
     125        ColorPalette::cpInit();
     126        update_tf_texture();
     127        break;
     128    case 3:
     129        cm_editState=3;
     130        if (cm_gvSelectedPoint!=0)
     131            map->removeKey(cm_gvSelectedPoint->x);
     132        cm_editState=0;
     133        update_tf_texture();
     134        break;
     135    case 4:
     136        cm_editState=4;
     137        break;
     138
     139    default:
     140        break;
     141    }
     142}
     143
     144void
     145ColorGradientGLUTWindow::cmDisplay()
     146{
     147    glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
     148    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     149
     150
     151    int i;
     152
     153    if(map == NULL){
     154        fprintf(stderr, "ColorGradient Window : forgot to bind the colorMap\n");
     155        return;
     156    }
     157
     158
     159    Color* leftColor=map->keyColors;
     160    Color* rightColor;
     161    ControlPoint* leftPoint=map->keyList;
     162    ControlPoint* rightPoint;
     163    double left_x, right_x;
     164    GLfloat left_color[3];
     165    GLfloat right_color[3];
     166
     167
     168    //draw "rainbow"
     169    glShadeModel(GL_SMOOTH);
     170    glDisable(GL_DEPTH_TEST);
     171       
     172    glBegin(GL_QUADS);
     173    for(i=0; i<map->numOfKeys-1; i++){
     174        left_x=leftPoint->x;
     175        leftColor->GetRGB(left_color);
     176
     177        rightColor=leftColor->next;
     178        rightPoint=leftPoint->next;
     179        right_x=rightPoint->x;
     180        rightColor->GetRGB(right_color);
     181
     182        //printf("right=(%g, %g, %g)\n", right_color[0], right_color[1], right_color[2]);
     183        glColor3f(left_color[0], left_color[1], left_color[2]);
     184        glVertex2d(left_x, 0);
     185        glVertex2d(left_x, cm_winy-10);
     186
     187        //printf("left=(%g, %g, %g)\n", left_color[0], left_color[1], left_color[2]);
     188        glColor3f(right_color[0], right_color[1], right_color[2]);     
     189        glVertex2d(right_x, cm_winy-10);
     190        glVertex2d(right_x, 0);
     191                       
     192        leftColor=leftColor->next;
     193        leftPoint=leftPoint->next;
     194
     195    }
     196    glEnd();
     197    glColor3f(0, 0, 0);
     198
     199
     200    //draw bar
     201    glLineWidth(1.0);
     202    glBegin(GL_LINES);
     203    glVertex2d(15, cm_winy-1);
     204    glVertex2d(15+tf_unitWidth, cm_winy-1);
     205    glEnd();
     206
     207
     208    i=0;
     209    Color* curColor=map->keyColors;
     210    ControlPoint* curKey=map->keyList;
     211    for (i=0; i<map->numOfKeys ; i++){
     212        //glColor3d(curColor->R, curColor->G, curColor->B);
     213        glColor3d(0.0, 0.0, 0.0);
     214        curKey->glDraw_2();
     215        curColor=curColor->next;
     216        curKey=curKey->next;
     217    }
     218
     219
     220    glColor3d(0.0, 0.0, 0.0);
     221    glutSwapBuffers();
     222}
     223
     224
     225
     226
     227void
     228ColorGradientGLUTWindow::cmReshape(int x, int y)
     229{
     230    //printf("cmReshape");
     231    cm_winx=x;
     232    cm_winy=100;
     233       
     234
     235    double scale=((double)tf_winx*0.95)/((double)cm_unitWidth);
     236    cm_unitWidth=tf_winx*0.95;
     237    map->scaleKeys(scale, cm_unitWidth);
     238
     239    glViewport( 0, 0, x, y );
     240
     241    glMatrixMode( GL_PROJECTION );
     242    glLoadIdentity();
     243
     244    glMatrixMode(GL_MODELVIEW);
     245    glLoadIdentity();
     246
     247    //Set up most convenient projection
     248    gluOrtho2D(0, x, 0, y);
     249    glutPostRedisplay();
     250}
     251
     252
     253
     254
     255void
     256ColorGradientGLUTWindow::cmDestroy()
     257{
     258}
     259
     260void
     261ColorGradientGLUTWindow::cmKeyboard(unsigned char key, int x, int y)
     262{
     263    switch(key) {   
     264    case 'p':
     265        //printPoints();
     266        break;
     267    case 'c':
     268        printInterpolation();
     269        break;
     270    default:
     271        break;
     272    }
     273}
     274
     275void
     276ColorGradientGLUTWindow::cmIdle()
     277{
     278}
     279
     280void
     281ColorGradientGLUTWindow::cmMouse(int button, int state, int x, int y)
     282{       
     283    //printf("cm mouse\n");
     284
     285    GLint viewportVector[4];
     286    double scX, scY;
     287
     288    glGetIntegerv(GL_VIEWPORT, viewportVector);
     289       
     290    gv_X = scX = x;
     291    gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     292
     293    gv_lastY = scY;
     294
     295    //Selecting point, begin dragging
     296    bool gvIsSelected = SelectPoint(scX, scY);
     297
     298    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
     299               
     300        if (gvIsSelected && cm_editState==0){
     301            //assert(cm_gvSelectedPoint!=NULL);
     302            gv_DX = scX - cm_gvSelectedPoint->x; //Offset from current point to selected point, for dragging corrections
     303       
     304            cm_gvSelectedPoint->dragged = true;
     305            cm_gvIsDragging = true;
     306        }
     307        /*else if ((!gvIsSelected) && cm_editState==1){
     308          GLint viewportVector[4];
     309          double scX, scY;
     310
     311          glGetIntegerv(GL_VIEWPORT, viewportVector);
     312
     313          gv_X = scX = x;
     314          gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     315          cm_gvSelectedPoint=map->addKey(scX - gv_DX, new Color(1,1,1));
     316          cm_editState=0;
     317          WriteControlPoints();
     318          }*/
     319    } else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
     320        //"     End dragging" code.
     321        cm_gvIsDragging = false;
     322        if (cm_gvSelectedPoint)
     323            cm_gvSelectedPoint->dragged = false;
     324    }
     325    if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
    278326        GLint viewportVector[4];
    279327        double scX, scY;
    280 
     328       
    281329        glGetIntegerv(GL_VIEWPORT, viewportVector);
    282330       
    283331        gv_X = scX = x;
    284332        gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    285 
    286         gv_lastY = scY;
    287 
    288         //Selecting point, begin dragging
    289         bool gvIsSelected = SelectPoint(scX, scY);
    290 
    291         if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    292                
    293                 if (gvIsSelected && cm_editState==0){
    294                         //assert(cm_gvSelectedPoint!=NULL);
    295                         gv_DX = scX - cm_gvSelectedPoint->x; //Offset from current point to selected point, for dragging corrections
    296        
    297                         cm_gvSelectedPoint->dragged = true;
    298                         cm_gvIsDragging = true;
    299                 }
    300                 /*else if ((!gvIsSelected) && cm_editState==1){
    301                         GLint viewportVector[4];
    302                         double scX, scY;
    303 
    304                         glGetIntegerv(GL_VIEWPORT, viewportVector);
    305 
    306                         gv_X = scX = x;
    307                         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    308                         cm_gvSelectedPoint=map->addKey(scX - gv_DX, new Color(1,1,1));
    309                         cm_editState=0;
    310                         WriteControlPoints();
    311                 }*/
    312         }
    313 
    314 
    315 
    316 
    317         else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){
    318                 //"     End dragging" code.
    319                 cm_gvIsDragging = false;
    320                 if (cm_gvSelectedPoint)
    321                         cm_gvSelectedPoint->dragged = false;
    322         }
    323        
    324         if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    325              {
    326                         GLint viewportVector[4];
    327                         double scX, scY;
    328 
    329                         glGetIntegerv(GL_VIEWPORT, viewportVector);
    330 
    331                         gv_X = scX = x;
    332                         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    333                         cm_gvSelectedPoint=map->addKey(scX - gv_DX, new Color(1,1,1));
    334                         cm_editState=0;
    335                         WriteControlPoints();
    336                 }
    337 
    338 
    339         if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    340                 cm_gvScaleDragging = true;
    341 
    342         if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
    343                 cm_gvScaleDragging = false;
    344 
    345         ///////////////////////////
    346         glutSetWindow(colorMapWindow);
    347         ////////////////////////
    348         glutPostRedisplay();
    349 }
    350 
    351 
    352 
    353 void ColorGradientGLUTWindow::cmMotion(int x, int y)
    354 {
    355         GLint viewportVector[4];
    356         double scX, scY;
    357 
    358         glGetIntegerv(GL_VIEWPORT, viewportVector);
    359 
    360         gv_X = scX = x;
    361         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    362 
    363         if (cm_gvIsDragging == true && cm_gvSelectedPoint!=NULL){
    364                 //Dragging handling code.
    365                 cm_gvSelectedPoint->dragged = true;
    366 
    367 
    368                 ControlPoint* cur=map->keyList;
    369                 while (cur->next!=0){
    370                         cur=cur->next;
    371                 }
    372 
    373                 //update x, y values for selected point
    374                 if (cm_gvSelectedPoint!=map->keyList && cm_gvSelectedPoint!=cur){
    375 
    376                         cm_gvSelectedPoint->x = scX - gv_DX;
    377                         //not the first one, not the last one
    378                         cm_gvSelectedPoint->x = scX - gv_DX;
    379                         if (cm_gvSelectedPoint->x<=15){
    380                                 cm_gvSelectedPoint->x=16;
    381                         }
    382                         else if(cm_gvSelectedPoint->x>=15+cm_unitWidth){
    383                                 cm_gvSelectedPoint->x=15+cm_unitWidth-1;
    384                         }
    385                        
    386                         boundaryChecking();
    387                         WriteControlPoints();
    388                 }
    389         }
    390 
    391         glutPostRedisplay();
    392 }
    393 
    394 
    395 
    396 
    397 bool ColorGradientGLUTWindow::SelectPoint(double x, double y){
    398 
    399         //Reset selected/dragged flags
    400         ControlPoint* cur=map->keyList;
    401         while(cur!=0){
    402                 cur->dragged=false;
    403                 cur->selected=false;
    404                 cur=cur->next;
    405         }
    406 
    407         cm_gvSelectedPoint = NULL;
    408 
    409         //Traverse the points and find the one that seems close
    410         cur=map->keyList;
    411         while(cur!=0){
    412                 double distanceSq;
    413                 distanceSq = (cur->x - x)*(cur->x - x) + (cur->y - y)*(cur->y - y);     
    414                 if (distanceSq < distanceThreshold){
    415                         cur->selected = true;
    416                         cm_gvSelectedPoint = cur;
    417                         return true;
    418                 }
    419                 cur=cur->next;
    420         }
    421 
    422         return false;
     333        cm_gvSelectedPoint=map->addKey(scX - gv_DX, new Color(1,1,1));
     334        cm_editState=0;
     335        WriteControlPoints();
     336    }
     337   
     338
     339    if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
     340        cm_gvScaleDragging = true;
     341   
     342    if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
     343        cm_gvScaleDragging = false;
     344
     345    ///////////////////////////
     346    glutSetWindow(colorMapWindow);
     347    ////////////////////////
     348    glutPostRedisplay();
     349}
     350
     351
     352
     353void
     354ColorGradientGLUTWindow::cmMotion(int x, int y)
     355{
     356    GLint viewportVector[4];
     357    double scX, scY;
     358
     359    glGetIntegerv(GL_VIEWPORT, viewportVector);
     360
     361    gv_X = scX = x;
     362    gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     363
     364    if (cm_gvIsDragging == true && cm_gvSelectedPoint!=NULL){
     365        //Dragging handling code.
     366        cm_gvSelectedPoint->dragged = true;
     367
     368
     369        ControlPoint* cur=map->keyList;
     370        while (cur->next!=0){
     371            cur=cur->next;
     372        }
     373
     374        //update x, y values for selected point
     375        if (cm_gvSelectedPoint!=map->keyList && cm_gvSelectedPoint!=cur){
     376
     377            cm_gvSelectedPoint->x = scX - gv_DX;
     378            //not the first one, not the last one
     379            cm_gvSelectedPoint->x = scX - gv_DX;
     380            if (cm_gvSelectedPoint->x<=15){
     381                cm_gvSelectedPoint->x=16;
     382            }
     383            else if(cm_gvSelectedPoint->x>=15+cm_unitWidth){
     384                cm_gvSelectedPoint->x=15+cm_unitWidth-1;
     385            }
     386                       
     387            boundaryChecking();
     388            WriteControlPoints();
     389        }
     390    }
     391
     392    glutPostRedisplay();
     393}
     394
     395bool
     396ColorGradientGLUTWindow::SelectPoint(double x, double y)
     397{
     398    //Reset selected/dragged flags
     399    ControlPoint* cur=map->keyList;
     400    while(cur!=0){
     401        cur->dragged=false;
     402        cur->selected=false;
     403        cur=cur->next;
     404    }
     405
     406    cm_gvSelectedPoint = NULL;
     407
     408    //Traverse the points and find the one that seems close
     409    cur=map->keyList;
     410    while(cur!=0){
     411        double distanceSq;
     412        distanceSq = (cur->x - x)*(cur->x - x) + (cur->y - y)*(cur->y - y);     
     413        if (distanceSq < distanceThreshold){
     414            cur->selected = true;
     415            cm_gvSelectedPoint = cur;
     416            return true;
     417        }
     418        cur=cur->next;
     419    }
     420
     421    return false;
    423422}
    424423
     
    426425void update_tf_texture();
    427426
    428 void ColorGradientGLUTWindow::WriteControlPoints()
    429 {
    430 
    431         //special case: all points have the same color
    432         Color* curColor=map->keyColors;
    433         int allSameY=1;
    434         int R=map->keyColors->R;
    435         int G=map->keyColors->G;
    436         int B=map->keyColors->B;
    437         while (curColor!=0){
    438                 if (curColor->R!=R || curColor->G!=G || curColor->B!=B){
    439                         allSameY=0;
    440                         break;
    441                 }
    442                 curColor=curColor->next;
    443         }
    444 
    445         if (allSameY==1){
    446                 int i=0;
    447                
    448                 for(i=0;i<mapNumOfOutput;i++){
    449                         color_table[i][0]=R;
    450                         color_table[i][1]=G;
    451                         color_table[i][2]=B;
    452                 }
    453                 return;
    454         }
    455 
    456         float unit=(float)cm_unitWidth/(float)mapNumOfOutput;
    457 
    458         curColor=map->keyColors;
    459         Color* nextColor=curColor->next;
    460 
    461         double r1=curColor->R*255;
    462         double g1=curColor->G*255;
    463         double b1=curColor->B*255;
    464 
    465         double r2=nextColor->R*255;
    466         double g2=nextColor->G*255;
    467         double b2=nextColor->B*255;
    468 
    469         ControlPoint* curPoint=map->keyList;
    470         ControlPoint* nextPoint=curPoint->next;
    471 
    472         double x1=curPoint->x;
    473         double x2=nextPoint->x;
    474 
    475         float t=0;
    476 
    477         int i=0;
    478         for(i=0;i<numOfOutput;i++){
    479                
    480                 if (unit*i+15>x2){
    481                         //go to next line segment
    482                         curColor=nextColor;
    483                         nextColor=nextColor->next;
    484 
    485                         curPoint=nextPoint;
    486                         nextPoint=nextPoint->next;
    487 
    488                         if (nextColor==0 || nextPoint==0){
    489                                 return;
    490                         }
    491 
    492                         x1=curPoint->x;
    493                         x2=nextPoint->x;
    494 
    495                         r1=curColor->R*255;
    496                         g1=curColor->G*255;
    497                         b1=curColor->B*255;
    498 
    499                         r2=nextColor->R*255;
    500                         g2=nextColor->G*255;
    501                         b2=nextColor->B*255;
    502 
    503                 }
    504                 t=((float)(unit*i+15-x1))/((float)(x2-x1));
    505 
    506                 color_table[i][0]=(float)(r1+t*(r2-r1))/(float)255;
    507                 color_table[i][1]=(float)(g1+t*(g2-g1))/(float)255;
    508                 color_table[i][2]=(float)(b1+t*(b2-b1))/(float)255;
    509         }       
    510 
    511         update_tf_texture();
    512         glutSetWindow(colorMapWindow);
    513 }
    514 
    515 
    516 ControlPoint* ColorGradientGLUTWindow::boundaryChecking(){
    517         //find left and right neighbor in the list
    518         //If point is dragged passing the neighbor, the points are reordered.
    519 
    520         ControlPoint* left=map->keyList;
    521         ControlPoint* right=map->keyList;
    522 
    523         while (right!=cm_gvSelectedPoint){
    524                 left=right;
    525                 right=right->next;
    526         }
    527 
    528         //selected point is not the right-end point
    529         if (right->next!=0){
    530                 right=right->next;
    531                 if (cm_gvSelectedPoint->x>right->x){
    532                         sortPoints();
    533                 }
    534         }
    535 
    536         //selected point is not the left-end point
    537         if (left!=cm_gvSelectedPoint) {
    538                 if (cm_gvSelectedPoint->x<left->x){
    539                         sortPoints();
    540                 }
    541         }
    542 
    543         return cm_gvSelectedPoint;
     427void
     428ColorGradientGLUTWindow::WriteControlPoints()
     429{
     430
     431    //special case: all points have the same color
     432    Color* curColor=map->keyColors;
     433    int allSameY=1;
     434    int R=map->keyColors->R;
     435    int G=map->keyColors->G;
     436    int B=map->keyColors->B;
     437    while (curColor!=0){
     438        if (curColor->R!=R || curColor->G!=G || curColor->B!=B){
     439            allSameY=0;
     440            break;
     441        }
     442        curColor=curColor->next;
     443    }
     444
     445    if (allSameY==1){
     446        int i=0;
     447               
     448        for(i=0;i<mapNumOfOutput;i++){
     449            color_table[i][0]=R;
     450            color_table[i][1]=G;
     451            color_table[i][2]=B;
     452        }
     453        return;
     454    }
     455
     456    float unit=(float)cm_unitWidth/(float)mapNumOfOutput;
     457
     458    curColor=map->keyColors;
     459    Color* nextColor=curColor->next;
     460
     461    double r1=curColor->R*255;
     462    double g1=curColor->G*255;
     463    double b1=curColor->B*255;
     464
     465    double r2=nextColor->R*255;
     466    double g2=nextColor->G*255;
     467    double b2=nextColor->B*255;
     468
     469    ControlPoint* curPoint=map->keyList;
     470    ControlPoint* nextPoint=curPoint->next;
     471
     472    double x1=curPoint->x;
     473    double x2=nextPoint->x;
     474
     475    float t=0;
     476
     477    int i=0;
     478    for(i=0;i<numOfOutput;i++){
     479               
     480        if (unit*i+15>x2){
     481            //go to next line segment
     482            curColor=nextColor;
     483            nextColor=nextColor->next;
     484
     485            curPoint=nextPoint;
     486            nextPoint=nextPoint->next;
     487
     488            if (nextColor==0 || nextPoint==0){
     489                return;
     490            }
     491
     492            x1=curPoint->x;
     493            x2=nextPoint->x;
     494
     495            r1=curColor->R*255;
     496            g1=curColor->G*255;
     497            b1=curColor->B*255;
     498
     499            r2=nextColor->R*255;
     500            g2=nextColor->G*255;
     501            b2=nextColor->B*255;
     502
     503        }
     504        t=((float)(unit*i+15-x1))/((float)(x2-x1));
     505
     506        color_table[i][0]=(float)(r1+t*(r2-r1))/(float)255;
     507        color_table[i][1]=(float)(g1+t*(g2-g1))/(float)255;
     508        color_table[i][2]=(float)(b1+t*(b2-b1))/(float)255;
     509    }   
     510
     511    update_tf_texture();
     512    glutSetWindow(colorMapWindow);
     513}
     514
     515
     516ControlPoint*
     517ColorGradientGLUTWindow::boundaryChecking()
     518{
     519    //find left and right neighbor in the list
     520    //If point is dragged passing the neighbor, the points are reordered.
     521
     522    ControlPoint* left=map->keyList;
     523    ControlPoint* right=map->keyList;
     524
     525    while (right!=cm_gvSelectedPoint){
     526        left=right;
     527        right=right->next;
     528    }
     529
     530    //selected point is not the right-end point
     531    if (right->next!=0){
     532        right=right->next;
     533        if (cm_gvSelectedPoint->x>right->x){
     534            sortPoints();
     535        }
     536    }
     537
     538    //selected point is not the left-end point
     539    if (left!=cm_gvSelectedPoint) {
     540        if (cm_gvSelectedPoint->x<left->x){
     541            sortPoints();
     542        }
     543    }
     544
     545    return cm_gvSelectedPoint;
    544546}
    545547
    546548
    547549//sort all controlpoints by bubble sort
    548 void ColorGradientGLUTWindow::sortPoints(){
    549         if (map->numOfKeys==0)
    550                 return;
    551 
    552         //bubble-sort list in ascending order by X
    553         ControlPoint* cur;
    554         ControlPoint* pre;
    555         ControlPoint* a;
    556         ControlPoint* b;
    557         cur=map->keyList;
    558 
    559         Color* curColor;
    560         Color* preColor;
    561         Color* c;
    562         Color* d;
    563         curColor=map->keyColors;
    564 
    565         int i;
    566         for(i=0;i<map->numOfKeys-1;i++){
    567                 while (cur->next!=NULL){
    568                         if (cur->x > cur->next->x){
    569                                 if (cur==map->keyList){
    570                                         //first node
    571                                         a=map->keyList;
    572                                         b=map->keyList->next;
    573                                         a->next=b->next;
    574                                         b->next=a;
    575                                         map->keyList=b;
    576                                         cur=map->keyList;
    577 
    578                                         c=map->keyColors;
    579                                         d=map->keyColors->next;
    580                                         c->next=d->next;
    581                                         d->next=c;
    582                                         map->keyColors=d;
    583                                         curColor=map->keyColors;
    584                                 }
    585                                 else {
    586                                         a=cur;
    587                                         b=cur->next;
    588                                         a->next=b->next;
    589                                         b->next=a;
    590                                         pre->next=b;
    591                                         cur=b;
    592 
    593                                         c=curColor;
    594                                         d=curColor->next;
    595                                         c->next=d->next;
    596                                         d->next=c;
    597                                         preColor->next=d;
    598                                         curColor=d;
    599                                 }
    600                         }
    601                         pre=cur;
    602                         cur=cur->next;
    603 
    604                         preColor=curColor;
    605                         curColor=curColor->next;
    606                 }
    607                 cur=map->keyList;
    608                 curColor=map->keyColors;
    609         }
    610         WriteControlPoints();
    611 }
    612 
    613 
    614 
    615 void ColorGradientGLUTWindow::printInterpolation(){
    616         int i=0;
    617         for(i=0;i<numOfOutput;i++){
    618                 //printf("(%g,%g,%g) ", mapOutput[3*i], mapOutput[3*i+1], mapOutput[3*i+2]);
    619                 fprintf(stderr, "(%g,%g,%g) ", color_table[i][0], color_table[i][1], color_table[i][2]);
    620         }
    621 }
    622 
    623 
     550void
     551ColorGradientGLUTWindow::sortPoints()
     552{
     553    if (map->numOfKeys==0)
     554        return;
     555
     556    //bubble-sort list in ascending order by X
     557    ControlPoint* cur;
     558    ControlPoint* pre;
     559    ControlPoint* a;
     560    ControlPoint* b;
     561    cur=map->keyList;
     562
     563    Color* curColor;
     564    Color* preColor;
     565    Color* c;
     566    Color* d;
     567    curColor=map->keyColors;
     568
     569    preColor = NULL;            /* Suppress compiler warning. */
     570    pre = NULL;
     571
     572    int i;
     573    for(i=0;i<map->numOfKeys-1;i++){
     574        while (cur->next!=NULL){
     575            if (cur->x > cur->next->x){
     576                if (cur==map->keyList){
     577                    //first node
     578                    a=map->keyList;
     579                    b=map->keyList->next;
     580                    a->next=b->next;
     581                    b->next=a;
     582                    map->keyList=b;
     583                    cur=map->keyList;
     584
     585                    c=map->keyColors;
     586                    d=map->keyColors->next;
     587                    c->next=d->next;
     588                    d->next=c;
     589                    map->keyColors=d;
     590                    curColor=map->keyColors;
     591                }
     592                else {
     593                    a=cur;
     594                    b=cur->next;
     595                    a->next=b->next;
     596                    b->next=a;
     597                    pre->next=b;
     598                    cur=b;
     599
     600                    c=curColor;
     601                    d=curColor->next;
     602                    c->next=d->next;
     603                    d->next=c;
     604                    preColor->next=d;
     605                    curColor=d;
     606                }
     607            }
     608            pre=cur;
     609            cur=cur->next;
     610
     611            preColor=curColor;
     612            curColor=curColor->next;
     613        }
     614        cur=map->keyList;
     615        curColor=map->keyColors;
     616    }
     617    WriteControlPoints();
     618}
     619
     620
     621
     622void
     623ColorGradientGLUTWindow::printInterpolation()
     624{
     625    int i=0;
     626    for(i=0;i<numOfOutput;i++){
     627        //printf("(%g,%g,%g) ", mapOutput[3*i], mapOutput[3*i+1], mapOutput[3*i+2]);
     628        fprintf(stderr, "(%g,%g,%g) ", color_table[i][0], color_table[i][1], color_table[i][2]);
     629    }
     630}
  • trunk/vizservers/nanovis/transfer-function/ColorPaletteWindow.cpp

    r380 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    2425int cpWinId;
    2526int cp_winx, cp_winy;
    26 int cp_color_r, cp_color_g, cp_color_b; //the RGB model
     27int cp_color_r, cp_color_g, cp_color_b; //the RGB model
    2728int cp_color_H, cp_color_S, cp_color_B; //the HSB model
    2829int color_model;
     
    3334
    3435
    35 namespace ColorPaletteWindow
    36 {
    37         int  cpwin_pos_x = 100;
    38         int  cpwin_pos_y = 425;
    39         enum colorMode{RGB, GRAY, HSB};
    40         Color* hue_color;
    41         ControlPoint* color_point_1;
    42         ControlPoint* color_point_2;
    43 
    44         //char fileName[255];
    45         static char* fileName= new char[255];// (char*) malloc(sizeof(char)*256);
    46 
    47 
     36namespace ColorPaletteWindow {
     37    int  cpwin_pos_x = 100;
     38    int  cpwin_pos_y = 425;
     39    enum colorMode{RGB, GRAY, HSB};
     40    Color* hue_color;
     41    ControlPoint* color_point_1;
     42    ControlPoint* color_point_2;
     43
     44    //char fileName[255];
     45    static char* fileName= new char[255];// (char*) malloc(sizeof(char)*256);
    4846};
    4947
     
    6058}
    6159
    62 void ColorPalette::cpSyncLive(){
    63         if (cp_glui!=0){
    64                 cp_glui->sync_live();
    65         }
     60void ColorPalette::cpSyncLive()
     61{
     62    if (cp_glui!=0){
     63        cp_glui->sync_live();
     64    }
    6665}
    6766
    6867void ColorPalette::cpInit()
    6968{
    70         color_model = 0;
    71 
    72         //fileName=(char*) malloc(sizeof(char)*30);
    73         for(int i=0;i<200;i++){
    74                 fileName[i]=0;
    75         }
    76 
    77         cp_winx = 430;
    78         cp_winy = 350;
    79 
    80         hue_color=new Color(1,0,0);
    81         color_point_1=new ControlPoint(0,cp_winy);
    82         color_point_2=new ControlPoint(0,2*cp_winy/3-15);
    83 
    84         glutInitWindowPosition( cpwin_pos_x, cpwin_pos_y);
    85         glutInitWindowSize( cp_winx, cp_winy );
     69    color_model = 0;
     70
     71    //fileName=(char*) malloc(sizeof(char)*30);
     72    for(int i=0;i<200;i++){
     73        fileName[i]=0;
     74    }
     75    cp_winx = 430;
     76    cp_winy = 350;
     77
     78    hue_color=new Color(1,0,0);
     79    color_point_1=new ControlPoint(0,cp_winy);
     80    color_point_2=new ControlPoint(0,2*cp_winy/3-15);
     81
     82    glutInitWindowPosition( cpwin_pos_x, cpwin_pos_y);
     83    glutInitWindowSize( cp_winx, cp_winy );
    8684 
    87         colorPaletteWindow = glutCreateWindow("Color Palette");
    88 
    89         glutDisplayFunc( cpDisplay );
    90         glutReshapeFunc( cpReshape );
    91         glutKeyboardFunc ( cpKeyboard );
    92         glutIdleFunc (cpIdle);
    93         glutMouseFunc (cpMouse);
    94         glutMotionFunc (cpMotion);
    95 
    96 
    97         GLUI_Master.set_glutReshapeFunc(cpReshape);
    98         GLUI_Master.set_glutDisplayFunc( cpDisplay );
    99         GLUI_Master.set_glutKeyboardFunc(cpKeyboard);
    100         GLUI_Master.set_glutSpecialFunc(NULL);
    101         GLUI_Master.set_glutMouseFunc(cpMouse);
    102         GLUI_Master.set_glutIdleFunc(cpIdle);
    103 
    104         createGLUIWidgets();
     85    colorPaletteWindow = glutCreateWindow("Color Palette");
     86
     87    glutDisplayFunc( cpDisplay );
     88    glutReshapeFunc( cpReshape );
     89    glutKeyboardFunc ( cpKeyboard );
     90    glutIdleFunc (cpIdle);
     91    glutMouseFunc (cpMouse);
     92    glutMotionFunc (cpMotion);
     93
     94
     95    GLUI_Master.set_glutReshapeFunc(cpReshape);
     96    GLUI_Master.set_glutDisplayFunc( cpDisplay );
     97    GLUI_Master.set_glutKeyboardFunc(cpKeyboard);
     98    GLUI_Master.set_glutSpecialFunc(NULL);
     99    GLUI_Master.set_glutMouseFunc(cpMouse);
     100    GLUI_Master.set_glutIdleFunc(cpIdle);
     101
     102    createGLUIWidgets();
    105103}
    106104
     
    109107
    110108void ColorPalette::cmdHandler(int arg){
    111         switch (arg){
    112                 case 1: //add color
    113                         cm_editState=1;
    114                         break;
    115                 case 2:
    116                         if (cm_gvSelectedPoint!=0)
    117                                 map->changeColor((double)cp_color_r/255, (double)cp_color_g/255, (double)cp_color_b/255);
    118                         update_tf_texture();
    119                         break;
    120                 case 3:
    121                         if (cm_gvSelectedPoint!=0)
    122                                 map->removeKey(cm_gvSelectedPoint->x);
    123                         update_tf_texture();
    124                         break;
    125                 case 4:
    126                         MainTransferFunctionWindow::loadFile(fileName);
    127                         break;
    128                 case 5:
    129                         MainTransferFunctionWindow::saveFile(fileName);
    130                         break;
    131                 case 6:
    132                         map->cleanUp();
    133                         update_tf_texture();
    134                         break;
    135                 case 7:
    136                         TransferFunctionGLUTWindow::cleanUpPoints();   
    137                         //map->cleanUp();
    138                         update_tf_texture();
    139                         break;
    140                 case 8:
    141                         if (color_model==0){
    142                                 color_r->enable();
    143                                 color_g->enable();
    144                                 color_b->enable();
    145 
    146                                 cp_color_r=255;
    147                                 cp_color_g=0;
    148                                 cp_color_b=0;
    149 
    150                                 color_H->disable();
    151                                 color_S->disable();
    152                                 color_B->disable();
    153 
    154                                 cp_color_H=0;
    155                                 cp_color_S=0;
    156                                 cp_color_B=0;
    157                                
    158                                 color_point_1->x=0;
    159                                 color_point_1->y=cp_winy;
    160 
    161                                 cp_glui->sync_live();
    162                         }
    163                         else if (color_model==1){
    164                                 color_r->enable();
    165                                 color_g->enable();
    166                                 color_b->enable();
    167 
    168                                 cp_color_r=0;
    169                                 cp_color_g=0;
    170                                 cp_color_b=0;
    171 
    172                                 color_H->disable();
    173                                 color_S->disable();
    174                                 color_B->disable();     
    175 
    176                                 cp_color_H=0;
    177                                 cp_color_S=0;
    178                                 cp_color_B=0;
    179 
    180                                 color_point_1->x=0;
    181                                 color_point_1->y=cp_winy;
    182 
    183                                 cp_glui->sync_live();
    184                         }
    185                         else if (color_model==2){
    186                                 color_r->enable();
    187                                 color_g->enable();
    188                                 color_b->enable();
    189 
    190                                 cp_color_r=255;
    191                                 cp_color_g=255;
    192                                 cp_color_b=255;
    193 
    194                                 color_H->enable();
    195                                 color_S->enable();
    196                                 color_B->enable();     
    197 
    198                                 cp_color_H=0;
    199                                 cp_color_S=100;
    200                                 cp_color_B=100;
    201 
    202                                 color_point_1->x=0;
    203                                 color_point_1->y=cp_winy;
    204 
    205                                 color_point_2->x=0;
    206 
    207                                 cp_glui->sync_live();
    208                         }
    209                
    210                         glutSetWindow(colorPaletteWindow);
    211                         glutPostRedisplay();
    212                         break;
    213                 case 9:
    214                         break;
    215                 case 10:
    216                         break;
    217 
    218                 default:
    219                         break;
    220         }
     109    switch (arg){
     110    case 1: //add color
     111        cm_editState=1;
     112        break;
     113    case 2:
     114        if (cm_gvSelectedPoint!=0)
     115            map->changeColor((double)cp_color_r/255, (double)cp_color_g/255, (double)cp_color_b/255);
     116        update_tf_texture();
     117        break;
     118    case 3:
     119        if (cm_gvSelectedPoint!=0)
     120            map->removeKey(cm_gvSelectedPoint->x);
     121        update_tf_texture();
     122        break;
     123    case 4:
     124        MainTransferFunctionWindow::loadFile(fileName);
     125        break;
     126    case 5:
     127        MainTransferFunctionWindow::saveFile(fileName);
     128        break;
     129    case 6:
     130        map->cleanUp();
     131        update_tf_texture();
     132        break;
     133    case 7:
     134        TransferFunctionGLUTWindow::cleanUpPoints();   
     135        //map->cleanUp();
     136        update_tf_texture();
     137        break;
     138    case 8:
     139        if (color_model==0){
     140            color_r->enable();
     141            color_g->enable();
     142            color_b->enable();
     143
     144            cp_color_r=255;
     145            cp_color_g=0;
     146            cp_color_b=0;
     147
     148            color_H->disable();
     149            color_S->disable();
     150            color_B->disable();
     151
     152            cp_color_H=0;
     153            cp_color_S=0;
     154            cp_color_B=0;
     155                               
     156            color_point_1->x=0;
     157            color_point_1->y=cp_winy;
     158
     159            cp_glui->sync_live();
     160        }
     161        else if (color_model==1){
     162            color_r->enable();
     163            color_g->enable();
     164            color_b->enable();
     165
     166            cp_color_r=0;
     167            cp_color_g=0;
     168            cp_color_b=0;
     169
     170            color_H->disable();
     171            color_S->disable();
     172            color_B->disable();
     173
     174            cp_color_H=0;
     175            cp_color_S=0;
     176            cp_color_B=0;
     177
     178            color_point_1->x=0;
     179            color_point_1->y=cp_winy;
     180
     181            cp_glui->sync_live();
     182        }
     183        else if (color_model==2){
     184            color_r->enable();
     185            color_g->enable();
     186            color_b->enable();
     187
     188            cp_color_r=255;
     189            cp_color_g=255;
     190            cp_color_b=255;
     191
     192            color_H->enable();
     193            color_S->enable();
     194            color_B->enable(); 
     195
     196            cp_color_H=0;
     197            cp_color_S=100;
     198            cp_color_B=100;
     199
     200            color_point_1->x=0;
     201            color_point_1->y=cp_winy;
     202
     203            color_point_2->x=0;
     204
     205            cp_glui->sync_live();
     206        }
     207               
     208        glutSetWindow(colorPaletteWindow);
     209        glutPostRedisplay();
     210        break;
     211    case 9:
     212        break;
     213    case 10:
     214        break;
     215
     216    default:
     217        break;
     218    }
    221219}
    222220
    223221
    224222void ColorPalette::openFile(){
    225        
     223       
    226224}
    227225
     
    230228
    231229void ColorPalette::createGLUIWidgets(){
    232         cp_glui = GLUI_Master.create_glui_subwindow(colorPaletteWindow, GLUI_SUBWINDOW_BOTTOM);
    233         cp_glui->set_main_gfx_window(colorPaletteWindow);
    234         //cp_glui->add_column( false );
    235         cp_glui->add_statictext("RGB Color:");
    236         color_r= cp_glui->add_spinner("R:", GLUI_SPINNER_INT, &cp_color_r);             
    237         color_r->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
    238         color_r->set_alignment(GLUI_ALIGN_CENTER);
    239         color_g= cp_glui->add_spinner("G:", GLUI_SPINNER_INT, &cp_color_g);
    240         color_g->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
    241         color_g->set_alignment(GLUI_ALIGN_CENTER);
    242         color_b = cp_glui->add_spinner("B:", GLUI_SPINNER_INT, &cp_color_b);
    243         color_b->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
    244         color_b->set_alignment(GLUI_ALIGN_CENTER);
    245 
    246         cp_glui->add_statictext("HSB Color:");
    247         color_H= cp_glui->add_spinner("H:Deg.", GLUI_SPINNER_INT, &cp_color_H);         
    248         color_H->set_int_limits(0, 360, GLUI_LIMIT_CLAMP);
    249         color_H->set_alignment(GLUI_ALIGN_CENTER);
    250         color_H->disable();
    251         color_S= cp_glui->add_spinner("S:%", GLUI_SPINNER_INT, &cp_color_S);
    252         color_S->set_int_limits(0, 100, GLUI_LIMIT_CLAMP);
    253         color_S->set_alignment(GLUI_ALIGN_CENTER);
    254         color_S->disable();
    255         color_B = cp_glui->add_spinner("B:%", GLUI_SPINNER_INT, &cp_color_B);
    256         color_B->set_int_limits(0, 100, GLUI_LIMIT_CLAMP);
    257         color_B->set_alignment(GLUI_ALIGN_CENTER);
    258         color_B->disable();
    259 
    260 
    261         cp_glui->add_column(true);
    262 
    263         GLUI_Panel* model_panel=cp_glui->add_panel("Color Model", GLUI_PANEL_EMBOSSED);
    264         GLUI_RadioGroup* model=cp_glui->add_radiogroup_to_panel(model_panel,&color_model, 8, cmdHandler);
    265         cp_glui->add_radiobutton_to_group(model, "RGB");
    266         cp_glui->add_radiobutton_to_group(model, "Grayscale");
    267         cp_glui->add_radiobutton_to_group(model, "HSB");
    268 
    269         cp_glui->add_statictext("Color Controls:");
    270         //cp_glui->add_button("Add Color", 1, cmdHandler);
    271         cp_glui->add_button("Set Color", 2, cmdHandler);
    272         cp_glui->add_button("Remove Color", 3, cmdHandler);
    273 
    274         cp_glui->add_separator();
    275 
    276         cp_glui->add_button("Reset Colormap", 6, cmdHandler);
    277         cp_glui->add_button("Reset TF", 7, cmdHandler);
    278 
    279 
    280         cp_glui->add_column(true);
    281         cp_glui->add_statictext("File:");
    282         cp_glui->add_edittext("Name: ",GLUI_EDITTEXT_TEXT, fileName);
    283         cp_glui->add_button("Load", 4, cmdHandler);
    284         cp_glui->add_button("Save", 5, cmdHandler);
    285 
    286         cp_glui->add_separator();
    287         cp_glui->add_spinner("Scale:", GLUI_SPINNER_FLOAT, &control_point_scale);
    288         eye_distance_label = cp_glui->add_statictext("Eye Distance: ");
    289         fps_label = cp_glui->add_statictext("FPS: ");
     230    cp_glui = GLUI_Master.create_glui_subwindow(colorPaletteWindow, GLUI_SUBWINDOW_BOTTOM);
     231    cp_glui->set_main_gfx_window(colorPaletteWindow);
     232    //cp_glui->add_column( false );
     233    cp_glui->add_statictext("RGB Color:");
     234    color_r= cp_glui->add_spinner("R:", GLUI_SPINNER_INT, &cp_color_r);         
     235    color_r->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
     236    color_r->set_alignment(GLUI_ALIGN_CENTER);
     237    color_g= cp_glui->add_spinner("G:", GLUI_SPINNER_INT, &cp_color_g);
     238    color_g->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
     239    color_g->set_alignment(GLUI_ALIGN_CENTER);
     240    color_b = cp_glui->add_spinner("B:", GLUI_SPINNER_INT, &cp_color_b);
     241    color_b->set_int_limits(0, 255, GLUI_LIMIT_CLAMP);
     242    color_b->set_alignment(GLUI_ALIGN_CENTER);
     243
     244    cp_glui->add_statictext("HSB Color:");
     245    color_H= cp_glui->add_spinner("H:Deg.", GLUI_SPINNER_INT, &cp_color_H);             
     246    color_H->set_int_limits(0, 360, GLUI_LIMIT_CLAMP);
     247    color_H->set_alignment(GLUI_ALIGN_CENTER);
     248    color_H->disable();
     249    color_S= cp_glui->add_spinner("S:%", GLUI_SPINNER_INT, &cp_color_S);
     250    color_S->set_int_limits(0, 100, GLUI_LIMIT_CLAMP);
     251    color_S->set_alignment(GLUI_ALIGN_CENTER);
     252    color_S->disable();
     253    color_B = cp_glui->add_spinner("B:%", GLUI_SPINNER_INT, &cp_color_B);
     254    color_B->set_int_limits(0, 100, GLUI_LIMIT_CLAMP);
     255    color_B->set_alignment(GLUI_ALIGN_CENTER);
     256    color_B->disable();
     257
     258
     259    cp_glui->add_column(true);
     260
     261    GLUI_Panel* model_panel=cp_glui->add_panel("Color Model", GLUI_PANEL_EMBOSSED);
     262    GLUI_RadioGroup* model=cp_glui->add_radiogroup_to_panel(model_panel,&color_model, 8, cmdHandler);
     263    cp_glui->add_radiobutton_to_group(model, "RGB");
     264    cp_glui->add_radiobutton_to_group(model, "Grayscale");
     265    cp_glui->add_radiobutton_to_group(model, "HSB");
     266
     267    cp_glui->add_statictext("Color Controls:");
     268    //cp_glui->add_button("Add Color", 1, cmdHandler);
     269    cp_glui->add_button("Set Color", 2, cmdHandler);
     270    cp_glui->add_button("Remove Color", 3, cmdHandler);
     271
     272    cp_glui->add_separator();
     273
     274    cp_glui->add_button("Reset Colormap", 6, cmdHandler);
     275    cp_glui->add_button("Reset TF", 7, cmdHandler);
     276
     277
     278    cp_glui->add_column(true);
     279    cp_glui->add_statictext("File:");
     280    cp_glui->add_edittext("Name: ",GLUI_EDITTEXT_TEXT, fileName);
     281    cp_glui->add_button("Load", 4, cmdHandler);
     282    cp_glui->add_button("Save", 5, cmdHandler);
     283
     284    cp_glui->add_separator();
     285    cp_glui->add_spinner("Scale:", GLUI_SPINNER_FLOAT, &control_point_scale);
     286    eye_distance_label = cp_glui->add_statictext("Eye Distance: ");
     287    fps_label = cp_glui->add_statictext("FPS: ");
    290288}
    291289
    292290
    293291void ColorPalette::cpIdle(){
    294         //glutSetWindow(mainWindow);
    295         //glutPostRedisplay();
     292    //glutSetWindow(mainWindow);
     293    //glutPostRedisplay();
    296294}
    297295
     
    300298
    301299void ColorPalette::confirm(int arg){
    302         if (cm_gvSelectedPoint!=0)
    303                 map->changeColor((double)cp_color_r/255, (double)cp_color_g/255, (double)cp_color_b/255);
    304         /*
    305         glutSetWindow(mainWindow);
    306         glutDestroyWindow(colorPaletteWindow);
    307         glutPostRedisplay();
    308         colorPaletteWindow=-1;
    309         */
     300    if (cm_gvSelectedPoint!=0)
     301        map->changeColor((double)cp_color_r/255, (double)cp_color_g/255, (double)cp_color_b/255);
     302    /*
     303      glutSetWindow(mainWindow);
     304      glutDestroyWindow(colorPaletteWindow);
     305      glutPostRedisplay();
     306      colorPaletteWindow=-1;
     307    */
    310308}
    311309
     
    314312void ColorPalette::cpDisplay()
    315313{
    316         //draw selectPoint
    317         //drawSelectPoint();
    318 
    319         glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
    320         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    321 
    322 
    323         glColor3d(0.0, 0.0, 0.0);
    324         glShadeModel(GL_SMOOTH);
    325         glDisable(GL_DEPTH_TEST);
    326 
    327 
    328         if (color_model==RGB){
    329 
    330                 Color* leftColor = new Color(1, 0, 0);
    331                 Color* rightColor = new Color(0, 0, 1);
    332 
    333                 GLfloat left_color[3];
    334                 GLfloat right_color[3];
    335 
    336                 leftColor->GetRGB(left_color);
    337                 rightColor->GetRGB(right_color);
    338 
    339                 //predifined 7 rainbow colors
    340                 GLfloat color_red[3] = {1, 0, 0};
    341                 GLfloat color_yelo[3] = {1, 1, 0};
    342                 GLfloat color_green[3] = {0, 1, 0};
    343                 GLfloat color_aqua[3] = {0, 1, 1};
    344                 GLfloat color_blue[3] = {0, 0, 1};
    345                 GLfloat color_magen[3] = {1, 0, 1};
    346 
    347 
    348                 GLfloat rainbowColor[18] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1};
    349                
    350                 int i=0;
    351                 float unitWidth = cp_winx/6;
    352 
    353                 glBegin(GL_QUADS);
    354                         for (i=0; i<6; i++){           
    355 
    356                                         glColor3f(rainbowColor[3*i], rainbowColor[3*i+1], rainbowColor[3*i+2]);
    357                                         glVertex2d(unitWidth*i, 0);
    358                                         glVertex2d(unitWidth*i, cp_winy);
    359 
    360                                         if (i==5){
    361                                                 glColor3f(rainbowColor[0], rainbowColor[1], rainbowColor[2]);
    362                                                 glVertex2d(cp_winx, cp_winy);
    363                                                 glVertex2d(cp_winx, 0);
    364                                         }
    365                                         else {
    366                                                 glColor3f(rainbowColor[3*i+3], rainbowColor[3*i+4], rainbowColor[3*i+5]);
    367                                                 glVertex2d(unitWidth*(i+1), cp_winy);
    368                                                 glVertex2d(unitWidth*(i+1), 0);
    369                                         }
    370                         }
    371                 glEnd();
    372 
    373                 //draw color point
    374                 glColor3d(0,0,0);
    375                 color_point_1->glDraw_3();
    376         }
    377 
    378         else if (color_model==GRAY){
    379                 glBegin(GL_QUADS);             
    380                         glColor3f(0, 0, 0);
    381                         glVertex2d(0, 0);
    382                         glVertex2d(0, cp_winy);
    383 
    384                         glColor3f(1, 1, 1);
    385                         glVertex2d(cp_winx, cp_winy);
    386                         glVertex2d(cp_winx, 0);
    387                 glEnd();
    388 
    389                 glColor3d(1,0,0);
    390                 color_point_1->glDraw_3();
    391         }
    392 
    393         else if(color_model==HSB){
    394                
    395                 glBegin(GL_QUADS);             
    396                         glColor3f(0, 0, 0);
    397                         glVertex2d(0, 2*cp_winy/3);
    398                         glColor3f(1, 1, 1);
    399                         glVertex2d(0, cp_winy);
    400 
    401                         glColor3f(hue_color->R, hue_color->G, hue_color->B);
    402                         glVertex2d(cp_winx, cp_winy);
    403                         glColor3f(0, 0, 0);
    404                         glVertex2d(cp_winx, 2*cp_winy/3);
    405                 glEnd();
    406 
    407                 //draw rainbow
    408                 Color* leftColor = new Color(1, 0, 0);
    409                 Color* rightColor = new Color(0, 0, 1);
    410 
    411                 GLfloat left_color[3];
    412                 GLfloat right_color[3];
    413 
    414                 leftColor->GetRGB(left_color);
    415                 rightColor->GetRGB(right_color);
    416 
    417                 //predifined 7 rainbow colors
    418                 GLfloat color_red[3] = {1, 0, 0};
    419                 GLfloat color_yelo[3] = {1, 1, 0};
    420                 GLfloat color_green[3] = {0, 1, 0};
    421                 GLfloat color_aqua[3] = {0, 1, 1};
    422                 GLfloat color_blue[3] = {0, 0, 1};
    423                 GLfloat color_magen[3] = {1, 0, 1};
    424 
    425 
    426                 GLfloat rainbowColor[18] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1};
    427                
    428                 int i=0;
    429                 float unitWidth = cp_winx/6;
    430 
    431                 glBegin(GL_QUADS);
    432                         for (i=0; i<6; i++){           
    433 
    434                                         glColor3f(rainbowColor[3*i], rainbowColor[3*i+1], rainbowColor[3*i+2]);
    435                                         glVertex2d(unitWidth*i, 2*cp_winy/3-15);
    436                                         glVertex2d(unitWidth*i, 2*cp_winy/3);
    437 
    438                                         if (i==5){
    439                                                 glColor3f(rainbowColor[0], rainbowColor[1], rainbowColor[2]);
    440                                                 glVertex2d(cp_winx, 2*cp_winy/3);
    441                                                 glVertex2d(cp_winx, 2*cp_winy/3-15);
    442                                         }
    443                                         else {
    444                                                 glColor3f(rainbowColor[3*i+3], rainbowColor[3*i+4], rainbowColor[3*i+5]);
    445                                                 glVertex2d(unitWidth*(i+1), 2*cp_winy/3);
    446                                                 glVertex2d(unitWidth*(i+1), 2*cp_winy/3-15);
    447                                         }
    448                         }
    449                 glEnd();
    450 
    451                 glColor3d(1-hue_color->R, 1-hue_color->G, 1-hue_color->B);
    452                 color_point_1->glDraw_3();
    453 
    454                 glColor3d(0,0,0);
    455                 color_point_2->glDraw_2();
    456                 /*
    457                 //draw bar
    458                 glLineWidth(1.0);
    459                 glBegin(GL_LINES);
    460                         glVertex2d(0, cp_winy/2-1);
    461                         glVertex2d(cp_winx, cp_winy/2-1);
    462                 glEnd();
    463                 */
    464 
    465                 /*
    466                 i=0;
    467                 Color* curColor=map->keyColors;
    468                 ControlPoint* curKey=map->keyList;
    469                 for (i=0; i<map->numOfKeys ; i++){
    470                         //glColor3d(curColor->R, curColor->G, curColor->B);
    471                         glColor3d(0.0, 0.0, 0.0);
    472                         curKey->glDraw_2();
    473                         curColor=curColor->next;
    474                         curKey=curKey->next;
    475                 }
    476                 */
    477 
    478         }
    479 
    480         glColor3f(0, 0, 0);
    481         glutSwapBuffers();
     314    //draw selectPoint
     315    //drawSelectPoint();
     316
     317    glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
     318    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     319
     320
     321    glColor3d(0.0, 0.0, 0.0);
     322    glShadeModel(GL_SMOOTH);
     323    glDisable(GL_DEPTH_TEST);
     324
     325
     326    if (color_model==RGB){
     327
     328        Color* leftColor = new Color(1, 0, 0);
     329        Color* rightColor = new Color(0, 0, 1);
     330
     331        GLfloat left_color[3];
     332        GLfloat right_color[3];
     333
     334        leftColor->GetRGB(left_color);
     335        rightColor->GetRGB(right_color);
     336
     337        GLfloat rainbowColor[18] = {
     338            1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1
     339        };
     340               
     341        int i=0;
     342        float unitWidth = cp_winx/6;
     343
     344        glBegin(GL_QUADS);
     345        for (i=0; i<6; i++){           
     346
     347            glColor3f(rainbowColor[3*i], rainbowColor[3*i+1], rainbowColor[3*i+2]);
     348            glVertex2d(unitWidth*i, 0);
     349            glVertex2d(unitWidth*i, cp_winy);
     350
     351            if (i==5){
     352                glColor3f(rainbowColor[0], rainbowColor[1], rainbowColor[2]);
     353                glVertex2d(cp_winx, cp_winy);
     354                glVertex2d(cp_winx, 0);
     355            }
     356            else {
     357                glColor3f(rainbowColor[3*i+3], rainbowColor[3*i+4], rainbowColor[3*i+5]);
     358                glVertex2d(unitWidth*(i+1), cp_winy);
     359                glVertex2d(unitWidth*(i+1), 0);
     360            }
     361        }
     362        glEnd();
     363
     364        //draw color point
     365        glColor3d(0,0,0);
     366        color_point_1->glDraw_3();
     367    }
     368
     369    else if (color_model==GRAY){
     370        glBegin(GL_QUADS);             
     371        glColor3f(0, 0, 0);
     372        glVertex2d(0, 0);
     373        glVertex2d(0, cp_winy);
     374
     375        glColor3f(1, 1, 1);
     376        glVertex2d(cp_winx, cp_winy);
     377        glVertex2d(cp_winx, 0);
     378        glEnd();
     379
     380        glColor3d(1,0,0);
     381        color_point_1->glDraw_3();
     382    }
     383
     384    else if(color_model==HSB){
     385               
     386        glBegin(GL_QUADS);             
     387        glColor3f(0, 0, 0);
     388        glVertex2d(0, 2*cp_winy/3);
     389        glColor3f(1, 1, 1);
     390        glVertex2d(0, cp_winy);
     391
     392        glColor3f(hue_color->R, hue_color->G, hue_color->B);
     393        glVertex2d(cp_winx, cp_winy);
     394        glColor3f(0, 0, 0);
     395        glVertex2d(cp_winx, 2*cp_winy/3);
     396        glEnd();
     397
     398        //draw rainbow
     399        Color* leftColor = new Color(1, 0, 0);
     400        Color* rightColor = new Color(0, 0, 1);
     401
     402        GLfloat left_color[3];
     403        GLfloat right_color[3];
     404
     405        leftColor->GetRGB(left_color);
     406        rightColor->GetRGB(right_color);
     407
     408        GLfloat rainbowColor[18] = {
     409            1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1
     410        };
     411               
     412        int i=0;
     413        float unitWidth = cp_winx/6;
     414
     415        glBegin(GL_QUADS);
     416        for (i=0; i<6; i++){           
     417
     418            glColor3f(rainbowColor[3*i], rainbowColor[3*i+1], rainbowColor[3*i+2]);
     419            glVertex2d(unitWidth*i, 2*cp_winy/3-15);
     420            glVertex2d(unitWidth*i, 2*cp_winy/3);
     421
     422            if (i==5){
     423                glColor3f(rainbowColor[0], rainbowColor[1], rainbowColor[2]);
     424                glVertex2d(cp_winx, 2*cp_winy/3);
     425                glVertex2d(cp_winx, 2*cp_winy/3-15);
     426            }
     427            else {
     428                glColor3f(rainbowColor[3*i+3], rainbowColor[3*i+4], rainbowColor[3*i+5]);
     429                glVertex2d(unitWidth*(i+1), 2*cp_winy/3);
     430                glVertex2d(unitWidth*(i+1), 2*cp_winy/3-15);
     431            }
     432        }
     433        glEnd();
     434
     435        glColor3d(1-hue_color->R, 1-hue_color->G, 1-hue_color->B);
     436        color_point_1->glDraw_3();
     437
     438        glColor3d(0,0,0);
     439        color_point_2->glDraw_2();
     440        /*
     441        //draw bar
     442        glLineWidth(1.0);
     443        glBegin(GL_LINES);
     444        glVertex2d(0, cp_winy/2-1);
     445        glVertex2d(cp_winx, cp_winy/2-1);
     446        glEnd();
     447        */
     448
     449        /*
     450          i=0;
     451          Color* curColor=map->keyColors;
     452          ControlPoint* curKey=map->keyList;
     453          for (i=0; i<map->numOfKeys ; i++){
     454          //glColor3d(curColor->R, curColor->G, curColor->B);
     455          glColor3d(0.0, 0.0, 0.0);
     456          curKey->glDraw_2();
     457          curColor=curColor->next;
     458          curKey=curKey->next;
     459          }
     460        */
     461
     462    }
     463
     464    glColor3f(0, 0, 0);
     465    glutSwapBuffers();
    482466}
    483467
     
    486470
    487471void ColorPalette::cpReshape(int x, int y){
    488         //printf("cpreshape=%d",x);
    489 
    490         if (x==0 || y==0)
    491                 return;
    492 
    493         cp_winx=x;
    494         cp_winy=y;
    495         glViewport( 0, 0, x, y );
    496 
    497         glMatrixMode( GL_PROJECTION );
    498         glLoadIdentity();
    499 
    500         glMatrixMode(GL_MODELVIEW);
    501         glLoadIdentity();
    502 
    503         //Set up most convenient projection
    504         gluOrtho2D(0, x, 0, y);
    505         glutPostRedisplay();
     472    //printf("cpreshape=%d",x);
     473
     474    if (x==0 || y==0)
     475        return;
     476
     477    cp_winx=x;
     478    cp_winy=y;
     479    glViewport( 0, 0, x, y );
     480
     481    glMatrixMode( GL_PROJECTION );
     482    glLoadIdentity();
     483
     484    glMatrixMode(GL_MODELVIEW);
     485    glLoadIdentity();
     486
     487    //Set up most convenient projection
     488    gluOrtho2D(0, x, 0, y);
     489    glutPostRedisplay();
    506490}
    507491
     
    509493void ColorPalette::cpKeyboard(unsigned char key, int x, int y)
    510494{
    511         //glutPostRedisplay();
     495    //glutPostRedisplay();
    512496}
    513497
     
    516500void ColorPalette::cpMouse(int button, int state, int x, int y){
    517501
    518         GLint viewportVector[4];
    519         GLdouble scX, scY; //the coordinates of selection point on rainbow
    520 
    521         glGetIntegerv(GL_VIEWPORT, viewportVector);
    522 
    523         scX = x;
    524         scY = viewportVector[3] - (GLint) y - 1;
    525 
    526 
    527         if (color_model!=HSB){
    528                 color_point_1->x=scX;
    529                 color_point_1->y=scY;
    530         }
    531         else{ //HSB
    532                 if (scY>=2*cp_winy/3){
    533                         color_point_1->x=scX;
    534                         color_point_1->y=scY;
    535                         getColor(scX, scY);
    536                 }
    537                 if (scY<2*cp_winy/3){
    538                         color_point_2->x=scX;
    539                         //color_point_1->x=0;
    540                         //color_point_1->y=cp_winy;
    541                         getColor(scX, color_point_2->y);
    542                 }
    543         }
    544         //printf("(%g, %g)\n", scX, scY);
    545 
    546 
    547         if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    548                 getColor(scX, scY);
    549         }
    550         else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){}
    551         else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN){}
    552         else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP){}
    553 
    554         cp_glui->sync_live();
    555         glutPostRedisplay();
     502    GLint viewportVector[4];
     503    GLdouble scX, scY; //the coordinates of selection point on rainbow
     504
     505    glGetIntegerv(GL_VIEWPORT, viewportVector);
     506
     507    scX = x;
     508    scY = viewportVector[3] - (GLint) y - 1;
     509
     510
     511    if (color_model!=HSB){
     512        color_point_1->x=scX;
     513        color_point_1->y=scY;
     514    }
     515    else{ //HSB
     516        if (scY>=2*cp_winy/3){
     517            color_point_1->x=scX;
     518            color_point_1->y=scY;
     519            getColor(scX, scY);
     520        }
     521        if (scY<2*cp_winy/3){
     522            color_point_2->x=scX;
     523            //color_point_1->x=0;
     524            //color_point_1->y=cp_winy;
     525            getColor(scX, color_point_2->y);
     526        }
     527    }
     528    //printf("(%g, %g)\n", scX, scY);
     529
     530
     531    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
     532        getColor(scX, scY);
     533    }
     534    else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){}
     535    else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN){}
     536    else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP){}
     537
     538    cp_glui->sync_live();
     539    glutPostRedisplay();
    556540
    557541}
     
    565549//interpolate GRB color
    566550Color ColorPalette::getColor(int x, int y){
    567         if (color_model==RGB){
    568                 float unitWidth = cp_winx/6;           
    569                 GLfloat rainbowColor[21] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0};
    570 
    571                 int pos = floor(x/unitWidth);
    572                 int red_left = rainbowColor[3*pos];
    573                 int red_right = rainbowColor[3*(pos+1)];
    574 
    575                 int green_left = rainbowColor[3*pos+1];
    576                 int green_right = rainbowColor[3*(pos+1)+1];
    577 
    578                 int blue_left = rainbowColor[3*pos+2];
    579                 int blue_right = rainbowColor[3*(pos+1)+2];
    580 
    581                 double r, g, b;
    582                 r = (red_left+((float)x/(float)unitWidth-pos)*(red_right-red_left));
    583                 g = (green_left+((float)x/(float)unitWidth-pos)*(green_right-green_left));
    584                 b = (blue_left+((float)x/(float)unitWidth-pos)*(blue_right-blue_left));
    585 
    586                 cp_color_r=255*r;
    587                 cp_color_g=255*g;
    588                 cp_color_b=255*b;
    589 
    590 
    591                 cp_glui->sync_live();
    592                 return Color(r,g,b);
    593         }
    594 
    595         else if (color_model==GRAY){
    596                 float t=((float)x)/((float)cp_winx);
    597                 double r, g, b;
    598                 r = t;
    599                 g = t;
    600                 b = t;
    601 
    602                 cp_color_r=255*r;
    603                 cp_color_g=255*g;
    604                 cp_color_b=255*b;
    605 
    606                 cp_glui->sync_live();
    607                 return Color(r,g,b);
    608         }
    609 
    610         else if (color_model==HSB){
    611                 double r, g, b;
    612                 if (y==color_point_2->y){
    613                         float unitWidth = cp_winx/6;           
    614                         GLfloat rainbowColor[21] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0};
    615 
    616                         int pos = floor(x/unitWidth);
    617                         int red_left = rainbowColor[3*pos];
    618                         int red_right = rainbowColor[3*(pos+1)];
    619 
    620                         int green_left = rainbowColor[3*pos+1];
    621                         int green_right = rainbowColor[3*(pos+1)+1];
    622 
    623                         int blue_left = rainbowColor[3*pos+2];
    624                         int blue_right = rainbowColor[3*(pos+1)+2];
    625 
    626                
    627                         r = (red_left+((float)x/(float)unitWidth-pos)*(red_right-red_left));
    628                         g = (green_left+((float)x/(float)unitWidth-pos)*(green_right-green_left));
    629                         b = (blue_left+((float)x/(float)unitWidth-pos)*(blue_right-blue_left));
    630 
    631                         cp_color_H=360*((double)x/(double)cp_winx);
    632 
    633                         hue_color->R=r;
    634                         hue_color->G=g;
    635                         hue_color->B=b;
    636                        
    637                 }
    638                 else{
    639                         cp_color_S=100*(1-(double)color_point_1->x/(double)cp_winx);
    640                         cp_color_B=100*((double)(color_point_1->y-(double)(2*cp_winy/3))/((double)cp_winy/3));
    641                 }
    642 
    643                 //convert HSB to RGB
    644 
    645                 //1. horizontal interpolation:
    646                 //upper left: (1,1,1)
    647                 //upper right: hue color
    648                 r=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->R-1);
    649                 g=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->G-1);
    650                 b=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->B-1);
    651 
    652                 //2. vertical interpolation:
    653                 //buttom (0,0,0)
    654                 r=(double)r+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-r));
    655                 g=(double)g+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-g));
    656                 b=(double)b+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-b));
    657 
    658                 cp_color_r=255*r;
    659                 cp_color_g=255*g;
    660                 cp_color_b=255*b;
    661 
    662 
    663 
    664                 //printf("(%d,%d,%d)    ", cp_color_r, cp_color_g, cp_color_b);
    665 
    666                 cp_glui->sync_live();
    667                
    668                 return Color(r,g,b);
    669         }
    670 
    671         return Color(0,0,0);
    672 }
     551    if (color_model==RGB){
     552        float unitWidth = cp_winx/6;           
     553        GLfloat rainbowColor[21] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0};
     554
     555        int pos = floor(x/unitWidth);
     556        int red_left = rainbowColor[3*pos];
     557        int red_right = rainbowColor[3*(pos+1)];
     558
     559        int green_left = rainbowColor[3*pos+1];
     560        int green_right = rainbowColor[3*(pos+1)+1];
     561
     562        int blue_left = rainbowColor[3*pos+2];
     563        int blue_right = rainbowColor[3*(pos+1)+2];
     564
     565        double r, g, b;
     566        r = (red_left+((float)x/(float)unitWidth-pos)*(red_right-red_left));
     567        g = (green_left+((float)x/(float)unitWidth-pos)*(green_right-green_left));
     568        b = (blue_left+((float)x/(float)unitWidth-pos)*(blue_right-blue_left));
     569
     570        cp_color_r=255*r;
     571        cp_color_g=255*g;
     572        cp_color_b=255*b;
     573
     574
     575        cp_glui->sync_live();
     576        return Color(r,g,b);
     577    }
     578
     579    else if (color_model==GRAY){
     580        float t=((float)x)/((float)cp_winx);
     581        double r, g, b;
     582        r = t;
     583        g = t;
     584        b = t;
     585
     586        cp_color_r=255*r;
     587        cp_color_g=255*g;
     588        cp_color_b=255*b;
     589
     590        cp_glui->sync_live();
     591        return Color(r,g,b);
     592    }
     593
     594    else if (color_model==HSB){
     595        double r, g, b;
     596        if (y==color_point_2->y){
     597            float unitWidth = cp_winx/6;               
     598            GLfloat rainbowColor[21] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0};
     599
     600            int pos = floor(x/unitWidth);
     601            int red_left = rainbowColor[3*pos];
     602            int red_right = rainbowColor[3*(pos+1)];
     603
     604            int green_left = rainbowColor[3*pos+1];
     605            int green_right = rainbowColor[3*(pos+1)+1];
     606
     607            int blue_left = rainbowColor[3*pos+2];
     608            int blue_right = rainbowColor[3*(pos+1)+2];
     609
     610               
     611            r = (red_left+((float)x/(float)unitWidth-pos)*(red_right-red_left));
     612            g = (green_left+((float)x/(float)unitWidth-pos)*(green_right-green_left));
     613            b = (blue_left+((float)x/(float)unitWidth-pos)*(blue_right-blue_left));
     614
     615            cp_color_H=360*((double)x/(double)cp_winx);
     616
     617            hue_color->R=r;
     618            hue_color->G=g;
     619            hue_color->B=b;
     620                       
     621        }
     622        else{
     623            cp_color_S=100*(1-(double)color_point_1->x/(double)cp_winx);
     624            cp_color_B=100*((double)(color_point_1->y-(double)(2*cp_winy/3))/((double)cp_winy/3));
     625        }
     626
     627        //convert HSB to RGB
     628
     629        //1. horizontal interpolation:
     630        //upper left: (1,1,1)
     631        //upper right: hue color
     632        r=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->R-1);
     633        g=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->G-1);
     634        b=(double)1+(double)color_point_1->x/((double)cp_winx)*(double)(hue_color->B-1);
     635
     636        //2. vertical interpolation:
     637        //buttom (0,0,0)
     638        r=(double)r+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-r));
     639        g=(double)g+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-g));
     640        b=(double)b+((double)cp_winy-(double)color_point_1->y)/((double)cp_winy/3)*((double)(0-b));
     641
     642        cp_color_r=255*r;
     643        cp_color_g=255*g;
     644        cp_color_b=255*b;
     645
     646
     647
     648        //printf("(%d,%d,%d)    ", cp_color_r, cp_color_g, cp_color_b);
     649
     650        cp_glui->sync_live();
     651               
     652        return Color(r,g,b);
     653    }
     654
     655    return Color(0,0,0);
     656}
  • trunk/vizservers/nanovis/transfer-function/TransferFunctionGLUTWindow.cpp

    r383 r953  
     1
    12/*
    23 * ----------------------------------------------------------------------
     
    4950{
    5051
    51 int winx = 600;
    52 int winy = 150;
    53 
    54 int unitHeight;
    55 
    56 double gv_X, gv_Y;
    57 double gv_lastY;
    58 
    59 bool gvScaleDragging=false;
    60 
    61 double gv_DX, gv_DY;
    62 
    63 float lv_tf_height_scale = 2.0;
    64 float last_lv_tf_height_scale= 2.0;
    65 
    66 float lv_tf_width_scale = 1.0;
    67 float last_lv_tf_width_scale= 1.0;
     52    int winx = 600;
     53    int winy = 150;
     54
     55    int unitHeight;
     56
     57    double gv_X, gv_Y;
     58    double gv_lastY;
     59
     60    bool gvScaleDragging=false;
     61
     62    double gv_DX, gv_DY;
     63
     64    float lv_tf_height_scale = 2.0;
     65    float last_lv_tf_height_scale= 2.0;
     66
     67    float lv_tf_width_scale = 1.0;
     68    float last_lv_tf_width_scale= 1.0;
    6869
    6970};
     
    8889{
    8990
    90         //initialize the global list of contorlPoint "tf_pointList"
    91         tf_winx=main_window_x;
    92         tf_winy=main_window_y*2/3;
    93 
    94         unitHeight = 0.8*tf_winy;
    95         tf_unitWidth = 0.95*tf_winx;
    96 
    97 
    98         //printf("tf_unitWidth=%d\n", tf_unitWidth);
    99 
    100 
    101         tf_pointList = new ControlPoint(15,15);
    102         tf_pointList->next = new ControlPoint(15+tf_unitWidth,15);
    103 
    104         int i=0;
    105         for(i=0;i<numOfOutput;i++){
    106                 //output[i]=0;
    107                 color_table[i][3]=0;
    108         }       
    109         tf_numOfPoints=2;
    110         tf_pointEditState=0;
    111 
    112         tf_gvSelectedPoint = new ControlPoint(0,0);
    113 
    114         fprintf(stderr, "Transferfunction Init...\n");
     91    //initialize the global list of contorlPoint "tf_pointList"
     92    tf_winx=main_window_x;
     93    tf_winy=main_window_y*2/3;
     94
     95    unitHeight = 0.8*tf_winy;
     96    tf_unitWidth = 0.95*tf_winx;
     97
     98
     99    //printf("tf_unitWidth=%d\n", tf_unitWidth);
     100
     101
     102    tf_pointList = new ControlPoint(15,15);
     103    tf_pointList->next = new ControlPoint(15+tf_unitWidth,15);
     104
     105    int i=0;
     106    for(i=0;i<numOfOutput;i++){
     107        //output[i]=0;
     108        color_table[i][3]=0;
     109    }   
     110    tf_numOfPoints=2;
     111    tf_pointEditState=0;
     112
     113    tf_gvSelectedPoint = new ControlPoint(0,0);
     114
     115    fprintf(stderr, "Transferfunction Init...\n");
    115116}
    116117
     
    121122{
    122123
    123         glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
    124         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    125 
    126 
    127         glColor3d(0, 0, 0);
    128 
    129         //draw x and y axis
    130         glBegin(GL_LINES);
    131                 glVertex2d(15, 15);
    132                 glVertex2d(15, winy);
    133 
    134                 glVertex2d(15, 15);
    135                 glVertex2d(winx, 15);
    136         glEnd();
    137 
    138         //printf("tf_unitWidth=%d\n", tf_unitWidth);
    139 
    140         //draw label for X
    141         glRasterPos2f(2, 2);
    142         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, '0');
    143 
    144         glRasterPos2f(18+tf_unitWidth, 2);
    145         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, '1');
    146 
    147 
    148         //draw marker on axis
    149         glLineWidth(2.0);
    150         glBegin(GL_LINES);
    151                 glVertex2d(15+tf_unitWidth, 20);
    152                 glVertex2d(15+tf_unitWidth, 15);
    153 
    154                 int k;
    155                 k = 1;
    156                 while (15+k*unitHeight/lv_tf_height_scale < winy)
    157                 {
    158                         glVertex2d(20, 15+k*unitHeight/lv_tf_height_scale);
    159                         glVertex2d(15, 15+k*unitHeight/lv_tf_height_scale);
    160                         k++;
    161                 }
    162         glEnd();
    163         glLineWidth(1.0);
    164 
    165         //draw label for Y
    166         k = 1;
    167         char label[3];
    168 
    169         /*
    170         while (15+k*unitHeight/lv_tf_height_scale < winy){
    171                 glRasterPos2f(2,15+k*unitHeight/lv_tf_height_scale);
    172                 label[0]=0;
    173                 label[1]=0;
    174                 label[2]=0;
    175                 itoa(k, label, 10);
    176                 int i=0;
    177                 while (label[i]!=0){
    178                         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, label[i]);
    179                         i++;
    180                 }
    181                 k++;
    182         }
    183         */
    184 
    185         //printf("tf_unitWidth=%d\n", tf_unitWidth);
    186 
    187         //draw all points
    188         ControlPoint* cur=tf_pointList;
    189         while(cur!=0){
    190                 cur->glDraw();
    191                 cur=cur->next;
    192         }
    193 
    194         glEnable(GL_LINE_SMOOTH);
    195         glLineWidth(2.0);
    196 
    197 
    198         //connect all points
    199         cur=tf_pointList;
    200         glBegin(GL_LINES);
    201                 glVertex2d(cur->x, cur->y);
    202 
    203                 while (cur!=0){
    204                         if (cur->next!=0){
    205                                 glVertex2d(cur->x, cur->y);
    206                                 glVertex2d(cur->x, cur->y);
    207                         }
    208                         else
    209                                 glVertex2d(cur->x, cur->y);
    210                         cur=cur->next;
    211                 }
    212                 glVertex2d(winx, 15);   
    213         glEnd();
    214 
    215         glLineWidth(1.0);
    216         glDisable(GL_LINE_SMOOTH);
    217         plotHist();
    218 
    219   glutSwapBuffers();
     124    glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
     125    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     126
     127
     128    glColor3d(0, 0, 0);
     129
     130    //draw x and y axis
     131    glBegin(GL_LINES);
     132    glVertex2d(15, 15);
     133    glVertex2d(15, winy);
     134
     135    glVertex2d(15, 15);
     136    glVertex2d(winx, 15);
     137    glEnd();
     138
     139    //printf("tf_unitWidth=%d\n", tf_unitWidth);
     140
     141    //draw label for X
     142    glRasterPos2f(2, 2);
     143    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, '0');
     144
     145    glRasterPos2f(18+tf_unitWidth, 2);
     146    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, '1');
     147
     148
     149    //draw marker on axis
     150    glLineWidth(2.0);
     151    glBegin(GL_LINES);
     152    glVertex2d(15+tf_unitWidth, 20);
     153    glVertex2d(15+tf_unitWidth, 15);
     154
     155    int k;
     156    k = 1;
     157    while (15+k*unitHeight/lv_tf_height_scale < winy)
     158        {
     159            glVertex2d(20, 15+k*unitHeight/lv_tf_height_scale);
     160            glVertex2d(15, 15+k*unitHeight/lv_tf_height_scale);
     161            k++;
     162        }
     163    glEnd();
     164    glLineWidth(1.0);
     165
     166    //draw label for Y
     167    k = 1;
     168
     169#ifdef notdef
     170      char label[3];
     171      while (15+k*unitHeight/lv_tf_height_scale < winy) {
     172          glRasterPos2f(2,15+k*unitHeight/lv_tf_height_scale);
     173          label[0]=0;
     174          label[1]=0;
     175          label[2]=0;
     176          itoa(k, label, 10);
     177          int i=0;
     178          while (label[i]!=0){
     179              glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, label[i]);
     180              i++;
     181          }
     182          k++;
     183      }
     184      printf("tf_unitWidth=%d\n", tf_unitWidth);
     185#endif
     186
     187
     188    //draw all points
     189    ControlPoint* cur=tf_pointList;
     190    while(cur!=0){
     191        cur->glDraw();
     192        cur=cur->next;
     193    }
     194
     195    glEnable(GL_LINE_SMOOTH);
     196    glLineWidth(2.0);
     197
     198
     199    //connect all points
     200    cur=tf_pointList;
     201    glBegin(GL_LINES);
     202    glVertex2d(cur->x, cur->y);
     203
     204    while (cur!=0){
     205        if (cur->next!=0){
     206            glVertex2d(cur->x, cur->y);
     207            glVertex2d(cur->x, cur->y);
     208        }
     209        else
     210            glVertex2d(cur->x, cur->y);
     211        cur=cur->next;
     212    }
     213    glVertex2d(winx, 15);       
     214    glEnd();
     215
     216    glLineWidth(1.0);
     217    glDisable(GL_LINE_SMOOTH);
     218    plotHist();
     219
     220    glutSwapBuffers();
    220221
    221222}
     
    225226bool TransferFunctionGLUTWindow::SelectPoint(double x, double y)
    226227{
    227         //printf("select point\n");
    228        
    229         if (tf_numOfPoints==0)
    230                 return false;
    231 
    232         //Reset selected/dragged flags
    233         ControlPoint* cur=tf_pointList;
    234         while(cur!=0){
    235                 cur->dragged=false;
    236                 cur->selected=false;
    237                 cur=cur->next;
    238         }
    239 
    240         tf_gvSelectedPoint = NULL;
    241 
    242         //Traverse the points and find the one that seems close
    243         cur=tf_pointList;
    244         while(cur!=0){
    245                 double distanceSq;
    246                 distanceSq = (cur->x - x)*(cur->x - x) + (cur->y - y)*(cur->y - y);
    247                
    248                 if (distanceSq < distanceThreshold){
    249                         cur->selected = true;
    250                         tf_gvSelectedPoint = cur;
    251                         return true;
    252                 }
    253                 cur=cur->next;
    254         }
    255 
    256         return false;
     228    //printf("select point\n");
     229       
     230    if (tf_numOfPoints==0)
     231        return false;
     232
     233    //Reset selected/dragged flags
     234    ControlPoint* cur=tf_pointList;
     235    while(cur!=0){
     236        cur->dragged=false;
     237        cur->selected=false;
     238        cur=cur->next;
     239    }
     240
     241    tf_gvSelectedPoint = NULL;
     242
     243    //Traverse the points and find the one that seems close
     244    cur=tf_pointList;
     245    while(cur!=0){
     246        double distanceSq;
     247        distanceSq = (cur->x - x)*(cur->x - x) + (cur->y - y)*(cur->y - y);
     248               
     249        if (distanceSq < distanceThreshold){
     250            cur->selected = true;
     251            tf_gvSelectedPoint = cur;
     252            return true;
     253        }
     254        cur=cur->next;
     255    }
     256
     257    return false;
    257258}
    258259
     
    260261void TransferFunctionGLUTWindow::tfMouse(int button, int state, int x, int y)
    261262{
    262         //printf("tf mouse\n");
    263         //printf("(%d,%d)\n", x, y);
    264         GLint viewportVector[4];
    265         double scX, scY;
    266 
    267         glGetIntegerv(GL_VIEWPORT, viewportVector);
    268        
    269         gv_X = scX = x;
    270         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    271 
    272         gv_lastY = scY;
    273 
    274         //Selecting point, begin dragging
    275         bool gvIsSelected = SelectPoint(scX, scY);     
    276 
    277         if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    278                
    279                 if (gvIsSelected && tf_pointEditState==0){
    280                         assert(tf_gvSelectedPoint!=NULL);
    281                         gv_DX = scX - tf_gvSelectedPoint->x; //Offset from current point to selected point, for dragging corrections
    282                         gv_DY = scY - tf_gvSelectedPoint->y;
    283        
    284                         tf_gvSelectedPoint->dragged = true;
    285                         tf_gvIsDragging = true;
    286                 }
    287                 /*else if ((!gvIsSelected) && tf_pointEditState==1){
    288                         GLint viewportVector[4];
    289                         double scX, scY;
    290 
    291                         glGetIntegerv(GL_VIEWPORT, viewportVector);
    292 
    293                         gv_X = scX = x;
    294                         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    295                         tf_gvSelectedPoint=addPoint(scX - gv_DX,scY - gv_DY);
    296                         tf_pointEditState=0;
    297                         WriteControlPoints();
    298                 }*/
    299         }
    300         else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){
    301                 //"     End dragging" code.
    302                 tf_gvIsDragging = false;
    303                 if (tf_gvSelectedPoint)
    304                         tf_gvSelectedPoint->dragged = false;
    305         }
    306 
    307         if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
    308         {
    309                         GLint viewportVector[4];
    310                         double scX, scY;
    311 
    312                         glGetIntegerv(GL_VIEWPORT, viewportVector);
    313 
    314                         gv_X = scX = x;
    315                         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    316                         tf_gvSelectedPoint=addPoint(scX - gv_DX,scY - gv_DY);
    317                         tf_pointEditState=0;
    318                         WriteControlPoints();
    319                 }
    320 
    321 
    322        
    323         if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    324                 gvScaleDragging = true;
    325 
    326         if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
    327                 gvScaleDragging = false;
    328 
    329         glutPostRedisplay();
     263    //printf("tf mouse\n");
     264    //printf("(%d,%d)\n", x, y);
     265    GLint viewportVector[4];
     266    double scX, scY;
     267
     268    glGetIntegerv(GL_VIEWPORT, viewportVector);
     269       
     270    gv_X = scX = x;
     271    gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     272
     273    gv_lastY = scY;
     274
     275    //Selecting point, begin dragging
     276    bool gvIsSelected = SelectPoint(scX, scY); 
     277
     278    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
     279               
     280        if (gvIsSelected && tf_pointEditState==0){
     281            assert(tf_gvSelectedPoint!=NULL);
     282            gv_DX = scX - tf_gvSelectedPoint->x; //Offset from current point to selected point, for dragging corrections
     283            gv_DY = scY - tf_gvSelectedPoint->y;
     284       
     285            tf_gvSelectedPoint->dragged = true;
     286            tf_gvIsDragging = true;
     287        }
     288        /*else if ((!gvIsSelected) && tf_pointEditState==1){
     289          GLint viewportVector[4];
     290          double scX, scY;
     291
     292          glGetIntegerv(GL_VIEWPORT, viewportVector);
     293
     294          gv_X = scX = x;
     295          gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     296          tf_gvSelectedPoint=addPoint(scX - gv_DX,scY - gv_DY);
     297          tf_pointEditState=0;
     298          WriteControlPoints();
     299          }*/
     300    }
     301    else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){
     302        //"     End dragging" code.
     303        tf_gvIsDragging = false;
     304        if (tf_gvSelectedPoint)
     305            tf_gvSelectedPoint->dragged = false;
     306    }
     307
     308    if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
     309        {
     310            GLint viewportVector[4];
     311            double scX, scY;
     312
     313            glGetIntegerv(GL_VIEWPORT, viewportVector);
     314
     315            gv_X = scX = x;
     316            gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     317            tf_gvSelectedPoint=addPoint(scX - gv_DX,scY - gv_DY);
     318            tf_pointEditState=0;
     319            WriteControlPoints();
     320        }
     321
     322
     323       
     324    if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
     325        gvScaleDragging = true;
     326
     327    if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
     328        gvScaleDragging = false;
     329
     330    glutPostRedisplay();
    330331}
    331332
     
    334335{
    335336
    336         GLint viewportVector[4];
    337         double scX, scY;
    338 
    339         glGetIntegerv(GL_VIEWPORT, viewportVector);
    340 
    341         gv_X = scX = x;
    342         gv_Y = scY = viewportVector[3] - (GLint) y - 1;
    343 
    344         if (tf_gvIsDragging == true && tf_gvSelectedPoint!=NULL){
    345                 //Dragging handling code.
    346                 tf_gvSelectedPoint->dragged = true;
    347 
    348 
    349                 ControlPoint* cur=tf_pointList;
    350                 while (cur->next!=0){
    351                         cur=cur->next;
    352                 }
    353 
    354                 //update x, y values for selected point
    355                 if (tf_gvSelectedPoint!=tf_pointList && tf_gvSelectedPoint!=cur){
    356                         //not the first one, not the last one
    357                         tf_gvSelectedPoint->x = scX - gv_DX;
    358                         if (tf_gvSelectedPoint->x<=15){
    359                                 tf_gvSelectedPoint->x=16;
    360                         }
    361                         else if(tf_gvSelectedPoint->x>=15+tf_unitWidth){
    362                                 tf_gvSelectedPoint->x=15+tf_unitWidth-1;
    363                         }
    364                 }
    365                 tf_gvSelectedPoint->y = scY - gv_DY;
    366 
    367 
    368                 //Boundary conditions, so we can't drag point away from the screen.
    369                 if (scY - gv_DY > viewportVector[3])
    370                         tf_gvSelectedPoint->y = viewportVector[3] - 1;
    371                 //points can't be dragged below x axis
    372                 if (scY - gv_DY < 15)
    373                         tf_gvSelectedPoint->y = 15;
    374 
    375 
    376                 boundaryChecking();     
    377         }
    378 
    379         if (gvScaleDragging == true){
    380                 last_lv_tf_height_scale=lv_tf_height_scale;
    381                 lv_tf_height_scale += (scY - gv_lastY)*0.007;
    382                 gv_lastY = scY;
    383                 scalePointsY(scY - gv_lastY);
    384         }       
    385 
    386         WriteControlPoints();
    387 
    388         glutPostRedisplay();
     337    GLint viewportVector[4];
     338    double scX, scY;
     339
     340    glGetIntegerv(GL_VIEWPORT, viewportVector);
     341
     342    gv_X = scX = x;
     343    gv_Y = scY = viewportVector[3] - (GLint) y - 1;
     344
     345    if (tf_gvIsDragging == true && tf_gvSelectedPoint!=NULL){
     346        //Dragging handling code.
     347        tf_gvSelectedPoint->dragged = true;
     348
     349
     350        ControlPoint* cur=tf_pointList;
     351        while (cur->next!=0){
     352            cur=cur->next;
     353        }
     354
     355        //update x, y values for selected point
     356        if (tf_gvSelectedPoint!=tf_pointList && tf_gvSelectedPoint!=cur){
     357            //not the first one, not the last one
     358            tf_gvSelectedPoint->x = scX - gv_DX;
     359            if (tf_gvSelectedPoint->x<=15){
     360                tf_gvSelectedPoint->x=16;
     361            }
     362            else if(tf_gvSelectedPoint->x>=15+tf_unitWidth){
     363                tf_gvSelectedPoint->x=15+tf_unitWidth-1;
     364            }
     365        }
     366        tf_gvSelectedPoint->y = scY - gv_DY;
     367
     368
     369        //Boundary conditions, so we can't drag point away from the screen.
     370        if (scY - gv_DY > viewportVector[3])
     371            tf_gvSelectedPoint->y = viewportVector[3] - 1;
     372        //points can't be dragged below x axis
     373        if (scY - gv_DY < 15)
     374            tf_gvSelectedPoint->y = 15;
     375
     376
     377        boundaryChecking();     
     378    }
     379
     380    if (gvScaleDragging == true){
     381        last_lv_tf_height_scale=lv_tf_height_scale;
     382        lv_tf_height_scale += (scY - gv_lastY)*0.007;
     383        gv_lastY = scY;
     384        scalePointsY(scY - gv_lastY);
     385    }   
     386
     387    WriteControlPoints();
     388
     389    glutPostRedisplay();
    389390}
    390391
    391392void TransferFunctionGLUTWindow::tfIdle(){
    392         /*
    393         glutSetWindow(tfWinId);
    394         glutPostRedisplay();
    395 
    396         glutSetWindow(cmWinId);
    397         glutPostRedisplay();
    398 
    399         glutSetWindow(cpWinId);
    400         glutPostRedisplay();
    401         */
     393    /*
     394      glutSetWindow(tfWinId);
     395      glutPostRedisplay();
     396
     397      glutSetWindow(cmWinId);
     398      glutPostRedisplay();
     399
     400      glutSetWindow(cpWinId);
     401      glutPostRedisplay();
     402    */
    402403}
    403404
    404405void TransferFunctionGLUTWindow::tfKeyboard(unsigned char key, int x, int y){
    405         switch(key)
    406         {   
    407     case 'q':
    408                 exit(0);
    409                 break;   
    410     case 'a':
    411                 tf_pointEditState=1;
    412                 break;
    413     case 'd':
    414                 tf_pointEditState=2;
    415                 if (tf_gvSelectedPoint!=NULL){
    416                         removePoint(tf_gvSelectedPoint);
    417                         tf_gvSelectedPoint=0;
    418                         WriteControlPoints();
    419                         glutPostRedisplay();
    420                 }
    421                 tf_pointEditState=0;
    422                 break;   
    423         case 'p':
    424                 printPoints();
    425                 break;
    426         case 'w':
    427                 printInterpolation();
    428                 break;
    429         case 'r':
    430                 readHist();
    431                 break;
    432         case 'u':
    433                 dumpHist();
    434                 break;
    435         case 'i':
    436                 printHist();
    437                 break;
    438 
    439         default:
    440                 break;
    441     }   
     406    switch(key)
     407        {   
     408        case 'q':
     409            exit(0);
     410            break;   
     411        case 'a':
     412            tf_pointEditState=1;
     413            break;
     414        case 'd':
     415            tf_pointEditState=2;
     416            if (tf_gvSelectedPoint!=NULL){
     417                removePoint(tf_gvSelectedPoint);
     418                tf_gvSelectedPoint=0;
     419                WriteControlPoints();
     420                glutPostRedisplay();
     421            }
     422            tf_pointEditState=0;
     423            break;   
     424        case 'p':
     425            printPoints();
     426            break;
     427        case 'w':
     428            printInterpolation();
     429            break;
     430        case 'r':
     431            readHist();
     432            break;
     433        case 'u':
     434            dumpHist();
     435            break;
     436        case 'i':
     437            printHist();
     438            break;
     439
     440        default:
     441            break;
     442        }   
    442443}
    443444
     
    445446void  TransferFunctionGLUTWindow::tfReshape(int x, int y){
    446447
    447         //do not accept 0 value!!!
    448         if (x==0 || y==0)
    449                 return;
    450 
    451 
    452         last_lv_tf_height_scale=lv_tf_height_scale;
    453         lv_tf_height_scale=lv_tf_height_scale*y/winy;
    454 
    455         last_lv_tf_width_scale= lv_tf_width_scale;
    456         lv_tf_width_scale=lv_tf_width_scale*x/winx;
    457         scalePointsX();
    458 
    459         float xy_aspect;
    460 
    461         //lastWinx= winx;
    462         //lastWiny= winy;
    463 
    464         winx = x;
    465         winy = y;
    466 
    467         unitHeight = 0.8*y;
    468         tf_unitWidth = 0.95*x;
    469 
    470         xy_aspect = (float)x / (float)y;
    471         glViewport( 0, 0, x, y );
    472 
    473         glMatrixMode( GL_PROJECTION );
    474         glLoadIdentity();
    475 
    476         //Set up most convenient projection
    477         gluOrtho2D(0, x, 0, y);
    478         glutPostRedisplay();
     448    //do not accept 0 value!!!
     449    if (x==0 || y==0)
     450        return;
     451
     452
     453    last_lv_tf_height_scale=lv_tf_height_scale;
     454    lv_tf_height_scale=lv_tf_height_scale*y/winy;
     455
     456    last_lv_tf_width_scale= lv_tf_width_scale;
     457    lv_tf_width_scale=lv_tf_width_scale*x/winx;
     458    scalePointsX();
     459
     460    float xy_aspect;
     461
     462    //lastWinx= winx;
     463    //lastWiny= winy;
     464
     465    winx = x;
     466    winy = y;
     467
     468    unitHeight = 0.8*y;
     469    tf_unitWidth = 0.95*x;
     470
     471    xy_aspect = (float)x / (float)y;
     472    glViewport( 0, 0, x, y );
     473
     474    glMatrixMode( GL_PROJECTION );
     475    glLoadIdentity();
     476
     477    //Set up most convenient projection
     478    gluOrtho2D(0, x, 0, y);
     479    glutPostRedisplay();
    479480}
    480481
     
    485486void  TransferFunctionGLUTWindow::ReadControlPoints()
    486487{
    487         //read the live variables and update the points.
     488    //read the live variables and update the points.
    488489}
    489490
     
    493494void TransferFunctionGLUTWindow::WriteControlPoints()
    494495{
    495        
    496         //special case: all points have the same y coordinates
    497         double rangeY=0;
    498         float unitY=0;
    499         float unitX=0;
    500         double maxY=tf_pointList->y;
    501         double maxX=tf_pointList->x;
    502 
    503         //special case: all points have the same y coordinates,generate an error
    504         ControlPoint* cur=tf_pointList;
    505         int allSameY=1;
    506         int y=tf_pointList->y;
    507         while (cur!=0){
    508                 if (cur->y!=y){
    509                         allSameY=0;
    510                         break;
    511                 }
    512                 cur=cur->next;
    513         }
    514 
    515        
    516         if (allSameY==1){
    517                 int i=0;
    518                 for(i=0;i<numOfOutput;i++){
    519                         //output[i]=0;
    520                         color_table[i][3]=0;
    521                 }               
    522                 return;
    523         }
    524 
    525         //get max Y
    526         cur=tf_pointList;
    527         while (cur!=0){
    528                 if (cur->y>maxY){
    529                         maxY=cur->y;
    530                 }
    531                 if (cur->x>maxX){
    532                         maxX=cur->x;
    533                 }
    534                 cur=cur->next;
    535         }
    536 
    537 
    538         //get min Y
    539         cur=tf_pointList;
    540         double minY=maxY;
    541         while (cur!=0){
    542                 if (cur->y<minY){
    543                         minY=cur->y;
    544                 }
    545                 cur=cur->next;
    546         }
    547 
    548         //range of Y
    549         rangeY=maxY-minY;
    550 
    551         tfMaximum = ((float) rangeY) / (unitHeight/lv_tf_height_scale);
    552 
    553         //unit to map [0,1] to [0, rangeY]
    554         unitY=1/(float)rangeY;
    555         unitX=(float)(maxX-15)/(float)numOfOutput;
    556 
    557         cur=tf_pointList;
    558         ControlPoint* next=cur->next;
    559         double x1=cur->x;
    560         double y1=(cur->y)-minY;
    561         double x2=next->x;
    562         double y2=(next->y)-minY;
    563         float slope=((float)(y2-y1))/((float)(x2-x1));
    564         float t;  //t=(x-a)/(b-a)
    565 
    566         int i=0;
    567         for(i=0;i<numOfOutput;i++){
    568                
    569                 if (unitX*i+15>x2){
    570                         //go to next line segment
    571                         cur=next;
    572                         next=next->next;
    573                         if (next==0){
    574                                 return;
    575                         }
    576                         x1=cur->x;
    577                         y1=cur->y-minY;
    578                         x2=next->x;
    579                         y2=next->y-minY;
    580 
    581                         slope=((float)(y2-y1))/((float)(x2-x1));
    582                 }
    583                 t=((float)(unitX*i+15-x1))/((float)(x2-x1));
    584                 //output[i] = (y1+t*(y2-y1))/rangeY;
    585                 color_table[i][3]=control_point_scale*(y1+t*(y2-y1))/rangeY;
    586         }
    587 
    588         update_tf_texture();
    589         glutSetWindow(transferFunctionWindow);
    590 }       
     496       
     497    //special case: all points have the same y coordinates
     498    double rangeY=0;
     499    float unitY=0;
     500    float unitX=0;
     501    double maxY=tf_pointList->y;
     502    double maxX=tf_pointList->x;
     503
     504    //special case: all points have the same y coordinates,generate an error
     505    ControlPoint* cur=tf_pointList;
     506    int allSameY=1;
     507    int y=tf_pointList->y;
     508    while (cur!=0){
     509        if (cur->y!=y){
     510            allSameY=0;
     511            break;
     512        }
     513        cur=cur->next;
     514    }
     515
     516       
     517    if (allSameY==1){
     518        int i=0;
     519        for(i=0;i<numOfOutput;i++){
     520            //output[i]=0;
     521            color_table[i][3]=0;
     522        }               
     523        return;
     524    }
     525
     526    //get max Y
     527    cur=tf_pointList;
     528    while (cur!=0){
     529        if (cur->y>maxY){
     530            maxY=cur->y;
     531        }
     532        if (cur->x>maxX){
     533            maxX=cur->x;
     534        }
     535        cur=cur->next;
     536    }
     537
     538
     539    //get min Y
     540    cur=tf_pointList;
     541    double minY=maxY;
     542    while (cur!=0){
     543        if (cur->y<minY){
     544            minY=cur->y;
     545        }
     546        cur=cur->next;
     547    }
     548
     549    //range of Y
     550    rangeY=maxY-minY;
     551
     552    tfMaximum = ((float) rangeY) / (unitHeight/lv_tf_height_scale);
     553
     554    //unit to map [0,1] to [0, rangeY]
     555    unitY=1/(float)rangeY;
     556    unitX=(float)(maxX-15)/(float)numOfOutput;
     557
     558    cur=tf_pointList;
     559    ControlPoint* next=cur->next;
     560    double x1=cur->x;
     561    double y1=(cur->y)-minY;
     562    double x2=next->x;
     563    double y2=(next->y)-minY;
     564    float slope=((float)(y2-y1))/((float)(x2-x1));
     565    float t;  //t=(x-a)/(b-a)
     566
     567    int i=0;
     568    for(i=0;i<numOfOutput;i++){
     569               
     570        if (unitX*i+15>x2){
     571            //go to next line segment
     572            cur=next;
     573            next=next->next;
     574            if (next==0){
     575                return;
     576            }
     577            x1=cur->x;
     578            y1=cur->y-minY;
     579            x2=next->x;
     580            y2=next->y-minY;
     581
     582            slope=((float)(y2-y1))/((float)(x2-x1));
     583        }
     584        t=((float)(unitX*i+15-x1))/((float)(x2-x1));
     585        //output[i] = (y1+t*(y2-y1))/rangeY;
     586        color_table[i][3]=control_point_scale*(y1+t*(y2-y1))/rangeY;
     587    }
     588
     589    update_tf_texture();
     590    glutSetWindow(transferFunctionWindow);
     591}       
    591592
    592593
     
    594595
    595596void TransferFunctionGLUTWindow::printInterpolation(){
    596         WriteControlPoints();
    597         int i=0;
    598         for(i=0;i<numOfOutput;i++){
    599                 //printf("%f, ",output[i]);
    600                 fprintf(stderr, "%f, ",color_table[i][3]);
    601         }
    602         fprintf(stderr, "\n");
     597    WriteControlPoints();
     598    int i=0;
     599    for(i=0;i<numOfOutput;i++){
     600        //printf("%f, ",output[i]);
     601        fprintf(stderr, "%f, ",color_table[i][3]);
     602    }
     603    fprintf(stderr, "\n");
    603604}
    604605
     
    606607
    607608//sort all controlpoints by bubble sort
    608 void TransferFunctionGLUTWindow::sortPoints(){
    609         if (tf_numOfPoints==0)
    610                 return;
    611 
    612         //bubble-sort list in ascending order by X
    613         ControlPoint* cur;
    614         ControlPoint* pre;
    615         ControlPoint* a;
    616         ControlPoint* b;
    617         cur=tf_pointList;
    618 
    619         int i;
    620         for(i=0;i<tf_numOfPoints-1;i++){
    621                 while (cur->next!=NULL){
    622                         if (cur->x > cur->next->x){
    623                                 if (cur==tf_pointList){
    624                                         //first node
    625                                         a=tf_pointList;
    626                                         b=tf_pointList->next;
    627                                         a->next=b->next;
    628                                         b->next=a;
    629                                         tf_pointList=b;
    630                                         cur=tf_pointList;
    631                                 }
    632                                 else {
    633                                         a=cur;
    634                                         b=cur->next;
    635                                         a->next=b->next;
    636                                         b->next=a;
    637                                         pre->next=b;
    638                                         cur=b;
    639                                 }
    640                         }
    641                         pre=cur;
    642                         cur=cur->next;
    643                 }
    644                 cur=tf_pointList;
    645         }
    646         WriteControlPoints();
    647 }
    648 
    649 
    650 ControlPoint* TransferFunctionGLUTWindow::addPoint(double x, double y){
    651         //check if x is between first and last point
    652         //if not return without adding point
    653         ControlPoint* last=tf_pointList;
    654         while (last->next!=0){
    655                 last=last->next;
    656         }
    657         if (x<15 || x>last->x || y<15){
    658                 return tf_pointList;
    659         }
    660 
    661 
    662        
    663        
    664         ControlPoint* cur=tf_pointList;
    665         ControlPoint* pre=cur;
    666 
    667 
    668         //if x is the first or last. Don't add, but update.
    669         if(x==cur->x){
    670                 cur->y=y;
    671                 return tf_pointList;
    672         }
    673         else if(x==last->x){
    674                 last->y=y;
    675                 return last;
    676         }
    677 
    678 
    679         while (cur->next!=0){
    680                 if (x<=cur->x){
    681                         ControlPoint* newPoint=new ControlPoint(x,y);
    682                         if (cur==tf_pointList){
    683                                 newPoint->next=tf_pointList;
    684                                 tf_pointList=newPoint;
    685                         }
    686                         else{
    687                                 newPoint->next=pre->next;
    688                                 pre->next=newPoint;
    689                         }
    690                         tf_numOfPoints++;
    691                         //map->addKey(x, new Color(0,0,0));
    692                         return newPoint;
    693                 }
    694                 pre=cur;
    695                 cur=cur->next; 
    696         }
    697 
    698         if (x<=cur->x){
    699                 ControlPoint* newPoint=new ControlPoint(x,y);
    700                 newPoint->next=pre->next;
    701                 pre->next=newPoint;
    702                 tf_numOfPoints++;
    703                 //map->addKey(x, new Color(0,0,0));
    704                 return newPoint;
    705         }
    706         else {
    707                 cur->next=new ControlPoint(x,y);
    708                 tf_numOfPoints++;
    709                 //map->addKey(x, new Color(0,0,0));
    710                 return cur->next;
    711         }
    712 }
    713 
    714 
    715 
    716 void TransferFunctionGLUTWindow::removePoint(void* ptr){
    717         if (tf_numOfPoints==0)
    718                 return;
    719 
    720         ControlPoint* last=tf_pointList;
    721         while (last->next!=0){
    722                 last=last->next;
    723         }
    724 
    725         if (ptr==tf_pointList || ptr==last){
    726                 /*
    727                 if (tf_numOfPoints=1){
    728                         delete(ptr);
    729                         tf_pointList=0;
    730                 }
    731                 else{
    732                         tf_pointList=tf_pointList->next;
    733                         delete(ptr);
    734                 }
    735                 */
    736                 return;
    737         }
    738         else {
    739                 //find the previous point
    740                 ControlPoint* cur=tf_pointList;
    741                 ControlPoint* pre=cur;
    742                 while (cur->next!=0){
    743                         if (cur==ptr){
    744                                 break;
    745                         }
    746                         pre=cur;
    747                         cur=cur->next; 
    748                 }
    749                 pre->next=cur->next;
    750                 map->removeKey(cur->x);
    751                 delete(cur);
    752         }
    753         tf_numOfPoints--;
     609void
     610TransferFunctionGLUTWindow::sortPoints()
     611{
     612    if (tf_numOfPoints==0)
     613        return;
     614
     615    //bubble-sort list in ascending order by X
     616    ControlPoint* cur;
     617    ControlPoint* pre;
     618    ControlPoint* a;
     619    ControlPoint* b;
     620    cur=tf_pointList;
     621
     622    int i;
     623    pre = NULL;                 /* Suppress compiler warning */
     624    for(i=0;i<tf_numOfPoints-1;i++){
     625        while (cur->next!=NULL){
     626            if (cur->x > cur->next->x){
     627                if (cur==tf_pointList){
     628                    //first node
     629                    a=tf_pointList;
     630                    b=tf_pointList->next;
     631                    a->next=b->next;
     632                    b->next=a;
     633                    tf_pointList=b;
     634                    cur=tf_pointList;
     635                } else {
     636                    a=cur;
     637                    b=cur->next;
     638                    a->next=b->next;
     639                    b->next=a;
     640                    pre->next=b;
     641                    cur=b;
     642                }
     643            }
     644            pre=cur;
     645            cur=cur->next;
     646        }
     647        cur=tf_pointList;
     648    }
     649    WriteControlPoints();
     650}
     651
     652
     653ControlPoint*
     654TransferFunctionGLUTWindow::addPoint(double x, double y)
     655{
     656    //check if x is between first and last point
     657    //if not return without adding point
     658    ControlPoint* last=tf_pointList;
     659    while (last->next!=0){
     660        last=last->next;
     661    }
     662    if (x<15 || x>last->x || y<15){
     663        return tf_pointList;
     664    }
     665
     666    ControlPoint* cur=tf_pointList;
     667    ControlPoint* pre=cur;
     668
     669    //if x is the first or last. Don't add, but update.
     670    if(x==cur->x) {
     671        cur->y=y;
     672        return tf_pointList;
     673    } else if(x==last->x) {
     674        last->y=y;
     675        return last;
     676    }
     677
     678    while (cur->next!=0) {
     679        if (x<=cur->x){
     680            ControlPoint* newPoint=new ControlPoint(x,y);
     681            if (cur==tf_pointList) {
     682                newPoint->next=tf_pointList;
     683                tf_pointList=newPoint;
     684            } else {
     685                newPoint->next=pre->next;
     686                pre->next=newPoint;
     687            }
     688            tf_numOfPoints++;
     689            //map->addKey(x, new Color(0,0,0));
     690            return newPoint;
     691        }
     692        pre=cur;
     693        cur=cur->next; 
     694    }
     695
     696    if (x<=cur->x) {
     697        ControlPoint* newPoint=new ControlPoint(x,y);
     698        newPoint->next=pre->next;
     699        pre->next=newPoint;
     700        tf_numOfPoints++;
     701        //map->addKey(x, new Color(0,0,0));
     702        return newPoint;
     703    } else {
     704        cur->next=new ControlPoint(x,y);
     705        tf_numOfPoints++;
     706        //map->addKey(x, new Color(0,0,0));
     707        return cur->next;
     708    }
     709}
     710
     711
     712
     713void
     714TransferFunctionGLUTWindow::removePoint(void* ptr)
     715{
     716    if (tf_numOfPoints==0)
     717        return;
     718
     719    ControlPoint* last=tf_pointList;
     720    while (last->next!=0){
     721        last=last->next;
     722    }
     723
     724    if (ptr==tf_pointList || ptr==last){
     725        /*
     726          if (tf_numOfPoints=1){
     727          delete(ptr);
     728          tf_pointList=0;
     729          }
     730          else{
     731          tf_pointList=tf_pointList->next;
     732          delete(ptr);
     733          }
     734        */
     735        return;
     736    } else {
     737        //find the previous point
     738        ControlPoint* cur=tf_pointList;
     739        ControlPoint* pre=cur;
     740        while (cur->next!=0){
     741            if (cur==ptr) {
     742                break;
     743            }
     744            pre=cur;
     745            cur=cur->next;     
     746        }
     747        pre->next=cur->next;
     748        map->removeKey(cur->x);
     749        delete(cur);
     750    }
     751    tf_numOfPoints--;
    754752}
    755753
     
    757755//remove all points but the two initial points
    758756void TransferFunctionGLUTWindow::cleanUpPoints(){
    759         if (tf_numOfPoints<2)
    760                 return;
    761 
    762         ControlPoint* cur=tf_pointList;
    763 
    764         if (tf_numOfPoints==2){
    765                 cur->x=15;
    766                 cur->y=15;
    767                
    768                 cur->next->x=15+tf_unitWidth;
    769                 cur->next->y=15;
    770                 return;
    771         }
    772 
    773 
    774         cur=tf_pointList->next;
    775         ControlPoint* pre=tf_pointList;
    776 
    777         while (cur->next!=0){
    778                 if(pre!=tf_pointList)
    779                         delete(pre);
    780                 pre=cur;
    781                 cur=cur->next;
    782         }
    783 
    784         delete(pre);
    785         tf_pointList->next=cur;
    786 
    787 
    788         tf_pointList->x=15;
    789         tf_pointList->y=15;
    790        
    791         tf_pointList->next->x=15+tf_unitWidth;
    792         tf_pointList->next->y=15;
    793 
    794         tf_numOfPoints=2;
    795         TransferFunctionGLUTWindow::WriteControlPoints();
     757    if (tf_numOfPoints<2)
     758        return;
     759
     760    ControlPoint* cur=tf_pointList;
     761
     762    if (tf_numOfPoints==2){
     763        cur->x=15;
     764        cur->y=15;
     765               
     766        cur->next->x=15+tf_unitWidth;
     767        cur->next->y=15;
     768        return;
     769    }
     770
     771
     772    cur=tf_pointList->next;
     773    ControlPoint* pre=tf_pointList;
     774
     775    while (cur->next!=0){
     776        if(pre!=tf_pointList)
     777            delete(pre);
     778        pre=cur;
     779        cur=cur->next;
     780    }
     781
     782    delete(pre);
     783    tf_pointList->next=cur;
     784
     785
     786    tf_pointList->x=15;
     787    tf_pointList->y=15;
     788       
     789    tf_pointList->next->x=15+tf_unitWidth;
     790    tf_pointList->next->y=15;
     791
     792    tf_numOfPoints=2;
     793    TransferFunctionGLUTWindow::WriteControlPoints();
    796794}
    797795
     
    801799
    802800//debugging: print out all points
    803 void TransferFunctionGLUTWindow::printPoints(){
    804         ControlPoint* cur=tf_pointList;
    805         fprintf(stderr, "********************\n");
    806         fprintf(stderr, "Total points %d \n", tf_numOfPoints);
    807 
    808         if (tf_numOfPoints==0)
    809                 return;
    810 
    811         while (cur->next!=0){
    812                 fprintf(stderr, "(%g,%g)\n", cur->x, cur->y);
    813                 cur=cur->next;
    814         }
    815         fprintf(stderr, "(%g,%g)\n", cur->x, cur->y);
    816         fprintf(stderr, "********************\n");
    817 }
    818 
    819 
    820 ControlPoint* TransferFunctionGLUTWindow::boundaryChecking(){
    821         assert(tf_gvSelectedPoint!=0);
    822         assert(tf_pointList!=0);
    823         //find left and right neighbor in the list
    824         //If point is dragged passing the neighbor, the points are reordered.
    825 
    826         ControlPoint* left=tf_pointList;
    827         ControlPoint* right=tf_pointList;
    828 
    829         while (right!=tf_gvSelectedPoint){
    830                 left=right;
    831                 right=right->next;
    832         }
    833 
    834         //selected point is not the right-end point
    835         if (right->next!=0){
    836                 right=right->next;
    837                 if (tf_gvSelectedPoint->x>right->x){
    838                         sortPoints();
    839                 }
    840         }
    841 
    842         //selected point is not the left-end point
    843         if (left!=tf_gvSelectedPoint) {
    844                 if (tf_gvSelectedPoint->x<left->x){
    845                         sortPoints();
    846                 }
    847         }
    848 
    849         return tf_gvSelectedPoint;
    850 }
    851 
    852 
    853 void TransferFunctionGLUTWindow::scalePointsY(int offset){
    854         if (tf_numOfPoints<2)
    855                 return;
    856 
    857         ControlPoint* cur=tf_pointList;
    858         while (cur!=0){
    859                 cur->y=((cur->y)-offset-15)*((float)last_lv_tf_height_scale/(float)lv_tf_height_scale)+15;
    860                 cur=cur->next;
    861         }               
    862         WriteControlPoints();
    863 }
    864 
    865 void TransferFunctionGLUTWindow::scalePointsX(){
    866         ControlPoint* cur=tf_pointList;
    867         while (cur!=0){
    868                 cur->x=(cur->x-15)*((float)lv_tf_width_scale/(float)last_lv_tf_width_scale)+15;
    869                 cur=cur->next;
    870         }
    871         WriteControlPoints();
    872 }
    873 
    874 
    875 void TransferFunctionGLUTWindow::dumpHist(){
    876 /*
    877         FILE* fp = fopen("HistFile", "wt");
    878 
    879         if(!fp)
    880                 return; //error
    881 
    882         int i=0;
    883         for (i=0; i<Hist.range; i++){
    884                 fprintf(fp, "%d\n", Hist.count[i]);     
    885         }
    886         printf("Histogram dumped.\n");
    887         fclose(fp);
    888 */
    889 }
    890 
    891 
    892 void TransferFunctionGLUTWindow::readHist(){
    893 /*
    894         FILE* fp = fopen("HistFile", "rt");
    895 
    896         if(!fp)
    897                 return; //error
    898 
    899         char buf[100];           //buffer to store one line from file
    900         char* token;             //pointer to token in the buffer
    901         char seps[]="\n";
    902 
    903         unsigned long value;
    904 
    905         int counter=0;
    906 
    907         while(true){
    908                 //read file
    909                 if (fgets(buf, 100, fp)==NULL)
    910                         break;
    911                 token=strtok(buf, seps);
    912                 value=atof(token);
    913                 Hist.count[counter]=value;
    914                 counter++;
    915         }
    916         printf("Histgram loaded. Total number of values:%d\n", counter);
    917         tfDisplay();
    918         fclose(fp);
    919 */
     801void
     802TransferFunctionGLUTWindow::printPoints()
     803{
     804    ControlPoint* cur=tf_pointList;
     805    fprintf(stderr, "********************\n");
     806    fprintf(stderr, "Total points %d \n", tf_numOfPoints);
     807
     808    if (tf_numOfPoints==0)
     809        return;
     810
     811    while (cur->next!=0){
     812        fprintf(stderr, "(%g,%g)\n", cur->x, cur->y);
     813        cur=cur->next;
     814    }
     815    fprintf(stderr, "(%g,%g)\n", cur->x, cur->y);
     816    fprintf(stderr, "********************\n");
     817}
     818
     819
     820ControlPoint*
     821TransferFunctionGLUTWindow::boundaryChecking()
     822{
     823    assert(tf_gvSelectedPoint!=0);
     824    assert(tf_pointList!=0);
     825    //find left and right neighbor in the list
     826    //If point is dragged passing the neighbor, the points are reordered.
     827
     828    ControlPoint* left=tf_pointList;
     829    ControlPoint* right=tf_pointList;
     830
     831    while (right!=tf_gvSelectedPoint){
     832        left=right;
     833        right=right->next;
     834    }
     835
     836    //selected point is not the right-end point
     837    if (right->next!=0){
     838        right=right->next;
     839        if (tf_gvSelectedPoint->x>right->x){
     840            sortPoints();
     841        }
     842    }
     843
     844    //selected point is not the left-end point
     845    if (left!=tf_gvSelectedPoint) {
     846        if (tf_gvSelectedPoint->x<left->x){
     847            sortPoints();
     848        }
     849    }
     850
     851    return tf_gvSelectedPoint;
     852}
     853
     854
     855void
     856TransferFunctionGLUTWindow::scalePointsY(int offset)
     857{
     858    if (tf_numOfPoints<2)
     859        return;
     860
     861    ControlPoint* cur=tf_pointList;
     862    while (cur!=0){
     863        cur->y=((cur->y)-offset-15)*((float)last_lv_tf_height_scale/(float)lv_tf_height_scale)+15;
     864        cur=cur->next;
     865    }           
     866    WriteControlPoints();
     867}
     868
     869void
     870TransferFunctionGLUTWindow::scalePointsX()
     871{
     872    ControlPoint* cur=tf_pointList;
     873    while (cur!=0){
     874        cur->x=(cur->x-15)*((float)lv_tf_width_scale/(float)last_lv_tf_width_scale)+15;
     875        cur=cur->next;
     876    }
     877    WriteControlPoints();
     878}
     879
     880
     881void
     882TransferFunctionGLUTWindow::dumpHist()
     883{
     884    /*
     885      FILE* fp = fopen("HistFile", "wt");
     886
     887      if(!fp)
     888      return; //error
     889
     890      int i=0;
     891      for (i=0; i<Hist.range; i++){
     892      fprintf(fp, "%d\n", Hist.count[i]);       
     893      }
     894      printf("Histogram dumped.\n");
     895      fclose(fp);
     896    */
     897}
     898
     899
     900void
     901TransferFunctionGLUTWindow::readHist()
     902{
     903    /*
     904      FILE* fp = fopen("HistFile", "rt");
     905
     906      if(!fp)
     907      return; //error
     908
     909      char buf[100];             //buffer to store one line from file
     910      char* token;               //pointer to token in the buffer
     911      char seps[]="\n";
     912
     913      unsigned long value;
     914
     915      int counter=0;
     916
     917      while(true){
     918      //read file
     919      if (fgets(buf, 100, fp)==NULL)
     920      break;
     921      token=strtok(buf, seps);
     922      value=atof(token);
     923      Hist.count[counter]=value;
     924      counter++;
     925      }
     926      printf("Histgram loaded. Total number of values:%d\n", counter);
     927      tfDisplay();
     928      fclose(fp);
     929    */
    920930}
    921931
    922932//output histogram to console
    923 void TransferFunctionGLUTWindow::printHist(){
    924         int i=0;
    925        
    926         for(int j=0; j<4; j++){
    927                 for (i=0; i<Hist[j].range; i++){
    928                         fprintf(stderr, "%ld\t", Hist[j].count[i]);     
    929                 }
    930                 fprintf(stderr, "\n\n");
    931         }
    932 
    933 /*
    934         for (i=0; i<Hist_p.range; i++){
    935                 printf("%ld\t", Hist_p.count[i]);       
    936         }
    937         printf("\n\n");
    938 
    939         for (i=0; i<Hist_d.range; i++){
    940                 printf("%ld\t", Hist_d.count[i]);       
    941         }
    942         printf("\n\n");
    943 
    944         for (i=0; i<Hist_ss.range; i++){
    945                 printf("%ld\t", Hist_ss.count[i]);     
    946         }
    947         printf("\n\n");
    948 */
    949 }
    950 
    951 
    952 void TransferFunctionGLUTWindow::plotHist(){
    953 
    954         for(int j=0; j<4; j++){
    955                 Histogram cur = Hist[j];
    956 
    957                 float unitX=(float)tf_unitWidth/(float)(cur.range-1);
    958                 float unitY;
    959                 unsigned long maxY;
    960                 unsigned long minY;
    961                 double rangeY;
    962                
    963                 float result[256];
    964                
    965                 //get max Y
    966                 maxY=0;
    967                 int i=0;
    968                 for (i=0; i<cur.range; i++){
    969                         if (cur.count[i]>maxY)
    970                                 maxY=cur.count[i];
    971                 }
    972                 //printf("maxY=%d\n", maxY);
    973 
    974 
    975                 //get min Y
    976                 minY=maxY;
    977                 for (i=0; i<cur.range; i++){
    978                         if (cur.count[i]<minY)
    979                                 minY=cur.count[i];
    980                 }
    981                 //printf("minY=%d\n", minY);
    982 
    983                 //rangeY=(double)log(maxY-minY);
    984                 //rangeY=(double)(maxY-minY);
    985                 rangeY=(float)log(float(maxY - minY));
    986                 unitY=(float)(winy-20)/(float)rangeY;
    987 
    988                 //reduce all values with log then by minY
    989                 for (i=0; i<cur.range; i++){
    990                         if (cur.count[i]==0)
    991                                 result[i]=0;
    992                         else
    993                                 //result[i]=log((double)(cur.count[i]));
    994                                 //result[i]=double(cur.count[i]);
    995                                 //result[i]=log((float)(cur.count[i]))-log((float)minY);
    996                                 result[i]=log((float)(cur.count[i])-(float)minY);       
    997                 }
    998 
    999 
    1000                 //draw points
    1001                 glColor3d(0, 0, 1);
    1002                 glBegin(GL_LINES);
    1003                         for (i=0; i<cur.range-1; i++){
    1004                                 glVertex2f(15+i*unitX, 15+result[i]*unitY);
    1005                                 glVertex2f(15+(i+1)*unitX, 15+result[i+1]*unitY);
    1006                         }
    1007                 glEnd();
    1008                 glColor3d(0, 0, 0);     
    1009 
    1010         }
    1011 }
     933void TransferFunctionGLUTWindow::printHist()
     934{
     935    int i=0;
     936       
     937    for(int j=0; j<4; j++){
     938        for (i=0; i<Hist[j].range; i++){
     939            fprintf(stderr, "%ld\t", Hist[j].count[i]);
     940        }
     941        fprintf(stderr, "\n\n");
     942    }
     943
     944    /*
     945      for (i=0; i<Hist_p.range; i++){
     946      printf("%ld\t", Hist_p.count[i]);
     947      }
     948      printf("\n\n");
     949
     950      for (i=0; i<Hist_d.range; i++){
     951      printf("%ld\t", Hist_d.count[i]);
     952      }
     953      printf("\n\n");
     954
     955      for (i=0; i<Hist_ss.range; i++){
     956      printf("%ld\t", Hist_ss.count[i]);       
     957      }
     958      printf("\n\n");
     959    */
     960}
     961
     962
     963void
     964TransferFunctionGLUTWindow::plotHist()
     965{
     966    for(int j=0; j<4; j++){
     967        Histogram cur = Hist[j];
     968
     969        float unitX=(float)tf_unitWidth/(float)(cur.range-1);
     970        float unitY;
     971        unsigned long maxY;
     972        unsigned long minY;
     973        double rangeY;
     974               
     975        float result[256];
     976               
     977        //get max Y
     978        maxY=0;
     979        int i=0;
     980        for (i=0; i<cur.range; i++){
     981            if (cur.count[i]>maxY)
     982                maxY=cur.count[i];
     983        }
     984        //printf("maxY=%d\n", maxY);
     985
     986
     987        //get min Y
     988        minY=maxY;
     989        for (i=0; i<cur.range; i++){
     990            if (cur.count[i]<minY)
     991                minY=cur.count[i];
     992        }
     993        //printf("minY=%d\n", minY);
     994
     995        //rangeY=(double)log(maxY-minY);
     996        //rangeY=(double)(maxY-minY);
     997        rangeY=(float)log(float(maxY - minY));
     998        unitY=(float)(winy-20)/(float)rangeY;
     999
     1000        //reduce all values with log then by minY
     1001        for (i=0; i<cur.range; i++){
     1002            if (cur.count[i]==0)
     1003                result[i]=0;
     1004            else
     1005                //result[i]=log((double)(cur.count[i]));
     1006                //result[i]=double(cur.count[i]);
     1007                //result[i]=log((float)(cur.count[i]))-log((float)minY);
     1008                result[i]=log((float)(cur.count[i])-(float)minY);       
     1009        }
     1010
     1011
     1012        //draw points
     1013        glColor3d(0, 0, 1);
     1014        glBegin(GL_LINES);
     1015        for (i=0; i<cur.range-1; i++){
     1016            glVertex2f(15+i*unitX, 15+result[i]*unitY);
     1017            glVertex2f(15+(i+1)*unitX, 15+result[i+1]*unitY);
     1018        }
     1019        glEnd();
     1020        glColor3d(0, 0, 0);     
     1021
     1022    }
     1023}
Note: See TracChangeset for help on using the changeset viewer.