コード例 #1
0
  void SteepestDescentSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps>::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP<Matrix>& P) const {
    RCP<const Matrix> A = rcpFromRef(Aref);
    RCP<Matrix> AP, G;

    Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2, 0);

    Teuchos::ArrayRCP<const SC> D = Utils::GetMatrixDiagonal(*A);

    RCP<CrsMatrix> Ptmp_ = CrsMatrixFactory::Build(C.GetPattern());
    Ptmp_->fillComplete(P0.getDomainMap(), P0.getRangeMap());
    RCP<Matrix>    Ptmp  = rcp(new CrsMatrixWrap(Ptmp_));

    // Initial P0 would only be used for multiplication
    P = rcp_const_cast<Matrix>(rcpFromRef(P0));

    for (size_t k = 0; k < nIts_; k++) {
      AP = Utils::Multiply(*A, false, *P, false, mmfancy, true, false);
#if 0
      // gradient = -2 A^T * A * P
      SC stepLength = 2*stepLength_;
      G = Utils::Multiply(*A, true, *AP, false, true, true);
      C.Apply(*G, *Ptmp);
#else
      // gradient = - A * P
      SC stepLength = stepLength_;
      Utils::MyOldScaleMatrix(*AP, D, true, false, false);
      C.Apply(*AP, *Ptmp);
#endif

      RCP<Matrix> newP;
      Utils2::TwoMatrixAdd(*Ptmp, false, -stepLength, *P, false, Teuchos::ScalarTraits<Scalar>::one(), newP, mmfancy);
      newP->fillComplete(P->getDomainMap(), P->getRangeMap() );
      P = newP;
    }
  }
コード例 #2
0
ファイル: shadow-view-impl.cpp プロジェクト: mettalla/dali
void ShadowView::SetShaderConstants()
{
  Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
  Constraint projectionMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowPlane, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() );
  projectionMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::PROJECTION_MATRIX ) );
  projectionMatrixConstraint.Apply();

  Property::Index lightCameraViewMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
  Constraint viewMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowPlane, lightCameraViewMatrixPropertyIndex, EqualToConstraint() );
  viewMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::VIEW_MATRIX ) );
  viewMatrixConstraint.Apply();

  mShadowColorPropertyIndex = mShadowPlane.RegisterProperty( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor );
}
コード例 #3
0
ファイル: shadow-view-impl.cpp プロジェクト: mettalla/dali
void ShadowView::ConstrainCamera()
{
  if( mPointLight && mShadowPlane )
  {
    // Constrain camera to look directly at center of shadow plane. (mPointLight position
    // is under control of application, can't use transform inheritance)

    Constraint cameraOrientationConstraint = Constraint::New<Quaternion> ( mCameraActor, Actor::Property::ORIENTATION, &LookAt );
    cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_POSITION ) );
    cameraOrientationConstraint.AddSource( Source( mPointLight,  Actor::Property::WORLD_POSITION ) );
    cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_ORIENTATION ) );
    cameraOrientationConstraint.Apply();

    Constraint pointLightPositionConstraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, EqualToConstraint() );
    pointLightPositionConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) );
    pointLightPositionConstraint.Apply();
  }
}
コード例 #4
0
ファイル: utc-Dali-Shader.cpp プロジェクト: mettalla/dali
int UtcDaliShaderConstraint02(void)
{
  TestApplication application;

  tet_infoline("Test that a uniform map shader property can be constrained");

  Shader shader = Shader::New(VertexSource, FragmentSource);
  Material material = Material::New( shader );
  material.SetProperty(Material::Property::COLOR, Color::WHITE);

  Geometry geometry = CreateQuadGeometry();
  Renderer renderer = Renderer::New( geometry, material );

  Actor actor = Actor::New();
  actor.AddRenderer(renderer);
  actor.SetSize(400, 400);
  Stage::GetCurrent().Add(actor);
  application.SendNotification();
  application.Render(0);

  Vector4 initialColor = Color::WHITE;
  Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );

  TestGlAbstraction& gl = application.GetGlAbstraction();

  application.SendNotification();
  application.Render(0);

  Vector4 actualValue(Vector4::ZERO);
  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
  DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );

  // Apply constraint
  Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
  constraint.Apply();
  application.SendNotification();
  application.Render(0);

   // Expect no blue component in either buffer - yellow
  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
  DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );

  application.Render(0);
  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
  DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );

  shader.RemoveConstraints();
  shader.SetProperty(colorIndex, Color::WHITE );
  application.SendNotification();
  application.Render(0);

  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
  DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );

  END_TEST;
}
コード例 #5
0
  void CGSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP<Matrix>& finalP) const {
    // Note: this function matrix notations follow Saad's "Iterative methods", ed. 2, pg. 246
    // So, X is the unknown prolongator, P's are conjugate directions, Z's are preconditioned P's
    PrintMonitor m(*this, "CG iterations");

    if (nIts_ == 0) {
      finalP = MatrixFactory2::BuildCopy(rcpFromRef(P0));
      return;
    }

    RCP<const Matrix>  A         = rcpFromRef(Aref);
    ArrayRCP<const SC> D         = Utilities::GetMatrixDiagonal(*A);
    bool               useTpetra = (A->getRowMap()->lib() == Xpetra::UseTpetra);

    Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2);

    SC one = Teuchos::ScalarTraits<SC>::one();

    RCP<Matrix> X, P, R, Z, AP;
    RCP<Matrix> newX, tmpAP;
