Plaquette
 
Loading...
Searching...
No Matches
AbstractField.h
1/*
2 * AbstractField.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2025 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef ABTRACT_FIELD_H_
22#define ABTRACT_FIELD_H_
23
24#include "PqCore.h"
25
26namespace pq {
27
28class AbstractField : public Unit {
29public:
30 AbstractField() {}
31 virtual ~AbstractField() {}
32
33public:
39 virtual float at(float proportion) = 0;
40
47 template <typename T>
48 void populate(T* array, size_t size, bool wrap = false) {
49 if (size) {
50 // Initialize step and proportion.
51 float step;
52 float proportion;
53 if (wrap) {
54 step = 1.0f / size;
55 proportion = step * 0.5f; // start in the middle of bin
56 }
57 else {
58 step = 1.0f / min(size-1, 1);
59 proportion = 0;
60 }
61
62 // Fill array.
63 for (size_t i = 0; i < size; i++, proportion += step) {
64 _write(array[i], at(proportion));
65 }
66 }
67 }
68
69private:
70 // Write a float value into a Chainable type.
71 template <typename T>
72 inline void _write(T& dst, float src) { src >> dst; }
73
74 // Write a float value to another float.
75 inline void _write(float& dst, float src) { dst = src; }
76};
77
78};
79#endif
Definition AbstractField.h:28
void populate(T *array, size_t size, bool wrap=false)
Fills an array with values from this field.
Definition AbstractField.h:48
virtual float at(float proportion)=0
Returns value at given proportion in [0, 1].
A generic class representing a unit in the system.
Definition PqCore.h:335