rect.h 2.49 KB
#ifndef RTS_RECT_H
#define RTS_RECT_H

//enable CUDA_CALLABLE macro
#include "rts/cuda_callable.h"
#include "rts/vector.h"
#include "rts/point.h"
#include <iostream>

namespace rts{

//template for a rectangle class in ND space
template <class T, int N>
struct rect
{
	/*
		C------------------>O
		^                   ^
		|                   |
		Y                   |
		|                   |
		|                   |
		A---------X-------->B
	*/

	/*T A[N];
	T B[N];
	T C[N];*/

	rts::point<T, N> A;
	rts::vector<T, N> X;
	rts::vector<T, N> Y;


	CUDA_CALLABLE rect()
	{

	}

	CUDA_CALLABLE rect(point<T, N> a, point<T, N> b, point<T, N> c)
	{

		A = a;
		X = b - a;
		Y = c - a;

	}

	CUDA_CALLABLE rect(rts::point<T, N> pMin, rts::point<T, N> pMax, rts::vector<T, N> normal)
	{

        //assign the corner point
        A = pMin;

        //compute the vector from pMin to pMax
        rts::vector<T, 3> v0;
        v0 = pMax - pMin;

        //compute the cross product of A and the plane normal
        rts::vector<T, 3> v1;
        v1 = v0.cross(normal);


        //calculate point B
        rts::point<T, 3> B;
        B = A + v0 * 0.5 + v1 * 0.5;

        //calculate point C
        rts::point<T, 3> C;
        C = A  + v0 * 0.5 - v1 * 0.5;

        //calculate X and Y
        X = B - A;
        Y = C - A;




	}

	/*CUDA_CALLABLE rect(rts::point<T, N> p, rts::vector<T, N> x, rts::vector<T, N> y, T sx, T sy)
	{
		//This constructor creates a rect given a position, orientation, and size
		//	p	= center position of the rect
		//	x	= x-axis for the rectangle
		//	y	= y-axis for the rectangle
		//	sx	= size of the rect along the A-B axis
		//	sy	= size of the rect along the A-C axis

		//normalize x and y
		rts::vector<T, N> nx = x.norm();
		rts::vector<T, N> ny = y.norm();

		//compute X and Y
		X = sx * x;
		Y = sy * y;

		//compute A
		A = p - 0.5 * X - 0.5 * Y;

	}*/

	CUDA_CALLABLE rts::point<T, N> p(T a, T b)
	{
		rts::point<T, N> result;
		//given the two parameters a, b = [0 1], returns the position in world space
		result = A + X * a + Y * b;
		
		return result;
	}

	CUDA_CALLABLE rts::point<T, N> operator()(T a, T b)
	{
		return p(a, b);
	}

	std::string toStr()
	{
		std::stringstream ss;

		ss<<"A = "<<A<<std::endl;
		ss<<"B = "<<A + X<<std::endl;
		ss<<"C = "<<A + X + Y<<std::endl;
		ss<<"D = "<<A + Y<<std::endl;

        return ss.str();

	}
};

}	//end namespace rts

template <typename T, int N>
std::ostream& operator<<(std::ostream& os, rts::rect<T, N> R)
{
    os<<R.toStr();
    return os;
}


#endif