Пример #1
0
/*
 * getTreeFromFile
 * Function: Use a previously encoded file to create a EncodingTree
 * Parameters: The path to the encoded file
 * Return: The EncodingTree produced
 */
EncodingTree * getTreeFromFile(char * fileName)
{
    FILE * file;
    EncodingTree * eTree;
    EncodingTree * toAdd;
    char * charBinary;
    char * binaryString;
    char phrase[6] = {'\0'};
    char letter;
    int i;

    file = fopen(fileName, "r");
    if(file == NULL)
    {
        printf("Error: not enough memory\n");
        exit(0);
    }
    
    eTree = NULL;
    toAdd = NULL;
    i = 0;
    letter = '\0';
    charBinary = NULL;
    binaryString = NULL;

    /* Find the point in the file where ":ARC:" is found */
    while(strcmp(phrase, ":ARC:") != 0)
    {
        for(i = 0; i < 5; i ++)
        {
            phrase[i] = fgetc(file);
        }
        phrase[i] = '\0';
        for(i = 4; i > 0; i--)
        {
            ungetc(phrase[i], file);
        }
    }
    for(i = 0; i < 5; i++)
    {
       fgetc(file);
    }
    /***/
    letter = fgetc(file);
    while(letter != EOF)
    {
        /*printf("%c\n", letter);*/
        toAdd = createSubTree(letter);
        eTree = insertSubTree(eTree, toAdd);    
        letter = fgetc(file);
    }
    /*printTree(eTree);*/
    
    fclose(file);    
    return(eTree);
}
Пример #2
0
/*
* Create a Tree
* input: root Node of the tree
* create root node and then call creatSubtree of root
*/
void createTree(node **ptr){
  char ch;
  if(*ptr == NULL){
    *ptr = (node*)malloc(sizeof(node));
    (*ptr)->left = NULL;
    (*ptr)->right = NULL;
  }
  printf("Enter element: ");
  scanf("%d",&(*ptr)->data);
  createSubTree(*ptr);
}
Пример #3
0
TempOctree::TempOctree(char* fileNameTemplate,unsigned int sMaxNumPointsPerNode,LidarPoint* points,size_t numPoints)
	:tempFileName(new char[strlen(fileNameTemplate)+1]),
	 file(createTempFile(fileNameTemplate),File::ReadWrite),
	 maxNumPointsPerNode(sMaxNumPointsPerNode),
	 pointBbox(Box::empty),
	 writerThreadRun(true),writerThread(this,&TempOctree::writerThreadMethod)
	{
	/* Save the temporary file name: */
	strcpy(tempFileName,fileNameTemplate);
	
	/* Immediately unlink the temporary file; it will stay alive until the file handle is closed: */
	unlink(tempFileName);
	
	/* Calculate the point set's bounding box: */
	for(unsigned int i=0;i<numPoints;++i)
		pointBbox.addPoint(points[i]);
	
	/* Extend the bounding box by a small delta to include all points in a half-open box: */
	Point newMax=pointBbox.max;
	for(int i=0;i<3;++i)
		{
		Scalar delta=Scalar(1);
		if(newMax[i]+delta!=newMax[i])
			{
			while(newMax[i]+(delta*Scalar(0.5))!=newMax[i])
				delta*=Scalar(0.5);
			}
		else
			{
			while(newMax[i]+delta==newMax[i])
				delta*=Scalar(2);
			}
		newMax[i]+=delta;
		}
	pointBbox=Box(pointBbox.min,newMax);
	
	/* Set the root's domain to contain all points: */
	root.domain=Cube(pointBbox);
	
	/* Create the root node's subtree: */
	root.numPoints=numPoints;
	root.points=points;
	createSubTree(root);
	
	/* Wait until the octree file is finished: */
	writerThreadRun=false;
	writeQueueCond.signal();
	writerThread.join();
	file.flush();
	}
