BRL-CAD
vector.c
Go to the documentation of this file.
1 /* V E C T O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup plot */
21 /** @{ */
22 /** @file libbn/vector.c
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdio.h>
29 #include <math.h>
30 
31 #include "vmath.h"
32 #include "plot3.h"
33 
34 /**
35  *@brief
36  * Draw a vector between points "from" and "to", with the option of
37  * having an arrowhead on either or both ends.
38  *
39  * The fromheadfract and toheadfract values indicate the length of the
40  * arrowheads relative to the length of the vector to-from. A typical
41  * value is 0.1, making the head 10% of the size of the vector. The
42  * sign of the "fract" values indicates the pointing direction.
43  * Positive points towards the "to" point, negative points towards
44  * "from". Upon return, the virtual pen is left at the "to" position.
45  */
46 void
47 tp_3vector(FILE *plotfp, fastf_t *from, fastf_t *to, double fromheadfract, double toheadfract)
48 {
49  register fastf_t len;
50  register fastf_t hooklen;
51  vect_t diff;
52  vect_t c1, c2;
53  vect_t h1, h2;
54  vect_t backup;
55  point_t tip;
56 
57  pdv_3line(plotfp, from, to);
58  /* "pen" is left at "to" position */
59 
60  VSUB2(diff, to, from);
61  if ((len = MAGNITUDE(diff)) < SMALL) return;
62  VSCALE(diff, diff, 1/len);
63  bn_vec_ortho(c1, diff);
64  VCROSS(c2, c1, diff);
65 
66  if (!ZERO(fromheadfract)) {
67  hooklen = fromheadfract*len;
68  VSCALE(backup, diff, -hooklen);
69 
70  VSCALE(h1, c1, hooklen);
71  VADD3(tip, from, h1, backup);
72  pdv_3move(plotfp, from);
73  pdv_3cont(plotfp, tip);
74 
75  VSCALE(h2, c2, hooklen);
76  VADD3(tip, from, h2, backup);
77  pdv_3move(plotfp, tip);
78  }
79  if (!ZERO(toheadfract)) {
80  hooklen = toheadfract*len;
81  VSCALE(backup, diff, -hooklen);
82 
83  VSCALE(h1, c1, hooklen);
84  VADD3(tip, to, h1, backup);
85  pdv_3move(plotfp, to);
86  pdv_3cont(plotfp, tip);
87 
88  VSCALE(h2, c2, hooklen);
89  VADD3(tip, to, h2, backup);
90  pdv_3move(plotfp, tip);
91  }
92  /* Be certain "pen" is left at "to" position */
93  if (!ZERO(fromheadfract) || !ZERO(toheadfract))
94  pdv_3cont(plotfp, to);
95 
96 }
97 
98 void
99 PL_FORTRAN(f3vect, F3VECT)(FILE **fp, float *fx, float *fy, float *fz, float *tx, float *ty, float *tz, float *fl , float *tl)
100 {
101  point_t from, to;
102 
103  VSET(from, *fx, *fy, *fz);
104  VSET(to, *tx, *ty, *tz);
105  tp_3vector(*fp, from, to, *fl, *tl);
106 }
107 
108 /** @} */
109 /*
110  * Local Variables:
111  * mode: C
112  * tab-width: 8
113  * indent-tabs-mode: t
114  * c-file-style: "stroustrup"
115  * End:
116  * ex: shiftwidth=4 tabstop=8
117  */
#define SMALL
Definition: defines.h:351
void pdv_3move(register FILE *plotfp, const fastf_t *pt)
Definition: plot3.c:618
#define VSET(a, b, c, d)
Definition: color.c:53
Header file for the BRL-CAD common definitions.
void PL_FORTRAN(f3vect, F3VECT)
Definition: vector.c:99
void pdv_3cont(register FILE *plotfp, const fastf_t *pt)
Definition: plot3.c:630
void pdv_3line(register FILE *plotfp, const fastf_t *a, const fastf_t *b)
Definition: plot3.c:642
#define ZERO(val)
Definition: units.c:38
void bn_vec_ortho(vect_t out, const vect_t in)
double fastf_t
Definition: defines.h:300
void tp_3vector(FILE *plotfp, fastf_t *from, fastf_t *to, double fromheadfract, double toheadfract)
Draw a vector between points "from" and "to", with the option of having an arrowhead on either or bot...
Definition: vector.c:47