ISAAC
Overview :: Library Doc :: Server Doc :: JSON Commands

In Situ Animation of Accelerated Computations

isaac_helper.hpp
Go to the documentation of this file.
1 /* This file is part of ISAAC.
2  *
3  * ISAAC is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * ISAAC is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with ISAAC. If not, see <www.gnu.org/licenses/>. */
15 
16 #pragma once
17 
18 namespace isaac
19 {
20 
21 void mergeJSON(json_t* result,json_t* candidate)
22 {
23  const char *c_key;
24  const char *r_key;
25  json_t *c_value;
26  json_t *r_value;
27  //metadata merge, old values stay, arrays are merged
28  json_t* m_candidate = json_object_get(candidate, "metadata");
29  json_t* m_result = json_object_get(result, "metadata");
30  void *temp,*temp2;
31  if (m_candidate && m_result)
32  {
33  json_object_foreach_safe( m_candidate, temp, c_key, c_value )
34  {
35  bool found_array = false;
36  json_object_foreach_safe( m_result, temp2, r_key, r_value )
37  {
38  if (strcmp(r_key,c_key) == 0)
39  {
40  if (json_is_array(r_value) && json_is_array(c_value))
41  {
42  json_array_extend(r_value,c_value);
43  found_array = true;
44  }
45  break;
46  }
47  }
48  if (!found_array)
49  json_object_set( m_result, c_key, c_value );
50  }
51  }
52  //general merge, new values stay
53  json_object_foreach_safe( candidate, temp, c_key, c_value )
54  {
55  bool found_meta = false;
56  json_object_foreach_safe( result, temp2, r_key, r_value )
57  {
58  if (strcmp(r_key,c_key) == 0 && strcmp(r_key,"metadata") == 0)
59  {
60  found_meta = true;
61  break;
62  }
63  }
64  if (!found_meta)
65  json_object_set( result, c_key, c_value );
66  }
67 }
68 void mulMatrixMatrix(IceTDouble* result,const IceTDouble* matrix1,const IceTDouble* matrix2)
69 {
70  for (isaac_int x = 0; x < 4; x++)
71  for (isaac_int y = 0; y < 4; y++)
72  result[y+x*4] = matrix1[y+0*4] * matrix2[0+x*4]
73  + matrix1[y+1*4] * matrix2[1+x*4]
74  + matrix1[y+2*4] * matrix2[2+x*4]
75  + matrix1[y+3*4] * matrix2[3+x*4];
76 }
77 void mulMatrixVector(IceTDouble* result,const IceTDouble* matrix,const IceTDouble* vector)
78 {
79  result[0] = matrix[ 0] * vector[0] + matrix[ 4] * vector[1] + matrix[ 8] * vector[2] + matrix[12] * vector[3];
80  result[1] = matrix[ 1] * vector[0] + matrix[ 5] * vector[1] + matrix[ 9] * vector[2] + matrix[13] * vector[3];
81  result[2] = matrix[ 2] * vector[0] + matrix[ 6] * vector[1] + matrix[10] * vector[2] + matrix[14] * vector[3];
82  result[3] = matrix[ 3] * vector[0] + matrix[ 7] * vector[1] + matrix[11] * vector[2] + matrix[15] * vector[3];
83 }
84 
85 void calcInverse(IceTDouble* inv,const IceTDouble* projection,const IceTDouble* modelview)
86 {
87  IceTDouble m[16];
88  mulMatrixMatrix(m,projection,modelview);
89  inv[0] = m[5] * m[10] * m[15] -
90  m[5] * m[11] * m[14] -
91  m[9] * m[6] * m[15] +
92  m[9] * m[7] * m[14] +
93  m[13] * m[6] * m[11] -
94  m[13] * m[7] * m[10];
95 
96  inv[4] = -m[4] * m[10] * m[15] +
97  m[4] * m[11] * m[14] +
98  m[8] * m[6] * m[15] -
99  m[8] * m[7] * m[14] -
100  m[12] * m[6] * m[11] +
101  m[12] * m[7] * m[10];
102 
103  inv[8] = m[4] * m[9] * m[15] -
104  m[4] * m[11] * m[13] -
105  m[8] * m[5] * m[15] +
106  m[8] * m[7] * m[13] +
107  m[12] * m[5] * m[11] -
108  m[12] * m[7] * m[9];
109 
110  inv[12] = -m[4] * m[9] * m[14] +
111  m[4] * m[10] * m[13] +
112  m[8] * m[5] * m[14] -
113  m[8] * m[6] * m[13] -
114  m[12] * m[5] * m[10] +
115  m[12] * m[6] * m[9];
116 
117  inv[1] = -m[1] * m[10] * m[15] +
118  m[1] * m[11] * m[14] +
119  m[9] * m[2] * m[15] -
120  m[9] * m[3] * m[14] -
121  m[13] * m[2] * m[11] +
122  m[13] * m[3] * m[10];
123 
124  inv[5] = m[0] * m[10] * m[15] -
125  m[0] * m[11] * m[14] -
126  m[8] * m[2] * m[15] +
127  m[8] * m[3] * m[14] +
128  m[12] * m[2] * m[11] -
129  m[12] * m[3] * m[10];
130 
131  inv[9] = -m[0] * m[9] * m[15] +
132  m[0] * m[11] * m[13] +
133  m[8] * m[1] * m[15] -
134  m[8] * m[3] * m[13] -
135  m[12] * m[1] * m[11] +
136  m[12] * m[3] * m[9];
137 
138  inv[13] = m[0] * m[9] * m[14] -
139  m[0] * m[10] * m[13] -
140  m[8] * m[1] * m[14] +
141  m[8] * m[2] * m[13] +
142  m[12] * m[1] * m[10] -
143  m[12] * m[2] * m[9];
144 
145  inv[2] = m[1] * m[6] * m[15] -
146  m[1] * m[7] * m[14] -
147  m[5] * m[2] * m[15] +
148  m[5] * m[3] * m[14] +
149  m[13] * m[2] * m[7] -
150  m[13] * m[3] * m[6];
151 
152  inv[6] = -m[0] * m[6] * m[15] +
153  m[0] * m[7] * m[14] +
154  m[4] * m[2] * m[15] -
155  m[4] * m[3] * m[14] -
156  m[12] * m[2] * m[7] +
157  m[12] * m[3] * m[6];
158 
159  inv[10] = m[0] * m[5] * m[15] -
160  m[0] * m[7] * m[13] -
161  m[4] * m[1] * m[15] +
162  m[4] * m[3] * m[13] +
163  m[12] * m[1] * m[7] -
164  m[12] * m[3] * m[5];
165 
166  inv[14] = -m[0] * m[5] * m[14] +
167  m[0] * m[6] * m[13] +
168  m[4] * m[1] * m[14] -
169  m[4] * m[2] * m[13] -
170  m[12] * m[1] * m[6] +
171  m[12] * m[2] * m[5];
172 
173  inv[3] = -m[1] * m[6] * m[11] +
174  m[1] * m[7] * m[10] +
175  m[5] * m[2] * m[11] -
176  m[5] * m[3] * m[10] -
177  m[9] * m[2] * m[7] +
178  m[9] * m[3] * m[6];
179 
180  inv[7] = m[0] * m[6] * m[11] -
181  m[0] * m[7] * m[10] -
182  m[4] * m[2] * m[11] +
183  m[4] * m[3] * m[10] +
184  m[8] * m[2] * m[7] -
185  m[8] * m[3] * m[6];
186 
187  inv[11] = -m[0] * m[5] * m[11] +
188  m[0] * m[7] * m[9] +
189  m[4] * m[1] * m[11] -
190  m[4] * m[3] * m[9] -
191  m[8] * m[1] * m[7] +
192  m[8] * m[3] * m[5];
193 
194  inv[15] = m[0] * m[5] * m[10] -
195  m[0] * m[6] * m[9] -
196  m[4] * m[1] * m[10] +
197  m[4] * m[2] * m[9] +
198  m[8] * m[1] * m[6] -
199  m[8] * m[2] * m[5];
200 
201  IceTDouble det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
202 
203  if (det == 0)
204  return;
205 
206  det = 1.0 / det;
207 
208  for (isaac_int i = 0; i < 16; i++)
209  inv[i] = inv[i] * det;
210 }
211 
213 {
214  isaac_int hi = isaac_int(floor(h / (M_PI/3)));
215  isaac_float f = h / (M_PI/3) - isaac_float(hi);
216  isaac_float p = v*(isaac_float(1)-s);
217  isaac_float q = v*(isaac_float(1)-s*f);
218  isaac_float t = v*(isaac_float(1)-s*(isaac_float(1)-f));
219  isaac_float4 result = {0,0,0,a};
220  switch (hi)
221  {
222  case 0: case 6:
223  result.x = v;
224  result.y = t;
225  result.z = p;
226  break;
227  case 1:
228  result.x = q;
229  result.y = v;
230  result.z = p;
231  break;
232  case 2:
233  result.x = p;
234  result.y = v;
235  result.z = t;
236  break;
237  case 3:
238  result.x = p;
239  result.y = q;
240  result.z = v;
241  break;
242  case 4:
243  result.x = t;
244  result.y = p;
245  result.z = v;
246  break;
247  case 5:
248  result.x = v;
249  result.y = p;
250  result.z = q;
251  break;
252  }
253  return result;
254 }
255 
256 void setFrustum(IceTDouble * const projection, const isaac_float left,const isaac_float right,const isaac_float bottom,const isaac_float top,const isaac_float znear,const isaac_float zfar )
257 {
258  isaac_float znear2 = znear * isaac_float(2);
259  isaac_float width = right - left;
260  isaac_float height = top - bottom;
261  isaac_float zRange = znear - zfar;
262  projection[ 0] = znear2 / width;
263  projection[ 1] = isaac_float( 0);
264  projection[ 2] = isaac_float( 0);
265  projection[ 3] = isaac_float( 0);
266  projection[ 4] = isaac_float( 0);
267  projection[ 5] = znear2 / height;
268  projection[ 6] = isaac_float( 0);
269  projection[ 7] = isaac_float( 0);
270  projection[ 8] = ( right + left ) / width;
271  projection[ 9] = ( top + bottom ) / height;
272  projection[10] = ( zfar + znear) / zRange;
273  projection[11] = isaac_float(-1);
274  projection[12] = isaac_float( 0);
275  projection[13] = isaac_float( 0);
276  projection[14] = ( -znear2 * zfar ) / -zRange;
277  projection[15] = isaac_float( 0);
278 }
279 void setPerspective(IceTDouble * const projection, const isaac_float fovyInDegrees,const isaac_float aspectRatio,const isaac_float znear,const isaac_float zfar )
280 {
281  isaac_float ymax = znear * tan( fovyInDegrees * M_PI / isaac_float(360) );
282  isaac_float xmax = ymax * aspectRatio;
283  setFrustum(projection, -xmax, xmax, -ymax, ymax, znear, zfar );
284 }
285 
286 void spSetPerspectiveStereoscopic( IceTDouble * const projection, const isaac_float fovyInDegrees,const isaac_float aspectRatio,const isaac_float znear,const isaac_float zfar,const isaac_float z0,const isaac_float distance )
287 {
288  isaac_float t_z0 = znear * tan( fovyInDegrees * M_PI / isaac_float(360) );
289  isaac_float xmin = -t_z0 + distance/2.0f*znear/z0;
290  isaac_float xmax = t_z0 + distance/2.0f*znear/z0;
291  isaac_float ymin = -t_z0 / aspectRatio;
292  isaac_float ymax = t_z0 / aspectRatio;
293  setFrustum(projection, xmin, xmax, ymin, ymax, znear, zfar );
294  projection[12] += distance;
295 }
296 
297 #if ISAAC_VALGRIND_TWEAKS == 1
298  static void *extra_malloc(size_t size)
299  {
300  /* Get 4 bytes more than requested */
301  void *ptr = malloc(size + 4);
302  return ptr;
303  }
304 
305  static void extra_free(void *ptr)
306  {
307  free(ptr);
308  }
309 #endif
310 
311 
312 } //namespace isaac;
float isaac_float
Definition: isaac_types.hpp:25
void calcInverse(IceTDouble *inv, const IceTDouble *projection, const IceTDouble *modelview)
isaac_float4 getHSVA(isaac_float h, isaac_float s, isaac_float v, isaac_float a)
void spSetPerspectiveStereoscopic(IceTDouble *const projection, const isaac_float fovyInDegrees, const isaac_float aspectRatio, const isaac_float znear, const isaac_float zfar, const isaac_float z0, const isaac_float distance)
void setPerspective(IceTDouble *const projection, const isaac_float fovyInDegrees, const isaac_float aspectRatio, const isaac_float znear, const isaac_float zfar)
Definition: isaac.hpp:60
void setFrustum(IceTDouble *const projection, const isaac_float left, const isaac_float right, const isaac_float bottom, const isaac_float top, const isaac_float znear, const isaac_float zfar)
void mergeJSON(json_t *result, json_t *candidate)
int32_t isaac_int
Definition: isaac_types.hpp:26
void mulMatrixMatrix(IceTDouble *result, const IceTDouble *matrix1, const IceTDouble *matrix2)
void mulMatrixVector(IceTDouble *result, const IceTDouble *matrix, const IceTDouble *vector)