/*
# mrcImagesSinogramCorrelation : $Revision$  
# $Date$ 
# Created by $Author$
# Usage : mrcImagesSinogramCorrelation
# Attention
#   $Loccker$
#  	$State$ 
#
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>                  
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define GLOBAL_DECLARATION
#include "../inc/config.h"

#define DEBUG
#include "genUtil.h"
#include "mrcImage.h"
#include "Memory.h"
#include "Matrix3D.h"
#include "lmrcImageFileListAndEulerAngleDataRead.h"
#include "lmrcImageSinogram.h"
#include "lCommonLineCalculation.h"

#define WORDLEN 1024

typedef struct lmrcImagesSinogramCorrelationInfo {
	int    LengthThresholdMode;
	double LengthThresholdRatio;

	char** OutFileListName;
	int    pairNumber;
	int    imagenumber;
	char*  OutHeader;
	char*  directory;

	char*   extensionOfList;
	char*   extensionOfCor;
	char*   resultDirectory;
	char**  CorDirectory;

	int Nmode;
	int D1mode;
	int D2mode;
	int Lmode;

	float* CommonLineAngleIn;
	float* CommonLineAngleRef;
	char** NormalFilename;
	char** Derivation1DFilename;
	char** Derivation2DFilename;
	char** LengthFilename;
} lmrcImagesSinogramCorrelationInfo;

typedef struct lCommonLineCalculationInfo{
	char* I1RotationalMode;
	char* I2RotationalMode;
	float I1Angle1;
	float I1Angle2;
	float I1Angle3;
	float I2Angle1;
	float I2Angle2;
	float I2Angle3;
	float I1Angle;
	float I2Angle;
}lCommonLineCalculationInfo;

/*
typedef enum lmrcImagesSinogramCorrelationMode {
	a=0,
	b=1
} lmrcImagesSinogramCorrelationMode;
*/

extern void lmrcImagesSinogramCorrelation(mrcImage* in, char** filename, lmrcImagesSinogramCorrelationInfo* linfo, int mode);
extern void __CommonLineCalculation(lCommonLineCalculationInfo* linfo);
extern void lmrcImageFileListAndCommonLinePositionDataWrite(lmrcImagesSinogramCorrelationInfo* linfo, char** filename, int mode);

int
main(int argc, char* argv[]) 
{
	mrcImagesSinogramCorrelationInfo  info;
	lmrcImagesSinogramCorrelationInfo linfo;
	mrcImage In;

	init0(&info);
    argCheck(&info, argc, argv);
    init1(&info);

	linfo.LengthThresholdMode  = info.LengthThresholdMode;
	linfo.LengthThresholdRatio = info.LengthThresholdRatio;

	linfo.OutHeader = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
	strcpy(linfo.OutHeader, info.OutHeader);

	linfo.extensionOfCor  = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
	strcpy(linfo.extensionOfCor,  info.CorrelationExtension);
	
	linfo.extensionOfList = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
	strcpy(linfo.extensionOfList, info.ListExtension);
	
	linfo.resultDirectory = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
	strcpy(linfo.resultDirectory, info.Rd);

	linfo.Nmode  = info.Nmode;
	linfo.D1mode = info.D1mode;
	linfo.D2mode = info.D2mode;
	linfo.Lmode  = info.Lmode;

	linfo.CorDirectory = (char **)memoryAllocate(sizeof(char*)*4, "in main");
	if(linfo.Nmode == 0){
		linfo.CorDirectory[0] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
		strcpy(linfo.CorDirectory[0], info.Nd);
	}
	if(linfo.D1mode == 0){
		linfo.CorDirectory[1] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
		strcpy(linfo.CorDirectory[1], info.D1d);
	}
	if(linfo.D2mode == 0){
		linfo.CorDirectory[2] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
		strcpy(linfo.CorDirectory[2], info.D2d);
	}
	if(linfo.Lmode == 0){
		linfo.CorDirectory[3] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in main");
		strcpy(linfo.CorDirectory[3], info.Ld);
	}

	DEBUGPRINT("Program Start\n");

	if(mkdir(linfo.resultDirectory, 00750) != 0){
		DEBUGPRINT("Can't creat directory\n");
		exit(1);
	}

	lmrcImageFileListAndEulerAngleDataRead(&In, info.In, info.flagIn, info.fptInList, 0);
	lmrcImagesSinogramCorrelation(&In, info.In, &linfo, 0);
	lmrcImageFileListAndCommonLinePositionDataWrite(&linfo, info.In, 0);

	DEBUGPRINT("Program End\n");
	exit(EXIT_SUCCESS);
}

void
additionalUsage()
{
	fprintf(stderr, "\n");
	fprintf(stderr, "----- mode -----\n");
	fprintf(stderr, ">>Nmode  : 0 :     creat normal correlation       (-Nd)\n");
	fprintf(stderr, "           1 : not creat normal correlation\n");
	fprintf(stderr, ">>D1mode : 0 :     creat derivation1D correlation (-D1d)\n");
	fprintf(stderr, "           1 : not creat derivation1D correlation\n");
	fprintf(stderr, ">>D2mode : 0 :     creat derivation2D correlation (-D2d)\n");
	fprintf(stderr, "           1 : not creat derivation2D correlation\n");
	fprintf(stderr, ">>Lmode  : 0 :     creat length correlation       (-Ld, -LTM, -LTR)\n");
	fprintf(stderr, "           1 : not creat length correlation\n");
	fprintf(stderr, "\n----- directory and file -----\n");
	fprintf(stderr, ">>create directory : Rd\n");
	fprintf(stderr, "                   : Rd/Nd (D1d, D2d, Ld)\n");
	fprintf(stderr, "\n>>create list file        : Rd/Nd/oH-Nd.LE\n");
	fprintf(stderr, "                           (Result/Normal/test-Normal.list)\n");
	fprintf(stderr, "\n>>create correlation file : Rd/Nd/infile1_infile2.Nd.CE\n");
	fprintf(stderr, "                           (Result/Normal/test1_test2.Normal.cor)\n");
}

void
lmrcImagesSinogramCorrelation(mrcImage* in , char** filename, lmrcImagesSinogramCorrelationInfo* linfo, int mode)
{
	int   					   i, j; 
	int      				   ImageNumber;
	int                        pair = 0;
	lCommonLineCalculationInfo CommonLineInfo;
	lmrcImageSinogramInfo      SinogramInfo;
	mrcImage 				   sino1;
	mrcImage 				   sino2;
	mrcImage                   NormalCorrelationImage;
	mrcImage                   sino1Derivation1D;
	mrcImage                   sino1Derivation2D;
	mrcImage                   sino2Derivation1D;
	mrcImage                   sino2Derivation2D;
	mrcImage                   Derivation1DCorrelationImage;
	mrcImage                   Derivation2DCorrelationImage;
	mrcImage                   LengthCorrelationImage;
	char**                     directoryName;
	char*                      workNormal;
	char*                      workDerivation1D;
	char*                      workDerivation2D;
	char*                      workLength;
	char**                     filenameWithoutExtension;
	char*                      extensionPosition;

	ImageNumber = in->numTailer;
	linfo->imagenumber = ImageNumber;
	linfo->pairNumber  = ImageNumber*(ImageNumber-1)/2;

	CommonLineInfo.I1RotationalMode = (char *)memoryAllocate(sizeof(char)*5, "in lmrcImageSinogramCorrelation");
	CommonLineInfo.I2RotationalMode = (char *)memoryAllocate(sizeof(char)*5, "in lmrcImageSinogramCorrelation");

	SinogramInfo.LengthCorrelationMode = 0;
	SinogramInfo.LengthThresholdMode   = linfo->LengthThresholdMode;
	SinogramInfo.LengthThresholdRatio  = linfo->LengthThresholdRatio;
	SinogramInfo.dphi                  = in->HeaderLength.y*RADIAN;
	SinogramInfo.correlationMode       = 0;

	filenameWithoutExtension = (char **)memoryAllocate(sizeof(char*)*ImageNumber, "in lmrcImagesSinogramCorrelation");
	for(i=0; i<ImageNumber; i++){
		filenameWithoutExtension[i] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		strcpy(filenameWithoutExtension[i], filename[i]);
	
		extensionPosition = strrchr(filenameWithoutExtension[i], '.');
		strcpy(extensionPosition, "\0");
	}

	directoryName = (char **)memoryAllocate(sizeof(char*)*4, "in lmrcImagesSinogramCorrelation");
	if(linfo->Nmode == 0){
		workNormal = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		linfo->NormalFilename = (char **)memoryAllocate(sizeof(char*)*linfo->pairNumber, "in lmrcImagesSinogramCorrelation");
		directoryName[0] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		sprintf(directoryName[0], "%s/%s", linfo->resultDirectory, linfo->CorDirectory[0]);
	
		if(mkdir(directoryName[0],00750) != 0){
			DEBUGPRINT1("Can't creat %s\n", directoryName[0]);
			exit(1);
		}
	}
	if(linfo->D1mode == 0){
		workDerivation1D = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		linfo->Derivation1DFilename = (char **)memoryAllocate(sizeof(char*)*linfo->pairNumber, "in lmrcImagesSinogramCprrelation");
		directoryName[1] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		sprintf(directoryName[1], "%s/%s", linfo->resultDirectory, linfo->CorDirectory[1]);
	
		if(mkdir(directoryName[1],00750) != 0){
			DEBUGPRINT1("Can't creat %s\n", directoryName[1]);
			exit(1);
		}
	}
	if(linfo->D2mode == 0){ 
		workDerivation2D = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		linfo->Derivation2DFilename = (char **)memoryAllocate(sizeof(char*)*linfo->pairNumber, "in lmrcImagesSinogramCorrelation");
		directoryName[2] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		sprintf(directoryName[2], "%s/%s", linfo->resultDirectory, linfo->CorDirectory[2]);
	
		if(mkdir(directoryName[2],00750) != 0){
			DEBUGPRINT1("Can't creat %s\n", directoryName[2]);
			exit(1);
		}
	}
	if(linfo->Lmode == 0){
		workLength = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		linfo->LengthFilename = (char **)memoryAllocate(sizeof(char*)*linfo->pairNumber, "in lmrcImagesSinogramCorrelation");
		directoryName[3] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
		sprintf(directoryName[3], "%s/%s", linfo->resultDirectory, linfo->CorDirectory[3]);
	
		if(mkdir(directoryName[3],00750) != 0){
			DEBUGPRINT1("Can't creat %s\n", directoryName[3]);
			exit(1);
		}
	}
	
	linfo->CommonLineAngleIn    = (float *)memoryAllocate(sizeof(float)*linfo->pairNumber, "in lmrcImagesSinogramCorrelation");
	linfo->CommonLineAngleRef   = (float *)memoryAllocate(sizeof(float)*linfo->pairNumber, "in lmrcImagesSinogramCorrelation");

	for(i=0; i<ImageNumber-1; i++){
		mrcImageSectionGet(&sino1, in, i, 1);
	for(j=i+1; j<ImageNumber; j++){
		mrcImageSectionGet(&sino2, in, j, 1);

		CommonLineInfo.I1Angle1 = in->Tailer[i].Cont.Rot1;
		CommonLineInfo.I1Angle2 = in->Tailer[i].Cont.Rot2;
		CommonLineInfo.I1Angle3 = in->Tailer[i].Cont.Rot3;
		CommonLineInfo.I2Angle1 = in->Tailer[j].Cont.Rot1;
		CommonLineInfo.I2Angle2 = in->Tailer[j].Cont.Rot2;
		CommonLineInfo.I2Angle3 = in->Tailer[j].Cont.Rot3;
		strcpy(CommonLineInfo.I1RotationalMode, in->Tailer[i].Cont.EulerAngleMode);
		strcpy(CommonLineInfo.I2RotationalMode, in->Tailer[j].Cont.EulerAngleMode);

		__CommonLineCalculation(&CommonLineInfo);
		linfo->CommonLineAngleIn[pair]  = CommonLineInfo.I1Angle; 
		linfo->CommonLineAngleRef[pair] = CommonLineInfo.I2Angle;
		
		if(linfo->Nmode == 0){
			lmrcImageSinogramCorrelation0(&NormalCorrelationImage, &sino1, &sino2, &SinogramInfo, 0);
			
			linfo->NormalFilename[pair] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
			sprintf(linfo->NormalFilename[pair], "%s_%s.%s.%s", filenameWithoutExtension[i], filenameWithoutExtension[j], 
			                                                    linfo->CorDirectory[0], linfo->extensionOfCor);
			sprintf(workNormal, "%s/%s", directoryName[0], linfo->NormalFilename[pair]);
			mrcFileWrite(&NormalCorrelationImage, workNormal, "Write NormalCorrelation", 0);
			mrcImageFree(&NormalCorrelationImage, 0);
		}
		if(linfo->D1mode == 0){
			lmrcImageDerivationCalculation(&sino1Derivation1D, &sino1, 1);
			lmrcImageDerivationCalculation(&sino2Derivation1D, &sino2, 1);
			lmrcImageSinogramCorrelation0(&Derivation1DCorrelationImage, &sino1Derivation1D, &sino2Derivation1D, &SinogramInfo, 0);
			
			linfo->Derivation1DFilename[pair] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
			sprintf(linfo->Derivation1DFilename[pair], "%s_%s.%s.%s", filenameWithoutExtension[i], filenameWithoutExtension[j], 
			                                                          linfo->CorDirectory[1], linfo->extensionOfCor);
			sprintf(workDerivation1D, "%s/%s", directoryName[1], linfo->Derivation1DFilename[pair]);
			mrcFileWrite(&Derivation1DCorrelationImage, workDerivation1D, "Write Derivation1DCorrelation", 0);
			mrcImageFree(&sino1Derivation1D, 0);
			mrcImageFree(&sino2Derivation1D, 0);
			mrcImageFree(&Derivation1DCorrelationImage, 0);
		}
		if(linfo->D2mode == 0){
			lmrcImageDerivationCalculation(&sino1Derivation2D, &sino1, 2);
			lmrcImageDerivationCalculation(&sino2Derivation2D, &sino2, 2);
			lmrcImageSinogramCorrelation0(&Derivation2DCorrelationImage, &sino1Derivation2D, &sino2Derivation2D, &SinogramInfo, 0);
			
			linfo->Derivation2DFilename[pair] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
			sprintf(linfo->Derivation2DFilename[pair], "%s_%s.%s.%s", filenameWithoutExtension[i], filenameWithoutExtension[j], 
			                                                          linfo->CorDirectory[2], linfo->extensionOfCor);
			sprintf(workDerivation2D, "%s/%s", directoryName[2], linfo->Derivation2DFilename[pair]);
			mrcFileWrite(&Derivation2DCorrelationImage, workDerivation2D, "Write Derivation2DCorrelation", 0);
			mrcImageFree(&sino1Derivation2D, 0);
			mrcImageFree(&sino2Derivation2D, 0);
			mrcImageFree(&Derivation2DCorrelationImage, 0);
		}
		if(linfo->Lmode == 0){
			lmrcImageSinogramLengthCorrelation(&LengthCorrelationImage, &sino1, &sino2, &SinogramInfo, 0);
			
			linfo->LengthFilename[pair] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImagesSinogramCorrelation");
			sprintf(linfo->LengthFilename[pair], "%s_%s.%s.%s", filenameWithoutExtension[i], filenameWithoutExtension[j], 
			                                                    linfo->CorDirectory[3], linfo->extensionOfCor);
			sprintf(workLength, "%s/%s", directoryName[3], linfo->LengthFilename[pair]);
			mrcFileWrite(&LengthCorrelationImage, workLength, "Write LengthCorrelation", 0);
			mrcImageFree(&LengthCorrelationImage, 0);
		}

		pair++;
	}
	}

	if(linfo->Nmode == 0){
		free(workNormal);
		free(directoryName[0]);
	}
	if(linfo->D1mode == 0){
		free(workDerivation1D);
		free(directoryName[1]);
	}
	if(linfo->D2mode == 0){
		free(workDerivation2D);
		free(directoryName[2]);
	}
	if(linfo->Lmode == 0){
		free(workLength);
		free(directoryName[3]);
	}
	free(directoryName);

	for(i=0; i<ImageNumber; i++){
		free(filenameWithoutExtension[i]);
	}
	free(filenameWithoutExtension);

	mrcImageFree(in, 0);
}

void
__CommonLineCalculation(lCommonLineCalculationInfo* linfo)
{
	floatVector xv;
    floatVector yv;
    floatVector nv;
    floatVector m1v;
    floatVector m2v;
    Matrix3D    Matrix1;
    Matrix3D    Matrix2;

    floatVectorInit(&nv, 4);
    nv.data[0] = 0.0;
    nv.data[1] = 0.0;
    nv.data[2] = 1.0;
    nv.data[3] = 1.0;

    floatVectorInit(&xv, 4);
    xv.data[0] = 1.0;
    xv.data[1] = 0.0;
    xv.data[2] = 0.0;
    xv.data[3] = 1.0;

    floatVectorInit(&yv, 4);
    yv.data[0] = 0.0;
    yv.data[1] = 1.0;
    yv.data[2] = 0.0;
    yv.data[3] = 1.0;

    matrix3DRotationAntiSetFollowingEulerAngle(Matrix1,
                                               linfo->I1RotationalMode,
                                               linfo->I1Angle1*RADIAN,
                                               linfo->I1Angle2*RADIAN,
                                               linfo->I1Angle3*RADIAN,
                                               MATRIX_3D_MODE_INITIALIZE);

    matrix3DMultiplyVector(&nv, Matrix1);
    matrix3DMultiplyVector(&xv, Matrix1);
    matrix3DMultiplyVector(&yv, Matrix1);

    matrix3DRotationSetFollowingEulerAngle(Matrix2,
                                           linfo->I2RotationalMode,
                                           linfo->I2Angle1*RADIAN,
                                           linfo->I2Angle2*RADIAN,
                                           linfo->I2Angle3*RADIAN,
                                           MATRIX_3D_MODE_INITIALIZE);

    matrix3DMultiplyVector(&nv, Matrix2);
    matrix3DMultiplyVector(&xv, Matrix2);
    matrix3DMultiplyVector(&yv, Matrix2);

    floatVectorInit(&m1v, 4);
    floatVectorInit(&m2v, 4);

    m1v.data[0] = -nv.data[1];
	m1v.data[1] = nv.data[0];
    m1v.data[2] = 0.0;
    m1v.data[3] = 1.0;

    m2v.data[0] = m1v.data[0]*xv.data[0]+m1v.data[1]*xv.data[1]+m1v.data[2]*xv.data[2];
    m2v.data[1] = m1v.data[0]*yv.data[0]+m1v.data[1]*yv.data[1]+m1v.data[2]*yv.data[2];
    m2v.data[2] = m1v.data[0]*nv.data[0]+m1v.data[1]*nv.data[1]+m1v.data[2]*nv.data[2];
    m2v.data[3] = 1.0;

    matrix3DRotationSetFollowingEulerAngle(Matrix1,
                                           linfo->I1RotationalMode,
                                           linfo->I1Angle1*RADIAN,
                                           linfo->I1Angle2*RADIAN,
                                           linfo->I1Angle3*RADIAN,
                                           MATRIX_3D_MODE_INITIALIZE);

    matrix3DMultiplyVector(&m1v, Matrix1);
    matrix3DMultiplyVector(&m2v, Matrix1);

    linfo->I1Angle = atan2(m1v.data[1],m1v.data[0])*DEGREE;
    linfo->I2Angle = atan2(m2v.data[1],m2v.data[0])*DEGREE;

	floatVectorFree(&xv);
	floatVectorFree(&yv);
	floatVectorFree(&nv);
	floatVectorFree(&m1v);
	floatVectorFree(&m2v);
}

void
lmrcImageFileListAndCommonLinePositionDataWrite(lmrcImagesSinogramCorrelationInfo* linfo, char** filename, int mode)
{
	int i, j, pair;
	FILE* fptNormal;
	FILE* fptDerivation1D;
	FILE* fptDerivation2D;
	FILE* fptLength;

	linfo->OutFileListName = (char **)memoryAllocate(sizeof(char*)*4, "in lmrcImageFileListAndCommonLinePositionDataWrite");

	if(linfo->Nmode == 0){
		linfo->OutFileListName[0] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImageFileListAndCommonLinePositionDataWrite");
		sprintf(linfo->OutFileListName[0], "%s/%s/%s-%s.%s", linfo->resultDirectory, linfo->CorDirectory[0], 
		                                                     linfo->OutHeader, linfo->CorDirectory[0], linfo->extensionOfList);
		fptNormal       = fopen(linfo->OutFileListName[0],"w");
		pair = 0;
		for(i=0; i<linfo->imagenumber-1; i++){
		for(j=i+1; j<linfo->imagenumber; j++){
			fprintf(fptNormal, "%s %s %s %f %f\n", linfo->NormalFilename[pair], filename[i], filename[j], 
			                                       linfo->CommonLineAngleIn[pair], linfo->CommonLineAngleRef[pair]);
			pair++;
		}
		}
		fclose(fptNormal);
	}
	if(linfo->D1mode == 0){
		linfo->OutFileListName[1] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImageFileListAndCommonLinePositionDataWrite");
		sprintf(linfo->OutFileListName[1], "%s/%s/%s-%s.%s", linfo->resultDirectory, linfo->CorDirectory[1], 
		                                                     linfo->OutHeader, linfo->CorDirectory[1], linfo->extensionOfList);
		fptDerivation1D = fopen(linfo->OutFileListName[1],"w");
		pair = 0;
		for(i=0; i<linfo->imagenumber-1; i++){
		for(j=i+1; j<linfo->imagenumber; j++){
			fprintf(fptDerivation1D, "%s %s %s %f %f\n", linfo->Derivation1DFilename[pair], filename[i], filename[j], 
			                                             linfo->CommonLineAngleIn[pair], linfo->CommonLineAngleRef[pair]);
			pair++;
		}
		}
		fclose(fptDerivation1D);
	}
	if(linfo->D2mode == 0){
		linfo->OutFileListName[2] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImageFileListAndCommonLinePositionDataWrite");
		sprintf(linfo->OutFileListName[2], "%s/%s/%s-%s.%s", linfo->resultDirectory, linfo->CorDirectory[2], 
		                                                     linfo->OutHeader, linfo->CorDirectory[2], linfo->extensionOfList);
		fptDerivation2D = fopen(linfo->OutFileListName[2],"w");
		pair = 0;
		for(i=0; i<linfo->imagenumber-1; i++){
		for(j=i+1; j<linfo->imagenumber; j++){
			fprintf(fptDerivation2D, "%s %s %s %f %f\n", linfo->Derivation2DFilename[pair], filename[i], filename[j], 
			                                             linfo->CommonLineAngleIn[pair], linfo->CommonLineAngleRef[pair]);
			pair++;
		}
		}
		fclose(fptDerivation2D);
	}
	if(linfo->Lmode == 0){
		linfo->OutFileListName[3] = (char *)memoryAllocate(sizeof(char)*WORDLEN, "in lmrcImageFileListAndCommonLinePositionDataWrite");
		sprintf(linfo->OutFileListName[3], "%s/%s/%s-%s.%s", linfo->resultDirectory, linfo->CorDirectory[3], 
		                                                     linfo->OutHeader, linfo->CorDirectory[3], linfo->extensionOfList);
		fptLength       = fopen(linfo->OutFileListName[3],"w");
		pair = 0;
		for(i=0; i<linfo->imagenumber-1; i++){
		for(j=i+1; j<linfo->imagenumber; j++){
			fprintf(fptLength, "%s %s %s %f %f\n", linfo->LengthFilename[pair], filename[i], filename[j], 
			                                       linfo->CommonLineAngleIn[pair], linfo->CommonLineAngleRef[pair]);
			pair++;
		}
		}
		fclose(fptLength);
	}
}
