Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/NSchertler/GeneralizedMotorcycleGraph
23 June 2024, 01:59:38 UTC
  • Code
  • Branches (4)
  • Releases (0)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/deploy-linux
    • refs/heads/deploy-osx
    • refs/heads/deploy-windows
    • refs/heads/master
    No releases to show
  • 505fc29
  • /
  • src
  • /
  • TexturePatch.cpp
Raw File Download Save again
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge
swh:1:cnt:812fed02d8241ccdbcc95f5f7f0811166861dcfd
origin badgedirectory badge
swh:1:dir:6b077e51a4e0137699fb0df51458390d621719a6
origin badgerevision badge
swh:1:rev:a34738fe34a051760b4042dc9d740231e511fec1
origin badgesnapshot badge
swh:1:snp:a981ea1718c19c4d9cde9d807965fd6d38bebcd2

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: a34738fe34a051760b4042dc9d740231e511fec1 authored by Nico Schertler on 31 October 2020, 07:04:57 UTC
Updated access token
Tip revision: a34738f
TexturePatch.cpp
#include "TexturePatch.h"
#include "MotorcycleGraph.h"

#include <nsessentials/math/LeastSquaresSystem.h>
#include <fstream>

#include <nsessentials/util/MathematicaFormatter.h>

#include "PolygonTriangulation.h"

bool IsGapBetween(const MotorcycleGraph::HalfArc& arc1, const MotorcycleGraph::HalfArc& arc2, const MotorcycleGraph& graph, const HEMesh& mesh)
{
	auto lastOf1 = graph.MotorcycleHalfedge(arc1.LastPathSegment());
	auto firstOf2 = graph.MotorcycleHalfedge(*arc2.begin());
	return mesh.to_vertex_handle(lastOf1) != mesh.from_vertex_handle(firstOf2);
}

TexturePatch::TexturePatch(MotorcycleGraph& graph)
	: graph(&graph)
{ }

void TexturePatch::PrepareBuild(std::vector<std::vector<size_t>>&& patchSides, std::set<HEMesh::FaceHandle>&& faces, std::vector<HEMesh::HalfedgeHandle>&& openBoundaryEdges)
{
	this->patchSides = patchSides;
	this->openBoundaryEdges = openBoundaryEdges;
	this->faces = faces;

	contextToIndex.clear();
	vertices.clear();
	filledHoles.clear();
}

HEMesh::HalfedgeHandle TexturePatch::GetContextEdgeOfToVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	auto v = mesh.to_vertex_handle(h);
	if (verticesOnArcs.find(v) == verticesOnArcs.end())
		return mesh.halfedge_handle(v);
	else
	{
		CirculateBackwardUntil<true>(h, mesh, [&](HEMesh::HalfedgeHandle checkH)
		{
			return halfedgesOnArcs.find(mesh.opposite_halfedge_handle(checkH)) != halfedgesOnArcs.end();
		});
		return h;
	}
}

HEMesh::HalfedgeHandle TexturePatch::GetContextEdgeOfFromVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	return GetContextEdgeOfToVertex(mesh.prev_halfedge_handle(h), mesh);	
}

