Krita Source Code Documentation
Loading...
Searching...
No Matches
nugrid.cpp File Reference
#include "nugrid.h"
#include <cmath>
#include <stdlib.h>
#include <assert.h>
#include <boost/math/special_functions/log1p.hpp>
#include <boost/math/special_functions/expm1.hpp>

Go to the source code of this file.

Functions

int center_grid_reverse_map (void *gridptr, double x)
 
NUgridcreate_center_grid (double start, double end, double ratio, int num_points)
 
NUgridcreate_general_grid (double *points, int num_points)
 
NUgridcreate_log_grid (double start, double end, int num_points)
 
void destroy_grid (NUgrid *grid)
 
int general_grid_reverse_map (void *gridptr, double x)
 
int log_grid_reverse_map (void *gridptr, double x)
 

Function Documentation

◆ center_grid_reverse_map()

int center_grid_reverse_map ( void * gridptr,
double x )

Definition at line 32 of file nugrid.cpp.

33{
34 center_grid *grid = (center_grid *)gridptr;
35
36 x -= grid->center;
37 double index =
38 copysign (log1p(fabs(x)*grid->aInv)*grid->bInv, x);
39 return (int)floor(grid->half_points + index - grid->even_half);
40}
T copysign(T x, T y)
int half_points
Definition nugrid.h:55
double even_half
Definition nugrid.h:54
double center
Definition nugrid.h:54
double aInv
Definition nugrid.h:54
double bInv
Definition nugrid.h:54

References center_grid::aInv, center_grid::bInv, center_grid::center, center_grid::even_half, and center_grid::half_points.

◆ create_center_grid()

NUgrid * create_center_grid ( double start,
double end,
double ratio,
int num_points )

Definition at line 83 of file nugrid.cpp.

85{
86 center_grid *grid = new center_grid;
87 if (grid != NULL) {
88 assert (ratio > 1.0);
89 grid->start = start;
90 grid->end = end;
91 grid->center = 0.5*(start + end);
92 grid->num_points = num_points;
93 grid->half_points = num_points/2;
94 grid->odd = ((num_points % 2) == 1);
95 grid->b = log(ratio) / (double)(grid->half_points-1);
96 grid->bInv = 1.0/grid->b;
97 grid->points = new double[num_points];
98 if (grid->odd) {
99 grid->even_half = 0.0;
100 grid->odd_one = 1;
101 grid->a = 0.5*(end-start)/expm1(grid->b*grid->half_points);
102 grid->aInv = 1.0/grid->a;
103 for (int i=-grid->half_points; i<=grid->half_points; i++) {
104 double sign;
105 if (i<0)
106 sign = -1.0;
107 else
108 sign = 1.0;
109 grid->points[i+grid->half_points] =
110 sign * grid->a*expm1(grid->b*abs(i))+grid->center;
111 }
112 }
113 else {
114 grid->even_half = 0.5;
115 grid->odd_one = 0;
116 grid->a =
117 0.5*(end-start)/expm1(grid->b*(-0.5+grid->half_points));
118 grid->aInv = 1.0/grid->a;
119 for (int i=-grid->half_points; i<grid->half_points; i++) {
120 double sign;
121 if (i<0) sign = -1.0;
122 else sign = 1.0;
123 grid->points[i+grid->half_points] =
124 sign * grid->a*expm1(grid->b*fabs(0.5+i)) + grid->center;
125 }
126 }
128 grid->code = CENTER;
129 }
130 return (NUgrid*) grid;
131}
Point abs(const Point &pt)
int center_grid_reverse_map(void *gridptr, double x)
Definition nugrid.cpp:32
@ CENTER
Definition nugrid.h:26
int odd_one
Definition nugrid.h:55
double end
Definition nugrid.h:48
grid_type code
Definition nugrid.h:47
int(* reverse_map)(void *grid, double x)
Definition nugrid.h:51
double a
Definition nugrid.h:54
double *restrict points
Definition nugrid.h:49
int num_points
Definition nugrid.h:50
double b
Definition nugrid.h:54
bool odd
Definition nugrid.h:56
double start
Definition nugrid.h:48

