diff options
Diffstat (limited to 'src/reflections.c')
-rw-r--r-- | src/reflections.c | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/src/reflections.c b/src/reflections.c index 63eaa54..a1f6a2c 100644 --- a/src/reflections.c +++ b/src/reflections.c @@ -102,6 +102,7 @@ void reflection_free(ReflectionContext *reflectionctx) { Reflection *reflection_add(ReflectionContext *reflectionctx, double x, double y, double z, double intensity, ReflectionType type) { Reflection *new_reflection; + Reflection *nearest; if ( reflectionctx->list_capped ) return NULL; @@ -110,6 +111,10 @@ Reflection *reflection_add(ReflectionContext *reflectionctx, double x, double y, fprintf(stderr, "No further reflections will be stored. Go and fix the peak detection.\n"); reflectionctx->list_capped = 1; } + + nearest = reflection_find_nearest_type(reflectionctx, x, y, z, type); + if ( nearest && distance3d(x, y, z, nearest->x, nearest->y, nearest->z) < 0.1e9 ) return NULL; + reflectionctx->n_reflections++; new_reflection = malloc(sizeof(Reflection)); @@ -119,6 +124,7 @@ Reflection *reflection_add(ReflectionContext *reflectionctx, double x, double y, new_reflection->z = z; new_reflection->intensity = intensity; new_reflection->type = type; + new_reflection->found = 0; reflectionctx->last_reflection->next = new_reflection; reflectionctx->last_reflection = new_reflection; @@ -248,7 +254,56 @@ Reflection *reflection_find_nearest(ReflectionContext *reflectionctx, double x, reflection = reflectionctx->reflections; while ( reflection ) { - if ( reflection->type == REFLECTION_NORMAL ) { + if ( (reflection->type == REFLECTION_NORMAL) && (!reflection->found) ) { + double mod; + mod = modulus(x - reflection->x, y - reflection->y, z - reflection->z); + if ( mod < max ) { + max = mod; + best = reflection; + } + } + reflection = reflection->next; + }; + + if ( best ) best->found = 1; + return best; + + +} + +Reflection *reflection_find_nearest_longer(ReflectionContext *reflectionctx, double x, double y, double z, double min_distance) { + + double max = +INFINITY; + Reflection *reflection; + Reflection *best = NULL; + + reflection = reflectionctx->reflections; + while ( reflection ) { + if ( (reflection->type == REFLECTION_NORMAL) && (!reflection->found) ) { + double mod; + mod = modulus(x - reflection->x, y - reflection->y, z - reflection->z); + if ( (mod < max) && (mod >= min_distance) ) { + max = mod; + best = reflection; + } + } + reflection = reflection->next; + }; + + if ( best ) best->found = 1; + return best; + +} + +Reflection *reflection_find_nearest_type(ReflectionContext *reflectionctx, double x, double y, double z, ReflectionType type) { + + double max = +INFINITY; + Reflection *reflection; + Reflection *best = NULL; + + reflection = reflectionctx->reflections; + while ( reflection ) { + if ( reflection->type == type ) { double mod; mod = modulus(x - reflection->x, y - reflection->y, z - reflection->z); if ( mod < max ) { |