Kale
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "Utils.hpp"
18
19#include <delaunator.hpp>
20
21#include <iterator>
22#include <algorithm>
23
24using namespace Kale;
25
32std::pair<std::vector<Vector2f>, std::vector<unsigned int>> Kale::triangulatePath(const Vector2f* begin, const Vector2f* end) {
33 std::vector<double> coords;
34 coords.reserve(std::distance(begin, end) * 2);
35
36 std::for_each(begin, end, [&](const Vector2f& vec) {
37 coords.push_back(static_cast<double>(vec.x));
38 coords.push_back(static_cast<double>(vec.y));
39 });
40
41 delaunator::Delaunator d(coords);
42
43 std::pair<std::vector<Vector2f>, std::vector<unsigned int>> output;
44 std::vector<Vector2f>& vertices = output.first;
45 std::vector<unsigned int>& elements = output.second;
46
47 elements.reserve(d.triangles.size());
48 std::transform(d.triangles.begin(), d.triangles.end(), std::back_inserter(elements), [](size_t index) {
49 return static_cast<unsigned int>(index);
50 });
51
52 vertices.reserve(d.coords.size() / 2);
53 for (size_t i = 0; i < d.coords.size(); i += 2)
54 vertices.emplace_back(static_cast<float>(d.coords[i]), static_cast<float>(d.coords[i+1]));
55
56 return output;
57}
58
65std::pair<std::vector<float>, std::vector<unsigned int>> Kale::triangulatePathFloat(const Vector2f* begin, const Vector2f* end) {
66 std::vector<double> coords;
67 coords.reserve(std::distance(begin, end) * 2);
68
69 for (const Vector2f* it = begin; it != end; ++it) {
70 const Vector2f& vec = *it;
71 coords.push_back(static_cast<double>(vec.x));
72 coords.push_back(static_cast<double>(vec.y));
73 }
74
75 delaunator::Delaunator d(coords);
76
77 std::pair<std::vector<float>, std::vector<unsigned int>> output;
78 std::vector<float>& vertices = output.first;
79 std::vector<unsigned int>& elements = output.second;
80
81 elements.reserve(d.triangles.size());
82 std::transform(d.triangles.begin(), d.triangles.end(), std::back_inserter(elements), [](size_t index) {
83 return static_cast<unsigned int>(index);
84 });
85
86 vertices.reserve(d.coords.size());
87 std::transform(d.coords.begin(), d.coords.end(), std::back_inserter(vertices), [](double value) {
88 return static_cast<float>(value);
89 });
90
91 return output;
92}
std::pair< std::vector< float >, std::vector< unsigned int > > triangulatePathFloat(const Vector2f *begin, const Vector2f *end)
Definition Utils.cpp:65
std::pair< std::vector< Vector2f >, std::vector< unsigned int > > triangulatePath(const Vector2f *begin, const Vector2f *end)
Definition Utils.cpp:32