How to read text(ASCII) files with Assimp? - c++

I can successfully read binary files of different extensions(fbx, blend, ifc and etc.) with assimp but if file is not binary assimp failed reading.
#include <string>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/Importer.hpp>
int main() {
Assimp::Importer importer;
std::string path = "myFile.ifc";
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenNormals);
if (scene == nullptr
|| scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE
|| scene->mRootNode == nullptr) {
return false;
}
// ProcessNode(scene->mRootNode, scene);
return 0;
}
I did not find any flags in docs for solving this problem. And nowhere is it said about file types.
For example, if .fbx file in ASCII format - aiScene* is just nullptr but if .ifc file in ASCII program throw exception
enter image description here

The Asset-Importer-Library will detect the kind of data automatically. So when loading an IFC-File the loader will take care which kind of import much be used.
So the problem you are dealing with is not a missing ASCII-Import-Option you have forgotten.
My first guess would be that there is an issue created by your IFC-File WOuld it be possible to check it? You can create an issue-report with our issue-tracker

Related

assimp fail to find the obj. file

I try to use Assimp::Importer.ReadFile() to load my obj.file but it turns out that assimp fail to find the file correctly.
Here is a simple test
#include<string>
#include<assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
using namespace std;
int main(){
Assimp::Importer importer;
string modelPath = "D:\\素材\\nanosuit\\nanosuit.obj";
const aiScene* scene = importer.ReadFile(modelPath, aiProcess_Triangulate | aiProcess_FlipUVs );
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
cout << "ERROR::ASSIMP::" << importer.GetErrorString() << endl;
}
else cout << scene;
and the output is following text
ERROR::ASSIMP::Unable to open file "D:\素材\nanosuit\nanosuit.obj".
I haved tried to load different obj.file and it doesn't work too
This is a problem with the unicode name. You need to use a workaround to fix this. Use the Wi32-API-Call
SetCurrentDirectory(L"D:\\素材\\nanosuit\\");
to open the folder and import the asset afterwards:
const aiScene* scene = importer.ReadFile(L"nanosuit.obj", aiProcess_Triangulate | aiProcess_FlipUVs );
There is a design error in the Asset-Importer-Lib. The imported name will be used to name the asset and the asset-name is using ASCII. So unicode names will interpreted as ASCII-names and this will cause your error.

WinAPI FileExists Function Implemetation

I am coding a simple replacement for std::filesystem::exists() function using Windows API. Surprisingly, it turned out to be pretty hard. I want to keep my code simple, so I am using minimum functions. My function of choice is GetFileAttributesW(). Code is tested with fs::recursive_directory_iterator() function. My function thinks that all files in “C:\Windows\servicing\LCU*” don’t exist (ERROR_PATH_NOT_FOUND). This directory is responsible for storing Windows Update Caches and is famous for having extremely long file names. I couldn’t find anything else about this directory. Example of filenames and my code are included below. Hope this helps!
Edited:
The solution to this problem is to prepend absolute file path with “\\?\” char sequence. It makes Windows handle short files correctly!
C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2006.1.7\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.19041.1741_none_ee5d4a8d060d7653\f\new360videossquare44x44logo.targetsize-16_altform-unplated_contrast-black.png
C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2006.1.7\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.19041.1741_none_ee5d4a8d060d7653\f\new360videossquare44x44logo.targetsize-16_altform-unplated_contrast-white.png
C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2006.1.7\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.19041.1741_none_ee5d4a8d060d7653\f\new360videossquare44x44logo.targetsize-20_altform-unplated_contrast-black.png
C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2006.1.7\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.19041.1741_none_ee5d4a8d060d7653\f\new360videossquare44x44logo.targetsize-20_altform-unplated_contrast-white.png
#include <windows.h>
#include <filesystem>
#include <iostream>
#include <string>
using namespace std;
namespace fs = std::filesystem;
int FileExists(wstring file_path) {
/* TODO:
1. Doesn't work with "C:\\Windows\\servicing\\LCU\\*".
2. Improve error system.
*/
DWORD attributes = GetFileAttributesW(file_path.c_str());
// Valid attributes => File exists
if (attributes != INVALID_FILE_ATTRIBUTES) {
return true;
}
DWORD error_code = GetLastError();
wcout << error_code << ' ' << file_path << '\n';
// Path related error => File doesn't exist
if (error_code == ERROR_PATH_NOT_FOUND || error_code == ERROR_INVALID_NAME ||
error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_BAD_NETPATH)
{
return false;
}
// Other errors are logged before if statement
// File is busy with IO operations, etc.
return error_code;
}
int main() {
for (fs::path path : fs::recursive_directory_iterator("C:\\", fs::directory_options::skip_permission_denied)) {
FileExists(path);
}
return 0;
}
The solution that worked for me is to prepend absolute file path with “\\?\” char sequence. Somehow, it makes Windows handle shortened file paths correctly!
Check out MSDN Article "Maximum File Path Limitation" for more info.

Validating if a JSON object is present in a file with nlohmann JSON C++ library

