LCOV - code coverage report
Current view: top level - source/collada - CommonConvert.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 12 17 70.6 %
Date: 2026-12-03 01:19:39 Functions: 3 4 75.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             : #ifndef INCLUDED_COMMONCONVERT
      19             : #define INCLUDED_COMMONCONVERT
      20             : 
      21             : #include <FCDocument/FCDExtra.h>
      22             : #include <FCDocument/FCDocument.h>
      23             : #include <FMath/FMMatrix44.h>
      24             : #include <FUtils/FUError.h>
      25             : #include <FUtils/Platforms.h>
      26             : #include <cstddef>
      27             : #include <exception>
      28             : #include <libxml/tree.h>
      29             : #include <memory>
      30             : #include <string>
      31             : #include <vector>
      32             : 
      33             : class FCDControllerInstance;
      34             : class FCDEntityInstance;
      35             : class FCDSceneNode;
      36             : class FCDSkinController;
      37             : 
      38             : class Skeleton;
      39             : 
      40             : class ColladaException : public std::exception
      41             : {
      42             : public:
      43           6 :     ColladaException(const std::string& msg) : msg(msg) { }
      44           6 :     ~ColladaException() throw() { }
      45           2 :     virtual const char* what() const throw() { return msg.c_str(); }
      46             : private:
      47             :     std::string msg;
      48             : };
      49             : 
      50           6 : struct OutputCB
      51             : {
      52           6 :     virtual ~OutputCB() { }
      53             :     virtual void operator() (const char* data, unsigned int length)=0;
      54             : };
      55             : 
      56             : /**
      57             :  * Standard error handler - logs FCollada messages using Log(), and also
      58             :  * maintains a list of XML parser errors.
      59             :  */
      60             : class FColladaErrorHandler
      61             : {
      62             : public:
      63             :     FColladaErrorHandler(std::string& xmlErrors);
      64             :     ~FColladaErrorHandler();
      65             : 
      66             : private:
      67             :     void OnError(FUError::Level errorLevel, uint32 errorCode, uint32 lineNumber);
      68             :     std::string& xmlErrors;
      69             : 
      70             :     void operator=(FColladaErrorHandler);
      71             : };
      72             : 
      73             : /**
      74             :  * Standard document loader. Based on FCDocument::LoadFromText, but allows
      75             :  * access to \<extra\> nodes at the document level (i.e. directly in \<COLLADA\>).
      76             :  */
      77           6 : class FColladaDocument
      78             : {
      79             : public:
      80             :     /**
      81             :      * Loads the document from the given XML string. Should be the first function
      82             :      * called on this object, and should only be called once.
      83             :      * @throws ColladaException if unable to load.
      84             :      */
      85             :     void LoadFromText(const char* text);
      86             : 
      87             :     /** Returns the FCDocument that was loaded. */
      88          15 :     FCDocument* GetDocument() const { return document.get(); }
      89             : 
      90             :     /** Returns the \<extra\> data from the \<COLLADA\> element. */
      91           0 :     FCDExtra* GetExtra() const { return extra.get(); }
      92             : 
      93             : private:
      94             :     void ReadExtras(xmlNode* colladaNode);
      95             :     std::unique_ptr<FCDocument> document;
      96             :     std::unique_ptr<FCDExtra> extra;
      97             : };
      98             : 
      99             : /**
     100             :  * Wrapper for code shared between the PMD and PSA converters. Loads the document
     101             :  * and provides access to the relevant objects and values.
     102             :  */
     103             : class CommonConvert
     104             : {
     105             : public:
     106             :     CommonConvert(const char* text, std::string& xmlErrors);
     107             :     ~CommonConvert();
     108           0 :     const FColladaDocument& GetDocument() const { return m_Doc; }
     109           0 :     FCDSceneNode& GetRoot() { return *m_Doc.GetDocument()->GetVisualSceneRoot(); }
     110          15 :     FCDEntityInstance& GetInstance() { return *m_Instance; }
     111           0 :     const FMMatrix44& GetEntityTransform() const { return m_EntityTransform; }
     112          10 :     bool IsYUp() const { return m_YUp; }
     113           0 :     bool IsXSI() const { return m_IsXSI; }
     114             : 
     115             : private:
     116             :     FColladaErrorHandler m_Err;
     117             :     FColladaDocument m_Doc;
     118             :     FCDEntityInstance* m_Instance;
     119             :     FMMatrix44 m_EntityTransform;
     120             :     bool m_YUp;
     121             :     bool m_IsXSI;
     122             : };
     123             : 
     124             : /** Throws a ColladaException unless the value is true. */
     125             : #define REQUIRE(value, message) require_(__LINE__, value, "Assertion not satisfied", "failed requirement \"" message "\"")
     126             : 
     127             : /** Throws a ColladaException unless the status is successful. */
     128             : #define REQUIRE_SUCCESS(status) require_(__LINE__, status, "FCollada error", "Line " STRINGIFY(__LINE__))
     129             : #define STRINGIFY(x) #x
     130             : 
     131             : void require_(int line, bool value, const char* type, const char* message);
     132             : 
     133             : /** Outputs a structure, using sizeof to get the size. */
     134        1424 : template<typename T> void write(OutputCB& output, const T& data)
     135             : {
     136        1399 :     output((char*)&data, sizeof(T));
     137         361 : }
     138             : 
     139             : /**
     140             :  * Tries to find a single suitable entity instance in the scene. Fails if there
     141             :  * are none, or if there are too many and it's not clear which one should
     142             :  * be converted.
     143             :  *
     144             :  * @param node root scene node to search under
     145             :  * @param instance output - the found entity instance (if any)
     146             :  * @param transform - the world-space transform of the found entity
     147             :  *
     148             :  * @return true if one was found
     149             :  */
     150             : bool FindSingleInstance(FCDSceneNode* node, FCDEntityInstance*& instance, FMMatrix44& transform);
     151             : 
     152             : /**
     153             :  * Like FCDSkinController::ReduceInfluences but works correctly.
     154             :  * Additionally, multiple influences for the same joint-vertex pair are
     155             :  * collapsed into a single influence.
     156             :  */
     157             : void SkinReduceInfluences(FCDSkinController* skin, size_t maxInfluenceCount, float minimumWeight);
     158             : 
     159             : /**
     160             :  * Fixes some occasional problems with the skeleton root definitions in a
     161             :  * controller. (In particular, it's needed for models exported from XSI.)
     162             :  * Should be called before extracting any joint information from the controller.
     163             :  */
     164             : void FixSkeletonRoots(FCDControllerInstance& controllerInstance);
     165             : 
     166             : /**
     167             :  * Finds the skeleton definition which best matches the given controller.
     168             :  * @throws ColladaException if none is found.
     169             :  */
     170             : const Skeleton& FindSkeleton(const FCDControllerInstance& controllerInstance);
     171             : 
     172             : /** Bone pose data */
     173             : struct BoneTransform
     174             : {
     175             :     float translation[3];
     176             :     float orientation[4];
     177             : };
     178             : 
     179             : /**
     180             :  * Performs the standard transformations on bones, applying a scale matrix and
     181             :  * moving them into the game's coordinate space.
     182             :  */
     183             : void TransformBones(std::vector<BoneTransform>& bones, const FMMatrix44& scaleTransform, bool yUp);
     184             : 
     185             : extern FMMatrix44 FMMatrix44_Identity;
     186             : 
     187             : #endif // INCLUDED_COMMONCONVERT

Generated by: LCOV version 1.16