Piero V.

Heterogeneous comparisons in C++ containers

Occasionally, I still work with my Intel RealSense, on my RGBD toolbox, and affine topics.

Recently, I decided to allow multiple formats for the color images (initially, I had hardcoded them to JPEG only).

Therefore, I had to modify my data structures to work with pairs of paths instead of their common stems.

The UI to add new frames to the scene lists all the valid frames once and puts them into an ordered std::set, now keyed on the path pair.

With my previous assumption on fixed formats, I could do lookups on the set to quickly check if a provided stem was valid.

After the changes, this involved a heterogeneous comparison, i.e., a comparison of different types.

The trivial way to do this is a linear search, e.g., with std::find and a lambda or a range-based for.

However, this seemed a frequent case to me, and I was curious to see if there was a way to still take advantage of the optimized algorithms provided by the containers.

Indeed, there is! But it was added only since C++14.

After implementing bool operator<(const Other &, const Key &), you can pass std::less<> as a comparator to your container instead of the default std::less<Key>.

That is a particular template specialization that was developed for this purpose. It contains an is_transparent type that enables the templated version of some methods in STL containers.

This stack overflow answer contains many details. A TL; DR is that this implementation avoids unwanted conversions that could have undesired effects (e.g., the continuous creation of temporary objects from literals).

Customize NSIS MUI2's welcome page

Recently, I had to customize NSIS installers for work.

It was my first time, and I had to get to know this tool first. While this system already offers many functionalities, if you want to change anything in its built-in pages, you must rely on Win32 APIs.

The welcome and finish pages are an exception, up to a certain sense, because they are made with nsDialogs, NSIS’s plugin for creating completely custom pages.

As a result, it is possible to pass to add new widgets using the same API in a function passed MUI_PAGE_CUSTOMFUNCTION_SHOW. However, I encountered a major problem: the default widgets fill the available space. Therefore, any new widgets will be hidden, and it will not be possible to interact with them with the mouse. I looked for a solution online - NSIS is more than 20 years old, so you can find a lot of documentation - but I did not find an answer that worked for me.

Eventually, I found that you can resize widgets on Win32 with MoveWindow. … [Leggi il resto]

I have become a professional FOSS developer

I had the luck to get to know free and open source software when I was still a kid. In this way, the willingness to share my knowledge became a part of my culture and personality.

If you browse this site, you will see that I have shared a lot of small projects, like FlatPress plugins. However, I have never been a long-time contributor to a big project.

Moreover, at the end of my University course of study, I had to do an internship to graduate. I went to a software company that creates proprietary programs for the enterprise. I remained for six months and then was hired as an employee, and I stayed for another two years.

I was on a small team developing a CAD, and I enjoyed working with my coworkers a lot (even though I worked remotely for most of the two years because of COVID).

But I did not like using proprietary libraries.

One of them was Parasolid, a geometry kernel developed by Siemens. It is powerful, but some functions are overly complicated to use. It comes with very prolific documentation, and its subscription includes technical support. But it is the only way to troubleshoot your problems: I could never find any public information online, which is extremely surprising in the 2020s! … [Leggi il resto]

Back to Rust again

In 2010 I tried to program a video game with a friend for the first time. And it was our first time with C++, too. Of course, it was C++2003 since C++11 was the new-not-yet-stable thing.

I must say that our approach was quite naive. For example, we thought we would always play the game on a LAN. Thus, we never worried about lag compensation.

Now, a few years have passed, and I have learned a thing or two, even though I still have to finish a game project.

So, at the beginning of this year, this friend pushed me to learn Rust, and I decided to learn it through game development again.

At a certain point, I decided to use a physics engine, and I found Rapier. I was especially interested in its determinism to finally implement a lag compensation mechanism. But I did not understand how to use it correctly. Only recently, I understood it favors an approach similar to functional programming, whereas I was trying an OOP-like approach. This also led to fights with the borrow checker. So, because of these problems and my usual decrease in interest and focus, I left yet another project incomplete. … [Leggi il resto]

VBO multipli con OpenGL ES 2.0

Emscripten e WebAssembly

Di recente ho iniziato un nuovo progettino, per il quale sto usando SDL e OpenGL ES 2.0.

Durante il lockdown, avevo imparato a fare qualcosa di base con OpenGL 3.3, e avevo usato sempre SDL e un po’ Bullet, ma poi avevo lasciato stare il tutto. Adesso ho ricominciato, ma con OpenGL ES 2.0 perché offre una possibilità molto interessante: con Emscripten, si può compilare il progetto per WebAssembly, e GLES 2.0 viene trasformato direttamente in WebGL.

Pur non essendo più molto appassionato di sviluppo web, questa cosa mi attrae particolarmente. Innanzitutto, pubblicando sul web si possono raggiungere più persone, anche per il solo fatto che aspettare qualche secondo che si carichi una pagina è molto meno impegnativo che far scaricare uno zip, decompattarlo, e far avviare il programma. E non parliamo nemmeno degli installanti…

Il codice può essere sviluppato praticamente in qualsiasi linguaggio (C, C++, o qualcosa che abbia un interprete/una VM scritta in questi linguaggi), ma poi si può interfacciare al JavaScript che gira nel browser. Questo vuol dire che potrei fare le parti di rendering 3D in C++, ma le parti di UI in HTML, CSS e JavaScript. Le ultime volte che ho provato a fare qualcosa in JS mi sono stufato subito, però, per fare UI, lo stack web ha pochi eguali. … [Leggi il resto]