RayZaler 0.1
The free opto-mechanical simulation framework
ScatterTree.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _SCATTER_TREE_H
20#define _SCATTER_TREE_H
21
22#include <list>
23#include <vector>
24#include <Helpers.h>
25
26namespace RZ {
27 struct ScatterVec {
28 union {
29 struct {
30 double x;
31 double y;
32 };
33
34 double coord[2];
35 };
36
37 ScatterVec();
38 ScatterVec(double x, double y);
39 ScatterVec(double);
40 ScatterVec(int);
41
42 ScatterVec &operator+=(ScatterVec const &);
43 ScatterVec &operator-=(ScatterVec const &);
44 ScatterVec &operator/=(double);
45 ScatterVec &operator*=(double);
46
47 ScatterVec operator+(ScatterVec const &) const;
48 ScatterVec operator-(ScatterVec const &) const;
49
50 inline bool
51 inRange(const ScatterVec &min, const ScatterVec &max) const
52 {
53 return min.x <= x && x < max.x && min.y <= y && y < max.y;
54 }
55 };
56
57 template<>
59 static constexpr bool value = true;
60 };
61
62 class ScatterTree;
63
65 ScatterVec cog;
66 ScatterVec topLeft;
67 ScatterVec bottomRight;
68 unsigned int nElem;
69
70 ScatterTreeNode *leaves[2][2];
71 std::vector<ScatterVec> unplaced;
72 };
73
75 public:
76 virtual ScatterVec resolution() const = 0;
77 virtual ScatterVec topLeft() const = 0;
78 virtual ScatterVec bottomRight() const = 0;
79 virtual void setId(uint32_t);
80 virtual void render(int x, int y, unsigned int count) = 0;
81
83 virtual ~ScatterTreeRenderer();
84 };
85
87 unsigned int m_stride = 2;
88 ScatterTreeNode *m_root = nullptr;
89 std::list<ScatterTreeNode> m_alloc;
90 std::vector<double> m_points;
91 double m_finestScale = 1;
92 unsigned int m_splitThreshold = 100;
93
94 ScatterTreeNode *makeNode();
95
96 void buildNode(ScatterTreeNode *);
97 void renderNode(
98 const ScatterTreeNode *,
100 ScatterVec const &min,
101 ScatterVec const &max,
102 ScatterVec const &res);
103
104 public:
105 ScatterTree();
106
107 void push(double x, double y);
108 void setStride(unsigned int stride);
109 void transfer(std::vector<double> &data);
110 void rebuild();
111 void render(ScatterTreeRenderer *);
112 void render(
114 ScatterVec const &,
115 ScatterVec const &);
116 void setSplitThreshold(unsigned int);
117 void setFinestScale(double);
118 };
119}
120
121#endif // _SCATTER_TREE_H
Definition: ScatterTree.h:86
Definition: ScatterTree.h:74
Definition: ScatterTree.h:64
Definition: ScatterTree.h:27
Definition: Helpers.h:66