-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget_sphere.c
More file actions
212 lines (168 loc) · 6.09 KB
/
target_sphere.c
File metadata and controls
212 lines (168 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* target_sphere.c
*
* Copyright (C) 2010 - 2018 Ivo Alxneit, Paul Scherrer Institute
*
* This file is part of rt
*
* rt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* rt is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rt. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cblas.h>
#include <string.h>
#include "io_utils.h"
#include "intercept.h"
#include "targets.h"
#define TARGET_TYPE "sphere"
#define NO_ITEMS 5
typedef struct sph_state_t {
double origin[3]; /* center coordinate of sphere */
double radius2; /* radius^2 of sphere */
double z_min, z_max; /* range of valid values of 'z' in local system */
double *M; /* transform matrix local -> global coordinates */
gsl_spline *refl_spectrum; /* for interpolated reflectivity spectrum */
refl_model_t *refl_model; /* reflection models */
union fh_t output; /* output file handle or name */
int flags;
pthread_key_t PTDT_key; /* access to output buffer and flags for each target */
pthread_mutex_t mutex_writefd; /* protect write(2) */
} sph_state_t;
static int sph_init_state(void *vstate, config_setting_t * this_target,
const int file_mode, const int keep_closed,
const double P_factor)
{
sph_state_t *state = (sph_state_t *) vstate;
read_vector(this_target, "origin", state->origin);
state->M = init_M(this_target, "x", "z");
config_setting_lookup_float(this_target, "radius", &state->radius2);
state->radius2 *= state->radius2;
config_setting_lookup_float(this_target, "z_min", &state->z_min);
config_setting_lookup_float(this_target, "z_max", &state->z_max);
if (state->z_max < state->z_min) /* safety */
SWAP(state->z_max, state->z_min);
state->flags = 0;
if (keep_closed)
state->flags |= KEEP_CLOSED;
if (init_output
(TARGET_TYPE, this_target, file_mode, P_factor, &state->output,
&state->flags, state->origin, state->M) == ERR) {
state->refl_spectrum = NULL;
return ERR;
}
/* initialize reflectivity spectrum */
init_spectrum(this_target, "reflectivity", &state->refl_spectrum);
state->refl_model = init_refl_model(this_target);
state->flags |= init_reflecting_surface(this_target);
pthread_key_create(&state->PTDT_key, free_PTDT);
pthread_mutex_init(&state->mutex_writefd, NULL);
return NO_ERR;
}
static void sph_free_state(void *vstate)
{
sph_state_t *state = (sph_state_t *) vstate;
state_free(state->output, state->flags, state->M,
state->refl_spectrum, state->refl_model);
}
static double *sph_get_intercept(void *vstate, ray_t * ray)
{
sph_state_t *state = (sph_state_t *) vstate;
double *intercept;
int hits_outside;
PTDT_t *data = pthread_getspecific(state->PTDT_key);
if (data->flag & LAST_WAS_HIT) {
/*
* ray starts on this target, no hit possible.
* this test fails for ray initially emitted by
* this solid source (flag cannot be set by source)
*/
data->flag &= ~LAST_WAS_HIT;
return NULL;
}
intercept =
intercept_sphere(ray, state->M, state->origin, state->radius2,
state->z_min, state->z_max, &hits_outside);
if (!intercept) /* ray does not hit target */
return NULL;
/*
* mark as absorbed if non-reflecting surface is hit.
* mark if convex (outside) surface is hit.
*/
if ((!(state->flags & OUTSIDE) && hits_outside)
|| (state->flags & OUTSIDE && !hits_outside))
data->flag |= ABSORBED;
if (hits_outside)
data->flag |= ICPT_ON_CONVEX_SIDE;
return intercept;
}
static ray_t *sph_get_out_ray(void *vstate, ray_t * ray, double *hit,
const gsl_rng * r)
{
sph_state_t *state = (sph_state_t *) vstate;
PTDT_t *data = pthread_getspecific(state->PTDT_key);
if (data->flag & ABSORBED
|| (gsl_rng_uniform(r) >
gsl_spline_eval(state->refl_spectrum, ray->lambda, NULL))) {
/*
* if ABSORBED is set we know ray has been absorbed
* because it was intercepted by a surface with absorptivity=1
* (reflectivity=0) e.g. the backside of the target. this was
* checked (and the flag was set) in 'xxx_get_intercept()'
* above.
* then we check if ray is absorbed because the reflectivity of
* the mirror surface is less than 1.0 (absorptivity > 0.0).
*/
if (state->flags & OUTPUT_REQUIRED)
store_xyz(state->output, state->flags, ray, hit, state->M,
state->origin, data, &state->mutex_writefd);
data->flag &= ~(LAST_WAS_HIT | ABSORBED | ICPT_ON_CONVEX_SIDE); /* clear flags */
free(ray);
return NULL;
} else { /* reflect 'in_ray' */
double l_N[3], N[3];
double O[] = { 0.0, 0.0, 0.0 };
double hit_local[3];
g2l(state->M, state->origin, hit, hit_local); /* transform to local coordinates */
sph_surf_normal(hit_local, l_N); /* normal vector local system */
l2g(state->M, O, l_N, N); /* normal vector global system */
if (!(state->flags & OUTSIDE))
cblas_dscal(3, -1.0, N, 1); /* make normal point inwards */
reflect_ray(ray, N, hit, r, state->refl_model);
if (data->flag & ICPT_ON_CONVEX_SIDE) {
data->flag |= LAST_WAS_HIT; /* mark as hit */
data->flag &= ~ICPT_ON_CONVEX_SIDE;
}
return ray;
}
}
static void sph_init_PTDT(void *vstate)
{
per_thread_init(((sph_state_t *) vstate)->PTDT_key,
NO_ITEMS * sizeof(float) + sizeof(unsigned char));
}
static void sph_flush_PTDT_outbuf(void *vstate)
{
sph_state_t *state = (sph_state_t *) vstate;
per_thread_flush(state->output, state->flags, state->PTDT_key,
&state->mutex_writefd);
}
static const target_type_t sph_t = {
TARGET_TYPE,
sizeof(struct sph_state_t),
&sph_init_state,
&sph_free_state,
&sph_get_intercept,
&sph_get_out_ray,
&sph_init_PTDT,
&sph_flush_PTDT_outbuf
};
const target_type_t *target_sphere = &sph_t;