El número 64 de la gran revista DotNetManía publica un artículo que he escrito sobre creación de combos de controles en Windows Forms con un solo click, además de muchas otras cosas muy muy interesantes. Espero os resulte de ayuda. Saludos!
Google StreetView-ed !
It was funny some months ago when I found the Google Street View car driving around our workplace. Now it has been uploaded to the web.
My pictures (excuse the quality):
Google version:
Ver mapa más grande
Jaaaaa… I´m the guy behind the streetlight… !!!
Cheers!
Autonomous car driving by V.A.I.L
Autoblog has published today a very interesting video on autonomous driving. It surely worth the time you´ll invest watching it…
During the first seconds, you can see some Java code. It appears there because that’s the “yesterday” part of the video. I´m sure that they were just about putting some .Net code in the “tomorrow” chapter. They probably run out of time… ;)
Specially beautiful the close look at the accelerating wheel, at the end of the video.
Hope you enjoy it.
Simax ECO-Driving goes U.K.
Simax is starting to distribute the ECO-Experience Simax Simulator in the U.K. Of course, they drive on the left, so we had to develop new environments, with a British look.
Want it real? Check this…
Simax ECO-Experience allows drivers to improve their driving skills, with a special focus on ecological driving concepts. Simax ECO-Experience has been installed in several 1st line Motor Shows around Europe, where thousands of drivers learned how to save tons of CO2 emissions. To know more:
The real simulation experience
Usuario y Password para configuración del router ADSL de timofónica
Si, como a mi, no te dieron el usuario y password para configurar tu router de telefónica no desesperes. Todos suelen tener los mismos. Aqui va una recopilación:
- Usuario: admin, Password: admin
- Usuario: adminttd, Password: adminttd
- Usuario: 1234, Password: 1234
- Usuario: user, Password: admin
- Usuario: admin, Password: 1234
- etc
Nota: Para acceder a la config de tu router tienes que ir en el navegador web (IE) a una dirección IP concreta, que suele ser: http://192.168.1.1
Espero que te sirva…
Properly scaling Point Sprites in DirectX
When rendering point sprites with DirectX, many people deals with the issue of point sprite scaling. There are many options regarding this, but in the 99% of the cases, people just wants one of the following two things:
1.- Point Sprites with a constant screen-space size
This sprites are rendered at the 3D position (projected to the viewport of course), but with a constant size, in pixels. So, if you move the camera towards the point sprite, it feels like if it shrinks, and if you move the camera away from the sprite, it will look like if “growing”.
This effect is sometimes desirable, specially if you are using point sprites to simulate 2D effects, post-processing or camera effects like Lens Flares (on your left).
To setup this kind of Point Sprite rendering in DirectX, you just need to disable PointSprite scaling. This way, you will be able to specify point sizes both with a default value or in each point vertex information. These are the render states you need to modify:
Disable point scaling: D3DRS_POINTSCALEENABLE = false
Set default point size (remember, in pixels): D3DRS_POINTSIZE = XX
2.- Point Sprites with a real 3D size
By contrary to what’s shown in the previous chapter, sometimes you need point sprites to behave like real 3D objects, i.e. when using them to simulate a particle system, or that kind of stuff.
In this case, you don’t want sprites to remain at the same size if you move away from them, as real 3D objects get smaller as we get farther from them and vice versa. It is obvious that we need to change the size of sprites (remember, it’s a 2D size, in pixels), accordingly to our 3D movements, to give the illusion of 3D behavior.
DirectX is already prepared for that, with a built-in system to handle point sprite scaling. You just need to activate it setting the render state D3DRS_POINTSCALEENABLE to true, and setting up some other parameters. If you take a look at the docs, you will find the following:
Point Size ComputationsPoint size is determined by D3DRS_POINTSCALEENABLE. If this value is set to FALSE, the application-specified point size is used as the screen-space (post-transformed) size. Vertices that are passed to Direct3D in screen space do not have point sizes computed; the specified point size is interpreted as screen-space size. If D3DRS_POINTSCALEENABLE is TRUE, Direct3D computes the screen-space point size according to the following formula. The application-specified point size is expressed in camera space units. S s = Vh * S i * sqrt(1/(A + B * D e + C *( D e2 ))) In this formula, the input point size, S i, is either per-vertex or the value of the D3DRS_POINTSIZE render state. The point scale factors, D3DRS_POINTSCALE_A, D3DRS_POINTSCALE_B, and D3DRS_POINTSCALE_C, are represented by the points A, B, and C. The height of the viewport, V h, is the D3DVIEWPORT9 structure's Height member representing the viewport. D e, the distance from the eye to the position (the eye at the origin), is computed by taking the eye space position of the point (Xe, Ye, Ze) and performing the following operation. D e = sqrt (Xe2 + Y e2 + Z e2) The maximum point size, Pmax, is determined by taking the smaller of either the D3DCAPS9 structure's MaxPointSize member or the D3DRS_POINTSIZE_MAX render state. The minimum point size, Pmin, is determined by querying the value of D3DRS_POINTSIZE_MIN. Thus the final screen-space point size, S, is determined in the following manner.
|
Well, that´s all. Clear ugh? Just kidding…
Setting the A,B,C scaling parameters for a correct, 3D, point scaling
In order to achieve the behavior you want, you just have to set the following configuration:
D3DRS_POINTSCALE_A = 0.0f
D3DRS_POINTSCALE_B = 0.0f
D3DRS_POINTSCALE_C = 1.0f
D3DRS_POINTSIZE_MIN = 0.0f
D3DRS_POINTSIZE_MAX = 9999.0f
Et voilá ! 3D point sprites…
Ultimas apariciones en prensa de Simax
Simax ha aparecido últimamente en prensa y radio, a raíz del galardón en los premios Bancaja, e interesándose especialmente por nuestros productos para formación en conducción ecológica. Ahí van los links:
La razón:
http://www.larazon.es/noticia/conduccion-ecologica-virtual
Punto Radio (con audio de la entrevista):
http://actual.lasprovincias.es/jovenesemprendedores/etiquetas/inaki-ayucar/
Saludos.
Dell XPS 630i, the BT Mini-Receiver, download managers, and other crap…
If you are one of the owners of a Dell computer, you´ll probably be happy with it, like me. However, nothing´s perfect, and of course dell is not either.
One of the things I hate most on Earth is that too-usual way of managing download from websites. Every company has it’s own download manager, and in a few months you end up with a dozen of them installed on your computer or on your browser: Adobe Update Manager, Dell Download Manager, Apple Update Manager, Google updater, Aaaaarrgggllll !!!! I just want to download the f**ing file !!!!! Stop selling me your crap !!!!
Well, back to business. If you own an XPS 630i (and probably other Dell models), and you don’t install all the crap that comes with your system (Dell tune up utility, Dell application, Dell recovery, Dell diagnostics, Dell sandwich maker and the Dell fantastasystem), or if you simply lost your original CD drivers, you will probably miss one: the driver for the BlueTooth Mini-Receiver.
It is not initially listed in the default downloads list at www.dell.com for the 630i (a very deficient list, I must say. Is it that difficult to include a small description of the purpose of each application or update?), and believe me, it’s not easy to find.
I also tried to use one of the magnificent Dell helper-applications to find it, but the one it suggested didn’t work. Finally, I found it here, thanks to the people of My630i.
Thanks a lot!
Ramblings about your own, fast C# Maths library
Intro
Why to write my own library?
Because I think it’s good to depend on the Graphics API as less as possible. Why?- Because APIs are deprecated from time to time (as happened with Managed DirectX), or evolve, or change, or re-appear in some form (as it seems that is going to happen with some parts of Managed DirectX, via the Windows 7 Code Pack), or you just decide to use another API, or you want your code to be compatible both with XNA and with DirectX.
- Because I want to be able to debug the inners of my Maths.
- Because writing your own library (even if you copy most parts of it), you will learn a lot.
- Because seeing the internals of things, you will realize how much work some tasks take, and you will think it twice before calling them…
Wait a minute. Faster code, using no API?
For CPU-related stuff, YES, it is possible.XNA (or DirectX) makes no magic. They just calculate things as you will do. But of course you must choose the best algorithms for each task.If you do so, you will end up with a code as fast as the one written in the API, but the point is, as you will know how things work in the inners, you will have a much more detailed idea of the cost of things, and you will optimize your code. In addition to that, you will be able to tune things a bit for your specific needs, increasing the speed a bit more.
However, keep in mind that companies like Microsoft have hundreds or thousands of the best engineers working in their products, so if there is a better method of doing something, the API will have it. You need to be that good too!
Let’s see an example of the kind of optimization you need to achieve to make your own Maths library as fast (or faster) than the ones included in the APIs. We will include some matrix operations and make some speed tests with different algorithms.
A note about Affine Transformation Matrices
In 3D graphics applications, transformations are typically built from the combination of elementary matrices (being this elementary matrices: translations, rotations, scales and the identity matrix). Matrices built this way, always result in Affine transformations (matrices with the right column of the matrix being 0,0,0,1 –if using row representation of matrices-).There are some algorithm optimizations in this article that are only valid for Affine Transformation matrices. You should avoid using them for non-affine matrices. It is the case of Perspective Projection matrices, which DO NOT BELONG to the affine transformations group.
A note about JIT compilation and .NET maths performance
This is a very interesting question: Are local variables faster than member variables?At first, one could say that they aren’t, as they have to be re-created each time the method is called. BUT, as this interesting blog post points out, while the JIT compiler can en-register local variables, it cannot do the same with member fields. So we should say that YES, THEY ARE, when talking about math calculations. As you can see the in the test, it makes a remarkable difference. So:
If you are developing in .Net and you have a performance critical math. calculation method, you should use local copy of variables to speed up calculations
|
Part #1 Matrix Determinant Calculation
The general Method (Based on Graphics Gems I)
The following is a pretty straight-forward algorithm for calculating the determinant of a matrix. It is mostly designed for non-realtime applications, as it quite slow and uses double-precission. It calculates a 4x4 determinant basing on the 3x3 method, then in the 2x2 and so on. It is a based in an algorithm used in Graphics Gems I for matrix inversion.The general Method (real-time applications optimized)
This second version is also a full, general method, but optimized for real-time applications. It is much faster, because of the calculation and because of the grouping of duplicated mults in numXX variables, saving multiplications and adds.Optimization I (faster Memory Accessing)
As stated in the intro, about the performance of local variables vs. member fields, we´ll make an optimization of the above method using this concept:Optimization II (Based on Graphics Gems II, for Affine transformations only)
The following algorithm is a direct translation of a piece of code found in a Affine Matrix Inversion algorithm, inside Graphics Gems II (see in the next post for the full algorithm). It makes only 12 mults and 7 adds. Keep in mind that this method is only valid for affine transformations (see intro for further information):The test
So, it’s time to measure this little boys.Performance
The test was made calculation a 4x4 affine matrix determinant 100 Million times, in a Intel Core 2 Quad Q9450 2.66Ghz with 3 Gb (non multi-threading).| General method (Graphics Gems I, non real-time) |
1 minute: 0.9 secs
|
| General method (real-time) |
2.6832 secs
|
| Optimization I (memory access) |
2.496 secs
|
| Optimization II (affine transformations) |
1.404 secs
|
Reliability
The reliability test was made with two different matrices.There is no surprises here, all the algorithms work well. The following is the matrices used for the tests (affine transformations) and the results (the determinant) calculated by each algorithm.Matrix 1:
| 0.7071068 | -0.235702276 | 0.6666667 | 0.0 |
| 0.0 | 0.9428091 | 0.33333334 | 0.0 |
| -0.7071068 | -0.235702276 | 0.6666667 | 0.0 |
| 0.0 | 0.0 | -15.0 | 1 |
| General method (Graphics Gems I) |
1.0000001641611829
|
| General method (real-time) |
1.00000012
|
| Optimization I (memory access) |
1.00000012
|
| Optimization II (affine transformations) |
1.00000012
|
Matrix 2:
| 9.848019 | 0.10061159 | 0.0 | 0.0 |
| -3.50578356 | 0.282625765 | 0.0 | 0.0 |
| 0.0 | 0.0 | 94.22 | 0.0 |
| 0.0 | 0.0 | 0.0 | 1 |
| General method (Graphics Gems I) |
295.47639778127973
|
| General method (real-time) |
295.4764
|
| Optimization I (memory access) |
295.4764
|
| Optimization II (affine transformations) |
295.4764
|
Part #2: Matrix Inverse Calculation
One of the most expensive Matrix operation is calculating the inverse of a matrix. Will start with the general algorithm for this, and study a couple of optimizations later:The general Method
This algorithm (explained here), is something like this:Optimization I (memory access and some operation savings)
This method applies the same concept, but using local variables and packing duplicated calculations in local variables too:Optimization II (for Affine Transformations only -Based on the Kevin Wu article, Graphics Gems II-)
The Graphics Gems books series are a must in any graphics or game developer’s bookshelves. You can by the the book I’m talking about here, or get more info about the Graphics Gems series here. Google have some excerpts of this book in the following link, but one page out of each two is missing: Graphics gems II - Página 342.So, the algorithm purposed by Mr. Wu looks like the following (ported to C#, original in C++):
Optimization III (for matrices composed by Rotations and Translations only)
If you know that your matrix is composed by rotations and translations only, you can apply this (even faster) method, which avoids calculating the determinant, and takes advantage of some properties of this kind of matrices. You can read all the info here.Additional Optimizations
If you need it, other optimizations could be included in this algorithms, detecting special matrices whose inverse can be calculated even faster. It is the case of diagonal matrices (where only the diagonal should be inverted using 1/value entries), or Orthogonal matrices (whose inverse is equal to its transpose).The Test
We made a very simple test, with 50.000.000 (50 Million) inverse calculations of affine transformations, in an Intel Core 2 Quad Q9450 2.66Ghz with 3 Gb (non multi-threading).| General method |
11.523 secs
|
| Optimization I (memory accessing and op. savings) |
5.4912 secs
|
| Optimization II (affine transformations only) |
2.7144 secs
|
| Optimization III (rotations and translations only) |
1.326 secs
|
Conclusions
Searching a bit, it is indeed possible to reach the performance of your graphics API and as mentioned above, the advantages of having (and building) your own library are countless.Take care!
2D Circle Packing algorithm ported to C#
Packing of different circles into a 2D space, trying to minimize the unused space, is a typical geometrical problem. Humans can solve it quite quickly, but it is very hard to find a solution mathematically. The most frequent implementations use numeric algorithms that get closer to the solution in each iteration.
It is the case of the above algorithm. It doesn’t implement the limits of the available space, but reorders the circles around a center point quite well. The “flowing” nature of numerical algorithms like these fit specially well if you want to use them for some kind of graphical interface, as you can change the adaptation (or reordering) speed, giving a very nice animated result, or you can even interact with your circles, as moving some of them will make the algorithm to react adapting others (you can try the above applet).
So, with all that material, it was easy to port it to C#. The result is the following:
The code for a very easy Circle class (please note that I´m using the XNA framework for the maths –Vector2, etc-):
This class holds a list of circles that will be re-arranged around a “PackingCenter” point, with a “MinSeparation” between them. You only need to iteratively call the OnFrameMove method, passing an “iterationCounter” parameter, that will hold the damping on the adaptation speed (the bigger value, the slower adaptation). Reset this parameter to 1 always you want to reset speed (never set this parameter to 0).
To do
A variable speed system could be done, and a way to interactively change the PackingCenter point would be nice too. It would also be necessary to implement the limits of the available space, changing the radius of the circles proportionally if they don´t fit.Cheers!
