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 <cassert>
18#include <utility>
19
20#include "NoiseLib/include/controlfunction.h"
21#include "NoiseLib/include/math2d.h"
22#include "NoiseLib/include/utils.h"
23
24#include "highmap/array.hpp"
25#include "highmap/functions.hpp"
26
27namespace hmap
28{
29
30class ArrayControlFunction : public ControlFunction<ArrayControlFunction>
31{
32 friend class ControlFunction<ArrayControlFunction>;
33
34public:
35 explicit ArrayControlFunction(hmap::Array array) : m_array(std::move(array))
36 {
37 }
38
39protected:
40 float EvaluateImpl(float x, float y) const
41 {
42 x = std::clamp(x, 0.f, 1.f);
43 y = std::clamp(y, 0.f, 1.f);
44
45 return sample(x, y);
46 }
47
48 bool InsideDomainImpl(float x, float y) const
49 {
50 return x >= 0.f && x <= 1.f && y >= 0.f && y <= 1.f;
51 }
52
53 float DistToDomainImpl(float x, float y) const
54 {
55 if (InsideDomainImpl(x, y))
56 {
57 return 0.f;
58 }
59
60 const Point2D p(x, y);
61
62 const Point2D topLeft(0.f, 0.f);
63 const Point2D topRight(1.f, 0.f);
64 const Point2D bottomLeft(0.f, 1.f);
65 const Point2D bottomRight(1.f, 1.f);
66
67 Point2D c; // Useless point for distToLineSegment
68
69 auto dist = std::numeric_limits<float>::max();
70
71 dist = std::min(dist, distToLineSegment(p, topLeft, topRight, c));
72 dist = std::min(dist, distToLineSegment(p, topRight, bottomRight, c));
73 dist = std::min(dist, distToLineSegment(p, bottomRight, bottomLeft, c));
74 dist = std::min(dist, distToLineSegment(p, bottomLeft, topLeft, c));
75
76 return dist;
77 }
78
79 float MinimumImpl() const
80 {
81 return 0.f;
82 }
83
84 float MaximumImpl() const
85 {
86 return 1.f;
87 }
88
89private:
90 float get(int i, int j) const
91 {
92 return (float)m_array(i, j);
93 }
94
95 float sample(float ri, float rj) const
96 {
97 float x = ri * (m_array.shape.x - 1);
98 float y = rj * (m_array.shape.y - 1);
99
100 int i = (int)x;
101 int j = (int)y;
102
103 float u;
104 float v;
105
106 if (i == m_array.shape.x - 1)
107 {
108 i = m_array.shape.x - 2;
109 u = 1.f;
110 }
111 else
112 u = x - (float)i;
113
114 if (j == m_array.shape.y - 1)
115 {
116 j = m_array.shape.y - 2;
117 v = 1.f;
118 }
119 else
120 v = y - (float)j;
121
122 return m_array.get_value_bilinear_at(i, j, u, v);
123 }
124
125 const hmap::Array m_array;
126};
127
128class XyControlFunction : public ControlFunction<XyControlFunction>
129{
130 friend class ControlFunction<XyControlFunction>;
131
132public:
134 float offset = 0.f,
135 float scaling = 1.f)
136 : noise_function(noise_function), offset(offset), scaling(scaling)
137 {
138 }
139
140protected:
141 double EvaluateImpl(float x, float y) const
142 {
143 return offset + scaling * noise_function.get_delegate()(x, y, 0.f);
144 }
145
146 bool InsideDomainImpl(float x, float y) const
147 {
148 return true;
149 }
150
151 double DistToDomainImpl(float x, float y) const
152 {
153 return 0.f;
154 }
155
156 double MinimumImpl() const
157 {
158 return 0.f;
159 }
160
161 double MaximumImpl() const
162 {
163 return 1.f;
164 }
165
166private:
167 NoiseFunction noise_function;
168 float offset;
169 float scaling;
170};
171
172} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition dendry_array_control_function.hpp:31
float DistToDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:53
float MaximumImpl() const
Definition dendry_array_control_function.hpp:84
float EvaluateImpl(float x, float y) const
Definition dendry_array_control_function.hpp:40
float MinimumImpl() const
Definition dendry_array_control_function.hpp:79
bool InsideDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:48
ArrayControlFunction(hmap::Array array)
Definition dendry_array_control_function.hpp:35
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:208
Vec2< int > 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:38
A class for generating noise functions.
Definition functions.hpp:693
Definition dendry_array_control_function.hpp:129
double EvaluateImpl(float x, float y) const
Definition dendry_array_control_function.hpp:141
XyControlFunction(NoiseFunction noise_function, float offset=0.f, float scaling=1.f)
Definition dendry_array_control_function.hpp:133
double DistToDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:151
bool InsideDomainImpl(float x, float y) const
Definition dendry_array_control_function.hpp:146
double MaximumImpl() const
Definition dendry_array_control_function.hpp:161
double MinimumImpl() const
Definition dendry_array_control_function.hpp:156
Defines modular function objects for procedural generation, including noise algorithms (Perlin,...
Definition algebra.hpp:28
T y
The x and y components of the vector.
Definition algebra.hpp:41
T x
Definition algebra.hpp:41