Projects

Tools · 2023–2024

3D CAD viewer for Flutter — bachelor’s thesis

A Flutter package that shows CAD models smoothly on a smartphone using the Open Design Alliance’s native C++ SDK. My bachelor’s thesis in business informatics at TH Rosenheim, written together with innFactory GmbH.

Flutter · DartAndroid · JavaNative · C++PigeonJNIsetFpsLimitupdate()frameframetouchTexture widgetshows the textureGesture detectionwaiting for input onTouchMove(x, y) zoom(1.04, x, y) zoomExtends FPS detectormeasures the display 120 Hz → limit 124 SurfaceTextureTextureRegistryPigeon · VisualizeApiMethodChannel onTouchMove zoom runNavigationAction Render threadEGL · limit 50 FPS EGL · limit 124 FPS OpenGL ESdraws into the textureVisualize SDKIFC · STL · DWG rotate camera zoom camera fit model DigitalHub.ifcFPS 50FPS 120Calls go down through Pigeon and JNI, finished images come back up as a texture.
Status
Completed
Period
2023–2024
Area
Tools
Role
Bachelor’s thesis, sole developer
> 60 FPS
with an 8.6 MB IFC model
3 languages
Dart, Java and C++ in one package
× 1.03
FPS limit above the display’s frame rate
FlutterDartC++JavaOpenGL ESJNIPigeonCMake

The task

innFactory GmbH in Rosenheim develops BlueVis, a tool for electricians: they upload floor plans, image recognition finds the parts they need and puts them straight into a web shop. For use on building sites, a mobile app in Flutter was planned, with a professional 3D CAD viewer. It was to be built on the Visualize SDK by the Open Design Alliance, an extensive C++ library for CAD formats such as IFC, DWG and STL.

Flutter apps are written in Dart, the SDK in C++, and Android with Java sits in between. Existing 3D packages for Flutter mostly show models via WebGL in a WebView; for large CAD models on a phone that is too slow and drains the battery. The thesis therefore examines how Dart and C++ can work together efficiently and turns the result into a reusable Flutter package. The figure shows how a call travels through the three languages and how the answer comes back; each scenario can be stepped through.

One call through three languages
Flutter Dart
Android Java
Visualize SDK C++

 

Which way across the language border?

Flutter offers two routes to native code: platform channels send messages to Java or Kotlin, from where JNI continues to C++. FFI calls a C interface directly from Dart, without a detour. I built all three variants and measured them on a Pixel 7 Pro, with payloads from one byte to one megabyte, there and back.

There and back across the language border Response time per call, measured on a Pixel 7 Pro (figure 20 of the thesis, smoothed)
  • FFI
  • PlatformChannel
  • PlatformChannel + JNI
01,0002,0003,000µs0250 kB500 kB750 kB1 MBdata transferred
FFI
14 µs
PlatformChannel
74 µs
PlatformChannel + JNI
69 µs

FFI is 5.3× as fast as the PlatformChannel here. Values are read off the thesis chart and accurate to within a few per cent.

FFI is fastest throughout because it works directly on memory; platform channels first copy the data into a queue. For a viewer this hardly matters: a touch event is a few bytes and takes under 0.1 ms through the channel, while a frame at 120 Hz has 8.3 ms. What mattered more is that OpenGL ES, the texture and the render thread live on the platform side anyway. So all calls go through Pigeon: the tool generates matching classes for Dart and Java from one interface written in Dart, and type errors show up at compile time instead of in production. Java then calls the SDK via JNI. An FFI interface could be added alongside if needed.

The experiments from this part of the thesis, i.e. platform channels, Pigeon, FFI with ffigen and loading precompiled libraries at runtime, are in the thesis repository, together with the library.

How the images get into Flutter

The SDK draws with OpenGL ES. There are two ways to show that in Flutter: the platform code renders into a SurfaceTexture that Flutter takes into its own frame as a texture, or a native GLSurfaceView is embedded into the UI through a PlatformView. I built and compared both.

Texture or PlatformView? The back animation in slow motion, recreated from figure 33 of the thesis
Texture widget
Home
OpenGL texture
OpenGL platform view
OpenGL page
  • the image enters Flutter’s frame as a texture
  • fades out with the page, transparency possible
  • up to 470 FPS without a limit: handover and buffer swap ≈ 2.17 ms
  • stable with the Visualize SDK, hence chosen