void TexturePatch::Build(const HEMesh& mesh)
{	
	//Record the vertices and halfedges that lie on the comprising halfarcs.
	for (auto& side : patchSides)
	{
		for (auto arcIdx : side)
		{
			auto& arc = graph->Halfarcs()[arcIdx];
			for (auto p : arc)
			{
				auto h = graph->MotorcycleHalfedge(p);
				verticesOnArcs.insert(mesh.to_vertex_handle(h));
				halfedgesOnArcs.insert(h);
			}
		}
	}

	//Assign contiguous vertex indices
	int nextVertexIndex = 0;
	for (auto f : faces)
	{
		for (auto h : mesh.fh_range(f))
		{
			auto context = GetContextEdgeOfToVertex(h, mesh);
			auto inserted = contextToIndex.emplace(context, nextVertexIndex);
			if (inserted.second)
			{
				vertices.push_back(mesh.to_vertex_handle(h));
				++nextVertexIndex;
			}
		}
	}

	//find which of the sides can grow (i.e. if they have open boundaries or the incident sides do not constrain them

	if (patchSides.size() == 4)
	{
		for (int k = 0; k < 4; ++k)
		{
			sidesCanGrow[k] = false;
			bool hasNonBoundaryArc = false;
			for (int i = 0; i < patchSides[k].size(); ++i)
			{
				auto arcIdx = patchSides[k][i];
				auto& arc = graph->Halfarcs()[arcIdx];
				auto arcEdge = graph->MotorcycleHalfedge(*arc.begin());

				bool isBoundary = mesh.is_boundary(arcEdge) || mesh.is_boundary(mesh.opposite_halfedge_handle(arcEdge));

				if (isBoundary)
				{
					sidesCanGrow[k] = true;
					break;
				}
				else
					hasNonBoundaryArc = true;

				//check if there is an open boundary between this arc and the previous one
				if (i != 0 && IsGapBetween(graph->Halfarcs()[patchSides[k][i - 1]], arc, *graph, mesh))
					sidesCanGrow[k] = true;
			}

			if (!hasNonBoundaryArc)
			{
				sidesCanGrow[k] = true;
				continue;
			}

			//check if the previous and next sides are fixed
			auto nextSide = (k + 1) % 4;
			auto prevSide = (k + 3) % 4;

			if (patchSides[nextSide].empty())
				sidesCanGrow[k] = true;
			else
			{
				auto& myLastArc = graph->Halfarcs()[patchSides[k].back()];
				auto& firstArcOfNext = graph->Halfarcs()[patchSides[nextSide].front()];
				if (IsGapBetween(myLastArc, firstArcOfNext, *graph, mesh))
					sidesCanGrow[k] = true;
			}

			if (patchSides[prevSide].empty())
				sidesCanGrow[k] = true;
			else
			{
				auto& myFirstArc = graph->Halfarcs()[patchSides[k].front()];
				auto& lastArcOfPrev = graph->Halfarcs()[patchSides[prevSide].back()];
				if (IsGapBetween(lastArcOfPrev, myFirstArc, *graph, mesh))
					sidesCanGrow[k] = true;
			}
		}
	}

	FillHoles(mesh);
}

void TexturePatch::FillHoles(const HEMesh& mesh)
{
	//try to fill holes	
	std::set<HEMesh::HalfedgeHandle> handledBoundary;
	std::set<HEMesh::HalfedgeHandle> removeFromBoundary;
	for (auto h : openBoundaryEdges)
	{
		if (handledBoundary.find(h) != handledBoundary.end())
			continue;

		h = mesh.opposite_halfedge_handle(h);
		std::vector<HEMesh::VertexHandle> holeVertices;
		std::vector<Eigen::Vector3f> holeCoordinates;
		std::vector<HEMesh::HalfedgeHandle> holeHalfedges;
		auto startVertex = mesh.from_vertex_handle(h);
		auto p = mesh.point(startVertex);
		holeCoordinates.emplace_back(p[0], p[1], p[2]);
		holeVertices.push_back(startVertex);
		auto v = mesh.to_vertex_handle(h);
		bool isInsidePatch;
		do
		{
			auto opp = mesh.opposite_halfedge_handle(h);
			handledBoundary.insert(opp);
			holeHalfedges.push_back(opp);

			holeVertices.push_back(v);
			p = mesh.point(v);
			holeCoordinates.emplace_back(p[0], p[1], p[2]);

			CirculateBackwardUntil<true>(h, mesh, [](HEMesh::HalfedgeHandle) { return false; });
			//h = mesh.next_halfedge_handle(h);
			v = mesh.to_vertex_handle(h);
			auto context = GetContextEdgeOfToVertex(h, mesh);
			isInsidePatch = contextToIndex.find(context) != contextToIndex.end();
		} while (v != startVertex && isInsidePatch);

		if (v == startVertex)
		{
			//we have found a hole
			for (auto h : holeHalfedges)
				removeFromBoundary.insert(h);

			auto triangulation = TriangulatePolygon(holeCoordinates);
			for (auto& tri : triangulation)
				filledHoles.push_back({ holeVertices[tri[0]], holeVertices[tri[1]], holeVertices[tri[2]] });
		}
	}

	openBoundaryEdges.erase(std::remove_if(openBoundaryEdges.begin(), openBoundaryEdges.end(),
		[&](HEMesh::HalfedgeHandle h) { return removeFromBoundary.find(h) != removeFromBoundary.end(); }), openBoundaryEdges.end());	
}

size_t TexturePatch::IdOfToVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	auto context = GetContextEdgeOfToVertex(h, mesh);
	auto id = contextToIndex.find(context);
	if (id == contextToIndex.end())
		throw std::runtime_error("The specified vertex does not belong to this patch");
	return id->second;
}

size_t TexturePatch::IdOfFromVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	auto context = GetContextEdgeOfFromVertex(h, mesh);
	auto id = contextToIndex.find(context);
	if (id == contextToIndex.end())
		throw std::runtime_error("The specified vertex does not belong to this patch");
	return id->second;
}

