Tachyon (current)  Current Main Branch
vector.c
Go to the documentation of this file.
1 /*
2  * vector.c - This file contains all of the vector arithmetic functions.
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: vector.c,v 1.9 2022/02/18 17:55:28 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 
16 #define TACHYON_INTERNAL 1
17 #include "tachyon.h"
18 #include "macros.h"
19 
20 flt VDot(const vector *a, const vector *b) {
21  return (a->x*b->x + a->y*b->y + a->z*b->z);
22 }
23 
24 void VCross(const vector * a, const vector * b, vector * c) {
25  c->x = (a->y * b->z) - (a->z * b->y);
26  c->y = (a->z * b->x) - (a->x * b->z);
27  c->z = (a->x * b->y) - (a->y * b->x);
28 }
29 
30 flt VLength(const vector * a) {
31  return (flt) SQRT((a->x * a->x) + (a->y * a->y) + (a->z * a->z));
32 }
33 
34 void VNorm(vector * a) {
35  flt len;
36 
37  len=SQRT((a->x * a->x) + (a->y * a->y) + (a->z * a->z));
38  if (len != 0.0) {
39  a->x /= len;
40  a->y /= len;
41  a->z /= len;
42  }
43 }
44 
45 void VAdd(const vector * a, const vector * b, vector * c) {
46  c->x = (a->x + b->x);
47  c->y = (a->y + b->y);
48  c->z = (a->z + b->z);
49 }
50 
51 void VSub(const vector * a, const vector * b, vector * c) {
52  c->x = (a->x - b->x);
53  c->y = (a->y - b->y);
54  c->z = (a->z - b->z);
55 }
56 
57 void VAddS(flt a, const vector * A, const vector * B, vector * C) {
58  C->x = (a * A->x) + B->x;
59  C->y = (a * A->y) + B->y;
60  C->z = (a * A->z) + B->z;
61 }
62 
63 vector Raypnt(const ray * a, flt t) {
64  vector temp;
65 
66  temp.x=a->o.x + (a->d.x * t);
67  temp.y=a->o.y + (a->d.y * t);
68  temp.z=a->o.z + (a->d.z * t);
69 
70  return temp;
71 }
72 
73 void VScale(vector * a, flt s) {
74  a->x *= s;
75  a->y *= s;
76  a->z *= s;
77 }
78 
79 void ColorAddS(color * a, const color * b, flt s) {
80  a->r += b->r * s;
81  a->g += b->g * s;
82  a->b += b->b * s;
83 }
84 
85 void ColorAccum(color * a, const color * b) {
86  a->r += b->r;
87  a->g += b->g;
88  a->b += b->b;
89 }
90 
91 void ColorScale(color * a, flt s) {
92  a->r *= s;
93  a->g *= s;
94  a->b *= s;
95 }
96 
void ColorScale(color *a, flt s)
Definition: vector.c:91
void ColorAddS(color *a, const color *b, flt s)
Definition: vector.c:79
void VAdd(const vector *a, const vector *b, vector *c)
Definition: vector.c:45
void ColorAccum(color *a, const color *b)
Definition: vector.c:85
void VScale(vector *a, flt s)
Definition: vector.c:73
flt VDot(const vector *a, const vector *b)
Definition: vector.c:20
flt VLength(const vector *a)
Definition: vector.c:30
void VSub(const vector *a, const vector *b, vector *c)
Definition: vector.c:51
void VNorm(vector *a)
Definition: vector.c:34
double flt
generic floating point number, using double
Definition: tachyon.h:47
void VAddS(flt a, const vector *A, const vector *B, vector *C)
Definition: vector.c:57
#define SQRT(x)
Definition: util.h:31
vector Raypnt(const ray *a, flt t)
Definition: vector.c:63
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
void VCross(const vector *a, const vector *b, vector *c)
Definition: vector.c:24