Skip to content

Instantly share code, notes, and snippets.

@Larry57
Created January 12, 2020 21:14
Show Gist options
  • Save Larry57/a8a66824b4b299a26fbe64ef5de6f53e to your computer and use it in GitHub Desktop.
Save Larry57/a8a66824b4b299a26fbe64ef5de6f53e to your computer and use it in GitHub Desktop.
Liang Barsky 3D clipping algorithm in homogeneous coordinates
using System;
using System.Numerics;
namespace Clipping {
// https://fr.mathworks.com/matlabcentral/fileexchange/51550-3d-and-2d-homogeneous-space-line-clipping-using-liang-barsky-algorithm
public class LiangBarskyClippingHomogeneous {
float _t0;
float _t1;
public bool Clip(ref Vector4 p0, ref Vector4 p1) {
if(p0.W < 0 && p1.W < 0)
return false;
_t0 = 0;
_t1 = 1;
var delta = p1 - p0;
if(!clip(p0.W - p0.X, -delta.W + delta.X)) return false;
if(!clip(p0.W + p0.X, -delta.W - delta.X)) return false;
if(!clip(p0.W - p0.Y, -delta.W + delta.Y)) return false;
if(!clip(p0.W + p0.Y, -delta.W - delta.Y)) return false;
if(!clip(p0.W - p0.Z, -delta.W + delta.Z)) return false;
if(!clip(p0.W + p0.Z, -delta.W - delta.Z)) return false;
if(_t1 < 1)
p1 = p0 + _t1 * delta;
if (_t0 > 0)
p0 = p0 + _t0 * delta;
return true;
}
bool clip(float q, float p) {
if(Math.Abs(p) < float.Epsilon && q < 0)
return false;
var r = q / p;
if(p < 0) {
if(r > _t1) return false;
if(r > _t0) _t0 = r;
}
else {
if(r < _t0) return false;
if(r < _t1) _t1 = r;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment