Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8232809

Triangle Centroid Calculation in TriangleMesh is Wrong

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8u221, openjfx13
    • Fix Version/s: tbd
    • Component/s: javafx
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      ADDITIONAL SYSTEM INFORMATION :
      All java versions

      A DESCRIPTION OF THE PROBLEM :
      In javafx.scene.shape.TriangleMesh.java the calculation of centroid is wrong:
          private Point3D computeCentroid(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

              return new Point3D(
                  v0x + (v2x + (v1x - v2x) / 2.0 - v0x) / 3.0,
                  v0y + (v2y + (v1y - v2y) / 2.0 - v0y) / 3.0,
                  v0z + (v2z + (v1z - v2z) / 2.0 - v0z) / 3.0);
          }

      The equation in this code is missing a factor of 2.
          private static Point3D computeCentroid(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

              return new Point3D(
                      v0x + (v2x + (v1x - v2x) / 2.0 - v0x) * 2 / 3.0,
                      v0y + (v2y + (v1y - v2y) / 2.0 - v0y) * 2 / 3.0,
                      v0z + (v2z + (v1z - v2z) / 2.0 - v0z) * 2 / 3.0);
          }
      The routine should be:
          private static Point3D computeCentroid(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

              return new Point3D(
                      v0x + (v2x + (v1x - v2x) / 2.0 - v0x) * 2 / 3.0,
                      v0y + (v2y + (v1y - v2y) / 2.0 - v0y) * 2 / 3.0,
                      v0z + (v2z + (v1z - v2z) / 2.0 - v0z) *2 / 3.0);
          }

      Alternatively, one can just do:
              return new Point3D(
                      (v0x + v1x + v2x) / 3.0,
                      (v0y + v1y + v2y) / 3.0,
                      (v0z + v1z + v2z) / 3.0);

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Calculate centroid of simple triangle with vertices (3, 0, 0) (-3, 0, 0) and (0, 6, 0)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Centroid is (0, 2, 0)
      ACTUAL -
      Centroid is (1.5, 1.0, 0)

      ---------- BEGIN SOURCE ----------
      package test;

      import javafx.geometry.Point3D;


      public class WrongCentroidResult {
          
          public WrongCentroidResult(){
          }

      // WRONG
      // code from javafx.scene.shape.TriangleMesh
          private Point3D computeCentroid(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

      // Point3D center = v1.midpoint(v2);
      // Point3D vec = center.subtract(v0);
      // return v0.add(new Point3D(vec.getX() / 3.0, vec.getY() / 3.0, vec.getZ() / 3.0));

              return new Point3D(
                  v0x + (v2x + (v1x - v2x) / 2.0 - v0x) / 3.0,
                  v0y + (v2y + (v1y - v2y) / 2.0 - v0y) / 3.0,
                  v0z + (v2z + (v1z - v2z) / 2.0 - v0z) / 3.0);
          }


          private Point3D computeCentroid_Corrected(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

              return new Point3D(
                  v0x + (v2x + (v1x - v2x) / 2.0 - v0x) * 2 / 3.0,
                  v0y + (v2y + (v1y - v2y) / 2.0 - v0y) * 2 / 3.0,
                  v0z + (v2z + (v1z - v2z) / 2.0 - v0z) * 2 / 3.0);
          }

          private Point3D computeCentroid_Alternative(
                  double v0x, double v0y, double v0z,
                  double v1x, double v1y, double v1z,
                  double v2x, double v2y, double v2z) {

              return new Point3D(
                  (v0x + v1x + v2x) / 3.0,
                  (v0y + v1y + v2y) / 3.0,
                  (v0z + v1z + v2z) / 3.0);
          }
          
          public static void main(String[] args){
              System.out.println("expected = Point3D [x = 0.0, y = 2.0, z = 0.0]");
              WrongCentroidResult wcr = new WrongCentroidResult();
              Point3D wrong = wcr.computeCentroid(3, 0, 0, -3, 0, 0, 0, 6, 0);
              System.out.println("wrong = " + wrong);
              Point3D corrected = wcr.computeCentroid_Corrected(3, 0, 0, -3, 0, 0, 0, 6, 0);
              System.out.println("corrected = " + corrected);
              Point3D alternative = wcr.computeCentroid_Alternative(3, 0, 0, -3, 0, 0, 0, 6, 0);
              System.out.println("alternative = " + alternative);
          }
          
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              kcr Kevin Rushforth
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: