Example #1
0
    /**
     * Locate the triangle with point inside it or on its boundary.
     *
     * @param point the point to locate
     * @return the triangle that holds point; null if no such triangle
     */
    public Triangle locate(Pnt point) {
      Triangle triangle = mostRecent;
      if (!this.contains(triangle)) triangle = null;

      // Try a directed walk (this works fine in 2D, but can
      // fail in 3D)
      Set<Triangle> visited = new HashSet<Triangle>();
      while (triangle != null) {
        if (visited.contains(triangle)) { // This should
          // never
          // happen
          ; // System.out.println("Warning: Caught in a locate loop");
          break;
        }
        visited.add(triangle);
        // Corner opposite point
        Pnt corner = point.isOutside(triangle.toArray(new Pnt[0]));
        if (corner == null) return triangle;
        triangle = this.neighborOpposite(corner, triangle);
      }
      // No luck; try brute force
      ; // System.out.println("Warning: Checking all triangles for " + point);
      for (Triangle tri : this) {
        if (point.isOutside(tri.toArray(new Pnt[0])) == null) return tri;
      }
      // No such triangle
      ; // System.out.println("Warning: No triangle holds " + point);
      return null;
    }
Example #2
0
 /**
  * Report neighbor opposite the given vertex of triangle.
  *
  * @param site a vertex of triangle
  * @param triangle we want the neighbor of this triangle
  * @return the neighbor opposite site in triangle; null if none
  * @throws IllegalArgumentException if site is not in this triangle
  */
 public Triangle neighborOpposite(Pnt site, Triangle triangle) {
   if (!triangle.contains(site))
     throw new IllegalArgumentException("Bad vertex; not in triangle");
   for (Triangle neighbor : triGraph.neighbors(triangle)) {
     if (!neighbor.contains(site)) return neighbor;
   }
   return null;
 }
Example #3
0
  public static void main(String args[]) {

    Scanner s = new Scanner(System.in);

    Point x = new Point();

    Triangle t = new Triangle();

    LineSegment l = new LineSegment();

    /*System.out.println("Enter Tester Point Values:");
    	x.setFromUser();
    	System.out.println("Enter Tester Triangle Values:");
    	t.setFromUser();
    	System.out.println("Enter Tester Line Values:");
    	l.setFromUser();
    */
    boolean exit = false;

    while (!exit) {
      System.out.println("Check for point or line is in a triangle or want to exit (1/2/3)");
      int choice = s.nextInt();

      if (choice == 1) {
        x.setFromUser();

        System.out.println("Check if is inside line or triangle or exit(1/2/3)");
        int secondchoice = s.nextInt();

        if (secondchoice == 1) {
          l.setFromUser();
          System.out.println(l.isInside(x));
        } else if (secondchoice == 2) {
          t.setFromUser();
          System.out.println(t.isInside(x));
        } else if (secondchoice == 3) {
          exit = true;
        }
      } else if (choice == 2) {
        l.setFromUser();

        System.out.println("Check if is inside triangle or exit(1/2)");
        int secondchoice = s.nextInt();

        if (secondchoice == 1) {
          t.setFromUser();
          System.out.println(t.isInside(x));
        } else if (secondchoice == 2) {
          exit = true;
        }
      } else if (choice == 3) {
        exit = true;
      }
    }
  }
Example #4
0
 public Pnt[] getContourForSite(Pnt sitex) {
   HashSet<Pnt> done = new HashSet<Pnt>(initialTriangle);
   for (Triangle triangle : dt)
     for (Pnt site : triangle) {
       if (done.contains(site)) continue;
       done.add(site);
       List<Triangle> list = dt.surroundingTriangles(site, triangle);
       Pnt[] vertices = new Pnt[list.size()];
       int i = 0;
       for (Triangle tri : list) vertices[i++] = tri.getCircumcenter();
       // draw(vertices, withFill ? getColor(site) :
       // null);
       if (site == sitex) return vertices;
     }
   return null;
 }
Example #5
0
    /**
     * Place a new site into the DT. Nothing happens if the site matches an existing DT vertex.
     *
     * @param site the new Pnt
     * @throws IllegalArgumentException if site does not lie in any triangle
     */
    public void delaunayPlace(Pnt site) {
      // Uses straightforward scheme rather than best
      // asymptotic time

      // Locate containing triangle
      Triangle triangle = locate(site);
      // Give up if no containing triangle or if site is
      // already in DT
      if (triangle == null) throw new IllegalArgumentException("No containing triangle");
      if (triangle.contains(site)) return;

      // Determine the cavity and update the triangulation
      Set<Triangle> cavity = getCavity(site, triangle);

      lastCavity = cavity;

      mostRecent = update(site, cavity);
    }