#ifndef TWO_ARG_MATRIX_ADD
    RCP<Matrix> newR, newP;
#endif

    SC oldRZ, newRZ, alpha, beta, app;

    // T is used only for projecting onto
    RCP<CrsMatrix> T_ = CrsMatrixFactory::Build(C.GetPattern());
    T_->fillComplete(P0.getDomainMap(), P0.getRangeMap());
    RCP<Matrix>    T = rcp(new CrsMatrixWrap(T_));

    // Initial P0 would only be used for multiplication
    X = rcp_const_cast<Matrix>(rcpFromRef(P0));

    tmpAP = MatrixMatrix::Multiply(*A, false, *X, false, mmfancy, true/*doFillComplete*/, true/*optimizeStorage*/);
    C.Apply(*tmpAP, *T);

    // R_0 = -A*X_0
    R = Xpetra::MatrixFactory2<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(T);

    R->resumeFill();
    R->scale(-one);
    R->fillComplete(R->getDomainMap(), R->getRangeMap());

    // Z_0 = M^{-1}R_0
    Z = Xpetra::MatrixFactory2<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(R);
    Utilities::MyOldScaleMatrix(*Z, D, true, true, false);

    // P_0 = Z_0
    P = Xpetra::MatrixFactory2<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildCopy(Z);

    oldRZ = Utilities::Frobenius(*R, *Z);

    for (size_t i = 0; i < nIts_; i++) {
      // AP = constrain(A*P)
      if (i == 0 || useTpetra) {
        // Construct the MxM pattern from scratch
        // This is done by default for Tpetra as the three argument version requires tmpAP
        // to *not* be locally indexed which defeats the purpose
        // TODO: need a three argument Tpetra version which allows reuse of already fill-completed matrix
        tmpAP = MatrixMatrix::Multiply(*A, false, *P, false,        mmfancy, true/*doFillComplete*/, true/*optimizeStorage*/);
      } else {
        // Reuse the MxM pattern
        tmpAP = MatrixMatrix::Multiply(*A, false, *P, false, tmpAP, mmfancy, true/*doFillComplete*/, true/*optimizeStorage*/);
      }
      C.Apply(*tmpAP, *T);
      AP = T;

      app = Utilities::Frobenius(*AP, *P);
      if (Teuchos::ScalarTraits<SC>::magnitude(app) < Teuchos::ScalarTraits<SC>::sfmin()) {
        // It happens, for instance, if P = 0
        // For example, if we use TentativePFactory for both nonzero pattern and initial guess
        // I think it might also happen because of numerical breakdown, but we don't test for that yet
        if (i == 0)
          X = MatrixFactory2::BuildCopy(rcpFromRef(P0));
        break;
      }

      // alpha = (R_i, Z_i)/(A*P_i, P_i)
      alpha = oldRZ / app;
      this->GetOStream(Runtime1,1) << "alpha = " << alpha << std::endl;

      // X_{i+1} = X_i + alpha*P_i
#ifndef TWO_ARG_MATRIX_ADD
      newX = Teuchos::null;
      MatrixMatrix::TwoMatrixAdd(*P, false, alpha, *X, false, one, newX, mmfancy);
      newX->fillComplete(P0.getDomainMap(), P0.getRangeMap());
      X.swap(newX);
#else
      MatrixMatrix::TwoMatrixAdd(*P, false, alpha, *X, one);
#endif

      if (i == nIts_ - 1)
        break;

      // R_{i+1} = R_i - alpha*A*P_i
#ifndef TWO_ARG_MATRIX_ADD
      newR = Teuchos::null;
      MatrixMatrix::TwoMatrixAdd(*AP, false, -alpha, *R, false, one, newR, mmfancy);
      newR->fillComplete(P0.getDomainMap(), P0.getRangeMap());
      R.swap(newR);
#else
      MatrixMatrix::TwoMatrixAdd(*AP, false, -alpha, *R, one);
#endif

      // Z_{i+1} = M^{-1} R_{i+1}
      Z = MatrixFactory2::BuildCopy(R);
      Utilities::MyOldScaleMatrix(*Z, D, true, true, false);

      // beta = (R_{i+1}, Z_{i+1})/(R_i, Z_i)
      newRZ = Utilities::Frobenius(*R, *Z);
      beta = newRZ / oldRZ;

      // P_{i+1} = Z_{i+1} + beta*P_i
#ifndef TWO_ARG_MATRIX_ADD
      newP = Teuchos::null;
      MatrixMatrix::TwoMatrixAdd(*P, false, beta, *Z, false, one, newP, mmfancy);
      newP->fillComplete(P0.getDomainMap(), P0.getRangeMap());
      P.swap(newP);
#else
      MatrixMatrix::TwoMatrixAdd(*Z, false, one, *P, beta);
#endif

      oldRZ = newRZ;
    }

    finalP = X;
  }
