DGtal 1.3.0
Loading...
Searching...
No Matches
TickedColorMap.ih
1/**
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 **/
16
17/**
18 * @file TickedColorMap.ih
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21 *
22 * @date 2015/04/06
23 *
24 * Implementation of inline methods defined in TickedColorMap.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
37
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
40
41
42template <typename Value, typename CMAP>
43inline
44DGtal::TickedColorMap<Value,CMAP>::TickedColorMap
45( const Value & amin,
46 const Value & amax,
47 const Color & color )
48 : myMin( amin ), myMax( amax ), myTickColor( color )
49{
50 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
51 myColorMap = new ColorMap(myMin,myMax);
52}
53
54template <typename Value, typename CMAP>
55inline
56DGtal::TickedColorMap<Value,CMAP>::TickedColorMap
57( const ColorMap& other,
58 const Color & color )
59 : myMin( other.min() ), myMax( other.max() ), myTickColor( color )
60{
61 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
62 myColorMap = new ColorMap( other );
63}
64
65
66template <typename Value, typename CMAP>
67inline
68DGtal::TickedColorMap<Value,CMAP>::TickedColorMap
69( const TickedColorMap<Value,CMAP> & other )
70 : myMin( other.myMin ), myMax( other.myMax ), myTickColor( other.myTickColor )
71{
72 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
73}
74
75template <typename Value, typename CMAP>
76inline
77DGtal::TickedColorMap<Value,CMAP>::~TickedColorMap()
78{
79 delete myColorMap;
80}
81
82template <typename Value, typename CMAP>
83DGtal::TickedColorMap<Value,CMAP> &
84DGtal::TickedColorMap<Value,CMAP>::operator=
85( const TickedColorMap<Value,CMAP> & other )
86{
87 if ( &other != this ) {
88 myMin = other.myMin;
89 myMax = other.myMax;
90 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
91 myTickColor = other.myTickColor;
92 myTicks = other.myTicks;
93 }
94 return *this;
95}
96
97///////////////////////////////////////////////////////////////////////////////
98// Interface - public :
99
100template<typename Value, typename CMAP>
101inline
102const Value &
103DGtal::TickedColorMap<Value,CMAP>::min() const
104{
105 return myMin;
106}
107
108template<typename Value, typename CMAP>
109inline
110const Value &
111DGtal::TickedColorMap<Value,CMAP>::max() const
112{
113 return myMax;
114}
115
116template<typename Value, typename CMAP>
117inline
118DGtal::Color
119DGtal::TickedColorMap<Value,CMAP>::operator()( const Value & value ) const
120{
121 //If there's no ticks, just return the color.
122 if (myTicks.size()==0)
123 return myColorMap->operator()(value);
124
125 typename std::vector< std::pair<Value,Value> >::const_iterator up;
126
127 //Test the upper bound
128 up = std::upper_bound(myTicks.begin(), myTicks.end(), std::make_pair(value,value));
129 if (up == myTicks.end())
130 {
131 if (std::abs(value - myTicks[myTicks.size() -1].first) < myTicks[myTicks.size() -1].second)
132 return myTickColor;
133 else
134 return myColorMap->operator()(value);
135 }
136
137 if (std::abs(value - up->first) < up->second)
138 return myTickColor;
139
140 //We test the lower bound
141 if ((up != myTicks.begin()) &&
142 (std::abs(value - (up-1)->first) < (up-1)->second))
143 return myTickColor;
144
145 return myColorMap->operator()(value);
146}
147
148/**
149 * Writes/Displays the object on an output stream.
150 * @param out the output stream where the object is written.
151 */
152template <typename Value, typename CMAP>
153inline
154void
155DGtal::TickedColorMap<Value,CMAP>::selfDisplay ( std::ostream & out ) const
156{
157 out << "[TickedColorMap "
158 << " min=" << myMin
159 << " max=" << myMax
160 << " tickColor= "<< myTickColor
161 << " number of ticks = "<< myTicks.size()
162 << " ]";
163}
164
165template<typename Value, typename CMAP>
166inline
167void DGtal::TickedColorMap<Value,CMAP>::addTick(const Value tick,
168 const Value thickness)
169{
170 ASSERT( tick >= myMin );
171 ASSERT( tick <= myMax );
172
173 myTicks.push_back(std::make_pair(tick,thickness));
174}
175
176template<typename Value, typename CMAP>
177inline
178void DGtal::TickedColorMap<Value,CMAP>::addRegularTicks(const unsigned int nbTicks,
179 const Value thickness)
180{
181 const Value width=myMax-myMin;
182 for(unsigned int i=0; i < nbTicks; ++i)
183 myTicks.push_back(std::make_pair(myMin + static_cast<Value>((double)i*width/(double)nbTicks),
184 thickness));
185}
186
187
188template<typename Value, typename CMAP>
189inline
190void DGtal::TickedColorMap<Value,CMAP>::finalize()
191{
192 std::sort(myTicks.begin(), myTicks.end());
193}
194
195
196/**
197 * Checks the validity/consistency of the object.
198 * @return 'true' if the object is valid, 'false' otherwise.
199 */
200template <typename Value, typename CMAP>
201inline
202bool
203DGtal::TickedColorMap<Value,CMAP>::isValid() const
204{
205 return true;
206}
207
208
209///////////////////////////////////////////////////////////////////////////////
210// Implementation of inline functions //
211
212template <typename Value, typename CMAP>
213inline
214std::ostream&
215DGtal::operator<< ( std::ostream & out,
216 const TickedColorMap<Value,CMAP> & object )
217{
218 object.selfDisplay( out );
219 return out;
220}
221
222// //
223///////////////////////////////////////////////////////////////////////////////
224
225
226
227