-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubdivisionController.cpp
More file actions
90 lines (75 loc) · 3.33 KB
/
SubdivisionController.cpp
File metadata and controls
90 lines (75 loc) · 3.33 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
#include "SubdivisionController.h"
SubdivisionController::SubdivisionController() {
strategyMap[SubdivisionScheme::Loop] = new LoopSubdivisionStrategy();
strategyMap[SubdivisionScheme::Butterfly] = new ButterflySubdivisionStrategy();
strategyMap[SubdivisionScheme::CatmullClark] = new CatmullClarkSubdivisionStrategy();
strategyMap[SubdivisionScheme::Kobbelt] = new KobbeltSubdivisionStrategy();
strategyMap[SubdivisionScheme::Custom] = nullptr;
}
SubdivisionController::~SubdivisionController() {
for (std::pair<SubdivisionScheme, SubdivisionStrategy*> strategy : strategyMap) {
delete strategy.second;
}
}
void SubdivisionController::switchTo(SubdivisionScheme scheme) {
std::cout << "Switching to scheme: " << scheme << std::endl;
if (scheme == SubdivisionScheme::Custom) {
CustomSchemeHandler& csh = CustomSchemeHandler::getInstance();
std::shared_ptr<CustomScheme> sptr_scheme = csh.getCurrentCustomScheme();
if (strategyMap[SubdivisionScheme::Custom] == nullptr) {
if (sptr_scheme) {
strategyMap[SubdivisionScheme::Custom] = new CustomSchemeSubdivisionStrategy(*sptr_scheme);
} else {
throw std::exception("Cannot switch to CustomScheme while there are no weights for it in CustomSchemeHandler!");
}
} else {
if (sptr_scheme) {
((CustomSchemeSubdivisionStrategy*)strategyMap[SubdivisionScheme::Custom])->setCustomScheme(*sptr_scheme);
} else {
throw std::exception("Cannot switch to CustomScheme while there are no weights for it in CustomSchemeHandler!");
}
}
}
currentStrategy = strategyMap[scheme];
Mesh baseMesh = meshHistory[0];
meshHistory.clear();
meshHistory.push_back(baseMesh);
currentMeshIndex = 0;
}
void SubdivisionController::doSubdivision() {
if (currentMeshIndex < meshHistory.size() - 1) {
++currentMeshIndex;
} else {
if (currentStrategy != nullptr) {
/*
std::cout << "Old mesh faceNum: " << (meshHistory[currentMeshIndex].m_indices.size() / 3) << std::endl;
std::cout << "Face indicies: " << meshHistory[currentMeshIndex].m_indices << std::endl;
std::cout << "Vertices:" << meshHistory[currentMeshIndex].m_vertices << std::endl;
*/
Mesh oddMesh = currentStrategy->doSubdivision(meshHistory[currentMeshIndex]);
meshHistory.push_back(oddMesh);
++currentMeshIndex;
/*
std::cout << "New mesh faceNum: " << (oddMesh.m_indices.size() / 3) << std::endl;
std::cout << "Face incides: " << oddMesh.m_indices << std::endl;
std::cout << "Vertices:" << oddMesh.m_vertices << std::endl;
*/
} else {
throw std::exception("Invalid scheme switch (possibly custom scheme has no weights)!");
}
}
}
void SubdivisionController::doBackwardStep() {
currentMeshIndex = currentMeshIndex > 0 ? --currentMeshIndex : 0;
}
Mesh SubdivisionController::getCurrentMesh() {
return meshHistory[currentMeshIndex];
}
void SubdivisionController::setBaseMesh(Mesh baseMesh) {
meshHistory.clear();
meshHistory.push_back(baseMesh);
currentMeshIndex = 0;
}
bool SubdivisionController::canDoBackwardStep() {
return currentMeshIndex > 0;
}