Thursday 28 February 2013

A bit of fun on the side: Compiling with Clang

In the other bit of spare time I have considered looking at compiling FreeCAD using Clang. The end result is that only a few changes are needed to be made to the FreeCAD source so that it compiles with Clang.It doesn't seem to affect building using g++ either. I haven't measured the performance yet, but noticeably compiling takes less time.

I still need to collect these changes and post a patch for this and allow other people to test it out for themselves. I also have to find how I can debug FreeCAD after compiling with Clang - it appears to be incompatible with GNU's gdb.


Other Progress (slightly off-topic):

Workload seems to have increased once again at University. I spent a bit more time at the weekend on the Drawing Module: the majority of the work is looking at projecting faces onto the drawing plane. This is relatively similar to before but requires further work to sort edges into order, store these in a general format and then drawing using Qt's painter system. There's still work to do, but when I get that complete can hopefully share it.

What is Clang?

Clang is an alternative compiler that use LLVM to compile a variety of programming langaguesusing runtime optimisation methods. LLVM is used heavily in Mac OS X to improve graphics performance applications, but is also used in a handful of open source projects - in particular Open Source graphics drivers. LLVM provides the possibility to compile many different programming languages from Python, Fotran, OpenCL and importantly for this post c / c++.

Clang provides the infrastructure for compiling c++ and aims to offer a better alternative to gnu's gcc in terms of performance, better usability to developers and other advanced features such as static code checking. I won't say much more, but it seems a good alternative and therefore I decided to look at compiling FreeCAD using Clang.

What I did?

  1. Install Clang
  2. Change default compiler for make tool chain
  3. Change some of the check inside CMakeList.txt file
  4. (Ubuntu) Add a symlink for libgfortran for the linker to correctly work

Selecting the default compiler for your system (Ubuntu)

Most importantly install clang on your system. It seems that with Ubuntu installing clang introduces a an unwanted dependency of  gcc 4.6. Once installed, depending on your system you can inform Cmake explicitly to use clang or alternatively you can change the system wide default for make to use the clang compiler.The first option is obviously the most sensible, but I am not sure of the command.

The only help I have seen on setting the compiler is the following stackoverflow question:

In Ubuntu, it is straighforward to update the system wide compiler use for c++, simply run

sudo update-alternatives --config c++

This will bring up an interface to select the compiler you wish to use:



I need to post the patches to CMake, hopefully soon and these can be verified by other users. 

Following this, configure using cmake and then attempt to build using clang. You may notice that there is a linking error for lgfortran and then the build system fails. I think this is an issue with Ubuntu, but to solve this I had to create a symlink to gfortran library which isn't very convenient.

sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/4.7/libgfortran.so libgfortran.so

Hopefully FreeCAD should happily compile very fast now at blinding speed!

----Update---


I have spent some time last week to look at some further details.

Compiling using Clang per project:

There is an advanced option within CMake to specify what compiler to use for c and c++ per project build. Fortunately KDevelop makes this rather easy: Open Project Configuration by right clicking on the project to open the following dialog:


Click 'Show Advanced Values' and then set the following variable:

  • CMAKE_CXX_COMPILER  = /usr/bin/clang++ 
  • CMAKE_C_COMPILER to /usr/bin/clang

Compiler flags can be conveniently set in CMAKE_CXX_FLAGS, where for both gnu compiler and clang -g is for full debug.

Specifying Variables in the command line

