1.5からコピーしたので1.4.3用に修正
@@ -1,30 +0,0 @@ | ||
1 | -/* 行列 */ | |
2 | -struct DXRubyMatrix { | |
3 | - int x; | |
4 | - int y; | |
5 | - union { | |
6 | - struct { | |
7 | - float m11;float m12; float m13; float m14; | |
8 | - float m21;float m22; float m23; float m24; | |
9 | - float m31;float m32; float m33; float m34; | |
10 | - float m41;float m42; float m43; float m44; | |
11 | - }; | |
12 | - float m[4][4]; | |
13 | - }; | |
14 | -}; | |
15 | - | |
16 | -/* ベクトル */ | |
17 | -struct DXRubyVector { | |
18 | - int x; | |
19 | - union { | |
20 | - struct { | |
21 | - float v1;float v2; float v3; float v4; | |
22 | - }; | |
23 | - float v[4]; | |
24 | - }; | |
25 | -}; | |
26 | - | |
27 | -void Init_dxruby_Matrix(void); | |
28 | -VALUE Vector_allocate( VALUE klass ); | |
29 | -void Matrix_release( struct DXRubyMatrix* mat ); | |
30 | -void Vector_release( struct DXRubyVector* vec ); |
@@ -1,1221 +0,0 @@ | ||
1 | -#define WINVER 0x0500 /* バージョン定義 Windows2000以上 */ | |
2 | -#define _WIN32_WINNT WINVER | |
3 | - | |
4 | -#include "ruby.h" | |
5 | -#ifndef RUBY_ST_H | |
6 | -#include "st.h" | |
7 | -#endif | |
8 | - | |
9 | -#define DXRUBY_EXTERN 1 | |
10 | -#include "dxruby.h" | |
11 | -#include "matrix.h" | |
12 | - | |
13 | -VALUE cMatrix; /* 行列クラス */ | |
14 | -VALUE cVector; /* ベクトルクラス */ | |
15 | - | |
16 | -#ifdef DXRUBY_USE_TYPEDDATA | |
17 | -const rb_data_type_t Matrix_data_type; | |
18 | -const rb_data_type_t Vector_data_type; | |
19 | -#endif | |
20 | - | |
21 | -static float pi = 3.141592653589793115997963468544185161590576171875f; | |
22 | - | |
23 | -/********************************************************************* | |
24 | - * Matrixクラス | |
25 | - * | |
26 | - * 行列を表現する。 | |
27 | - *********************************************************************/ | |
28 | - | |
29 | -/*-------------------------------------------------------------------- | |
30 | - 参照されなくなったときにGCから呼ばれる関数 | |
31 | - ---------------------------------------------------------------------*/ | |
32 | -void Matrix_release( struct DXRubyMatrix* mat ) | |
33 | -{ | |
34 | - free( mat ); | |
35 | -} | |
36 | - | |
37 | -#ifdef DXRUBY_USE_TYPEDDATA | |
38 | -const rb_data_type_t Matrix_data_type = { | |
39 | - "Matrix", | |
40 | - { | |
41 | - 0, | |
42 | - Matrix_release, | |
43 | - 0, | |
44 | - }, | |
45 | - NULL, NULL | |
46 | -}; | |
47 | -#endif | |
48 | - | |
49 | -/*-------------------------------------------------------------------- | |
50 | - Matrixクラスのallocate。メモリを確保する為にinitialize前に呼ばれる。 | |
51 | - ---------------------------------------------------------------------*/ | |
52 | -static VALUE Matrix_allocate( VALUE klass ) | |
53 | -{ | |
54 | - VALUE obj; | |
55 | - struct DXRubyMatrix *mat; | |
56 | - | |
57 | - /* DXRubyMatrixのメモリ取得&Matrixオブジェクト生成 */ | |
58 | - mat = malloc( sizeof( struct DXRubyMatrix ) ); | |
59 | - if( mat == NULL ) rb_raise( eDXRubyError, "メモリの取得に失敗しました - Matrix_allocate" ); | |
60 | -#ifdef DXRUBY_USE_TYPEDDATA | |
61 | - obj = TypedData_Wrap_Struct( klass, &Matrix_data_type, mat ); | |
62 | -#else | |
63 | - obj = Data_Wrap_Struct( klass, 0, Matrix_release, mat ); | |
64 | -#endif | |
65 | - mat->x = 0; | |
66 | - mat->y = 0; | |
67 | - ZeroMemory( mat->m, sizeof( float ) * 16 ); | |
68 | - | |
69 | - return obj; | |
70 | -} | |
71 | - | |
72 | -/*-------------------------------------------------------------------- | |
73 | - MatrixクラスのInitialize | |
74 | - ---------------------------------------------------------------------*/ | |
75 | -static VALUE Matrix_initialize( int argc, VALUE *argv, VALUE self ) | |
76 | -{ | |
77 | - struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self ); | |
78 | - VALUE ary, *ary_p; | |
79 | - int i, j, count; | |
80 | - | |
81 | - if( argc == 0 ) | |
82 | - { | |
83 | - mat->m11 = 1;mat->m22 = 1;mat->m33 = 1;mat->m44 = 1; | |
84 | - mat->x = mat->y = 4; | |
85 | - return self; | |
86 | - } | |
87 | - | |
88 | - if( argc == 1 ) | |
89 | - { | |
90 | - ary = argv[0]; | |
91 | - Check_Type( ary, T_ARRAY ); | |
92 | - if( RARRAY_LEN( ary ) > 4 || RARRAY_LEN( ary ) < 1 ) rb_raise( eDXRubyError, "配列の数が正しくありません。 - Matrix_initialize"); | |
93 | - ary_p = RARRAY_PTR( ary ); | |
94 | - count = RARRAY_LEN( ary ); | |
95 | - } | |
96 | - else | |
97 | - { | |
98 | - if( argc > 4 ) rb_raise( eDXRubyError, "引数の数が正しくありません。 - Matrix_initialize"); | |
99 | - ary_p = argv; | |
100 | - count = argc; | |
101 | - } | |
102 | - | |
103 | - mat->y = count; | |
104 | - for( i = 0; i < count; i++ ) | |
105 | - { | |
106 | - VALUE ary2 = ary_p[i]; | |
107 | - Check_Type( ary2, T_ARRAY ); | |
108 | - if( RARRAY_LEN( ary2 ) > 4 || RARRAY_LEN( ary2 ) < 1 || RARRAY_LEN( ary2 ) != mat->y ) rb_raise( eDXRubyError, "配列の数が正しくありません。 - Matrix_initialize"); | |
109 | - | |
110 | - for( j = 0; j < RARRAY_LEN( ary2 ); j++ ) | |
111 | - { | |
112 | - mat->m[i][j] = NUM2FLOAT( RARRAY_PTR( ary2 )[j] ); | |
113 | - } | |
114 | - } | |
115 | - mat->x = RARRAY_LEN( ary_p[0] ); | |
116 | - | |
117 | - return self; | |
118 | -} | |
119 | - | |
120 | -static VALUE Matrix_mul( VALUE self, VALUE varg ) | |
121 | -{ | |
122 | - struct DXRubyMatrix *mat_d = DXRUBY_GET_STRUCT( Matrix, self ); | |
123 | - struct DXRubyMatrix *result; | |
124 | - VALUE vresult; | |
125 | - | |
126 | - if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
127 | - { | |
128 | - int i, j; | |
129 | - | |
130 | - vresult = Matrix_allocate( cMatrix ); | |
131 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
132 | - result->x = mat_d->x; | |
133 | - result->y = mat_d->y; | |
134 | - for( i = 0; i < mat_d->y; i++ ) | |
135 | - { | |
136 | - for( j = 0; j < mat_d->x; j++ ) | |
137 | - { | |
138 | - result->m[i][j] = mat_d->m[i][j] * NUM2FLOAT( varg ); | |
139 | - } | |
140 | - } | |
141 | - } | |
142 | - else | |
143 | - { | |
144 | - int i, j, k; | |
145 | - struct DXRubyMatrix *mat_s; | |
146 | - | |
147 | - DXRUBY_CHECK_TYPE( Matrix, varg ); | |
148 | - mat_s = DXRUBY_GET_STRUCT( Matrix, varg ); | |
149 | - | |
150 | - if( mat_d->x != mat_s->y || mat_d->y != mat_s->x ) rb_raise( eDXRubyError, "要素数が一致していません。 - Matrix_*"); | |
151 | - vresult = Matrix_allocate( cMatrix ); | |
152 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
153 | - result->x = mat_s->x; | |
154 | - result->y = mat_d->y; | |
155 | - | |
156 | - for( i = 0; i < mat_d->y; i++ ) | |
157 | - { | |
158 | - for( j = 0; j < mat_s->x; j++ ) | |
159 | - { | |
160 | - for( k = 0; k < mat_s->x; k++ ) | |
161 | - { | |
162 | - result->m[i][j] += mat_d->m[i][k] * mat_s->m[k][j]; | |
163 | - } | |
164 | - } | |
165 | - } | |
166 | - } | |
167 | - return vresult; | |
168 | -} | |
169 | - | |
170 | - | |
171 | -static VALUE Matrix_to_s( VALUE self ) | |
172 | -{ | |
173 | - struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self ); | |
174 | - char buf[1024]; | |
175 | - char temp[256]; | |
176 | - int i; | |
177 | - | |
178 | - sprintf( buf, "size = %d,%d ", mat->x, mat->y ); | |
179 | - | |
180 | - for( i = 0; i < mat->y; i++ ) | |
181 | - { | |
182 | - switch( mat->x ) { | |
183 | - case 1: | |
184 | - sprintf( temp, "(%f)", mat->m[i][0] ); | |
185 | - strcat( buf, temp ); | |
186 | - break; | |
187 | - case 2: | |
188 | - sprintf( temp, "(%f, %f)", mat->m[i][0], mat->m[i][1] ); | |
189 | - strcat( buf, temp ); | |
190 | - break; | |
191 | - case 3: | |
192 | - sprintf( temp, "(%f, %f, %f)", mat->m[i][0], mat->m[i][1], mat->m[i][2] ); | |
193 | - strcat( buf, temp ); | |
194 | - break; | |
195 | - default: | |
196 | - sprintf( temp, "(%f, %f, %f, %f)", mat->m[i][0], mat->m[i][1], mat->m[i][2], mat->m[i][3] ); | |
197 | - strcat( buf, temp ); | |
198 | - break; | |
199 | - } | |
200 | - } | |
201 | - | |
202 | - return rb_str_new2( buf ); | |
203 | -} | |
204 | - | |
205 | -/* ビュー行列作成 */ | |
206 | -static VALUE Matrix_look_at( VALUE klass, VALUE veye, VALUE vat, VALUE vup ) | |
207 | -{ | |
208 | - struct DXRubyMatrix *result; | |
209 | - D3DVECTOR eye, at, up; | |
210 | - struct DXRubyVector *vec_eye, *vec_at, *vec_up; | |
211 | - VALUE vresult; | |
212 | - | |
213 | - DXRUBY_CHECK_TYPE( Vector, veye ); | |
214 | - vec_eye = DXRUBY_GET_STRUCT( Vector, veye ); | |
215 | - DXRUBY_CHECK_TYPE( Vector, vat ); | |
216 | - vec_at = DXRUBY_GET_STRUCT( Vector, vat ); | |
217 | - DXRUBY_CHECK_TYPE( Vector, vup ); | |
218 | - vec_up = DXRUBY_GET_STRUCT( Vector, vup ); | |
219 | - | |
220 | - vresult = Matrix_allocate( cMatrix ); | |
221 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
222 | - | |
223 | -// memcpy( &matrix, mat->m, sizeof( float ) * 16 ); | |
224 | -// D3DXMatrixInverse( (D3DMATRIX *)result->m, 0, (D3DMATRIX *)mat->m); | |
225 | - | |
226 | - eye.x = vec_eye->v1; | |
227 | - eye.y = vec_eye->v2; | |
228 | - eye.z = vec_eye->v3; | |
229 | - at.x = vec_at->v1; | |
230 | - at.y = vec_at->v2; | |
231 | - at.z = vec_at->v3; | |
232 | - up.x = vec_up->v1; | |
233 | - up.y = vec_up->v2; | |
234 | - up.z = vec_up->v3; | |
235 | - D3DXMatrixLookAtLH( (D3DMATRIX *)result->m, &eye, &at, &up ); | |
236 | - | |
237 | - result->x = 4; | |
238 | - result->y = 4; | |
239 | - return vresult; | |
240 | -} | |
241 | - | |
242 | - | |
243 | -/* 射影変換行列作成 */ | |
244 | -static VALUE Matrix_create_projection( VALUE klass, VALUE vwidth, VALUE vheight, VALUE vzn, VALUE vzf ) | |
245 | -{ | |
246 | - struct DXRubyMatrix *result; | |
247 | - VALUE vresult; | |
248 | - vresult = Matrix_allocate( cMatrix ); | |
249 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
250 | - result->x = result->y = 4; | |
251 | - | |
252 | - D3DXMatrixPerspectiveLH( (D3DMATRIX*)result->m, NUM2FLOAT( vwidth ), NUM2FLOAT( vheight ), NUM2FLOAT( vzn ), NUM2FLOAT( vzf ) ); | |
253 | - | |
254 | - return vresult; | |
255 | -} | |
256 | - | |
257 | - | |
258 | -/* 射影変換行列作成(画角指定) */ | |
259 | -static VALUE Matrix_create_projection_fov( VALUE klass, VALUE vfov, VALUE vaspect, VALUE vzn, VALUE vzf ) | |
260 | -{ | |
261 | - struct DXRubyMatrix *result; | |
262 | - D3DMATRIX matrix; | |
263 | - VALUE vresult; | |
264 | - float angle; | |
265 | - vresult = Matrix_allocate( cMatrix ); | |
266 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
267 | - result->x = result->y = 4; | |
268 | - | |
269 | - angle = pi / 180.0f * NUM2FLOAT( vfov ); | |
270 | - D3DXMatrixPerspectiveFovLH( (D3DMATRIX*)result->m, angle, NUM2FLOAT( vaspect ), NUM2FLOAT( vzn ), NUM2FLOAT( vzf ) ); | |
271 | - | |
272 | - return vresult; | |
273 | -} | |
274 | - | |
275 | - | |
276 | -/* 正射影変換行列作成 */ | |
277 | -static VALUE Matrix_create_projection_ortho( VALUE klass, VALUE vwidth, VALUE vheight, VALUE vzn, VALUE vzf ) | |
278 | -{ | |
279 | - struct DXRubyMatrix *result; | |
280 | - D3DMATRIX matrix; | |
281 | - VALUE vresult; | |
282 | - vresult = Matrix_allocate( cMatrix ); | |
283 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
284 | - result->x = result->y = 4; | |
285 | - | |
286 | - D3DXMatrixOrthoLH( (D3DMATRIX*)result->m, NUM2FLOAT( vwidth ), NUM2FLOAT( vheight ), NUM2FLOAT( vzn ), NUM2FLOAT( vzf ) ); | |
287 | - | |
288 | - return vresult; | |
289 | -} | |
290 | - | |
291 | - | |
292 | -/* 2D回転行列作成 */ | |
293 | -static VALUE Matrix_create_rot( VALUE klass, VALUE vangle ) | |
294 | -{ | |
295 | - struct DXRubyMatrix *result; | |
296 | - VALUE vresult; | |
297 | - float angle; | |
298 | - | |
299 | - vresult = Matrix_allocate( cMatrix ); | |
300 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
301 | - | |
302 | - result->x = 3; | |
303 | - result->y = 3; | |
304 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
305 | - result->m11 = cos( angle ); | |
306 | - result->m12 = sin( angle ); | |
307 | - result->m21 = -result->m12; | |
308 | - result->m22 = result->m11; | |
309 | - result->m33 = 1; | |
310 | - | |
311 | - return vresult; | |
312 | -} | |
313 | -/* x軸回転行列作成 */ | |
314 | -static VALUE Matrix_create_rot_x( VALUE klass, VALUE vangle ) | |
315 | -{ | |
316 | - struct DXRubyMatrix *result; | |
317 | - VALUE vresult; | |
318 | - float angle; | |
319 | - | |
320 | - vresult = Matrix_allocate( cMatrix ); | |
321 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
322 | - | |
323 | - result->x = 4; | |
324 | - result->y = 4; | |
325 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
326 | - result->m11 = 1; | |
327 | - result->m22 = cos( angle ); | |
328 | - result->m23 = sin( angle ); | |
329 | - result->m32 = -result->m23; | |
330 | - result->m33 = result->m22; | |
331 | - result->m44 = 1; | |
332 | - | |
333 | - return vresult; | |
334 | -} | |
335 | -/* y軸回転行列作成 */ | |
336 | -static VALUE Matrix_create_rot_y( VALUE klass, VALUE vangle ) | |
337 | -{ | |
338 | - struct DXRubyMatrix *result; | |
339 | - VALUE vresult; | |
340 | - float angle; | |
341 | - | |
342 | - vresult = Matrix_allocate( cMatrix ); | |
343 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
344 | - | |
345 | - result->x = 4; | |
346 | - result->y = 4; | |
347 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
348 | - result->m11 = cos( angle ); | |
349 | - result->m13 = -sin( angle ); | |
350 | - result->m22 = 1; | |
351 | - result->m31 = -result->m13; | |
352 | - result->m33 = result->m11; | |
353 | - result->m44 = 1; | |
354 | - | |
355 | - return vresult; | |
356 | -} | |
357 | -/* z軸回転行列作成 */ | |
358 | -static VALUE Matrix_create_rot_z( VALUE klass, VALUE vangle ) | |
359 | -{ | |
360 | - struct DXRubyMatrix *result; | |
361 | - VALUE vresult; | |
362 | - float angle; | |
363 | - | |
364 | - vresult = Matrix_allocate( cMatrix ); | |
365 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
366 | - | |
367 | - result->x = 4; | |
368 | - result->y = 4; | |
369 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
370 | - result->m11 = cos( angle ); | |
371 | - result->m12 = sin( angle ); | |
372 | - result->m21 = -result->m12; | |
373 | - result->m22 = result->m11; | |
374 | - result->m33 = 1; | |
375 | - result->m44 = 1; | |
376 | - | |
377 | - return vresult; | |
378 | -} | |
379 | -/* 平行移動行列作成 */ | |
380 | -static VALUE Matrix_create_trans( int argc, VALUE *argv, VALUE self ) | |
381 | -{ | |
382 | - struct DXRubyMatrix *result; | |
383 | - VALUE vresult; | |
384 | - | |
385 | - if( argc < 1 || argc > 3 ) rb_raise( eDXRubyError, "引数の数が正しくありません。 - Matrix_create_trans"); | |
386 | - | |
387 | - vresult = Matrix_allocate( cMatrix ); | |
388 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
389 | - | |
390 | - if( argc == 1 ) | |
391 | - { | |
392 | - result->x = 2; | |
393 | - result->y = 2; | |
394 | - result->m11 = 1; | |
395 | - result->m21 = NUM2FLOAT( argv[0] ); | |
396 | - result->m22 = 1; | |
397 | - } | |
398 | - else if( argc == 2 ) | |
399 | - { | |
400 | - result->x = 3; | |
401 | - result->y = 3; | |
402 | - result->m11 = 1; | |
403 | - result->m22 = 1; | |
404 | - result->m31 = NUM2FLOAT( argv[0] ); | |
405 | - result->m32 = NUM2FLOAT( argv[1] ); | |
406 | - result->m33 = 1; | |
407 | - } | |
408 | - else if( argc == 3) | |
409 | - { | |
410 | - result->x = 4; | |
411 | - result->y = 4; | |
412 | - result->m11 = 1; | |
413 | - result->m22 = 1; | |
414 | - result->m33 = 1; | |
415 | - result->m41 = NUM2FLOAT( argv[0] ); | |
416 | - result->m42 = NUM2FLOAT( argv[1] ); | |
417 | - result->m43 = NUM2FLOAT( argv[2] ); | |
418 | - result->m44 = 1; | |
419 | - } | |
420 | - | |
421 | - return vresult; | |
422 | -} | |
423 | -/* スケーリング行列作成 */ | |
424 | -static VALUE Matrix_create_scale( int argc, VALUE *argv, VALUE self ) | |
425 | -{ | |
426 | - struct DXRubyMatrix *result; | |
427 | - VALUE vresult; | |
428 | - | |
429 | - if( argc < 1 || argc > 3 ) rb_raise( eDXRubyError, "引数の数が正しくありません。 - Matrix_create_scale"); | |
430 | - | |
431 | - vresult = Matrix_allocate( cMatrix ); | |
432 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
433 | - | |
434 | - if( argc == 1 ) | |
435 | - { | |
436 | - result->x = 2; | |
437 | - result->y = 2; | |
438 | - result->m11 = NUM2FLOAT( argv[0] ); | |
439 | - result->m22 = 1; | |
440 | - } | |
441 | - else if( argc == 2 ) | |
442 | - { | |
443 | - result->x = 3; | |
444 | - result->y = 3; | |
445 | - result->m11 = NUM2FLOAT( argv[0] ); | |
446 | - result->m22 = NUM2FLOAT( argv[1] ); | |
447 | - result->m33 = 1; | |
448 | - } | |
449 | - else if( argc == 3) | |
450 | - { | |
451 | - result->x = 4; | |
452 | - result->y = 4; | |
453 | - result->m11 = NUM2FLOAT( argv[0] ); | |
454 | - result->m22 = NUM2FLOAT( argv[1] ); | |
455 | - result->m33 = NUM2FLOAT( argv[2] ); | |
456 | - result->m44 = 1; | |
457 | - } | |
458 | - | |
459 | - return vresult; | |
460 | -} | |
461 | - | |
462 | -/* 配列化 */ | |
463 | -static VALUE Matrix_to_a( VALUE self ) | |
464 | -{ | |
465 | - struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self ); | |
466 | - VALUE vresult; | |
467 | - int i, j; | |
468 | - | |
469 | - vresult = rb_ary_new(); | |
470 | - for( i = 0; i < mat->y; i++ ) | |
471 | - { | |
472 | - for( j = 0; j < mat->x; j++ ) | |
473 | - { | |
474 | - rb_ary_push( vresult, rb_float_new( mat->m[i][j] ) ); | |
475 | - } | |
476 | - } | |
477 | - return vresult; | |
478 | -} | |
479 | - | |
480 | -/* 逆行列作成 */ | |
481 | -static VALUE Matrix_inverse( VALUE self ) | |
482 | -{ | |
483 | - struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self ); | |
484 | - struct DXRubyMatrix *result; | |
485 | - VALUE vresult; | |
486 | - | |
487 | - vresult = Matrix_allocate( cMatrix ); | |
488 | - result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
489 | - result->x = result->y = 4; | |
490 | - D3DXMatrixInverse( (D3DMATRIX *)&result->m, 0, (D3DMATRIX *)&mat->m); | |
491 | - | |
492 | - return vresult; | |
493 | -} | |
494 | - | |
495 | - | |
496 | -/********************************************************************* | |
497 | - * Vectorクラス | |
498 | - * | |
499 | - * ベクトルを表現する。 | |
500 | - *********************************************************************/ | |
501 | - | |
502 | -/*-------------------------------------------------------------------- | |
503 | - 参照されなくなったときにGCから呼ばれる関数 | |
504 | - ---------------------------------------------------------------------*/ | |
505 | -void Vector_release( struct DXRubyVector* vec ) | |
506 | -{ | |
507 | - vec->v1 = 0; | |
508 | - free( vec ); | |
509 | -} | |
510 | - | |
511 | -#ifdef DXRUBY_USE_TYPEDDATA | |
512 | -const rb_data_type_t Vector_data_type = { | |
513 | - "Vector", | |
514 | - { | |
515 | - 0, | |
516 | - Vector_release, | |
517 | - 0, | |
518 | - }, | |
519 | - NULL, NULL | |
520 | -}; | |
521 | -#endif | |
522 | - | |
523 | -/*-------------------------------------------------------------------- | |
524 | - Vectorクラスのallocate。メモリを確保する為にinitialize前に呼ばれる。 | |
525 | - ---------------------------------------------------------------------*/ | |
526 | -VALUE Vector_allocate( VALUE klass ) | |
527 | -{ | |
528 | - VALUE obj; | |
529 | - struct DXRubyVector *vec; | |
530 | - | |
531 | - /* DXRubyVectorのメモリ取得&Vectorオブジェクト生成 */ | |
532 | - vec = malloc( sizeof( struct DXRubyVector ) ); | |
533 | - if( vec == NULL ) rb_raise( eDXRubyError, "メモリの取得に失敗しました - Vector_allocate" ); | |
534 | -#ifdef DXRUBY_USE_TYPEDDATA | |
535 | - obj = TypedData_Wrap_Struct( klass, &Vector_data_type, vec ); | |
536 | -#else | |
537 | - obj = Data_Wrap_Struct( klass, 0, Vector_release, vec ); | |
538 | -#endif | |
539 | - vec->x = 0; | |
540 | - vec->v1 = 0; | |
541 | - vec->v2 = 0; | |
542 | - vec->v3 = 0; | |
543 | - vec->v4 = 0; | |
544 | - | |
545 | - return obj; | |
546 | -} | |
547 | - | |
548 | -/*-------------------------------------------------------------------- | |
549 | - VectorクラスのInitialize | |
550 | - ---------------------------------------------------------------------*/ | |
551 | -static VALUE Vector_initialize( int argc, VALUE *argv, VALUE self ) | |
552 | -{ | |
553 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
554 | - VALUE *ary_p; | |
555 | - int i, count; | |
556 | - | |
557 | - if( argc == 0 ) | |
558 | - { | |
559 | - return self; | |
560 | - } | |
561 | - | |
562 | - if( argc == 1 && TYPE( argv[0] ) == T_ARRAY ) | |
563 | - { | |
564 | - if( RARRAY_LEN( argv[0] ) > 4 || RARRAY_LEN( argv[0] ) < 1 ) rb_raise( eDXRubyError, "配列の要素数が正しくありません。 - Vector_initialize"); | |
565 | - ary_p = RARRAY_PTR( argv[0] ); | |
566 | - count = RARRAY_LEN( argv[0] ); | |
567 | - } | |
568 | - else | |
569 | - { | |
570 | - if( argc > 4 ) rb_raise( eDXRubyError, "引数の数が正しくありません。 - Vector_initialize"); | |
571 | - ary_p = argv; | |
572 | - count = argc; | |
573 | - } | |
574 | - | |
575 | - vec->x = count; | |
576 | - for( i = 0; i < count; i++ ) | |
577 | - { | |
578 | - vec->v[i] = NUM2FLOAT( ary_p[i] ); | |
579 | - } | |
580 | - | |
581 | - return self; | |
582 | -} | |
583 | - | |
584 | -static VALUE Vector_mul( VALUE self, VALUE varg ) | |
585 | -{ | |
586 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
587 | - struct DXRubyVector *result; | |
588 | - VALUE vresult; | |
589 | - vresult = Vector_allocate( cVector ); | |
590 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
591 | - result->x = vec->x; | |
592 | - | |
593 | - if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
594 | - { | |
595 | - int i; | |
596 | - | |
597 | - for( i = 0; i < vec->x; i++ ) | |
598 | - { | |
599 | - result->v[i] = vec->v[i] * NUM2FLOAT( varg ); | |
600 | - } | |
601 | - } | |
602 | - else if( DXRUBY_CHECK( Vector, varg ) ) | |
603 | - { | |
604 | - int i; | |
605 | - struct DXRubyVector* vec_s = DXRUBY_GET_STRUCT( Vector, varg ); | |
606 | - | |
607 | - for( i = 0; i < vec->x; i++ ) | |
608 | - { | |
609 | - result->v[i] = vec->v[i]; | |
610 | - } | |
611 | - | |
612 | - for( i = 0; i < vec->x && i < vec_s->x; i++ ) | |
613 | - { | |
614 | - result->v[i] = vec->v[i] * vec_s->v[i]; | |
615 | - } | |
616 | - } | |
617 | - else if( DXRUBY_CHECK( Matrix, varg ) ) | |
618 | - { | |
619 | - struct DXRubyMatrix *mat; | |
620 | - int i, j; | |
621 | - float temp[4] = {1.0f,1.0f,1.0f,1.0f}; | |
622 | - DXRUBY_CHECK_TYPE( Matrix, varg ); | |
623 | - mat = DXRUBY_GET_STRUCT( Matrix, varg ); | |
624 | - | |
625 | - if( vec->x != mat->y && vec->x != mat->y - 1 ) rb_raise( eDXRubyError, "要素数が一致していません。 - Vector_mul"); | |
626 | - for( i = 0; i < vec->x; i++ ) | |
627 | - { | |
628 | - temp[i] = vec->v[i]; | |
629 | - } | |
630 | - | |
631 | - for( i = 0; i < mat->x; i++ ) | |
632 | - { | |
633 | - for( j = 0; j < mat->y; j++) | |
634 | - { | |
635 | - result->v[i] += temp[j] * mat->m[j][i]; | |
636 | - } | |
637 | - } | |
638 | - | |
639 | - for( i = result->x; i < 4; i++) | |
640 | - { | |
641 | - result->v[i] = 0.0f; | |
642 | - } | |
643 | - } | |
644 | - else | |
645 | - { | |
646 | - rb_raise( eDXRubyError, "引数が異常です - Vector_mul"); | |
647 | - } | |
648 | - | |
649 | - return vresult; | |
650 | -} | |
651 | - | |
652 | -static VALUE Vector_add( VALUE self, VALUE varg ) | |
653 | -{ | |
654 | - struct DXRubyVector *vec_d = DXRUBY_GET_STRUCT( Vector, self ); | |
655 | - struct DXRubyVector *vec_s; | |
656 | - struct DXRubyVector *result; | |
657 | - VALUE vresult; | |
658 | - int i; | |
659 | - | |
660 | - vresult = Vector_allocate( cVector ); | |
661 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
662 | - result->x = vec_d->x; | |
663 | - | |
664 | - if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
665 | - { | |
666 | - for( i = 0; i < vec_d->x; i++ ) | |
667 | - { | |
668 | - result->v[i] = vec_d->v[i] + NUM2FLOAT( varg ); | |
669 | - } | |
670 | - } | |
671 | - else | |
672 | - { | |
673 | - DXRUBY_CHECK_TYPE( Vector, varg ); | |
674 | - vec_s = DXRUBY_GET_STRUCT( Vector, varg ); | |
675 | - | |
676 | - for( i = 0; i < vec_d->x; i++ ) | |
677 | - { | |
678 | - result->v[i] = vec_d->v[i]; | |
679 | - } | |
680 | - | |
681 | - for( i = 0; i < vec_d->x && i < vec_s->x; i++ ) | |
682 | - { | |
683 | - result->v[i] = vec_d->v[i] + vec_s->v[i]; | |
684 | - } | |
685 | - } | |
686 | - | |
687 | - return vresult; | |
688 | -} | |
689 | - | |
690 | -static VALUE Vector_sub( VALUE self, VALUE varg ) | |
691 | -{ | |
692 | - struct DXRubyVector *vec_d = DXRUBY_GET_STRUCT( Vector, self ); | |
693 | - struct DXRubyVector *vec_s; | |
694 | - struct DXRubyVector *result; | |
695 | - VALUE vresult; | |
696 | - int i; | |
697 | - | |
698 | - vresult = Vector_allocate( cVector ); | |
699 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
700 | - result->x = vec_d->x; | |
701 | - | |
702 | - if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
703 | - { | |
704 | - for( i = 0; i < vec_d->x; i++ ) | |
705 | - { | |
706 | - result->v[i] = vec_d->v[i] - NUM2FLOAT( varg ); | |
707 | - } | |
708 | - } | |
709 | - else | |
710 | - { | |
711 | - DXRUBY_CHECK_TYPE( Vector, varg ); | |
712 | - vec_s = DXRUBY_GET_STRUCT( Vector, varg ); | |
713 | - | |
714 | - for( i = 0; i < vec_d->x; i++ ) | |
715 | - { | |
716 | - result->v[i] = vec_d->v[i]; | |
717 | - } | |
718 | - | |
719 | - for( i = 0; i < vec_d->x && i < vec_s->x; i++ ) | |
720 | - { | |
721 | - result->v[i] = vec_d->v[i] - vec_s->v[i]; | |
722 | - } | |
723 | - } | |
724 | - | |
725 | - return vresult; | |
726 | -} | |
727 | - | |
728 | -static VALUE Vector_div( VALUE self, VALUE varg ) | |
729 | -{ | |
730 | - struct DXRubyVector *vec_d = DXRUBY_GET_STRUCT( Vector, self ); | |
731 | - struct DXRubyVector *vec_s; | |
732 | - struct DXRubyVector *result; | |
733 | - VALUE vresult; | |
734 | - int i; | |
735 | - | |
736 | - vresult = Vector_allocate( cVector ); | |
737 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
738 | - result->x = vec_d->x; | |
739 | - | |
740 | - if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
741 | - { | |
742 | - for( i = 0; i < vec_d->x; i++ ) | |
743 | - { | |
744 | - result->v[i] = vec_d->v[i] / NUM2FLOAT( varg ); | |
745 | - } | |
746 | - } | |
747 | - else | |
748 | - { | |
749 | - DXRUBY_CHECK_TYPE( Vector, varg ); | |
750 | - vec_s = DXRUBY_GET_STRUCT( Vector, varg ); | |
751 | - | |
752 | - for( i = 0; i < vec_d->x; i++ ) | |
753 | - { | |
754 | - result->v[i] = vec_d->v[i]; | |
755 | - } | |
756 | - | |
757 | - for( i = 0; i < vec_d->x && i < vec_s->x; i++ ) | |
758 | - { | |
759 | - result->v[i] = vec_d->v[i] / vec_s->v[i]; | |
760 | - } | |
761 | - } | |
762 | - | |
763 | - return vresult; | |
764 | -} | |
765 | - | |
766 | -static VALUE Vector_minus( VALUE self ) | |
767 | -{ | |
768 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
769 | - struct DXRubyVector *result; | |
770 | - VALUE vresult; | |
771 | - int i; | |
772 | - | |
773 | - vresult = Vector_allocate( cVector ); | |
774 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
775 | - result->x = vec->x; | |
776 | - | |
777 | - for( i = 0; i < vec->x; i++ ) | |
778 | - { | |
779 | - result->v[i] = -vec->v[i]; | |
780 | - } | |
781 | - | |
782 | - return vresult; | |
783 | -} | |
784 | - | |
785 | -static VALUE Vector_normalize( VALUE self ) | |
786 | -{ | |
787 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
788 | - struct DXRubyVector *result; | |
789 | - VALUE vresult; | |
790 | - float magsq = vec->v1*vec->v1 + vec->v2*vec->v2 + vec->v3*vec->v3 + vec->v4*vec->v4; | |
791 | - int i; | |
792 | - | |
793 | - vresult = Vector_allocate( cVector ); | |
794 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
795 | - result->x = vec->x; | |
796 | - | |
797 | - if( magsq > 0.0f ) | |
798 | - { | |
799 | - float temp = 1.0f / sqrt( magsq ); | |
800 | - int i; | |
801 | - for( i = 0; i < vec->x; i++ ) | |
802 | - { | |
803 | - result->v[i] = temp * vec->v[i]; | |
804 | - } | |
805 | - } | |
806 | - | |
807 | - return vresult; | |
808 | -} | |
809 | - | |
810 | -static VALUE Vector_distance( VALUE klass, VALUE vvec1, VALUE vvec2 ) | |
811 | -{ | |
812 | - struct DXRubyVector *vec1; | |
813 | - struct DXRubyVector *vec2; | |
814 | - float dx, dy, dz, dw; | |
815 | - | |
816 | - DXRUBY_CHECK_TYPE( Vector, vvec1 ); | |
817 | - DXRUBY_CHECK_TYPE( Vector, vvec2 ); | |
818 | - vec1 = DXRUBY_GET_STRUCT( Vector, vvec1 ); | |
819 | - vec2 = DXRUBY_GET_STRUCT( Vector, vvec2 ); | |
820 | - | |
821 | - dx = vec1->v1 - vec2->v1; | |
822 | - dy = vec1->v2 - vec2->v2; | |
823 | - dz = vec1->v3 - vec2->v3; | |
824 | - dw = vec1->v4 - vec2->v4; | |
825 | - return rb_float_new( sqrt( dx*dx + dy*dy + dz*dz + dw*dw ) ); | |
826 | -} | |
827 | - | |
828 | -static VALUE Vector_cross_product( VALUE klass, VALUE vvec1, VALUE vvec2 ) | |
829 | -{ | |
830 | - struct DXRubyVector *vec1; | |
831 | - struct DXRubyVector *vec2; | |
832 | - struct DXRubyVector *result; | |
833 | - VALUE vresult; | |
834 | - | |
835 | - DXRUBY_CHECK_TYPE( Vector, vvec1 ); | |
836 | - DXRUBY_CHECK_TYPE( Vector, vvec2 ); | |
837 | - vec1 = DXRUBY_GET_STRUCT( Vector, vvec1 ); | |
838 | - vec2 = DXRUBY_GET_STRUCT( Vector, vvec2 ); | |
839 | - | |
840 | - vresult = Vector_allocate( cVector ); | |
841 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
842 | - result->x = 3; | |
843 | - | |
844 | - result->v1 = vec1->v2*vec2->v3 - vec1->v3*vec2->v2; | |
845 | - result->v2 = vec1->v3*vec2->v1 - vec1->v1*vec2->v3; | |
846 | - result->v3 = vec1->v1*vec2->v2 - vec1->v2*vec2->v1; | |
847 | - | |
848 | - return vresult; | |
849 | -} | |
850 | - | |
851 | -static VALUE Vector_dot_product( VALUE klass, VALUE vvec1, VALUE vvec2 ) | |
852 | -{ | |
853 | - struct DXRubyVector *vec1; | |
854 | - struct DXRubyVector *vec2; | |
855 | - DXRUBY_CHECK_TYPE( Vector, vvec1 ); | |
856 | - DXRUBY_CHECK_TYPE( Vector, vvec2 ); | |
857 | - vec1 = DXRUBY_GET_STRUCT( Vector, vvec1 ); | |
858 | - vec2 = DXRUBY_GET_STRUCT( Vector, vvec2 ); | |
859 | - return rb_float_new( vec1->v1 * vec2->v1 + vec1->v2 * vec2->v2 + vec1->v3 * vec2->v3 + vec1->v4 * vec2->v4 ); | |
860 | -} | |
861 | - | |
862 | -static VALUE Vector_to_s( VALUE self ) | |
863 | -{ | |
864 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
865 | - char buf[1024]; | |
866 | - switch( vec->x ) { | |
867 | - case 1: | |
868 | - sprintf( buf, "size = %d (%f)", vec->x, vec->v1 ); | |
869 | - break; | |
870 | - case 2: | |
871 | - sprintf( buf, "size = %d (%f, %f)", vec->x, vec->v1, vec->v2 ); | |
872 | - break; | |
873 | - case 3: | |
874 | - sprintf( buf, "size = %d (%f, %f, %f)", vec->x, vec->v1, vec->v2, vec->v3 ); | |
875 | - break; | |
876 | - default: | |
877 | - sprintf( buf, "size = %d (%f, %f, %f, %f)", vec->x, vec->v1, vec->v2, vec->v3, vec->v4 ); | |
878 | - break; | |
879 | - } | |
880 | - return rb_str_new2( buf ); | |
881 | -} | |
882 | - | |
883 | -static VALUE Vector_get_x( VALUE self ) | |
884 | -{ | |
885 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
886 | - return rb_float_new(vec->v1); | |
887 | -} | |
888 | - | |
889 | -static VALUE Vector_get_y( VALUE self ) | |
890 | -{ | |
891 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
892 | - return rb_float_new(vec->v2); | |
893 | -} | |
894 | - | |
895 | -static VALUE Vector_get_z( VALUE self ) | |
896 | -{ | |
897 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
898 | - return rb_float_new(vec->v3); | |
899 | -} | |
900 | - | |
901 | -static VALUE Vector_get_w( VALUE self ) | |
902 | -{ | |
903 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
904 | - return rb_float_new(vec->v4); | |
905 | -} | |
906 | - | |
907 | -static VALUE Vector_get_size( VALUE self ) | |
908 | -{ | |
909 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
910 | - return rb_float_new(vec->x); | |
911 | -} | |
912 | - | |
913 | -static VALUE Vector_get_xy( VALUE self ) | |
914 | -{ | |
915 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
916 | - struct DXRubyVector *result; | |
917 | - VALUE vresult; | |
918 | - | |
919 | - vresult = Vector_allocate( cVector ); | |
920 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
921 | - result->x = 2; | |
922 | - | |
923 | - result->v1 = vec->v1; | |
924 | - result->v2 = vec->v2; | |
925 | - | |
926 | - return vresult; | |
927 | -} | |
928 | - | |
929 | -static VALUE Vector_get_xyz( VALUE self ) | |
930 | -{ | |
931 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
932 | - struct DXRubyVector *result; | |
933 | - VALUE vresult; | |
934 | - | |
935 | - vresult = Vector_allocate( cVector ); | |
936 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
937 | - result->x = 3; | |
938 | - | |
939 | - result->v1 = vec->v1; | |
940 | - result->v2 = vec->v2; | |
941 | - result->v3 = vec->v3; | |
942 | - | |
943 | - return vresult; | |
944 | -} | |
945 | - | |
946 | -static VALUE Vector_equal( VALUE self, VALUE vvector ) | |
947 | -{ | |
948 | - struct DXRubyVector *vec_d = DXRUBY_GET_STRUCT( Vector, self ); | |
949 | - struct DXRubyVector *vec_s; | |
950 | - int i; | |
951 | - | |
952 | - DXRUBY_CHECK_TYPE( Vector, vvector ); | |
953 | - vec_s = DXRUBY_GET_STRUCT( Vector, vvector ); | |
954 | - | |
955 | - if( vec_d->x != vec_s->x ) | |
956 | - { | |
957 | - return Qfalse; | |
958 | - } | |
959 | - for( i = 0; i < vec_d->x; i++ ) | |
960 | - { | |
961 | - if( vec_d->v[i] != vec_s->v[i] ) | |
962 | - { | |
963 | - return Qfalse; | |
964 | - } | |
965 | - } | |
966 | - | |
967 | - return Qtrue; | |
968 | -} | |
969 | - | |
970 | -static VALUE Vector_translate( int argc, VALUE *argv, VALUE self ) | |
971 | -{ | |
972 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
973 | - struct DXRubyVector *result; | |
974 | - VALUE vresult; | |
975 | - int i; | |
976 | - | |
977 | - if( argc < 1 || argc > 4 ) rb_raise( eDXRubyError, "引数の数が正しくありません。 - Vector_translate"); | |
978 | - vresult = Vector_allocate( cVector ); | |
979 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
980 | - result->x = vec->x; | |
981 | - | |
982 | - for( i = 0; i < vec->x; i++ ) | |
983 | - { | |
984 | - result->v[i] = vec->v[i]; | |
985 | - } | |
986 | - | |
987 | - for( i = 0; i < vec->x && i < argc; i++ ) | |
988 | - { | |
989 | - result->v[i] = vec->v[i] + NUM2FLOAT( argv[i] ); | |
990 | - } | |
991 | - | |
992 | - return vresult; | |
993 | -} | |
994 | - | |
995 | -static VALUE Vector_rotate( int argc, VALUE *argv, VALUE obj ) | |
996 | -{ | |
997 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, obj ); | |
998 | - struct DXRubyVector *center = NULL; | |
999 | - struct DXRubyVector *result; | |
1000 | - VALUE vresult, vangle, vcenter; | |
1001 | - float angle, x, y; | |
1002 | - | |
1003 | - if( vec->x != 2 && vec->x != 3 ) rb_raise( eDXRubyError, "2D回転できるVectorではありません - Vector_rotate"); | |
1004 | - | |
1005 | - rb_scan_args( argc, argv, "11", &vangle, &vcenter ); | |
1006 | - | |
1007 | - if( vcenter != Qnil ) | |
1008 | - { | |
1009 | - center = DXRUBY_GET_STRUCT( Vector, vcenter ); | |
1010 | - if( center->x != 2 && center->x != 3 ) rb_raise( eDXRubyError, "回転中心に設定できるVectorではありません - Vector_rotate"); | |
1011 | - } | |
1012 | - | |
1013 | - vresult = Vector_allocate( cVector ); | |
1014 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
1015 | - result->x = vec->x; | |
1016 | - x = vec->v1; | |
1017 | - y = vec->v2; | |
1018 | - | |
1019 | - if( center ) | |
1020 | - { | |
1021 | - x -= center->v1; | |
1022 | - y -= center->v2; | |
1023 | - } | |
1024 | - | |
1025 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
1026 | - x = cos( angle ) * x - sin( angle ) * y; | |
1027 | - y = sin( angle ) * x + cos( angle ) * y; | |
1028 | - | |
1029 | - if( center ) | |
1030 | - { | |
1031 | - x += center->v1; | |
1032 | - y += center->v2; | |
1033 | - } | |
1034 | - | |
1035 | - result->v1 = x; | |
1036 | - result->v2 = y; | |
1037 | - result->v3 = vec->v3; | |
1038 | - | |
1039 | - return vresult; | |
1040 | -} | |
1041 | - | |
1042 | -static VALUE Vector_rotate_x( VALUE self, VALUE vangle ) | |
1043 | -{ | |
1044 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
1045 | - struct DXRubyVector *result; | |
1046 | - VALUE vresult; | |
1047 | - float angle; | |
1048 | - | |
1049 | - if( vec->x != 3 && vec->x != 4 ) rb_raise( eDXRubyError, "3D回転できるVectorではありません - Vector_rotate_x"); | |
1050 | - vresult = Vector_allocate( cVector ); | |
1051 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
1052 | - result->x = vec->x; | |
1053 | - | |
1054 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
1055 | - result->v1 = vec->v1; | |
1056 | - result->v2 = -sin( angle ) * vec->v2 + cos( angle ) * vec->v3; | |
1057 | - result->v3 = cos( angle ) * vec->v2 + sin( angle ) * vec->v3; | |
1058 | - result->v4 = vec->v4; | |
1059 | - | |
1060 | - return vresult; | |
1061 | -} | |
1062 | - | |
1063 | -static VALUE Vector_rotate_y( VALUE self, VALUE vangle ) | |
1064 | -{ | |
1065 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
1066 | - struct DXRubyVector *result; | |
1067 | - VALUE vresult; | |
1068 | - float angle; | |
1069 | - | |
1070 | - if( vec->x != 3 && vec->x != 4 ) rb_raise( eDXRubyError, "3D回転できるVectorではありません - Vector_rotate_x"); | |
1071 | - vresult = Vector_allocate( cVector ); | |
1072 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
1073 | - result->x = vec->x; | |
1074 | - | |
1075 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
1076 | - result->v1 = sin( angle ) * vec->v1 + cos( angle ) * vec->v3; | |
1077 | - result->v2 = vec->v2; | |
1078 | - result->v3 = cos( angle ) * vec->v1 - sin( angle ) * vec->v3; | |
1079 | - result->v4 = vec->v4; | |
1080 | - | |
1081 | - return vresult; | |
1082 | -} | |
1083 | - | |
1084 | -static VALUE Vector_rotate_z( VALUE self, VALUE vangle ) | |
1085 | -{ | |
1086 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
1087 | - struct DXRubyVector *result; | |
1088 | - VALUE vresult; | |
1089 | - float angle; | |
1090 | - | |
1091 | - if( vec->x != 3 && vec->x != 4 ) rb_raise( eDXRubyError, "3D回転できるVectorではありません - Vector_rotate_x"); | |
1092 | - vresult = Vector_allocate( cVector ); | |
1093 | - result = DXRUBY_GET_STRUCT( Vector, vresult ); | |
1094 | - result->x = vec->x; | |
1095 | - | |
1096 | - angle = pi / 180.0f * NUM2FLOAT( vangle ); | |
1097 | - result->v1 = cos( angle ) * vec->v1 - sin( angle ) * vec->v2; | |
1098 | - result->v2 = sin( angle ) * vec->v1 + cos( angle ) * vec->v2; | |
1099 | - result->v3 = vec->v3; | |
1100 | - result->v4 = vec->v4; | |
1101 | - | |
1102 | - return vresult; | |
1103 | -} | |
1104 | - | |
1105 | -/* 配列化 */ | |
1106 | -static VALUE Vector_to_a( VALUE self ) | |
1107 | -{ | |
1108 | - struct DXRubyVector *vec = DXRUBY_GET_STRUCT( Vector, self ); | |
1109 | - VALUE vresult; | |
1110 | - int i; | |
1111 | - | |
1112 | - vresult = rb_ary_new(); | |
1113 | - for( i = 0; i < vec->x; i++ ) | |
1114 | - { | |
1115 | - rb_ary_push( vresult, rb_float_new( vec->v[i] ) ); | |
1116 | - } | |
1117 | - return vresult; | |
1118 | -} | |
1119 | - | |
1120 | - | |
1121 | -static VALUE Vector_angle_to( VALUE self, VALUE vvector ) | |
1122 | -{ | |
1123 | - struct DXRubyVector *vec_d = DXRUBY_GET_STRUCT( Vector, self ); | |
1124 | - struct DXRubyVector *vec_s; | |
1125 | - float angle; | |
1126 | - | |
1127 | - DXRUBY_CHECK_TYPE( Vector, vvector ); | |
1128 | - vec_s = DXRUBY_GET_STRUCT( Vector, vvector ); | |
1129 | - | |
1130 | - angle = atan2(vec_s->v2 - vec_d->v2, vec_s->v1 - vec_d->v1) / pi * 180; | |
1131 | - | |
1132 | - return rb_float_new(angle); | |
1133 | -} | |
1134 | - | |
1135 | - | |
1136 | -/* | |
1137 | -*************************************************************** | |
1138 | -* | |
1139 | -* Global functions | |
1140 | -* | |
1141 | -***************************************************************/ | |
1142 | - | |
1143 | -void Init_dxruby_Matrix() | |
1144 | -{ | |
1145 | - | |
1146 | - /* Matrixクラス定義 */ | |
1147 | - cMatrix = rb_define_class_under( mDXRuby, "Matrix", rb_cObject ); | |
1148 | - | |
1149 | - /* Matrixクラスにクラスメソッド登録*/ | |
1150 | - rb_define_singleton_method( cMatrix, "look_at", Matrix_look_at, 3 ); | |
1151 | - rb_define_singleton_method( cMatrix, "lookAt", Matrix_look_at, 3 ); | |
1152 | - rb_define_singleton_method( cMatrix, "projection", Matrix_create_projection, 4 ); | |
1153 | - rb_define_singleton_method( cMatrix, "projection_fov", Matrix_create_projection_fov, 4 ); | |
1154 | - rb_define_singleton_method( cMatrix, "projectionFov", Matrix_create_projection_fov, 4 ); | |
1155 | - rb_define_singleton_method( cMatrix, "projection_ortho", Matrix_create_projection_ortho, 4 ); | |
1156 | - rb_define_singleton_method( cMatrix, "projectionOrtho", Matrix_create_projection_ortho, 4 ); | |
1157 | - rb_define_singleton_method( cMatrix, "rotation", Matrix_create_rot, 1 ); | |
1158 | - rb_define_singleton_method( cMatrix, "rotation_x", Matrix_create_rot_x, 1 ); | |
1159 | - rb_define_singleton_method( cMatrix, "rotationX", Matrix_create_rot_x, 1 ); | |
1160 | - rb_define_singleton_method( cMatrix, "rotation_y", Matrix_create_rot_y, 1 ); | |
1161 | - rb_define_singleton_method( cMatrix, "rotationY", Matrix_create_rot_y, 1 ); | |
1162 | - rb_define_singleton_method( cMatrix, "rotation_z", Matrix_create_rot_z, 1 ); | |
1163 | - rb_define_singleton_method( cMatrix, "rotationZ", Matrix_create_rot_z, 1 ); | |
1164 | - rb_define_singleton_method( cMatrix, "scaling", Matrix_create_scale, -1 ); | |
1165 | - rb_define_singleton_method( cMatrix, "translation", Matrix_create_trans, -1 ); | |
1166 | - | |
1167 | - /* Matrixクラスにインスタンスメソッド登録*/ | |
1168 | - rb_define_private_method( cMatrix, "initialize", Matrix_initialize, -1 ); | |
1169 | - rb_define_method( cMatrix, "*", Matrix_mul, 1 ); | |
1170 | - rb_define_method( cMatrix, "to_s", Matrix_to_s, 0 ); | |
1171 | - rb_define_method( cMatrix, "to_a", Matrix_to_a, 0 ); | |
1172 | - rb_define_method( cMatrix, "inverse", Matrix_inverse, 0 ); | |
1173 | - | |
1174 | - /* Matrixオブジェクトを生成した時にinitializeの前に呼ばれるメモリ割り当て関数登録 */ | |
1175 | - rb_define_alloc_func( cMatrix, Matrix_allocate ); | |
1176 | - | |
1177 | - | |
1178 | - /* Vectorクラス定義 */ | |
1179 | - cVector = rb_define_class_under( mDXRuby, "Vector", rb_cObject ); | |
1180 | - | |
1181 | - /* Vectorクラスにクラスメソッド登録*/ | |
1182 | - rb_define_singleton_method( cVector, "distance", Vector_distance, 2 ); | |
1183 | - rb_define_singleton_method( cVector, "cross_product", Vector_cross_product, 2 ); | |
1184 | - rb_define_singleton_method( cVector, "crossProduct", Vector_cross_product, 2 ); | |
1185 | - rb_define_singleton_method( cVector, "dot_product", Vector_dot_product, 2 ); | |
1186 | - rb_define_singleton_method( cVector, "dotProduct", Vector_dot_product, 2 ); | |
1187 | - | |
1188 | - /* Vectorクラスにインスタンスメソッド登録*/ | |
1189 | - rb_define_private_method( cVector, "initialize", Vector_initialize, -1 ); | |
1190 | - rb_define_method( cVector, "*", Vector_mul, 1 ); | |
1191 | - rb_define_method( cVector, "+", Vector_add, 1 ); | |
1192 | - rb_define_method( cVector, "-", Vector_sub, 1 ); | |
1193 | - rb_define_method( cVector, "-@", Vector_minus, 0 ); | |
1194 | - rb_define_method( cVector, "/", Vector_div, 1 ); | |
1195 | - rb_define_method( cVector, "to_s", Vector_to_s, 0 ); | |
1196 | - rb_define_method( cVector, "to_a", Vector_to_a, 0 ); | |
1197 | - rb_define_method( cVector, "x", Vector_get_x, 0 ); | |
1198 | - rb_define_method( cVector, "y", Vector_get_y, 0 ); | |
1199 | - rb_define_method( cVector, "z", Vector_get_z, 0 ); | |
1200 | - rb_define_method( cVector, "w", Vector_get_w, 0 ); | |
1201 | - rb_define_method( cVector, "xy", Vector_get_xy, 0 ); | |
1202 | - rb_define_method( cVector, "xyz", Vector_get_xyz, 0 ); | |
1203 | - rb_define_method( cVector, "size", Vector_get_size, 0 ); | |
1204 | - rb_define_method( cVector, "normalize", Vector_normalize, 0 ); | |
1205 | - rb_define_method( cVector, "==", Vector_equal, 1 ); | |
1206 | - rb_define_method( cVector, "translate", Vector_translate, -1 ); | |
1207 | - rb_define_method( cVector, "rotate", Vector_rotate, -1 ); | |
1208 | - rb_define_method( cVector, "rotate_x", Vector_rotate_x, 1 ); | |
1209 | - rb_define_method( cVector, "rotateX", Vector_rotate_x, 1 ); | |
1210 | - rb_define_method( cVector, "rotate_y", Vector_rotate_y, 1 ); | |
1211 | - rb_define_method( cVector, "rotateY", Vector_rotate_y, 1 ); | |
1212 | - rb_define_method( cVector, "rotate_z", Vector_rotate_z, 1 ); | |
1213 | - rb_define_method( cVector, "rotateZ", Vector_rotate_z, 1 ); | |
1214 | - rb_define_method( cVector, "angle_to", Vector_angle_to, 1 ); | |
1215 | - rb_define_method( cVector, "angleTo", Vector_angle_to, 1 ); | |
1216 | - | |
1217 | - /* Vectorオブジェクトを生成した時にinitializeの前に呼ばれるメモリ割り当て関数登録 */ | |
1218 | - rb_define_alloc_func( cVector, Vector_allocate ); | |
1219 | - | |
1220 | -} | |
1221 | - |
@@ -1,10 +1,10 @@ | ||
1 | 1 | /* |
2 | 2 | ################################### |
3 | 3 | # |
4 | -# DXRuby 1.5.20dev | |
4 | +# DXRuby 1.4.3 | |
5 | 5 | # |
6 | 6 | ################################### |
7 | 7 | */ |
8 | -#define DXRUBY_VERSION "1.5.20dev" | |
8 | +#define DXRUBY_VERSION "1.4.3" | |
9 | 9 | |
10 | -#define DXRUBY15 | |
10 | +//#define DXRUBY15 |