DGtal  1.5.beta
RandomColorMap.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 RandomColorMap.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/07/18
23  *
24  * Implementation of inline methods defined in RandomColorMap.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 //////////////////////////////////////////////////////////////////////////////
36 
37 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // Implementation of inline methods //
41 
42 
43 
44 
45 
46 inline
47 DGtal::RandomColorMap::RandomColorMap(const unsigned int &aFirstIndex, const unsigned int & aLastIndex,
48  const Color &aFirstColor, const Color &aLastColor)
49 {
50  myMin = aFirstIndex;
51  myMax = aLastIndex;
52  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
53  myGradientMap = new GradientColorMap<unsigned int> (aFirstIndex, aLastIndex);
54  myGradientMap->addColor(aFirstColor);
55  myGradientMap->addColor(aLastColor);
56  associateRandomIndexColor();
57 }
58 
59 
60 
61 inline
62 void
63 DGtal::RandomColorMap::associateRandomIndexColor(){
64  for(unsigned int i=myMin; i<=myMax; i++){
65  myColorIndex.push_back(myMax+1);
66  myFreeColorIndex.push_back(i);
67  }
68  unsigned int i=0;
69  while(myFreeColorIndex.size()!=0){
70  unsigned int pos = rand()%myFreeColorIndex.size();
71  myColorIndex[i]=myFreeColorIndex.at(pos);
72  myFreeColorIndex.erase(myFreeColorIndex.begin()+pos);
73  i++;
74  }
75 
76 }
77 
78 inline
79 DGtal::RandomColorMap::~RandomColorMap()
80 {
81  delete myGradientMap;
82 }
83 
84 
85 
86 inline
87 void
88 DGtal::RandomColorMap::addColor( const Color &aColor)
89 {
90  myGradientMap->addColor(aColor);
91  associateRandomIndexColor();
92 }
93 
94 inline
95 void
96 DGtal::RandomColorMap::selfDisplay ( std::ostream & out ) const
97 {
98  out << "[RandomColorMap]";
99 }
100 
101 
102 inline
103 bool
104 DGtal::RandomColorMap::isValid() const
105 {
106  return true;
107 }
108 
109 
110 inline
111 DGtal::RandomColorMap &
112 DGtal::RandomColorMap::operator=(const DGtal::RandomColorMap & anOther)
113 {
114 if ( &anOther != this ) {
115  myMin = anOther.myMin;
116  myMax = anOther.myMax;
117  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
118  myColorIndex = anOther.myColorIndex;
119  myFreeColorIndex= anOther.myFreeColorIndex;
120  myGradientMap = new GradientColorMap<unsigned int> (myMin, myMax);
121  }
122  return *this;
123 }
124 
125 inline
126 DGtal::Color
127 DGtal::RandomColorMap::operator()( const unsigned int & aValue ) const
128 {
129  return (*myGradientMap)(myColorIndex.at(aValue));
130 }
131 
132 
133 inline
134 const unsigned int &
135 DGtal::RandomColorMap::min() const
136 {
137  return myMin;
138 }
139 
140 inline
141 const unsigned int &
142 DGtal::RandomColorMap::max() const
143 {
144  return myMax;
145 }
146 
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 // Implementation of inline functions and external operators //
150 
151 /**
152  * Overloads 'operator<<' for displaying objects of class 'RandomColorMap'.
153  * @param out the output stream where the object is written.
154  * @param object the object of class 'RandomColorMap' to write.
155  * @return the output stream after the writing.
156  */
157 inline
158 std::ostream&
159 DGtal::operator<< ( std::ostream & out,
160  const RandomColorMap & object )
161 {
162  object.selfDisplay ( out );
163  return out;
164 }
165 
166 // //
167 ///////////////////////////////////////////////////////////////////////////////
168 
169