Line data Source code
1 : /* File: geometry_non_linear_scale.c; Copyright and License: see below */ 2 : 3 : #define __STDC_LIMIT_MACROS 4 : #include "geometry/geometry_non_linear_scale.h" 5 : #include "u8/u8_trace.h" 6 : #include "u8/u8_log.h" 7 : #include <assert.h> 8 : #include <stdint.h> 9 : #include <stdbool.h> 10 : 11 1 : void geometry_non_linear_scale_init ( geometry_non_linear_scale_t *this_, double lower_bound, double upper_bound ) 12 : { 13 1 : U8_TRACE_BEGIN(); 14 1 : assert( lower_bound <= upper_bound ); 15 : 16 1 : (*this_).num_points = 2; 17 1 : (*this_).location[0] = lower_bound; 18 1 : (*this_).order[0] = INT32_MIN; 19 1 : (*this_).location[1] = upper_bound; 20 1 : (*this_).order[1] = INT32_MAX; 21 : 22 1 : U8_TRACE_END(); 23 1 : } 24 : 25 1 : void geometry_non_linear_scale_trace ( const geometry_non_linear_scale_t *this_ ) 26 : { 27 1 : U8_TRACE_BEGIN(); 28 1 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS ); 29 : 30 1 : U8_TRACE_INFO( "geometry_non_linear_scale_t" ); 31 1 : U8_TRACE_INFO_INT( "- num_points:", (*this_).num_points ); 32 5 : for ( uint32_t pos = 0; pos < (*this_).num_points; pos ++ ) 33 : { 34 4 : U8_TRACE_INFO_INT_INT( "- location-%, order:", (int)((*this_).location[pos]*100.0), (*this_).order[pos] ); 35 : } 36 : 37 1 : U8_TRACE_END(); 38 1 : } 39 : 40 3 : void geometry_non_linear_scale_add_order ( geometry_non_linear_scale_t *this_, int32_t order ) 41 : { 42 3 : U8_TRACE_BEGIN(); 43 3 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS ); 44 3 : assert( (*this_).num_points >= 2 ); /* prevent division by zero */ 45 : 46 : /* check for duplicates and for possible insert position */ 47 3 : bool duplicate = false; 48 3 : uint32_t insert_pos = 0; 49 12 : for ( uint32_t pos = 0; pos < (*this_).num_points; pos ++ ) 50 : { 51 9 : if ( order > (*this_).order[pos] ) 52 : { 53 5 : insert_pos = pos+1; 54 : } 55 4 : else if ( order == (*this_).order[pos] ) 56 : { 57 1 : duplicate = true; 58 1 : U8_TRACE_INFO_INT( "duplicate:", order ); 59 : } 60 : } 61 : 62 : /* insert if possible */ 63 3 : if ( ! duplicate ) 64 : { 65 2 : if ( (*this_).num_points < GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS ) 66 : { 67 : double lower_bound; 68 : double upper_bound; 69 2 : lower_bound = (*this_).location[ 0 ]; 70 2 : upper_bound = (*this_).location[ (*this_).num_points - 1 ]; 71 : 72 : /* insert order value */ 73 4 : for ( uint32_t pos2 = (*this_).num_points; pos2 > insert_pos; pos2 -- ) 74 : { 75 2 : (*this_).order[ pos2 ] = (*this_).order[ pos2-1 ]; 76 : } 77 2 : (*this_).order[ insert_pos ] = order; 78 2 : (*this_).num_points ++; 79 : 80 : /* re-location in interval */ 81 : double interval_width; 82 2 : interval_width = ( upper_bound - lower_bound ); 83 9 : for ( uint32_t pos3 = 0; pos3 < (*this_).num_points; pos3 ++ ) 84 : { 85 7 : (*this_).location[ pos3 ] = lower_bound + ( (double) pos3 * interval_width ) / (double) ( (*this_).num_points - 1 ); 86 : } 87 : } 88 : else 89 : { 90 0 : U8_LOG_WARNING( "geometry_non_linear_scale_t has not enough points." ); 91 : } 92 : } 93 : 94 : /* check for monotonic increase of order values and location values */ 95 : #ifndef NDEBUG 96 3 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS ); 97 3 : assert( (*this_).num_points >= 2 ); 98 11 : for ( uint32_t pos2 = 1; pos2 < (*this_).num_points; pos2 ++ ) 99 : { 100 8 : assert( (*this_).order[pos2-1] < (*this_).order[pos2] ); 101 8 : assert( (*this_).location[pos2-1]-0.000000001 < (*this_).location[pos2] ); /* equal locations are possible if lower_bound == upper_bound */ 102 : } 103 : #endif 104 : 105 3 : U8_TRACE_END(); 106 3 : } 107 : 108 : 109 : /* 110 : Copyright 2016-2024 Andreas Warnke 111 : 112 : Licensed under the Apache License, Version 2.0 (the "License"); 113 : you may not use this file except in compliance with the License. 114 : You may obtain a copy of the License at 115 : 116 : http://www.apache.org/licenses/LICENSE-2.0 117 : 118 : Unless required by applicable law or agreed to in writing, software 119 : distributed under the License is distributed on an "AS IS" BASIS, 120 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121 : See the License for the specific language governing permissions and 122 : limitations under the License. 123 : */