forked from theo-dep/OpenGL-Transparency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertexBspTree.cpp
More file actions
133 lines (107 loc) · 4.28 KB
/
Copy pathVertexBspTree.cpp
File metadata and controls
133 lines (107 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "VertexBspTree.hpp"
#include <iostream>
#include <fstream>
inline void writeNode(std::ofstream &ofs, const std::unique_ptr<VertexBspTreeType::Node> &node) noexcept;
inline std::unique_ptr<VertexBspTreeType::Node> readNode(std::ifstream &ifs) noexcept;
//--------------------------------------------------------------------------
VertexBspTree::VertexBspTree() : VertexBspTreeType(std::vector<Vertex>())
{
}
//--------------------------------------------------------------------------
VertexBspTree::VertexBspTree(std::vector<Vertex> && vertices, const std::vector<unsigned int> & indices)
: VertexBspTreeType(std::move(vertices), indices)
{
}
//--------------------------------------------------------------------------
VertexBspTree::VertexBspTree(std::vector<Vertex> && vertices) : VertexBspTreeType(std::move(vertices))
{
}
//--------------------------------------------------------------------------
bool VertexBspTree::save(const std::string &filename) const noexcept
{
std::ofstream ofs(filename, std::ios::binary);
if (!ofs)
{
std::cerr << "Failed to open file for writing: " << filename << std::endl;
return false;
}
// Write vertices
size_t verticesSize = vertices_.size();
ofs.write(reinterpret_cast<const char*>(&verticesSize), sizeof(verticesSize));
ofs.write(reinterpret_cast<const char*>(vertices_.data()), verticesSize * sizeof(Vertex));
// Write BSP-tree
writeNode(ofs, root_);
ofs.close();
return true;
}
//--------------------------------------------------------------------------
bool VertexBspTree::load(const std::string &filename) noexcept
{
std::ifstream ifs(filename, std::ios::binary);
if (!ifs)
{
std::cerr << "Failed to open file for reading: " << filename << std::endl;
return false;
}
// Read vertices
size_t verticesSize;
ifs.read(reinterpret_cast<char*>(&verticesSize), sizeof(verticesSize));
vertices_.resize(verticesSize);
ifs.read(reinterpret_cast<char*>(vertices_.data()), verticesSize * sizeof(Vertex));
// Read BSP-tree
root_ = readNode(ifs);
ifs.close();
return true;
}
//--------------------------------------------------------------------------
inline void writeNode(std::ofstream &ofs, const std::unique_ptr<VertexBspTreeType::Node> &node) noexcept
{
if (node)
{
// Write presence of node
bool hasNode = true;
ofs.write(reinterpret_cast<const char*>(&hasNode), sizeof(hasNode));
// Write plane
ofs.write(reinterpret_cast<const char*>(&std::get<0>(node->plane)), sizeof(VertexBspTreeType::point_type));
ofs.write(reinterpret_cast<const char*>(&std::get<1>(node->plane)), sizeof(VertexBspTreeType::coord_type));
// Write triangles
size_t trianglesSize = node->triangles.size();
ofs.write(reinterpret_cast<const char*>(&trianglesSize), sizeof(trianglesSize));
ofs.write(reinterpret_cast<const char*>(node->triangles.data()), trianglesSize * sizeof(unsigned int));
// Write child nodes
writeNode(ofs, node->behind);
writeNode(ofs, node->infront);
}
else
{
// Write absence of node
bool hasNode = false;
ofs.write(reinterpret_cast<const char*>(&hasNode), sizeof(hasNode));
}
}
//--------------------------------------------------------------------------
inline std::unique_ptr<VertexBspTreeType::Node> readNode(std::ifstream &ifs) noexcept
{
bool hasNode;
ifs.read(reinterpret_cast<char*>(&hasNode), sizeof(hasNode));
if (hasNode)
{
auto node = std::make_unique<VertexBspTreeType::Node>();
// Read plane
ifs.read(reinterpret_cast<char*>(&std::get<0>(node->plane)), sizeof(VertexBspTreeType::point_type));
ifs.read(reinterpret_cast<char*>(&std::get<1>(node->plane)), sizeof(VertexBspTreeType::coord_type));
// Read triangles
size_t trianglesSize;
ifs.read(reinterpret_cast<char*>(&trianglesSize), sizeof(trianglesSize));
node->triangles.resize(trianglesSize);
ifs.read(reinterpret_cast<char*>(node->triangles.data()), trianglesSize * sizeof(unsigned int));
// Read child nodes
node->behind = readNode(ifs);
node->infront = readNode(ifs);
return node;
}
else
{
return nullptr;
}
}