home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / cutNpaste / Motif+Xt / data.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.1 KB  |  139 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include "data.h"
  22.  
  23. #define HUGE 999999999.0
  24.  
  25. struct atom *freeAtoms(struct atom *p)
  26. {
  27.     if (p) {
  28.         freeAtoms(p->next);
  29.         free(p);
  30.     }
  31.     return NULL;
  32. }
  33.  
  34. struct molecule *freeMolecules(struct molecule *p)
  35. {
  36.     if (p) {
  37.         p->atoms = freeAtoms(p->atoms);
  38.         freeMolecules(p->next);
  39.         free(p);
  40.     }
  41.     return NULL;
  42. }
  43.  
  44. struct element *freeElements(struct element *p)
  45. {
  46.     if (p) {
  47.         freeElements(p->next);
  48.         free(p);
  49.     }
  50.     return NULL;
  51. }
  52.  
  53. struct element *createElement(struct element *p, char *name, 
  54.         float radius, float red, float green, float blue) {
  55.  
  56.     if (!p) {
  57.         p = malloc(sizeof(struct element));
  58.         strncpy(p->name, name, ELEMNAMESIZE);
  59.         p->radius = radius;
  60.         p->color = (int)(blue*255);
  61.         p->color = p->color << 8;
  62.         p->color |= (int)(green*255);
  63.         p->color = p->color << 8;
  64.         p->color |= (int)(red*255);
  65.         p->next = NULL;
  66.     }
  67.     else 
  68.         if (strcmp(name, p->name))
  69.             p->next = createElement(p->next, name, radius, 
  70.                     red, green, blue);
  71.     return p;
  72. }
  73.         
  74. struct molecule *createMolecule(struct molecule *p, char *name) {
  75.  
  76.     if (!p) {
  77.         p = malloc(sizeof(struct molecule));
  78.         strncpy(p->name, name, MOLNAMESIZE);
  79.         p->min[0] = p->min[1] = p->min[2] = HUGE;
  80.         p->max[0] = p->max[1] = p->max[2] = 0;
  81.         p->atoms = NULL;
  82.         p->next = NULL;
  83.     }
  84.     else 
  85.         p->next = createMolecule(p->next, name);
  86.     return p;
  87. }
  88.  
  89. struct element *findElement(struct element *p, char *name)
  90. {
  91.     if (p) 
  92.         if (strcmp(name, p->name) == 0)
  93.             return p;
  94.         else
  95.              return findElement(p->next, name);
  96.     else
  97.         return NULL;
  98. }
  99.  
  100. struct atom *createAtom(struct atom *p, struct element *e, float x, float y, float z) {
  101.     if (!p) {
  102.         p = malloc(sizeof(struct atom));
  103.         p->e = e;
  104.         p->xyz[0] = x;
  105.         p->xyz[1] = y;
  106.         p->xyz[2] = z;
  107.         p->next = NULL;
  108.     }
  109.     else 
  110.         p->next = createAtom(p->next, e, x, y, z);
  111.     return p;
  112. }
  113.  
  114. void addAtom(struct molecule *p, char *name, float x, float y, float z) {
  115.     float r;
  116.     struct element *e = findElement(elementRoot, name);
  117.     if (!e)
  118.         error("Unknown element");
  119.     if (!p)
  120.         error("Data structure error--Can't add to NULL molecule");
  121.     p->atoms = createAtom(p->atoms, e, x, y, z);
  122.  
  123.     r = e->radius;
  124.     p->min[0] = MIN(p->min[0], x-r);
  125.     p->min[1] = MIN(p->min[1], y-r);
  126.     p->min[2] = MIN(p->min[2], z-r);
  127.  
  128.     p->max[0] = MAX(p->max[0], x+r);
  129.     p->max[1] = MAX(p->max[1], y+r);
  130.     p->max[2] = MAX(p->max[2], z+r);
  131. }
  132.     
  133. error (char *s)
  134. {
  135.     fprintf(stderr, "%s\n", s);
  136.     exit(1);
  137. }
  138.  
  139.