From 0d29203e24a8bc2c75278931a6bd25b2ae5848de Mon Sep 17 00:00:00 2001
From: Gordon MacPherson <gordon@gordonite.tech>
Date: Wed, 21 Aug 2019 20:29:46 +0100
Subject: [PATCH 1/3] Prototype unit system for assimp generic enough to be
 used across formats.

---
 code/FBX/FBXImporter.cpp             |  6 ++-
 code/PostProcessing/ScaleProcess.cpp | 10 ++++-
 include/assimp/BaseImporter.h        | 62 +++++++++++++++++++++++++++-
 include/assimp/config.h.in           |  7 ++++
 4 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/code/FBX/FBXImporter.cpp b/code/FBX/FBXImporter.cpp
index ec8bbd2b4..6a851b9ee 100644
--- a/code/FBX/FBXImporter.cpp
+++ b/code/FBX/FBXImporter.cpp
@@ -189,8 +189,12 @@ void FBXImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS
         if (settings.convertToMeters) {
             unit = FbxUnit::m;
         }
+
         // convert the FBX DOM to aiScene
-        ConvertToAssimpScene(pScene,doc, settings.removeEmptyBones, unit);
+        ConvertToAssimpScene(pScene, doc, settings.removeEmptyBones, unit);
+        
+        // Set file scale relative to meters
+        SetFileScale( doc.GlobalSettings().UnitScaleFactor() );
 
         std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
     }
diff --git a/code/PostProcessing/ScaleProcess.cpp b/code/PostProcessing/ScaleProcess.cpp
index 2a605d6bc..ac770c41f 100644
--- a/code/PostProcessing/ScaleProcess.cpp
+++ b/code/PostProcessing/ScaleProcess.cpp
@@ -43,7 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <assimp/scene.h>
 #include <assimp/postprocess.h>
