Uploaded image for project: 'Commons Geometry'
  1. Commons Geometry
  2. GEOMETRY-10

Reuse Normalized Vectors

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Done
    • None
    • 1.0
    • None
    • None

    Description

      The Vector.normalize() method is used quite frequently. We currently always return a new instance with each method call, but we should be able to reuse the current instance if it is already normalized. 

      Ex:

      public Vector3D normalize() {
         final double normSq = getNormSq();
         if (normSq == 1.0) {
            return this;
         }
         else if (normSq == 0.0) {
            throw new IllegalStateException(ZERO_NORM_MSG);
         }
      
         return scalarMultiply(1 / Math.sqrt(normSq);
      }

      We'll need to verify that this won't introduce any floating point accuracy issues.

       

      Pull request: https://github.com/apache/commons-geometry/pull/10

      Attachments

        Activity

          People

            Unassigned Unassigned
            mattjuntunen Matt Juntunen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: