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,286 +17,311 @@ import org.jzy3d.plot3d.transform.Transform;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.glu.GLU;
/**
* A Composite gathers several Drawable and provides default methods
* for rendering them all in one call.
* <br>
* A Composite gathers several Drawable and provides default methods for
* rendering them all in one call. <br>
*
* @author Martin Pernollet
*
*/
public abstract class AbstractComposite extends AbstractWireframeable implements ISingleColorable, IMultiColorable{
public AbstractComposite(){
super();
components = new ArrayList<AbstractDrawable>();
}
public abstract class AbstractComposite extends AbstractWireframeable implements ISingleColorable, IMultiColorable {
public AbstractComposite() {
super();
components = new ArrayList<AbstractDrawable>();
}
/****************************************************************/
/****************************************************************/
/** Append a list of Drawables to this composite.*/
public void add(List<? extends AbstractDrawable> drawables){
components.addAll(drawables);
}
/** Append a list of Drawables to this composite. */
public void add(List<? extends AbstractDrawable> drawables) {
synchronized (components) {
components.addAll(drawables);
}
}
/** Clear the list of Drawables from this composite.*/
public void clear(){
components.clear();
}
/** Clear the list of Drawables from this composite. */
public void clear() {
synchronized (components) {
components.clear();
}
}
/** Add a Drawable to this composite.*/
public void add(AbstractDrawable drawable){
components.add(drawable);
}
/** Add a Drawable to this composite. */
public void add(AbstractDrawable drawable) {
components.add(drawable);
}
/** Remove a Drawable from this composite.*/
public void remove(AbstractDrawable drawable){
components.remove(drawable);
}
/** Remove a Drawable from this composite. */
public void remove(AbstractDrawable drawable) {
components.remove(drawable);
}
/** Get a Drawable stored by this composite.*/
public AbstractDrawable get(int p){
return components.get(p);
}
/** Get a Drawable stored by this composite. */
public AbstractDrawable get(int p) {
return components.get(p);
}
public List<AbstractDrawable> getDrawables(){
return components;
}
public List<AbstractDrawable> getDrawables() {
return components;
}
/** Return the number of Drawable stored by this composite.*/
public int size(){
return components.size();
}
/** Return the number of Drawable stored by this composite. */
public int size() {
return components.size();
}
/* */
/* */
/** Delegate rendering iteratively to all Drawable of this composite.*/
@Override
public void draw(GL gl, GLU glu, Camera camera){
if(mapper!=null)
mapper.preDraw(this);
/** Delegate rendering iteratively to all Drawable of this composite. */
@Override
public void draw(GL gl, GLU glu, Camera camera) {
if (mapper != null)
mapper.preDraw(this);
synchronized(components){
for(AbstractDrawable c: components){
if(c!=null)
c.draw(gl, glu, camera);
}
}
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null)
c.draw(gl, glu, camera);
}
}
if(mapper!=null)
if (mapper != null)
mapper.postDraw(this);
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.*/
@Override
public void setTransform(Transform transform){
this.transform = transform;
/**
* Delegate transforming iteratively to all Drawable of this composite and
* stores the given transform for keeping the ability of retrieving it.
*/
@Override
public void setTransform(Transform transform) {
this.transform = transform;
synchronized(components){
for(AbstractDrawable c: components){
if(c!=null)
c.setTransform(transform);
}
}
}
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null)
c.setTransform(transform);
}
}
}
@Override
public void setTransformBefore(Transform transform){
@Override
public void setTransformBefore(Transform transform) {
this.transformBefore = transform;
synchronized(components){
for(AbstractDrawable c: components){
if(c!=null)
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null)
c.setTransformBefore(transform);
}
}
}
/** Return the transform that was affected to this composite.*/
@Override
public Transform getTransform(){
return transform;
}
/** Return the transform that was affected to this composite. */
@Override
public Transform getTransform() {
return transform;
}
/** Creates and return a BoundingBox3d that embed all available Drawable bounds.*/
@Override
public BoundingBox3d getBounds(){
updateBounds();
return bbox;
}
/**
* Creates and return a BoundingBox3d that embed all available Drawable
* bounds.
*/
@Override
public BoundingBox3d getBounds() {
updateBounds();
return bbox;
}
@Override
public void updateBounds() {
BoundingBox3d box = new BoundingBox3d();
synchronized(components){
for(AbstractDrawable c: components){
if(c!=null && c.getBounds()!=null)
box.add(c.getBounds());
}
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c.getBounds() != null)
box.add(c.getBounds());
}
}
bbox = box;
bbox = box;
}
@Override
public void applyGeometryTransform(Transform transform){
for(AbstractDrawable c: components){
c.applyGeometryTransform(transform);
public void applyGeometryTransform(Transform transform) {
synchronized (components) {
for (AbstractDrawable c : components) {
c.applyGeometryTransform(transform);
}
}
// updateBounds(); no need, as computed by getBounds()
}
/****************************************************************/
/****************************************************************/
@Override
public void setWireframeColor(Color color) {
super.setWireframeColor(color);
@Override
public void setWireframeColor(Color color){
super.setWireframeColor(color);
if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeColor(color);
}
}
}
}
if(components != null)
for(AbstractDrawable c: components){
if(c!=null && c instanceof AbstractWireframeable)
((AbstractWireframeable)c).setWireframeColor(color);
}
}
@Override
public void setWireframeDisplayed(boolean status) {
super.setWireframeDisplayed(status);
@Override
public void setWireframeDisplayed(boolean status){
super.setWireframeDisplayed(status);
if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeDisplayed(status);
}
}
}
}
if(components != null)
for(AbstractDrawable c: components){
if(c!=null && c instanceof AbstractWireframeable)
((AbstractWireframeable)c).setWireframeDisplayed(status);
}
}
@Override
public void setWireframeWidth(float width) {
super.setWireframeWidth(width);
@Override
public void setWireframeWidth(float width){
super.setWireframeWidth(width);
if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setWireframeWidth(width);
}
}
}
}
if(components != null)
for(AbstractDrawable c: components){
if(c!=null && c instanceof AbstractWireframeable)
((AbstractWireframeable)c).setWireframeWidth(width);
}
}
@Override
public void setFaceDisplayed(boolean status) {
super.setFaceDisplayed(status);
@Override
public void setFaceDisplayed(boolean status){
super.setFaceDisplayed(status);
if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setFaceDisplayed(status);
}
}
}
}
if(components != null)
for(AbstractDrawable c: components){
if(c!=null && c instanceof AbstractWireframeable)
((AbstractWireframeable)c).setFaceDisplayed(status);
}
}
@Override
public void setDisplayed(boolean status) {
super.setDisplayed(status);
@Override
public void setDisplayed(boolean status){
super.setDisplayed(status);
if (components != null) {
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null && c instanceof AbstractWireframeable)
((AbstractWireframeable) c).setDisplayed(status);
}
}
}
}
if(components != null)
for(AbstractDrawable c: components){
if(c!=null && c instanceof AbstractWireframeable)
((AbstractWireframeable)c).setDisplayed(status);
}
}
/****************************************************************/
/****************************************************************/
@Override
public void setColorMapper(ColorMapper mapper) {
this.mapper = mapper;
@Override
public void setColorMapper(ColorMapper mapper){
this.mapper = mapper;
if (components != null) {
if(components != null){
for(AbstractDrawable d: components){
if(d instanceof IMultiColorable)
((IMultiColorable)d).setColorMapper(mapper);
else if(d instanceof ISingleColorable)
((ISingleColorable)d).setColor(mapper.getColor(d.getBarycentre()));
}
synchronized (components) {
for (AbstractDrawable d : components) {
if (d instanceof IMultiColorable)
((IMultiColorable) d).setColorMapper(mapper);
else if (d instanceof ISingleColorable)
((ISingleColorable) d).setColor(mapper.getColor(d.getBarycentre()));
}
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
}
}
}
@Override
public ColorMapper getColorMapper(){
return mapper;
}
@Override
public ColorMapper getColorMapper() {
return mapper;
}
@Override
public void setColor(Color color){
this.color = color;
@Override
public void setColor(Color color) {
this.color = color;
if(components != null){
for(AbstractDrawable d: components)
if(d instanceof ISingleColorable)
((ISingleColorable)d).setColor(color);
if (components != null) {
synchronized (components) {
for (AbstractDrawable d : components)
if (d instanceof ISingleColorable)
((ISingleColorable) d).setColor(color);
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
}
}
fireDrawableChanged(new DrawableChangedEvent(this, DrawableChangedEvent.FIELD_COLOR));
}
}
@Override
public Color getColor() {
return color;
}
@Override
public Color getColor(){
return color;
}
/****************************************************************/
/****************************************************************/
/** Print out information concerning all Drawable of this composite. */
@Override
public String toString() {
return toString(0);
}
/** Print out information concerning all Drawable of this composite.*/
@Override
public String toString(){
return toString(0);
}
@Override
public String toString(int depth) {
String output = Utils.blanks(depth) + "(Composite3d) #elements:" + components.size() + " | isDisplayed=" + isDisplayed();
@Override
public String toString(int depth){
String output = Utils.blanks(depth) + "(Composite3d) #elements:"+ components.size()+ " | isDisplayed=" + isDisplayed();
if (detailedToString) {
int k = 0;
for (AbstractDrawable c : components) {
if (c != null) {
if (c instanceof AbstractComposite)
output += "\n" + ((AbstractComposite) c).toString(depth + 1);
else
output += "\n" + Utils.blanks(depth + 1) + " Composite element[" + (k++) + "]:" + c.toString();
} else
output += Utils.blanks(depth + 1) + "(null)\n";
}
}
if(detailedToString){
int k = 0;
for(AbstractDrawable c: components){
if(c!=null){
if(c instanceof AbstractComposite)
output += "\n" + ((AbstractComposite)c).toString(depth+1);
else
output += "\n" + Utils.blanks(depth+1) + " Composite element["+ (k++) +"]:" + c.toString();
}
else
output += Utils.blanks(depth+1) + "(null)\n";
}
}
return output;
}
return output;
}
public boolean isDetailedToString() {
return detailedToString;
}
public boolean isDetailedToString() {
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.
*/
public void setDetailedToString(boolean detailedToString) {
this.detailedToString = detailedToString;
}
/** 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) {
this.detailedToString = detailedToString;
}
/****************************************************************/
protected List<AbstractDrawable> components = Collections.synchronizedList(new ArrayList<AbstractDrawable>());
protected Transform transform;
/****************************************************************/
protected List<AbstractDrawable> components = Collections.synchronizedList(new ArrayList<AbstractDrawable>());
protected Transform transform;
protected ColorMapper mapper;
protected Color color;
protected boolean detailedToString = false;
protected ColorMapper mapper;
protected Color color;
protected boolean detailedToString = false;
}

View File

@ -6,34 +6,36 @@ import java.util.List;
import org.jzy3d.plot3d.primitives.AbstractComposite;
import org.jzy3d.plot3d.primitives.AbstractDrawable;
public class Decomposition {
public static ArrayList<AbstractDrawable> getDecomposition(List<AbstractDrawable> drawables){
ArrayList<AbstractDrawable> monotypes = new ArrayList<AbstractDrawable>();
public static ArrayList<AbstractDrawable> getDecomposition(List<AbstractDrawable> drawables) {
ArrayList<AbstractDrawable> monotypes = new ArrayList<AbstractDrawable>();
for(AbstractDrawable c: drawables){
if(c!=null && c.isDisplayed()){
if(c instanceof AbstractComposite)
monotypes.addAll(getDecomposition((AbstractComposite)c));
else if(c instanceof AbstractDrawable)
monotypes.add(c);
}
}
return monotypes;
}
for (AbstractDrawable c : drawables) {
if (c != null && c.isDisplayed()) {
if (c instanceof AbstractComposite)
monotypes.addAll(getDecomposition((AbstractComposite) c));
else if (c instanceof AbstractDrawable)
monotypes.add(c);
}
}
return monotypes;
}
/** Recursively expand all monotype Drawables from the given Composite.*/
public static ArrayList<AbstractDrawable> getDecomposition(AbstractComposite input){
ArrayList<AbstractDrawable> selection = new ArrayList<AbstractDrawable>();
/** Recursively expand all monotype Drawables from the given Composite. */
public static ArrayList<AbstractDrawable> getDecomposition(AbstractComposite input) {
ArrayList<AbstractDrawable> selection = new ArrayList<AbstractDrawable>();
for(AbstractDrawable c: input.getDrawables()){
if(c!=null && c.isDisplayed()){
if(c instanceof AbstractComposite)
selection.addAll(getDecomposition((AbstractComposite)c));
else if(c instanceof AbstractDrawable)
selection.add(c);
}
}
return selection;
}
// composite internally make use of synchronisation on its list of child, so we do so
synchronized (input.getDrawables()) {
for (AbstractDrawable c : input.getDrawables()) {
if (c != null && c.isDisplayed()) {
if (c instanceof AbstractComposite)
selection.addAll(getDecomposition((AbstractComposite) c));
else if (c instanceof AbstractDrawable)
selection.add(c);
}
}
}
return selection;
}
}

View File

@ -255,12 +255,12 @@ public class Graph {
public synchronized void setTransform(Transform transform) {
this.transform = transform;
// synchronized(components){
for (AbstractDrawable c : components) {
if (c != null)
c.setTransform(transform);
synchronized (components) {
for (AbstractDrawable c : components) {
if (c != null)
c.setTransform(transform);
}
}
// }
}
/** Return the transform that was affected to this composite. */
@ -280,8 +280,8 @@ public class Graph {
for (AbstractDrawable c : components) {
if (c != null && c.getBounds() != null) {
BoundingBox3d drawableBounds= c.getBounds();
if(!drawableBounds.isReset()){
BoundingBox3d drawableBounds = c.getBounds();
if (!drawableBounds.isReset()) {
box.add(drawableBounds);
}
}
@ -344,8 +344,29 @@ public class Graph {
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 Scene scene;
protected Transform transform;