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

Revision 39b13612ebd645a65eda854771b517371f2f858a authored by ennetws on 13 March 2015, 18:17:18 UTC, committed by ennetws on 13 March 2015, 18:17:18 UTC
Create README.md
1 parent c702819
  • Files
  • Changes
  • adfc2e5
  • /
  • DynamicVoxel
  • /
  • PolygonArea.h
Raw File Download

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.

  • revision
  • directory
  • content
revision badge
swh:1:rev:39b13612ebd645a65eda854771b517371f2f858a
directory badge
swh:1:dir:edd3ff344c75f506a69780cf3c38ee37c8e7341f
content badge
swh:1:cnt:b4fc42873ee9ee128aeeacc2a20c8dc527517d9a

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.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
PolygonArea.h
#pragma once

// Functions from paper entitled "Fast Polygon Area and Newell Normal Computation",
// Daniel Sunday, journal of graphics tools, 7(2):9-13, 2002.
// http://www.acm.org/jgt/papers/Sunday02/FastArea.html

#define PREV(i, N) ((i + N-1) % N)
#define NEXT(i, N) ((i + 1) % N)

// Input: 2D polygon
static inline double findArea(int n, double *x, double *y)        
{
	// Assume that the 2D polygon's vertex coordinates are
	// stored in (x[0], y[0]), ..., (x[n-1], y[n-1]),
	// with no assumed vertex replication at the end.

	// Initialize sum with the boundary vertex terms
	double sum = x[0] * (y[1] - y[n-1]) + x[n-1] * (y[0] - y[n-2]);

	for (int i=1; i < n-1; i++) {
		sum += x[i] * ( y[i+1] - y[i-1] );
	}

	return (sum / 2.0);
}

// return the signed area of a 3D planar polygon (given normal vector)
// 3D planar polygon, and plane normal
static inline double findArea3D(int n, double *x, double *y, double *z, double nx, double ny, double nz) 
{
	// select largest normal coordinate to ignore for projection
	double ax = (nx>0 ? nx : -nx);	// abs nx
	double ay = (ny>0 ? ny : -ny);	// abs ny
	double az = (nz>0 ? nz : -nz);	// abs nz
	double len = sqrt(nx*nx + ny*ny + nz*nz); // length of normal

	if (ax > ay) {
		if (ax > az)			       // ignore x-coord
			return findArea(n, y, z) * (len / nx);
	}
	else if (ay > az)			       // ignore y-coord
		return findArea(n, z, x) * (len / ny);
	return findArea(n, x, y) * (len / nz); // ignore z-coord
}

// output the approximate unit normal of a 3D nearly planar polygon
// return the area of the polygon
// Inputs: 3D polygon, output unit normal
static inline double findNormal3D(int n, double *x, double *y, double *z, double *nx, double *ny, double *nz) 
{
	// get the Newell normal
	double nwx = findArea(n, y, z);
	double nwy = findArea(n, z, x);
	double nwz = findArea(n, x, y);
	// get length of the Newell normal
	double nlen = sqrt( nwx*nwx + nwy*nwy + nwz*nwz );
	// compute the unit normal
	*nx = nwx / nlen;
	*ny = nwy / nlen;
	*nz = nwz / nlen;
	return nlen;    // area of polygon = length of Newell normal
}

static inline Vector3 centerOfPoints(const std::vector< Vector3 > & points)
{
	Vector3 center;

	for(std::vector<Vector3>::const_iterator it = points.begin(); it != points.end(); it++)
		center += *it;

	center /= points.size();

	return center;
}

static inline double findArea3D(const std::vector<Vector3>& points, const Vector3& n)
{
	int N = points.size();

	std::vector<double> x(N), y(N), z(N);

	for(int i = 0; i < N; i++)
	{
		x[i] = points[i][0];
		y[i] = points[i][1];
		z[i] = points[i][2];
	}

	double * X = &x.front();
	double * Y = &y.front();
	double * Z = &z.front();

	double area = findArea3D(N, X, Y, Z, n[0], n[1], n[2]);

	return area;
}

static inline double findNormal3D(const std::vector<Vector3>& points, Vector3& n)
{
	int N = points.size();

	std::vector<double> x(N), y(N), z(N);

	for(int i = 0; i < N; i++)
	{
		x[i] = points[i][0];
		y[i] = points[i][1];
		z[i] = points[i][2];
	}

	double * X = &x.front();
	double * Y = &y.front();
	double * Z = &z.front();

	return findNormal3D(N, X, Y, Z, &n[0], &n[1], &n[2]);
}

static inline double signedArea(const std::vector<Vector3>& points, const Vector3& n, const Vector3& center)
{
	int N = points.size();
	Vector3 sumVec;

	for(int i = 0; i < N; i++)
	{
		int j = NEXT(i, N);
		sumVec += cross(Vector3(points[i]-center),Vector3(points[j]-center));
	}

	return dot(sumVec, n);
}
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

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