Visualization Toolkit - How to read and render multiple objects? - c++

I'm trying to read and render multiple files in a directory (when combined they form an object) using vtk. But so far I'm getting the following error:
ERROR: In D:\VTK\VTK-src\IO\XML\vtkXMLReader.cxx, line 283
vtkXMLPolyDataReader (00D1B560): Error opening file D:\3d models\Dist\.
ERROR: In D:\VTK\VTK-src\Common\ExecutionModel\vtkExecutive.cxx, line 782
vtkCompositeDataPipeline (00CC2078): Algorithm
vtkXMLPolyDataReader(00D1B560) returned failure for request: vtkInformation
(00D20688)
Debug: Off
Modified Time: 8721
Reference Count: 1
Registered Events: (none)
Request: REQUEST_DATA
FORWARD_DIRECTION: 0
ALGORITHM_AFTER_FORWARD: 1
FROM_OUTPUT_PORT: 0
So far what I've tried is reading only 1 file instead of multiple files, but I still the error I mentioned above.
Here's the coding I'm working on:
int main(int argc, char *argv[])
{
std::string directoryName = "D:\\3d models\\Dist\\" ;
vtkSmartPointer<vtkDirectory> directory = vtkSmartPointer<vtkDirectory>::New();
int opened = directory->Open(directoryName.c_str());
if(!opened)
{
std::cout << "No es posible abrir este directorio!" << std::endl;
return EXIT_FAILURE;
}
int numberOfFiles = directory->GetNumberOfFiles();
std::cout << "NUmero de archivos: " << numberOfFiles << std::endl;
for (int i = 0; i < numberOfFiles; i++)
{
std::string fileString = directoryName;
////fileString += "/";
fileString += directory->GetFile(i);
std::string ext = vtksys::SystemTools::GetFilenameLastExtension(fileString);
std::cout << fileString.c_str() << " extension: " << ext << std::endl;
std::string name = vtksys::SystemTools::GetFilenameWithoutLastExtension(fileString);
std::cout << "nombre: " << name << std::endl;
const char*cstr = fileString.c_str();
std::cout << cstr << endl;
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(cstr);
reader->Update();
reader->GetOutput();
vtkSmartPointer<vtkTransform> transform =
vtkSmartPointer<vtkTransform>::New();
transform->Scale(.005, .005, .005);
vtkSmartPointer<vtkTransformFilter> transformFilter =
vtkSmartPointer<vtkTransformFilter>::New();
transformFilter->SetInputConnection(reader->GetOutputPort());
transformFilter->SetTransform(transform);
// Visualizar
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(transformFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetPosition(1.1, .5, .1);
actor->SetMapper(mapper);
vtkSmartPointer<vtkOpenVRRenderer> renderer =
vtkSmartPointer<vtkOpenVRRenderer>::New();
vtkSmartPointer<vtkOpenVRRenderWindow> renderWindow =
vtkSmartPointer<vtkOpenVRRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkOpenVRRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkOpenVRRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkOpenVRCamera> cam;
renderer->SetActiveCamera(cam);
renderer->AddActor(actor);
renderer->SetBackground(.2, .3, .4);
renderWindow->Render();
renderWindowInteractor->Start();
}
return EXIT_SUCCESS;
}
What am I missing? I've tried with different type of files but I still haven't been able to read and render anything using this method.

It looks like the first entry when you list the files in a folder is "." (which is normal), so the path to the file you try to open is "D:\3d models\Dist\." and it is not a valid file for vtkXMLPolyDataReader.
You should only try to open vtk files written with vtkXMLPolyDataWriter.
For example by checking the extension is ".vtp" (or whatever extension you used to save the files containing your vtkPolyData models).
Check the extension in the first part of your loop:
for (int i = 0; i < numberOfFiles; i++)
{
std::string fileString = directoryName;
////fileString += "/";
fileString += directory->GetFile(i);
std::string ext = vtksys::SystemTools::GetFilenameLastExtension(fileString);
std::cout << fileString.c_str() << " extension: " << ext << std::endl;
// add this line here to skip "." and "..", also fix the extension if not .xml
if (ext.find(".vtp") == std::string::npos) continue;
(...)
I also suspect you are trying to use vtkXMLPolyDataReader while you should use vtkPolyDataReader (this really depends on what writer was used to produce the files). vtkXMLPolyDataReader is the standard reader for .vtp files.
Last, you are creating a renderer, a rendering window and a camera inside the loop: it means one window per object. It's uncommon, is that what you want?
Long story short: you need at least one renderer and one window to display one or many models. Your models are represented by an actor each: the window is where the renderer draws (one or many) actors. A renderer is a rendering pass: of course you can have one rendering pass for each actor, but unless you are sure you need it, you don't.
Here's your code, modified as I would do at your place:
int main(int argc, char *argv[])
{
std::string directoryName = "D:\\3d models\\Dist\\" ;
vtkSmartPointer<vtkOpenVRRenderer> renderer =
vtkSmartPointer<vtkOpenVRRenderer>::New();
vtkSmartPointer<vtkOpenVRRenderWindow> renderWindow =
vtkSmartPointer<vtkOpenVRRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkOpenVRRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkOpenVRRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkOpenVRCamera> cam;
renderer->SetActiveCamera(cam);
renderer->SetBackground(.2, .3, .4);
vtkSmartPointer<vtkDirectory> directory = vtkSmartPointer<vtkDirectory>::New();
int opened = directory->Open(directoryName.c_str());
if(!opened)
{
std::cout << "No es posible abrir este directorio!" << std::endl;
return EXIT_FAILURE;
}
int numberOfFiles = directory->GetNumberOfFiles();
std::cout << "NUmero de archivos: " << numberOfFiles << std::endl;
for (int i = 0; i < numberOfFiles; i++)
{
std::string fileString = directoryName;
////fileString += "/";
fileString += directory->GetFile(i);
std::string ext = vtksys::SystemTools::GetFilenameLastExtension(fileString);
std::cout << fileString.c_str() << " extension: " << ext << std::endl;
if (ext.find(".vtp") == std::string::npos) continue;
std::string name = vtksys::SystemTools::GetFilenameWithoutLastExtension(fileString);
std::cout << "nombre: " << name << std::endl;
const char*cstr = fileString.c_str();
std::cout << cstr << endl;
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(cstr);
reader->Update();
reader->GetOutput();
vtkSmartPointer<vtkTransform> transform =
vtkSmartPointer<vtkTransform>::New();
transform->Scale(.005, .005, .005);
vtkSmartPointer<vtkTransformFilter> transformFilter =
vtkSmartPointer<vtkTransformFilter>::New();
transformFilter->SetInputConnection(reader->GetOutputPort());
transformFilter->SetTransform(transform);
// Visualizar
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(transformFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetPosition(1.1, .5, .1);
actor->SetMapper(mapper);
renderer->AddActor(actor);
}
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}

Related

Add multiple files from buffers to ZIP archive using libzip

I'm trying to use libzip in a program that needs to archive several data chunks in different files. At the moment I have a code similar to the following snippet, edited from in-memory.c example in libzip examples.
The zip file is correctly saved with the files inside, but each file contains garbage.
Any help is appreciated.
bool push_files(zip_t* za) {
for (int i = 0; i < 10; i++) {
// Generate data
std::stringstream ss;
ss << "Test file #" << i;
std::string a = ss.str();
zip_source_t* source = zip_source_buffer(za, a.c_str(), a.size(), 0);
if (source == NULL) {
std::cerr << "error creating source: " << zip_strerror(za) << std::endl;
return false;
}
// Add buffer with filename
std::stringstream fname;
fname << "TEST-" << i;
a = fname.str();
if (zip_file_add(za, a.c_str(), source, ZIP_FL_ENC_UTF_8) < 0) {
std::cerr << "error adding source: " << zip_strerror(za) << std::endl;
return false;
}
}
return true;
}
int main() {
zip_source_t* src;
zip_error_t error;
zip_t* za;
zip_error_init(&error);
if ((src = zip_source_buffer_create(NULL, 0, 1, &error)) == NULL) {
std::cerr << "can't create source: " << zip_error_strerror(&error) << std::endl;
zip_error_fini(&error);
return 1;
}
if ((za = zip_open_from_source(src, ZIP_TRUNCATE, &error)) == NULL) {
std::cerr << "can't open zip from source: " << zip_error_strerror(&error) << std::endl;
zip_source_free(src);
zip_error_fini(&error);
return 1;
}
zip_error_fini(&error);
zip_source_keep(src);
if (!push_files(za))
return -1;
if (zip_close(za) < 0) {
std::cerr << "can't close zip archive" << zip_strerror(za) << std::endl;
return 1;
}
// ... omissis, save archive to file as in in-memory.c
}
zip_source_buffer does not copy the data out of the buffer - it just creates a zip_source_t which points to the same buffer. So you must keep the buffer alive until you're done adding the file.
Your code does not keep the buffer alive. The buffer you use is a.c_str() which is the data buffer of the string a. Fair enough, so far. But then before adding the file, you reassign the variable a = fname.str(); which (probably) frees that buffer and allocates a new one.
Solution: use a separate variable for the filename. Don't overwrite a until the file has been added.

Unable to find/read config file .conf - FileIOException

I'm trying to open and read a .conf file in my program using libconfig::readFile(), but it gives me always a FileIOException. I think that the program is unable to locate the file but I don't know where the problem is.
This is the .conf file code:
controlLoopPeriod = 100.0; # ms
saturationMax = [10.0];
saturationMin = [-10.0];
task = { Linear_Velocity = {
type = 0; # 0 equality, 1 inequality
gain = 1.0;
enable = true;
saturation = 10.0;
};
};
priorityLevels = ( { name = "PL_JPosition";
tasks = ["Joint_Position"];
lambda = 0.0001;
threshold = 0.01;
}
);
actions = ( { name = "jPosition";
levels = ["PL_JPosition"];
}
);
states = { State_move = {
minHeadingError = 0.05;
maxHeadingError = 0.2;
};
};
This is where I'm trying to open the file and read it:
bool RobotPositionController::LoadConfiguration() {
libconfig::Config confObj;
// Inizialization
std::string confPath = "home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";
confPath.append("/conf/");
confPath.append(fileName_);
std::cout << "PATH TO CONF FILE : " << confPath << std::endl;
// Read the file. If there is an error, report it and exit.
try {
confObj.readFile(confPath.c_str());
} catch (const libconfig::FileIOException& fioex) {
std::cerr << "I/O error while reading file: " << fioex.what() << std::endl;
return -1;
} catch (const libconfig::ParseException& pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl;
return -1;
}
conf_->ConfigureFromFile(confObj);
ConfigureTaskFromFile(tasksMap_, confObj);
ConfigurePriorityLevelsFromFile(actionManager_, tasksMap_, confObj);
// Set Saturation values for the iCAT (read from conf file)
iCat_->SetSaturation(conf_->saturationMin, conf_->saturationMax);
ConfigureActionsFromFile(actionManager_, confObj);
//insert states in the map
statesMap_.insert({ states::ID::jPos, stateJPosition_ });
ConfigureSatesFromFile(statesMap_, confObj);
//insert command in the map
commandsMap_.insert({ commands::ID::jPos, commandJPosition_ });
return true;
}
Thanks in advance for any type of help.
It looks to me you are missing a / at the beginning of your home path.
I.e., change:
std::string confPath = "home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";
For:
std::string confPath = "/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot";
I take it you have checked the .conf file exists at:
/home/nimblbot/tpik_ctr_algo/ctrl_rob_nimblbot/conf

Cannot rename rosbag file after recording

I would like to record a rosbag file with a specific name; however, I only know it after the recording is halfway done, so I cannot set the filename here:
cfg.enable_record_to_file("some/path/filename.bag");
I have tried to rename the new file as shown below without satisfactory success. I have also used std::experimental::filesystem::rename, with the same outcome. What did work was to record a second video (on the fly) and only then rename the first. This indicates that the (both) rename functions work, but I cannot change the filename it as it seems the file is still open in my current program.
int main(int argc, char* argv[])
{
rs2::config cfg;
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
device.as<rs2::recorder>().pause();
pipe->stop();
int res = std::rename("tmp.bag", "test.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
return 0;
}
I wonder how to 'unload' the produced video from the pipeline (pipe->stop() did not work), so I can rename the generated rosbag files on the fly.
As suggested by #TedLyngmo, adding curly brackets allowed me to change the filename on the fly. Code as follows:
int main(int argc, char* argv[])
{
{
rs2::config cfg;
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
device.as<rs2::recorder>().pause();
pipe->stop();
}
int res = std::rename("tmp.bag", "test.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
return 0;
}
EDIT
I studied rs-record-playback a bit more and the code below fixes the renaming issue.
int main(int argc, char* argv[])
{
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
pipe->start();
device = pipe->get_active_profile().get_device();
if (!device.as<rs2::recorder>())
{
pipe->stop(); // Stop the pipeline with the default configuration
pipe = std::make_shared<rs2::pipeline>();
rs2::config cfg;
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
}
// record for 1 sec, if conditions can be added to check if recorder is initialised
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// if condition can be added to check if recording has been completed
pipe->stop();
pipe = std::make_shared<rs2::pipeline>(); // reset the shared pointer
pipe->start();
device = pipe->get_active_profile().get_device();
int res = std::rename("tmp.bag", "testing.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
pipe->stop();
pipe = std::make_shared<rs2::pipeline>();
}

How to use caffemodel with OpenCV on iOS?

I am trying to use a .caffemodel alongside OpenCV on iOS devices. I found this github repository, but it can only be built with Xcode 6. I am working with Xcode 7, but I also downloaded Xcode 6 and still have no success on building it.
How can I use a caffemodel with OpenCV on iOS 9?
PS: The alternative would be this but it's written with swift & metal and I need to be able to use it with OpenCV.
You can use OpenCV DNN contrib module.
You need first to build OpenCV with contrib modules, you can find the steps here.
Then you can import and use the .caffemodel following this tutorial.
Here is an updated version of the tutorial, since it's not working as is:
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
/* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
{
Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
Point classNumber;
minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;
}
std::vector<String> readClassNames(const char *filename = "synset_words.txt")
{
std::vector<String> classNames;
std::ifstream fp(filename);
if (!fp.is_open())
{
std::cerr << "File with classes labels not found: " << filename << std::endl;
exit(-1);
}
std::string name;
while (!fp.eof())
{
std::getline(fp, name);
if (name.length())
classNames.push_back( name.substr(name.find(' ')+1) );
}
fp.close();
return classNames;
}
int main(int argc, char **argv)
{
cv::dnn::initModule();
String modelTxt = "bvlc_googlenet.prototxt";
String modelBin = "bvlc_googlenet.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
Ptr<dnn::Importer> importer;
try //Try to import Caffe GoogleNet model
{
importer = dnn::createCaffeImporter(modelTxt, modelBin);
}
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
std::cerr << err.msg << std::endl;
}
if (!importer)
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << modelTxt << std::endl;
std::cerr << "caffemodel: " << modelBin << std::endl;
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
exit(-1);
}
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
Mat img = imread(imageFile);
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
resize(img, img, Size(224, 224)); //GoogLeNet accepts only 224x224 RGB-images
dnn::Blob inputBlob = dnn::Blob(img); //Convert Mat to dnn::Blob batch of images
net.setBlob(".data", inputBlob); //set the network input
net.forward(); //compute output
dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer
int classId;
double classProb;
getMaxClass(prob, &classId, &classProb);//find the best class
std::vector<String> classNames = readClassNames();
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
return 0;
} //main
I will post another answer because with recent versions there are some differences.
First of all now dnn is already inside the standard OpenCV library so you do not have to build it from contrib_modules.
The function to load the network is readNetFromCaffe.
For example the following code load the NN:
std::string modelName = "path/to/mymodel.caffemodel";
std::string protoName = "path/to/deploy.prototxt";
cv::dnn::Net net;
try
{
net = cv::dnn::readNetFromCaffe(protoName, modelName);
}
catch (cv::Exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << protoName << std::endl;
std::cerr << "caffemodel: " << modelName << std::endl;
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
exit(-1);
}
}
Then you can run the NN:
cv::Mat res_mat;
float res;
cv::Mat inputBlob = cv::dnn::blobFromImage(roi, 1.0f, cv::Size(227, 227),
cv::Scalar(0, 0, 0), false);
net.setInput(inputBlob);
//During the forward pass output of each network layer is computed,
//but in this example we need output from "prob" layer only.
res_mat = net.forward("score");
std::cout<<res_mat<<std::endl;
res_mat = res_mat.reshape(1, 1); //reshape the blob to 1x2 matrix
return res_mat.at<float>(0);
The function cv::dnn::blobFromImage resize the image to the input network size specified by the third argument (in my case cv::Size(227, 227)). The argument cv::Scalar(0, 0, 0) is to subtract the means from the three BGR channels.
score is the name of the output layer in the NN I used. You can see this information in the prototxt file.

I got an error with sdl2 ttf and emscripten

i was porting my game to emscripten. Everything was fine until SDL_ttf.
Actually i am using sdl2 + sdl2 image + sdl mixer.
I will show an example:
SDL_Color color = {255,255,255};
std::cout << "1\n";
font = TTF_OpenFont("saucery/font/font1.otf", 8);
if (!font)
printf("Unable to load font: %s \n", TTF_GetError());
std::cout << "2\n";
SDL_Surface *surf = TTF_RenderText_Solid(font,"Oieee",color);
std::cout << "3\n";
if (surf){
std::cout << (int)surf << "\n";
texture = SDL_CreateTextureFromSurface(Game::instance->GetRenderer(),surf);
std::cout << "4\n";
Uint32 format;
int acess,w,h;
SDL_QueryTexture(texture, &format,&acess,&w,&h);
dimensions2.x = 0;
dimensions2.y = 0;
dimensions2.h = h;
dimensions2.w = w;
SDL_FreeSurface(surf);
}
In this code i just open an font (i have already changed the size and the type to .ttf). Everything seems to run fine until:
SDL_CreateTextureFromSurface
I put some std::cout to see where the code was "crashing". Everytime it calls the createTextureFrom on the console shows to me "45" and the running stops there.
Even with any std::cout i still get this error.
this is driving me crazy already ._.