~ubuntu-branches/ubuntu/quantal/rss-glx/quantal

« back to all changes in this revision

Viewing changes to reallyslick/rsMath/rsVec.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2009-06-03 18:41:32 UTC
  • mfrom: (1.1.5 upstream) (2.2.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090603184132-znjy66pq9xom7hac
Tags: 0.9.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop dependency on openal. It is not enabled by default anyway, and
    allows openal to migrate to universe.
  - src/skyrocket.{cpp,xml}: Disable sound by default.
  - Add --disable-sound configure flag to debian/rules, as we don't keep 
    the dependencies on openal nor freeglut (both universe packages).
* Dropped changes, merged upstream:
  - other_src/Makefile.am: fix the upstream build rules to actually use
    the ImageMagick CFLAGS returned by pkg-config.
  - Move the unconditional ImageMagick check up in configure.in so that
    our first PKG_CHECK_MODULES() call isn't hidden behind a shell
    conditional.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 1999-2005  Terence M. Welsh
3
 
 *
4
 
 * This file is part of rsMath.
5
 
 *
6
 
 * rsMath is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Lesser General Public
8
 
 * License version 2.1 as published by the Free Software Foundation.
9
 
 *
10
 
 * rsMath is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 */
19
 
 
20
 
 
21
 
#include <rsMath/rsMath.h>
22
 
#include <math.h>
23
 
 
24
 
 
25
 
 
26
 
rsVec::rsVec(){
27
 
}
28
 
 
29
 
rsVec::rsVec(float xx, float yy, float zz){
30
 
        v[0] = xx;
31
 
        v[1] = yy;
32
 
        v[2] = zz;
33
 
}
34
 
 
35
 
rsVec::~rsVec(){
36
 
}
37
 
 
38
 
void rsVec::set(float xx, float yy, float zz){
39
 
        v[0] = xx;
40
 
        v[1] = yy;
41
 
        v[2] = zz;
42
 
}
43
 
 
44
 
float rsVec::length(){
45
 
        return(float(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])));
46
 
}
47
 
 
48
 
float rsVec::normalize(){
49
 
        float length = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
50
 
        if(length == 0.0f){
51
 
                v[1] = 1.0f;
52
 
                return(0.0f);
53
 
        }
54
 
        const float normalizer(1.0f / length);
55
 
        v[0] *= normalizer;
56
 
        v[1] *= normalizer;
57
 
        v[2] *= normalizer;
58
 
        return(length);
59
 
}
60
 
 
61
 
float rsVec::dot(rsVec vec1){
62
 
        return(v[0] * vec1[0] + v[1] * vec1[1] + v[2] * vec1[2]);
63
 
}
64
 
 
65
 
void rsVec::cross(rsVec vec1, rsVec vec2){
66
 
        v[0] = vec1[1] * vec2[2] - vec2[1] * vec1[2];
67
 
        v[1] = vec1[2] * vec2[0] - vec2[2] * vec1[0];
68
 
        v[2] = vec1[0] * vec2[1] - vec2[0] * vec1[1];
69
 
}
70
 
 
71
 
void rsVec::scale(float scale){
72
 
        v[0] *= scale;
73
 
        v[1] *= scale;
74
 
        v[2] *= scale;
75
 
}
76
 
 
77
 
void rsVec::transPoint(const rsMatrix &m){
78
 
        float x = v[0];
79
 
        float y = v[1];
80
 
        float z = v[2];
81
 
        v[0] = x * m[0] + y * m[4] + z * m[8] + m[12];
82
 
        v[1] = x * m[1] + y * m[5] + z * m[9] + m[13];
83
 
        v[2] = x * m[2] + y * m[6] + z * m[10] + m[14];
84
 
}
85
 
 
86
 
 
87
 
void rsVec::transVec(const rsMatrix &m){
88
 
        float x = v[0];
89
 
        float y = v[1];
90
 
        float z = v[2];
91
 
        v[0] = x * m[0] + y * m[4] + z * m[8];
92
 
        v[1] = x * m[1] + y * m[5] + z * m[9];
93
 
        v[2] = x * m[2] + y * m[6] + z * m[10];
94
 
}
95
 
 
96
 
 
97
 
int rsVec::almostEqual(rsVec vec, float tolerance){
98
 
        if(sqrtf((v[0]-vec[0])*(v[0]-vec[0])
99
 
                + (v[1]-vec[1])*(v[1]-vec[1])
100
 
                + (v[2]-vec[2])*(v[2]-vec[2]))
101
 
                <= tolerance)
102
 
                return 1;
103
 
        else
104
 
                return 0;
105
 
}
106
 
 
107
 
 
108
 
 
109
 
 
110
 
 
111
 
// Generic vector math functions
112
 
float rsLength(float *xyz){
113
 
        return(float(sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2])));
114
 
}
115
 
 
116
 
 
117
 
float rsNormalize(float *xyz){
118
 
        float length = float(sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2]));
119
 
        if(length == 0.0f)
120
 
                return(0.0f);
121
 
        float reciprocal = 1.0f / length;
122
 
        xyz[0] *= reciprocal;
123
 
        xyz[1] *= reciprocal;
124
 
        xyz[2] *= reciprocal;
125
 
        // Really freakin' stupid compiler bug fix for VC++ 5.0
126
 
        /*xyz[0] /= length;
127
 
        xyz[1] /= length;
128
 
        xyz[2] /= length;*/
129
 
        return(length);
130
 
}
131
 
 
132
 
 
133
 
float rsDot(float *xyz1, float *xyz2){
134
 
        return(xyz1[0] * xyz2[0] + xyz1[1] * xyz2[1] + xyz1[2] * xyz2[2]);
135
 
}
136
 
 
137
 
 
138
 
void rsCross(float *xyz1, float *xyz2, float *xyzOut){
139
 
        xyzOut[0] = xyz1[1] * xyz2[2] - xyz2[1] * xyz1[2];
140
 
        xyzOut[1] = xyz1[2] * xyz2[0] - xyz2[2] * xyz1[0];
141
 
        xyzOut[2] = xyz1[0] * xyz2[1] - xyz2[0] * xyz1[1];
142
 
}
143
 
 
144
 
 
145
 
void rsScaleVec(float *xyz, float scale){
146
 
        xyz[0] *= scale;
147
 
        xyz[1] *= scale;
148
 
        xyz[2] *= scale;
149
 
}