GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sigset.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <grass/imagery.h>
4 #include <grass/gis.h>
5 
6 static int gettag(FILE *, char *);
7 static int get_nbands(FILE *, struct SigSet *);
8 static int get_title(FILE *, struct SigSet *);
9 static int get_class(FILE *, struct SigSet *);
10 static int get_classnum(FILE *, struct ClassSig *);
11 static int get_classtype(FILE *, struct ClassSig *);
12 static int get_classtitle(FILE *, struct ClassSig *);
13 static int get_subclass(FILE *, struct SigSet *, struct ClassSig *);
14 static int get_subclass_pi(FILE *, struct SubSig *);
15 static int get_subclass_means(FILE *, struct SubSig *, int);
16 static int get_subclass_covar(FILE *, struct SubSig *, int);
17 
18 static double **alloc_matrix(int rows, int cols)
19 {
20  double **m;
21  int i;
22 
23  m = (double **)G_calloc(rows, sizeof(double *));
24  m[0] = (double *)G_calloc(rows * cols, sizeof(double));
25  for (i = 1; i < rows; i++)
26  m[i] = m[i - 1] + cols;
27 
28  return m;
29 }
30 
31 int I_SigSetNClasses(struct SigSet *S)
32 {
33  int i, count;
34 
35  for (i = 0, count = 0; i < S->nclasses; i++)
36  if (S->ClassSig[i].used)
37  count++;
38 
39  return count;
40 }
41 
42 
43 struct ClassData *I_AllocClassData(struct SigSet *S,
44  struct ClassSig *C, int npixels)
45 {
46  struct ClassData *Data;
47 
48  Data = &(C->ClassData);
49  Data->npixels = npixels;
50  Data->count = 0;
51  Data->x = alloc_matrix(npixels, S->nbands);
52  Data->p = alloc_matrix(npixels, C->nsubclasses);
53  return Data;
54 }
55 
56 int I_InitSigSet(struct SigSet *S)
57 {
58  S->nbands = 0;
59  S->nclasses = 0;
60  S->ClassSig = NULL;
61  S->title = NULL;
62 
63  return 0;
64 }
65 
66 int I_SigSetNBands(struct SigSet *S, int nbands)
67 {
68  S->nbands = nbands;
69 
70  return 0;
71 }
72 
73 struct ClassSig *I_NewClassSig(struct SigSet *S)
74 {
75  struct ClassSig *Sp;
76 
77  if (S->nclasses == 0)
78  S->ClassSig = (struct ClassSig *)G_malloc(sizeof(struct ClassSig));
79  else
80  S->ClassSig = (struct ClassSig *)G_realloc((char *)S->ClassSig,
81  sizeof(struct ClassSig) *
82  (S->nclasses + 1));
83 
84  Sp = &S->ClassSig[S->nclasses++];
85  Sp->classnum = 0;
86  Sp->nsubclasses = 0;
87  Sp->used = 1;
88  Sp->type = SIGNATURE_TYPE_MIXED;
89  Sp->title = NULL;
90  return Sp;
91 }
92 
93 struct SubSig *I_NewSubSig(struct SigSet *S, struct ClassSig *C)
94 {
95  struct SubSig *Sp;
96  int i;
97 
98  if (C->nsubclasses == 0)
99  C->SubSig = (struct SubSig *)G_malloc(sizeof(struct SubSig));
100  else
101  C->SubSig = (struct SubSig *)G_realloc((char *)C->SubSig,
102  sizeof(struct SubSig) *
103  (C->nsubclasses + 1));
104 
105  Sp = &C->SubSig[C->nsubclasses++];
106  Sp->used = 1;
107  Sp->R = (double **)G_calloc(S->nbands, sizeof(double *));
108  Sp->R[0] = (double *)G_calloc(S->nbands * S->nbands, sizeof(double));
109  for (i = 1; i < S->nbands; i++)
110  Sp->R[i] = Sp->R[i - 1] + S->nbands;
111  Sp->Rinv = (double **)G_calloc(S->nbands, sizeof(double *));
112  Sp->Rinv[0] = (double *)G_calloc(S->nbands * S->nbands, sizeof(double));
113  for (i = 1; i < S->nbands; i++)
114  Sp->Rinv[i] = Sp->Rinv[i - 1] + S->nbands;
115  Sp->means = (double *)G_calloc(S->nbands, sizeof(double));
116  Sp->N = 0;
117  Sp->pi = 0;
118  Sp->cnst = 0;
119  return Sp;
120 }
121 
122 #define eq(a,b) strcmp(a,b)==0
123 
124 int I_ReadSigSet(FILE * fd, struct SigSet *S)
125 {
126  char tag[256];
127 
128  I_InitSigSet(S);
129 
130  while (gettag(fd, tag)) {
131  if (eq(tag, "title:"))
132  get_title(fd, S);
133  if (eq(tag, "nbands:"))
134  get_nbands(fd, S);
135  if (eq(tag, "class:"))
136  get_class(fd, S);
137  }
138  return 1; /* for now assume success */
139 }
140 
141 static int gettag(FILE * fd, char *tag)
142 {
143  if (fscanf(fd, "%s", tag) != 1)
144  return 0;
145  G_strip(tag);
146  return 1;
147 }
148 
149 static int get_nbands(FILE * fd, struct SigSet *S)
150 {
151  fscanf(fd, "%d", &S->nbands);
152 
153  return 0;
154 }
155 
156 static int get_title(FILE * fd, struct SigSet *S)
157 {
158  char title[1024];
159 
160  *title = 0;
161  fscanf(fd, "%[^\n]", title);
162  I_SetSigTitle(S, title);
163 
164  return 0;
165 }
166 
167 static int get_class(FILE * fd, struct SigSet *S)
168 {
169  char tag[1024];
170  struct ClassSig *C;
171 
172  C = I_NewClassSig(S);
173  while (gettag(fd, tag)) {
174  if (eq(tag, "endclass:"))
175  break;
176  if (eq(tag, "classnum:"))
177  get_classnum(fd, C);
178  if (eq(tag, "classtype:"))
179  get_classtype(fd, C);
180  if (eq(tag, "classtitle:"))
181  get_classtitle(fd, C);
182  if (eq(tag, "subclass:"))
183  get_subclass(fd, S, C);
184  }
185 
186  return 0;
187 }
188 
189 static int get_classnum(FILE * fd, struct ClassSig *C)
190 {
191  fscanf(fd, "%ld", &C->classnum);
192 
193  return 0;
194 }
195 
196 static int get_classtype(FILE * fd, struct ClassSig *C)
197 {
198  fscanf(fd, "%d", &C->type);
199 
200  return 0;
201 }
202 
203 static int get_classtitle(FILE * fd, struct ClassSig *C)
204 {
205  char title[1024];
206 
207  *title = 0;
208  fscanf(fd, "%[^\n]", title);
209  I_SetClassTitle(C, title);
210 
211  return 0;
212 }
213 
214 static int get_subclass(FILE * fd, struct SigSet *S, struct ClassSig *C)
215 {
216  struct SubSig *Sp;
217  char tag[1024];
218 
219  Sp = I_NewSubSig(S, C);
220 
221  while (gettag(fd, tag)) {
222  if (eq(tag, "endsubclass:"))
223  break;
224  if (eq(tag, "pi:"))
225  get_subclass_pi(fd, Sp);
226  if (eq(tag, "means:"))
227  get_subclass_means(fd, Sp, S->nbands);
228  if (eq(tag, "covar:"))
229  get_subclass_covar(fd, Sp, S->nbands);
230  }
231 
232  return 0;
233 }
234 
235 static int get_subclass_pi(FILE * fd, struct SubSig *Sp)
236 {
237  fscanf(fd, "%lf", &Sp->pi);
238 
239  return 0;
240 }
241 
242 static int get_subclass_means(FILE * fd, struct SubSig *Sp, int nbands)
243 {
244  int i;
245 
246  for (i = 0; i < nbands; i++)
247  fscanf(fd, "%lf", &Sp->means[i]);
248 
249  return 0;
250 }
251 
252 static int get_subclass_covar(FILE * fd, struct SubSig *Sp, int nbands)
253 {
254  int i, j;
255 
256  for (i = 0; i < nbands; i++)
257  for (j = 0; j < nbands; j++)
258  fscanf(fd, "%lf", &Sp->R[i][j]);
259 
260  return 0;
261 }
262 
263 int I_SetSigTitle(struct SigSet *S, const char *title)
264 {
265  if (title == NULL)
266  title = "";
267  if (S->title)
268  free(S->title);
269  S->title = G_store(title);
270 
271  return 0;
272 }
273 
274 const char *I_GetSigTitle(const struct SigSet *S)
275 {
276  if (S->title)
277  return S->title;
278  else
279  return "";
280 }
281 
282 int I_SetClassTitle(struct ClassSig *C, const char *title)
283 {
284  if (title == NULL)
285  title = "";
286  if (C->title)
287  free(C->title);
288  C->title = G_store(title);
289 
290  return 0;
291 }
292 
293 const char *I_GetClassTitle(const struct ClassSig *C)
294 {
295  if (C->title)
296  return C->title;
297  else
298  return "";
299 }
300 
301 int I_WriteSigSet(FILE * fd, const struct SigSet *S)
302 {
303  const struct ClassSig *Cp;
304  const struct SubSig *Sp;
305  int i, j, b1, b2;
306 
307  fprintf(fd, "title: %s\n", I_GetSigTitle(S));
308  fprintf(fd, "nbands: %d\n", S->nbands);
309  for (i = 0; i < S->nclasses; i++) {
310  Cp = &S->ClassSig[i];
311  if (!Cp->used)
312  continue;
313  if (Cp->nsubclasses <= 0)
314  continue;
315  fprintf(fd, "class:\n");
316  fprintf(fd, " classnum: %ld\n", Cp->classnum);
317  fprintf(fd, " classtitle: %s\n", I_GetClassTitle(Cp));
318  fprintf(fd, " classtype: %d\n", Cp->type);
319 
320  for (j = 0; j < Cp->nsubclasses; j++) {
321  Sp = &Cp->SubSig[j];
322  fprintf(fd, " subclass:\n");
323  fprintf(fd, " pi: %g\n", Sp->pi);
324  fprintf(fd, " means:");
325  for (b1 = 0; b1 < S->nbands; b1++)
326  fprintf(fd, " %g", Sp->means[b1]);
327  fprintf(fd, "\n");
328  fprintf(fd, " covar:\n");
329  for (b1 = 0; b1 < S->nbands; b1++) {
330  fprintf(fd, " ");
331  for (b2 = 0; b2 < S->nbands; b2++)
332  fprintf(fd, " %g", Sp->R[b1][b2]);
333  fprintf(fd, "\n");
334  }
335  fprintf(fd, " endsubclass:\n");
336  }
337  fprintf(fd, "endclass:\n");
338  }
339 
340  return 0;
341 }
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
const char * I_GetSigTitle(const struct SigSet *S)
Definition: sigset.c:274
FILE * fd
Definition: g3dcolor.c:368
int count
int I_InitSigSet(struct SigSet *S)
Definition: sigset.c:56
#define eq(a, b)
Definition: sigset.c:122
int I_SetClassTitle(struct ClassSig *C, const char *title)
Definition: sigset.c:282
int I_SetSigTitle(struct SigSet *S, const char *title)
Definition: sigset.c:263
struct ClassSig * I_NewClassSig(struct SigSet *S)
Definition: sigset.c:73
#define C
Definition: intr_char.c:17
int I_SigSetNClasses(struct SigSet *S)
Definition: sigset.c:31
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
struct ClassData * I_AllocClassData(struct SigSet *S, struct ClassSig *C, int npixels)
Definition: sigset.c:43
int I_WriteSigSet(FILE *fd, const struct SigSet *S)
Definition: sigset.c:301
int I_SigSetNBands(struct SigSet *S, int nbands)
Definition: sigset.c:66
return NULL
Definition: dbfopen.c:1394
tuple cols
struct SubSig * I_NewSubSig(struct SigSet *S, struct ClassSig *C)
Definition: sigset.c:93
void free(void *)
int I_ReadSigSet(FILE *fd, struct SigSet *S)
Definition: sigset.c:124
const char * I_GetClassTitle(const struct ClassSig *C)
Definition: sigset.c:293