How to delete faces from mesh using openmesh? - c++

Pretty much the title I am trying to delete a few faces of a mesh using open mesh, like this:
MyMesh mesh;
char fname[1024];
sprintf(fname, "box_%i.obj", 0);
if (!OpenMesh::IO::read_mesh(mesh, std::string(fname)))
{
std::cerr << "read error\n";
exit(1);
}
MyMesh::FaceIter v_it, v_end(mesh.faces_end());
uint count = 0;
for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
{
mesh.delete_face(*v_it, true);
}
This is segfaulting on the first call to delete_face.
However, writing this mesh (without trying to delete faces):
if (!OpenMesh::IO::write_mesh(mesh, std::string("open_mesh.obj")))
{
std::cerr << "write error\n";
exit(1);
}
Works perfectly fine and blender can open the obj. So the issue very much seems to be with how I am trying to delete the faces.
The docs don;t seem to provide any explanation as to why this is not valid:
https://www.graphics.rwth-aachen.de/media/openmesh_static/Documentations/OpenMesh-Doc-Latest/a02618.html#ae20c4e746b52c34ace6a7b805fbd61ac

I think iterator being invalid after removing an element in it.
for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
{
mesh.delete_face(*v_it, true);
v_it = mesh.faces_begin(); //<- add this to test
}

Related

SFML/C++ | Drawing text from a vector gives access violation error

I'm making my own text editor with C++ and the SFML multi-media library. I'm now trying to implement a user interface so users can interact and select documents to edit. I have this function:
std::vector<sf::Text> Menu::GetAllTextOptions(TextHandler& handler, sf::Font font) {
std::vector<TextDocument>* all_available_documents = handler.GetTextDocuments();
std::vector<sf::Text> menu_text_options;
for (int i = 0; i < all_available_documents->size(); i++) {
sf::Text text;
text.setFont(font);
text.setString(std::to_string(i) + ") " + all_available_documents->at(i).GetDocumentName() + '\n');
menu_text_options.push_back(text);
}
return menu_text_options;
}
What this is supposed to do is collect all of the names of already existing text documents in a path that I have specified (the code behind gathering the TextDocument objects works perfectly). It then makes a text object, sets a font to it and sets the string to the name of the document. When I std::cout the count of the vector, it displays correctly the amount of documents that exist.
I then have a function that is supposed to go through the vector of texts and draw all of them, so they can be later displayed:
// ACCESS VIOLATION ERROR
void Menu::DrawAllOptions(sf::RenderWindow& window, std::vector<sf::Text>&
menu_text_options) {
std::cout << menu_text_options.size() << " is the current size " << std::endl;
try {
for (int i = 0; i < menu_text_options.size(); i++) {
// EXCEPTION THROWN HERE
window.draw(menu_text_options.at(i));
}
}
catch (const std::exception& ex) {
std::cout << "Exception: " << ex.what() << '\n';
}
}
Finally, I have the main loop where everything comes into place:
std::string Menu::MenuControl(TextHandler& handler) {
Utility utility;
sf::RenderWindow menu_window(sf::VideoMode(800, 600), "Menu");
sf::Color backgroundColor(0, 32, 63);
sf::Event event;
sf::Font font;
sf::Text menu_text;
utility.CheckFontLoaded(font, "../8bitfont.ttf");
std::vector<sf::Text> menu_text_options = GetAllTextOptions(handler, font);
int menu_counter = 0; // Start at the first document, count goes up and down
with arrows
menu_text.setOrigin(sf::Vector2f(-20.f, 0.f));
menu_text.setFont(font);
while (menu_window.isOpen()) {
// DRAWING
menu_window.clear(backgroundColor); // Setting menu background color
//window.draw(background_overlay);
DrawAllOptions(menu_window, menu_text_options);
// DISPLAYING
menu_window.display();
// TO DO - ABSTRACT USER INPUT LOGIC
while (menu_window.pollEvent(event)) {
switch (event.type)
{
case sf::Event::Closed:
menu_window.close();
return "Test";
break;
case sf::Event::TextEntered:
if (event.text.unicode < 128) {
}
break;
case sf::Event::KeyReleased:
break;
}
}
}
return "Not correctly working";
}
The exact error I get is:
Exception thrown at 0x00007FFA3E2869E9 (sfml-graphics-d-2.dll) in texteditor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
At this point, I've attempted putting a try catch block, initializing the vector with new and making everything within the main loop function. I am sensing its something to do with the Text objects that I push into the vector, but I tried initializing them differently as well with no success. Any suggestions would be greatly appreciated!
The answer to the problem, as mentioned by #Botje, is that the function Menu::GetAllTextOptions was getting a copy of font, which ultimately ended up not being loaded and resulting in an access violation.

Magick++ gif annotation artefacts

I want to make an annotation in a gif image using ImageMagick and specifically Magick++, the code is quite simple, but the output image has artifacts in the text, if the gif does not move, then the artifacts do not appear, here:
here is the code itself, very simple:
int main()
{
InitializeMagick(nullptr);
list<Image> *imageList = new list<Image>();
try {
readImages(imageList, "source.gif" );
}
catch( exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
for(Image &i : *imageList) {
i.fontPointsize(36);
i.font("helvetica-bold");
i.fillColor(Color("white"));
i.fontStyle(MagickCore::ObliqueStyle);
i.annotate("why are you lagging", SouthGravity);
}
writeImages(imageList->begin(), imageList->end(), "/home/deymos/GifnText/n.gif");
return 0;
I tried to save the list of images as png and this is what I saw when the movement on the gif started:
is it possible to fix it, or is it a bug?
If this cannot be fixed, maybe there are similar tools with which I can implement text on gifs? or at least a library that can disassemble gifs into images and fold, I can do the rest with qt, tnx.

Problems Displaying Mat Image in Windows Forms C++ Visual Studio 2019

I am new to C++ gui design and I am not too familiar with using pointers. Recently I have run into a few problems when trying to get an OpenCV Mat Image to display in a PictureBox in a constructed gui. I have searching online and even found a very similar post to my question but when trying to implement the guidance I ran into an exception when trying to operate the gui.
A very similar post
Displaying webcam feed in cv::Mat format in a picturebox
Code I grabbed from this post to save you a click:
void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
System::Drawing::Graphics^ graphics = control->CreateGraphics();
System::IntPtr ptr(colorImage.ptr());
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
System::Drawing::RectangleF rect(0,0,control->Width,control->Height);
graphics->DrawImage(b,rect);
delete graphics;
}
Now I am trying to display a "video" feed (really an array of cv::Mat Objects) but I am having a
"Source Not Available"/System.ArgumentException: 'Parameter is not valid.'
screen come up when I attempt to call the function that houses that playback. I also know specific line of code that throws the issue is the line
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
Now for specifics, the form has an event (currently the click on the picture box, but I want to move to a "play" button in the future) and on this event, the code:
private: System::Void leftEyeImage_Click(System::Object^ sender, System::EventArgs^ e) {
std::cout << "Click Received" << std::endl;
cv::Mat& frame = cv::imread("Desktop/testcapture.png");
std::cout << "Import successful" << std::endl;
drawLeftEye(this, frame);
}
This code does get execute on click and starts the function drawLeftEye. The function is a modified version of the code from the other post and is below:
This is on the ShowResults.cpp file
namespace DevProject {
void ShowResults::drawLeftEye(System::Windows::Forms::Control^ control, cv::Mat& framebmp) {
System::Drawing::Graphics^ graphics = control->CreateGraphics();
std::cout << "Now Here" << std::endl;
System::IntPtr ptr(framebmp.data);
// Issue line
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(framebmp.cols, framebmp.rows, framebmp.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr);
std::cout << "Converted successfully" << std::endl;
System::Drawing::RectangleF rect(0, 0, control->Width, control->Height); //No issue
graphics->DrawImage(b, rect);
std::cout << "Now Here before delete" << std::endl;
delete graphics;
//delete b;
}
}
I know based on my cout statements that I do make it into my function and I know my code compiles and runs through the function if I comment out the line:
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(framebmp.cols, framebmp.rows, framebmp.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr);
//As well as (but only because b is defined in the line above^
graphics->DrawImage(b, rect);
I am not quite sure how to fix this, my friend mentioned this could be a memory issue but I don't know how I would go about fixing it. It is also very possible I have made a simple mistake in the design of this or where my pointers are going and I am just not competent enough to know the error.
By the way the pointer for "this" that is called in the function is linked to the Windows Autogenerated code (from gui construction) of:
this->leftEyeImage->Anchor = System::Windows::Forms::AnchorStyles::None;
this->leftEyeImage->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->leftEyeImage->Location = System::Drawing::Point(11, 836);
this->leftEyeImage->Name = L"leftEyeImage";
this->leftEyeImage->Size = System::Drawing::Size(991, 646);
this->leftEyeImage->TabIndex = 4;
this->leftEyeImage->TabStop = false;
this->leftEyeImage->Click += gcnew System::EventHandler(this, &ShowResults::leftEyeImage_Click);
Any and all advice or tests would be very appreciated as I am both interested in the answer and the reasoning. Thanks in advance!

Text over picture

Is it possible to create text displayed on a picture with nana?
I tried this
int main() {
using namespace nana;
form fm;
place plc(fm);
picture pic(fm);
label lbl(fm, "LBL", true);
pic.load(paint::image("xxx.png"));
pic.caption("PIC");
pic.align::center, align_v::center);
lbl.transparent(true);
lbl.text_align(align::center, align_v::center);
plc.div("<<here>>");
plc["here"] << pic;
plc["here"] << lbl; // (1)
plc.collocate();
lbl.move(pic.pos());
fm.show();
exec();
}
But its shifting both to the left as nana creates a grid for 2 elements in the layout. Without adding it, so deleting (1), the label will not be shown in the layout at all.
I couldnt find any information on it online. nana::image::caption(std::string) method seems to be ignored
Is there a way to get two elements on top of each other, in a place of one element? My goal is make a picture of a waterdrop and then writing the humidity-% in the middle of it.
Thank you
Display your picture, then use nana::paint::graphics::string to write your text on top of the picture.
This
is produced by
#include <iostream>
#include <nana/gui.hpp>
#include <nana/gui/widgets/picture.hpp>
#include <nana/gui/widgets/label.hpp>
int main()
{
using namespace nana;
paint::image I("xxx.bmp");
if (I.empty())
{
msgbox err("Error");
err << "Cannot read image";
err.show();
exit(1);
}
form fm({50,50,400,700});
drawing dw(fm);
dw.draw([&I](paint::graphics& g)
{
I.paste(g, {0,0} );
g.string({200,400}, "THIS IS A TEST", colors::black);
});
fm.show();
exec();
}