コード例 #6
0
  /**
   * Invoked upon creation of application
   * @param[in] application The application instance
   */
  void Create( Application& application )
  {
    Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);

    mStageSize = Stage::GetCurrent().GetSize();

    // The Init signal is received once (only) during the Application lifetime

    // Hide the indicator bar
    application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );

    // Creates a default view with a default tool bar.
    // The view is added to the stage.
    Toolkit::ToolBar toolBar;
    mContent = DemoHelper::CreateView( application,
                                       mView,
                                       toolBar,
                                       BACKGROUND_IMAGE,
                                       TOOLBAR_IMAGE,
                                       APPLICATION_TITLE );

    mContent.SetLeaveRequired(true);
    mContent.TouchSignal().Connect( this, &ExampleController::OnTouched );

    // Create magnifier (controlled by human touch)
    Layer overlay = Layer::New();
    overlay.SetSensitive(false);
    overlay.SetParentOrigin( ParentOrigin::CENTER );
    overlay.SetSize(mStageSize);
    Stage::GetCurrent().Add(overlay);

    mMagnifier = Toolkit::Magnifier::New();
    mMagnifier.SetSourceActor( mView );
    mMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width );  // Size of magnifier is in relation to stage width
    mMagnifier.SetProperty( Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR, MAGNIFICATION_FACTOR );
    mMagnifier.SetScale(Vector3::ZERO);
    overlay.Add( mMagnifier );

    // Apply constraint to animate the position of the magnifier.
    Constraint constraint = Constraint::New<Vector3>( mMagnifier, Actor::Property::POSITION, ConfinementConstraint(Vector3( 0.5f, 0.5f, 0.0f ), Vector2::ONE * MAGNIFIER_INDENT, Vector2::ONE * MAGNIFIER_INDENT) );
    constraint.AddSource( LocalSource(Actor::Property::SIZE) );
    constraint.AddSource( LocalSource(Actor::Property::PARENT_ORIGIN) );
    constraint.AddSource( LocalSource(Actor::Property::ANCHOR_POINT) );
    constraint.AddSource( ParentSource(Actor::Property::SIZE) );
    constraint.SetRemoveAction(Constraint::Discard);
    constraint.Apply();

    // Create bouncing magnifier automatically bounces around screen.
    mBouncingMagnifier = Toolkit::Magnifier::New();
    mBouncingMagnifier.SetSourceActor( mView );
    mBouncingMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width ); // Size of magnifier is in relation to stage width
    mBouncingMagnifier.SetProperty( Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR, MAGNIFICATION_FACTOR );
    overlay.Add( mBouncingMagnifier );

    mAnimationTimeProperty = mBouncingMagnifier.RegisterProperty("animationTime",  0.0f);
    ContinueAnimation();

    // Apply constraint to animate the position of the magnifier.
    constraint = Constraint::New<Vector3>( mBouncingMagnifier, Actor::Property::POSITION, MagnifierPathConstraint(mStageSize, mStageSize * 0.5f) );
    constraint.AddSource( LocalSource(Actor::Property::SIZE) );
    constraint.AddSource( LocalSource(mAnimationTimeProperty) );
    constraint.Apply();

    // Apply constraint to animate the source of the magnifier.
    constraint = Constraint::New<Vector3>( mBouncingMagnifier, Toolkit::Magnifier::Property::SOURCE_POSITION, MagnifierPathConstraint(mStageSize) );
    constraint.AddSource( LocalSource(Actor::Property::SIZE) );
    constraint.AddSource( LocalSource(mAnimationTimeProperty) );
    constraint.Apply();
  }
