tfgeometry1d.cpp 5.81 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
29
#include "cgt/assert.h"
#include "cgt/texture.h"
#include "cgt/tgt_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
43
44
45
46
        : _keyPoints(keyPoints)
    {
        std::sort(_keyPoints.begin(), _keyPoints.end());
    }

schultezub's avatar
schultezub committed
47
    TFGeometry1D::~TFGeometry1D() {
48
49
50

    }

51
52
53
54
    TFGeometry1D* TFGeometry1D::clone() const {
        return new TFGeometry1D(_keyPoints);
    }

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

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

64
65
66
67
        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) {
68
            glColor4ub(a->_color.r, a->_color.g, a->_color.b, 144);
69
70
71
            float y = static_cast<float>(a->_color.a) / 255.f;
            glVertex2f(a->_position, 0.f);
            glVertex2f(a->_position, y);
72

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

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

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

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

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

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

101
102
    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");
103
104
105
106

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

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

114
115
116
    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).");
117
118
119

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

125
126
    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);
127
128
    }

129
130
    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].");
131
132

        std::vector<KeyPoint> keyPoints;
133
134
135
136
        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)));
137
138
139
        return new TFGeometry1D(keyPoints);
    }

140
}