tfgeometry2d.cpp 4.78 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 
// ================================================================================================

#include "tfgeometry2d.h"

#include "tgt/assert.h"
#include "tgt/texture.h"
#include "tgt/tgt_math.h"

#include <algorithm>

namespace {
    tgt::col4 toCol(const tgt::vec4& c) {
        return tgt::col4(
            static_cast<uint8_t>(255 * c.r), 
            static_cast<uint8_t>(255 * c.g), 
            static_cast<uint8_t>(255 * c.b), 
            static_cast<uint8_t>(255 * c.a));
    }

    tgt::vec4 toVec(const tgt::col4& c) {
        return tgt::vec4(
            static_cast<float>(c.r) / 255.f, 
            static_cast<float>(c.g) / 255.f, 
            static_cast<float>(c.b) / 255.f, 
            static_cast<float>(c.a) / 255.f);
    }
}

schultezub's avatar
schultezub committed
51
namespace campvis {
52
53
54
55
56
57
58
59
60
61
62
63
    
    TFGeometry2D::TFGeometry2D(const std::vector<KeyPoint>& keyPoints)
        : _keyPoints(keyPoints)
        , _center(tgt::vec2::zero, tgt::col4(255))
    {
        computeCenterAndSortKeyPoints();
    }

    TFGeometry2D::~TFGeometry2D() {

    }

64
65
66
67
    TFGeometry2D* TFGeometry2D::clone() const {
        return new TFGeometry2D(_keyPoints);
    }

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    std::vector<TFGeometry2D::KeyPoint>& TFGeometry2D::getKeyPoints() {
        return _keyPoints;
    }

    void TFGeometry2D::render() const {
        // TODO: get rid of intermediade mode?
        if (_keyPoints.size() < 2)
            return;

        glBegin(GL_TRIANGLE_FAN);
        glColor4ubv(_center._color.elem);
        glVertex2fv(_center._position.elem);
        for (std::vector<KeyPoint>::const_iterator it = _keyPoints.begin(); it != _keyPoints.end(); ++it) {
            glColor4ubv(it->_color.elem);
            glVertex2fv(it->_position.elem);
        }
        glEnd();
    }

    TFGeometry2D* TFGeometry2D::createQuad(const tgt::vec2& ll, const tgt::vec2& ur, const tgt::col4& color) {
        tgtAssert(tgt::hand(tgt::greaterThanEqual(ll, tgt::vec2::zero)) && tgt::hand(tgt::lessThanEqual(ur, tgt::vec2(1.f))), "Interval out of bounds");
        tgtAssert(tgt::hand(tgt::lessThan(ll, ur)), "Lower left corner coordinates must be smaller than the upper right ones!");

        std::vector<KeyPoint> keyPoints;
        keyPoints.push_back(KeyPoint(ll, color));
        keyPoints.push_back(KeyPoint(tgt::vec2(ur.x, ll.y), color));
        keyPoints.push_back(KeyPoint(ur, color));
        keyPoints.push_back(KeyPoint(tgt::vec2(ll.x, ur.y), color));
        return new TFGeometry2D(keyPoints);
    }

    namespace {
        struct KeyPointSorter {
            KeyPointSorter(const tgt::vec2& center)
                : _center(center)
            {};

            bool operator() (const TFGeometry2D::KeyPoint& left, const TFGeometry2D::KeyPoint& right) {
                float angleLeft = atan2(left._position.y - _center.y, left._position.x - _center.x);
                float angleRight = atan2(right._position.y - _center.y, right._position.x - _center.x);
                return angleLeft < angleRight;
            };

            tgt::vec2 _center;
        };
    }

    void TFGeometry2D::computeCenterAndSortKeyPoints() {
        if (_keyPoints.empty())
            return;

        tgt::vec2 cPos(0.f);
        tgt::vec4 cCol(0.f);
        for (std::vector<KeyPoint>::const_iterator it = _keyPoints.begin(); it != _keyPoints.end(); ++it) {
            cPos += it->_position;
            cCol += toVec(it->_color);
        }
        cPos /= static_cast<float>(_keyPoints.size());
        cCol /= static_cast<float>(_keyPoints.size());
        _center = KeyPoint(cPos, toCol(cCol));

        std::sort(_keyPoints.begin(), _keyPoints.end(), KeyPointSorter(_center._position));
    }


133

134
}