size_t TexturePatch::IdOfInnerVertex(HEMesh::VertexHandle v, const HEMesh& mesh) const
{
	auto context = mesh.halfedge_handle(v);
	auto id = contextToIndex.find(context);
	if (id == contextToIndex.end())
		throw std::runtime_error("The specified vertex does not belong to this patch");

	return id->second;
}

HEMesh::VertexHandle TexturePatch::Vertex(size_t id) const
{
	return vertices[id];
}

void TexturePatch::InsertArcAfter(size_t arcToInsert, size_t afterArc)
{
	//Find afterArc
	bool found = false;
	for (auto& side : patchSides)
	{
		for (auto it = side.begin(); it != side.end(); ++it)
		{
			if (*it == afterArc)
			{
				//We found afterArc; insert arcToInsert
				++it;
				side.insert(it, arcToInsert);
				found = true;
				break;
			}
		}
		if (found)
			break;
	}
}

void TexturePatch::InsertArcBefore(size_t arcToInsert, size_t beforeArc)
{
	//Find beforeArc
	bool found = false;
	for (auto& side : patchSides)
	{
		for (auto it = side.begin(); it != side.end(); ++it)
		{
			if (*it == beforeArc)
			{
				//We found beforeArc; insert arcToInsert
				side.insert(it, arcToInsert);
				found = true;
				break;
			}
		}
		if (found)
			break;
	}
}

void TexturePatch::AddInteriorPatchLength(int dimension, float length)
{
	assert(dimension == 0 || dimension == 1);
	sumInteriorPatchLengths[dimension] += length;
	++countInteriorEdgeLengths[dimension];
}

float TexturePatch::GetAverageInteriorPatchLength(int dimension, int & outWeight) const
{
	assert(dimension == 0 || dimension == 1);
	outWeight = countInteriorEdgeLengths[dimension];
	return sumInteriorPatchLengths[dimension] / outWeight;
}

size_t TexturePatch::NumberOfVertices() const
{
	return contextToIndex.size();
}

// ----------  TextureCoordinatesStorage  ----------

TextureCoordinatesStorage::TextureCoordinatesStorage(const TexturePatch& patch)
	: patch(&patch)
{
	ResetTextureCoordinates();
}

const TexturePatch& TextureCoordinatesStorage::Patch() const
{
	return *patch;
}

const Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtToVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	return textureCoordinates.at(patch->IdOfToVertex(h, mesh));
}

const Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtFromVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) const
{
	return textureCoordinates.at(patch->IdOfFromVertex(h, mesh));
}

const Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtInnerVertex(HEMesh::VertexHandle v, const HEMesh& mesh) const
{
	return textureCoordinates.at(patch->IdOfInnerVertex(v, mesh));
}

const Eigen::Vector2f& TextureCoordinatesStorage::TexCoord(size_t i) const
{
	return textureCoordinates[i];
}

Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtToVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) { return const_cast<Eigen::Vector2f&>(static_cast<const TextureCoordinatesStorage*>(this)->TexCoordAtToVertex(h, mesh)); }
Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtFromVertex(HEMesh::HalfedgeHandle h, const HEMesh& mesh) { return const_cast<Eigen::Vector2f&>(static_cast<const TextureCoordinatesStorage*>(this)->TexCoordAtFromVertex(h, mesh)); }
Eigen::Vector2f& TextureCoordinatesStorage::TexCoordAtInnerVertex(HEMesh::VertexHandle v, const HEMesh& mesh) { return const_cast<Eigen::Vector2f&>(static_cast<const TextureCoordinatesStorage*>(this)->TexCoordAtInnerVertex(v, mesh)); }
Eigen::Vector2f& TextureCoordinatesStorage::TexCoord(size_t i) { return const_cast<Eigen::Vector2f&>(static_cast<const TextureCoordinatesStorage*>(this)->TexCoord(i)); }

size_t TextureCoordinatesStorage::TexCoordCount() const
{
	return textureCoordinates.size();
}

void TextureCoordinatesStorage::ResetTextureCoordinates()
{
	textureCoordinates.clear();
	textureCoordinates.resize(patch->NumberOfVertices(), Eigen::Vector2f::Constant(std::numeric_limits<float>::quiet_NaN()));
}

void TextureCoordinatesStorage::GetMinMaxTextureCoordinates(Eigen::Vector2f& min, Eigen::Vector2f& max) const
{
	min.setConstant(std::numeric_limits<float>::infinity());
	max.setConstant(-std::numeric_limits<float>::infinity());
	for (auto& entry : textureCoordinates)
	{
		for (int i = 0; i < 2; ++i)
		{
			if (entry[i] < min[i])
				min[i] = entry[i];
			if (entry[i] > max[i])
				max[i] = entry[i];
		}
	}

	if (std::isinf(min.x()))
	{
		min.setZero();
		max.setZero();
	}
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API