GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/mite/shade.c Lines: 0 107 0.0 %
Date: 2017-05-26 Branches: 0 55 0.0 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "mite.h"
25
#include "privateMite.h"
26
27
miteShadeSpec *
28
miteShadeSpecNew(void) {
29
  miteShadeSpec *shpec;
30
31
  shpec = (miteShadeSpec *)calloc(1, sizeof(miteShadeSpec));
32
  if (shpec) {
33
    shpec->method = miteShadeMethodUnknown;
34
    shpec->vec0 = gageItemSpecNew();
35
    shpec->vec1 = gageItemSpecNew();
36
    shpec->scl0 = gageItemSpecNew();
37
    shpec->scl1 = gageItemSpecNew();
38
    if (!( shpec->vec0 && shpec->vec1 &&
39
           shpec->scl0 && shpec->scl1 )) {
40
      return NULL;
41
    }
42
  }
43
  return shpec;
44
}
45
46
miteShadeSpec *
47
miteShadeSpecNix(miteShadeSpec *shpec) {
48
49
  if (shpec) {
50
    shpec->vec0 = gageItemSpecNix(shpec->vec0);
51
    shpec->vec1 = gageItemSpecNix(shpec->vec1);
52
    shpec->scl0 = gageItemSpecNix(shpec->scl0);
53
    shpec->scl1 = gageItemSpecNix(shpec->scl1);
54
    airFree(shpec);
55
  }
56
  return NULL;
57
}
58
59
/*
60
******** miteShadeSpecParse
61
**
62
** set up a miteShadeSpec based on a string.  Valid forms are:
63
**
64
**   none
65
**   phong:<vector>
66
**   litten:<vector>,<vector>,<scalar>,<scalar>
67
**
68
** where <vector> and <scalar> are specifications of 3-vector and scalar
69
** parsable by miteVariableParse
70
*/
71
int
72
miteShadeSpecParse(miteShadeSpec *shpec, char *shadeStr) {
73
  static const char me[]="miteShadeSpecParse";
74
  char *buff, *qstr, *tok, *state;
75
  airArray *mop;
76
  int ansLength;
77
78
  mop = airMopNew();
79
  if (!( shpec && airStrlen(shadeStr) )) {
80
    biffAddf(MITE, "%s: got NULL pointer and/or empty string", me);
81
    airMopError(mop); return 1;
82
  }
83
  buff = airToLower(airStrdup(shadeStr));
84
  if (!buff) {
85
    biffAddf(MITE, "%s: couldn't strdup shading spec", me);
86
    airMopError(mop); return 1;
87
  }
88
  airMopAdd(mop, buff, airFree, airMopAlways);
89
  shpec->method = miteShadeMethodUnknown;
90
  if (!strcmp("none", buff)) {
91
    shpec->method = miteShadeMethodNone;
92
  } else if (buff == strstr(buff, "phong:")) {
93
    shpec->method = miteShadeMethodPhong;
94
    qstr = buff + strlen("phong:");
95
    if (miteVariableParse(shpec->vec0, qstr)) {
96
      biffAddf(MITE, "%s: couldn't parse \"%s\" as shading vector", me, qstr);
97
      airMopError(mop); return 1;
98
    }
99
    ansLength = shpec->vec0->kind->table[shpec->vec0->item].answerLength;
100
    if (3 != ansLength) {
101
      biffAddf(MITE, "%s: \"%s\" isn't a vector (answer length is %d, not 3)",
102
               me, qstr, ansLength);
103
      airMopError(mop); return 1;
104
    }
105
    shpec->method = miteShadeMethodPhong;
106
  } else if (buff == strstr(buff, "litten:")) {
107
    qstr = buff + strlen("litten:");
108
    /* ---- first vector */
109
    tok = airStrtok(qstr, ",", &state);
110
    if (miteVariableParse(shpec->vec0, tok)) {
111
      biffAddf(MITE, "%s: couldn't parse \"%s\" as first lit-tensor vector",
112
               me, tok);
113
      airMopError(mop); return 1;
114
    }
115
    ansLength = shpec->vec0->kind->table[shpec->vec0->item].answerLength;
116
    if (3 != ansLength) {
117
      biffAddf(MITE, "%s: \"%s\" isn't a vector (answer length is %d, not 3)",
118
               me, qstr, ansLength);
119
      airMopError(mop); return 1;
120
    }
121
    /* ---- second vector */
122
    tok = airStrtok(qstr, ",", &state);
123
    if (miteVariableParse(shpec->vec1, tok)) {
124
      biffAddf(MITE, "%s: couldn't parse \"%s\" as second lit-tensor vector",
125
               me, tok);
126
      airMopError(mop); return 1;
127
    }
128
    ansLength = shpec->vec1->kind->table[shpec->vec1->item].answerLength;
129
    if (3 != ansLength) {
130
      biffAddf(MITE, "%s: \"%s\" isn't a vector (answer length is %d, not 3)",
131
               me, qstr, ansLength);
132
      airMopError(mop); return 1;
133
    }
134
    /* ---- first scalar */
135
    tok = airStrtok(qstr, ",", &state);
136
    if (miteVariableParse(shpec->scl0, tok)) {
137
      biffAddf(MITE, "%s: couldn't parse \"%s\" as first lit-tensor scalar",
138
               me, tok);
139
      airMopError(mop); return 1;
140
    }
141
    ansLength = shpec->scl0->kind->table[shpec->scl0->item].answerLength;
142
    if (1 != ansLength) {
143
      biffAddf(MITE, "%s: \"%s\" isn't a scalar (answer length is %d, not 1)",
144
               me, qstr, ansLength);
145
      airMopError(mop); return 1;
146
    }
147
    /* ---- second scalar */
148
    tok = airStrtok(qstr, ",", &state);
149
    if (miteVariableParse(shpec->scl1, tok)) {
150
      biffAddf(MITE, "%s: couldn't parse \"%s\" as second lit-tensor scalar",
151
               me, tok);
152
      airMopError(mop); return 1;
153
    }
154
    ansLength = shpec->scl1->kind->table[shpec->scl1->item].answerLength;
155
    if (1 != ansLength) {
156
      biffAddf(MITE, "%s: \"%s\" isn't a scalar (answer length is %d, not 1)",
157
               me, qstr, ansLength);
158
      airMopError(mop); return 1;
159
    }
160
    shpec->method = miteShadeMethodLitTen;
161
  } else {
162
    biffAddf(MITE, "%s: shading specification \"%s\" not understood",
163
             me, shadeStr);
164
    airMopError(mop); return 1;
165
  }
166
  airMopOkay(mop);
167
  return 0;
168
}
169
170
void
171
miteShadeSpecPrint(char *buff, const miteShadeSpec *shpec) {
172
  static const char me[]="miteShadeSpecPrint";
173
  char var[4][AIR_STRLEN_MED];
174
175
  if (buff && shpec) {
176
    switch(shpec->method) {
177
    case miteShadeMethodNone:
178
      sprintf(buff, "none");
179
      break;
180
    case miteShadeMethodPhong:
181
      miteVariablePrint(var[0], shpec->vec0);
182
      sprintf(buff, "phong:%s", var[0]);
183
      break;
184
    case miteShadeMethodLitTen:
185
      miteVariablePrint(var[0], shpec->vec0);
186
      miteVariablePrint(var[1], shpec->vec1);
187
      miteVariablePrint(var[2], shpec->scl0);
188
      miteVariablePrint(var[3], shpec->scl1);
189
      sprintf(buff, "litten:%s,%s,%s,%s", var[0], var[1], var[2], var[3]);
190
      break;
191
    default:
192
      sprintf(buff, "%s: unknown shade method!", me);
193
      break;
194
    }
195
  }
196
  return;
197
}
198
199
void
200
miteShadeSpecQueryAdd(gageQuery queryScl, gageQuery queryVec,
201
                      gageQuery queryTen, gageQuery queryMite,
202
                      miteShadeSpec *shpec) {
203
  if (shpec) {
204
    switch(shpec->method) {
205
    case miteShadeMethodNone:
206
      /* no queries to add */
207
      break;
208
    case miteShadeMethodPhong:
209
      miteQueryAdd(queryScl, queryVec, queryTen, queryMite, shpec->vec0);
210
      break;
211
    case miteShadeMethodLitTen:
212
      miteQueryAdd(queryScl, queryVec, queryTen, queryMite, shpec->vec0);
213
      miteQueryAdd(queryScl, queryVec, queryTen, queryMite, shpec->vec1);
214
      miteQueryAdd(queryScl, queryVec, queryTen, queryMite, shpec->scl0);
215
      miteQueryAdd(queryScl, queryVec, queryTen, queryMite, shpec->scl1);
216
      break;
217
    default:
218
      break;
219
    }
220
  }
221
  return;
222
}
223