-
+#include <assimp/BaseImporter.h>
 
 namespace Assimp {
 
@@ -69,7 +69,15 @@ bool ScaleProcess::IsActive( unsigned int pFlags ) const {
 }
 
 void ScaleProcess::SetupProperties( const Importer* pImp ) {
+    // User scaling
     mScale = pImp->GetPropertyFloat( AI_CONFIG_GLOBAL_SCALE_FACTOR_KEY, 1.0f );
+
+    // File scaling * Application Scaling
+    float importerScale = pImp->GetPropertyFloat( AI_CONFIG_APP_SCALE_KEY, 1.0f );
+
+    // apply scale to the scale 
+    // helps prevent bugs with backward compatibility for anyone using normal scaling.
+    mScale *= importerScale;
 }
 
 void ScaleProcess::Execute( aiScene* pScene ) {
diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h
index 48dfc8ed8..3244fd74f 100644
--- a/include/assimp/BaseImporter.h
+++ b/include/assimp/BaseImporter.h
@@ -48,8 +48,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <vector>
 #include <set>
+#include <map>
 #include <assimp/types.h>
 #include <assimp/ProgressHandler.hpp>
+#include <assimp/ai_assert.h>
 
 struct aiScene;
 struct aiImporterDesc;
@@ -161,14 +163,72 @@ public:
      *  some loader features. Importers must provide this information. */
     virtual const aiImporterDesc* GetInfo() const = 0;
 
+    /**
+     * Will be called only by scale process when scaling is requested.
+     */
+    virtual void SetFileScale(double scale)
+    {
+        fileScale = scale;
+    }
+
+    virtual double GetFileScale() const
+    {
+        return fileScale;
+    }
+
+    enum ImporterUnits {
+        M,
+        MM,
+        CM,
+        INCHES,
+        FEET
+    };
+
+    /**
+     * Assimp Importer
+     * unit conversions available 
+     * if you need another measurment unit add it below.
+     * it's currently defined in assimp that we prefer meters.
+     * */
+    std::map<ImporterUnits, double> importerUnits = {
+        {ImporterUnits::M, 1},
+        {ImporterUnits::CM, 0.01},
+        {ImporterUnits::MM, 0.001},
+        {ImporterUnits::INCHES, 0.0254},
+        {ImporterUnits::FEET, 0.3048}
+    };
+
+    virtual void SetApplicationUnits( const ImporterUnits& unit )
+    {
+        importerScale = importerUnits[unit];
+        applicationUnits = unit;
+    }
+
+    virtual const ImporterUnits& GetApplicationUnits()
+    {
+        return applicationUnits;
+    }
+
+    /* Returns scale used by application called by ScaleProcess */
+    const double GetImporterScale()
+    {
+        ai_assert(importerScale != 0);
+        ai_assert(fileScale != 0);
+        return importerScale * fileScale;
+    }
+
     // -------------------------------------------------------------------
     /** Called by #Importer::GetExtensionList for each loaded importer.
      *  Take the extension list contained in the structure returned by
      *  #GetInfo and insert all file extensions into the given set.
      *  @param extension set to collect file extensions in*/
     void GetExtensionList(std::set<std::string>& extensions);
+    
+protected:    
+    ImporterUnits applicationUnits = ImporterUnits::M;
+    double importerScale = 1.0;
+    double fileScale = 1.0;
 
-protected:
 
     // -------------------------------------------------------------------
     /** Imports the given file into the given scene structure. The
diff --git a/include/assimp/config.h.in b/include/assimp/config.h.in
index dad43710e..3a6379bf4 100644
--- a/include/assimp/config.h.in
+++ b/include/assimp/config.h.in
@@ -999,6 +999,13 @@ enum aiComponent
 #   define AI_CONFIG_GLOBAL_SCALE_FACTOR_DEFAULT  1.0f
 #endif // !! AI_DEBONE_THRESHOLD
 
+#define AI_CONFIG_APP_SCALE_KEY "APP_SCALE_FACTOR"
+
+#if (!defined AI_CONFIG_APP_SCALE_KEY)
+#   define AI_CONFIG_APP_SCALE_KEY 1.0
+#endif // AI_CONFIG_APP_SCALE_KEY
+
+
 // ---------- All the Build/Compile-time defines ------------
 
 /** @brief Specifies if double precision is supported inside assimp

From ffc19790fbe802483044c982bd35c887b614255c Mon Sep 17 00:00:00 2001
From: Gordon MacPherson <gordon@gordonite.tech>
Date: Wed, 21 Aug 2019 22:22:23 +0100
Subject: [PATCH 2/3] Depreciated compiler which doesn't support standard
 features

---
 appveyor.yml | 2 --
 1 file changed, 2 deletions(-)

diff --git a/appveyor.yml b/appveyor.yml
index 851d9c096..3729ea028 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -14,7 +14,6 @@ matrix:
   fast_finish: true
     
 image:
-  - Visual Studio 2013
   - Visual Studio 2015
   - Visual Studio 2017
     
@@ -27,7 +26,6 @@ configuration: Release
 install:
   - set PATH=C:\Ruby24-x64\bin;%PATH%
   - set CMAKE_DEFINES -DASSIMP_WERROR=ON
-  - if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2013" set CMAKE_GENERATOR_NAME=Visual Studio 12 2013
   - if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2015" set CMAKE_GENERATOR_NAME=Visual Studio 14 2015
   - if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2017" set CMAKE_GENERATOR_NAME=Visual Studio 15 2017
   - if "%platform%"=="x64" set CMAKE_GENERATOR_NAME=%CMAKE_GENERATOR_NAME% Win64

From db8606ba5723bb26a3c0aa0698b43403e9f4a0fa Mon Sep 17 00:00:00 2001
From: Gordon MacPherson <gordon@gordonite.tech>
Date: Wed, 21 Aug 2019 23:32:31 +0100
Subject: [PATCH 3/3] Fix typo

---
 include/assimp/BaseImporter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h
index 3244fd74f..79a9b1b94 100644
--- a/include/assimp/BaseImporter.h
+++ b/include/assimp/BaseImporter.h
@@ -210,7 +210,7 @@ public:
     }
 
     /* Returns scale used by application called by ScaleProcess */
-    const double GetImporterScale()
+    double GetImporterScale() const
     {
         ai_assert(importerScale != 0);
         ai_assert(fileScale != 0);