I have a project where I import JSON files to setup global variables. There are various possibilities to the JSON object names in the files, so I want to know how can I check if an object is there or not. The way I tried to do it (with an if statement as shown below) causes an error
The program '..\..\build\jacky.exe' has exited with code 3 (0x00000003).
So it does not work at all. Is there a way to do this? Thanks!
#include <iostream>
#include <string>
#include <fstream>
#include "json.hpp" // json library by nlohmann
std::ifstream gJsonFile;
nlohmann::json j;
int var;
int main(int argc, char *argv[])
{
gJsonFile.open(/* whatever json file path */, std::ifstream::in);
if (gJsonFile.is_open())
{
j << gJsonFile;
if (j["Shirt Size"]) // causes exit with code 3 if false
var = (j["Shirt Size"]);
else
var = (j["AB Circumference"]);
}
}
if ( j.find("Shirt Size") != j.end() )
Or (this will create an entry if it does not already exist)
if ( !j["Shirt Size"].is_null() )

Program with SPI_SETDESKWALLPAPER Function Only Changes the Desktop Background to the Color Black when Trying to Change it to an Image using C++

I am trying to change the desktop background/wallpaper to a different image with a .png file. Although when I run the program, the background turns to solid black instead.
I am certain that I typed the file name, "ksa.png", correctly in my code to be the image I want to be on my background. I used an if condition to write out the last error on a file when the error occurred and used an else condition to write out "Success" if no errors occurred; but when I run the program, it writes "Success" to the file. I have thought about using a .jpg file instead, thinking that maybe .png files just don't work. I'll give an update when I tried using that.
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"ksa.png";
std::ofstream log;
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log.open("log.txt");
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
When I run this program, the desktop background is suppose to be set as the image "ksa.png". Instead it's solid black. Any help is appreciated for making this work, thank you.
UPDATE
Okay so I updated the code to where it would run a .jpg file and I'm still getting the same result. Also I moved the line log.open("log.txt") command before the SystemParametersInfo() function like Remy Lebeau suggested and it still writes out "Success" to the file. I'm still having the same problem.
Here is my updated code:
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"3.jpg";
std::ofstream log;
log.open("log.txt");
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
Emmmm,there is a problem with your picture path. I've tried your code. You can't get pictures under relative paths unless you use absolute paths.
Like Cody Gray♦'s judgment .
const wchar_t *filenm = L"C:\\Users\\strives\\Desktop\\timg.bmp";

How to give folder of images as input to Magick++ api?

I am need of passing a folder of images as input to Magick++ api. It can be done using mogrify in commandline as shown in post "ImageMagick script to resize folder of images". Reading a single image could be done through api call as
Image image(inputimage)
But how could we do the same for a folder of images? Can anyone help me with the respective api call?
That feature is not included in the Magick++ API. You will need to iterate the directory yourself and then use the Magick++ API to read and write the image. You can find an example on how to iterate through a folder in C/C++ in the following Stack Overflow post: How can I get the list of files in a directory using C or C++?.
I believe you would be responsible of reading the directory. The C library dirent.h is the first thing I think of, but I'm sure there's better C++/system/framework techniques.
#include <iostream>
#include <vector>
#include <dirent.h>
#include <Magick++.h>
int main(int argc, const char * argv[]) {
std::vector<Magick::Image> stack; // Hold images found
DIR * dir_handler = opendir("/tmp/images"); // Open dir
struct dirent * dir_entry;
if (dir_handler != NULL)
{
// Iterate over entries in directory
while ( (dir_entry = readdir(dir_handler)) != NULL ) {
// Only act on regular files
if (dir_entry->d_type == DT_REG) {
// Concatenate path (could be better)
std::string filename("/tmp/images/");
filename += dir_entry->d_name;
// Read image at path
stack.push_back(Magick::Image(filename));
}
}
closedir(dir_handler); // House keeping
} else {
// Handle DIR error
}
// Append all images into single montage
Magick::Image output;
Magick::appendImages(&output, stack.begin(), stack.end());
output.write("/tmp/all.png");
return 0;
}
There's also ExpandFilenames(int *,char ***) in the MagickCore library.
// Patterns to scan
int pattern_count = 1;
// First pattern
char pattern[PATH_MAX] = "/tmp/images/*.png";
// Allocate memory for list of patterns
char ** dir_pattern = (char **)MagickCore::AcquireMagickMemory(sizeof(char *));
// Assign first pattern
dir_pattern[0] = pattern;
// Expand patterns
Magick::MagickBooleanType ok;
ok = MagickCore::ExpandFilenames(&pattern_count, &dir_pattern);
if (ok == Magick::MagickTrue) {
std::vector<Magick::Image> stack;
// `pattern_count' now holds results count
for ( int i = 0; i < pattern_count; ++i) {
// `dir_pattern' has been re-allocated with found results
std::string filename(dir_pattern[i]);
stack.push_back(Magick::Image(filename));
}
Magick::Image output;
Magick::appendImages(&output, stack.begin(), stack.end());
output.write("/tmp/all.png");
} else {
// Error handle
}