CSEngine
Loading...
Searching...
No Matches
VertexSkinData.h
1#pragma once
2
3#include <list>
4#include <vector>
5#include <cmath>
6
7namespace CSE {
8
10 public:
11 VertexSkinData() = default;
12
13 ~VertexSkinData() = default;
14
15 void addJointEffect(int jointId, float weight) {
16 auto jointID_it = m_jointIDs.begin();
17 auto weight_it = m_weights.begin();
18 for (const auto& weight_object : m_weights) {
19 if (weight > weight_object) {
20 m_jointIDs.insert(jointID_it++, jointId);
21 m_weights.insert(weight_it++, weight);
22 return;
23 }
24 }
25 m_jointIDs.push_back(jointId);
26 m_weights.push_back(weight);
27 }
28
29 void limitJointNumber(int max) {
30 if (m_jointIDs.size() > max) {
31 std::vector<float> topWeights;
32 topWeights.resize(max);
33 float total = saveTopWeights(topWeights);
34 refillWeightList(topWeights, total);
35 removeExcessJointIds(max);
36 } else if (m_jointIDs.size() < max) {
37 fillEmptyWeights(max);
38 }
39 }
40
41
42 const std::vector<int>& getJointIDs() const {
43 return m_jointIDs;
44 }
45
46 const std::vector<float>& getWeights() const {
47 return m_weights;
48 }
49
50 private:
51 void fillEmptyWeights(int max) {
52 m_jointIDs.reserve(max);
53 m_weights.reserve(max);
54 while (m_jointIDs.size() < max) {
55 m_jointIDs.push_back(0);
56 m_weights.push_back(0);
57 }
58 }
59
60 float saveTopWeights(std::vector<float>& topWeightsArray) {
61 float total = 0;
62 auto size = topWeightsArray.size();
63 for (int i = 0; i < size; ++i) {
64 topWeightsArray[i] = m_weights[i];
65 total += topWeightsArray[i];
66 }
67 return total;
68 }
69
70 void refillWeightList(const std::vector<float>& topWeights, float total) {
71 m_weights.clear();
72 m_weights.reserve(topWeights.size());
73 for (const auto& topWeight : topWeights) {
74 m_weights.push_back(std::fmin(topWeight / total, 1));
75 }
76 }
77
78 void removeExcessJointIds(int max) {
79 while (m_jointIDs.size() > max) {
80 m_jointIDs.erase(m_jointIDs.begin() + m_jointIDs.size() - 1);
81 }
82 }
83
84 private:
85 std::vector<int> m_jointIDs;
86 std::vector<float> m_weights;
87 };
88}