Пример #4
0
/*
*Create - sub tree of current node:
*Logic: if: there exist left child
*          insertLeft
*       else: 
*           make leftChild as NULL 
*       if : there exist right child
*           insertRight
*       else:
*           make rightChild as NULL
* call recursively for each node
*/
void createSubTree(node *ptr){
  char ch;
  printf("Sub tree of %d\n",ptr->data);  
  fflush(stdin);
  getchar();
  printf("Is left child 'y':");
  scanf("%c",&ch);
  if(ch == 'y' || ch == 'Y')
    insertLeft(ptr);
  printf("Is right child 'y':");
  fflush(stdin);
  getchar();
  scanf("%c",&ch);
  if(ch == 'y' || ch == 'Y')
    insertRight(ptr);
  if(ptr->left != NULL){
    printf("\nCreate sub tree of %d:\n",ptr->left->data);
    createSubTree(ptr->left);
  }
  if(ptr->right != NULL){
    printf("\nCreate sub tree of %d:\n",ptr->right->data);
    createSubTree(ptr->right);
  }
}
void TempOctree::createSubTree(TempOctree::Node& node,PointePoint* points,size_t numPoints)
	{
	/* Check if the number of points is smaller than the maximum: */
	if(numPoints<=size_t(maxNumPointsPerNode))
		{
		/* Make this node a leaf and write the points to the temporary file: */
		node.numPoints=numPoints;
		node.pointsOffset=file.tell();
		file.write(points,numPoints);
		}
	else
		{
		/* Make this node an interior node: */
		node.numPoints=numPoints;
		node.pointsOffset=0;
		
		/* Split the point array between the node's children: */
		PointePoint* childPoints[8];
		size_t childNumPoints[8];
		childPoints[0]=points;
		childNumPoints[0]=numPoints;
		
		/* Split the point set along the three dimensions, according to the node's center: */
		int numSplits=1;
		int splitSize=4;
		for(int i=2;i>=0;--i,numSplits<<=1,splitSize>>=1)
			{
			int leftIndex=0;
			for(int j=0;j<numSplits;++j,leftIndex+=splitSize*2)
				{
				size_t leftNumPoints=splitPoints(childPoints[leftIndex],childNumPoints[leftIndex],i,node.domain.getCenter(i));
				childPoints[leftIndex+splitSize]=childPoints[leftIndex]+leftNumPoints;
				childNumPoints[leftIndex+splitSize]=childNumPoints[leftIndex]-leftNumPoints;
				childNumPoints[leftIndex]=leftNumPoints;
				}
			}
		
		/* Initialize the child nodes and create their subtrees: */
		node.children=new Node[8];
		for(int childIndex=0;childIndex<8;++childIndex)
			{
			Node& child=node.children[childIndex];
			child.domain=Cube(node.domain,childIndex);
			createSubTree(node.children[childIndex],childPoints[childIndex],childNumPoints[childIndex]);
			}
		}
	}
Пример #6
0
void TempOctree::createSubTree(TempOctree::Node& node)
	{
	/* Check if the number of points is smaller than the maximum: */
	if(node.numPoints<=size_t(maxNumPointsPerNode))
		{
		/* Queue up this node to be written to the octree file: */
		Threads::MutexCond::Lock writeQueueLock(writeQueueCond);
		writeQueue.push_back(&node);
		writeQueueCond.signal();
		}
	else
		{
		/* Make this node an interior node: */
		node.children=new Node[8];
		
		/* Split the point array between the node's children: */
		node.children[0].numPoints=node.numPoints;
		node.children[0].points=node.points;
		
		/* Split the point set along the three dimensions, according to the node's center: */
		int numSplits=1;
		int splitSize=4;
		for(int i=2;i>=0;--i,numSplits<<=1,splitSize>>=1)
			{
			int leftIndex=0;
			for(int j=0;j<numSplits;++j,leftIndex+=splitSize*2)
				{
				size_t leftNumPoints=splitPoints(node.children[leftIndex].points,node.children[leftIndex].numPoints,i,node.domain.getCenter(i));
				node.children[leftIndex+splitSize].points=node.children[leftIndex].points+leftNumPoints;
				node.children[leftIndex+splitSize].numPoints=node.children[leftIndex].numPoints-leftNumPoints;
				node.children[leftIndex].numPoints=leftNumPoints;
				}
			}
		
		/* Create the node's children's subtrees: */
		for(int childIndex=0;childIndex<8;++childIndex)
			{
			Node& child=node.children[childIndex];
			child.domain=Cube(node.domain,childIndex);
			createSubTree(node.children[childIndex]);
			}
		
		node.points=0;
		}
	}