コード例 #7
0
ファイル: shadow-view-impl.cpp プロジェクト: mettalla/dali
void ShadowView::OnInitialize()
{
  // root actor to parent all user added actors. Used as source actor for shadow render task.
  mChildrenRoot.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
  mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );

  Vector2 stageSize = Stage::GetCurrent().GetSize();
  mCameraActor = CameraActor::New(stageSize);

  mCameraActor.SetParentOrigin( ParentOrigin::CENTER );

  // Target is constrained to point at the shadow plane origin
  mCameraActor.SetNearClippingPlane( 1.0f );
  mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position
  mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS);
  mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION);


  Property::Map customShader;
  customShader[ "vertex-shader" ] = RENDER_SHADOW_VERTEX_SOURCE;
  customShader[ "fragment-shader" ] = RENDER_SHADOW_FRAGMENT_SOURCE;

  customShader[ "subdivide-grid-x" ] = 20;
  customShader[ "subdivide-grid-y" ] = 20;

  customShader[ "hints" ] = "output-is-transparent";

  mShadowRenderShader[ "shader" ] = customShader;

  // Create render targets needed for rendering from light's point of view
  mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 );

  mOutputImage = FrameBufferImage::New( stageSize.width * 0.5f, stageSize.height * 0.5f, Pixel::RGBA8888 );

  //////////////////////////////////////////////////////
  // Connect to actor tree

  Self().Add( mChildrenRoot );
  Stage::GetCurrent().Add( mCameraActor );

  mBlurFilter.SetRefreshOnDemand(false);
  mBlurFilter.SetInputImage(mSceneFromLightRenderTarget);
  mBlurFilter.SetOutputImage(mOutputImage);
  mBlurFilter.SetSize(stageSize * 0.5f);
  mBlurFilter.SetPixelFormat(Pixel::RGBA8888);

  mBlurRootActor = Actor::New();
  mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" );

  // Turn off inheritance to ensure filter renders properly
  mBlurRootActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
  mBlurRootActor.SetInheritOrientation(false);
  mBlurRootActor.SetInheritScale(false);
  mBlurRootActor.SetColorMode(USE_OWN_COLOR);

  Self().Add(mBlurRootActor);

  mBlurFilter.SetRootActor(mBlurRootActor);
  mBlurFilter.SetBackgroundColor(Vector4::ZERO);

  CustomActor self = Self();
  // Register a property that the user can use to control the blur in the internal object
  mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);

  Constraint blurStrengthConstraint = Constraint::New<float>( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() );
  blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) );
  blurStrengthConstraint.Apply();
}
コード例 #8
0
void GaussianBlurView::OnInitialize()
{
    // root actor to parent all user added actors, needed to allow us to set that subtree as exclusive for our child render task
    mChildrenRoot.SetParentOrigin(ParentOrigin::CENTER);

    //////////////////////////////////////////////////////
    // Create shaders

    // horiz
    std::ostringstream horizFragmentShaderStringStream;
    horizFragmentShaderStringStream << "#define NUM_SAMPLES " << mNumSamples << "\n";
    horizFragmentShaderStringStream << GAUSSIAN_BLUR_FRAGMENT_SOURCE;
    mHorizBlurShader = ShaderEffect::New( "", horizFragmentShaderStringStream.str() );
    // vert
    std::ostringstream vertFragmentShaderStringStream;
    vertFragmentShaderStringStream << "#define NUM_SAMPLES " << mNumSamples << "\n";
    vertFragmentShaderStringStream << GAUSSIAN_BLUR_FRAGMENT_SOURCE;
    mVertBlurShader = ShaderEffect::New( "", vertFragmentShaderStringStream.str() );


    //////////////////////////////////////////////////////
    // Create actors

    // Create an ImageActor for performing a horizontal blur on the texture
    mImageActorHorizBlur = ImageActor::New();
    mImageActorHorizBlur.SetParentOrigin(ParentOrigin::CENTER);
    mImageActorHorizBlur.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
    mImageActorHorizBlur.SetShaderEffect( mHorizBlurShader );

    // Create an ImageActor for performing a vertical blur on the texture
    mImageActorVertBlur = ImageActor::New();
    mImageActorVertBlur.SetParentOrigin(ParentOrigin::CENTER);
    mImageActorVertBlur.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
    mImageActorVertBlur.SetShaderEffect( mVertBlurShader );

    // Register a property that the user can control to fade the blur in / out via the GaussianBlurView object
    mBlurStrengthPropertyIndex = Self().RegisterProperty(GAUSSIAN_BLUR_VIEW_STRENGTH_PROPERTY_NAME, GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH);

    // Create an ImageActor for compositing the blur and the original child actors render
    if(!mBlurUserImage)
    {
        mImageActorComposite = ImageActor::New();
        mImageActorComposite.SetParentOrigin(ParentOrigin::CENTER);
        mImageActorComposite.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
        mImageActorComposite.SetOpacity(GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH); // ensure alpha is enabled for this object and set default value

        Constraint blurStrengthConstraint = Constraint::New<float>( mImageActorComposite, Actor::Property::COLOR_ALPHA, EqualToConstraint());
        blurStrengthConstraint.AddSource( ParentSource(mBlurStrengthPropertyIndex) );
        blurStrengthConstraint.Apply();

        // Create an ImageActor for holding final result, i.e. the blurred image. This will get rendered to screen later, via default / user render task
        mTargetActor = ImageActor::New();
        mTargetActor.SetParentOrigin(ParentOrigin::CENTER);
        mTargetActor.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME


        //////////////////////////////////////////////////////
        // Create cameras for the renders corresponding to the view size
        mRenderFullSizeCamera = CameraActor::New();
        mRenderFullSizeCamera.SetParentOrigin(ParentOrigin::CENTER);


        //////////////////////////////////////////////////////
        // Connect to actor tree
        Self().Add( mImageActorComposite );
        Self().Add( mTargetActor );
        Self().Add( mRenderFullSizeCamera );
    }


    //////////////////////////////////////////////////////
    // Create camera for the renders corresponding to the (potentially downsampled) render targets' size
    mRenderDownsampledCamera = CameraActor::New();
    mRenderDownsampledCamera.SetParentOrigin(ParentOrigin::CENTER);


    //////////////////////////////////////////////////////
    // Connect to actor tree
    Self().Add( mChildrenRoot );
    Self().Add( mImageActorHorizBlur );
    Self().Add( mImageActorVertBlur );
    Self().Add( mRenderDownsampledCamera );
}