treelist.c


     1	#include "global.h"
     2	#include "trees.h"
     3	
     4	/********************************************************************
     5	 * newBrchSeg, newRootSeg, and newLeafStruct:
     6	 *    Allocates space for a new branch segment, root segment, or
     7	 *    leaf structure and points the pointer given in the parameter 
     8	 *    at it.
     9	 ********************************************************************/
    10	
    11	void newBrchSeg (brchSegPtr *B)
    12	{
    13	   brchSegPtr temp;
    14	   temp = (brchSegPtr) malloc (sizeof (branchSeg));
    15	   *B = temp;
    16	}
    17	
    18	void newRootSeg (rtSegPtr *R)
    19	{
    20	   rtSegPtr temp;
    21	   temp = (rtSegPtr) malloc (sizeof (rootSeg));
    22	   *R = temp;
    23	}
    24	
    25	void newLeafStruct (lfStructPtr *L)
    26	{
    27	   lfStructPtr temp;
    28	   temp = (lfStructPtr) malloc (sizeof (leafStruct));
    29	   *L = temp;
    30	}
    31	
    32	/********************************************************************
    33	 * addBranchSeg, addRootSeg, and addLeafStruct:
    34	 *    Calls the function to allocate space for a new branch segment, 
    35	 *    root segment, or leaf structure and initializes it with the
    36	 *    data given in the parameters.
    37	 ********************************************************************/
    38	
    39	void addBranchSeg (B,x1,y1,z1,x2,y2,z2,radius)
    40	   brchSegPtr *B;
    41	   float x1,y1,z1,x2,y2,z2,radius;
    42	{
    43	   newBrchSeg (B);
    44	   (*B)->x1 = x1;
    45	   (*B)->y1 = y1;  
    46	   (*B)->z1 = z1;   
    47	   (*B)->x2 = x2;
    48	   (*B)->y2 = y2;
    49	   (*B)->z2 = z2;
    50	   (*B)->radius = radius;
    51	   (*B)->next = NULL;
    52	}
    53	
    54	void addRootSeg (R,order,rho,x1,y1,z1,x2,y2,z2,radius)
    55	   rtSegPtr *R;
    56	   float order,rho,x1,y1,z1,x2,y2,z2,radius;
    57	{
    58	   newRootSeg (R);
    59	   (*R)->order = order;
    60	   (*R)->rho = rho;
    61	   (*R)->x1 = x1;
    62	   (*R)->y1 = y1;
    63	   (*R)->z1 = z1;
    64	   (*R)->x2 = x2;
    65	   (*R)->y2 = y2;
    66	   (*R)->z2 = z2;
    67	   (*R)->radius = radius;
    68	   (*R)->diameter = 2 * radius;
    69	   (*R)->next = NULL;
    70	}
    71	
    72	void addLeafStruct (L,psyn,shade,lpi,ppfdSUN,ppfdSHADE,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4)
    73	   lfStructPtr *L;
    74	   float psyn,shade,lpi,ppfdSUN,ppfdSHADE;
    75	   float x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4;
    76	{
    77	   newLeafStruct (L);
    78	   (*L)->psyn = psyn;
    79	   (*L)->shade = shade;
    80	   (*L)->lpi = lpi;
    81	   (*L)->ppfdSUN = ppfdSUN;
    82	   (*L)->ppfdSHADE = ppfdSHADE;
    83	   (*L)->x1 = x1;
    84	   (*L)->y1 = y1;
    85	   (*L)->z1 = z1;
    86	   (*L)->x2 = x2;
    87	   (*L)->y2 = y2;
    88	   (*L)->z2 = z2;
    89	   (*L)->x3 = x3;
    90	   (*L)->y3 = y3;
    91	   (*L)->z3 = z3;
    92	   (*L)->x4 = x4;
    93	   (*L)->y4 = y4;
    94	   (*L)->z4 = z4;
    95	   (*L)->next = NULL;
    96	}
    97	
    98	/********************************************************************
    99	 * clearBrchList, clearRootList, clearLeafList:
   100	 *    Recursively frees the memory allocated for all of the 
   101	 *    branch segments, root segments, or leaf structures in the 
   102	 *    list given in the parameter.
   103	 ********************************************************************/
   104	
   105	void clearBrchList (brchSegPtr *B)
   106	{
   107	   if (*B != NULL) {
   108	      clearBrchList (&((*B)->next));
   109	      free (*B);
   110	      (*B) = NULL;
   111	    }
   112	}
   113	
   114	void clearRootList (rtSegPtr *R)
   115	{
   116	   if (*R != NULL) {
   117	      clearRootList (&((*R)->next));
   118	      free (*R);
   119	      (*R) = NULL;
   120	   }
   121	}
   122	
   123	void clearLeafList (lfStructPtr *L)
   124	{
   125	   if (*L != NULL) {
   126	      clearLeafList (&((*L)->next));
   127	      free (*L);
   128	      (*L) = NULL; 
   129	   }
   130	}
   131	
   132	/********************************************************************
   133	 * readBrchList, readRootList, readLeafList:
   134	 *    Reads in the data from the file pointed to by fp and calls 
   135	 *    the proper "add" function to add the data to the linked list
   136	 *    given in the parameter.
   137	 ********************************************************************/
   138	
   139	void readBrchList (brchSegPtr *B, FILE *fp)
   140	{
   141	   float x1,y1,z1,x2,y2,z2,radius;
   142	   while (fscanf (fp, "%f %f %f %f %f %f %f\n",
   143			&x1,&z1,&y1,&x2,&z2,&y2,&radius) != EOF) {
   144	      addBranchSeg (B,x1,y1,z1,x2,y2,z2,radius);
   145	      B = &((*B)->next);
   146	   }
   147	   fclose (fp);
   148	}
   149	
   150	void readRootList (rtSegPtr *R, FILE *fp)
   151	{
   152	   float order,rho,x1,y1,z1,x2,y2,z2,radius;
   153	   while (fscanf (fp, "%f %f %f %f %f %f %f %f %f\n", &rho,&order,
   154	                  &x1,&z1,&y1,&x2,&z2,&y2,&radius) != EOF) {
   155	      addRootSeg (R,order,rho,x1,y1,z1,x2,y2,z2,radius);
   156	      R = &((*R)->next);
   157	   }
   158	   fclose (fp);
   159	}
   160	
   161	void readLeafList (lfStructPtr *L, FILE *fp)
   162	{
   163	   float psyn,shade,lpi,ppfdSUN,ppfdSHADE;
   164	   float x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4;
   165	   while (fscanf (fp, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
   166			&psyn,&shade,&lpi,&ppfdSUN,&ppfdSHADE,&x1,&z1,&y1,&x2,
   167			&z2,&y2,&x3,&z3,&y3,&x4,&z4,&y4) != EOF) {
   168	      addLeafStruct (L,psyn,shade,lpi,ppfdSUN,ppfdSHADE,x1,y1,
   169			z1,x2,y2,z2,x3,y3,z3,x4,y4,z4);
   170	      L = &((*L)->next);
   171	   }
   172	   fclose (fp);
   173	}
   174	
   175	/********************************************************************
   176	 * printBrch, printRoot, printLeaf:
   177	 *    Prints the data of the given branch segment, root segment, 
   178	 *    or leaf structure to standard output.
   179	 ********************************************************************/
   180	
   181	void printBrch (brchSegPtr B) 
   182	{
   183	   printf ("%f %f %f %f %f %f %f ",B->x1,B->y1,B->z1,B->x2,B->y2,B->z2,B->radius);
   184	}
   185	
   186	void printRoot (rtSegPtr R) 
   187	{
   188	   printf ("%f %f %f %f %f %f %f %f %f ",R->rho,R->order,R->x1,R->y1,R->z1,R->x2,R->y2,R->z2,R->radius);
   189	}
   190	
   191	void printLeaf (lfStructPtr L, eco_color c) 
   192	{
   193	   printf ("%f %f %f %1.2f %1.2f %1.2f 1.0 ",L->x1,L->y1,L->z1,c.red,c.green,c.blue);
   194	   printf ("%f %f %f %1.2f %1.2f %1.2f 1.0 ",L->x2,L->y2,L->z2,c.red,c.green,c.blue);
   195	   printf ("%f %f %f %1.2f %1.2f %1.2f 1.0 ",L->x3,L->y3,L->z3,c.red,c.green,c.blue);
   196	   printf ("%f %f %f %1.2f %1.2f %1.2f 1.0 ",L->x4,L->y4,L->z4,c.red,c.green,c.blue);
   197	}
   198	      
   199	/********************  end of treelist.c **************************/
   200