| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "merger.hpp" | ||
| 2 | |||
| 3 | namespace assmpq::merger { | ||
| 4 | |||
| 5 | ✗ | void recalculate_aabb(assmpq::merger::MeshData &mesh) | |
| 6 | { | ||
| 7 | ✗ | aiVector3D min(assmpq::merger::kHighestValue, assmpq::merger::kHighestValue, assmpq::merger::kHighestValue); | |
| 8 | ✗ | aiVector3D max(assmpq::merger::kLowestValue, assmpq::merger::kLowestValue, assmpq::merger::kLowestValue); | |
| 9 | |||
| 10 | ✗ | for (const auto& vertex : mesh.vertices) { | |
| 11 | ✗ | min.x = std::min(vertex.x, min.x); | |
| 12 | ✗ | min.y = std::min(vertex.y, min.y); | |
| 13 | ✗ | min.z = std::min(vertex.z, min.z); | |
| 14 | |||
| 15 | ✗ | max.x = std::max(vertex.x, max.x); | |
| 16 | ✗ | max.y = std::max(vertex.y, max.y); | |
| 17 | ✗ | max.z = std::max(vertex.z, max.z); | |
| 18 | } | ||
| 19 | |||
| 20 | ✗ | mesh.aabb.mMin = min; | |
| 21 | ✗ | mesh.aabb.mMax = max; | |
| 22 | ✗ | } | |
| 23 | |||
| 24 | ✗ | void transform_mesh_to_base_xz(assmpq::merger::MeshData &mesh) | |
| 25 | { | ||
| 26 | ✗ | for(size_t i = 0; i < mesh.vertices.size(); i++) { | |
| 27 | // coord | ||
| 28 | ✗ | aiVector3D &vertex = mesh.vertices[i]; | |
| 29 | ✗ | vertex.x = -vertex.x; | |
| 30 | ✗ | std::swap(vertex.y, vertex.x); | |
| 31 | ✗ | std::swap(vertex.y, vertex.z); | |
| 32 | ✗ | vertex.z = -vertex.z; | |
| 33 | |||
| 34 | // normal | ||
| 35 | ✗ | aiVector3D &normal = mesh.normals[i]; | |
| 36 | ✗ | normal.x = -normal.x; | |
| 37 | ✗ | std::swap(normal.y, normal.x); | |
| 38 | ✗ | std::swap(normal.y, normal.z); | |
| 39 | ✗ | normal.z = -normal.z; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | recalculate_aabb(mesh); | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | ✗ | void scale_mesh(assmpq::merger::MeshData &mesh, float scale_factor) | |
| 46 | { | ||
| 47 | ✗ | for(auto& vertex: mesh.vertices) { | |
| 48 | ✗ | vertex.x *= scale_factor; | |
| 49 | ✗ | vertex.y *= scale_factor; | |
| 50 | ✗ | vertex.z *= scale_factor; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | recalculate_aabb(mesh); | |
| 54 | ✗ | } | |
| 55 | |||
| 56 | ✗ | auto split_ramp_mesh(const assmpq::merger::MeshData& mesh, assmpq::merger::MeshData& out_mesh_0, assmpq::merger::MeshData& out_mesh_1)-> bool | |
| 57 | { | ||
| 58 | ✗ | const aiAABB& aabb = mesh.aabb; | |
| 59 | ✗ | if (std::abs(std::abs(aabb.mMax.x) - (kW3MapCellSize * 2.0F)) < assmpq::merger::kEpsilonValue) { | |
| 60 | // Spilt by x axis | ||
| 61 | ✗ | for (const auto& face: mesh.faces) { | |
| 62 | ✗ | aiVector3D vertex0 = mesh.vertices[face.i0]; | |
| 63 | ✗ | aiVector3D vertex1 = mesh.vertices[face.i1]; | |
| 64 | ✗ | aiVector3D vertex2 = mesh.vertices[face.i2]; | |
| 65 | |||
| 66 | ✗ | if (std::abs(vertex0.x) < (kW3MapCellSize + kEpsilonValue) && | |
| 67 | ✗ | std::abs(vertex1.x) < (kW3MapCellSize + kEpsilonValue) && | |
| 68 | ✗ | std::abs(vertex2.x) < (kW3MapCellSize + kEpsilonValue)) { | |
| 69 | |||
| 70 | ✗ | auto from_idx = static_cast<uint32_t>(out_mesh_0.vertices.size()); | |
| 71 | |||
| 72 | ✗ | out_mesh_0.vertices.push_back(vertex0); | |
| 73 | ✗ | out_mesh_0.vertices.push_back(vertex1); | |
| 74 | ✗ | out_mesh_0.vertices.push_back(vertex2); | |
| 75 | |||
| 76 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i0]); | |
| 77 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i1]); | |
| 78 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i2]); | |
| 79 | |||
| 80 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i0]); | |
| 81 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i1]); | |
| 82 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i2]); | |
| 83 | |||
| 84 | ✗ | out_mesh_0.faces.push_back( | |
| 85 | ✗ | assmpq::merger::TriFace { | |
| 86 | .i0 = from_idx, | ||
| 87 | ✗ | .i1 = from_idx + 1, | |
| 88 | ✗ | .i2 = from_idx + 2 | |
| 89 | } | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | ✗ | if (std::abs(vertex0.x) > (kW3MapCellSize - kEpsilonValue) && | |
| 94 | ✗ | std::abs(vertex1.x) > (kW3MapCellSize - kEpsilonValue) && | |
| 95 | ✗ | std::abs(vertex2.x) > (kW3MapCellSize - kEpsilonValue)) { | |
| 96 | |||
| 97 | ✗ | auto from_idx = static_cast<uint32_t>(out_mesh_1.vertices.size()); | |
| 98 | |||
| 99 | ✗ | vertex0.x -= kW3MapCellSize; | |
| 100 | ✗ | vertex1.x -= kW3MapCellSize; | |
| 101 | ✗ | vertex2.x -= kW3MapCellSize; | |
| 102 | |||
| 103 | ✗ | out_mesh_1.vertices.push_back(vertex0); | |
| 104 | ✗ | out_mesh_1.vertices.push_back(vertex1); | |
| 105 | ✗ | out_mesh_1.vertices.push_back(vertex2); | |
| 106 | |||
| 107 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i0]); | |
| 108 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i1]); | |
| 109 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i2]); | |
| 110 | |||
| 111 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i0]); | |
| 112 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i1]); | |
| 113 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i2]); | |
| 114 | |||
| 115 | ✗ | out_mesh_1.faces.push_back( | |
| 116 | ✗ | assmpq::merger::TriFace { | |
| 117 | .i0 = from_idx, | ||
| 118 | ✗ | .i1 = from_idx + 1, | |
| 119 | ✗ | .i2 = from_idx + 2 | |
| 120 | } | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | ✗ | } else if (std::abs(std::abs(aabb.mMin.z) - (kW3MapCellSize * 2.0F)) < assmpq::merger::kEpsilonValue) { | |
| 125 | // Spilt by z axis | ||
| 126 | ✗ | for (const auto& face: mesh.faces) { | |
| 127 | ✗ | aiVector3D vertex0 = mesh.vertices[face.i0]; | |
| 128 | ✗ | aiVector3D vertex1 = mesh.vertices[face.i1]; | |
| 129 | ✗ | aiVector3D vertex2 = mesh.vertices[face.i2]; | |
| 130 | |||
| 131 | ✗ | if (std::abs(vertex0.z) < (kW3MapCellSize + kEpsilonValue) && | |
| 132 | ✗ | std::abs(vertex1.z) < (kW3MapCellSize + kEpsilonValue) && | |
| 133 | ✗ | std::abs(vertex2.z) < (kW3MapCellSize + kEpsilonValue)) { | |
| 134 | |||
| 135 | ✗ | auto from_idx = static_cast<uint32_t>(out_mesh_0.vertices.size()); | |
| 136 | |||
| 137 | ✗ | out_mesh_0.vertices.push_back(vertex0); | |
| 138 | ✗ | out_mesh_0.vertices.push_back(vertex1); | |
| 139 | ✗ | out_mesh_0.vertices.push_back(vertex2); | |
| 140 | |||
| 141 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i0]); | |
| 142 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i1]); | |
| 143 | ✗ | out_mesh_0.normals.push_back(mesh.normals[face.i2]); | |
| 144 | |||
| 145 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i0]); | |
| 146 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i1]); | |
| 147 | ✗ | out_mesh_0.uvs.push_back(mesh.uvs[face.i2]); | |
| 148 | |||
| 149 | ✗ | out_mesh_0.faces.push_back( | |
| 150 | ✗ | assmpq::merger::TriFace { | |
| 151 | .i0 = from_idx, | ||
| 152 | ✗ | .i1 = from_idx + 1, | |
| 153 | ✗ | .i2 = from_idx + 2 | |
| 154 | } | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | |||
| 158 | ✗ | if (std::abs(vertex0.z) > (kW3MapCellSize - kEpsilonValue) && | |
| 159 | ✗ | std::abs(vertex1.z) > (kW3MapCellSize - kEpsilonValue) && | |
| 160 | ✗ | std::abs(vertex2.z) > (kW3MapCellSize - kEpsilonValue)) { | |
| 161 | |||
| 162 | ✗ | auto from_idx = static_cast<uint32_t>(out_mesh_1.vertices.size()); | |
| 163 | |||
| 164 | ✗ | vertex0.z += kW3MapCellSize; | |
| 165 | ✗ | vertex1.z += kW3MapCellSize; | |
| 166 | ✗ | vertex2.z += kW3MapCellSize; | |
| 167 | |||
| 168 | ✗ | out_mesh_1.vertices.push_back(vertex0); | |
| 169 | ✗ | out_mesh_1.vertices.push_back(vertex1); | |
| 170 | ✗ | out_mesh_1.vertices.push_back(vertex2); | |
| 171 | |||
| 172 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i0]); | |
| 173 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i1]); | |
| 174 | ✗ | out_mesh_1.normals.push_back(mesh.normals[face.i2]); | |
| 175 | |||
| 176 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i0]); | |
| 177 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i1]); | |
| 178 | ✗ | out_mesh_1.uvs.push_back(mesh.uvs[face.i2]); | |
| 179 | |||
| 180 | ✗ | out_mesh_1.faces.push_back( | |
| 181 | ✗ | assmpq::merger::TriFace { | |
| 182 | .i0 = from_idx, | ||
| 183 | ✗ | .i1 = from_idx + 1, | |
| 184 | ✗ | .i2 = from_idx + 2 | |
| 185 | } | ||
| 186 | ); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } else { | ||
| 190 | ✗ | return false; | |
| 191 | } | ||
| 192 | |||
| 193 | ✗ | out_mesh_0.name = mesh.name + "0"; | |
| 194 | ✗ | out_mesh_1.name = mesh.name + "1"; | |
| 195 | |||
| 196 | ✗ | recalculate_aabb(out_mesh_0); | |
| 197 | ✗ | recalculate_aabb(out_mesh_1); | |
| 198 | ✗ | return true; | |
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | } // namespace assmpq::merger | ||
| 203 |