I'm using C# for grphics. And now I want to return a null for PointF
/// <summary>
/// Project the point according to camera position.
/// </summary>
/// <param name="p">Point position, in WC</param>
/// <returns>pixel position</returns>
public PointF Project(MCvPoint3D32f p)
{
//return new PointF(p.x, p.y);
// Extend p
Matrix<double> p_ex = new Matrix<double>(3, 1);
p_ex[0, 0] = p.x;
p_ex[1, 0] = p.y;
p_ex[2, 0] = p.z;
var rst = HomographMatrix * p_ex;
rst[0, 0] = rst[0,0] / rst[2, 0] * focalLength * scale;
rst[1, 0] = rst[1, 0] / rst[2, 0] * focalLength * scale;
return new PointF((float)rst[0, 0], (float)rst[1, 0]);
}
Here I want to return a 'null' for the point if the point is out of FoV of my camera. But actually PointF is not 'nullable', so is there a straight forward way to return a 'null' for the point?
I don't want to inherit the native PointF.
In some ways this depends on your requirements. For example, if you need to store lots of points (so space is a factor), you could use some kind of sentinel value - perhaps:
static readonly PointF NilPoint = new PointF(float.NaN, float.NaN);
then check for that runtime, perhaps with a helper method:
public static bool IsNil(this PointF value)
{
return float.IsNaN(value.X) || float.IsNaN(value.Y);
}
so you can just check:
if(!point.IsNil()) {
// etc
}
However, an easier option (with a slight space overhead) is just to use Nullable<T>
, i.e.
PointF? pt1 = null; // fine
PointF? pt2 = new PointF(123,456); // also fine