Package wolf.city.road

Examples of wolf.city.road.Road


 
  public boolean generateIteration(CityView cv){
   
    if(!rqH.isEmpty()){
      //generate highways, save street seeds to rq
      Road road = rqH.remove();
      if(road.finished){
        //finished
      }else{
        if(city.pop.getCircleAvg((int)road.b.pos.x, (int)road.b.pos.y, populationSampleRadiusMainRoad) >= minimumPopulationMainRoad){
          //seed a main road (these go from highway to highway)
          Road r;
          if(city.random.nextBoolean()){
            r = road.rule.globalGoals(city, road, Direction.LEFT);
          }else{
            r = road.rule.globalGoals(city, road, Direction.RIGHT);
          }
          r.setType(RoadType.MAIN);
          rqM.add(localConstraints(r));

        }else if(city.pop.getCircleAvg((int)road.b.pos.x, (int)road.b.pos.y, populationSampleRadiusHighwayIntersection) >= minimumPopulationHighwayIntersection){
          //determine if this location should have an intersection (or should all locations have an intersection and then prune later?)

          OffRamp rampRule = new OffRamp(city);
          //yes, have an intersection! Free, with your purchase!
          rq.add(localConstraints(rampRule.globalGoals(city, road, Direction.LEFT)));
          rq.add(localConstraints(rampRule.globalGoals(city, road, Direction.RIGHT)));
        }
        if(road.getType() == RoadType.HIGHWAY){
          Basic basicRule = new Basic(city);
          Road newRoad = basicRule.globalGoals(city, road, Direction.FORWARD);
          rqH.add(localConstraints(newRoad));
        }else{
          rqM.add(road);
        }
       
      }
      road = connect(road);
      roads.insert(road.getEnvelope(), road);
      cv.roads.add(road);
      if(cv != null){
        cv.draw();
      }
    }


    //setup for main roads
    //basicRule.turnRateForward = 40;
    //log.log("Main roads generating");
    rqM.stackStyle = true;
    if(!rqM.isEmpty()){
      Road road = rqM.remove();
      if(road.finished){
        //finished
      }else{
        Road r = road.rule.globalGoals(city, road, Direction.FORWARD);
        r.setType(RoadType.MAIN);
        //have an intersection! Free, with your purchase!
        Road inters1 = r.rule.globalGoals(city, road, Direction.LEFT);
        Road inters2 = r.rule.globalGoals(city, road, Direction.RIGHT);
        //set the type to street, give it a proper rule
        inters1.setType(RoadType.STREET);
        inters1.rule = new Grid(city);
        inters2.setType(RoadType.STREET);
        inters2.rule = new Grid(city);

        rq.add(inters1);
        rq.add(inters2);
        rqM.add(localConstraints(r));
      }
     
      road = connect(road);
      roads.insert(road.getEnvelope(), road);
      cv.roads.add(road);
     
      if(cv != null){
        cv.draw();
      }
    }
    //generate streets entirely

    //log.log("Streets generating");
    rq.stackStyle = true;
    if(!rq.isEmpty()){
     
      //generate streets
      if(city.random.nextDouble()>.99){
        rq.stackStyle = false;
      }else{
        rq.stackStyle = true;
      }
      Road road = localConstraints(rq.remove());
      if(road != null){
        if(road.finished){
          //finished
        }else{
          if(road.rule instanceof OffRamp){
            road.rule = new Grid(city);
          }
          if(city.random.nextDouble()>.9){ //makes it look like a modern/whatever neighborhood
            road.rule = road.rule.mutate();
          }
          //use grid pattern to fill in areas between highways (Manhattan-esque pattern, but not perfect)
          if(city.pop.get((int)road.b.pos.x, (int)road.b.pos.y)>minimumPopulation){
            rq.add(localConstraints(road.rule.globalGoals(city, road, Direction.BACKWARD)));
            rq.add(localConstraints(road.rule.globalGoals(city, road, Direction.FORWARD)));
            rq.add(localConstraints(road.rule.globalGoals(city, road, Direction.LEFT)));
            rq.add(localConstraints(road.rule.globalGoals(city, road, Direction.RIGHT)));
          }
          //city.pop.removeDensityLine(road);
        }
        road = connect(road);
        roads.insert(road.getEnvelope(), road);
        cv.roads.add(road);

      }
      if(cv != null){
        cv.draw();
View Full Code Here


  }


  private Road connect(Road road) {
    if(road.intersectedRoad != null){
      Road split = new Road(road.b, road.intersectedRoad.b, road.intersectedRoad.getType(), road.intersectedRoad.rule);
      road.intersectedRoad.b = road.b;
      split.a.addConnecting(road);
      roads.insert(split.getEnvelope(), split);
      cv.roads.add(split);
    }
    road.a.addConnecting(road); //connect intersections
    road.b.addConnecting(road);
    return road;
View Full Code Here

      int blue = 0;
      int alpha = 0;
      int green = 0;
      ArrayList<Road> roadList = (ArrayList<Road>) c.rm.roads.queryAll();
      for(int i=0; i<roadList.size(); i++){
        Road road =roadList.get(i);
        Geometry g = road.getGeometry();
        switch(road.getType()){
        case BRIDGE:{ //grey
          red = 50;
          green = 50;
          blue = 50;
          break;
View Full Code Here

      Coordinate startPoint = new Coordinate(x,y);
      double angle = Math.toDegrees(Angle.angle(startPoint, new Coordinate(0,0)));
      Turtle t = new Turtle(startPoint, angle);
      //t.turn(45);
      t.move(32);
      roadQueue.add(new Road(new Intersection(startPoint), new Intersection(t.pos), RoadType.HIGHWAY, basicRule));
    }
    int highwayCount = Math.max((city.sizeX*2+city.sizeY*2)/(3*1024),1);
    for(int i=0; i<highwayCount; i++){
      //highway from random side
      int direction = Math.abs(city.random.nextInt()%4);
      float length = 128;
      switch(direction){
      case 0:{ //North
        log.log("Highway from the north");
        float x = (float) ((this.city.random.nextFloat()-.5)*city.sizeX); //can place in center half of city
        Coordinate startPoint = new Coordinate(x, city.sizeY/2);
        Turtle t = new Turtle(startPoint, ((city.random.nextDouble()*180)%seedHighwayAngleSize)+270);
        t.move(length);
        roadQueue.add(new Road(new Intersection(startPoint), new Intersection(t.pos), RoadType.HIGHWAY, basicRule));
        break;
      }
      case 1:{ //South
        log.log("Highway from the south");
        float x = (float) ((this.city.random.nextFloat()-.5)*city.sizeX); //can place in center half of city
        Coordinate startPoint = new Coordinate(x, -city.sizeY/2);
        Turtle t = new Turtle(startPoint, ((city.random.nextDouble()*180)%seedHighwayAngleSize)+90);
        t.move(length);
        roadQueue.add(new Road(new Intersection(startPoint), new Intersection(t.pos), RoadType.HIGHWAY, basicRule));
        break;
      }
      case 2:{ //East
        log.log("Highway from the east");
        float y = (float) ((this.city.random.nextFloat()-.5)*city.sizeY);
        Coordinate startPoint = new Coordinate(-city.sizeX/2, y);
        Turtle t = new Turtle(startPoint, ((city.random.nextDouble()*180)%seedHighwayAngleSize)+0);
        t.move(length);
        roadQueue.add(new Road(new Intersection(startPoint), new Intersection(t.pos), RoadType.HIGHWAY, basicRule));
        break;
      }
      case 3:{ //West
        log.log("Highway from the west");
        float y = (float) ((this.city.random.nextFloat()-.5)*city.sizeY);
        Coordinate startPoint = new Coordinate(city.sizeX/2, y);
        Turtle t = new Turtle(startPoint, ((city.random.nextDouble()*180)%seedHighwayAngleSize)+180);
        t.move(length);
        roadQueue.add(new Road(new Intersection(startPoint), new Intersection(t.pos), RoadType.HIGHWAY, basicRule));
        break;
      }
      default:{
        System.out.println("You done goofed. Check the seedRoadMap function's direction variable");
      }
View Full Code Here

    }

    Coordinate point =  r.b.pos;
    ArrayList<Road> roadsLoc = (ArrayList<Road>) roads.query(r.getEnvelope());
    for(int ir=0; ir<roadsLoc.size(); ir++){
      Road i = roadsLoc.get(ir);

      if(i.getType() == RoadType.HIGHWAY && r.getType() == RoadType.STREET){

      }else{
        //doesn't find closest, just finds one and goes with it.
        double distA = r.b.pos.distance(i.a.pos);
        double distB = r.b.pos.distance(i.b.pos);
        if(distA<maximumRoadSnapDistance && distB<maximumRoadSnapDistance){
          if(distA<distB){
            r.b = i.a;
            return r;
          }else{
            r.b = i.b;
            return r;
          }
        }else{
          if(distA<maximumRoadSnapDistance){
            r.b = i.a;
            return r;
          }
          if(distB<maximumRoadSnapDistance){
            r.b = i.b;
            return r;
          }
        }

        //snap to road
        Coordinate closest = i.getLineSegment().closestPoint(point);
        double dist = point.distance(closest);
        if(dist < maximumRoadSnapDistance ){//&& dist > minimumRoadSnapDistance){
          r.b.pos = closest;
          r.intersectedRoad = i;
          return r;
View Full Code Here

      angle = Math.PI + angle; //half circle
    }

    ArrayList<Road> roadList = (ArrayList<Road>) roads.query(r.getEnvelope());
    for(int i=0; i<roadList.size(); i++){
      Road road = roadList.get(i);
      Geometry g1 = road.getGeometry(2).difference(road.getIntersectionGeometry());
      if(g0.intersects(g1) || g0.distance(g1)<4){
        double thisAngle = Angle.angle(road.a.pos, road.b.pos);
        if(thisAngle<0){
          thisAngle = Math.PI + thisAngle; //half circle
        }
View Full Code Here

    double expand = r.width*1f;
    Geometry a = r.getGeometry(expand);
    //check related spaces in grid
    ArrayList<Road> roadList = (ArrayList<Road>) roads.query(r.getEnvelope());
    for(int i=0; i<roadList.size(); i++){
      Road ir = roadList.get(i);
      if(ir.getType() != RoadType.HIGHWAY && ir.getType() != RoadType.MAIN){
        //if(!tested.contains(i) && !((i.getType() == RoadType.HIGHWAY || i.getType() == RoadType.MAIN) && (r.getType() == RoadType.STREET || r.getType() == RoadType.HIGHWAY)) /*streets ignore main and highway types*/){
        Geometry b = ir.getGeometry(expand);
        if(a.intersects(b)){
          Geometry c = a.intersection(b);
          if(c.getArea()>maximumRatioIntersectionArea*a.getArea()){
            return null;
          }
View Full Code Here

      //GL11.glEnable(GL11.GL_BLEND);
      //GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);

      for(int i=0; i<roads.size(); i++){
        Road road = roads.get(i);
        Geometry g = road.getGeometry();

        switch(road.getType()){
        case BRIDGE:{ //grey
          red = 50;
          green = 50;
          blue = 50;
          break;
View Full Code Here

    }

    if(r.getType() == RoadType.MAIN || r.getType() == RoadType.HIGHWAY){
      ArrayList<Road> roadList = (ArrayList<Road>) roads.query(r.getEnvelope());
      for(int i=0; i<roadList.size(); i++){
        Road road = roadList.get(i);

        li.computeIntersection(r.a.pos, r.b.pos, road.a.pos, road.b.pos);
        if(li.hasIntersection()){
          Coordinate intersection = li.getIntersection(0);
          r.intersectedRoad = road;
View Full Code Here

        ArrayList<LineSegment> segments0 = new ArrayList<LineSegment>();
        ArrayList<LineSegment> segments1 = new ArrayList<LineSegment>();
        //compute intersection points and max radius of roads (minimum value for roadExtrusion value)
        double maxRadius = 0;
        for(int j=0; j<is.connecting.size(); j++){
          Road r = is.connecting.get(j);
          double radius = r.width/2;
          if(radius>maxRadius){
            maxRadius = radius;
          }
          //calculate intersection lines
          Turtle t0;
          if(r.a.pos.equals(is.pos)){
            t0 = new Turtle(is.pos, Math.toDegrees(Angle.angle(is.pos, r.b.pos)));
          }else{
            t0 = new Turtle(is.pos, Math.toDegrees(Angle.angle(is.pos, r.a.pos)));
          }
          Turtle t1 = new Turtle(t0.pos, t0.angle);
          //move out to sides of roads
          t0.turn(90);
          t1.turn(-90);
          t0.move(radius);
          t1.move(radius);
          Coordinate t0Start = new Coordinate(t0.pos);
          Coordinate t1Start = new Coordinate(t1.pos);
          //return to first orientation
          t0.turn(-90);
          t1.turn(90);
          t0.move(r.width*6);
          t1.move(r.width*6);
         
          LineSegment ls0 = new LineSegment(t0Start, t0.pos);
          LineSegment ls1 = new LineSegment(t1Start, t1.pos);
          segments0.add(ls0);
          segments1.add(ls1);
        }
        //compute intersections of roads extruded from center of intersection.
        double[] extrusion = new double[is.connecting.size()];
        //double maxDistance = -1;
        for(int j=0; j<is.connecting.size(); j++){
          for(int k=0; k<is.connecting.size(); k++){
            if(j != k){
              Coordinate c0 = segments0.get(j).intersection(segments1.get(k));
              Coordinate c1 = segments1.get(j).intersection(segments0.get(k));
              if(c0 != null){
                double dist = c0.distance(is.pos);
               
                if(dist>extrusion[j]){
                  extrusion[j] = dist;
                  log.log("dist: "+dist+" other");
                }
              }
             
              if(c1 != null){
                double dist = c1.distance(is.pos);
               
                if(dist>extrusion[j]){
                  extrusion[j] = dist;
                }
              }
             
            }
          }
          if(extrusion[j] < maxRadius) extrusion[j] = maxRadius;
          for(int k=0; k<is.connecting.size(); k++){
            if(is.connecting.get(k).a == is){
              is.connecting.get(k).roadExtrusionA = extrusion[k];
            }else{
              is.connecting.get(k).roadExtrusionB = extrusion[k];
            }
          }
        }
        //create final road extrusion
       
        LineSegment[] segments = new LineSegment[is.connecting.size()];
        for(int j=0; j<is.connecting.size(); j++){
          Road r = is.connecting.get(j);
          double radius = r.width/2; //maybe a tad faster to load into variable?
          Turtle t0;
          if(r.a.pos.equals(is.pos)){
            t0 = new Turtle(is.pos, Math.toDegrees(Angle.angle(is.pos, r.b.pos)));
          }else{
            t0 = new Turtle(is.pos, Math.toDegrees(Angle.angle(is.pos, r.a.pos)));
          }
         
          //move out from intersection and split into road width
          t0.move(extrusion[j]);
          t0.turn(90);
          Turtle t1 = new Turtle(t0.pos, t0.angle-180);
          t0.move(radius);
          t1.move(radius);
         
          segments[j] = new LineSegment(t0.pos, t1.pos);
        }
        //need to sort segments by angle order, then use that to generate shape. Convex hull will not work.
       
        double[] angles = new double[segments.length]; //no ring, so no last element
        //calculate all point angles
        for(int j=0; j<angles.length; j++) angles[j] = Math.min(Math.PI*2-Angle.angle(is.pos, segments[j].p0),Math.PI*2-Angle.angle(is.pos, segments[j].p1));
        //selection sort
        for(int j=0; j<angles.length; j++){
          int min = j;
          for(int k=j+1; k<angles.length; k++){
            if(angles[k]<angles[min]){
              min = k; //remember new min
            }
          }
         
          //swap min element with current element
          if(min != j){
            //swap angle array
            double ang = angles[j];
            angles[j] = angles[min];
            angles[min] = ang;
           
            //swap segment array
            LineSegment tempS = segments[j];
            segments[j] = segments[min];
            segments[min] = tempS;
          }
        }
        //close ring
        Coordinate[] points = new Coordinate[is.connecting.size()*2+1];
        for(int j=0; j<segments.length; j++){
          points[j*2] = segments[j].p0;
          points[j*2+1] = segments[j].p1;
        }
        points[points.length-1] = points[0];
        //create ring
        Geometry intersectionShape = gf.createLinearRing(points);
        obj.startObject("intersection_"+this.hashCode());
 
        //convert Coordinate to Vector3f
        Coordinate[] cs = intersectionShape.getCoordinates();
        Vector3f[] verts = new Vector3f[cs.length];
        Vector3f[] vertsz = new Vector3f[cs.length];
 
       
       
       
        float elevation = city.ter.get((int)is.pos.x, (int)is.pos.y);
 
        for(int j=0; j<cs.length; j++){
          verts[cs.length-j-1] = new Vector3f((float)cs[j].x,(float)cs[j].y, elevation);
          vertsz[j] = new Vector3f((float)cs[j].x,(float)cs[j].y, 0);
        }
 
        for(int j=0; j<cs.length; j++){
          obj.face(new Vector3f[]{new Vector3f(verts[j].x, verts[j].y, 0), new Vector3f(verts[(j+1)%cs.length].x, verts[(j+1)%cs.length].y, 0), verts[j]});
          obj.face(new Vector3f[]{new Vector3f(verts[(j+1)%cs.length].x, verts[(j+1)%cs.length].y, 0), verts[(j+1)%cs.length], verts[j]});
        }
 
        obj.face(verts);
        obj.face(vertsz);
        obj.endObject();
      }else{
        //roads that have only 1 intersection
       
      }
    }
    ArrayList<Road> roadList = (ArrayList<Road>) roads.queryAll();
    for(int i=0; i<roadList.size(); i++){
      Road r = roadList.get(i);
      //make rectangle geometry with road ends as terrain height.
      Vector3f a = new Vector3f((float)r.a.pos.x, (float)r.a.pos.y, city.ter.get((int)r.a.pos.x, (int)r.a.pos.y));
      Vector3f b = new Vector3f((float)r.b.pos.x, (float)r.b.pos.y, city.ter.get((int)r.b.pos.x, (int)r.b.pos.y));

      double ang = Angle.angle(r.a.pos, r.b.pos);
View Full Code Here

TOP

Related Classes of wolf.city.road.Road

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.