Compilation Error when Invoking “CudafyTranslator.Cudafy()”

When using CUDAfy.NET, if you encounter the “Compilation Error” when running examples, simply add “cl.exe” to your PATH! It will resolve the translator exceptions. It’s funny how I didn’t find much about it online…

Carve Error : “didn’t manage to link up hole!”

Carve is a fast and robust library for performing Constructive Solid Geometry (CSG) operations (boolean operations) on meshes.

In case the mesh is corrupted or has self-intersections, Carve will refuse to perform operations on it and will terminate with this error: “didn’t manage to link up hole!”.

To solve that, simply open the mesh in a mesh software (such as netfabb) and repair it.

C# Deserialization of a List<> Returns a List with “null” objects

In .NET, the List<> class is serializable. However, you should note that during a call to the deserialization constructor of the objects the list contains, the list will be empty. All operations on the newly deserialized objects (such as value checking and so on) need to be performed in another function marked by the [OnDeserializedAttribute] attribute. More info on this can be found here.

CMake Unable to Find AMD’s ACML Files Under Windows

One popular BLAS implementation under Windows is AMD’s ACML.
CMake has some modules that can find certain required dependency libraries. As far as I know, under Windows the “FindBLAS” and “FindLapack” modules are unable to locate AMD’s ACML libraries. This is because ACML > v4.0 does not include the “mv” related packages anymore.

I looked around a little bit and I found a patched version of these files here and here. However, I couldn’t get them to work. If anyone was able to get them to work, comment below.

Cannot Find “getopt.h” File When Compiling Under Windows

Often times, issues arise when compiling C/C++ code developed for Linux under Windows. One annoying problem is when the code requires some header which is only available in the POSIX API. A header commonly used for parsing the command line arguments is getopt.h. Unfortunately, this header is only available under Linux. After some digging around, I found a port of this header file for Windows here.

In case the repository went down in the future, I’ve pasted the code here. All credits go to the original author. Click on the link below for the full code.

Continue reading

Rotate a 3D object around its center and own axes in WPF 3D

In WPF 3D, various transformations could be applied to an object. One particular problem that I occasionally run into is when I want to apply a rotation transformation to an object and rotate it around its own center and axes.

The way I do it is as follows. First I apply 3 identity RotateTransform3D‘s to the object with AxisAngleRotation3D‘s objects underneath: one for the X axis, one for the Y axis and one for the Z axis. Then whenever I want to rotate the object around a certain axis, I obtain the corresponding RotateTransform3D object, set its center according to the (possibly) translated center of the object, and apply the rotation angle to the underlying AxisAngleRotation3D object. Some code will make this more clear.

First apply the 3 identity transforms to the object:

Transform3DGroup transforms = new Transform3DGroup();
// Rotation around X
transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0)));
// Rotation around Y 
transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0)));
// Rotation around Z
transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0))); 
// Translate transform (if required)
transforms.Children.Add(new TranslateTransform3D());
MyObject.Transform = transforms;

Let’s say the function SetRotation is to be used for setting the rotation of the object. It will work as follows:

public void SetRotation(double amountX, double amountY, double amountZ)
{
	// Suppose we have a function that gives us all the transforms
	// applied to this object
	Transform3DGroup transfomrs = GetTransforms();
	TranslateTransform3D translation = transforms.Children[3];
	// Suppose GetCenter() obtains the center point of an object
	// in Point3D format
	Point3D translatedCenter = translation.Transform(GetCenter());

	RotateTransform3D rotX = (transforms.Children[0] as RotateTransform3D);
	RotateTransform3D rotY = (transforms.Children[1] as RotateTransform3D);
	RotateTransform3D rotZ = (transforms.Children[2] as RotateTransform3D);

	// Set the center point of transformation to the translated center of the object
	rotX.CenterX = rotY.CenterX = rotZ.CenterX = translatedCenter.X;
	rotX.CenterY = rotY.CenterY = rotZ.CenterY = translatedCenter.Y;
	rotX.CenterZ = rotY.CenterZ = rotZ.CenterZ = translatedCenter.Z;

	// Apply the angle amounts
	(rotX.Rotation as AxisAngleRotation3D).Angle = amountX;
	(rotY.Rotation as AxisAngleRotation3D).Angle = amountY;
	(rotZ.Rotation as AxisAngleRotation3D).Angle = amountZ;
}

Orient a Transform to a Specific Direction in Unity 3D

If you are too lazy to do the math to orient an object to a specific direction, this tip is useful for your.

The Transform objects in unity have the Forward, Up and Right properties. You probably don’t know that they are not only accessors but also mutators! You can simply assign a direction vector to an object’s Forward direction and the object will face that direction:

Vector3 dir = new Vector3(10, 0, 10);
this.Transform.Forward = dir;

This can be handy sometimes, especially when you want to use the Kinect sensor inside the game. If the players arm direction signifies the orientation of something (eg. a sword), this tip saves you some time.

C/C++: Code Not Working under a Certain Build Configuration

If your C/C++ code works fine under a certain build configuration (eg. Release) but not under another (eg. Debug) or simply works fine when built with a certain compiler, it is a sign that the code is not robust and some small detail which depends on compiler optimization is producing undefined behavior.

For instance, my code was working fine under Linux (using G++) and also Visual C++ 2013 (using the Release) configuration, but was giving me a hard time under the “Debug” configuration in VC++. Turned out that the compiler optimization under “Release” was preventing a destructor from being called. Since the destructor was never called, no memory leak was occurring. Building under “Debug” would have disabled that optimization and the code would have crashed with a memory leak error!

This just emphasizes how important it is to test the code thoroughly and detect those parts that could lead to undefined behavior.

Fix MATLAB Error: “dlopen: cannot load any more object with static TLS”

On Linux, you may occasionally encounter the error “dlopen: cannot load any more object with static TLS” in MATLAB. This is a known bug since way back!

To fix, create a file “startup.m” in the directory that you start MATLAB from with the following content:

ones(10)*ones(10);

I know! It’s ugly… But it works!!

EDIT: Since it was not clear, the folder that you start MATLAB from is by default “~/Documents/MATLAB” under Linux. On Windows, that would be “Documents\MATLAB”.

Apache Always Redirect to HTTPS for SSL Websites

After getting an SSL certificate, it is usually good idea to redirect all http (port 80) connections on your website to https (port 443). This can be simply done in Apache. To do this, just go to your public_html folder and either create a .htaccess file and add these lines, or add them to the already existing .htaccess file:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Note: The Apache mod_rewrite must be enabled for this to work!