Example #6
0
    /**
     * Update the triangulation by removing the cavity triangles and then filling the cavity with
     * new triangles.
     *
     * @param site the site that created the cavity
     * @param cavity the triangles with site in their circumcircle
     * @return one of the new triangles
     */
    private Triangle update(Pnt site, Set<Triangle> cavity) {
      Set<Set<Pnt>> boundary = new HashSet<Set<Pnt>>();
      Set<Triangle> theTriangles = new HashSet<Triangle>();

      // Find boundary facets and adjacent triangles
      for (Triangle triangle : cavity) {
        theTriangles.addAll(neighbors(triangle));
        for (Pnt vertex : triangle) {
          Set<Pnt> facet = triangle.facetOpposite(vertex);
          if (boundary.contains(facet)) boundary.remove(facet);
          else boundary.add(facet);
        }
      }
      theTriangles.removeAll(cavity); // Adj triangles only

      // Remove the cavity triangles from the triangulation
      for (Triangle triangle : cavity) triGraph.remove(triangle);

      // Build each new triangle and add it to the
      // triangulation
      Set<Triangle> newTriangles = new HashSet<Triangle>();
      for (Set<Pnt> vertices : boundary) {
        vertices.add(site);
        Triangle tri = new Triangle(vertices);
        triGraph.add(tri);
        newTriangles.add(tri);
      }

      // Update the graph links for each new triangle
      theTriangles.addAll(newTriangles); // Adj triangle + new
      // triangles
      for (Triangle triangle : newTriangles)
        for (Triangle other : theTriangles)
          if (triangle.isNeighbor(other)) triGraph.add(triangle, other);

      // Return one of the new triangles
      return newTriangles.iterator().next();
    }
Example #7
0
    /**
     * Report triangles surrounding site in order (cw or ccw).
     *
     * @param site we want the surrounding triangles for this site
     * @param triangle a "starting" triangle that has site as a vertex
     * @return all triangles surrounding site in order (cw or ccw)
     * @throws IllegalArgumentException if site is not in triangle
     */
    public List<Triangle> surroundingTriangles(Pnt site, Triangle triangle) {
      if (!triangle.contains(site)) throw new IllegalArgumentException("Site not in triangle");
      List<Triangle> list = new ArrayList<Triangle>();
      Triangle start = triangle;
      Pnt guide = triangle.getVertexButNot(site); // Affects
      // cw or
      // ccw
      while (true) {
        list.add(triangle);
        Triangle previous = triangle;

        ; // System.out.println(" site :" + site + " " + guide + " " + triangle);

        if (triangle == null) break;

        triangle = this.neighborOpposite(guide, triangle); // Next
        // triangle
        guide = previous.getVertexButNot(site, guide); // Update
        // guide
        if (triangle == start) break;
      }
      return list;
    }
Example #8
0
 /**
  * Determine the cavity caused by site.
  *
  * @param site the site causing the cavity
  * @param triangle the triangle containing site
  * @return set of all triangles that have site in their circumcircle
  */
 private Set<Triangle> getCavity(Pnt site, Triangle triangle) {
   Set<Triangle> encroached = new HashSet<Triangle>();
   Queue<Triangle> toBeChecked = new LinkedList<Triangle>();
   Set<Triangle> marked = new HashSet<Triangle>();
   toBeChecked.add(triangle);
   marked.add(triangle);
   while (!toBeChecked.isEmpty()) {
     triangle = toBeChecked.remove();
     if (site.vsCircumcircle(triangle.toArray(new Pnt[0])) == 1)
       continue; // Site outside triangle =>
     // triangle not in
     // cavity
     encroached.add(triangle);
     // Check the neighbors
     for (Triangle neighbor : triGraph.neighbors(triangle)) {
       if (marked.contains(neighbor)) continue;
       marked.add(neighbor);
       toBeChecked.add(neighbor);
     }
   }
   return encroached;
 }
