[Bprocessor-commit] bprocessor/src/net/sourceforge/bprocessor/packages/physics PhysicsSurface.java
Status: Pre-Alpha
Brought to you by:
henryml
Update of /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics In directory vz-cvs-4.sog:/tmp/cvs-serv31827/src/net/sourceforge/bprocessor/packages/physics Modified Files: PhysicsSurface.java PhysicsNet.java PhysicsSpring.java PhysicsParticle.java PhysicsPackage.java Log Message: bending stiffness added to physics Index: PhysicsParticle.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics/PhysicsParticle.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PhysicsParticle.java 28 Mar 2012 07:48:35 -0000 1.2 --- PhysicsParticle.java 4 Apr 2012 09:16:21 -0000 1.3 *************** *** 7,10 **** --- 7,13 ---- public class PhysicsParticle { + + //display guides + boolean displayMovementGuides = false; //properties *************** *** 93,98 **** //vel = vel.scale(-1); //a = a.scale(-1); ! Edge eA = new Edge(firstPos,nPos); ! guides.add(eA); vel = pcs.reflect(vel); a = pcs.reflect(a); --- 96,103 ---- //vel = vel.scale(-1); //a = a.scale(-1); ! if (displayMovementGuides) { ! Edge eA = new Edge(firstPos,nPos); ! guides.add(eA); ! } vel = pcs.reflect(vel); a = pcs.reflect(a); Index: PhysicsPackage.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics/PhysicsPackage.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** PhysicsPackage.java 28 Mar 2012 07:48:35 -0000 1.12 --- PhysicsPackage.java 4 Apr 2012 09:16:21 -0000 1.13 *************** *** 35,38 **** --- 35,39 ---- import net.sourceforge.bprocessor.model.Geometric; import net.sourceforge.bprocessor.model.Item; + import net.sourceforge.bprocessor.model.Material; import net.sourceforge.bprocessor.model.Operation; import net.sourceforge.bprocessor.model.Space; *************** *** 62,65 **** --- 63,69 ---- boolean firstTime = false; + + Material m = new Material(); + Material[] matGR= new Material[511]; HashMap<Edge, Edge> edges = new HashMap(); *************** *** 99,103 **** private DoubleValue restLengthFactor = new DoubleValue(1.0); ! public DoubleValue getRestLengthFactor() { return restLengthFactor; --- 103,107 ---- private DoubleValue restLengthFactor = new DoubleValue(1.0); ! double oldRestlengthFactor = 1.0; public DoubleValue getRestLengthFactor() { return restLengthFactor; *************** *** 165,168 **** --- 169,184 ---- } + private DoubleValue bendingStiffness = new DoubleValue(0); + double oldBendingStiffness = 0; + public DoubleValue getBendingStiffness() { + return bendingStiffness; + } + + private DoubleValue circularForce = new DoubleValue(0); + double oldCircularForce = 0; + public DoubleValue getCircularForce() { + return circularForce; + } + private BooleanValue collisionON = new BooleanValue(true); *************** *** 170,176 **** return collisionON; } public void initialize(Space net) { ! super.initializeNoItem(net); // create copy of b-Net --- 186,251 ---- return collisionON; } + + public void setMaterialToPlanarity() { + Collection<Surface> nSurfaces = cNet.getSurfaces(); + for (Surface s : nSurfaces) { + double dist = 0; + double length = 0; + s.calcNormals(); + PhysicsPlane pp = new PhysicsPlane(s.normal(),s.center()); + Collection<Vertex> sVertices = s.getVertices(); + for (Vertex v : sVertices) { + Vertex d = pp.PointNormalToPlane(v); + if (d.length() > dist) { + dist = d.length(); + } + } + Collection<Edge> sEdges = s.getEdges(); + for (Edge e : sEdges) { + Vertex a = e.getFrom(); + Vertex b = e.getTo(); + double l = a.distance(b); + if (l>length) { + length = l; + } + } + long i = Math.round(5*510*dist/length); + if (i>510) { + i = 510; + } + //Material m = Project.getInstance().findMaterial("materialRG-"+i); + Material m = matGR[(int)i]; + s.setFrontMaterial(m); + s.setBackMaterial(m); + } + } + public void createMaterials() { + for (int i = 0; i <=255; i ++) { + matGR[i] = Project.getInstance().findMaterial("materialRG-"+i); + if (matGR[i] == null) { + matGR[i] = new Material(); + matGR[i].setName("materialRG-"+i); + float[] diffuseColor = new float[]{i/255f, 1.0f, 0.0f}; + matGR[i].setColor(diffuseColor); + Project.getInstance().add(matGR[i]); + } + } + for (int i = 255; i >=0; i --) { + matGR[510-i] = Project.getInstance().findMaterial("materialRG-"+(510-i)); + if (matGR[510-i] == null) { + matGR[510-i] = new Material(); + matGR[510-i].setName("materialRG-"+(510-i)); + float[] diffuseColor = new float[]{1.0f, i/255f, 0.0f}; + matGR[510-i].setColor(diffuseColor); + Project.getInstance().add(matGR[510-i]); + } + } + } + public void initialize(Space net) { ! // create Materials ! createMaterials(); ! //############################################################### super.initializeNoItem(net); // create copy of b-Net *************** *** 179,182 **** --- 254,258 ---- cNet = net.copy(map); cNet.setName(net.getName() + " - Physics Net"); + setMaterialToPlanarity(); // cNet.edit(); // copy corner points *************** *** 204,208 **** pN = new PhysicsNet(cNet, gravity.getValue(), springConstant.getValue(), cDamp.getValue(), pDamp.getValue(), mass.getValue(), restLengthFactor ! .getValue()); pN.toolFactor = factor.getValue(); pN.selectionDistance = selectionDistance.getValue(); --- 280,284 ---- pN = new PhysicsNet(cNet, gravity.getValue(), springConstant.getValue(), cDamp.getValue(), pDamp.getValue(), mass.getValue(), restLengthFactor ! .getValue(),bendingStiffness.getValue()); pN.toolFactor = factor.getValue(); pN.selectionDistance = selectionDistance.getValue(); *************** *** 214,217 **** --- 290,295 ---- // find other elements for collision detection + // search for other surfaces as well or only at main level? + // ############################################################## Space top = Project.getInstance().world(); //get spaces...top.getElements(); *************** *** 222,227 **** pN.addCollisionSurface(s); } - System.out.print("so many surfaces "); - System.out.println(aSurf.size()); } --- 300,303 ---- *************** *** 248,251 **** --- 324,332 ---- } oldSpringConstant = springConstant.getValue(); + //bending stiffness + if (oldBendingStiffness != bendingStiffness.getValue()) { + pN.updateBendingStiffness(bendingStiffness.getValue()); + } + oldBendingStiffness = bendingStiffness.getValue(); // tool Factor pN.toolFactor = factor.getValue(); *************** *** 273,276 **** --- 354,361 ---- pN.setRestLengthFactor(edges.get(bE), restLengthFactor.getValue()); } + if (oldRestlengthFactor != restLengthFactor.getValue()) { + pN.updateBendingStiffnessSprings(restLengthFactor.getValue()); + oldRestlengthFactor = restLengthFactor.getValue(); + } // add a union space containing all new elements *************** *** 289,298 **** // change p-net ! pN.update(guides, timeStep.getValue(), equalConstant.getValue(), ! equalDamping.getValue(), rectConstant.getValue(), rectDamping ! .getValue(), planeConstant.getValue(), planeDamping.getValue(), ! volume.getValue()); union.add(cNet); - } } --- 374,381 ---- // change p-net ! pN.update(guides, timeStep.getValue(), equalConstant.getValue(), equalDamping.getValue(), rectConstant.getValue(), rectDamping.getValue(), planeConstant.getValue(), planeDamping.getValue(), ! volume.getValue(), bendingStiffness.getValue(), circularForce.getValue()); ! setMaterialToPlanarity(); union.add(cNet); } } *************** *** 412,419 **** Vertex bV = (Vertex) g; double bDist = bV.distance(pN.sOrigin); ! pN ! .scaleParticleMass( ! bV, ! 1 + (((pN.selectionDistance - bDist) / pN.selectionDistance) * pN.toolFactor)); } } --- 495,499 ---- Vertex bV = (Vertex) g; double bDist = bV.distance(pN.sOrigin); ! pN.scaleParticleMass(bV,1 + (((pN.selectionDistance - bDist) / pN.selectionDistance) * pN.toolFactor)); } } *************** *** 568,571 **** --- 648,661 ---- content.add(labelled.row()); } + { + SliderControl control = new SliderControl(0, 100, skeleton.getBendingStiffness(), this); + Label labelled = new Label("Bending Stiffness", control); + content.add(labelled.row()); + } + { + SliderControl control = new SliderControl(0, 100, skeleton.getCircularForce(), this); + Label labelled = new Label("Circular Mesh", control); + content.add(labelled.row()); + } /* * { SliderControl control = new SliderControl(0, 50, Index: PhysicsSpring.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics/PhysicsSpring.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PhysicsSpring.java 16 May 2011 06:11:05 -0000 1.1 --- PhysicsSpring.java 4 Apr 2012 09:16:21 -0000 1.2 *************** *** 6,9 **** --- 6,12 ---- public class PhysicsSpring { + //display guides + boolean displayBendingStiffness = false; + //properties PhysicsParticle pA; //Particle at one end of the spring *************** *** 39,42 **** --- 42,46 ---- sf_damped = 0; restLengthScale = 1.0; + restLengthFactor = 1.0; } Index: PhysicsSurface.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics/PhysicsSurface.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PhysicsSurface.java 16 May 2011 06:11:05 -0000 1.1 --- PhysicsSurface.java 4 Apr 2012 09:16:21 -0000 1.2 *************** *** 15,18 **** --- 15,25 ---- public class PhysicsSurface { + //diplay guides + boolean displayNormalForce = false; + boolean displayPlanarization = false; + boolean displayDiagonals = false; + boolean displayCircularMesh = true; + + //properties Surface bSurf; //the original bNet Surface PhysicsPlane avPlane; //average Plane of Surface *************** *** 22,25 **** --- 29,33 ---- double lastLength; double lastDiaLength; + double lastCircRad; //constructors *************** *** 44,50 **** --- 52,67 ---- } lastDiaLength = dl; + //calculate plane distance for each particle for (Vertex v : vertices) { lastPlaneDist.put(v,new Double(0)); } + //calculate current circular raidus + double cl = 0.0; + Vertex center = bSurf.center(); + for (Vertex v : vertices) { + cl = cl + center.distance(v); + } + cl = cl / vertices.size(); + lastCircRad = cl; } *************** *** 86,118 **** bSurf.calcNormals(); return bSurf.normal(); - /* - //* @Michael: algorithm for Normal seems to be so complicated... why? - Vertex nV = new Vertex(0,0,0); - List<Vertex> vertices = bSurf.getVertices(); - double num = vertices.size(); - if (num>3) { - //if surface has more than 3 vertices - //add first two vertices to the end of the list for loop - vertices.add(vertices.get(0)); - vertices.add(vertices.get(1)); - for (int i=0; i<num; i++) { - Vertex a = vertices.get(0).minus(vertices.get(1)); - Vertex b = vertices.get(2).minus(vertices.get(1)); - Vertex n = a.cross(b); - n.normalize(); - nV = nV.add(n); - } - nV.normalize(); - } - if (num == 3) { - //surface is triangular - there is only one normal - Vertex a = vertices.get(0).minus(vertices.get(1)); - Vertex b = vertices.get(2).minus(vertices.get(1)); - nV = nV.add(a.cross(b)); - //nV = nV.scale(1/3); - nV.normalize(); - } - return nV; - */ } --- 103,106 ---- *************** *** 146,151 **** //display force vector Vertex c = bSurf.center(); ! Edge eA = new Edge(c,c.add(n)); ! guides.add(eA); for (Vertex v : vertices) { //get corresponding Particle --- 134,141 ---- //display force vector Vertex c = bSurf.center(); ! if (displayNormalForce) { ! Edge eA = new Edge(c,c.add(n)); ! guides.add(eA); ! } for (Vertex v : vertices) { //get corresponding Particle *************** *** 177,186 **** pA.addForce(nV); //display force vector ! Edge eA = new Edge(v,avPlane.PointProjectToPlane(v)); ! guides.add(eA); } } } void quadToRect(Space guides, double time, double spring_constant, double spring_damping) { //make a quad surface rectangular by equalizing the length of both diagonals --- 167,179 ---- pA.addForce(nV); //display force vector ! if (displayPlanarization) { ! Edge eA = new Edge(v,avPlane.PointProjectToPlane(v)); ! guides.add(eA); ! } } } } + void quadToRect(Space guides, double time, double spring_constant, double spring_damping) { //make a quad surface rectangular by equalizing the length of both diagonals *************** *** 191,199 **** ArrayList<Edge> dEdges = new ArrayList<Edge>(); Edge eA = new Edge(vertices.get(0), vertices.get(2)); - guides.add(eA); dEdges.add(eA); Edge eB = new Edge(vertices.get(1), vertices.get(3)); - guides.add(eB); dEdges.add(eB); //get average diagonal length double dl = (eA.getLength() + eB.getLength()) / 2; --- 184,194 ---- ArrayList<Edge> dEdges = new ArrayList<Edge>(); Edge eA = new Edge(vertices.get(0), vertices.get(2)); dEdges.add(eA); Edge eB = new Edge(vertices.get(1), vertices.get(3)); dEdges.add(eB); + if (displayDiagonals) { + guides.add(eA); + guides.add(eB); + } //get average diagonal length double dl = (eA.getLength() + eB.getLength()) / 2; *************** *** 224,228 **** --- 219,260 ---- } } + } + + void circularSurface(Space guides, double time, double spring_constant, double spring_damping) { + Vertex center = bSurf.center(); + List<Vertex> vertices = bSurf.getVertices(); + //desired length of "radius" center - vertex + double dl = 0.0; + //create List of Edges from center to each Vertex + ArrayList<Edge> cEdges = new ArrayList<Edge>(); + for (Vertex v : vertices) { + Edge e = new Edge(center, v); + cEdges.add(e); + if (displayCircularMesh) { + guides.add(e); + } + dl = dl + e.getLength(); + } + dl = dl / vertices.size(); + //calculate force on vertices + for (Edge e : cEdges) { + //calculate force on particles using spring equation incl. damping + double sf = (dl-e.getLength())*spring_constant; + sf = sf + ((dl-lastCircRad)*spring_damping / time); + lastCircRad = dl; + + //get Vector from A to B + Vertex fB = e.getTo().minus(e.getFrom()); + + //normalize force Vector + fB.normalize(); + //scale force Vectors to force value + fB.scaleIt(sf); + + //add forces to corresponding particles + PhysicsParticle pB = particles.get(e.getTo()); + pB.addForce(fB); + } } } Index: PhysicsNet.java =================================================================== RCS file: /cvsroot/bprocessor/bprocessor/src/net/sourceforge/bprocessor/packages/physics/PhysicsNet.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PhysicsNet.java 28 Mar 2012 07:48:35 -0000 1.5 --- PhysicsNet.java 4 Apr 2012 09:16:21 -0000 1.6 *************** *** 12,15 **** --- 12,18 ---- public class PhysicsNet { + //display guide lines + boolean displayBendingStiffness = false; + //properties Space cNet; *************** *** 27,35 **** HashMap<Edge,PhysicsSpring> springs = new HashMap(); HashMap<Surface, PhysicsSurface> surfaces = new HashMap(); //Collision Detection List<PhysicsCollisionSurface> collisionSurfaces = new ArrayList<PhysicsCollisionSurface>(); //constructor ! PhysicsNet(Space cNet, double gravity, double spring_constant, double spring_damping, double particle_damping, double mass, double restLengthFactor) { Map pMap = new HashMap(); globalParticleMass = mass; --- 30,43 ---- HashMap<Edge,PhysicsSpring> springs = new HashMap(); HashMap<Surface, PhysicsSurface> surfaces = new HashMap(); + HashMap<Vertex, PhysicsParticle> particlesBS = new HashMap(); //Collision Detection List<PhysicsCollisionSurface> collisionSurfaces = new ArrayList<PhysicsCollisionSurface>(); + //Bending Stiffness + List<PhysicsParticle> bendingStiffnessParticles = new ArrayList<PhysicsParticle>(); + List<PhysicsSpring> bendingStiffnessSprings = new ArrayList<PhysicsSpring>(); + //constructor ! PhysicsNet(Space cNet, double gravity, double spring_constant, double spring_damping, double particle_damping, double mass, double restLengthFactor, double bendingStiffness) { Map pMap = new HashMap(); globalParticleMass = mass; *************** *** 61,65 **** pA.connectSpring(pS); pB.connectSpring(pS); ! counter++;; saveSpringParameter(bE,pS); } --- 69,73 ---- pA.connectSpring(pS); pB.connectSpring(pS); ! counter++; saveSpringParameter(bE,pS); } *************** *** 86,89 **** --- 94,192 ---- surfaces.put(bS, pS); } + //create Particles and Springs for bending stiffness + bVertices = cNet.getVertices(); + for (Vertex v : bVertices){ + //create Particles + //find average Normal for all surrounding surfaces + Collection<Surface> vSurfaces = v.getSurfaces(); + Vertex normal = new Vertex(0,0,0); + for (Surface s : vSurfaces) { + s.calcNormals(); + normal = normal.add(s.normal()); + } + double l = 0; + Collection<Edge> vEdges = v.getEdges(); + for (Edge e : vEdges) { + l = l + e.getLength(); + } + l = l / vEdges.size(); + normal.normalize(); + normal = normal.scale(l); + Vertex pA = v.add(normal); + Vertex pB = v.minus(normal); + PhysicsParticle ppA = new PhysicsParticle(pA.copy(), new Vertex(0,0,0), new Vertex(0,0,0), mass, counter); + ppA.fix = false; + ppA.pos.setCorner(false); + counter++; + PhysicsParticle ppB = new PhysicsParticle(pB.copy(), new Vertex(0,0,0), new Vertex(0,0,0), mass, counter); + ppB.fix = false; + ppB.pos.setCorner(false); + counter++; + bendingStiffnessParticles.add(ppA); + bendingStiffnessParticles.add(ppB); + //add to Hashmap to move particle when no bending stiffness in place + particlesBS.put(v, ppA); + particlesBS.put(v, ppB); + //create according springs + PhysicsParticle ppC = particles.get(v); + PhysicsSpring ps = new PhysicsSpring(ppA,ppC,l,bendingStiffness); + bendingStiffnessSprings.add(ps); + ppA.connectSpring(ps); + ppC.connectSpring(ps); + ps = new PhysicsSpring(ppB,ppC,l,bendingStiffness); + bendingStiffnessSprings.add(ps); + ppB.connectSpring(ps); + ppC.connectSpring(ps); + for (Edge e : vEdges) { + Vertex pC = e.getFrom(); + if (pC.equalEps(v)) { + pC = e.getTo(); + } + ppC = particles.get(pC); + ps = new PhysicsSpring(ppA,ppC,pC.distance(pA),bendingStiffness); + bendingStiffnessSprings.add(ps); + ppA.connectSpring(ps); + ppC.connectSpring(ps); + ps = new PhysicsSpring(ppB,ppC,pC.distance(pB),bendingStiffness); + bendingStiffnessSprings.add(ps); + ppB.connectSpring(ps); + ppC.connectSpring(ps); + } + } + } + + public void repositionBendingStiffnessParticles() { + Iterator it = particlesBS.entrySet().iterator(); + long counter = 0; + while (it.hasNext()) { + Map.Entry ee = (Map.Entry)it.next(); + PhysicsParticle pp = (PhysicsParticle)ee.getValue(); + Vertex v = (Vertex)ee.getKey(); + double mult = 1.0; + if (counter%2==1) { + mult = -1.0; + } + //find average Normal for all surrounding surfaces + Collection<Surface> vSurfaces = v.getSurfaces(); + Vertex normal = new Vertex(0,0,0); + for (Surface s : vSurfaces) { + s.calcNormals(); + normal = normal.add(s.normal()); + } + double l = 0; + Collection<Edge> vEdges = v.getEdges(); + for (Edge e : vEdges) { + l = l + e.getLength(); + } + l = l / vEdges.size(); + l = l * mult; + normal.normalize(); + normal = normal.scale(l); + Vertex pA = v.add(normal); + pp.pos = pA.copy(); + pp.nPos = pA.copy(); + pp.lastPos = pA.copy(); + counter ++; + } } *************** *** 192,196 **** } ! void update(Space guides, double time, double equalConstant, double equalDamping, double rectConstant, double rectDamping, double planeConstant, double planeDamping, double volume) { Iterator it; //calculate surface forces --- 295,311 ---- } ! void updateBendingStiffnessSprings(double restLengthFactor) { ! for (PhysicsSpring pS : bendingStiffnessSprings) { ! pS.restLengthFactor = restLengthFactor; ! } ! } ! ! void updateBendingStiffness(double bendingStiffness) { ! for (PhysicsSpring pS : bendingStiffnessSprings) { ! pS.c = bendingStiffness; ! } ! } ! ! void update(Space guides, double time, double equalConstant, double equalDamping, double rectConstant, double rectDamping, double planeConstant, double planeDamping, double volume, double bendingStiffness, double circularForce) { Iterator it; //calculate surface forces *************** *** 203,206 **** --- 318,322 ---- s.quadToRect(guides, time, rectConstant, rectDamping); s.planarize(guides, time, planeConstant, planeDamping); + s.circularSurface(guides, time, circularForce, rectDamping); if ((volume>0) && (solid)) { double normalForce = s.calcArea()*volume/currentVolume; *************** *** 216,219 **** --- 332,344 ---- p.move(time, gravity, particle_d,collisionSurfaces,collisionOn,guides); } + //calculate particle movement for bending stiffness + if (bendingStiffness!=0) { + for (PhysicsParticle pp : bendingStiffnessParticles) { + pp.move(time, 0, particle_d,collisionSurfaces,false,guides); + pp.pos = pp.nPos.copy(); + } + } else { + repositionBendingStiffnessParticles(); + } //move nodes of b-net to particle positions it = particles.entrySet().iterator(); *************** *** 234,237 **** --- 359,370 ---- saveSpringParameter(e,s); } + //update springs for bending stiffness + for (PhysicsSpring pS : bendingStiffnessSprings) { + pS.update(time, spring_d); + if (displayBendingStiffness) { + Edge eB = new Edge(pS.pA.pos, pS.pB.pos); + guides.add(eB); + } + } cNet.update(); } |