tfgeometry1d.cpp 6.85 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "tfgeometry1d.h"
26

27
28
#include "cgt/assert.h"
#include "cgt/texture.h"
29
#include "cgt/cgt_math.h"
30

31
32
#include "core/datastructures/facegeometry.h"

33
34
#include <algorithm>

schultezub's avatar
schultezub committed
35
namespace campvis {
36

schultezub's avatar
schultezub committed
37
    bool operator< (const TFGeometry1D::KeyPoint& left, const TFGeometry1D::KeyPoint& right) {
38
39
40
        return left._position < right._position;
    }
    
schultezub's avatar
schultezub committed
41
    TFGeometry1D::TFGeometry1D(const std::vector<KeyPoint>& keyPoints)
42
        : _keyPoints(keyPoints)
43
44
        , _tfRenderFace(nullptr)
        , _tfEditorFace(nullptr)
45
46
47
48
    {
        std::sort(_keyPoints.begin(), _keyPoints.end());
    }

schultezub's avatar
schultezub committed
49
    TFGeometry1D::~TFGeometry1D() {
50
51
52

    }

53
54
55
56
    TFGeometry1D* TFGeometry1D::clone() const {
        return new TFGeometry1D(_keyPoints);
    }

schultezub's avatar
schultezub committed
57
    std::vector<TFGeometry1D::KeyPoint>& TFGeometry1D::getKeyPoints() {
58
59
60
        return _keyPoints;
    }

61
62
    void TFGeometry1D::renderIntoEditor() const {
        // TODO: get rid of intermediade mode?
63
64
65
        if (_keyPoints.size() < 2)
            return;

66
67
68
69
        glBegin(GL_QUADS);
        std::vector<KeyPoint>::const_iterator a = _keyPoints.begin();
        std::vector<KeyPoint>::const_iterator b = _keyPoints.begin()+1;
        for (/* already inited */; b != _keyPoints.end(); ++a, ++b) {
70
            glColor4ub(a->_color.r, a->_color.g, a->_color.b, 144);
71
72
73
            float y = static_cast<float>(a->_color.a) / 255.f;
            glVertex2f(a->_position, 0.f);
            glVertex2f(a->_position, y);
74

75
            glColor4ub(b->_color.r, b->_color.g, b->_color.b, 144);
76
77
78
            y = static_cast<float>(b->_color.a) / 255.f;
            glVertex2f(b->_position, y);
            glVertex2f(b->_position, 0.f);
79
        }
80
        glEnd();
81
82
    }

schultezub's avatar
schultezub committed
83
    void TFGeometry1D::render() const {
84
85
        if (_keyPoints.size() < 2)
            return;
86

87
        // TODO: regenerating these buffers each time is slow as hell
88
89
        std::vector<cgt::vec3> vertices;
        std::vector<cgt::vec4> colors;
90

91
        for (std::vector<KeyPoint>::const_iterator a = _keyPoints.begin(); a != _keyPoints.end(); ++a) {
92
93
            vertices.push_back(cgt::vec3(a->_position, 0.f, 0.f));
            vertices.push_back(cgt::vec3(a->_position, 1.f, 0.f));
94

95
96
            colors.push_back(cgt::vec4(a->_color) / 255.f);
            colors.push_back(cgt::vec4(a->_color) / 255.f);
97
        }
98

99
        FaceGeometry fg(vertices, std::vector<cgt::vec3>(), colors);
100
        fg.render(GL_TRIANGLE_STRIP);
101
    }
102

103
104
    TFGeometry1D* TFGeometry1D::createQuad(const cgt::vec2& interval, const cgt::col4& leftColor, const cgt::col4& rightColor) {
        cgtAssert(interval.x >= 0.f && interval.y <= 1.f, "Interval out of bounds");
105
106
107
108

        std::vector<KeyPoint> keyPoints;
        keyPoints.push_back(KeyPoint(interval.x, leftColor));
        keyPoints.push_back(KeyPoint(interval.y, rightColor));
schultezub's avatar
schultezub committed
109
        return new TFGeometry1D(keyPoints);
110
111
    }

112
113
    TFGeometry1D* TFGeometry1D::crateRamp(const cgt::vec2& interval, const cgt::col4& color) {
        return createQuad(interval, cgt::col4(color.xyz(), 0), cgt::col4(color.xyz(), 255));
114
    }
115

116
117
118
    TFGeometry1D* TFGeometry1D::createDivergingColorMap(const cgt::vec2& interval, const cgt::col4& leftColor, const cgt::col4& rightColor, float bias /*= 0.5f*/) {
        cgtAssert(interval.x >= 0.f && interval.y <= 1.f, "Interval out of bounds, must be in [0, 1].");
        cgtAssert(bias > 0.f && bias < 1.f, "Bias out of bounds, must be in (0, 1).");
119
120
121

        std::vector<KeyPoint> keyPoints;
        keyPoints.push_back(KeyPoint(interval.x, leftColor));
122
        keyPoints.push_back(KeyPoint(interval.x + (interval.y - interval.x) * bias, cgt::col4(255, 255, 255, 255)));
123
124
125
126
        keyPoints.push_back(KeyPoint(interval.y, rightColor));
        return new TFGeometry1D(keyPoints);
    }

127
128
    TFGeometry1D* TFGeometry1D::createColdHotColorMap(const cgt::vec2& interval /*= cgt::vec2(0.f, 1.f)*/) {
        return createDivergingColorMap(interval, cgt::col4(0, 0, 255, 255), cgt::col4(255, 0, 0, 255), 0.5f);
129
130
    }

131
132
    TFGeometry1D* TFGeometry1D::createHeatedBodyColorMap(const cgt::vec2& interval /*= cgt::vec2(0.f, 1.f)*/) {
        cgtAssert(interval.x >= 0.f && interval.y <= 1.f, "Interval out of bounds, must be in [0, 1].");
133
134

        std::vector<KeyPoint> keyPoints;
135
136
137
138
        keyPoints.push_back(KeyPoint(interval.x, cgt::col4(0, 0, 0, 255)));
        keyPoints.push_back(KeyPoint(interval.x + (interval.y - interval.x) * 0.35f, cgt::col4(224, 0, 0, 255)));
        keyPoints.push_back(KeyPoint(interval.x + (interval.y - interval.x) * 0.85f, cgt::col4(255, 255, 0, 255)));
        keyPoints.push_back(KeyPoint(interval.y, cgt::col4(255, 255, 255, 255)));
139
140
141
        return new TFGeometry1D(keyPoints);
    }

142
143
    void TFGeometry1D::addKeyPoint( float position, float alpha) {
        TFGeometry1D::KeyPoint kp(position, cgt::col4(255));
Hossain Mahmud's avatar
Hossain Mahmud committed
144
        cgt::col4 color(255);
145
146
        std::vector<TFGeometry1D::KeyPoint>::iterator lb = std::upper_bound(_keyPoints.begin(), _keyPoints.end(), kp);
        if (lb != _keyPoints.end()) {
Hossain Mahmud's avatar
Hossain Mahmud committed
147
            color = lb->_color;
148
149
        }
        else {
Hossain Mahmud's avatar
Hossain Mahmud committed
150
            color = _keyPoints.back()._color;
151
        }
Hossain Mahmud's avatar
Hossain Mahmud committed
152
153
        color.a = static_cast<uint8_t>(alpha * 255.f);
        addKeyPoint(position, color);
154
155
156
    }

    void TFGeometry1D::addKeyPoint( float position, const cgt::col4& color ) {
Hossain Mahmud's avatar
Hossain Mahmud committed
157
        TFGeometry1D::KeyPoint kp(position, color);
158
159
160
161
        std::vector<TFGeometry1D::KeyPoint>::iterator lb = std::upper_bound(_keyPoints.begin(), _keyPoints.end(), kp);
        _keyPoints.insert(lb, kp);
    }

162
163
164
165
    cgt::vec2 TFGeometry1D::getIntensityDomain() const {
        return cgt::vec2(_keyPoints.front()._position, _keyPoints.back()._position);
    }

166
}