Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/manifold/src/tree2d.cpp
9903 views
1
// Copyright 2025 The Manifold Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "tree2d.h"
16
17
#include "parallel.h"
18
19
#ifndef ZoneScoped
20
#if __has_include(<tracy/Tracy.hpp>)
21
#include <tracy/Tracy.hpp>
22
#else
23
#define FrameMarkStart(x)
24
#define FrameMarkEnd(x)
25
// putting ZoneScoped in a function will instrument the function execution when
26
// TRACY_ENABLE is set, which allows the profiler to record more accurate
27
// timing.
28
#define ZoneScoped
29
#define ZoneScopedN(name)
30
#endif
31
#endif
32
33
namespace manifold {
34
35
// Not really a proper KD-tree, but a kd tree with k = 2 and alternating x/y
36
// partition.
37
// Recursive sorting is not the most efficient, but simple and guaranteed to
38
// result in a balanced tree.
39
void BuildTwoDTreeImpl(VecView<PolyVert> points, bool sortX) {
40
auto cmpx = [](const PolyVert& a, const PolyVert& b) {
41
return a.pos.x < b.pos.x;
42
};
43
auto cmpy = [](const PolyVert& a, const PolyVert& b) {
44
return a.pos.y < b.pos.y;
45
};
46
if (sortX)
47
manifold::stable_sort(points.begin(), points.end(), cmpx);
48
else
49
manifold::stable_sort(points.begin(), points.end(), cmpy);
50
if (points.size() < 2) return;
51
BuildTwoDTreeImpl(points.view(0, points.size() / 2), !sortX);
52
BuildTwoDTreeImpl(points.view(points.size() / 2 + 1), !sortX);
53
}
54
55
void BuildTwoDTree(VecView<PolyVert> points) {
56
ZoneScoped;
57
// don't even bother...
58
if (points.size() <= 8) return;
59
BuildTwoDTreeImpl(points, true);
60
}
61
} // namespace manifold
62
63