Example #9
0
 public void computeImage(double time) {
   initFrame(time);
   // initial each pixar pz depth
   for (int i = 0; i < W * H; i++) {
     pz_pix[i] = 0;
   }
   // initialize each pixar color to background color
   for (int i = 0; i < H * W; i++)
     pix[i] =
         pack(
             (int) (background[0] * 255),
             (int) (background[1] * 255),
             (int) (background[2] * 255));
   // for each shapes
   for (int i = 0; i < geoI; i++) {
     double vertex[][] = t[i].vertices;
     double normal[][] = t[i].normals;
     int face[][] = t[i].faces;
     // calculate rgb color for each vertice
     for (int j = 0; j < vertex.length; j++) {
       normalize(normal[j]);
       double I[] = {vertex[i][0] - viewX, vertex[i][1] - viewY, vertex[i][2] - viewZ};
       normalize(I);
       double dotP = dot(normal[j], I);
       double R[] = {
         I[0] - 2.0 * dotP * normal[j][0],
         I[1] - 2.0 * dotP * normal[j][1],
         I[2] - 2.0 * dotP * normal[j][2]
       };
       normalize(R);
       double p = materials[i].p;
       t[i].colors[j][0] = materials[i].Argb[0];
       t[i].colors[j][1] = materials[i].Argb[1];
       t[i].colors[j][2] = materials[i].Argb[2];
       // for each light!
       for (int iL = 0; iL < lights.length; iL++) {
         double dotLN = Math.max(0, dot(lights[iL].Lxyz, normal[j]));
         double dotLV = Math.pow(Math.max(0, dot(lights[iL].Lxyz, R)), p);
         t[i].colors[j][0] +=
             lights[iL].Irgb[0] * (materials[i].Drgb[0] * dotLN + materials[i].Srgb[0] * dotLV);
         t[i].colors[j][1] +=
             lights[iL].Irgb[1] * (materials[i].Drgb[1] * dotLN + materials[i].Srgb[1] * dotLV);
         t[i].colors[j][2] +=
             lights[iL].Irgb[2] * (materials[i].Drgb[2] * dotLN + materials[i].Srgb[2] * dotLV);
       }
     }
     for (int j = 0; j < face.length; j++) {
       // first half => triangle
       draw.set1vertex(vertex[face[j][0]][0], vertex[face[j][0]][1], vertex[face[j][0]][2]);
       draw.set2vertex(vertex[face[j][1]][0], vertex[face[j][1]][1], vertex[face[j][1]][2]);
       draw.set3vertex(vertex[face[j][2]][0], vertex[face[j][2]][1], vertex[face[j][2]][2]);
       draw.set1rgb(
           t[i].colors[face[j][0]][0], t[i].colors[face[j][0]][1], t[i].colors[face[j][0]][2]);
       draw.set2rgb(
           t[i].colors[face[j][1]][0], t[i].colors[face[j][1]][1], t[i].colors[face[j][1]][2]);
       draw.set3rgb(
           t[i].colors[face[j][2]][0], t[i].colors[face[j][2]][1], t[i].colors[face[j][2]][2]);
       perspective(draw);
       draw.toTrapezoids();
       interpTri(draw);
       // second half => triangle
       draw.set1vertex(vertex[face[j][0]][0], vertex[face[j][0]][1], vertex[face[j][0]][2]);
       draw.set2vertex(vertex[face[j][2]][0], vertex[face[j][2]][1], vertex[face[j][2]][2]);
       draw.set3vertex(vertex[face[j][3]][0], vertex[face[j][3]][1], vertex[face[j][3]][2]);
       draw.set1rgb(
           t[i].colors[face[j][0]][0], t[i].colors[face[j][0]][1], t[i].colors[face[j][0]][2]);
       draw.set2rgb(
           t[i].colors[face[j][2]][0], t[i].colors[face[j][2]][1], t[i].colors[face[j][2]][2]);
       draw.set3rgb(
           t[i].colors[face[j][3]][0], t[i].colors[face[j][3]][1], t[i].colors[face[j][3]][2]);
       perspective(draw);
       draw.toTrapezoids();
       interpTri(draw);
     }
   }
 }
  /** tests the canvas. This one as XmlElementRefs, XmlElements, and an attachment... */
  public void testCanvas() throws Exception {
    Canvas canvas = new Canvas();
    Bus bus = new Bus();
    bus.setId("busId");
    Rectangle busFrame = new Rectangle();
    busFrame.setWidth(100);
    bus.setFrame(busFrame);
    Cat cat = new Cat();
    cat.setId("catId");
    Circle catFace = new Circle();
    catFace.setRadius(30);
    cat.setFace(catFace);
    House house = new House();
    house.setId("houseId");
    Rectangle houseBase = new Rectangle();
    houseBase.setWidth(76);
    house.setBase(houseBase);
    canvas.setFigures(Arrays.asList(bus, cat, house));
    Rectangle rectangle = new Rectangle();
    rectangle.setHeight(50);
    rectangle.setId("rectId");
    Circle circle = new Circle();
    circle.setRadius(10);
    circle.setId("circleId");
    Triangle triangle = new Triangle();
    triangle.setBase(80);
    triangle.setId("triId");
    canvas.setShapes(Arrays.asList(rectangle, circle, triangle));
    byte[] swaRefBytes =
        "This is a bunch of random bytes that are to be used as an SWA ref attachment.".getBytes();
    byte[] explicitBase64Bytes =
        "This is some more random bytes that are to be used as a base 64 encoded attachment."
            .getBytes();
    byte[] attachment1Bytes =
        "This is some more random bytes that are to be used as the first MTOM attachment."
            .getBytes();
    byte[] attachment2Bytes =
        "This is some more random bytes that are to be used as the second MTOM attachment."
            .getBytes();
    byte[] attachment3Bytes =
        "This is some more random bytes that are to be used as the third MTOM attachment."
            .getBytes();
    CanvasAttachment attachment1 = new CanvasAttachment();
    attachment1.setValue(attachment1Bytes);
    CanvasAttachment attachment2 = new CanvasAttachment();
    attachment2.setValue(attachment2Bytes);
    CanvasAttachment attachment3 = new CanvasAttachment();
    attachment3.setValue(attachment3Bytes);
    ByteArrayDataSource dataSource =
        new ByteArrayDataSource(swaRefBytes, "application/octet-stream");
    dataSource.setName("somename");
    // todo: uncomment when JAXB bug is fixed
    //    canvas.setBackgroundImage(new DataHandler(dataSource));
    canvas.setExplicitBase64Attachment(explicitBase64Bytes);
    canvas.setOtherAttachments(Arrays.asList(attachment1, attachment2, attachment3));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper canvasMapper =
        provider.locateMapper(Canvas.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    canvasMapper.writeValue(out, canvas);
    // set up the attachments that were written

    shapes.json.draw.Canvas clientCanvas =
        clientMapper.readValue(
            new ByteArrayInputStream(out.toByteArray()), shapes.json.draw.Canvas.class);
    Collection<ObjectNode> clientShapes = clientCanvas.getShapes();
    assertEquals(3, clientShapes.size());
    for (ObjectNode shape : clientShapes) {
      assertEquals(1, shape.size());
      String shapeKey = shape.getFields().next().getKey();
      shape = (ObjectNode) shape.get(shapeKey);
      if ("circle".equals(shapeKey)) {
        assertEquals("circleId", shape.get("id").getTextValue());
        assertEquals(10, shape.get("radius").getIntValue());
      } else if ("triangle".equals(shapeKey)) {
        assertEquals("triId", shape.get("id").getTextValue());
        assertEquals(80, shape.get("base").getIntValue());
      } else if ("rectangle".equals(shapeKey)) {
        assertEquals("rectId", shape.get("id").getTextValue());
        assertEquals(50, shape.get("height").getIntValue());
      } else {
        fail("Unknown shape: " + shapeKey);
      }
    }

    Collection<ObjectNode> clientFigures = clientCanvas.getFigures();
    assertEquals(3, clientFigures.size());
    for (ObjectNode figure : clientFigures) {
      assertEquals(1, figure.size());
      String figureKey = figure.getFields().next().getKey();
      figure = (ObjectNode) figure.get(figureKey);
      if ("bus".equals(figureKey)) {
        assertEquals("busId", figure.get("id").getTextValue());
        assertTrue(figure.get("frame") != null);
        assertEquals(100, figure.get("frame").get("width").getIntValue());
      } else if ("cat".equals(figureKey)) {
        assertEquals("catId", figure.get("id").getTextValue());
        assertTrue(figure.get("circle") != null);
        assertEquals(30, figure.get("circle").get("radius").getIntValue());
      } else if ("house".equals(figureKey)) {
        assertEquals("houseId", figure.get("id").getTextValue());
        assertTrue(figure.get("base") != null);
        assertEquals(76, figure.get("base").get("width").getIntValue());
      } else {
        fail("Unknown figure: " + figure);
      }
    }

    //    DataHandler backgroundImage = clientCanvas.getBackgroundImage();
    //    InputStream attachmentStream = backgroundImage.getInputStream();
    //    ByteArrayOutputStream bgImageIn = new ByteArrayOutputStream();
    //    int byteIn = attachmentStream.read();
    //    while (byteIn > 0) {
    //      bgImageIn.write(byteIn);
    //      byteIn = attachmentStream.read();
    //    }
    //    assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));

    byte[] base64Attachment = clientCanvas.getExplicitBase64Attachment();
    assertNotNull(base64Attachment);
    assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));

    Collection otherAttachments = clientCanvas.getOtherAttachments();
    assertEquals(3, otherAttachments.size());
    Iterator attachmentsIt = otherAttachments.iterator();
    int attachmentCount = 0;
    while (attachmentsIt.hasNext()) {
      shapes.json.draw.CanvasAttachment otherAttachment =
          (shapes.json.draw.CanvasAttachment) attachmentsIt.next();
      byte[] otherAttachmentBytes = otherAttachment.getValue();
      if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment3Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else {
        fail("Unknown attachment.");
      }
    }
    assertEquals(3, attachmentCount);

    clientCanvas.setShapes(null); // @XmlElementRefs can't be (de)serialized.
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientCanvas);
    canvas = canvasMapper.readValue(new ByteArrayInputStream(out.toByteArray()), Canvas.class);

    Collection shapes = canvas.getShapes();
    assertNull(shapes);
    //    assertEquals(3, shapes.size());
    //    for (Object Shape : shapes) {
    //      if (Shape instanceof Circle) {
    //        assertEquals("circleId", ((Circle) Shape).getId());
    //        assertEquals(10, ((Circle) Shape).getRadius());
    //      }
    //      else if (Shape instanceof Rectangle) {
    //        assertEquals("rectId", ((Rectangle) Shape).getId());
    //        assertEquals(50, ((Rectangle) Shape).getHeight());
    //      }
    //      else if (Shape instanceof Triangle) {
    //        assertEquals("triId", ((Triangle) Shape).getId());
    //        assertEquals(80, ((Triangle) Shape).getBase());
    //      }
    //      else {
    //        fail("Unknown shape: " + Shape);
    //      }
    //    }

    Collection figures = canvas.getFigures();
    assertEquals(3, figures.size());
    for (Object Figure : figures) {
      if (Figure instanceof Bus) {
        bus = (Bus) Figure;
        assertEquals("busId", bus.getId());
        Rectangle BusFrame = bus.getFrame();
        assertNotNull(BusFrame);
        assertEquals(100, busFrame.getWidth());
      } else if (Figure instanceof Cat) {
        cat = (Cat) Figure;
        assertEquals("catId", cat.getId());
        Circle CatFace = cat.getFace();
        assertNotNull(CatFace);
        assertEquals(30, CatFace.getRadius());
      } else if (Figure instanceof House) {
        house = (House) Figure;
        assertEquals("houseId", house.getId());
        Rectangle HouseBase = house.getBase();
        assertNotNull(HouseBase);
        assertEquals(76, HouseBase.getWidth());
      } else {
        fail("Unknown figure: " + Figure);
      }
    }

    //    backgroundImage = canvas.getBackgroundImage();
    //    attachmentStream = backgroundImage.getInputStream();
    //    bgImageIn = new ByteArrayOutputStream();
    //    byteIn = attachmentStream.read();
    //    while (byteIn > 0) {
    //      bgImageIn.write(byteIn);
    //      byteIn = attachmentStream.read();
    //    }
    //
    //    assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));

    base64Attachment = canvas.getExplicitBase64Attachment();
    assertNotNull(base64Attachment);
    assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));

    otherAttachments = canvas.getOtherAttachments();
    assertEquals(3, otherAttachments.size());
    attachmentsIt = otherAttachments.iterator();
    attachmentCount = 0;
    while (attachmentsIt.hasNext()) {
      CanvasAttachment otherAttachment = (CanvasAttachment) attachmentsIt.next();
      byte[] otherAttachmentBytes = otherAttachment.getValue();
      if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment3Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else {
        fail("Unknown attachment.");
      }
    }
    assertEquals(3, attachmentCount);

    // todo: test element ref to an attachment element
    // todo: test element refs of attachment elements.
  }
  /** tests cat. This one has IDREFs. */
  public void testCat() throws Exception {
    Cat cat = new Cat();
    Circle face = new Circle();
    face.setRadius(20);
    cat.setFace(face);
    Triangle ear = new Triangle();
    ear.setBase(5);
    ear.setHeight(10);
    ear.setId("earId");
    cat.setEars(Arrays.asList(ear, ear));

    // The eyes are the same as the ears, but so it needs to be for this test.
    cat.setEyes(new Triangle[] {ear, ear});

    Line noseLine = new Line();
    noseLine.setId("noseId");
    Line mouthLine = new Line();
    mouthLine.setId("mouthLine");

    cat.setNose(noseLine);
    cat.setMouth(mouthLine);
    cat.setWhiskers(Arrays.asList(noseLine, mouthLine));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper catMapper = provider.locateMapper(Cat.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    catMapper.writeValue(out, cat);
    shapes.json.animals.Cat clientCat =
        clientMapper.readValue(
            new ByteArrayInputStream(out.toByteArray()), shapes.json.animals.Cat.class);

    shapes.json.Circle clientFace = clientCat.getFace();
    assertEquals(20, clientFace.getRadius());
    assertEquals(2, clientCat.getEars().size());
    shapes.json.Triangle[] clientEars =
        (shapes.json.Triangle[]) clientCat.getEars().toArray(new shapes.json.Triangle[2]);
    assertNotSame(
        "referential integrity should NOT have been preserved since Jackson doesn't support it yet",
        clientEars[0],
        clientEars[1]);
    assertEquals(5, clientEars[0].getBase());
    assertEquals(10, clientEars[0].getHeight());
    assertEquals("earId", clientEars[0].getId());
    assertEquals(5, clientEars[1].getBase());
    assertEquals(10, clientEars[1].getHeight());
    assertEquals("earId", clientEars[1].getId());

    shapes.json.Triangle[] clientEyes = clientCat.getEyes();
    assertEquals(2, clientEyes.length);
    assertNotSame(clientEyes[0], clientEyes[1]);
    assertEquals(5, clientEyes[0].getBase());
    assertEquals(10, clientEyes[0].getHeight());
    assertEquals("earId", clientEyes[0].getId());
    assertEquals(5, clientEyes[1].getBase());
    assertEquals(10, clientEyes[1].getHeight());
    assertEquals("earId", clientEyes[1].getId());
    assertFalse(
        "The ears should NOT be the same object as one of the eyes since Jackson doesn't support referential integrity.",
        clientEars[0] == clientEyes[0] || clientEars[0] == clientEyes[1]);

    shapes.json.Line clientNose = clientCat.getNose();
    assertEquals("noseId", clientNose.getId());
    shapes.json.Line clientMouth = clientCat.getMouth();
    assertEquals("mouthLine", clientMouth.getId());
    assertFalse(
        "The nose line should NOT also be one of the whiskers since Jackson doesn't support referential integrity.",
        clientCat.getWhiskers().contains(clientNose));
    assertFalse(
        "The mouth line should NOT also be one of the whiskers since Jackson doesn't support referential integrity.",
        clientCat.getWhiskers().contains(clientMouth));

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientCat);
    cat = catMapper.readValue(new ByteArrayInputStream(out.toByteArray()), Cat.class);

    face = cat.getFace();
    assertEquals(20, face.getRadius());
    assertEquals(2, cat.getEars().size());
    Triangle[] ears = cat.getEars().toArray(new Triangle[2]);
    assertNotSame(
        "referential integrity should NOT have been preserved since Jackson doesn't support referential integrity.",
        ears[0],
        ears[1]);
    assertEquals(5, ears[0].getBase());
    assertEquals(10, ears[0].getHeight());
    assertEquals("earId", ears[0].getId());

    Triangle[] eyes = cat.getEyes();
    assertEquals(2, eyes.length);
    assertNotSame(eyes[0], eyes[1]);
    assertEquals(5, eyes[0].getBase());
    assertEquals(10, eyes[0].getHeight());
    assertEquals("earId", eyes[0].getId());
    assertEquals(5, eyes[1].getBase());
    assertEquals(10, eyes[1].getHeight());
    assertEquals("earId", eyes[1].getId());
    assertFalse(
        "The ears should NOT be the same object as one of the eyes since Jackson doesn't support referential integrity.",
        ears[0] == eyes[0] || ears[0] == eyes[1]);

    Line nose = cat.getNose();
    assertEquals("noseId", nose.getId());
    Line mouth = cat.getMouth();
    assertEquals("mouthLine", mouth.getId());
    assertFalse(
        "The nose line should also be one of the whiskers since Jackson doesn't support referential integrity.",
        cat.getWhiskers().contains(nose));
    assertFalse(
        "The mouth line should also be one of the whiskers since Jackson doesn't support referential integrity.",
        cat.getWhiskers().contains(mouth));
  }
  /** tests the basic shapes. */
  public void testBasicShapes() throws Exception {
    Circle circle = new Circle();
    circle.setColor(Color.BLUE);
    circle.setId("someid");
    circle.setLineStyle(LineStyle.solid);
    circle.setPositionX(8);
    circle.setPositionY(9);
    circle.setRadius(10);

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper circleMapper =
        provider.locateMapper(Circle.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    circleMapper.writeValue(out, circle);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Circle clientCircle = clientMapper.readValue(in, shapes.json.Circle.class);
    assertSame(shapes.json.Color.BLUE, clientCircle.getColor());
    assertEquals("someid", clientCircle.getId());
    assertEquals(shapes.json.LineStyle.solid, clientCircle.getLineStyle());
    assertEquals(8, clientCircle.getPositionX());
    assertEquals(9, clientCircle.getPositionY());
    assertEquals(10, clientCircle.getRadius());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientCircle);
    in = new ByteArrayInputStream(out.toByteArray());

    circle = circleMapper.readValue(in, Circle.class);
    assertSame(Color.BLUE, circle.getColor());
    assertEquals("someid", circle.getId());
    assertEquals(LineStyle.solid, circle.getLineStyle());
    assertEquals(8, circle.getPositionX());
    assertEquals(9, circle.getPositionY());
    assertEquals(10, circle.getRadius());

    ObjectMapper rectangleMapper =
        provider.locateMapper(Rectangle.class, MediaType.APPLICATION_JSON_TYPE);

    Rectangle rectangle = new Rectangle();
    rectangle.setColor(Color.GREEN);
    rectangle.setId("rectid");
    rectangle.setHeight(500);
    rectangle.setWidth(1000);
    rectangle.setLineStyle(LineStyle.dotted);
    rectangle.setPositionX(-100);
    rectangle.setPositionY(-300);

    out = new ByteArrayOutputStream();
    rectangleMapper.writeValue(out, rectangle);
    in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Rectangle clientRect = clientMapper.readValue(in, shapes.json.Rectangle.class);
    assertSame(shapes.json.Color.GREEN, clientRect.getColor());
    assertEquals("rectid", clientRect.getId());
    assertEquals(shapes.json.LineStyle.dotted, clientRect.getLineStyle());
    assertEquals(500, clientRect.getHeight());
    assertEquals(1000, clientRect.getWidth());
    assertEquals(-100, clientRect.getPositionX());
    assertEquals(-300, clientRect.getPositionY());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientRect);
    in = new ByteArrayInputStream(out.toByteArray());

    rectangle = rectangleMapper.readValue(in, Rectangle.class);
    assertSame(Color.GREEN, rectangle.getColor());
    assertEquals("rectid", rectangle.getId());
    assertEquals(LineStyle.dotted, rectangle.getLineStyle());
    assertEquals(500, rectangle.getHeight());
    assertEquals(1000, rectangle.getWidth());
    assertEquals(-100, rectangle.getPositionX());
    assertEquals(-300, rectangle.getPositionY());

    ObjectMapper triangleMapper =
        provider.locateMapper(Triangle.class, MediaType.APPLICATION_JSON_TYPE);

    Triangle triangle = new Triangle();
    triangle.setBase(90);
    triangle.setColor(Color.RED);
    triangle.setHeight(100);
    triangle.setId("triangleId");
    triangle.setLineStyle(LineStyle.dashed);
    triangle.setPositionX(0);
    triangle.setPositionY(-10);

    out = new ByteArrayOutputStream();
    triangleMapper.writeValue(out, triangle);
    in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Triangle clientTri = clientMapper.readValue(in, shapes.json.Triangle.class);
    assertSame(shapes.json.Color.RED, clientTri.getColor());
    assertEquals("triangleId", clientTri.getId());
    assertEquals(shapes.json.LineStyle.dashed, clientTri.getLineStyle());
    assertEquals(90, clientTri.getBase());
    assertEquals(100, clientTri.getHeight());
    assertEquals(0, clientTri.getPositionX());
    assertEquals(-10, clientTri.getPositionY());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientTri);
    in = new ByteArrayInputStream(out.toByteArray());

    triangle = triangleMapper.readValue(in, Triangle.class);
    assertSame(Color.RED, triangle.getColor());
    assertEquals(90, triangle.getBase());
    assertEquals(100, triangle.getHeight());
    assertEquals("triangleId", triangle.getId());
    assertSame(LineStyle.dashed, triangle.getLineStyle());
    assertEquals(0, triangle.getPositionX());
    assertEquals(-10, triangle.getPositionY());
  }
  /** tests house. This one has things like nillable and required properties. */
  public void testHouse() throws Exception {
    House house = new House();
    Rectangle base = new Rectangle();
    base.setColor(Color.BLUE);
    base.setHeight(80);
    base.setWidth(80);
    base.setLineStyle(LineStyle.solid);
    base.setId("baseid");
    house.setBase(base);
    Rectangle door = new Rectangle();
    door.setColor(Color.YELLOW);
    door.setHeight(35);
    door.setWidth(20);
    door.setLineStyle(LineStyle.solid);
    door.setId("doorId");
    house.setDoor(door);
    Circle knob = new Circle();
    knob.setColor(Color.RED);
    knob.setId("knobId");
    knob.setLineStyle(LineStyle.dashed);
    knob.setRadius(2);
    house.setDoorKnob(knob);
    Label label1 = new Label();
    label1.setValue("bachelor-pad");
    Label label2 = new Label();
    label2.setValue("single-family-dwelling");
    house.setLabels(Arrays.asList(label1, label2));
    Triangle roof = new Triangle();
    roof.setBase(84);
    roof.setHeight(20);
    roof.setColor(Color.YELLOW);
    roof.setLineStyle(LineStyle.solid);
    house.setRoof(roof);
    Rectangle window = new Rectangle();
    window.setColor(Color.YELLOW);
    window.setHeight(10);
    window.setWidth(10);
    window.setLineStyle(LineStyle.solid);
    house.setWindows(Arrays.asList(window));
    house.setConstructedDate(new DateTime(3L));
    house.setType(XmlQNameEnumUtil.toQName(HouseType.brick));
    house.setStyle(XmlQNameEnumUtil.toQName(HouseStyle.latin));
    house.setColor(XmlQNameEnumUtil.toURI(HouseColor.blue));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper houseMapper = provider.locateMapper(House.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    houseMapper.writeValue(out, house);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    shapes.json.structures.House clientHouse =
        clientMapper.readValue(in, shapes.json.structures.House.class);

    shapes.json.Rectangle clientBase = clientHouse.getBase();
    assertSame(shapes.json.Color.BLUE, clientBase.getColor());
    assertSame(shapes.json.LineStyle.solid, clientBase.getLineStyle());
    assertEquals(80, clientBase.getHeight());
    assertEquals(80, clientBase.getWidth());
    assertEquals("baseid", clientBase.getId());
    shapes.json.Rectangle clientDoor = clientHouse.getDoor();
    assertSame(shapes.json.Color.YELLOW, clientDoor.getColor());
    assertSame(shapes.json.LineStyle.solid, clientDoor.getLineStyle());
    assertEquals(35, clientDoor.getHeight());
    assertEquals(20, clientDoor.getWidth());
    assertEquals("doorId", clientDoor.getId());
    shapes.json.Circle clientKnob = clientHouse.getDoorKnob();
    assertSame(shapes.json.Color.RED, clientKnob.getColor());
    assertSame(shapes.json.LineStyle.dashed, clientKnob.getLineStyle());
    assertEquals(2, clientKnob.getRadius());
    assertEquals("knobId", clientKnob.getId());
    List<String> labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    clientHouse.getLabels().size();
    for (Object l : clientHouse.getLabels()) {
      shapes.json.Label label = (shapes.json.Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    shapes.json.Triangle clientRoof = clientHouse.getRoof();
    assertSame(shapes.json.Color.YELLOW, clientRoof.getColor());
    assertSame(shapes.json.LineStyle.solid, clientRoof.getLineStyle());
    assertNull(clientRoof.getId());
    assertEquals(84, clientRoof.getBase());
    assertEquals(20, clientRoof.getHeight());
    assertEquals(1, clientHouse.getWindows().size());
    shapes.json.Rectangle clientWindow = (shapes.json.Rectangle) clientHouse.getWindows().get(0);
    assertSame(shapes.json.Color.YELLOW, clientWindow.getColor());
    assertSame(shapes.json.LineStyle.solid, clientWindow.getLineStyle());
    assertEquals(10, clientWindow.getHeight());
    assertEquals(10, clientWindow.getWidth());
    assertNull(clientWindow.getId());
    assertEquals(new Date(3L), clientHouse.getConstructedDate());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertSame(Color.YELLOW, roof.getColor());
    assertSame(LineStyle.solid, roof.getLineStyle());
    assertNull(roof.getId());
    assertEquals(84, roof.getBase());
    assertEquals(20, roof.getHeight());
    assertEquals(1, house.getWindows().size());
    window = house.getWindows().get(0);
    assertSame(Color.YELLOW, window.getColor());
    assertSame(LineStyle.solid, window.getLineStyle());
    assertEquals(10, window.getHeight());
    assertEquals(10, window.getWidth());
    assertNull(window.getId());
    assertEquals(new DateTime(3L), house.getConstructedDate());
    assertEquals(XmlQNameEnumUtil.toQName(HouseType.brick), house.getType());
    assertEquals(XmlQNameEnumUtil.toQName(HouseStyle.latin), house.getStyle());
    assertEquals(XmlQNameEnumUtil.toURI(HouseColor.blue), house.getColor());

    // now let's check the nillable and required stuff:

    // roof is required, but nillable.
    clientHouse.setRoof(null);
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertNull(roof);
    assertEquals(1, house.getWindows().size());
    window = house.getWindows().get(0);
    assertSame(Color.YELLOW, window.getColor());
    assertSame(LineStyle.solid, window.getLineStyle());
    assertEquals(10, window.getHeight());
    assertEquals(10, window.getWidth());
    assertNull(window.getId());

    // windows are nillable...
    clientHouse.setWindows(null);
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertNull(roof);
    assertNull(house.getWindows());
  }
Example #14
0
 /**
  * True iff triangles are neighbors. Two triangles are neighbors if they share a facet.
  *
  * @param triangle the other Triangle
  * @return true iff this Triangle is a neighbor of triangle
  */
 public boolean isNeighbor(Triangle triangle) {
   int count = 0;
   for (Pnt vertex : this) if (!triangle.contains(vertex)) count++;
   return count == 1;
 }
 @Override
 public int compare(Triangle o1, Triangle o2) {
   if (o1.perimeter() > o2.perimeter()) return 1;
   else if (o1.perimeter() == o2.perimeter()) return 0;
   else return -1;
 }