//
// Asura_PatchSubdivision.h
//
// Created by Richard Rice - 09/09/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//


#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef __ASURA_PATCHSUBDIVISION_H__
#define __ASURA_PATCHSUBDIVISION_H__

class Asura_Vector_3;
class Asura_Mesh;


#define ASURA_PATCH_DEFAULT_SUBDIVIDE_SIZE 0.16f

//Doesn't so much do the subdivision , as control the settings for determing when subdivision should be done.
//Many of the settings are too slow to be used for anything other than a preprocessing stage.

class Asura_PatchSubdivision
{
public :
	
	static float GetDistanceSquared(const Asura_Vector_3& xPos,int iMaterialID);

	static void SetSubdivideFlat(bool bSubdivide){s_bSubdivideFlat = bSubdivide;};
	static bool GetSubdivideFlat(){return s_bSubdivideFlat;}

	static void SetFlatSubdivideSize(float fSize){s_fFlatSubdivideSize = fSize*fSize;}
	static void SetCurveSubdivideSize(float fSize){s_fCurveSubdivideSize = fSize*fSize;}

	static void SetPointSearchInitialDistance(float fDistance) { s_fPointSearchInitialDistance = fDistance; }
	static void SetPointSearchMinimumDistance(float fDistance) { s_fPointSearchMinimumDistance = fDistance; }

	static void SetDistancePower(float fPower) { s_fDistancePower = fPower; }
	static float GetDistancePower() { return s_fDistancePower; }

	static void SubdivideEvenly();
	static void SubdivideNearCamera();
	static void SubdivideNearWaypoints();
	static void SubdivideNearTracks();

	static bool ShouldSubdivideEvenly(){return s_eSubdivideMode == SUBDIVIDE_EVENLY;}
	
	static bool ShouldAvoidTriangleSlivers() {return s_bAvoidTriangleSlivers;}
	static void SetAvoidTriangleSlivers(bool bAvoid) {s_bAvoidTriangleSlivers = bAvoid;}

	static void FindPointsNearModule(Asura_Mesh* pxMesh,const Asura_Vector_3& xPos);

	enum SubdivideMode{SUBDIVIDE_EVENLY,SUBDIVIDE_CAMERA,SUBDIVIDE_WAYPOINT,SUBDIVIDE_TRACK};

private :
	
	static void InitialiseArrays();
	static void DeallocateArrays();
	
	static void FindTrackPointsNearModule(Asura_Mesh* pxMesh,const Asura_Vector_3& xPos);
	static void FindWaypointsNearModule(Asura_Mesh* pxMesh,const Asura_Vector_3& xPos);


	static SubdivideMode s_eSubdivideMode;

	static bool s_bSubdivideFlat;

	static bool s_bAvoidTriangleSlivers;
	
	static int s_iNumberOfPoints;
	static Asura_Vector_3* s_pxPoints; //list of all positions in the environment that we need to check for proximity to
	static int s_iNumberOfPointsForModule;
	static void** s_ppPointsForModule;// subset of points to check for the current mesh

	static float s_fFlatSubdivideSize;
	static float s_fCurveSubdivideSize;
	static float s_fPointSearchInitialDistance;
	static float s_fPointSearchMinimumDistance;
	static float s_fDistancePower;

	friend class Asura_Bicubic_Patch;
};


#endif //__ASURA_PATCHSUBDIVISION_H__
