rmath3d 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ba56d50a0d57ad2687e2d3fb34278961e0524e5
4
+ data.tar.gz: d12e16143db7abb2ceb3018ebba52081b72631dd
5
+ SHA512:
6
+ metadata.gz: 442b329ea744cafb2ed79b33e1443d604dce6770dbe2f2fb39cda1921a738a84f20ae4575ef837ff8a93d030d53285fb387e80b7d9a06323a9674ed8862703a0
7
+ data.tar.gz: 801f92748ee33a253fbb0bf232ed1879d99ff6ee2fd76b34673fa7dadcd4a7d69e1166f094d917c04786b3ee06f7ed790d716ffe3a777642a6bc658369b48773
data/ChangeLog ADDED
@@ -0,0 +1,73 @@
1
+ 2013-09-16 vaiorabbit <http://twitter.com/vaiorabbit>
2
+
3
+ * sample/opengl-bingings: Added sample using my opengl-bindings gem ( https://rubygems.org/gems/opengl-bindings ).
4
+
5
+ 2013-07-15 vaiorabbit <http://twitter.com/vaiorabbit>
6
+
7
+ * Fixed directory organization / source file names to comply with RubyGems' packaging system.
8
+
9
+ 2010-09-14 vaiorabbit <http://twitter.com/vaiorabbit>
10
+
11
+ * RVec3.c|h, RVec4.c|h, WrapRMath.c, RMath.rb (RVec3#transformTransposed, etc): Added.
12
+
13
+ 2010-08-15 vaiorabbit <http://twitter.com/vaiorabbit>
14
+
15
+ * RVec3.c, WrapRMath.c, RMath.rb (RVec3#transformByQuaternion): Added.
16
+
17
+ 2010-06-23 vaiorabbit <http://twitter.com/vaiorabbit>
18
+
19
+ * WrapRMath.c (RQuat_getElement/RVec3_getElement/RVec4_getElement):
20
+ Return values from R*GetElement() are now received by correct type (int -> rmReal).
21
+
22
+ 2010-01-03 vaiorabbit <http://twitter.com/vaiorabbit>
23
+
24
+ * RMtx4.c, WrapRMath.c, RMath.rb (RMtx4#getUpper3x3/RMtx4#setUpper3x3): Added.
25
+ * WrapRMath.c (RMtx4#e00=~RMtx4#e33=): The number of arguments to C function (4th argument of rb_define_method()) is corrected.
26
+
27
+ 2010-01-02 vaiorabbit <http://twitter.com/vaiorabbit>
28
+
29
+ * RMtx3.c, RMtx4.c, WrapRMath.c, RMath.rb (getRow/getColumn, SetRow/SetColumn): Added.
30
+
31
+ 2009-12-21 vaiorabbit <http://twitter.com/vaiorabbit>
32
+
33
+ * Update: Old contact information is updated to the current one.
34
+
35
+ 2008-08-30 vaiorabbit <http://twitter.com/vaiorabbit>
36
+
37
+ * RMtx3.c, RMtx4.c (RMtx3Inverse,RMtx4Inverse): Modified to reduce
38
+ the number of scalar multiplication.
39
+
40
+ 2008-08-02 vaiorabbit <http://twitter.com/vaiorabbit>
41
+
42
+ * WrapRMath.c (RMtx3_to_s, RMtx4_to_s, etc.): Functions for "to_s"
43
+ implementation now buid strings by +sprintf+, not +rb_str_plus+, etc.
44
+
45
+ 2008-07-27 vaiorabbit <http://twitter.com/vaiorabbit>
46
+
47
+ * RType.h (RMATH_SINGLE_PRECISION): Added. Enable this to force
48
+ RMath using +float+ for its internal calculation.
49
+
50
+ * WrapRMath.c (RMATH_ENABLE_ARGUMENT_CHECK): Added. Enable this
51
+ macro for strict type checking.
52
+
53
+ * WrapRMath.c: The codes for overriding "!=" operators are
54
+ removed. Note that the operation "a != b" is just a syntactic
55
+ shortcut of "!(a == b)", and the former expression is
56
+ automatically convertd into the latter one.
57
+ See: Programming Ruby: The Pragmatic Programmer's Guide
58
+ http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG
59
+ Both == and =~ have negated forms, != and !~. However, these are
60
+ converted by Ruby when your program is read. a!=b is equivalent
61
+ to !(a==b), and a!~b is the same as !(a=~b). This means that if
62
+ you write a class that overrides == or =~ you get a working !=
63
+ and !~ for free. But on the flip side, this also means that you
64
+ cannot define != and !~ independent of == and =~, respectively.
65
+
66
+ 2008-07-21 vaiorabbit <http://twitter.com/vaiorabbit>
67
+
68
+ * WrapRMath.c (RMtx3,RMtx4): Added new element-wise
69
+ getters/setters (mtx.e00, mtx.e00=, etc.)
70
+
71
+ 2008-07-20 vaiorabbit <http://twitter.com/vaiorabbit>
72
+
73
+ * Announce: First release.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ rmath3d : Ruby math module for 3D Applications
2
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
3
+
4
+ This software is provided 'as-is', without any express or implied
5
+ warranty. In no event will the authors be held liable for any damages
6
+ arising from the use of this software.
7
+
8
+ Permission is granted to anyone to use this software for any purpose,
9
+ including commercial applications, and to alter it and redistribute it
10
+ freely, subject to the following restrictions:
11
+
12
+ 1. The origin of this software must not be misrepresented; you must not
13
+ claim that you wrote the original software. If you use this software
14
+ in a product, an acknowledgment in the product documentation would be
15
+ appreciated but is not required.
16
+
17
+ 2. Altered source versions must be plainly marked as such, and must not be
18
+ misrepresented as being the original software.
19
+
20
+ 3. This notice may not be removed or altered from any source
21
+ distribution.
data/README.txt ADDED
@@ -0,0 +1,154 @@
1
+ = rmath3d : Ruby Math Module for 3D Applications
2
+
3
+ rmath3d is a math module for 3D game programming and computer graphics.
4
+
5
+ Last Update: Sep 16, 2013
6
+ Since: Jul 20, 2008
7
+ by vaiorabbit
8
+ == Features
9
+
10
+ === Supports frequently-used vector and matrix classes
11
+
12
+ * RMtx3 (3x3 matrix)
13
+ * RMtx4 (4x4 matrix)
14
+ * RQuat (Quaternion)
15
+ * RVec3 (3 element vector)
16
+ * RVec4 (4 element vector)
17
+
18
+ === Two implementations that are interchangeable with each other
19
+
20
+ 1. rmath3d.{so|bundle} : Ruby extension library for faster execution.
21
+ 2. rmath3d_plain.rb : Ruby implemantation for debugging use.
22
+
23
+
24
+ == Building rmath3d.{so|bundle}
25
+
26
+ === From source package
27
+
28
+ $ rake install
29
+
30
+ === For mkmf users
31
+
32
+ $ ruby extconf.rb
33
+ $ make
34
+
35
+ For Windows users, type commands below via the "Visual Studio 200{5|8}
36
+ Command Prompt". This process requires "cl.exe", "link.exe and
37
+ "mt.exe" to be exist in the program PATH:
38
+
39
+ X:\> ruby extconf.rb
40
+ X:\> nmake
41
+
42
+ You might encounter the "MSC version unmatch: _MSC_VER: XXXX is
43
+ expected." error at the time compiling "rmath3d.c". See the
44
+ instruction in the extconf.rb to avoid this error.
45
+
46
+
47
+ === Embedding manifest file (For Windows users)
48
+
49
+ Make sure that your rmath3d.so has "rmath3d.so.manifest" embedded into itself.
50
+ Otherwise, while using rmath3d.so in your application, you might come
51
+ across an error dialog saying:
52
+
53
+ ---------------------------
54
+ Microsoft Visual C++ Runtime Library
55
+ ---------------------------
56
+ Runtime Error!
57
+
58
+ Program: d:\ruby\bin\ruby.exe
59
+
60
+ R6034
61
+ An application has made an attempt to load the C runtime library incorrectly.
62
+ Please contact the application's support team for more information.
63
+
64
+ ---------------------------
65
+ OK
66
+ ---------------------------
67
+
68
+ To ensure the file status, try checking if the string "manifest" can
69
+ be found in the file:
70
+
71
+ X:\> strings rmath3d.so | grep manifest
72
+
73
+ If you don't see any outputs, it's time to embed "rmath3d.so.manifest".
74
+
75
+ X:\> mt -manifest rmath3d.so.manifest -outputresource:rmath3d.so;2
76
+
77
+ or run the "embed_manifest.bat" instead. Then check again the status.
78
+
79
+ X:\> strings rmath3d.so | grep manifest
80
+ <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
81
+
82
+ X:\>
83
+
84
+ If you see some similar output like that, the manifest file
85
+ "rmath3d.so.manifest" is successfully embedded.
86
+
87
+
88
+ == About rmath3d_plain.rb
89
+
90
+ rmath3d_plain.rb is a pure Ruby version of rmath3d.so: all the functionality of
91
+ rmath3d.so is implemented with plain Ruby code.
92
+ It is possible to trace the bugs caused by rmath3d.so by replacing the code
93
+
94
+ gem 'rmath3d'
95
+ require 'rmath3d/rmath3d'
96
+
97
+ with
98
+
99
+ gem 'rmath3d'
100
+ require 'rmath3d/rmath3d_plain'
101
+
102
+ rmath3d_plain.rb performs more strict type checking, that might be some burden
103
+ for its execution speed. So don't expect the performance efficiency
104
+ when you use rmath3d_plain.rb instead of rmath3d.so.
105
+
106
+
107
+ == Notes
108
+
109
+ === Collaboration with ruby-opengl
110
+
111
+ The instances of RMtx4 can be used directly as the arguments of the
112
+ ruby-opengl functions that accept matrices.
113
+
114
+ For example, this code snippet using RMath:
115
+
116
+ eye = RVec3.new(0.0, 15.0, 15.0)
117
+ at = RVec3.new(0.0, 0.0, 0.0)
118
+ up = RVec3.new(0.0, 1.0, 0.0)
119
+ mtxLookAt = RMtx4.new.lookAtRH( eye, at, up )
120
+ glMatrixMode( GL_PROJECTION )
121
+ glLoadMatrix( @mtxProj )
122
+
123
+ mtxProj = RMtx4.new.perspectiveFovRH( 30.0*Math::PI/180.0, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1, 1000.0 )
124
+ glMatrixMode( GL_MODELVIEW )
125
+ glLoadMatrix( @mtxLookAt )
126
+
127
+ has the same transformation effect with the OpenGL fixed pipeline:
128
+
129
+ glMatrixMode( GL_PROJECTION )
130
+ gluPerspective( 30.0*Math::PI/180.0, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1, 1000.0 )
131
+
132
+ glMatrixMode( GL_MODELVIEW )
133
+ gluLookAt( 0.0,15.0,15.0, 0.0,0.0,0.0, 0.0,1.0,0.0 )
134
+
135
+
136
+ === Compatibility with matrix.rb
137
+
138
+ Currently, there is no combenient way to convert between matrix.rb's
139
+ +Matrix+ and +RMtx{3|4}".
140
+
141
+ (The matrix.rb is a standard Ruby library that provides N-dimensional
142
+ +Vector+ class and "N x M" +Matrix+ class.)
143
+
144
+
145
+
146
+ == Credits
147
+
148
+ * vaiorabbit <http://twitter.com/vaiorabbit>
149
+
150
+
151
+ == License
152
+
153
+ All source codes are available under the terms of the zlib/libpng license
154
+ (see LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ require 'rake/clean'
4
+ require 'rbconfig'
5
+
6
+ NAME = 'rmath3d'
7
+
8
+ # "bundle", "so", etc.
9
+ DLEXT = RbConfig::CONFIG['DLEXT']
10
+
11
+ # rule to build the extension: this says
12
+ # that the extension should be rebuilt
13
+ # after any change to the files in ext
14
+ file "lib/#{NAME}/#{NAME}.#{DLEXT}" =>
15
+ Dir.glob("ext/#{NAME}/*{.rb,.c}") do
16
+ Dir.chdir("ext/#{NAME}") do
17
+ # this does essentially the same thing
18
+ # as what RubyGems does
19
+ ruby "extconf.rb", *ARGV.grep(/\A--/)
20
+ sh "make", *ARGV.grep(/\A(?!--)/)
21
+ end
22
+ cp "ext/#{NAME}/#{NAME}.#{DLEXT}", "lib/#{NAME}"
23
+ end
24
+
25
+ # make the :test task depend on the shared
26
+ # object, so it will be built automatically
27
+ # before running the tests
28
+ task :test => "lib/#{NAME}/#{NAME}.#{DLEXT}"
29
+
30
+ # use 'rake clean' and 'rake clobber' to
31
+ # easily delete generated files
32
+ CLEAN.include('ext/**/*{.o,.log,.#{DLEXT}}')
33
+ CLEAN.include('ext/**/Makefile')
34
+ CLOBBER.include('lib/**/*.#{DLEXT}')
35
+
36
+ # the same as before
37
+ Rake::TestTask.new do |t|
38
+ t.libs << 'test'
39
+ end
40
+
41
+ desc "Run tests"
42
+ task :default => :test
@@ -0,0 +1,35 @@
1
+ #ifndef RMATH_H_INCLUDED
2
+ #define RMATH_H_INCLUDED
3
+
4
+ #include "RType.h"
5
+ #include "RVec3.h"
6
+ #include "RVec4.h"
7
+ #include "RQuat.h"
8
+ #include "RMtx3.h"
9
+ #include "RMtx4.h"
10
+
11
+ #endif
12
+
13
+ /*
14
+ RMath : Ruby math module for 3D Applications
15
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
16
+
17
+ This software is provided 'as-is', without any express or implied
18
+ warranty. In no event will the authors be held liable for any damages
19
+ arising from the use of this software.
20
+
21
+ Permission is granted to anyone to use this software for any purpose,
22
+ including commercial applications, and to alter it and redistribute it
23
+ freely, subject to the following restrictions:
24
+
25
+ 1. The origin of this software must not be misrepresented; you must not
26
+ claim that you wrote the original software. If you use this software
27
+ in a product, an acknowledgment in the product documentation would be
28
+ appreciated but is not required.
29
+
30
+ 2. Altered source versions must be plainly marked as such, and must not be
31
+ misrepresented as being the original software.
32
+
33
+ 3. This notice may not be removed or altered from any source
34
+ distribution.
35
+ */
@@ -0,0 +1,370 @@
1
+ #include <math.h>
2
+ #include <string.h>
3
+
4
+ #include "RVec3.h"
5
+ #include "RQuat.h"
6
+ #include "RMtx3.h"
7
+
8
+ /* NOTE : column-major */
9
+ #define SET_ELEMENT(out, row, col, val) (out)->e[(col)*3+(row)] = (val)
10
+ #define GET_ELEMENT(in, row, col) ((in)->e[(col)*3+(row)])
11
+
12
+ void
13
+ RMtx3SetElement( RMtx3* out, int row, int col, rmReal e )
14
+ {
15
+ SET_ELEMENT( out, row, col, e );
16
+ }
17
+
18
+ rmReal
19
+ RMtx3GetElement( const RMtx3* in, int row, int col )
20
+ {
21
+ return GET_ELEMENT( in, row, col );
22
+ }
23
+
24
+ void
25
+ RMtx3GetRow( RVec3* out, const RMtx3* in, int row )
26
+ {
27
+ RVec3SetX( out, GET_ELEMENT( in, row, 0 ) );
28
+ RVec3SetY( out, GET_ELEMENT( in, row, 1 ) );
29
+ RVec3SetZ( out, GET_ELEMENT( in, row, 2 ) );
30
+ }
31
+
32
+ void
33
+ RMtx3GetColumn( RVec3* out, const RMtx3* in, int col )
34
+ {
35
+ RVec3SetX( out, GET_ELEMENT( in, 0, col ) );
36
+ RVec3SetY( out, GET_ELEMENT( in, 1, col ) );
37
+ RVec3SetZ( out, GET_ELEMENT( in, 2, col ) );
38
+ }
39
+
40
+ void
41
+ RMtx3SetRow( RMtx3* out, const RVec3* in, int row )
42
+ {
43
+ SET_ELEMENT( out, row, 0, RVec3GetElement( in, 0 ) );
44
+ SET_ELEMENT( out, row, 1, RVec3GetElement( in, 1 ) );
45
+ SET_ELEMENT( out, row, 2, RVec3GetElement( in, 2 ) );
46
+ }
47
+
48
+ void
49
+ RMtx3SetColumn( RMtx3* out, const RVec3* in, int col )
50
+ {
51
+ SET_ELEMENT( out, 0, col, RVec3GetElement( in, 0 ) );
52
+ SET_ELEMENT( out, 1, col, RVec3GetElement( in, 1 ) );
53
+ SET_ELEMENT( out, 2, col, RVec3GetElement( in, 2 ) );
54
+ }
55
+
56
+ void
57
+ RMtx3Copy( RMtx3* out, const RMtx3* in )
58
+ {
59
+ memmove( out, in, sizeof(RMtx3) );
60
+ }
61
+
62
+ void
63
+ RMtx3SetElements( RMtx3* out,
64
+ rmReal e00, rmReal e01, rmReal e02,
65
+ rmReal e10, rmReal e11, rmReal e12,
66
+ rmReal e20, rmReal e21, rmReal e22
67
+ )
68
+ {
69
+ SET_ELEMENT( out, 0, 0, e00 );
70
+ SET_ELEMENT( out, 0, 1, e01 );
71
+ SET_ELEMENT( out, 0, 2, e02 );
72
+
73
+ SET_ELEMENT( out, 1, 0, e10 );
74
+ SET_ELEMENT( out, 1, 1, e11 );
75
+ SET_ELEMENT( out, 1, 2, e12 );
76
+
77
+ SET_ELEMENT( out, 2, 0, e20 );
78
+ SET_ELEMENT( out, 2, 1, e21 );
79
+ SET_ELEMENT( out, 2, 2, e22 );
80
+ }
81
+
82
+ void
83
+ RMtx3Zero( RMtx3* out )
84
+ {
85
+ int row, col;
86
+ for ( row = 0; row < 3; ++row )
87
+ for ( col = 0; col < 3; ++col )
88
+ SET_ELEMENT( out, row, col, 0.0f );
89
+ }
90
+
91
+ void
92
+ RMtx3Identity( RMtx3* out )
93
+ {
94
+ int row, col;
95
+ for ( row = 0; row < 3; ++row )
96
+ for ( col = 0; col < 3; ++col )
97
+ SET_ELEMENT( out, row, col, (row==col) ? 1.0f : 0.0f );
98
+ }
99
+
100
+ rmReal
101
+ RMtx3Determinant( const RMtx3* in )
102
+ {
103
+ #define I( r, c ) GET_ELEMENT( in, (r), (c) )
104
+ #define D( e00, e01, e10, e11 ) ((e00)*(e11)-(e01)*(e10))
105
+
106
+ return
107
+ I(0,0) * D( I(1,1), I(1,2), I(2,1), I(2,2) ) -
108
+ I(0,1) * D( I(1,0), I(1,2), I(2,0), I(2,2) ) +
109
+ I(0,2) * D( I(1,0), I(1,1), I(2,0), I(2,1) ) ;
110
+
111
+ #undef I
112
+ #undef D
113
+ }
114
+
115
+ void
116
+ RMtx3Transpose( RMtx3* out, const RMtx3* in )
117
+ {
118
+ int row, col;
119
+ RMtx3 tmp;
120
+ for ( row = 0; row < 3; ++row )
121
+ for ( col = 0; col < 3; ++col )
122
+ SET_ELEMENT( &tmp, row, col, GET_ELEMENT( in, col, row ) );
123
+
124
+ RMtx3Copy( out, &tmp );
125
+ }
126
+
127
+ rmReal
128
+ RMtx3Inverse( RMtx3* out, const RMtx3* in )
129
+ {
130
+ #define I( r, c ) GET_ELEMENT( in, (r), (c) )
131
+ #define R( r, c ) GET_ELEMENT( &result, (r), (c) )
132
+ #define D( e00, e01, e10, e11 ) ((e00)*(e11)-(e01)*(e10))
133
+
134
+ RMtx3 result;
135
+ rmReal det;
136
+
137
+ SET_ELEMENT( &result, 0, 0, D( I(1,1),I(1,2), I(2,1),I(2,2) ) );
138
+ SET_ELEMENT( &result, 0, 1, -D( I(0,1),I(0,2), I(2,1),I(2,2) ) );
139
+ SET_ELEMENT( &result, 0, 2, D( I(0,1),I(0,2), I(1,1),I(1,2) ) );
140
+
141
+ SET_ELEMENT( &result, 1, 0, -D( I(1,0),I(1,2), I(2,0),I(2,2) ) );
142
+ SET_ELEMENT( &result, 1, 1, D( I(0,0),I(0,2), I(2,0),I(2,2) ) );
143
+ SET_ELEMENT( &result, 1, 2, -D( I(0,0),I(0,2), I(1,0),I(1,2) ) );
144
+
145
+ SET_ELEMENT( &result, 2, 0, D( I(1,0),I(1,1), I(2,0),I(2,1) ) );
146
+ SET_ELEMENT( &result, 2, 1, -D( I(0,0),I(0,1), I(2,0),I(2,1) ) );
147
+ SET_ELEMENT( &result, 2, 2, D( I(0,0),I(0,1), I(1,0),I(1,1) ) );
148
+
149
+ det = I(0,0) * R(0,0) + I(0,1) * R(1,0) + I(0,2) * R(2,0);
150
+
151
+ if ( rmFabs(det) < RMATH_TOLERANCE )
152
+ return det;
153
+
154
+ RMtx3Scale( out, &result, 1.0f / det );
155
+
156
+ return det;
157
+
158
+ #undef I
159
+ #undef R
160
+ #undef D
161
+ }
162
+
163
+
164
+ /* http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29
165
+ http://en.wikipedia.org/wiki/Rotation_matrix
166
+ */
167
+ void
168
+ RMtx3RotationX( RMtx3* out, rmReal radian )
169
+ {
170
+ rmReal s = rmSin( radian );
171
+ rmReal c = rmCos( radian );
172
+
173
+ RMtx3Identity( out );
174
+ SET_ELEMENT( out, 1, 1, c );
175
+ SET_ELEMENT( out, 1, 2, -s );
176
+ SET_ELEMENT( out, 2, 1, s );
177
+ SET_ELEMENT( out, 2, 2, c );
178
+ }
179
+
180
+ /* http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29
181
+ http://en.wikipedia.org/wiki/Rotation_matrix
182
+ */
183
+ void
184
+ RMtx3RotationY( RMtx3* out, rmReal radian )
185
+ {
186
+ rmReal s = rmSin( radian );
187
+ rmReal c = rmCos( radian );
188
+
189
+ RMtx3Identity( out );
190
+ SET_ELEMENT( out, 0, 0, c );
191
+ SET_ELEMENT( out, 0, 2, s );
192
+ SET_ELEMENT( out, 2, 0, -s );
193
+ SET_ELEMENT( out, 2, 2, c );
194
+ }
195
+
196
+ /* http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29
197
+ http://en.wikipedia.org/wiki/Rotation_matrix
198
+ */
199
+ void
200
+ RMtx3RotationZ( RMtx3* out, rmReal radian )
201
+ {
202
+ rmReal s = rmSin( radian );
203
+ rmReal c = rmCos( radian );
204
+
205
+ RMtx3Identity( out );
206
+ SET_ELEMENT( out, 0, 0, c );
207
+ SET_ELEMENT( out, 0, 1, -s );
208
+ SET_ELEMENT( out, 1, 0, s );
209
+ SET_ELEMENT( out, 1, 1, c );
210
+ }
211
+
212
+ /* http://en.wikipedia.org/wiki/Rotation_matrix
213
+ */
214
+ void
215
+ RMtx3RotationAxis( RMtx3* out, const RVec3* axis, rmReal radian )
216
+ {
217
+ rmReal s = rmSin( radian );
218
+ rmReal c = rmCos( radian );
219
+ rmReal C = 1.0f - c;
220
+ rmReal x = RVec3GetX( axis );
221
+ rmReal y = RVec3GetY( axis );
222
+ rmReal z = RVec3GetZ( axis );
223
+
224
+ SET_ELEMENT( out, 0, 0, x*x*C + c );
225
+ SET_ELEMENT( out, 0, 1, x*y*C - z*s );
226
+ SET_ELEMENT( out, 0, 2, z*x*C + y*s );
227
+ SET_ELEMENT( out, 1, 0, x*y*C + z*s );
228
+ SET_ELEMENT( out, 1, 1, y*y*C + c );
229
+ SET_ELEMENT( out, 1, 2, y*z*C - x*s );
230
+ SET_ELEMENT( out, 2, 0, z*x*C - y*s );
231
+ SET_ELEMENT( out, 2, 1, y*z*C + x*s );
232
+ SET_ELEMENT( out, 2, 2, z*z*C + c );
233
+ }
234
+
235
+ /* From Quaternion to Matrix and Back
236
+ http://www.intel.com/cd/ids/developer/asmo-na/eng/293748.htm
237
+ */
238
+ void
239
+ RMtx3RotationQuaternion( RMtx3* out, const struct RQuat* q )
240
+ {
241
+ rmReal x = RQuatGetX( q );
242
+ rmReal y = RQuatGetY( q );
243
+ rmReal z = RQuatGetZ( q );
244
+ rmReal w = RQuatGetW( q );
245
+
246
+ rmReal x2 = 2.0f * x;
247
+ rmReal y2 = 2.0f * y;
248
+ rmReal z2 = 2.0f * z;
249
+
250
+ rmReal xx2 = x * x2;
251
+ rmReal yy2 = y * y2;
252
+ rmReal zz2 = z * z2;
253
+
254
+ rmReal yz2 = y * z2;
255
+ rmReal wx2 = w * x2;
256
+ rmReal xy2 = x * y2;
257
+ rmReal wz2 = w * z2;
258
+ rmReal xz2 = x * z2;
259
+ rmReal wy2 = w * y2;
260
+
261
+ RMtx3Identity( out );
262
+ SET_ELEMENT( out, 0, 0, 1.0f - yy2 - zz2 );
263
+ SET_ELEMENT( out, 1, 0, xy2 + wz2 );
264
+ SET_ELEMENT( out, 2, 0, xz2 - wy2 );
265
+ SET_ELEMENT( out, 0, 1, xy2 - wz2 );
266
+ SET_ELEMENT( out, 1, 1, 1.0f - xx2 - zz2 );
267
+ SET_ELEMENT( out, 2, 1, yz2 + wx2 );
268
+ SET_ELEMENT( out, 0, 2, xz2 + wy2 );
269
+ SET_ELEMENT( out, 1, 2, yz2 - wx2 );
270
+ SET_ELEMENT( out, 2, 2, 1.0f - xx2 - yy2 );
271
+ }
272
+
273
+ void
274
+ RMtx3Scaling( RMtx3* out, rmReal sx, rmReal sy, rmReal sz )
275
+ {
276
+ RMtx3Identity( out );
277
+ SET_ELEMENT( out, 0, 0, sx );
278
+ SET_ELEMENT( out, 1, 1, sy );
279
+ SET_ELEMENT( out, 2, 2, sz );
280
+ }
281
+
282
+
283
+ int
284
+ RMtx3Equal( const RMtx3* m1, const RMtx3* m2 )
285
+ {
286
+ if ( 0 == memcmp( m1, m2, sizeof(RMtx3) ) )
287
+ return !0;
288
+ else
289
+ return 0;
290
+ }
291
+
292
+
293
+ void
294
+ RMtx3Add( RMtx3* out, const RMtx3* m1, const RMtx3* m2 )
295
+ {
296
+ int row, col;
297
+ RMtx3 tmp;
298
+ for ( row = 0; row < 3; ++row )
299
+ for ( col = 0; col < 3; ++col )
300
+ SET_ELEMENT( &tmp, row, col,
301
+ GET_ELEMENT( m1, row, col ) + GET_ELEMENT( m2, row, col ) );
302
+
303
+ RMtx3Copy( out, &tmp );
304
+ }
305
+
306
+ void
307
+ RMtx3Sub( RMtx3* out, const RMtx3* m1, const RMtx3* m2 )
308
+ {
309
+ int row, col;
310
+ for ( row = 0; row < 3; ++row )
311
+ for ( col = 0; col < 3; ++col )
312
+ SET_ELEMENT( out, row, col,
313
+ GET_ELEMENT( m1, row, col ) - GET_ELEMENT( m2, row, col ) );
314
+ }
315
+
316
+ void
317
+ RMtx3Mul( RMtx3* out, const RMtx3* m1, const RMtx3* m2 )
318
+ {
319
+ int row, col;
320
+ RMtx3 tmp;
321
+ for ( row = 0; row < 3; ++row )
322
+ {
323
+ for ( col = 0; col < 3; ++col )
324
+ {
325
+ int i;
326
+ rmReal sum = 0.0f;
327
+ for ( i = 0; i < 3; ++i )
328
+ {
329
+ sum += GET_ELEMENT( m1, row, i ) * GET_ELEMENT( m2, i, col );
330
+ }
331
+ SET_ELEMENT( &tmp, row, col, sum );
332
+ }
333
+ }
334
+
335
+ RMtx3Copy( out, &tmp );
336
+ }
337
+
338
+ void
339
+ RMtx3Scale( RMtx3* out, const RMtx3* m, rmReal f )
340
+ {
341
+ int row, col;
342
+ for ( row = 0; row < 3; ++row )
343
+ for ( col = 0; col < 3; ++col )
344
+ SET_ELEMENT( out, row, col,
345
+ GET_ELEMENT( m, row, col ) * f );
346
+ }
347
+
348
+ /*
349
+ RMath : Ruby math module for 3D Applications
350
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
351
+
352
+ This software is provided 'as-is', without any express or implied
353
+ warranty. In no event will the authors be held liable for any damages
354
+ arising from the use of this software.
355
+
356
+ Permission is granted to anyone to use this software for any purpose,
357
+ including commercial applications, and to alter it and redistribute it
358
+ freely, subject to the following restrictions:
359
+
360
+ 1. The origin of this software must not be misrepresented; you must not
361
+ claim that you wrote the original software. If you use this software
362
+ in a product, an acknowledgment in the product documentation would be
363
+ appreciated but is not required.
364
+
365
+ 2. Altered source versions must be plainly marked as such, and must not be
366
+ misrepresented as being the original software.
367
+
368
+ 3. This notice may not be removed or altered from any source
369
+ distribution.
370
+ */