Line data Source code
1 : /* File: geometry_non_linear_scale.inl; Copyright and License: see below */
2 :
3 : #include "u8/u8_trace.h"
4 : #include "u8/u8_log.h"
5 : #include <assert.h>
6 : #include <stdbool.h>
7 :
8 0 : static inline void geometry_non_linear_scale_reinit ( geometry_non_linear_scale_t *this_, double lower_bound, double upper_bound )
9 : {
10 0 : geometry_non_linear_scale_init( this_, lower_bound, upper_bound );
11 0 : }
12 :
13 2 : static inline void geometry_non_linear_scale_destroy ( geometry_non_linear_scale_t *this_ )
14 : {
15 2 : }
16 :
17 2 : static inline double geometry_non_linear_scale_get_location ( const geometry_non_linear_scale_t *this_, int32_t order )
18 : {
19 2 : U8_TRACE_BEGIN();
20 2 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS );
21 2 : double result = (*this_).location[(*this_).num_points-1];
22 : bool found;
23 :
24 2 : found = false;
25 2 : if ( order <= (*this_).order[0] )
26 : {
27 0 : found = true;
28 0 : result = (*this_).location[0];
29 0 : U8_TRACE_INFO_INT( "result-%", result*100 );
30 : }
31 6 : for ( uint32_t pos = 1; ( pos < (*this_).num_points ) && ( ! found ) ; pos ++ )
32 : {
33 4 : if ( order < (*this_).order[pos] )
34 : {
35 1 : found = true;
36 1 : double loc_interval_width = (*this_).location[pos] - (*this_).location[pos-1];
37 1 : uint32_t ord_interval_width = (*this_).order[pos] - (*this_).order[pos-1];
38 1 : if ( ord_interval_width == 0 )
39 : {
40 0 : result = (*this_).location[pos-1]; /* prevent division by zero */
41 : }
42 : else
43 : {
44 1 : uint32_t order_interval_offset = order - (*this_).order[pos-1];
45 1 : result = (*this_).location[pos-1] + ( loc_interval_width * ((double)order_interval_offset) / ((double)ord_interval_width) );
46 : }
47 1 : U8_TRACE_INFO_INT( "interval id:", pos );
48 1 : U8_TRACE_INFO_INT_INT( "interval [i-1,i]:", (int32_t)(*this_).location[pos-1], (int32_t)(*this_).location[pos] );
49 1 : U8_TRACE_INFO_INT( "result", (int32_t)result );
50 : }
51 3 : else if ( order == (*this_).order[pos] )
52 : {
53 1 : found = true;
54 1 : result = (*this_).location[pos];
55 1 : U8_TRACE_INFO_INT( "result", (int32_t)result );
56 : }
57 : }
58 :
59 2 : U8_TRACE_END();
60 2 : return result;
61 : }
62 :
63 3 : static inline int32_t geometry_non_linear_scale_get_order ( const geometry_non_linear_scale_t *this_, double location, double snap_interval )
64 : {
65 3 : U8_TRACE_BEGIN();
66 3 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS );
67 3 : int32_t result = (*this_).order[(*this_).num_points-1];
68 : bool found;
69 :
70 3 : found = false;
71 3 : if ( location <= ( (*this_).location[0] + snap_interval) )
72 : {
73 1 : found = true;
74 1 : result = (*this_).order[0];
75 1 : U8_TRACE_INFO_INT( "result", result );
76 : }
77 6 : for ( uint32_t pos = 1; ( pos < (*this_).num_points ) && ( ! found ) ; pos ++ )
78 : {
79 3 : if ( location <= ( (*this_).location[pos] + snap_interval ) )
80 : {
81 2 : if ( location > ( (*this_).location[pos] - snap_interval ) )
82 : {
83 0 : found = true;
84 0 : result = (*this_).order[pos];
85 0 : U8_TRACE_INFO_INT( "result", result );
86 : }
87 : else
88 : {
89 2 : found = true;
90 2 : double loc_interval_width = (*this_).location[pos] - (*this_).location[pos-1];
91 2 : uint32_t ord_interval_width = (*this_).order[pos] - (*this_).order[pos-1];
92 2 : if ( ( loc_interval_width > -0.000000001 ) && ( loc_interval_width < 0.000000001 ) )
93 : {
94 0 : result = (*this_).order[pos-1]; /* prevent division by zero */
95 : }
96 : else
97 : {
98 2 : uint32_t order_interval_offset = ((double)ord_interval_width) * ( location - (*this_).location[pos-1] ) / loc_interval_width;
99 2 : result = (*this_).order[pos-1] + order_interval_offset;
100 : }
101 2 : U8_TRACE_INFO_INT( "interval id:", pos );
102 2 : U8_TRACE_INFO_INT_INT( "interval [i-1,i]:", (*this_).order[pos-1], (*this_).order[pos] );
103 2 : U8_TRACE_INFO_INT( "result", result );
104 : }
105 : }
106 : }
107 :
108 3 : U8_TRACE_END();
109 3 : return result;
110 : }
111 :
112 1 : static inline double geometry_non_linear_scale_get_closest_fix_location ( const geometry_non_linear_scale_t *this_, double location )
113 : {
114 1 : U8_TRACE_BEGIN();
115 1 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS );
116 1 : double result = (*this_).location[(*this_).num_points-1];
117 : bool found;
118 :
119 1 : found = false;
120 1 : if ( location <= (*this_).location[0] )
121 : {
122 0 : found = true;
123 0 : result = (*this_).location[0];
124 0 : U8_TRACE_INFO_INT( "result-pos", 0 );
125 : }
126 5 : for ( uint32_t pos = 1; ( pos < (*this_).num_points ) && ( ! found ) ; pos ++ )
127 : {
128 4 : if ( location <= (*this_).location[pos] )
129 : {
130 1 : found = true;
131 1 : if ( ( location - (*this_).location[pos-1] ) < ( (*this_).location[pos] - location ) )
132 : {
133 1 : result = (*this_).location[pos-1];
134 1 : U8_TRACE_INFO_INT( "result-pos", pos-1 );
135 : }
136 : else
137 : {
138 0 : result = (*this_).location[pos];
139 0 : U8_TRACE_INFO_INT( "result-pos", pos );
140 : }
141 : }
142 : }
143 :
144 1 : U8_TRACE_END();
145 1 : return result;
146 : }
147 :
148 0 : static inline uint32_t geometry_non_linear_scale_get_grid_intervals ( const geometry_non_linear_scale_t *this_ )
149 : {
150 0 : assert( (*this_).num_points >= 2 );
151 0 : return ( (*this_).num_points - 1 );
152 : }
153 :
154 0 : static inline double geometry_non_linear_scale_get_grid_distances ( const geometry_non_linear_scale_t *this_ )
155 : {
156 0 : assert( (*this_).num_points >= 2 );
157 0 : assert( (*this_).num_points <= GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS );
158 0 : return (( (*this_).location[(*this_).num_points-1] - (*this_).location[0] )/( (*this_).num_points - 1 ));
159 : }
160 :
161 :
162 : /*
163 : Copyright 2016-2025 Andreas Warnke
164 :
165 : Licensed under the Apache License, Version 2.0 (the "License");
166 : you may not use this file except in compliance with the License.
167 : You may obtain a copy of the License at
168 :
169 : http://www.apache.org/licenses/LICENSE-2.0
170 :
171 : Unless required by applicable law or agreed to in writing, software
172 : distributed under the License is distributed on an "AS IS" BASIS,
173 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
174 : See the License for the specific language governing permissions and
175 : limitations under the License.
176 : */
|