Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, July 9, 2013

Blender: Find the longest edge in a mesh

I had to write this simple script for my thesis on acoustic simulations on a 3D model of a human head (HRTF stuff).

So this script iterates over all the selected edges of the current object and show the longest one's length (you have to look at the console to view the message).
It also writes the maximum frequency that can be calculated using the BEM according to a paper by Brian Katz:
$$f_{max} = \frac{c}{6 \times  edge_{max}}$$


import bpy

sound_speed = 343.0

obj = bpy.context.active_object
edges = obj.data.edges
vertices = obj.data.vertices
matrix = obj.matrix_world.copy()

mm = 0.0
for ee in edges:
    vv = ee.vertices
    v1 = matrix * vertices[ee.vertices[0]].co
    v2 = matrix * vertices[ee.vertices[1]].co
    mm = max((v1-v2).length, mm)

max_freq = sound_speed / (6 * mm)

print("Longest edge's length: %f"%mm)
print("fmax: %f"%max_freq)

Friday, July 24, 2009

Experimenting with haXe

So, I found on the web this nice language that can compile to flash/javascript/php and other bytecodes (even to pure binary) and I decided to give it a try.

And, also, some time ago I started to wonder if I could make a Facebook app (a game).
I started to think to something easy to make and funny to play and then a little flash game on a site with some simple tutorials reminded me of a stupid game I wrote in C at school using only OpenGL's rectangle and triangle.

I spent something like 3 hours trying to code a GameMaker-like engine in flash, but I wasn't able to figure out how the flash drawing system works.
Tired, I pointed my browser to Google and found this nice tutorial on making a 2D game with haXe. Its approach to bitmap blitting is odd: it creates a BitmapData object for the screen, then it creates a Bitmap from it and adds it to the root (flash.Lib.current).

I adapted some of the tutorial code to my engine and now I have some rectangle moving on the screen :). The next step will be to add support for animating sprite and then I can start the funny part: programming the gameplay.

Here is the demo (only 6Kb, you should understand why it is so small xD):

Thursday, June 25, 2009

New release of KlonDuke

New release of my tool to edit iPod Klondike savefiles!

Well, actually it is not a new release, I just rewrote the old code using Qt4 instead of FLTK.
Advantages? Easier to compile, solves some issue and looks nicer :).

You can download the linux binary or the source code from emptylabs @ Google Code (it is name v0.2).

Some screenshots (before, after, about):


P.S.: If someone manages to compile it under MSVC or XCode I'll be happy to link it here :)

Friday, March 13, 2009

Python CGI Debugging howto: Part 2

Ok, in the previous post I wrote that probably there was a better way to display errors for cgi script in python, so, here it is:

import cgitb
cgitb.enable() #This enable it all


Two lines :)

Simply put them at the beginning of a script and they do all the work. It's also possible to output errors in a log file instead of displaying them in the browser of the user (just take a look at python documentation on cgitb.enable()), a far better solution for production environment.

Python CGI Debugging howto

I found this useful when you are trying to debug a python script running on a remote server (that has a previous version of python, in my case :( ).

To show the classic interpreter error messages just do this:

import sys
sys.stderr = sys.stdout

print "Content-type: text/html\n"


In this way, we set the errors to output text on the standard output (in other words, we redirect the errors to the standard output), then we print the standard HTTP header to inform the browser that the following text will be an HTML page (even text/plain works fine) and if we get an error, it will be in some part of the page source :D.

Simple, probably not the best, but it works.

Wednesday, January 21, 2009

FLTK2 howto: Display images on widgets

I spent some time figuring out how to do this. Yes, you simply call:

widgetName->image(myPrettyImage);

But (at least for me) this did not work.
So I searched in the documentation but all seemed right.
The next logical step was "search on Google" (everyone should know that "he is your friend").

I read something on a forum regarding the function fltk::register_images() (Absolutely not documented) and I understood that it must be called before you use any other image-related stuff with fltk2.

So, if you want a widget to display your beautiful image you must code a main() like this:


#include "myMainWindowWithBackgrounImage.h"
#include <fltk/SharedImage.h>
#include <fltk/run.h>

int main()
{
fltk::register_images();
myMainWindowWithBackgroundImage win;
win.show();
return fltk::run();
}
You must then link the executable to libpng, libjpeg and libfltk2_images passing "-lpng -ljpeg -lfltk2_images" to the compiler.