File: ThresholdShader.cpp

package info (click to toggle)
mercator 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,008 kB
  • sloc: sh: 10,433; cpp: 4,482; makefile: 115
file content (186 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2003 Alistair Riddoch

#include <Mercator/ThresholdShader.h>

#include <Mercator/Segment.h>
#include <Mercator/Surface.h>

#include <cassert>

namespace Mercator {

const std::string HighShader::key_threshold("threshold");

const float HighShader::default_threshold = 1.f;

HighShader::HighShader(float threshold) : m_threshold(threshold)
{
}

HighShader::HighShader(const Parameters & params) : m_threshold(default_threshold)
{
    Parameters::const_iterator I = params.find(key_threshold);
    Parameters::const_iterator Iend = params.end();
    if (I != Iend) {
        m_threshold = I->second;
    }
}

HighShader::~HighShader()
{
}

bool HighShader::checkIntersect(const Segment & s) const
{
    if (s.getMax() > m_threshold) {
        return true;
    } else {
        return false;
    }
}

void HighShader::shade(Surface & s) const
{
    unsigned int channels = s.getChannels();
    assert(channels > 0);
    unsigned int colors = channels - 1;
    ColorT * data = s.getData();
    const float * height_data = s.getSegment().getPoints();
    if (height_data == 0) {
        std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
                  << std::endl << std::flush;
        return;
    }
    unsigned int size = s.getSegment().getSize();

    unsigned int count = size * size;
    int j = -1;
    for (unsigned int i = 0; i < count; ++i) {
        for (unsigned int k = 0; k < colors; ++k) {
            data[++j] = colorMax;
        }
        data[++j] = ((height_data[i] > m_threshold) ? colorMax : colorMin);
    }
}

const std::string LowShader::key_threshold("threshold");

const float LowShader::default_threshold = -1.f;

LowShader::LowShader(float threshold) : m_threshold(threshold)
{
}

LowShader::LowShader(const Parameters & params) : m_threshold(default_threshold)
{
    Parameters::const_iterator I = params.find(key_threshold);
    Parameters::const_iterator Iend = params.end();
    if (I != Iend) {
        m_threshold = I->second;
    }
}

LowShader::~LowShader()
{
}

bool LowShader::checkIntersect(const Segment & s) const
{
    if (s.getMin() < m_threshold) {
        return true;
    } else {
        return false;
    }
}

void LowShader::shade(Surface & s) const
{
    unsigned int channels = s.getChannels();
    assert(channels > 0);
    unsigned int colors = channels - 1;
    ColorT * data = s.getData();
    const float * height_data = s.getSegment().getPoints();
    if (height_data == 0) {
        std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
                  << std::endl << std::flush;
        return;
    }
    unsigned int size = s.getSegment().getSize();

    unsigned int count = size * size;
    int j = -1;
    for (unsigned int i = 0; i < count; ++i) {
        for (unsigned int k = 0; k < colors; ++k) {
            data[++j] = colorMax;
        }
        data[++j] = ((height_data[i] < m_threshold) ? colorMax : colorMin);
    }
}

const std::string BandShader::key_lowThreshold("lowThreshold");
const std::string BandShader::key_highThreshold("highThreshold");

const float BandShader::default_lowThreshold = -1.f;
const float BandShader::default_highThreshold = 1.f;

BandShader::BandShader(float low_threshold, float high_threshold) : 
    m_lowThreshold(low_threshold), m_highThreshold(high_threshold)
{
}

BandShader::BandShader(const Parameters & params) :
    m_lowThreshold(default_lowThreshold), m_highThreshold(default_highThreshold)
{
    Parameters::const_iterator I = params.find(key_lowThreshold);
    Parameters::const_iterator Iend = params.end();
    if (I != Iend) {
        m_lowThreshold = I->second;
    }
    I = params.find(key_highThreshold);
    if (I != Iend) {
        m_highThreshold = I->second;
    }
}

BandShader::~BandShader()
{
}

bool BandShader::checkIntersect(const Segment & s) const
{
    if ((s.getMin() < m_highThreshold) &&
        (s.getMax() > m_lowThreshold)) {
        return true;
    } else {
        return false;
    }
}

void BandShader::shade(Surface & s) const
{
    unsigned int channels = s.getChannels();
    assert(channels > 0);
    unsigned int colors = channels - 1;
    ColorT * data = s.getData();
    const float * height_data = s.getSegment().getPoints();
    if (height_data == 0) {
        std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
                  << std::endl << std::flush;
        return;
    }
    unsigned int size = s.getSegment().getSize();

    unsigned int count = size * size;
    int j = -1;
    for (unsigned int i = 0; i < count; ++i) {
        for (unsigned int k = 0; k < colors; ++k) {
            data[++j] = colorMax;
        }
        data[++j] = (((height_data[i] > m_lowThreshold) &&
                      (height_data[i] < m_highThreshold)) ? colorMax : colorMin);
    }
}

} // namespace Mercator