Search code examples
c#wpfstoryboardinotifypropertychangedhelix-3d-toolkit

How to detect if a sub-property is changed


I want to animate a ModelVisual3D but I cannot detect when the parent object's frame of reference has changed.

I have a class FrameOfReference which is a 3D coordinate frame using ZYX Euler angles to describe the orientation. This class has a TransformGroup property that reflects the frame's position that gets updated whenever X,Y,Z,A,B, or C is changed. It is very important to me that I interact with the ZYX Euler angles to create transformations, and not a TransformGroup.

I also have a class EOAT ("end of arm tool"), which has properties FrameOfReference and ModelVisual3D. I am trying to get the ModelVisual3D to update if a property of the FrameOfReference has changed.

public class FrameOfReference
{
    private double _x, _y, _z, _a, _b, _c, _legLength;
    private ModelVisual3D _model;
    private Transform3DGroup _transformGroup;

    public FrameOfReference()
    {
        _x = 0;
        _y = 0;
        _z = 0;
        _a = 0;
        _b = 0;
        _c = 0;
    }

    public double X
    {
        get { return _x; }
        set { _x = value; UpdateTransformGroup();}
    }

    public double Y
    {
        get { return _y; }
        set {
            _y = value; UpdateTransformGroup();}
    }

    public double Z
    {
        get { return _z; }
        set { _z = value; UpdateTransformGroup();}
    }

    public double A
    {
        get { return _a; }
        set{ _a = value; UpdateTransformGroup();}
    }

    public double B
    {
        get { return _b; }
        set { _b = value; UpdateTransformGroup();}
    }

    public double C
    {
        get { return _c; }
        set { _c = value; UpdateTransformGroup();}
    }
    public void Transform3DGroup UpdateTransformGroup()
    {
            //convert ZYX Euler angles into a transformgroup
            Transform3DGroup group = new Transform3DGroup();
            Vector3D e = new Vector3D(C, B, A);
            Quaternion q = new Quaternion(new Vector3D(0.0, 0.0, 1.0), e.Z)
                * new Quaternion(new Vector3D(0.0, 1.0, 0.0), e.Y)
                * new Quaternion(new Vector3D(1.0, 0.0, 0.0), e.X);
            AxisAngleRotation3D r = new AxisAngleRotation3D(q.Axis, q.Angle);
            group.Children.Add(new RotateTransform3D(r));
            group.Children.Add(new TranslateTransform3D(X, Y, Z));
            _transformGroup = group;
    }
    public Transform3DGroup TransformGroup
    {
        get
        { return _transformGroup;}
        set { _transformGroup = value;}

    }
}

and my class that is the EOAT and contains the ModelVisual3D and FrameOfReference

class EOATModel
{

    private FrameOfReference _frame;
    private ModelVisual3D _model;

    public EOATModel()
    {
        Frame = new FrameOfReference();
    }

    public EOATModel(FrameOfReference Frame)
    {
        this.Frame = Frame;
    }

    public FrameOfReference Frame
    {
        get
        {
            return _frame;
        }
        set
        {
            _frame = value;
            CreateModel();
        }
    }

    public ModelVisual3D Model
    {
        get
        {
            return _model;
        }
        set
        {
            _model = value;
        }
    }

    public void CreateModel()
    {
        if (_model != null)
        {
            //--a bunch of code that makes the EOAT since its irrelevant--

            _model.Children.Add(EOATCube);
            _model.Transform = Frame.TransformGroup;
        }
    }

}

Some sample code showing how I make the _eoat.

public class Main
{
    EOAT _eoat = new EOAT()
    _eoat.Frame.X = 100;
    _eoat.Frame.Y = 100;
    _eoat.Frame.Z = 100;
    _eoat.Frame.A = 100;
    _eoat.Frame.B = 100;
    _eoat.Frame.C = 100;
    _eoat.Model = new ModelVisual3D();
    viewPort3d.Children.Add(_eoat.Model);
    _eoat.Frame.C = 45;
}

If I enter "_eoat.Frame.C = 45", the FrameOfReference inside _eoat does not detect that the frame has been changed and therefore does not update the model with CreateModel(). I know I could just manually call CreateModel() after I change the frame, but this won't work when it comes time to do animation storyboard stuff, since the animation simply changes a value over time and cannot call CreateModel() during the animation.

In short, how do I get EOAT to automatically call CreateModel() whenever anything inside Frame is changed. I have tried INotifyPropertyChange code, but haven't gotten any of it to work.

Sorry about all the content. I hope this question makes sense. Thank you.


Solution

  • You can simply add an event handler to the NotifyPropertyChanged event of FrameOfReference and call CreateModel from the event handler.

    public EOATModel()
    {
        Frame = new FrameOfReference();
        Frame.PropertyChanged += (o,e) => CreateModel());
    }