insert edges in faces- CGAL error: assertion violation

I'm creating an application using qt creator which read .off files as CGAL::Linear_cell_complex_for_combinatorial_map and preview it I want to make operations on the read mesh such as removing edge and restore it .
it shows the following error :
terminate called after throwing an instance of 'CGAL::Assertion_exception'
what(): CGAL ERROR: assertion violation!
Expr: is_insertable_cell_1_in_cell_2(adart1, adart2)
I searched about it but I cannot get an useful solution
QWidget* viewer ;
std::string filename;
Dart_handle dh3;
LCC_3 lcc;
QMainWindow qWin;
CGAL::DefaultColorFunctorLCC fcolor;
void MainWindow ::insertCell()
{
dh3=lcc.insert_cell_1_in_cell_2(lcc.darts().begin(), lcc.darts().end());
CGAL_assertion( lcc.is_valid() );
lcc.display_characteristics(std::cout) << ", valid=" <<
lcc.is_valid() << std::endl;
setCentralWidget( new CGAL::SimpleLCCViewerQt<LCC_3, CGAL::DefaultColorFunctorLCC>(&qWin ,
lcc,
"Basic LCC Viewer",
false,
fcolor ) );
show();
}
void MainWindow ::removeCell()
{
CGAL_assertion( (lcc.is_removable<2>(dh3)) );
lcc.remove_cell<1>(dh3);
CGAL_assertion( lcc.is_valid() );
lcc.display_characteristics(std::cout) << ", valid="
<< lcc.is_valid() << std::endl;
}
any help? or usefull links to simplify mesh or removing vertices , edges or faces ?
I appreciate any help
To insert an edge in a face, cf the doc here. For a face in a volume, the doc is there. You can also have a look at the example here.
The important thing to understand is when you add an edge into a face, you need to say where to insert the edge. This is the reason why you give two precise darts. This is similar when you want to insert a face into a volume: you need to give a sequence of darts which is a cycle of edges in the volume.