#ifndef VECTORMATH_H_
#define VECTORMATH_H_

#include "../config.h"
#include <math.h>
#include "mathex.h"
#include "vmath.h"
#include "../typedefs.h"

/** Gives random integer number in the range lowest-highest number */
int randomIntInRange(int lowest, int highest);

/** Gives the point in the middle of a line formed by the A and B points */
inline Vector3r midPoint(const Vector3r &a, const Vector3r &b) {
	return Vector3r((a.x+b.x)/2, (a.y+b.y)/2, (a.z+b.z)/2);
};

/** Gives the point in the middle of a line formed by the A and B points */
inline Vector3r midPoint(const dReal* a, const dReal* b) {
	return Vector3r((a[0]+b[0])/2, (a[1]+b[1])/2, (a[2]+b[2])/2);
};

/** Gives the point in the middle of a line */
inline Vector3r midPoint(const Line &l) {
	return Vector3r((l._a.x+l._b.x)/2, (l._a.y+l._b.y)/2, (l._a.z+l._b.z)/2);
};

/** Types of line-to-line relation */
enum IntersectResult {PARALLEL, COINCIDENT, NOT_INTERESECTING, INTERESECTING};

/** Finds intersection of a line with another line
 * @param l1 the first line
 * @param l2 the second line
 * @param intersection gets filled with the intersection coordinates, if any
 * @return the state of the two lines one to each other
 */
IntersectResult intersect(const Line &l1, const Line &l2, Vector3r &intersection);

/** Finds intersection of a line with another line
 * @param l1 the first line
 * @param l2 the second line
 * @param intersection gets filled with the intersection coordinates, if any
 * @return the state of the two lines one to each other
 */
IntersectResult intersect(const Vector3r &l1a, const Vector3r &l1b, const Vector3r &l2a, const Vector3r &l2b, Vector3r &intersection);

/** Finds all intersections of line with a polygon
 * @param p the polygon
 * @param l the line
 * @param intersections
 * @param indexes of the intersected polygon segments
 * @return number of the intersections found, at max 32
 */
unsigned IntersectPolygon(const Polygon &p, const Line &l, Vector3r* intersections, unsigned* intersected);

/** Finds all intersections of line with a polygon
 * @param p the polygon
 * @param l the line
 * @param intersections
 * @param indexes of the intersected polygon segments
 * @return number of the intersections found, at max 32
 */
unsigned IntersectPolygon(const Polygon* p, const Line &l, Vector3r* intersections, unsigned* intersected);

/** Finds all intersections of line with a polygon
 * @param p the polygon
 * @param l the line
 * @param intersections
 * @param indexes of the intersected polygon segments
 * @return number of the intersections found, at max 32
 */
unsigned IntersectPolygon(const Polygon &p, const Vector3r &la, const Vector3r &lb, Vector3r* intersections, unsigned* intersected);

/** Finds all intersections of line with a polygon
 * @param p the polygon
 * @param l the line
 * @param intersections
 * @param indexes of the intersected polygon segments
 * @return number of the intersections found, at max 32
 */
unsigned IntersectPolygon(const Polygon* p, const Vector3r &la, const Vector3r &lb, Vector3r* intersections, unsigned* intersected);

/** Returns oriented distance of a point from line.
 * Oriented means the result is:
 * > 0 if the point P lies on the right of the line, taken by the orientation of the line
 * < 0 if the point P lies on the left of the line, taken by the orientation of the line
 */
dReal orientedDistFromLine(const Line &l, const Vector3r p);

/** Finds if point lies within the polygon using the ray tracing method */
bool pointInPolygon(const Polygon &polygon, const Vector3r &point);

#endif /*VECTORMATH_H_*/
