private void updateSpotColors() {
    @SuppressWarnings("unchecked")
    final FeatureColorGenerator<Spot> spotColorGenerator =
        (FeatureColorGenerator<Spot>) displaySettings.get(KEY_SPOT_COLORING);

    for (final int frame : blobs.keySet()) {
      final SpotGroupNode<Spot> spotGroup = blobs.get(frame);
      for (final Iterator<Spot> iterator = model.getSpots().iterator(frame, false);
          iterator.hasNext(); ) {
        final Spot spot = iterator.next();
        spotGroup.setColor(spot, new Color3f(spotColorGenerator.color(spot)));
      }
    }
  }
  private void buildFrameContent(
      final SpotCollection spots,
      final Integer frame,
      final double radiusRatio,
      final FeatureColorGenerator<Spot> spotColorGenerator) {
    final Map<Spot, Point4d> centers = new HashMap<Spot, Point4d>(spots.getNSpots(frame, false));
    final Map<Spot, Color4f> colors = new HashMap<Spot, Color4f>(spots.getNSpots(frame, false));
    final double[] coords = new double[3];

    for (final Iterator<Spot> it = spots.iterator(frame, false); it.hasNext(); ) {
      final Spot spot = it.next();
      TMUtils.localize(spot, coords);
      final Double radius = spot.getFeature(Spot.RADIUS);
      final double[] pos = new double[] {coords[0], coords[1], coords[2], radius * radiusRatio};
      centers.put(spot, new Point4d(pos));
      final Color4f col = new Color4f(spotColorGenerator.color(spot));
      col.w = 0f;
      colors.put(spot, col);
    }
    final SpotGroupNode<Spot> blobGroup = new SpotGroupNode<Spot>(centers, colors);
    final ContentInstant contentThisFrame = new ContentInstant("Spots_frame_" + frame);

    try {
      contentThisFrame.display(blobGroup);
    } catch (final BadTransformException bte) {
      System.err.println(
          "Bad content for frame "
              + frame
              + ". Generated an exception:\n"
              + bte.getLocalizedMessage()
              + "\nContent was:\n"
              + blobGroup.toString());
    }

    // Set visibility:
    if (spots.getNSpots(frame, true) > 0) {
      blobGroup.setVisible(spots.iterable(frame, true));
    }

    contentAllFrames.put(frame, contentThisFrame);
    blobs.put(frame, blobGroup);
  }
  @Override
  public void modelChanged(final ModelChangeEvent event) {
    if (DEBUG) {
      System.out.println(
          "[SpotDisplayer3D: modelChanged() called with event ID: " + event.getEventID());
      System.out.println(event);
    }

    switch (event.getEventID()) {
      case ModelChangeEvent.SPOTS_COMPUTED:
        makeSpotContent();
        break;

      case ModelChangeEvent.SPOTS_FILTERED:
        for (final int frame : blobs.keySet()) {
          final SpotGroupNode<Spot> frameBlobs = blobs.get(frame);
          for (final Iterator<Spot> it = model.getSpots().iterator(frame, false); it.hasNext(); ) {
            final Spot spot = it.next();
            final boolean visible =
                spot.getFeature(SpotCollection.VISIBLITY).compareTo(SpotCollection.ZERO) > 0;
            frameBlobs.setVisible(spot, visible);
          }
        }
        break;

      case ModelChangeEvent.TRACKS_COMPUTED:
        trackContent = makeTrackContent();
        universe.removeContent(TRACK_CONTENT_NAME);
        universe.addContent(trackContent);
        break;

      case ModelChangeEvent.TRACKS_VISIBILITY_CHANGED:
        updateTrackColors();
        trackNode.setTrackVisible(model.getTrackModel().trackIDs(true));
        break;

      case ModelChangeEvent.MODEL_MODIFIED:
        {

          /*
           * Deal with spots first.
           */

          // Useful fields.
          @SuppressWarnings("unchecked")
          final FeatureColorGenerator<Spot> spotColorGenerator =
              (FeatureColorGenerator<Spot>) displaySettings.get(KEY_SPOT_COLORING);
          final double radiusRatio = (Double) displaySettings.get(KEY_SPOT_RADIUS_RATIO);

          // Iterate each spot of the event.
          final Set<Spot> spotsModified = event.getSpots();
          for (final Spot spot : spotsModified) {
            final int spotFlag = event.getSpotFlag(spot);
            final int frame = spot.getFeature(Spot.FRAME).intValue();
            final SpotGroupNode<Spot> spotGroupNode = blobs.get(frame);

            switch (spotFlag) {
              case ModelChangeEvent.FLAG_SPOT_REMOVED:
                spotGroupNode.remove(spot);
                break;

              case ModelChangeEvent.FLAG_SPOT_ADDED:
                {

                  // Sphere location and radius
                  final double[] coords = new double[3];
                  TMUtils.localize(spot, coords);
                  final Double radius = spot.getFeature(Spot.RADIUS);
                  final double[] pos =
                      new double[] {coords[0], coords[1], coords[2], radius * radiusRatio};
                  final Point4d center = new Point4d(pos);

                  // Sphere color
                  final Color4f color = new Color4f(spotColorGenerator.color(spot));
                  color.w = 0;

                  // Do we have an empty frame?
                  if (null == spotGroupNode) {
                    /*
                     * We then just give up. I dig really hard on an elegant
                     * way to add a new ContentInstant for a missing frame,
                     * but found no satisfying way. There is no good way to
                     * add spots to an empty frame. The way I found is very
                     * similar to closing the 3D viewer and re-opening it,
                     * therefore I let the user do it.
                     *
                     * So because of this, the SpotDisplayer3D is only a
                     * partial ModelListener.
                     */
                    System.err.println(
                        "[SpotDisplayer3D] The TrackMate 3D viewer cannot deal with adding a spot to an empty frame.");
                  } else {
                    spotGroupNode.add(spot, center, color);
                  }

                  break;
                }

              case ModelChangeEvent.FLAG_SPOT_FRAME_CHANGED:
                {

                  // Where did it belonged?
                  Integer targetFrame = -1;
                  for (final Integer f : blobs.keySet()) {
                    if (blobs.get(f).centers.containsKey(spot)) {
                      targetFrame = f;
                      break;
                    }
                  }

                  if (targetFrame < 0) {
                    System.err.println(
                        "[SpotDisplayer3D] Could not find the frame spot " + spot + " belongs to.");
                    return;
                  }

                  blobs.get(targetFrame).remove(spot);
                  // Sphere location and radius
                  final double[] coords = new double[3];
                  TMUtils.localize(spot, coords);
                  final Double radius = spot.getFeature(Spot.RADIUS);
                  final double[] pos =
                      new double[] {coords[0], coords[1], coords[2], radius * radiusRatio};
                  final Point4d center = new Point4d(pos);

                  // Sphere color
                  final Color4f color = new Color4f(spotColorGenerator.color(spot));
                  color.w = 0;
                  if (null == spotGroupNode) {
                    /*
                     * We then just give up. See above.
                     */
                    System.err.println(
                        "[SpotDisplayer3D] The TrackMate 3D viewer cannot deal with moving a spot to an empty frame.");
                  } else {
                    spotGroupNode.add(spot, center, color);
                  }
                  break;
                }

              case ModelChangeEvent.FLAG_SPOT_MODIFIED:
                {
                  if (null != spotGroupNode) {
                    spotGroupNode.remove(spot);
                    // Sphere location and radius
                    final double[] coords = new double[3];
                    TMUtils.localize(spot, coords);
                    final Double radius = spot.getFeature(Spot.RADIUS);
                    final double[] pos =
                        new double[] {coords[0], coords[1], coords[2], radius * radiusRatio};
                    final Point4d center = new Point4d(pos);

                    // Sphere color
                    final Color4f color = new Color4f(spotColorGenerator.color(spot));
                    color.w = 0;
                    spotGroupNode.add(spot, center, color);
                  }
                  break;
                }

              default:
                {
                  System.err.println("[SpotDisplayer3D] Unknown spot flag ID: " + spotFlag);
                }
            }
          }

          /*
           * Deal with edges
           */

          for (final DefaultWeightedEdge edge : event.getEdges()) {
            final int edgeFlag = event.getEdgeFlag(edge);
            switch (edgeFlag) {
              case ModelChangeEvent.FLAG_EDGE_ADDED:
              case ModelChangeEvent.FLAG_EDGE_MODIFIED:
              case ModelChangeEvent.FLAG_EDGE_REMOVED:
                {
                  if (null == trackNode) {
                    trackContent = makeTrackContent();
                    universe.removeContent(TRACK_CONTENT_NAME);
                    universe.addContent(trackContent);
                  } else {
                    trackNode.makeMeshes();
                    updateTrackColors();
                  }
                  break;
                }

              default:
                {
                  System.err.println("[SpotDisplayer3D] Unknown edge flag ID: " + edgeFlag);
                }
            }
          }
          break;
        }

      default:
        {
          System.err.println("[SpotDisplayer3D] Unknown event ID: " + event.getEventID());
        }
    }
  }