Moka libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matrix.hh
Go to the documentation of this file.
1 /*
2  * lib-spamod : Visualisation des objets en discret.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-spamod
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /******************************************************************************
25  * Fichier : Matrix.h *
26  * Auteur : DEXET Martine *
27  *----------------------------------------------------------------------------*
28  * Ce fichier contient la spécification de la classe Matrix. Cette classe *
29  * permet de représenter une matrice de taille quelconque contenant des *
30  * variables de type unsigned char. Un tableau comportant six cases et *
31  * contenant des variables de type int alpha de plus été ajouté pour les besoins *
32  * du programme. *
33  * *
34  *****************************************************************************/
35 
36 
37 #ifndef MATRIX_H
38 #define MATRIX_H
39 
40 #include "attribute.hh"
41 
42 
43 
44 /******************************************************************************
45  * Classe Matrix *
46  *****************************************************************************/
47 
48 class Matrix {
49 
50  public:
51 
52  // Constructeurs.
53  Matrix();
54  Matrix(Matrix const & mat);
55  Matrix(int tab[6], unsigned char color);
56 
57  // Destructeur.
58  ~Matrix();
59 
60  // Méthodes permettant la lecture des variables de la classe.
61  int Get_X_Min() const;
62  int Get_X_Max() const;
63  int Get_Y_Min() const;
64  int Get_Y_Max() const;
65  int Get_Z_Min() const;
66  int Get_Z_Max() const;
67  unsigned char Get_Color(int alpha, int b, int c) const;
68  bool Get_Bit(int alpha, int b, int c, int bit) const;
69  unsigned char Get_Value(int alpha, int b, int c) const;
70 
71  // Méthodes permettant l'écriture de variables dans la classe.
72  void Set_Color(int alpha, int b, int c, unsigned char color);
73  void Set_Bit(int alpha, int b, int c, int bit);
74  void Unset_Bit(int alpha, int b, int c, int bit);
75  void Set_Value(int alpha, int b, int c, unsigned char val);
76 
77 
78  private:
79 
80  // Matrice.
81  unsigned char ***m;
82 
83  // Tableau comportant six cases et contenant des variables de type int.
84  int limits[6];
85 };
86 
87 /******************************************************************************
88  * Fichier : Matrix.inl *
89  * Auteur : DEXET Martine *
90  *----------------------------------------------------------------------------*
91  * Ce fichier contient l'implémentation des méthodes de la classe Matrix. *
92  * *
93  *****************************************************************************/
94 
95 
96 #include <cassert>
97 #include "definition.hh"
98 
99 
100 
101 // Constructeurs.
102 inline
104 {
105  for (int i=0; i<6; i++)
106  limits[i] = 0;
107 
108  m = NULL;
109 }
110 
111 inline
112 Matrix::Matrix(int tab[6], unsigned char color)
113 {
114  for (int i=0; i<6; i++)
115  limits[i] = tab[i];
116 
117  m = new unsigned char**[tab[0]-tab[1]+1];
118 
119  for (int i=0; i<limits[0]-limits[1]+1; i++)
120  {
121  m[i] = new unsigned char*[tab[2]-tab[3]+1];
122 
123  for (int j=0; j<tab[2]-tab[3]+1; j++)
124  {
125  m[i][j] = new unsigned char[tab[4]-tab[5]+1];
126 
127  for (int k=0; k<tab[4]-tab[5]+1; k++)
128  m[i][j][k] = (m[i][j][k] & 0xFC) | (color & 0x3);
129  }
130  }
131 }
132 
133 inline
134 Matrix::Matrix(Matrix const & mat)
135 {
136  for (int i=0; i<6; i++)
137  limits[i] = mat.limits[i];
138 
139  m = new unsigned char**[limits[0]-limits[1]+1];
140 
141  for (int i=0; i<limits[0]-limits[1]+1; i++)
142  {
143  m[i] = new unsigned char*[limits[2]-limits[3]+1];
144 
145  for (int j=0; j<limits[2]-limits[3]+1; j++)
146  {
147  m[i][j] = new unsigned char[limits[4]-limits[5]+1];
148 
149  for (int k=0; k<limits[4]-limits[5]+1; k++)
150  m[i][j][k] = mat.m[i][j][k];
151  }
152  }
153 }
154 
155 
156 // Destructeur.
157 inline
159 {
160  if (m != NULL)
161  {
162  for (int i=0; i<limits[0]-limits[1]+1; i++)
163  {
164  for (int j=0; j<limits[2]-limits[3]+1; j++)
165  delete [] m[i][j];
166  delete [] m[i];
167  }
168  delete [] m;
169  }
170 }
171 
172 
173 // Méthodes permettant la lecture des variables de la classe.
174 inline
175 int Matrix::Get_X_Min() const
176 {
177  return limits[1];
178 }
179 
180 inline
181 int Matrix::Get_X_Max() const
182 {
183  return limits[0];
184 }
185 
186 inline
187 int Matrix::Get_Y_Min() const
188 {
189  return limits[3];
190 }
191 
192 inline
193 int Matrix::Get_Y_Max() const
194 {
195  return limits[2];
196 }
197 
198 inline
199 int Matrix::Get_Z_Min() const
200 {
201  return limits[5];
202 }
203 
204 inline
205 int Matrix::Get_Z_Max() const
206 {
207  return limits[4];
208 }
209 
210 inline
211 unsigned char Matrix::Get_Color(int alpha, int b, int c) const
212 {
213  assert(alpha >= limits[1] && alpha <= limits[0] &&
214  b >= limits[3] && b <= limits[2] &&
215  c >= limits[5] && c <= limits[4]);
216 
217  return (m[alpha-limits[1]][b-limits[3]][c-limits[5]] & 0x3);
218 }
219 
220 inline
221 bool Matrix::Get_Bit(int alpha, int b, int c, int bit) const
222 {
223  assert(alpha >= limits[1] && alpha <= limits[0] &&
224  b >= limits[3] && b <= limits[2] &&
225  c >= limits[5] && c <= limits[4]);
226 
227  return ((m[alpha-limits[1]][b-limits[3]][c-limits[5]] >> bit) & 0x1);
228 }
229 
230 inline
231 unsigned char Matrix::Get_Value(int alpha, int b, int c) const
232 {
233  assert(alpha >= limits[1] && alpha <= limits[0] &&
234  b >= limits[3] && b <= limits[2] &&
235  c >= limits[5] && c <= limits[4]);
236 
237  return (m[alpha-limits[1]][b-limits[3]][c-limits[5]]);
238 }
239 
240 
241 // Méthodes permettant l'écriture de variables dans la classe.
242 inline
243 void Matrix::Set_Color(int alpha, int b, int c, unsigned char color)
244 {
245  assert(alpha >= limits[1] && alpha <= limits[0] &&
246  b >= limits[3] && b <= limits[2] &&
247  c >= limits[5] && c <= limits[4]);
248 
249  m[alpha-limits[1]][b-limits[3]][c-limits[5]] =
250  (m[alpha-limits[1]][b-limits[3]][c-limits[5]] & 0xFC) | (color & 0x3);
251 }
252 
253 inline
254 void Matrix::Set_Bit(int alpha, int b, int c, int bit)
255 {
256  assert(alpha >= limits[1] && alpha <= limits[0] &&
257  b >= limits[3] && b <= limits[2] &&
258  c >= limits[5] && c <= limits[4]);
259 
260  m[alpha-limits[1]][b-limits[3]][c-limits[5]] =
261  (m[alpha-limits[1]][b-limits[3]][c-limits[5]] | (0x1 << bit));
262 }
263 
264 inline
265 void Matrix::Unset_Bit(int alpha, int b, int c, int bit)
266 {
267  assert(alpha >= limits[1] && alpha <= limits[0] &&
268  b >= limits[3] && b <= limits[2] &&
269  c >= limits[5] && c <= limits[4]);
270 
271  m[alpha-limits[1]][b-limits[3]][c-limits[5]] =
272  (m[alpha-limits[1]][b-limits[3]][c-limits[5]] & (0xFF - (0x1 << bit)));
273 }
274 
275 inline
276 void Matrix::Set_Value(int alpha, int b, int c, unsigned char val)
277 {
278  assert(alpha >= limits[1] && alpha <= limits[0] &&
279  b >= limits[3] && b <= limits[2] &&
280  c >= limits[5] && c <= limits[4]);
281 
282  m[alpha-limits[1]][b-limits[3]][c-limits[5]] = val;
283 }
284 
285 #endif