File: utils.c

package info (click to toggle)
savi 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,824 kB
  • sloc: ansic: 6,932; tcl: 5,011; makefile: 265; sh: 237
file content (234 lines) | stat: -rw-r--r-- 4,473 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 *****************************************************
 *
 *  SaVi by Lloyd Wood (lloydwood@users.sourceforge.net),
 *          Patrick Worfolk (worfolk@alum.mit.edu) and
 *          Robert Thurman.
 *
 *  Copyright (c) 1997 by The Geometry Center.
 *  Also Copyright (c) 2017 by Lloyd Wood.
 *
 *  This file is part of SaVi.  SaVi is free software;
 *  you can redistribute it and/or modify it only under
 *  the terms given in the file COPYRIGHT which you should
 *  have received along with this file.  SaVi may be
 *  obtained from:
 *  http://savi.sourceforge.net/
 *  http://www.geom.uiuc.edu/locate/SaVi
 *
 *****************************************************
 *
 * utils.c
 *
 * $Id: utils.c,v 1.21 2017/01/02 06:11:32 lloydwood Exp $
 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* for select */
#include <unistd.h>
#include <sys/time.h>

#include "constants.h"
#include "globals.h"
#include "utils.h"

/*
 * copyfile(stdout, "data.txt")
 *
 * Will copy the file data.txt to stdout.
 * Returns TRUE if successful, else FALSE.
 */
unsigned int
copyfile(FILE * out_fp, const char filename[])
{
  FILE *in_fp;
  char buf[LENGTH_STRING_BUFFER];

  if (!out_fp)
    return FALSE;

  if (NULL == (in_fp = fopen(filename, "r")))
    return FALSE;
  while (fgets(buf, LENGTH_STRING_BUFFER, in_fp)) {
    fprintf(out_fp, "%s", buf);
  }
  fflush(out_fp);
  fclose(in_fp);


  return TRUE;
}

/*
 * forward over comments and any newlines and white space
 *
 */
void
forward_over_comments(FILE * fp)
{
  int c = getc(fp);

  while ((c == '\n') || (c == ' ') || (c == '\t') || (c == '#')) {
    if (c == '#') {
      /* skip to end of line */
      do {
	c = getc(fp);
      } while ((c != '\n') && (c != EOF));
    }
    c = getc(fp);
  }
  ungetc(c, fp);
}

/*
 * identity
 *
 * Fills in a 4x4 matrix to be the identity matrix
 */
void
identity(double m[4][4])
{
  unsigned int i, j;

  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      m[i][j] = ((i == j) ? 1.0 : 0.0);
}


/*
 * Return the norm of a CartesianCoordinate vector
 */
double
norm(const CartesianCoordinates * px)
{
  return (sqrt(px->x * px->x + px->y * px->y + px->z * px->z));
}

/*
 * Normalizes a CartesianCoordinate vector to unit length
 */
void
normalize(CartesianCoordinates * px)
{
  double l = norm(px);

  if (l == 0.0)
    return;
  px->x /= l;
  px->y /= l;
  px->z /= l;
}

/*
 * Return the dot-product of two CartesianCoordinate vectors.
 */
double
dot(const CartesianCoordinates * px, const CartesianCoordinates * py)
{
  return (px->x * py->x + px->y * py->y + px->z * py->z);
}


/*
 * The vector cross-product of two CartesianCoordinate vectors
 */
void
cross_product(CartesianCoordinates * pucv, const CartesianCoordinates * pu,
	      const CartesianCoordinates * pv)
{
  double pu_x = pu->x;
  double pu_y = pu->y;
  double pu_z = pu->z;

  double pv_x = pv->x;
  double pv_y = pv->y;
  double pv_z = pv->z;

  pucv->x = (pu_y * pv_z - pu_z * pv_y);
  pucv->y = (pu_z * pv_x - pu_x * pv_z);
  pucv->z = (pu_x * pv_y - pu_y * pv_x);
}

void
print_vec(const char msg[], const CartesianCoordinates * pv)
{
  fprintf(stderr, "%s = (%f,%f,%f)\n", msg, pv->x, pv->y, pv->z);
}

/*
 * Rotate a vector by angle theta about the z-axis.
 *
 * The rotation matrix is
 *
 *   cos(theta)    -sin(theta)    0
 *   sin(theta)     cos(theta)    0
 *       0              0         1
 *
 */
void
rotate_z(const CartesianCoordinates * v_old, const double theta,
	 CartesianCoordinates * v_new)
{
  double cos_theta = cos(theta);
  double sin_theta = sin(theta);
  double old_x = v_old->x;
  double old_y = v_old->y;

  v_new->x = cos_theta * old_x - sin_theta * old_y;
  v_new->y = sin_theta * old_x + cos_theta * old_y;
  v_new->z = v_old->z;
}



/*
 * error_format
 *
 * prints a string using formatting to stderr
 *   - a newline is automatically appended
 */
void
error_format(const char format[], const char msg[])
{
  fprintf(stderr, "\nSaVi: ");
  fprintf(stderr, format, msg);
}


/*
 * error_and_exit
 *
 * prints an error msg and exits program with value 1
 */
void
error_and_exit(const char msg[])
{
  error_format("%s", msg);
  error_format("%s", "Error! Exiting.");
  exit(1);
}

/*
 * error
 *
 * prints a string as an error message
 */
void
error(const char msg[])
{
  error_format("%s", msg);
}


void
millisleep(const unsigned int ms)
{

  struct timeval tv;

  tv.tv_usec = (ms % 1000) * 1000;
  tv.tv_sec = ms / 1000;
  select(0, NULL, NULL, NULL, &tv);
}