# Ruby 3D Math libraries?

**URL:** <https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618>\
**Category:** Ruby\
**Created:** [December 7, 2006, 10:42am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618 "2006-12-07T10:42:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 10:42am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/1 "2006-12-07T10:42:00Z")

</div>

Hello,

Does anyone know good Ruby 3D Math libraries? I found Math3D  
([http://raa.ruby-lang.org/project/math3d/](http://raa.ruby-lang.org/project/math3d/)) but I can not get it  
working,  
ruby extconf.rb fails:  
$ ruby -r mkmf extconf.rb  
checking for atan() in -lm… no  
No atan() in -lm  
\*\*\* extconf.rb failed \*\*\*

This is after I commented the following line, which made the  
configuration  
fail too:  
CFLAGS \<\< " -Wall -Wno-comment"

The Alg3D library is not bad, but it does not contain everything from  
the 3D  
Math library. E.g. rotation is missing, which is something I need.

Regards,  
Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 10:52am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/2 "2006-12-07T10:52:02Z")

</div>

Bart B. wrote:

> Hello,
> 
> Does anyone know good Ruby 3D Math libraries? I found Math3D  
> ([http://raa.ruby-lang.org/project/math3d/](http://raa.ruby-lang.org/project/math3d/)) but I can not get it working,  
> ruby extconf.rb fails:  
> $ ruby -r mkmf extconf.rb  
> checking for atan() in -lm… no  
> No atan() in -lm

For some reason, you have a very limited math library on your system.  
Chances are you will succeed in the compilation if you will replace your  
math library with a complete one.

It is a little difficult to believe there is a math library without  
“atan()”, and it implies there is something basically wrong with the one  
installed on your system.

My point is that the Math3D library is not the problem, it is solely a  
problem with your native-code math library, on which Math3D depends.

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 11:10am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/3 "2006-12-07T11:10:09Z")

</div>

From: “Bart B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> fail too:  
> CFLAGS \<\< " -Wall -Wno-comment"

Are you possibly on Windows? I recall needing to make a few  
tweaks here and there to Math3D to get it to compile on  
Windows.

Here are the changes:

diff -u orig/rb\_math3d.c rb\_math3d.c  
— orig/rb\_math3d.c 2005-02-08 01:10:14.189772800 -0800  
+++ rb\_math3d.c 2005-02-08 02:10:09.669819200 -0800  
@@ -4,8 +4,15 @@

- removed\_email\_address@domain.invalid

* * *

\*/  
+#if defined(\_WIN32)  
+#define \_USE\_MATH\_DEFINES  
+#endif  
#include “ruby.h”  
#include \<math.h\>  
+#if defined(\_WIN32)  
+#undef FAR  
+#undef NEAR  
+#endif  
#include “math3d\_native.h”

Hope this helps,

Bill

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 11:36am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/4 "2006-12-07T11:36:18Z")

</div>

Bill K. wrote:

> Are you possibly on Windows? I recall needing to make a few  
> tweaks here and there to Math3D to get it to compile on  
> Windows.

I’m working on linux, but thanks for the information!

Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 11:46am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/5 "2006-12-07T11:46:20Z")

</div>

Bart B. wrote:

> > My point is that the Math3D library is not the problem, it is solely a  
> > problem with your native-code math library, on which Math3D depends.
> 
> Strange, but I noticed my libm.a is not in my path. I’ll fix that first…

That should change things, but if this problem persists, write a simple,  
short C program that calls “atan()” and that needs to be compiled using  
the  
-lm linker directive (these two don’t necessarily follow from each  
other).  
See what happens. If this fails, it will become obvious where the  
problem  
lies.

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 11:36am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/6 "2006-12-07T11:36:17Z")

</div>

Paul L. wrote:

> For some reason, you have a very limited math library on your system.  
> Chances are you will succeed in the compilation if you will replace your  
> math library with a complete one.
> 
> It is a little difficult to believe there is a math library without  
> “atan()”, and it implies there is something basically wrong with the one  
> installed on your system.
> 
> My point is that the Math3D library is not the problem, it is solely a  
> problem with your native-code math library, on which Math3D depends.

Strange, but I noticed my libm.a is not in my path. I’ll fix that  
first…

Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 11:56am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/7 "2006-12-07T11:56:24Z")

</div>

Paul L. wrote:

> For some reason, you have a very limited math library on your system.  
> Chances are you will succeed in the compilation if you will replace your  
> math library with a complete one.

I removed the check for the function and now the Makefile is perfectly  
generated and make works flawlessly. Make test fails however:

make test  
ruby test.rb  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require__': ./tests/Vector.tests.rb:556: no .<digit> floating literal anymore; put 0 before dot (SyntaxError) expected = vec.to_a.inject(0) {|x,y| x+(y*y)}.**(.5) ^ ./tests/Vector.tests.rb:556: parse error, unexpected '.', expecting ')' expected = vec.to_a.inject(0) {|x,y| x+(y*y)}.**(.5) ^ from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in`require’  
from test.rb:30  
from /usr/lib/ruby/1.8/find.rb:39:in `find' from /usr/lib/ruby/1.8/find.rb:38:in`catch’  
from /usr/lib/ruby/1.8/find.rb:38:in `find’  
from test.rb:20  
make: \*\*\* [test] Error 1

Turning .5 into 0.5 fixed that. Afterwards I got this error:

make test  
ruby test.rb  
./tests/Matrix4.tests.rb:32: warning: default `to_a' will be obsolete ./tests/Bound.tests.rb:30: warning: default`to\_a’ will be obsolete  
./tests/Require.tests.rb:27: warning: parenthesize argument(s) for  
future  
version  
Required 9 files.  
test.rb:45:in `suite': undefined method`add’ for  
#Test::Unit::TestSuite:0x404e0b84 (NoMethodError)  
from test.rb:44:in `each_object' from test.rb:44:in`suite’  
from /usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:27:in  
`initialize' from test.rb:53:in`new’  
from test.rb:53  
make: \*\*\* [test] Error 1

