Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/constraints
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3153/src/net/sourceforge/bprocessor/model/constraints
Added Files:
OffsetConstraint.java package.html
Log Message:
Moved OffsetConstraint to model.constraints
--- NEW FILE: package.html ---
<body>
Defines the package that contains the constraints in the model
</body>
--- NEW FILE: OffsetConstraint.java ---
//---------------------------------------------------------------------------------
// $Id: OffsetConstraint.java,v 1.1 2006/01/30 15:20:31 henryml Exp $
//
// Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net)
// Released under the Lesser GNU Public License v2.1
//---------------------------------------------------------------------------------
package net.sourceforge.bprocessor.model.constraints;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import net.sourceforge.bprocessor.model.Constraint;
import net.sourceforge.bprocessor.model.CoordinateSystem;
import net.sourceforge.bprocessor.model.Edge;
import net.sourceforge.bprocessor.model.Surface;
import net.sourceforge.bprocessor.model.Vertex;
/**
* @author henryml
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class OffsetConstraint extends Constraint {
/** The offset */
private double offset;
/**
* Constructor for OffsetConstraint
* @param master The master
* @param slave The slave
*/
public OffsetConstraint(Surface master, Surface slave) {
super(master, slave);
updateOffset();
}
/**
* Collect vertices from object
* FIXME: Make reuable by moving somewhere accessible
* @param o The object
* @param vertices The vertices
*/
protected void collect(Object o, Set vertices) {
if (o instanceof Vertex) {
vertices.add(o);
} else if (o instanceof Edge) {
Edge edge = (Edge) o;
vertices.add(edge.getFrom());
vertices.add(edge.getTo());
} else if (o instanceof Surface) {
Surface surface = (Surface) o;
vertices.addAll(surface.getVertices());
if (surface.getHoles() != null) {
Iterator iter = surface.getHoles().iterator();
while (iter.hasNext()) {
Surface current = (Surface) iter.next();
collect(current, vertices);
}
}
}
}
/**
* Update offset
*/
protected void updateOffset() {
Surface master = (Surface) getMaster();
Surface slave = (Surface) getSlave();
CoordinateSystem system = master.coordinateSystem();
Vertex origin = system.origin();
Vertex n = system.getN();
n.scale(1 / n.length());
Vertex v = slave.getFirstVertex();
Vertex u = v.minus(origin);
double distance = n.dot(u);
this.offset = distance;
}
/**
* Maintain offset
*/
protected void maintainOffset() {
Surface master = (Surface) getMaster();
Surface slave = (Surface) getSlave();
CoordinateSystem system = master.coordinateSystem();
Vertex origin = system.origin();
Vertex n = system.getN();
n.scale(1 / n.length());
Set vertices = new HashSet();
// List vertices = slave.getVertices();
// vertices.remove(0);
collect(slave, vertices);
Iterator iter = vertices.iterator();
while (iter.hasNext()) {
Vertex current = (Vertex) iter.next();
Vertex p = system.translate(current);
p.setZ(offset);
p = system.unTranslate(p);
current.setX(p.getX());
current.setY(p.getY());
current.setZ(p.getZ());
}
}
/**
* Update
* @param entity The changed entity
*/
public void update(Object entity) {
if (entity == getSlave()) {
updateOffset();
} else {
maintainOffset();
}
}
}
|