LCOV - code coverage report
Current view: top level - source/collada - GeomReindex.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 90 122 73.8 %
Date: 2026-12-03 01:19:39 Functions: 4 10 40.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "GeomReindex.h"
      21             : 
      22             : #include <FCDocument/FCDGeometryMesh.h>
      23             : #include <FCDocument/FCDGeometryPolygons.h>
      24             : #include <FCDocument/FCDGeometryPolygonsInput.h>
      25             : #include <FCDocument/FCDGeometrySource.h>
      26             : #include <FCDocument/FCDSkinController.h>
      27             : #include <FMath/FMath.h>
      28             : #include <FUtils/FUDaeEnum.h>
      29             : #include <FUtils/Platforms.h>
      30             : #include <algorithm>
      31             : #include <cassert>
      32             : #include <cmath>
      33             : #include <cstring>
      34             : #include <map>
      35             : #include <utility>
      36             : #include <vector>
      37             : 
      38             : typedef std::pair<float, float> uv_pair_type;
      39             : 
      40         447 : struct VertexData
      41             : {
      42        1008 :     VertexData(const float* pos, const float* norm, const std::vector<uv_pair_type> &uvs,
      43             :            const std::vector<FCDJointWeightPair>& weights)
      44        1008 :         : x(pos[0]), y(pos[1]), z(pos[2]),
      45        1008 :         nx(norm[0]), ny(norm[1]), nz(norm[2]),
      46        1008 :         uvs(uvs),
      47        1008 :         weights(weights)
      48             :     {
      49        1008 :     }
      50             : 
      51             :     float x, y, z;
      52             :     float nx, ny, nz;
      53             :     std::vector<uv_pair_type> uvs;
      54             :     std::vector<FCDJointWeightPair> weights;
      55             : };
      56             : 
      57           0 : bool similar(float a, float b)
      58             : {
      59           0 :     return (fabsf(a - b) < 0.000001f);
      60             : }
      61             : 
      62           0 : bool operator==(const FCDJointWeightPair& a, const FCDJointWeightPair& b)
      63             : {
      64           0 :     return (a.jointIndex == b.jointIndex && similar(a.weight, b.weight));
      65             : }
      66             : 
      67           0 : bool operator<(const FCDJointWeightPair& a, const FCDJointWeightPair& b)
      68             : {
      69             :     // Sort by decreasing weight, then by increasing joint ID
      70           0 :     if (a.weight > b.weight)
      71             :         return true;
      72           0 :     else if (a.weight < b.weight)
      73             :         return false;
      74           0 :     else if (a.jointIndex < b.jointIndex)
      75             :         return true;
      76             :     else
      77           0 :         return false;
      78             : }
      79             : 
      80           0 : bool operator==(const uv_pair_type& a, const uv_pair_type& b)
      81             : {
      82           0 :     return similar(a.first, b.first) && similar(a.second, b.second);
      83             : }
      84             : 
      85           0 : bool operator==(const VertexData& a, const VertexData& b)
      86             : {
      87           0 :     return (similar(a.x,  b.x)  && similar(a.y,  b.y)  && similar(a.z,  b.z)
      88           0 :          && similar(a.nx, b.nx) && similar(a.ny, b.ny) && similar(a.nz, b.nz)
      89           0 :          && (a.uvs == b.uvs)
      90           0 :          && (a.weights == b.weights));
      91             : }
      92             : 
      93       10587 : bool operator<(const VertexData& a, const VertexData& b)
      94             : {
      95             : #define CMP(f) if (a.f < b.f) return true; if (a.f > b.f) return false
      96       10587 :     CMP(x);  CMP(y);  CMP(z);
      97        1834 :     CMP(nx); CMP(ny); CMP(nz);
      98        1834 :     CMP(uvs);
      99        1294 :     CMP(weights);
     100             : #undef CMP
     101             :     return false;
     102             : }
     103             : 
     104             : template <typename T>
     105           0 : struct InserterWithoutDuplicates
     106             : {
     107           5 :     InserterWithoutDuplicates(std::vector<T>& vec) : vec(vec)
     108             :     {
     109             :     }
     110             : 
     111        1008 :     size_t add(const T& val)
     112             :     {
     113        1008 :         typename std::map<T, size_t>::iterator it = btree.find(val);
     114        1008 :         if (it != btree.end())
     115         647 :             return it->second;
     116             : 
     117         361 :         size_t idx = vec.size();
     118         361 :         vec.push_back(val);
     119         361 :         btree.insert(std::make_pair(val, idx));
     120         361 :         return idx;
     121             :     }
     122             : 
     123             :     std::vector<T>& vec;
     124             :     std::map<T, size_t> btree; // for faster lookups (so we can build a duplicate-free list in O(n log n) instead of O(n^2))
     125             : 
     126             : private:
     127             :     InserterWithoutDuplicates& operator=(const InserterWithoutDuplicates&);
     128             : };
     129             : 
     130           0 : void CanonicaliseWeights(std::vector<FCDJointWeightPair>& weights)
     131             : {
     132             :     // Convert weight-lists into a standard format, so simple vector equality
     133             :     // can be used to determine equivalence
     134           0 :     std::sort(weights.begin(), weights.end());
     135           0 : }
     136             : 
     137           5 : void ReindexGeometry(FCDGeometryPolygons* polys, FCDSkinController* skin)
     138             : {
     139             :     // Given geometry with:
     140             :     //   positions, normals, texcoords, bone blends
     141             :     // each with their own data array and index array, change it to
     142             :     // have a single optimised index array shared by all vertexes.
     143             : 
     144           5 :     FCDGeometryPolygonsInput* inputPosition = polys->FindInput(FUDaeGeometryInput::POSITION);
     145           5 :     FCDGeometryPolygonsInput* inputNormal   = polys->FindInput(FUDaeGeometryInput::NORMAL);
     146           5 :     FCDGeometryPolygonsInput* inputTexcoord = polys->FindInput(FUDaeGeometryInput::TEXCOORD);
     147             : 
     148           5 :     size_t numVertices = polys->GetFaceVertexCount();
     149             : 
     150           5 :     assert(inputPosition->GetIndexCount() == numVertices);
     151           5 :     assert(inputNormal  ->GetIndexCount() == numVertices);
     152           5 :     assert(inputTexcoord->GetIndexCount() == numVertices);
     153             : 
     154           5 :     const uint32* indicesPosition = inputPosition->GetIndices();
     155           5 :     const uint32* indicesNormal   = inputNormal->GetIndices();
     156           5 :     const uint32* indicesTexcoord = inputTexcoord->GetIndices();
     157             : 
     158           5 :     assert(indicesPosition);
     159           5 :     assert(indicesNormal);
     160           5 :     assert(indicesTexcoord); // TODO - should be optional, because textureless meshes aren't unreasonable
     161             : 
     162           5 :     FCDGeometrySourceList texcoordSources;
     163           5 :     polys->GetParent()->FindSourcesByType(FUDaeGeometryInput::TEXCOORD, texcoordSources);
     164             : 
     165           5 :     FCDGeometrySource* sourcePosition = inputPosition->GetSource();
     166           5 :     FCDGeometrySource* sourceNormal   = inputNormal  ->GetSource();
     167             : 
     168           5 :     const float* dataPosition = sourcePosition->GetData();
     169           5 :     const float* dataNormal   = sourceNormal  ->GetData();
     170             : 
     171           5 :     if (skin)
     172             :     {
     173             : #ifndef NDEBUG
     174             :         size_t numVertexPositions = sourcePosition->GetDataCount() / sourcePosition->GetStride();
     175             :         assert(skin->GetInfluenceCount() == numVertexPositions);
     176             : #endif
     177             :     }
     178             : 
     179           5 :     uint32 stridePosition = sourcePosition->GetStride();
     180           5 :     uint32 strideNormal   = sourceNormal  ->GetStride();
     181             : 
     182           5 :     std::vector<uint32> indicesCombined;
     183           5 :     std::vector<VertexData> vertexes;
     184           5 :     InserterWithoutDuplicates<VertexData> inserter(vertexes);
     185             : 
     186        1013 :     for (size_t i = 0; i < numVertices; ++i)
     187             :     {
     188        1008 :         std::vector<FCDJointWeightPair> weights;
     189        1008 :         if (skin)
     190             :         {
     191           0 :             FCDSkinControllerVertex* influences = skin->GetVertexInfluence(indicesPosition[i]);
     192           0 :             assert(influences != NULL);
     193           0 :             for (size_t j = 0; j < influences->GetPairCount(); ++j)
     194             :             {
     195           0 :                 FCDJointWeightPair* pair = influences->GetPair(j);
     196           0 :                 assert(pair != NULL);
     197           0 :                 weights.push_back(*pair);
     198             :             }
     199           0 :             CanonicaliseWeights(weights);
     200             :         }
     201             : 
     202        1008 :         std::vector<uv_pair_type> uvs;
     203        2016 :         for (size_t set = 0; set < texcoordSources.size(); ++set)
     204             :         {
     205        1008 :             const float* dataTexcoord = texcoordSources[set]->GetData();
     206        1008 :             uint32 strideTexcoord = texcoordSources[set]->GetStride();
     207             : 
     208        1008 :             uv_pair_type p;
     209        1008 :             p.first = dataTexcoord[indicesTexcoord[i]*strideTexcoord];
     210        1008 :             p.second = dataTexcoord[indicesTexcoord[i]*strideTexcoord + 1];
     211        1008 :             uvs.push_back(p);
     212             :         }
     213             : 
     214        1008 :         VertexData vtx (
     215        1008 :             &dataPosition[indicesPosition[i]*stridePosition],
     216        1008 :             &dataNormal  [indicesNormal  [i]*strideNormal],
     217             :             uvs,
     218             :             weights
     219        1008 :         );
     220        1008 :         size_t idx = inserter.add(vtx);
     221        1008 :         indicesCombined.push_back((uint32)idx);
     222        2016 :     }
     223             : 
     224             :     // TODO: rearrange indicesCombined (and rearrange vertexes to match) to use
     225             :     // the vertex cache efficiently
     226             :     // (<http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html> etc)
     227             : 
     228           5 :     FloatList newDataPosition;
     229           5 :     FloatList newDataNormal;
     230           5 :     FloatList newDataTexcoord;
     231           5 :     std::vector<std::vector<FCDJointWeightPair> > newWeightedMatches;
     232             : 
     233         366 :     for (size_t i = 0; i < vertexes.size(); ++i)
     234             :     {
     235         361 :         newDataPosition.push_back(vertexes[i].x);
     236         361 :         newDataPosition.push_back(vertexes[i].y);
     237         361 :         newDataPosition.push_back(vertexes[i].z);
     238         361 :         newDataNormal  .push_back(vertexes[i].nx);
     239         361 :         newDataNormal  .push_back(vertexes[i].ny);
     240         361 :         newDataNormal  .push_back(vertexes[i].nz);
     241         361 :         newWeightedMatches.push_back(vertexes[i].weights);
     242             :     }
     243             : 
     244             :     // (Slightly wasteful to duplicate this array so many times, but FCollada
     245             :     // doesn't seem to support multiple inputs with the same source data)
     246           5 :     inputPosition->SetIndices(&indicesCombined.front(), indicesCombined.size());
     247           5 :     inputNormal  ->SetIndices(&indicesCombined.front(), indicesCombined.size());
     248           5 :     inputTexcoord->SetIndices(&indicesCombined.front(), indicesCombined.size());
     249             : 
     250          10 :     for (size_t set = 0; set < texcoordSources.size(); ++set)
     251             :     {
     252           5 :         newDataTexcoord.clear();
     253         366 :         for (size_t i = 0; i < vertexes.size(); ++i)
     254             :         {
     255         361 :             newDataTexcoord.push_back(vertexes[i].uvs[set].first);
     256         361 :             newDataTexcoord.push_back(vertexes[i].uvs[set].second);
     257             :         }
     258           5 :         texcoordSources[set]->SetData(newDataTexcoord, 2);
     259             :     }
     260             : 
     261           5 :     sourcePosition->SetData(newDataPosition, 3);
     262           5 :     sourceNormal  ->SetData(newDataNormal,   3);
     263             : 
     264           5 :     if (skin)
     265             :     {
     266           0 :         skin->SetInfluenceCount(newWeightedMatches.size());
     267           0 :         for (size_t i = 0; i < newWeightedMatches.size(); ++i)
     268             :         {
     269           0 :             skin->GetVertexInfluence(i)->SetPairCount(0);
     270           0 :             for (size_t j = 0; j < newWeightedMatches[i].size(); ++j)
     271           0 :                 skin->GetVertexInfluence(i)->AddPair(newWeightedMatches[i][j].jointIndex, newWeightedMatches[i][j].weight);
     272             :         }
     273             :     }
     274          30 : }

Generated by: LCOV version 1.16