File: vector.c

package info (click to toggle)
plotmtv 1.4.1-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (349 lines) | stat: -rw-r--r-- 8,077 bytes parent folder | download | duplicates (6)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * vector.c - linked list procedures building and maintaining lists
 *           of vectors 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "CNdata.h"
#include "CNround.h"
#include "CNvector.h"

static void print_vector();

/*
 * VECTOR MANIPULATION UTILITITES
 */

/*
 * Calculate the default scaling factor for vectors
 */
double CNdefault_vector_scale(vectorhead, vectortail,
                              xmin, xmax, ymin, ymax, zmin, zmax, vlen_max)
CNvecptr vectorhead, vectortail;               /* Vector list         */
double   xmin, xmax, ymin, ymax, zmin, zmax;   /* Vector bounding box */
double   vlen_max;                             /* Max vector length   */
{
   double vscale=1.0;
   double max_len;
   double delx, dely, delz, delt;
   int    smallx, smally, smallz;
   int    nvectors;

   /* Find the number of vectors */
   nvectors = CNcount_vectors(vectorhead, vectortail);
   if (nvectors == 0) return(1.0);

   /* Get the maximum magnitude */
   max_len = vlen_max;

   /* Figure out the best scale factor */
   delx = fabs(xmax-xmin);
   dely = fabs(ymax-ymin);
   delz = fabs(zmax-zmin);
   smallx = delx < CN_SMALLER;
   smally = dely < CN_SMALLER;
   smallz = delz < CN_SMALLER;
   if (smallx && smally && smallz)
      delt  = max_len;
   else if (smallx && smally)
      delt  = 0.2*delz/(double)nvectors;
   else if (smally && smallz)
      delt  = 0.2*delx/(double)nvectors;
   else if (smallx && smallz)
      delt  = 0.2*dely/(double)nvectors;
   else if (smallx)
      delt  = 0.3*SMALLER_OF(dely,delz)/sqrt((double)nvectors);
   else if (smally)
      delt  = 0.3*SMALLER_OF(delx,delz)/sqrt((double)nvectors);
   else if (smallz)
      delt  = 0.3*SMALLER_OF(delx,dely)/sqrt((double)nvectors);
   else
      delt  = 0.3*MINOF3(delx,dely,delz)/exp(0.33*log((double)nvectors));

   vscale    = (max_len <= 0.0) ? 0.0 : delt/max_len;

   /* Return */
   return(vscale);
}

/*
 * Calculate the maximum magnitude of a collection of vectors
 */
/*ARGSUSED*/
double CNmax_vector(vectorhead, vectortail)
CNvecptr vectorhead, vectortail;
{
   CNvecptr V;
   double   lsqmax = 0.0;
   double   lsq, max_len;

   /* Go thru the vector list */
   for (V=vectorhead; V!=NULL; V=V->next)
      if ((lsq = V->vx*V->vx + V->vy*V->vy + V->vz*V->vz) > lsqmax) 
         lsqmax = lsq;   

   /* Take the square-root */
   max_len = sqrt(lsqmax);

   /* Return */
   return(max_len);
}

/*
 * Calculate the minimum magnitude of a collection of vectors
 * This actually finds the smallest non-zero-length vector
 */
/*ARGSUSED*/
double CNmin_vector(vectorhead, vectortail)
CNvecptr vectorhead, vectortail;
{
   CNvecptr V;
   double   lsqmin = CN_LARGE;
   double   lsq, min_len;
   int      MIN_TOUCHED = CN_FALSE;

   /* Go thru the vector list */
   for (V=vectorhead; V!=NULL; V=V->next) {
      lsq = V->vx*V->vx + V->vy*V->vy + V->vz*V->vz;
      if (lsq < lsqmin && lsq > CN_TINY) {
         lsqmin = lsq; 
         MIN_TOUCHED = CN_TRUE;
      }
   }

   /* Take the square-root */
   min_len = sqrt(lsqmin);

   /* If no non-zero min was found... */
   if (!MIN_TOUCHED) min_len = CN_SMALL;

   /* Return */
   return(min_len);
}

/*
 * Log Utility for vectors
 * If the value is less than 1, return 0.
 * Otherwise return log of the absolute value multiplied by the sign
 */
double CNveclog10(x)
double x;
{ 
   double val;
   int    sign;

   if (fabs(x) < 1.0)
      val  = 0.0;
   else {
      sign = (x > 0.0) ? 1 : -1;
      val  = sign * log10(fabs(x));
   }
   return(val);
}



/*
 * VECTOR BOX
 *    a vector-box is used to store the list of vectors
 */

/* 
 * Allocate room for a vector-box
 */
CNvecboxptr CNmake_vectorbox(vectorhead, vectortail, 
                             vlen_min, vlen_max, ID)
CNvecptr    vectorhead, vectortail;
double      vlen_min, vlen_max;
int         ID;
{
   CNvecboxptr newptr;
   unsigned int size = sizeof(CNvecbox);

   if ((newptr = (CNvecboxptr)malloc(size))!=NULL) {
      newptr->ID         = ID;
      newptr->linetype   = CN_LN_SOLID;
      newptr->linecolor  = 0;
      newptr->linewidth  = 1;
      newptr->marktype   = CN_MK_SQUARE1;
      newptr->markcolor  = 0;
      newptr->vlen_min   = vlen_min;
      newptr->vlen_max   = vlen_max;
      newptr->vectorhead = vectorhead;
      newptr->vectortail = vectortail;
   }
   return(newptr);
}


