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

Reuse Normalized Vectors

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 1.0
    • Component/s: None
    • Labels:
      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

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

              Dates

              • Created:
                Updated:
                Resolved: