commit a19e5c454f881ca074f3610e59748ba05ff41fb1
parent a3536fc67d27b7ee3b4a1b7c9267d2642ea414ce
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Wed, 16 Mar 2016 16:47:35 +0100
Some improvements in code and comments
Diffstat:
| M | src/s4vs.c | | | 80 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 51 insertions(+), 29 deletions(-)
diff --git a/src/s4vs.c b/src/s4vs.c
@@ -62,8 +62,8 @@ realization(struct ssp_rng* rng, struct context* ctx)
struct s3d_hit hit;
int nfailures = 0;
+ /* sample a starting point on the scene surface to start the random walk from */
for(;;) {
-
/* Sample the Scene */
float u, v, w;
do { u = (float)ssp_rng_canonical(rng); } while(u >= 1.f);
@@ -153,7 +153,7 @@ compute_4v_s(struct s3d_scene* scene, const size_t max_steps, const double ks)
unsigned i;
size_t nb_t, me;
- ASSERT(max_steps && ks > 0.0);
+ ASSERT(max_steps && ks > 0);
S3D(scene_instantiate(scene, &shape));
@@ -175,8 +175,8 @@ compute_4v_s(struct s3d_scene* scene, const size_t max_steps, const double ks)
if(eq_epsf(V, 0.f, 1.e-6f) || V < 0.f) {
logger_print(LOGGER_DEFAULT, LOG_ERROR,
-"Invalid volume \"%.2f\". The scene might not match the prerequisites:\n"
-"it must be closed and its normals must point *into* the volume.\n", V);
+ "Invalid volume \"%.2f\". The scene might not match the prerequisites:\n"
+ "it must be closed and its normals must point *into* the volume.\n", V);
goto error;
}
@@ -222,8 +222,8 @@ compute_4v_s(struct s3d_scene* scene, const size_t max_steps, const double ks)
if(ctx.exit_failure) {
logger_print(LOGGER_DEFAULT, LOG_ERROR,
-"Too many failures. The scene might not match the prerequisites: it must be\n"
-"closed and its normals must point *into* the volume.\n");
+ "Too many failures. The scene might not match the prerequisites: it must be\n"
+ "closed and its normals must point *into* the volume.\n");
goto error;
}
@@ -249,14 +249,46 @@ error:
goto exit;
}
+static res_T
+import_obj(const char *filename, struct s3d_scene **scene) {
+ struct s3d_device* s3d = NULL;
+ struct s3daw* s3daw = NULL;
+ size_t i, count;
+ res_T res;
+ const int VERBOSE = 1;
+
+ res = s3d_device_create(NULL, NULL, VERBOSE, &s3d);
+ if (res != RES_OK) goto end;
+ res = s3daw_create(NULL, NULL, NULL, NULL, s3d, VERBOSE, &s3daw);
+ if (res != RES_OK) goto end;
+ res = s3daw_load(s3daw, filename);
+ if (res != RES_OK) goto end;
+ res = s3daw_get_shapes_count(s3daw, &count);
+ if (res != RES_OK) goto end;
+ res = s3d_scene_create(s3d, scene);
+ if (res != RES_OK) goto end;
+ for (i = 0; i < count; i ++) {
+ struct s3d_shape* shape;
+ if (res != RES_OK) goto end;
+ res = s3daw_get_shape(s3daw, i, &shape);
+ if (res != RES_OK) goto end;
+ res = s3d_scene_attach_shape(*scene, shape);
+ if (res != RES_OK) goto end;
+ }
+
+ /* release memory */
+ end:
+ if (s3daw) res = s3daw_ref_put(s3daw);
+ if (s3d) res = s3d_device_ref_put(s3d);
+
+ return res;
+}
+
int main(int argc, char *argv[]) {
struct s3d_scene *scene;
size_t nsteps = 0;
double ks = 0;
res_T res = RES_OK;
- struct s3daw* s3daw;
- struct s3d_device* s3d;
- size_t i, count;
if(argc < 2 || argc > 4) {
if(argc < 2)
@@ -268,45 +300,35 @@ int main(int argc, char *argv[]) {
return RES_BAD_ARG;
}
-#define VERB 1
- s3d_device_create(NULL, NULL, VERB, &s3d);
- s3daw_create(NULL, NULL, NULL, NULL, s3d, VERB, &s3daw);
- res = s3daw_load(s3daw, argv[1]);
+ res = import_obj(argv[1], &scene);
if(res != RES_OK) {
logger_print(LOGGER_DEFAULT, LOG_ERROR, "Invalid file '%s'\n", argv[1]);
- return RES_BAD_ARG;
- }
- s3daw_get_shapes_count(s3daw, &count);
- s3d_scene_create(s3d, &scene);
- for (i = 0; i < count; i ++) {
- struct s3d_shape* shape;
- s3daw_get_shape(s3daw, i, &shape);
- s3d_scene_attach_shape(scene, shape);
+ return res;
}
if(argc < 3) {
- nsteps = 10000;
- } else {
+ nsteps = 10000; /* default value */
+ }
+ else {
long ns = atol(argv[2]);
if(ns <= 0) {
logger_print(LOGGER_DEFAULT, LOG_ERROR, "Invalid number of steps `%s'\n", argv[2]);
- return res;
+ return RES_BAD_ARG;
}
nsteps = (size_t)ns;
}
if(argc < 4) {
ks = 1.0;
- } else {
+ }
+ else {
ks = atof(argv[3]);
if(ks <= 0.0) {
logger_print(LOGGER_DEFAULT, LOG_ERROR,
"Invalid k-scattering value `%s'\n", argv[3]);
- return res;
+ return RES_BAD_ARG;
}
}
- res = compute_4v_s(scene, nsteps, ks);
-
- return res;
+ return compute_4v_s(scene, nsteps, ks);
}