view Framework/Scene2D/CompositorHelper.cpp @ 585:b9ce24c606ae

TextSceneLayer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 17:57:58 +0200
parents
children
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2019 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "CompositorHelper.h"

#include <Core/OrthancException.h>

namespace OrthancStone
{
  class CompositorHelper::Item : public boost::noncopyable
  {
  private:
    std::auto_ptr<ILayerRenderer>  renderer_;
    const ISceneLayer&             layer_;
    uint64_t                       lastRevision_;

  public:
    Item(ILayerRenderer* renderer,     // Takes ownership
         const ISceneLayer& layer) :
      renderer_(renderer),
      layer_(layer),
      lastRevision_(layer.GetRevision())
    {
      if (renderer == NULL)
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
      }
    }

    ILayerRenderer& GetRenderer() const
    {
      assert(renderer_.get() != NULL);
      return *renderer_;
    }

    const ISceneLayer& GetLayer() const
    {
      return layer_;
    }

    uint64_t GetLastRevision() const
    {
      return lastRevision_;
    }

    void UpdateRenderer()
    {
      assert(renderer_.get() != NULL);
      renderer_->Update(layer_);
      lastRevision_ = layer_.GetRevision();
    }
  };


  void CompositorHelper::Visit(const ISceneLayer& layer,
                               int depth)
  {
    Content::iterator found = content_.find(depth);

    assert(found == content_.end() ||
           found->second != NULL);

    if (found == content_.end() ||
        &found->second->GetLayer() != &layer)
    {
      // This is the first time this layer is rendered, or the layer
      // is not the same as before
      if (found != content_.end())
      {
        delete found->second;
        content_.erase(found);
      }

      std::auto_ptr<ILayerRenderer> renderer(factory_.Create(layer));

      if (renderer.get() != NULL)
      {
        renderer->Render(sceneTransform_);
        content_[depth] = new Item(renderer.release(), layer);
      }
    }
    else
    {
      // This layer has already been rendered
      if (found->second->GetLastRevision() < layer.GetRevision())
      {
        found->second->UpdateRenderer();
      }

      found->second->GetRenderer().Render(sceneTransform_);
    }

    // Check invariants
    assert(content_.find(depth) == content_.end() ||
           (&content_[depth]->GetLayer() == &layer &&
            content_[depth]->GetLastRevision() == layer.GetRevision()));
  }


  CompositorHelper::~CompositorHelper()
  {
    for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
    {
      assert(it->second != NULL);
      delete it->second;
    }
  }

  
  void CompositorHelper::Refresh(unsigned int canvasWidth,
                                 unsigned int canvasHeight)
  {
    // Bring coordinate (0,0) to the center of the canvas
    AffineTransform2D offset = AffineTransform2D::CreateOffset(
      static_cast<double>(canvasWidth) / 2.0,
      static_cast<double>(canvasHeight) / 2.0);

    sceneTransform_ = AffineTransform2D::Combine(offset, scene_.GetSceneToCanvasTransform());
    scene_.Apply(*this);
  }
}