DGtal  1.4.beta
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 
42 template <typename Value, typename CMAP>
43 inline
44 DGtal::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 
54 template <typename Value, typename CMAP>
55 inline
56 DGtal::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 
66 template <typename Value, typename CMAP>
67 inline
68 DGtal::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 
75 template <typename Value, typename CMAP>
76 inline
77 DGtal::TickedColorMap<Value,CMAP>::~TickedColorMap()
78 {
79  delete myColorMap;
80 }
81 
82 template <typename Value, typename CMAP>
83 DGtal::TickedColorMap<Value,CMAP> &
84 DGtal::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 
100 template<typename Value, typename CMAP>
101 inline
102 const Value &
103 DGtal::TickedColorMap<Value,CMAP>::min() const
104 {
105  return myMin;
106 }
107 
108 template<typename Value, typename CMAP>
109 inline
110 const Value &
111 DGtal::TickedColorMap<Value,CMAP>::max() const
112 {
113  return myMax;
114 }
115 
116 template<typename Value, typename CMAP>
117 inline
118 DGtal::Color
119 DGtal::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  */
152 template <typename Value, typename CMAP>
153 inline
154 void
155 DGtal::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 
165 template<typename Value, typename CMAP>
166 inline
167 void 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 
176 template<typename Value, typename CMAP>
177 inline
178 void 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 
188 template<typename Value, typename CMAP>
189 inline
190 void 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  */
200 template <typename Value, typename CMAP>
201 inline
202 bool
203 DGtal::TickedColorMap<Value,CMAP>::isValid() const
204 {
205  return true;
206 }
207 
208 
209 ///////////////////////////////////////////////////////////////////////////////
210 // Implementation of inline functions //
211 
212 template <typename Value, typename CMAP>
213 inline
214 std::ostream&
215 DGtal::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