References center_grid::a, center_grid::aInv, center_grid::b, center_grid::bInv, CENTER, center_grid::center, center_grid_reverse_map(), center_grid::code, center_grid::end, center_grid::even_half, center_grid::half_points, center_grid::num_points, center_grid::odd, center_grid::odd_one, center_grid::points, center_grid::reverse_map, sign(), and center_grid::start.

◆ create_general_grid()

NUgrid * create_general_grid ( double * points,
int num_points )

Definition at line 155 of file nugrid.cpp.

156{
157 NUgrid* grid = new NUgrid;
158 if (grid != NULL) {
160 grid->code = GENERAL;
161 grid->points = new double[num_points];
162 grid->start = points[0];
163 grid->end = points[num_points-1];
164 grid->num_points = num_points;
165 for (int i=0; i<num_points; i++)
166 grid->points[i] = points[i];
167 grid->code = GENERAL;
168 }
169 return grid;
170}
int general_grid_reverse_map(void *gridptr, double x)
Definition nugrid.cpp:57
@ GENERAL
Definition nugrid.h:26
int(* reverse_map)(void *grid, double x)
Definition nugrid.h:36
int num_points
Definition nugrid.h:35
double end
Definition nugrid.h:33
double start
Definition nugrid.h:33
grid_type code
Definition nugrid.h:32
double *restrict points
Definition nugrid.h:34

References NUgrid::code, NUgrid::end, GENERAL, general_grid_reverse_map(), NUgrid::num_points, NUgrid::points, NUgrid::reverse_map, and NUgrid::start.

◆ create_log_grid()

NUgrid * create_log_grid ( double start,
double end,
int num_points )

Definition at line 135 of file nugrid.cpp.

137{
138 log_grid *grid = new log_grid;
139 grid->code = LOG;
140 grid->start = start;
141 grid->end = end;
142 grid->num_points = num_points;
143 grid->points = new double[num_points];
144 grid->a = 1.0/(double)(num_points-1)*log(end/start);
145 grid->ainv = 1.0/grid->a;
146 grid->startinv = 1.0/start;
147 for (int i=0; i<num_points; i++)
148 grid->points[i] = start*exp(grid->a*(double)i);
150 return (NUgrid*) grid;
151}
int log_grid_reverse_map(void *gridptr, double x)
Definition nugrid.cpp:43
@ LOG
Definition nugrid.h:26
double startinv
Definition nugrid.h:70
double a
Definition nugrid.h:70
int(* reverse_map)(void *grid, double x)
Definition nugrid.h:67
double start
Definition nugrid.h:64
grid_type code
Definition nugrid.h:63
int num_points
Definition nugrid.h:66
double ainv
Definition nugrid.h:70
double *restrict points
Definition nugrid.h:65
double end
Definition nugrid.h:64

References log_grid::a, log_grid::ainv, log_grid::code, log_grid::end, LOG, log_grid_reverse_map(), log_grid::num_points, log_grid::points, log_grid::reverse_map, log_grid::start, and log_grid::startinv.

◆ destroy_grid()

void destroy_grid ( NUgrid * grid)

Definition at line 173 of file nugrid.cpp.

174{
175 delete[] grid->points;
176 delete grid;
177}

References NUgrid::points.

◆ general_grid_reverse_map()

int general_grid_reverse_map ( void * gridptr,
double x )

Definition at line 57 of file nugrid.cpp.

58{
59 NUgrid* grid = (NUgrid*) gridptr;
60 int N = grid->num_points;
61 double *points = grid->points;
62 if (x <= points[0])
63 return (0);
64 else if (x >= points[N-1])
65 return (N-1);
66 else {
67 int hi = N-1;
68 int lo = 0;
69 bool done = false;
70 while (!done) {
71 int i = (hi+lo)>>1;
72 if (points[i] > x)
73 hi = i;
74 else
75 lo = i;
76 done = (hi-lo)<2;
77 }
78 return (lo);
79 }
80}

References NUgrid::num_points, and NUgrid::points.

◆ log_grid_reverse_map()

int log_grid_reverse_map ( void * gridptr,
double x )

Definition at line 43 of file nugrid.cpp.

44{
45 log_grid *grid = (log_grid *)gridptr;
46
47 int index = (int) floor(grid->ainv*log(x*grid->startinv));
48
49 if (index < 0)
50 return 0;
51 else
52 return index;
53}

References log_grid::ainv, and log_grid::startinv.