What is fix geometry actually doing in QGIS?

I think @Vince's idea is what you can try.

From QGIS Documetation:

Attempts to create a valid representation of a given invalid geometry without losing any of the input vertices. Already valid geometries are returned without further intervention. Always outputs multi-geometry layer.

More details you can find in:

  • qgsalgorithmfixgeometries.h
  • qgsalgorithmfixgeometries.cpp

P.S. I hope that @Nyall Dawson will answer this question.


References:

  • Docs » QGIS User Guide » 23.1.15.32. Fix geometries
  • GitHub/QGIS/src/analysis/processing/

It is basically fixing your geometry using the makeValid method, that is, correcting your geometry without loosing nodes:

https://qgis.org/pyqgis/master/core/QgsGeometry.html?highlight=qgsgeometry#qgis.core.QgsGeometry.makeValid

similar of the St_MakeValid of PostGIS:

https://postgis.net/docs/ST_MakeValid.html


As per the OGC Simple Feature Access specification, geometries need to follow the OpenGIS compliance (see the PostGIS docs for quick reference), where (listing only the most prominent predicates)

  • Points are considered inherently valid
  • LineStrings are valid if they are simple, meaning that they don't pass an inner vertex twice
  • Polygons are valid if their linear components are simple and none of their rings cross
  • MultiPoints are valid if no coordinate pair is present twice
  • MultiLineStrings are simple if all their components are simple, and common vertices only touch
  • MultiPolygons are valid if all their components are valid and do not overlap

In order to do so, the internally called GEOS modules will (listing only the most prominent operations)

  • correctly node a LineString and remove consecutive duplicated vertices
  • correctly node the linear components of a Polygon and iteratively rebuild all possible valid areas from them
  • do the above for all parts of their Multi geometries

where noding means breaking apart a linear component at non-consecutive duplicated vertices.

In this context, 'fixing' geometries rather means creating the maximal set of their valid and/or simple parts, and may result in a collection of geometries of different dimensionality.