(I’ll ignore the to\_a warnings, those tests work)  
But the suite method add is missing, I replaced it with the \<\< method in  
test.rb:  
ObjectSpace.each\_object( Class ) {|klass|  
suite \<\< klass.suite if klass \<  
Test::Unit::TestCase  
}

Next my tests ran, without good results:

make test  
ruby test.rb  
./tests/Matrix4.tests.rb:32: warning: default `to_a' will be obsolete ./tests/Bound.tests.rb:30: warning: default`to\_a’ will be obsolete  
./tests/Require.tests.rb:27: warning: parenthesize argument(s) for  
future  
version  
Required 9 files.  
Loaded suite Math3d Test Suite  
Started  
EEE.EEEEEE…EEEEEEEEEEE.EEEEEEEEE.EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE.EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE.EEEEEEFFEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  
Finished in 0.207462 seconds.

1. Error:  
test\_00\_7\_contains?(OrthoTestCase):  
NoMethodError: undefined method `each' for nil:NilClass ./tests/Ortho.tests.rb:65:in`test\_00\_7\_contains?’  
./tests/m3dUnitTest.rb:63:in `run’

Somehow the setup methods don’t work? It seems there is something wrong  
with  
the testsuite, but I don’t see what…

Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 7, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/8 "2006-12-07T20:20:30Z")

</div>

Bart B. wrote:

> Paul L. wrote:
> 
> > For some reason, you have a very limited math library on your system.  
> > Chances are you will succeed in the compilation if you will replace your  
> > math library with a complete one.
> 
> I removed the check for the function and now the Makefile is perfectly  
> generated and make works flawlessly.

This is not a very good idea. The resulting library may appear to be  
intact,  
but it may fail at some future point when the missing function is  
required.  
There is a reason the test is present in the configuration procedure, it  
should not be removed.

> Make test fails however:

/ … snip long section of test results

> Somehow the setup methods don’t work? It seems there is something wrong  
> with the testsuite, but I don’t see what…

It’s possible the methods used to force the compilation to finish have  
ruined the library’s functioning. It’s also possible that there is  
something wrong with your system’s math library. But those are minor  
points.

I have been having a friendly debate in this newsgroup about the use of  
libraries. I am in favor of using whatever method solves the problem at  
hand, some others feel that a library, by definition carefully designed  
and  
wrung out, is a better approach. There are good arguments for both sides  
of  
this question.

But if the library is not wrung out or isn’t very portable, or if it  
cannot  
be easily compiled and run, the arguments in favor of using it break  
down.

It turns out that 3D math, that is to say, math involving vectors of at  
most  
three elements, is not very difficult to write. If one executes a lot of  
mathematical statements involving 3D vectors, one may encounter a  
significant speed penalty if the executed code is written in plain Ruby  
code, when compared to using a compiled library such as the one you are  
trying to compile.

But I ask you to consider this. From a user’s perspective, the total  
runtime  
is the total of the preparation time plus the time spent using the  
library.  
I suspect that the total time for making this library work for you will  
soon cross over a threshold beyond which it would have been quicker and  
easier to write, test and use your own 3D vector routines, composed in  
plain Ruby.

Another advantage to writing your own code – oops, I mean your  
own /library/ – is that the resulting /library/ will be understood by  
you,  
it will be composed in the very readable and transparent syntax for  
which  
Ruby is justly famous, and you will be able to change it very quickly  
and  
easily to meet new requirements.

In exchange, it will not be nearly as fast as a compiled, native-code  
library would have been, if the library had ever been successfully  
compiled  
and run.

It’s your time, your requirements, therefore it’s your decision.

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [December 8, 2006, 10:46am UTC](https://www.ruby-forum.com/t/ruby-3d-math-libraries/78618/9 "2006-12-08T10:46:14Z")

</div>

Paul L. wrote:

> Ruby is justly famous, and you will be able to change it very quickly and  
> easily to meet new requirements.
> 
> In exchange, it will not be nearly as fast as a compiled, native-code  
> library would have been, if the library had ever been successfully  
> compiled and run.

I understand your arguments but I’m looking at this from different  
perspectives.

As a programmer I do not mind fixing this to learn more about Ruby  
libraries  
and have a better understanding on how to write and use them. I got this  
one fixed (almost, see my most recent post) so I don’t mind. Of course  
when  
it would not have worked out soon enough I would have left it.

Another argument pro is that this library is tested. When writing this  
myself I am bound to make errors. Here there are a number of tests so  
that’s less probable.

A last and most important argument is that I need the performance. I am  
going to write large programs where lots of 3D calculations are  
necessary.  
Then I need the C performance, but preferably hidden away in a nice Ruby  
library.

(So my decision is to use the library, as I have it working.)

Regards,  
Bart