/*
 * Delete vector-box
 */
void CNdelete_vectorbox(Vptr)
CNvecboxptr Vptr;
{
   /* Free the vectors */
   if (Vptr->vectorhead != NULL)
   CNdelete_vector_list(&(Vptr->vectorhead),&(Vptr->vectortail));

   /* Now delete Vptr */
   free ((char*)Vptr);
}


/*
 * Print information on the vector box 
 */
void CNprint_vectorbox(Vptr, verbose)
CNvecboxptr Vptr;
int         verbose;
{
   int nvectors=CNcount_vectors(Vptr->vectorhead, Vptr->vectortail);
    
   (void) fprintf(stdout,"Vector-box ID = %s\n",Vptr->ID);
   (void) fprintf(stdout,"   Max Length = %g\n",Vptr->vlen_max);
   (void) fprintf(stdout,"   Min Length = %g\n",Vptr->vlen_min);
   (void) fprintf(stdout,"   No. vectors= %d\n",nvectors);
   if (verbose) CNprint_vector_list(Vptr->vectorhead, Vptr->vectortail);
}


/*
 * VECTOR DATA STRUCTURE
 *    vectors are used to store the physical coordinates (x,y,z)
 */

/*
 * Allocate room for a vector
 */
CNvecptr CNmake_vector(x,y,z,vx,vy,vz,ID)
double x,y,z,vx,vy,vz; 
int    ID;
{
   CNvecptr newptr;
   unsigned int size = sizeof(CNvector);

   if ((newptr = (CNvecptr)malloc(size))!=NULL) {
      newptr->ID     = ID;
      newptr->flag   = 0;
      newptr->noplot = CN_FALSE;
      newptr->x      = x;
      newptr->y      = y;
      newptr->z      = z;
      newptr->vx     = vx;
      newptr->vy     = vy;
      newptr->vz     = vz;
      newptr->nd     = NULL;
      newptr->next   = NULL;
      newptr->prev   = NULL;
   }
   return(newptr);
}

/*  
 * Insert a vector at the tail of the current vector list
 */
CNvecptr CNinsert_vector(vector_listhead, vector_listtail, 
                         x, y, z, vx, vy, vz, ID)
CNvecptr   *vector_listhead, *vector_listtail;
double     x, y, z, vx, vy, vz;
int        ID;
{
   CNvecptr CNmake_vector();
   CNvecptr next,A,B;

   A = *vector_listtail;
   if ((B=CNmake_vector(x,y,z,vx,vy,vz,ID))!=NULL) {
      if (A==NULL) {
         *vector_listhead = B;
         *vector_listtail = B;
      } else {
         next = A->next;
         B->next = next;
         B->prev = A;
         A->next = B;
         if (next != NULL) next->prev = B;
         if (B->next == NULL) *vector_listtail = B;
      }
   }
   return(B);
}


/*
 * Delete vector at address L 
 */
void CNdelete_vector(vector_listhead, vector_listtail, L)
CNvecptr *vector_listhead, *vector_listtail;
CNvecptr L;
{
   CNvecptr prev,next;

   prev = L->prev;
   next = L->next;
   if (prev!=NULL) prev->next = next;
   if (next!=NULL) next->prev = prev;
   if (L== *vector_listhead) *vector_listhead = next;
   if (L== *vector_listtail) *vector_listtail = prev;
   free ((char*)L);
   L = NULL;
}


/* 
 * Delete all the vectors in the list
 */
void CNdelete_vector_list(vector_listhead, vector_listtail)
CNvecptr *vector_listhead, *vector_listtail;
{
   CNvecptr P;

   while ((P = *vector_listhead) != NULL)
      CNdelete_vector(vector_listhead, vector_listtail, P);
}


/* 
 * Print out the list of vectors 
 */
/*ARGSUSED*/
void CNprint_vector_list(vector_listhead, vector_listtail)
CNvecptr vector_listhead, vector_listtail;
{
   CNvecptr P;

   for (P=vector_listhead; P!=NULL; P=P->next) 
      print_vector(P);
}


/* 
 * print the coordinates of a vector
 */
static void print_vector(pt)
CNvecptr pt;
{
   (void) fprintf(stdout,"x =%8.5g y =%8.5g z =%8.5g\n",pt->x, pt->y, pt->z);
   (void) fprintf(stdout,"vx=%8.5g vy=%8.5g vz=%8.5g\n",pt->vx,pt->vy,pt->vz);
}


/*
 * Count the number of vectors in the list 
 */
/*ARGSUSED*/
int CNcount_vectors(vector_listhead, vector_listtail)
CNvecptr vector_listhead, vector_listtail;
{
   CNvecptr P;
   int        count = 0;

   for (P=vector_listhead; P!=NULL; P=P->next) count++;

   return(count);
}