The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings Complex Surface Machining Optimization
Summary Activity SCM

SCM Repository

authorJean-Max Redonnet <jean-max.redonnet@unniv-tlse3.fr>
Mon, 25 May 2020 17:33:19 +0000 (19:33 +0200)
committerJean-Max Redonnet <jean-max.redonnet@unniv-tlse3.fr>
Mon, 25 May 2020 17:33:19 +0000 (19:33 +0200)
Update VtkPoints accordingly

src/org/ica/vtkviewer/model/ObservablePointsList.java [new file with mode: 0644]
src/org/ica/vtkviewer/model/VtkPoints.java

diff --git a/src/org/ica/vtkviewer/model/ObservablePointsList.java b/src/org/ica/vtkviewer/model/ObservablePointsList.java
new file mode 100644 (file)
index 0000000..e5be896
--- /dev/null
@@ -0,0 +1,61 @@
+package org.ica.vtkviewer.model;
+
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeSupport;
+import java.util.ArrayList;
+
+import org.lgmt.dgl.vecmath.Point3d;
+
+public class ObservablePointsList<T extends Point3d> extends ArrayList<T> {
+       private static final long serialVersionUID = -6303675815201345590L;
+       protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
+
+       @Override
+       public boolean add(T point) {
+               boolean result = super.add(point);
+               this.fireAddPointEvent(point);
+               return result;
+       }
+
+       @Override
+       public void add(int index, T point) {
+               super.add(index, point);
+               this.fireAddPointEvent(point);
+       }
+
+       @Override
+       public T remove(int index) {
+               T result = super.remove(index);
+               this.fireRemovePointEvent(result);
+               return result;
+       }
+
+       @Override
+       public boolean remove(Object o) {
+               if (o instanceof Point3d) {
+                       @SuppressWarnings("unchecked")
+                       T p = (T) o;
+                       boolean result = super.remove(o);
+                       this.fireRemovePointEvent(p);
+                       return result;
+               }
+               return false;
+       }
+
+       public void addPropertyChangeListener(PropertyChangeListener listener) {
+               this.pcs.addPropertyChangeListener(listener);
+       }
+
+       public void removePropertyChangeListener(PropertyChangeListener listener) {
+               this.pcs.removePropertyChangeListener(listener);
+       }
+
+       public void fireAddPointEvent(T point) {
+               pcs.firePropertyChange("AddPoint", null, point);
+       }
+
+       public void fireRemovePointEvent(T point) {
+               pcs.firePropertyChange("RemovePoint", null, point);
+       }
+
+}
index 7a27c8f..4b060d9 100644 (file)
@@ -1,6 +1,7 @@
 package org.ica.vtkviewer.model;
 
-import java.util.ArrayList;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 
 import org.lgmt.dgl.vecmath.Point3d;
 
@@ -13,68 +14,60 @@ import vtk.vtkVertexGlyphFilter;
 /**
  * A class to represent a set of Points.
  *
- * <p>
- * For moment, underlying points cannot be observed, reducing significantly the
- * usefulness of this class. Needs to be improved.
- * </p>
- * <p>
- * Should only be used for debuging purpose.
- * </p>
+ * Intended to be used with ObservablePointsList
  * 
  * @author redonnet
  *
  */
-public class VtkPoints extends VtkObject {
+public class VtkPoints extends VtkObject implements PropertyChangeListener {
        private vtkPoints points;
        private double size;
-       private Color4d color;
+       private vtkPolyData data;
+       private vtkPolyDataMapper mapper;
+       private vtkActor actor;
+       private ObservablePointsList<? extends Point3d> pointsList;
+
+       public VtkPoints(ObservablePointsList<? extends Point3d> pointsList, double size, Color4d color) {
+               super(pointsList);
+               this.pointsList = pointsList;
+               this.pointsList.addPropertyChangeListener(this);
+               this.size = size;
+               this.color = color;
 
-       public VtkPoints() {
-               super(new ArrayList<Point3d>());
                points = new vtkPoints();
-               this.size = 6.0;
-               this.color = new Color4d(1.0, 1.0, 1.0);
+               for (Point3d p : pointsList)
+                       points.InsertNextPoint(p.x, p.y, p.z);
 
-               vtkPolyData polyData = new vtkPolyData();
-               polyData.SetPoints(points);
+               data = new vtkPolyData();
+               data.SetPoints(points);
 
                vtkVertexGlyphFilter VGF = new vtkVertexGlyphFilter();
-               VGF.AddInputData(polyData);
+               VGF.AddInputData(data);
                VGF.Update();
 
-               // Create a Mapper and Actor
-               vtkPolyDataMapper mapper = new vtkPolyDataMapper();
+               mapper = new vtkPolyDataMapper();
                mapper.SetInputConnection(VGF.GetOutputPort());
-               vtkActor actor = new vtkActor();
+
+               actor = new vtkActor();
                actor.SetMapper(mapper);
                actor.GetProperty().SetColor(color.toVTK());
-               actor.GetProperty().SetPointSize(size);
+               actor.GetProperty().SetPointSize(this.size);
                actors.add(actor);
        }
 
-       @SuppressWarnings("unchecked")
        public void addPoint(Point3d p) {
-               ((ArrayList<Point3d>) source).add(p);
-               int n = points.GetNumberOfPoints();
-               points.InsertPoint(n, p.x, p.y, p.z);
+               points.InsertNextPoint(p.x, p.y, p.z);
                points.Modified();
-               this.pcs.firePropertyChange("addPoint", points.GetPoint(n - 1), points.GetPoint(n));
-       }
-
-       public double getSize() {
-               return size;
        }
 
-       public void setSize(double size) {
-               this.size = size;
-       }
-
-       public Color4d getColor() {
-               return color;
-       }
-
-       public void setColor(Color4d color) {
-               this.color = color;
+       @Override
+       public void propertyChange(PropertyChangeEvent event) {
+               if (event.getPropertyName() == "AddPoint") {
+                       addPoint((Point3d) event.getNewValue());
+               }
+               if (event.getPropertyName() == "RemovePoint") {
+                       // TODO
+               }
        }
 
 }