//------------------------------------------------------------
// $Id: mpi2_funcs.h,v 1.2 2000/05/19 20:35:51 yamauchi Exp $
//------------------------------------------------------------
//	mpi2 funcs
//	1996/7/5
//	Copyright (C) 1996--1998 YAMAUCHI Hitoshi : 山内 斉
//------------------------------------------------------------
#ifndef _mpi2_funcs_h
#define _mpi2_funcs_h 1

#include <stdio.h>
#include "ray.h"
//------------------------------------------------------------
// get option
//------------------------------------------------------------
class FileInfo;
extern void getOption(int argc, char *argv[], FileInfo *pCommandLineFinfo);
extern void setFileInfo(const FileInfo &commandLineFinfo);

//------------------------------------------------------------
// read data
//------------------------------------------------------------
class SysInfoClass;
class ViewDataClass;
class SpaceParam;

extern YorN readSysinfoFile(const char *sysinfoFile, SysInfoClass *pSysinfo);
extern YorN readViewdataFile(const char *viewdtFile, ViewDataClass *pView);
extern YorN readSpcparamFile(const char *spcpmFile,  SpaceParam *pSpcPrm);

//------------------------------------------------------------
// fileUtil.cc
//------------------------------------------------------------
extern FILE *getOpenedFp(const char *pFileName);
extern FILE *getUniqueWriteFP(const char *pCandidateWriteFname, char *resBuf,
			      const int resbufsize);

//------------------------------------------------------------
// position = minpos + unitlen * index;
//------------------------------------------------------------
inline Vector3 getPosition(const Vector3& minpos, double unitlen, 
			   const Vector3& index)
{
    return(minpos + (unitlen * index));
}

//------------------------------------------------------------
// radiosity ray-tracing common
//------------------------------------------------------------
// define at host.cc 
class StopWatchClass;
class ElapsedTimerClass;
extern void pollingSleep(void);
extern void retrySleep(void);	
extern void outPEwatch(const char *pMes, const StopWatchClass &watch);
extern void outPEEtime(const char *pMes, const ElapsedTimerClass &etimer);
extern void outPERawTime(const char *pMes);
extern double getNowTime(void);

// define at PE.cc
extern void PEoutVerboseMes(int rank, const char *pMes);
extern void PEoutVerboseMes(const IntVec3& PEid, const char *pMes);
extern void PEoutDebugMes(int rank, const char *pMes);

// define at boundtest.cc
extern DIR_3D isHitBoundingBox(const Vector3& minBox, const Vector3& maxBox, 
			       const Vector3& rayOrigin, const Vector3& rayDir,
			       Vector3 *pHitPoint); 
extern YorN isPosInBox(const Vector3& pos, 
		       const Vector3& minpos, const Vector3& maxpos);
YorN isPosInBoxWithRes(const Vector3& pos, 
		       const Vector3& minpos, const Vector3& maxpos,
		       double err);

//------------------------------------------------------------
// radiosity
//------------------------------------------------------------



//------------------------------------------------------------
// ray tracing
//------------------------------------------------------------
// calcRayTrFRG.cc
//------------------------------
extern void generateInitialRays(const ViewDataClass& viewData, 
				const SpaceParam&    spcDat, 
				const IntVec3& PEid);


//------------------------------
// calcRayTrIC.cc 輝度を Frame Buffer へ送信する
//------------------------------
extern void sendRayIntensityToFB(const Ray& aRay);

//------------------------------------------------------------
// host
//------------------------------------------------------------
extern const char* genHd(const char *pHead, const IntVec3& PEid);
extern const char* genHd(const char *pHead, const int      PErank);
extern void  outNowTime(void);

// 部分空間内部の物体情報を出力する (PE, host 共通)
template < class T >
class array3dClass;

template < class T >
void printObjInSpcMapCommon(const char *pHead, const array3dClass < T > & map)
{
    int idx[3];
    const int xmaxIdx = map.getMaxIdx(0);
    const int ymaxIdx = map.getMaxIdx(1);
    const int zmaxIdx = map.getMaxIdx(2); 
    idx[0] = idx[1] = idx[2] = 0;
    cout << pHead << map.get(idx).getVisualOutDescription() << endl;
    for(idx[0] = 0; idx[0] <= xmaxIdx; idx[0]++)
    {
	for(idx[1] = 0; idx[1] <= ymaxIdx; idx[1]++)
	{
	    cout << pHead << " obj in subspace [" << idx[0] << "][" 
		<< idx[1] << "]: ";
	    for(idx[2] = 0; idx[2] <= zmaxIdx; idx[2]++)
	    {
		cout << map.get(idx).getVisualOut();
	    }
	    cout << endl;
	}
    }
}


//------------------------------------------------------------
// spc2pe
//------------------------------------------------------------
extern int getSystemDimension(const IntVec3& PEnum);

//------------------------------
// virtual 関数はどうも break point の設定ができないらしいので
// デバック用に
//------------------------------
#ifndef NDEBUG
extern void forBreakPoint(void);
#endif


//------------------------------
// for Proximitry Clouds 
//------------------------------
YorN readDistanceDataInfo(const char *pDistFname, IntVec3 *pDefSpcVolNum,
			  char	*pDistanceMap, int size);

//------------------------------
// main
//------------------------------
extern void usage(const char * const com_name);

//------------------------------
// output object sizes
//------------------------------
extern void outputObjectSize(const char *pHead);

#endif				// #ifndef _mpi2_funcs_h
