OpenCores
URL https://opencores.org/ocsvn/wf3d/wf3d/trunk

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [clib/] [mp_vector3.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   mp_vector3.c
7
//
8
// Abstract:
9
//   vector3 implementation
10
//
11
// Author:
12 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
13 2 specular
//
14
//======================================================================
15
//
16
// Copyright (c) 2015, Kenji Ishimaru
17
// All rights reserved.
18
//
19
// Redistribution and use in source and binary forms, with or without
20
// modification, are permitted provided that the following conditions are met:
21
//
22
//  -Redistributions of source code must retain the above copyright notice,
23
//   this list of conditions and the following disclaimer.
24
//  -Redistributions in binary form must reproduce the above copyright notice,
25
//   this list of conditions and the following disclaimer in the documentation
26
//   and/or other materials provided with the distribution.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
// Revision History
41
 
42
#include <math.h>
43
#include "mp_vector3.h"
44
 
45
void mp_vector3_mp_vector3(mp_vector3 *dst) {
46
    dst->x = 0.0;
47
    dst->y = 0.0;
48
    dst->z = 0.0;
49
}
50
 
51
void mp_vector3_set0(mp_vector3 *dst, float lx,  float ly,  float lz) {
52
    dst->x = lx;
53
    dst->y = ly;
54
    dst->z = lz;
55
}
56
 
57
void mp_vector3_set2(mp_vector3 *dst, mp_vector3 *v) {
58
    dst->x = v->x;
59
    dst->y = v->y;
60
    dst->z = v->z;
61
}
62
 
63
int mp_vector3_is_zero_vector(mp_vector3 *dst) {
64
    if (dst->x < 0 &&
65
        dst->y < 0 &&
66
        dst->z < 0) return 1;
67
 
68
    return 0;
69
}
70
 
71
float mp_vector3_size(mp_vector3 *dst) {
72
    float t = dst->x*dst->x + dst->y*dst->y + dst->z*dst->z;
73
    t = sqrt(t);
74
    return t;
75
}
76
 
77
float mp_vector3_size2(mp_vector3 *dst) {
78
    float t = dst->x*dst->x + dst->y*dst->y + dst->z*dst->z;
79
    return t;
80
}
81
 
82
void mp_vector3_normalize(mp_vector3 *dst) {
83
    float t = mp_vector3_size(dst);
84
    if (t <= 0) {
85
 
86
        dst->x = 0;
87
        dst->y = 0;
88
        dst->z = 0;
89
    } else {
90
      t = 1.0F/t;
91
      dst->x *= t;
92
      dst->y *= t;
93
      dst->z *= t;
94
    }
95
}
96
 
97
void mp_vector3_cross_product0(mp_vector3 *dst, mp_vector3 *a,  mp_vector3 *b){
98
    dst->x = a->y*b->z - b->y*a->z;
99
    dst->y = a->z*b->x - b->z*a->x;
100
    dst->z = a->x*b->y - b->x*a->y;
101
}
102
 
103
void mp_vector3_cross_product1(mp_vector3 *dst, mp_vector3 *b) {
104
    dst->x = dst->y*b->z - b->y*dst->z;
105
    dst->y = dst->z*b->x - b->z*dst->x;
106
    dst->z = dst->x*b->y - b->x*dst->y;
107
}
108
 
109
float mp_vector3_inner_product0(mp_vector3 *dst, mp_vector3 *a) {
110
    float t = dst->x*a->x + dst->y*a->y + dst->z*a->z;
111
    return t;
112
}
113
 
114
float mp_vector3_inner_product1(float x,  float y,  float z){
115
    float t = x*x + y*y + z*z;
116
    return t;
117
}
118
 
119
float mp_vector3_inner_product3( mp_vector3 *a,  mp_vector3 *b) {
120
    float t = a->x*b->x+a->y*b->y+a->z*b->z;
121
    return t;
122
}
123
 
124
void mp_vector3_scale(mp_vector3 *dst, float t) {
125
    dst->x *= t;
126
    dst->y *= t;
127
    dst->z *= t;
128
}
129
 
130
void mp_vector3_add0(mp_vector3 *dst, mp_vector3 *v) {
131
    dst->x += v->x;
132
    dst->y += v->y;
133
    dst->z += v->z;
134
}
135
 
136
void mp_vector3_subtract0(mp_vector3 *dst, mp_vector3 *v) {
137
    dst->x -= v->x;
138
    dst->y -= v->y;
139
    dst->z -= v->z;
140
}
141
 
142
void mp_vector3_assign0(mp_vector3 *dst, mp_vector3 *v) {
143
    dst->x = v->x;
144
    dst->y = v->y;
145
    dst->z = v->z;
146
}
147
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.