DGtal  0.9.2
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 #include <boost/bind.hpp>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 
43 template <typename Value, typename CMAP>
44 inline
45 DGtal::TickedColorMap<Value,CMAP>::TickedColorMap
46 ( const Value & amin,
47  const Value & amax,
48  const Color & color )
49  : myMin( amin ), myMax( amax ), myTickColor( color )
50 {
51  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
52  myColorMap = new ColorMap(myMin,myMax);
53 }
54 
55 
56 template <typename Value, typename CMAP>
57 inline
58 DGtal::TickedColorMap<Value,CMAP>::TickedColorMap
59 ( const TickedColorMap<Value,CMAP> & other )
60  : myMin( other.myMin ), myMax( other.myMax ), myTickColor( other.myTickColor )
61 {
62  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
63 }
64 
65 template <typename Value, typename CMAP>
66 inline
67 DGtal::TickedColorMap<Value,CMAP>::~TickedColorMap()
68 {
69  delete myColorMap;
70 }
71 
72 template <typename Value, typename CMAP>
73 DGtal::TickedColorMap<Value,CMAP> &
74 DGtal::TickedColorMap<Value,CMAP>::operator=
75 ( const TickedColorMap<Value,CMAP> & other )
76 {
77  if ( &other != this ) {
78  myMin = other.myMin;
79  myMax = other.myMax;
80  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
81  myTickColor = other.myTickColor;
82  myTicks = other.myTicks;
83  }
84  return *this;
85 }
86 
87 ///////////////////////////////////////////////////////////////////////////////
88 // Interface - public :
89 
90 template<typename Value, typename CMAP>
91 inline
92 const Value &
93 DGtal::TickedColorMap<Value,CMAP>::min() const
94 {
95  return myMin;
96 }
97 
98 template<typename Value, typename CMAP>
99 inline
100 const Value &
101 DGtal::TickedColorMap<Value,CMAP>::max() const
102 {
103  return myMax;
104 }
105 
106 template<typename Value, typename CMAP>
107 inline
108 DGtal::Color
109 DGtal::TickedColorMap<Value,CMAP>::operator()( const Value & value ) const
110 {
111  //If there's no ticks, just return the color.
112  if (myTicks.size()==0)
113  return myColorMap->operator()(value);
114 
115  typename std::vector< std::pair<Value,Value> >::const_iterator up;
116 
117  //Test the upper bound
118  up = std::upper_bound(myTicks.begin(), myTicks.end(), std::make_pair(value,value));
119  if (up == myTicks.end())
120  {
121  if (std::abs(value - myTicks[myTicks.size() -1].first) < myTicks[myTicks.size() -1].second)
122  return myTickColor;
123  else
124  return myColorMap->operator()(value);
125  }
126 
127  if (std::abs(value - up->first) < up->second)
128  return myTickColor;
129 
130  //We test the lower bound
131  if ((up != myTicks.begin()) &&
132  (std::abs(value - (up-1)->first) < (up-1)->second))
133  return myTickColor;
134 
135  return myColorMap->operator()(value);
136 }
137 
138 /**
139  * Writes/Displays the object on an output stream.
140  * @param out the output stream where the object is written.
141  */
142 template <typename Value, typename CMAP>
143 inline
144 void
145 DGtal::TickedColorMap<Value,CMAP>::selfDisplay ( std::ostream & out ) const
146 {
147  out << "[TickedColorMap "
148  << " min=" << myMin
149  << " max=" << myMax
150  << " tickColor= "<< myTickColor
151  << " number of ticks = "<< myTicks.size()
152  << " ]";
153 }
154 
155 template<typename Value, typename CMAP>
156 inline
157 void DGtal::TickedColorMap<Value,CMAP>::addTick(const Value tick,
158  const Value thickness)
159 {
160  ASSERT( tick >= myMin );
161  ASSERT( tick <= myMax );
162 
163  myTicks.push_back(std::make_pair(tick,thickness));
164 }
165 
166 template<typename Value, typename CMAP>
167 inline
168 void DGtal::TickedColorMap<Value,CMAP>::addRegularTicks(const unsigned int nbTicks,
169  const Value thickness)
170 {
171  const Value width=myMax-myMin;
172  for(unsigned int i=0; i < nbTicks; ++i)
173  myTicks.push_back(std::make_pair(myMin + static_cast<Value>((double)i*width/(double)nbTicks),
174  thickness));
175 }
176 
177 
178 template<typename Value, typename CMAP>
179 inline
180 void DGtal::TickedColorMap<Value,CMAP>::finalize()
181 {
182  std::sort(myTicks.begin(), myTicks.end());
183 }
184 
185 
186 /**
187  * Checks the validity/consistency of the object.
188  * @return 'true' if the object is valid, 'false' otherwise.
189  */
190 template <typename Value, typename CMAP>
191 inline
192 bool
193 DGtal::TickedColorMap<Value,CMAP>::isValid() const
194 {
195  return true;
196 }
197 
198 
199 ///////////////////////////////////////////////////////////////////////////////
200 // Implementation of inline functions //
201 
202 template <typename Value, typename CMAP>
203 inline
204 std::ostream&
205 DGtal::operator<< ( std::ostream & out,
206  const TickedColorMap<Value,CMAP> & object )
207 {
208  object.selfDisplay( out );
209  return out;
210 }
211 
212 // //
213 ///////////////////////////////////////////////////////////////////////////////
214 
215 
216 
217