Radium Engine  1.5.20
Loading...
Searching...
No Matches
CameraLoader.cpp
1#include <IO/CameraLoader/CameraLoader.hpp>
2
3#include <Core/Asset/FileData.hpp>
4#include <Core/Utils/StringUtils.hpp>
5
6#include <fstream>
7#include <iostream>
8#include <string>
9
10const std::string camExt { "cam" };
11
12namespace Ra {
13namespace IO {
14
15using namespace Core::Utils; // log
16using namespace Core::Asset;
17
18std::vector<std::string> CameraFileLoader::getFileExtensions() const {
19 return std::vector<std::string>( { "*." + camExt } );
20}
21
22bool CameraFileLoader::handleFileExtension( const std::string& extension ) const {
23 return extension == camExt;
24}
25
26FileData* CameraFileLoader::loadFile( const std::string& filename ) {
27 // Create the FileData
28 auto fileData = new FileData( filename );
29 if ( !fileData->isInitialized() ) {
30 delete fileData;
31 LOG( logERROR ) << "[CameraLoader] Filedata cannot be initialized.";
32 return nullptr;
33 }
34 if ( fileData->isVerbose() ) { LOG( logERROR ) << "[CameraLoader] File Loading begin..."; }
35
37 std::ifstream ss( filename );
38 if ( !ss.is_open() ) {
39 delete fileData;
40 LOG( logERROR ) << "[CameraLoader] Camera file " << filename << " cannot be read.";
41 return nullptr;
42 }
43
45 std::clock_t startTime = std::clock();
46 std::string str;
47 int type;
48 Scalar M[16]; // 4x4 view matrix;
49 Scalar fov, znear, zfar, zoom, aspect;
50 ss >> str;
51 bool result = !ss.fail();
52 ss >> type;
53 result &= !ss.fail();
54 for ( uint i = 0; i < 16; ++i ) {
55 ss >> M[i];
56 result &= !ss.fail();
57 }
58 ss >> fov >> znear >> zfar >> zoom >> aspect;
59 result &= !ss.fail();
60 if ( !result ) {
61 delete fileData;
62 LOG( logERROR ) << "[CameraLoader] Could not load camera file data: " << filename;
63 return nullptr;
64 }
65 std::clock_t endTime = std::clock();
66
68 Camera* camera = new Camera();
69 // camera->setName( Core::Utils::getBaseName( filename, false ) );
70 camera->setType( Camera::ProjType( type ) );
71 Core::Matrix4 frame;
72 frame << M[0], M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9], M[10], M[11], M[12], M[13],
73 M[14], M[15];
74 camera->setFrame( Core::Transform { frame } );
75 camera->setFOV( fov );
76 camera->setZNear( znear );
77 camera->setZFar( zfar );
78 camera->setZoomFactor( zoom );
79 camera->setViewport( aspect, 1 );
80 fileData->m_cameraData.emplace_back( camera );
81 if ( fileData->isVerbose() ) { fileData->displayInfo(); }
82
83 fileData->m_loadingTime = ( endTime - startTime ) / Scalar( CLOCKS_PER_SEC );
84 fileData->m_processed = true;
85 return fileData;
86}
87
88std::string CameraFileLoader::name() const {
89 return "CameraLoader";
90}
91
92} // namespace IO
93} // namespace Ra
ProjType
Define the projection type.
Definition Camera.hpp:19
T clock(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3