PlatformView (GLSurfaceView)
Home
OpenGL texture
OpenGL platform view
OpenGL page
  • an Android view is embedded into Flutter
  • locks to the display’s 120 Hz on its own
  • lingers during page transitions, until frame 17
  • unstable with the Visualize SDK, despite a mutex
Frame budget: 16.67 ms
2.17 ms texture + buffer swap 14.5 ms left for rendering

At 60 Hz, 14.5 ms per frame remain for the Visualize SDK.

The GLSurfaceView is more convenient: it sets up EGL and the render thread itself and follows the display’s frame rate. Embedded in Flutter, though, it does not behave like a widget. During a page transition the 3D area stays on screen after the page is gone, and with the Visualize SDK it was not stable despite a mutex. The texture needs more code of its own for the EGL context, render loop and resizing, but behaves like any other widget. Without a limit, the render thread produced up to 470 frames per second this way, almost four times what the 120 Hz display can show. A limit was a must.

Rendering efficiently: the limit adjusts itself

An invisible widget counts how many frames Flutter actually shows each second and sets the render thread’s limit to that value times 1.03. No frame is rendered that nobody sees, and none is missing when the display runs faster. The render thread measures its work per frame and sleeps for the rest of the budget. Its actual frame rate comes back to Dart through getFps() as a return value, for the debug overlay.

A large IFC model over five minutes DigitalHub by RWTH Aachen (8.6 MB) on a Pixel 7 Pro, 5 minutes (figure 43 of the thesis)
  • rendered (Visualize SDK)
  • displayed (Flutter)
  • limit = displayed × 1.03
801001201400 s50 s100 s150 s200 s250 s300 s
rendered (Visualize SDK), at 136 s
101 FPS
displayed (Flutter)
120 FPS
average rendered · minimum
109 · 84 FPS

Short dips happen mostly while the model is being rotated or zoomed; the display stays at 120. Values read off the thesis chart.

Package structure

The library consists of three Flutter packages, so that further platforms only need to add an implementation later:

  • visualize_lib: the public side, with the VisualizeTexture widget for the 3D view, the gesture detection and the action bar. An app only needs the path to the file.
  • visualize_lib_api_base: the abstract interface every platform has to fulfil: create a texture, open a file, touch, zoom, navigation, FPS limit.
  • visualize_lib_api_android: the Android implementation with its Dart side, Java platform code and the native Visualize libraries for arm64-v8a, armeabi-v7a and x86_64.

The matching implementation registers itself: on Android, Flutter’s plugin system calls registerWith at startup and the class enters itself into the base class. The app does not need to know which platform it runs on.

Features

The images are taken from the bachelor’s thesis; click to enlarge.

Example app

The library comes with an example app: a file list with import through the system file picker, settings for a fixed FPS limit and a debug overlay, and the 3D view with an action bar. Its six buttons switch between panning and rotating, zoom into a lasso area, fit the model and zoom around the centre.

Three pages of the example app: file list, settings with FPS limit and debug mode, 3D view of a cube with the action bar

Gestures

One finger rotates or pans the model, depending on which button is active; two fingers zoom around their midpoint. The gesture detection tracks each finger separately and converts the positions into physical pixels of the 3D view before they go to the SDK.

Different processors

The package ships the native libraries for three architectures, and Android picks the right one at install time. I tested in the emulator on x86_64 and on a Pixel 7 Pro with arm64-v8a, with STL and IFC files.

Left: the Eiffel Tower as an STL file in the Android emulator (x86_64); right: the DigitalHub IFC model by RWTH Aachen on a Pixel 7 Pro (arm64)

Result

All mandatory requirements are met: an 8.6 MB IFC model runs at more than 60 frames per second, the library runs on the architectures tested, and an app embeds the viewer with a single widget. For production use, iOS is still missing (the modular structure is prepared for it), as are features such as cutting through the model or highlighting similar parts, for example every socket on a floor.

The source code of the library, the example app, the prototypes and all experiments is in the thesis repository. What is missing there is the Visualize SDK itself with its JNI layer: it is licensed by the Open Design Alliance. Without a membership the library cannot be built; the experiments do not need the SDK.