LocalExpansionStereo Plane.h

#pragma once
#include <math.h>

struct Plane  {
	float a;
	float b;
	float c;
	float v;

	Plane(){}
	Plane(float a, float b, float c) : a(a), b(b), c(c), v(0){}
	Plane(float a, float b, float c, float v) : a(a), b(b), c(c), v(v){}

	static Plane CreatePlane(float nx, float ny, float nz, float z, float x, float y)
	{
		Plane p;
		p.a = -nx / nz;
		p.b = -ny / nz;
		p.c = z - p.a*x - p.b*y;
		p.v = 0;
		return p;
	}
	static Plane CreatePlane(float nx, float ny, float nz, float z, float x, float y, float v)
	{
		Plane p;
		p.a = -nx / nz;
		p.b = -ny / nz;
		p.c = z - p.a*x - p.b*y;
		p.v = v;
		return p;
	}
	static Plane CreatePlane(cv::Vec<float, 3> n, float z, float x, float y)
	{
		return CreatePlane(n[0], n[1], n[2], z, x, y);
	}

	static Plane CreatePlane(cv::Vec<float, 3> n, float z, float x, float y, float v)
	{
		return CreatePlane(n[0], n[1], n[2], z, x, y, v);
	}

	cv::Vec<float, 3> GetNormal() const
	{
		// Calc sqrt in double then cast to float.
		// Doing sqrt in float changes the results.
		float nz = float(1.0 / sqrt(1.0 + a*a + b*b));
		float nx = -a*nz;
		float ny = -b*nz;
		return cv::Vec<float, 3>(nx, ny, nz);
	}
	const float GetZ(float x, float y) const
	{
		return a*x + b*y + c;
	}
	const float GetZ(cv::Point p) const
	{
		return a*p.x + b*p.y + c;
	}

	bool operator ==(const Plane& in) const
	{
		return a == in.a && b == in.b && c == in.c && v == in.v;
	}

	bool operator !=(const Plane& in) const
	{
		return a != in.a || b != in.b || c != in.c || v != in.v;
	}

	cv::Mat toDispMap(cv::Rect rect) const
	{
		int w = rect.width;
		int h = rect.height;

		cv::Mat_<float> u(1, rect.width);
		cv::Mat_<float> v(1, rect.height);
		auto pu = u.ptr<float>(0);
		auto pv = v.ptr<float>(0);
		for (int x = 0; x < w; x++)
			pu[x] = a * (rect.x + x);

		for (int y = 0; y < h; y++)
			pv[y] = b * (rect.y + y);
		
		cv::Mat_<float> disp(rect.size());
		for (int y = 0; y < h; y++)
		{
			auto d = disp.ptr<float>(y);
			auto dv = pv[y];
			for (int x = 0; x < w; x++)
			{
				d[x] = (pu[x] + dv) + c;
			}
		}
		return disp;
	}

	cv::Vec<float, 4> toVec4() const
	{
		return cv::Vec<float, 4>(a, b, c, v);
	}
	cv::Scalar toScalar() const
	{
		return cv::Scalar(a, b, c, v);
	}
};

namespace cv
{
	template<> class DataType<Plane>
	{
	public:
		typedef float       value_type;
		typedef value_type  work_type;
		typedef value_type  channel_type;
		typedef value_type  vec_type;
		enum {
			generic_type = 0,
			depth = CV_32F,
			channels = sizeof(Plane) / sizeof(float),
			fmt = (int)'f',
			type = CV_MAKETYPE(depth, channels)
		};
	};
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值