TempOctree::TempOctree(char* fileNameTemplate,unsigned int sMaxNumPointsPerNode,PointePoint* points,size_t numPoints)
	:tempFileName(new char[strlen(fileNameTemplate)+1]),
	 file(mkstemp(fileNameTemplate),"w+b",File::DontCare),
	 maxNumPointsPerNode(sMaxNumPointsPerNode),
	 pointBbox(Box::empty)
	{
	/* Save the temporary file name: */
	strcpy(tempFileName,fileNameTemplate);
	
	/* Calculate the point set's bounding box: */
	for(unsigned int i=0;i<numPoints;++i)
		pointBbox.addPoint(points[i]);
	
	/* Extend the bounding box by a small delta to include all points in a half-open box: */
	Point newMax=pointBbox.max;
	for(int i=0;i<3;++i)
		{
		Scalar delta=Scalar(1);
		if(newMax[i]+delta!=newMax[i])
			{
			while(newMax[i]+(delta*Scalar(0.5))!=newMax[i])
				delta*=Scalar(0.5);
			}
		else
			{
			while(newMax[i]+delta==newMax[i])
				delta*=Scalar(2);
			}
		newMax[i]+=delta;
		}
	pointBbox=Box(pointBbox.min,newMax);
	
	/* Set the root's domain to contain all points: */
	root.domain=Cube(pointBbox);
	
	/* Create the root node's subtree: */
	createSubTree(root,points,numPoints);
	}
Пример #8
0
struct Node* readFile(const char *filename, const char *target,	const char *fPath) {
	//Read in remodelfile and check for new target
	char lineBuf[2048];
	struct Node* root = NULL;
	FILE *file = fopen(filename, "r");
	if (!file) {
		printf("Could not open remodelfile\n");
		abort();
	} else {
		//Read first line
		if (readLine(file, lineBuf, sizeof(lineBuf)) != 0)
			return NULL;

		while (1) {
			struct Node* ptr = createSubTree(file);
			if (ptr == NULL)
				break;

			if (root == NULL)
				root = ptr;
			else {
				addSubTreeToParent(ptr, root);
			}
		}
		//Change target to one given in command argument
		if (target != NULL)
			resetTarget(target, root);

		char *dirName = ".remodel";
		char *fileName = ".remodel/md5.txt";
		struct stat st;


		if (stat(dirName, &st) == 0) {
			printf(".remodel is present\n");
		} else {
			system("mkdir .remodel");
		}

		if (stat(fileName, &st) == 0) {
			printf(".remodel/md5.txt is present\n");
			FILE *cmd = popen("md5sum -c .remodel/md5.txt", "r");
			char result[2048];
			memset(result, 0, sizeof(result));
			//Set StatusFlag for all to 1
			setStatus(root);
			int namel = 0;
			while (fgets(result, sizeof(result), cmd) != NULL) {
				printf("%s\n", result);

				if (result != NULL) {

					char *sName = strrchr(result, '/');
					sName++;
					char *eName = strrchr(result, ':');
					namel = eName - sName;
					char nameValue[256];
					memset(nameValue, 0, sizeof(nameValue));
					memcpy(nameValue, sName, namel);

					char *fStatus = strchr(result, "F");
					//char *nFile = strchr(result, "N");
					if (fStatus != NULL) {
						if (strcmp(fStatus, "F") == 0) {
							if (treeSearch(root, nameValue) != 0)
								printf("Failed hash check");
						}
					}

				}

			}

			pclose(cmd);
			runCmd(root, fPath);
			counter = 0;
			runMD5(root, fPath);

		} else {

			runCmd(root, fPath);
			runMD5(root, fPath);

		}

		//free(st);
	}
	fclose(file);
	return root;
}