fixing concurrency issue on incremental delaunay demo

This commit is contained in:
Martin Pernollet 2016-04-14 14:59:27 +02:00
parent 89ebb76871
commit 5cfc459ece
3 changed files with 336 additions and 288 deletions

View File

@ -17,11 +17,9 @@ import org.jzy3d.plot3d.transform.Transform;
import com.jogamp.opengl.GL; import com.jogamp.opengl.GL;
import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.glu.GLU;
/** /**
* A Composite gathers several Drawable and provides default methods * A Composite gathers several Drawable and provides default methods for
* for rendering them all in one call. * rendering them all in one call. <br>
* <br>
* *
* @author Martin Pernollet * @author Martin Pernollet
* *
@ -36,13 +34,17 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
/** Append a list of Drawables to this composite. */ /** Append a list of Drawables to this composite. */
public void add(List<? extends AbstractDrawable> drawables) { public void add(List<? extends AbstractDrawable> drawables) {
synchronized (components) {
components.addAll(drawables); components.addAll(drawables);
} }
}
/** Clear the list of Drawables from this composite. */ /** Clear the list of Drawables from this composite. */
public void clear() { public void clear() {
synchronized (components) {
components.clear(); components.clear();
} }
}
/** Add a Drawable to this composite. */ /** Add a Drawable to this composite. */
public void add(AbstractDrawable drawable) { public void add(AbstractDrawable drawable) {
@ -89,8 +91,10 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
doDrawBounds(gl, glu, camera); doDrawBounds(gl, glu, camera);
} }
/** Delegate transforming iteratively to all Drawable of this composite /**
* and stores the given transform for keeping the ability of retrieving it.*/ * Delegate transforming iteratively to all Drawable of this composite and
* stores the given transform for keeping the ability of retrieving it.
*/
@Override @Override
public void setTransform(Transform transform) { public void setTransform(Transform transform) {
this.transform = transform; this.transform = transform;
@ -121,7 +125,10 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
return transform; return transform;
} }
/** Creates and return a BoundingBox3d that embed all available Drawable bounds.*/ /**
* Creates and return a BoundingBox3d that embed all available Drawable
* bounds.
*/
@Override @Override
public BoundingBox3d getBounds() { public BoundingBox3d getBounds() {
updateBounds(); updateBounds();
@ -143,69 +150,85 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
@Override @Override
public void applyGeometryTransform(Transform transform) { public void applyGeometryTransform(Transform transform) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
c.applyGeometryTransform(transform); c.applyGeometryTransform(transform);
} }
}
// updateBounds(); no need, as computed by getBounds() // updateBounds(); no need, as computed by getBounds()
} }
/****************************************************************/ /****************************************************************/
@Override @Override
public void setWireframeColor(Color color) { public void setWireframeColor(Color color) {
super.setWireframeColor(color); super.setWireframeColor(color);
if(components != null) if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable) if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeColor(color); ((AbstractWireframeable) c).setWireframeColor(color);
} }
} }
}
}
@Override @Override
public void setWireframeDisplayed(boolean status) { public void setWireframeDisplayed(boolean status) {
super.setWireframeDisplayed(status); super.setWireframeDisplayed(status);
if(components != null) if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable) if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeDisplayed(status); ((AbstractWireframeable) c).setWireframeDisplayed(status);
} }
} }
}
}
@Override @Override
public void setWireframeWidth(float width) { public void setWireframeWidth(float width) {
super.setWireframeWidth(width); super.setWireframeWidth(width);
if(components != null) if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable) if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeWidth(width); ((AbstractWireframeable) c).setWireframeWidth(width);
} }
} }
}
}
@Override @Override
public void setFaceDisplayed(boolean status) { public void setFaceDisplayed(boolean status) {
super.setFaceDisplayed(status); super.setFaceDisplayed(status);
if(components != null) if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable) if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setFaceDisplayed(status); ((AbstractWireframeable) c).setFaceDisplayed(status);
} }
} }
}
}
@Override @Override
public void setDisplayed(boolean status) { public void setDisplayed(boolean status) {
super.setDisplayed(status); super.setDisplayed(status);
if(components != null) if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable) if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setDisplayed(status); ((AbstractWireframeable) c).setDisplayed(status);
} }
} }
}
}
/****************************************************************/ /****************************************************************/
@ -214,12 +237,15 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
this.mapper = mapper; this.mapper = mapper;
if (components != null) { if (components != null) {
synchronized (components) {
for (AbstractDrawable d : components) { for (AbstractDrawable d : components) {
if (d instanceof IMultiColorable) if (d instanceof IMultiColorable)
((IMultiColorable) d).setColorMapper(mapper); ((IMultiColorable) d).setColorMapper(mapper);
else if (d instanceof ISingleColorable) else if (d instanceof ISingleColorable)
((ISingleColorable) d).setColor(mapper.getColor(d.getBarycentre())); ((ISingleColorable) d).setColor(mapper.getColor(d.getBarycentre()));
} }
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR)); fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
} }
@ -236,10 +262,11 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
this.color = color; this.color = color;
if (components != null) { if (components != null) {
synchronized (components) {
for (AbstractDrawable d : components) for (AbstractDrawable d : components)
if (d instanceof ISingleColorable) if (d instanceof ISingleColorable)
((ISingleColorable) d).setColor(color); ((ISingleColorable) d).setColor(color);
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR)); fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
} }
} }
@ -269,8 +296,7 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
output += "\n" + ((AbstractComposite) c).toString(depth + 1); output += "\n" + ((AbstractComposite) c).toString(depth + 1);
else else
output += "\n" + Utils.blanks(depth + 1) + " Composite element[" + (k++) + "]:" + c.toString(); output += "\n" + Utils.blanks(depth + 1) + " Composite element[" + (k++) + "]:" + c.toString();
} } else
else
output += Utils.blanks(depth + 1) + "(null)\n"; output += Utils.blanks(depth + 1) + "(null)\n";
} }
} }
@ -282,15 +308,14 @@ public abstract class AbstractComposite extends AbstractWireframeable implements
return detailedToString; return detailedToString;
} }
/** When to true, the {@link toString()} method will give the detail of each element /**
* of this composite object in a tree like layout. * When to true, the {@link toString()} method will give the detail of each
* element of this composite object in a tree like layout.
*/ */
public void setDetailedToString(boolean detailedToString) { public void setDetailedToString(boolean detailedToString) {
this.detailedToString = detailedToString; this.detailedToString = detailedToString;
} }
/****************************************************************/ /****************************************************************/
protected List<AbstractDrawable> components = Collections.synchronizedList(new ArrayList<AbstractDrawable>()); protected List<AbstractDrawable> components = Collections.synchronizedList(new ArrayList<AbstractDrawable>());

View File

@ -6,7 +6,6 @@ import java.util.List;
import org.jzy3d.plot3d.primitives.AbstractComposite; import org.jzy3d.plot3d.primitives.AbstractComposite;
import org.jzy3d.plot3d.primitives.AbstractDrawable; import org.jzy3d.plot3d.primitives.AbstractDrawable;
public class Decomposition { public class Decomposition {
public static ArrayList<AbstractDrawable> getDecomposition(List<AbstractDrawable> drawables) { public static ArrayList<AbstractDrawable> getDecomposition(List<AbstractDrawable> drawables) {
ArrayList<AbstractDrawable> monotypes = new ArrayList<AbstractDrawable>(); ArrayList<AbstractDrawable> monotypes = new ArrayList<AbstractDrawable>();
@ -26,6 +25,8 @@ public class Decomposition {
public static ArrayList<AbstractDrawable> getDecomposition(AbstractComposite input) { public static ArrayList<AbstractDrawable> getDecomposition(AbstractComposite input) {
ArrayList<AbstractDrawable> selection = new ArrayList<AbstractDrawable>(); ArrayList<AbstractDrawable> selection = new ArrayList<AbstractDrawable>();
// composite internally make use of synchronisation on its list of child, so we do so
synchronized (input.getDrawables()) {
for (AbstractDrawable c : input.getDrawables()) { for (AbstractDrawable c : input.getDrawables()) {
if (c != null && c.isDisplayed()) { if (c != null && c.isDisplayed()) {
if (c instanceof AbstractComposite) if (c instanceof AbstractComposite)
@ -34,6 +35,7 @@ public class Decomposition {
selection.add(c); selection.add(c);
} }
} }
}
return selection; return selection;
} }
} }

View File

@ -255,12 +255,12 @@ public class Graph {
public synchronized void setTransform(Transform transform) { public synchronized void setTransform(Transform transform) {
this.transform = transform; this.transform = transform;
// synchronized(components){ synchronized (components) {
for (AbstractDrawable c : components) { for (AbstractDrawable c : components) {
if (c != null) if (c != null)
c.setTransform(transform); c.setTransform(transform);
} }
// } }
} }
/** Return the transform that was affected to this composite. */ /** Return the transform that was affected to this composite. */
@ -344,8 +344,29 @@ public class Graph {
return output; return output;
} }
/* */ /* */
public boolean isSort() {
return sort;
}
/**
* Set sort to false to desactivate decomposition of drawable.
*
* This bypass ranking polygons w.r.t. camera. This will produce visual cue
* if the scene is dynamic (changing the list of polygons or viewpoints).
* @param sort
*/
public void setSort(boolean sort) {
this.sort = sort;
}
public Scene getScene() {
return scene;
}
protected List<AbstractDrawable> components; protected List<AbstractDrawable> components;
protected Scene scene; protected Scene scene;
protected Transform transform; protected Transform transform;