HighMap library (C++)
Loading...
Searching...
No Matches
dendry_array_control_function.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
15#pragma once
16
17#include <utility>
18
19#include "highmap/array.hpp"
20#include "highmap/functions.hpp"
21
22#include "NoiseLib/include/controlfunction.h"
23#include "NoiseLib/include/math2d.h"
24#include "NoiseLib/include/utils.h"
25
26#include <cassert>
27
28namespace hmap
29{
30
31class ArrayControlFunction : public ControlFunction<ArrayControlFunction>
32{
33 friend class ControlFunction<ArrayControlFunction>;
34
35public:
36 explicit ArrayControlFunction(hmap::Array array) : m_array(std::move(array))
37 {
38 }
39
40protected:
41 float EvaluateImpl(float x, float y) const
42 {
43 x = std::clamp(x, 0.f, 1.f);
44 y = std::clamp(y, 0.f, 1.f);
45
46 return sample(x, y);
47 }
48
49 bool InsideDomainImpl(float x, float y) const
50 {
51 return x >= 0.f && x <= 1.f && y >= 0.f && y <= 1.f;
52 }
53
54 float DistToDomainImpl(float x, float y) const
55 {
56 if (InsideDomainImpl(x, y))
57 {
58 return 0.f;
59 }
60
61 const Point2D p(x, y);
62
63 const Point2D topLeft(0.f, 0.f);
64 const Point2D topRight(1.f, 0.f);
65 const Point2D bottomLeft(0.f, 1.f);
66 const Point2D bottomRight(1.f, 1.f);
67
68 Point2D c; // Useless point for distToLineSegment
69
70 auto dist = std::numeric_limits<float>::max();
71
72 dist = std::min(dist, distToLineSegment(p, topLeft, topRight, c));
73 dist = std::min(dist, distToLineSegment(p, topRight, bottomRight, c));
74 dist = std::min(dist, distToLineSegment(p, bottomRight, bottomLeft, c));
75 dist = std::min(dist, distToLineSegment(p, bottomLeft, topLeft, c));
76
77 return dist;
78 }
79
80 float MinimumImpl() const
81 {
82 return 0.f;
83 }
84
85 float MaximumImpl() const
86 {
87 return 1.f;
88 }
89
90private:
91 float get(int i, int j) const
92 {
93 return (float)m_array(i, j);
94 }
95
96 float sample(float ri, float rj) const
97 {
98 float x = ri * (m_array.shape.x - 1);
99 float y = rj * (m_array.shape.y - 1);
100
101 int i = (int)x;
102 int j = (int)y;
103
104 float u;
105 float v;
106
107 if (i == m_array.shape.x - 1)
108 {
109 i = m_array.shape.x - 2;
110 u = 1.f;
111 }
112 else
113 u = x - (float)i;
114
115 if (j == m_array.shape.y - 1)
116 {
117 j = m_array.shape.y - 2;
118 v = 1.f;
119 }
120 else
121 v = y - (float)j;
122
123 return m_array.get_value_bilinear_at(i, j, u, v);
124 }
125
126 const hmap::Array m_array;
127};
128
129class XyControlFunction : public ControlFunction<XyControlFunction>
130{
131 friend class ControlFunction<XyControlFunction>;
132
133public:
135 float offset = 0.f,
136 float scaling = 1.f)
137 : noise_function(noise_function), offset(offset), scaling(scaling)
138 {
139 }
140
141protected:
142 double EvaluateImpl(float x, float y) const
143 {
144 return offset + scaling * noise_function.get_delegate()(x, y, 0.f);
145 }
146
147 bool InsideDomainImpl(float /* x */, float /* y */) const
148 {
149 return true;
150 }
151
152 double DistToDomainImpl(float /* x */, float /* y */) const
153 {
154 return 0.f;
155 }
156
157 double MinimumImpl() const
158 {
159 return 0.f;
160 }
161
162 double MaximumImpl() const
163 {
164 return 1.f;
165 }
166
167private:
168 NoiseFunction noise_function;
169 float offset;
170 float scaling;
171};
172
173} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition dendry_array_control_function.hpp:32
float DistToDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:54
float MaximumImpl() const
Definition dendry_array_control_function.hpp:85
float EvaluateImpl(float x, float y) const
Definition dendry_array_control_function.hpp:41
float MinimumImpl() const
Definition dendry_array_control_function.hpp:80
bool InsideDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:49
ArrayControlFunction(hmap::Array array)
Definition dendry_array_control_function.hpp:36
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
float get_value_bilinear_at(int i, int j, float u, float v) const
Retrieves the array value at the location (x, y) near the index (i, j) using bilinear interpolation.
Definition methods.cpp:225
glm::ivec2 shape
The shape of the array {ni, nj}.
Definition array.hpp:38
HMAP_FCT_XY_TYPE get_delegate() const
Get the current delegate function.
Definition functions.cpp:44
A class for generating noise functions.
Definition functions.hpp:714
Definition dendry_array_control_function.hpp:130
double EvaluateImpl(float x, float y) const
Definition dendry_array_control_function.hpp:142
XyControlFunction(NoiseFunction noise_function, float offset=0.f, float scaling=1.f)
Definition dendry_array_control_function.hpp:134
bool InsideDomainImpl(float, float) const
Definition dendry_array_control_function.hpp:147
double DistToDomainImpl(float, float) const
Definition dendry_array_control_function.hpp:152
double MaximumImpl() const
Definition dendry_array_control_function.hpp:162
double MinimumImpl() const
Definition dendry_array_control_function.hpp:157
Defines modular function objects for procedural generation, including noise algorithms (Perlin,...
Definition algebra.hpp:23