It can also be done in the command line - I found really straightforward Guide for setting Variables in Cmake from (http://www.llvm.org/docs/CMake.html#id8)


Variables are set quite easily - simply prepend -D before the variable name and assign the value like so
cmake -DVARIABLE=value
Alternatively, you can specify the type of the variable below:
cmake -DVARIABLE:TYPE=value


Debugging with Clang:

Unfortunatly there is currently a bug that is preventing gdb being used with clang, which is apparently fixed in the latest release. I tried to get this too compile but I encountered errors. Hopefully with the next release of LLVM and Clang 3.3, the issue will be resolved. 

Sunday 17 February 2013

Compile OpenCASCADE Community Edition the Easy Way! (Update)

Introduction:

After a very late last night as a result from the following: splitting my head for three hours on Finite Element Modelling and for some reason watching Back to the Future:Part II whilst figuring out some code for the section plane, unbelievably I managed to wake up reasonably fresh.

Before starting on the chore of further University work this afternoon. I've been playing with compiling OpenCascade Community Edition in attempt to remove the shackles of Ubuntu's dictated policy of offering old packages.



All of this was inspired by a message on IRC #freecad over the past week about compiling OpenCascade with debug flags, so we actually have useful output when OpenCascade crashes without a warning (mostly from an unhandled exceptions on our part).

Benefits:

  • The other benefits are testing some new features - only recently has a commit been made to improve performance of boolean operations that will soon be merged - see discussusion
  • Full debug build so we can trace crashes correctly
  • Living life on the edge of your seat!

The first compile went really well, however, after attempting to compile FreeCAD I found that there were a few missing libraries that prevent FreeCAD from building. These were the visualisation and Model Exachange packages used by SMESH and Part for the IGES and STEP formats import.  I am not sure if these should have been detected by FreeCAD's cmake but anyway, trial and error prevails.

I also attempted to build OCE using TBB which is a threading library that can be used to speed up operations, however from what I read this is only used in a few algorithms.

Note:

Compiling OpenCascade does take a long time, around probably 50% longer than FreeCAD for a clean build. On my six core AMD Bulldozer computer, it took in the region of 18 minutes in total to compile with each core at 100%. Also remember that you will need additional time to compile FreeCAD again. Once this has been done, recompiling is very quick.



Be prepared to find something useful to do in the mean and drink tea or even a beer for a good half an hour.

Compiling OCE OpenCascade Community Edition (Easy Guide):


Reminder: expect 20-30 Minutes to compile:

Clone the repository:

Grab the latest git source from the official Open Cascascade Community Edition from the github page. The source is around 240mb when fully expanded and may take a while to download on a slow internet connection.

git clone https://github.com/tpaviot/oce.git

Create a build directory:

Create a suitable build directory, this can be in the main source folder but preferably outside to prevent potential conflicts
mkdir oceBuild && cd oceBuild

Install libftgl-dev in Package Manager:

You need to install following development package for libftgl to compile the Visualisation Package in OCE. This will depend on your Distribution. 

Install libtb2 & libtbb-dev in Package Manager:

For enabling TBB - Threaded Building Blocks Library to be used in multithreading, you will need to ensure that these are installed on your system. Below are the package names in Ubuntu.

Remove any previous Opencascade libraries provided by distro:

I don't think OpenCascade and OCE can be run in parallel on the same machine - correct me if I'm wrong. Therefore you will need to remove any of these on your system.  

Configure the sources using CMake

Configure the OCE sources running the following cmake command in the current directory. This will install OCE to your /usr directory. 

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr/ \
      -DOCE_INSTALL_PREFIX:PATH=/usr/ \
      -DOCE_WITH_FREEIMAGE=OFF \
      -DOCE_WITH_GL2PS=OFF \
      -DOCE_VISUALISATION=ON \
      -DOCE_DATAEXCHANGE=ON \
      -DOCE_MULTITHREAD_LIBRARY=TBB \
      -DCMAKE_BUILD_TYPE=Debug \
      ../oce

If you're installing on Ubuntu or Debian based distribution, it's advisable to change the install directory to /usr/local to isolate user compiled packages (@yorik) - (I need to confirm if FreeCAD can detect the location of the OCE header files from the location)

-DCMAKE_INSTALL_PREFIX:PATH=/usr/local \

You may find that there are some missing development dependencies, so if there are you will have to install these using your package manager. I found generally if you could install FreeCAD you wouldn't experience any problems atleast on Ubuntu.

Create a release build (Improve Performance):

You may not want debug flags enabled so that you can have full compiler optimisations and full performance when using FreeCAD: change the line previously from
DCMAKE_BUILD_TYPE=Debug \
to
DCMAKE_BUILD_TYPE=Release \ 

Begin Compiling:

When you start compiling OCE  be prepared to wait a while. Replace 6 with the number of cores or independent threads that can be run on your computer to speed up compiling by running concurrent make operations.
make -j6 -pipe

There has been some discussion whether the '-pipe' argument can reduce compiling times. If someone has enough time to make a comparison with OCE it would be interesting to see the difference.

Install:

Usually I install any compiled software to my user directory, but that wouldn't be straightforward. Therefore CMake was configured to install to /usr directory for simplicity later on. 
sudo make install
 
 

Installing on Debain / Ubuntu Systems:

Another helpful hint from @yorik is to install using the built in utility checkinstall. This provides a convenient method of uninstalling OCE using the system's package manager. Once you have finished compiling, run the following command:

sudo checkinstall

Type (y) to accept the defaults, then you will be presented with a menu to further describe the package - do this at your own leisure:

Once checkinstall is completed - this will take a while, it will produce a .deb package and OCE will have been installed to your system and registered in your system package manager.
 

Recompile FreeCAD:

There will be changes to the development sources since we are using a different edition of OpenCascade with FreeCAD. You will need to clean all the sources. From your previous FreeCAD build directory make sure run the following before recompiling (see previous post on compiling FreeCAD :

sudo make clean

If OCE was compiled and installed correctly, whilst configuring FreeCAD you will find the following log message (OpenCASCADE Community Edition has been found):



Recompile once again and you should have FreeCAD working with the latest OCE!

Hopefully people will have understand the guide and will now have a working version of OCE that is is used by FreeCAD. If there seems to be problems or something isn't clear, please let me know and I will update the guide. 

Saturday 16 February 2013

Drawing Module: Section Views

This past week has been a tremendously busy one at University, but in the few bits of time I do have available, I have been looking at incorporating section views into my experiments on the Drawing Module. For those unfamiliar what a 'Section' views, below is an example. These are highly important in mechanical design and allow complex features to be seen and also annotated

Sectional View Along Axis A-A
Yorik, already did some work within his arch module purely using the python capabilities within FreeCAD. This can be used in the latest release of 0.13. His solution is simple but works well. Below is a representation of the sectioning algorithm (I am currently waiting for a download to finish so found the time to draw it)

  1. The user defines a 3D plane. The 3D Plane is then checked to see if it intersects with the object
  2. The bounding box for the FreeCAD part is then projected onto the cutting plane - there are 8 points.
  3. The maximum size of each projection on the principal axes of the plane are found. These are then turned into an extruded box prism and a boolean cut operation leaves the remaining part.
  4. The remaining sectioned part remains. The faces are then projected and if they are coincident with the cutting plane it will be sectioned face.
Interestingly enough I decided to mimic such behavior in the Sectioned View of this. Although relatively simple, in c++ requires finding the correct modeling algorithms in OpenCascade. After some mind boggling, I eventually got something crude to work.

The functionality I am aiming towards is something similar to the functionality in other commercial CAD packages (namely Solidworks). My view is that using a 3D plane to section an object can be slightly tedious and awkward - atleast until we get a plane feature with the arrival on the Assembly Module. Instead I think it is more practical to create a section in the 2D Drawing User Interface.



 The user can create both 3D and Orthographic projections of the object and the user will 'draw' a section line AA in 2D. Upon completion this will create a new 'Section' View to the side that can be freely positioned.

The section plane is calculated simply by taking the cross product of the current view normal and the line, which produces the new normal of the plane.

This would hopefully cover most cases, but it should also allow an arbitrary plane to be set to create more flexibility when sectioning. 

The one complication that arises is that references will be lost because of the boolean operation. This will be of concern atleast until we get the more robust naming mechanism.

Hatching:

Any sectioned faces or even chosen ones should be fillable with a hatch pattern. This can be detected automatically if the face is coincident on the plane. I haven't yet decided the approach for storing hatching information but should be relatively straight forward.

I am hoping to get some more of this done tonight or tomorrow and have something to show but that all depends on University work... 

Tuesday 12 February 2013

FreeCAD 0.13 Official Release Available

A big congratulations to everyone who has helped make our latest 0.13 release possible! This include all the contributors who have helped develop, code, build and of course our endless list of patient users who have been diligently testing our development snapshots.


There has been so many improvements throughout the whole application. Many new features have been added and further performance and stability improvements in sketcher has been achieved over the year. The release notes give a snapshot of what has been achieved over the past year. I stand now still finding it unbelievable how much momentum we have gained over the past year. 


It isn't official but we may decide to back-port improvements and bug fixes for the relatively stable 0.13 release now. 

The Future: The long road ahead

The future looks very promising, with numerous cool features in the works! The most anticipated being the Assembly Project. This is not to mention my render workbench and some other improvements that are hidden away in my git repos which are waiting further testing and refinement. My personal motivation is to get someone to use my render module to produce a render of their design for the new FreeCAD splash screen!

My pledge to everyone get involved, however you think you can! This doesn't have to be programming in python or c++, but there are  many ways you can get involved
  • Test our development releases for bugs
  • Help other users on the FreeCAD forums
  • Create and publish tutorial guides
  • Improve our documentation and translation efforts
  • Design, Maintain and help our websites and promoting FreeCAD
  • Package FreeCAD for Linux Distributions