Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 // Unit tests for src/core/SkPoint3.cpp and its header
      9 
     10 #include "SkPoint3.h"
     11 #include "Test.h"
     12 
     13 static void test_eq_ops(skiatest::Reporter* reporter) {
     14     const SkPoint3 p0 = SkPoint3::Make(0, 0, 0);
     15     const SkPoint3 p1 = SkPoint3::Make(1, 1, 1);
     16     const SkPoint3 p2 = SkPoint3::Make(1, 1, 1);
     17 
     18     REPORTER_ASSERT(reporter, p0 != p1);
     19     REPORTER_ASSERT(reporter, p1 == p2);
     20 }
     21 
     22 static void test_ops(skiatest::Reporter* reporter) {
     23     SkPoint3 v = SkPoint3::Make(1, 1, 1);
     24     v.normalize();
     25     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(v.length(), SK_Scalar1));
     26 
     27     // scale
     28     SkPoint3 p = v.makeScale(3.0f);
     29     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.length(), 3.0f));
     30 
     31     p.scale(1.0f/3.0f);
     32     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.length(), SK_Scalar1));
     33 
     34     SkPoint3 p1 = SkPoint3::Make(20.0f, 2.0f, 10.0f);
     35     SkPoint3 p2 = -p1;
     36 
     37     // -
     38     p = p1 - p1;
     39     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.x(), 0.0f));
     40     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.y(), 0.0f));
     41     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.z(), 0.0f));
     42 
     43     // +
     44     p = p1 + p2;
     45     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.x(), 0.0f));
     46     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.y(), 0.0f));
     47     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.z(), 0.0f));
     48 }
     49 
     50 static void test_dot(skiatest::Reporter* reporter) {
     51     const SkPoint3 xAxis = SkPoint3::Make(1.0f, 0.0f, 0.0f);
     52     const SkPoint3 yAxis = SkPoint3::Make(0.0f, 1.0f, 0.0f);
     53     const SkPoint3 zAxis = SkPoint3::Make(0.0f, 0.0f, 1.0f);
     54 
     55     SkScalar dot = xAxis.dot(yAxis);
     56     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
     57 
     58     dot = yAxis.dot(zAxis);
     59     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
     60 
     61     dot = zAxis.dot(xAxis);
     62     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
     63 
     64     SkPoint3 v = SkPoint3::Make(13.0f, 2.0f, 7.0f);
     65     v.normalize();
     66 
     67     dot = v.dot(v);
     68     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 1.0f));
     69 
     70     v = SkPoint3::Make(SK_ScalarRoot2Over2, SK_ScalarRoot2Over2, 0.0f);
     71 
     72     dot = xAxis.dot(v);
     73     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, SK_ScalarRoot2Over2));
     74 
     75     dot = yAxis.dot(v);
     76     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, SK_ScalarRoot2Over2));
     77 }
     78 
     79 static void test_length(skiatest::Reporter* reporter,
     80                         SkScalar x, SkScalar y, SkScalar z, SkScalar expectedLen) {
     81     SkPoint3 point = SkPoint3::Make(x, y, z);
     82 
     83     SkScalar s1 = point.length();
     84     SkScalar s2 = SkPoint3::Length(x, y, z);
     85 
     86     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
     87     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLen));
     88 }
     89 
     90 static void test_normalize(skiatest::Reporter* reporter,
     91                            SkScalar x, SkScalar y, SkScalar z, SkScalar expectedLen) {
     92     SkPoint3 point = SkPoint3::Make(x, y, z);
     93 
     94     bool result = point.normalize();
     95     SkScalar newLength = point.length();
     96 
     97     if (0 == expectedLen) {
     98         const SkPoint3 empty = SkPoint3::Make(0.0f, 0.0f, 0.0f);
     99 
    100         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, 0));
    101         REPORTER_ASSERT(reporter, !result);
    102         REPORTER_ASSERT(reporter, point == empty);
    103     } else {
    104         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
    105         REPORTER_ASSERT(reporter, result);
    106     }
    107 }
    108 
    109 DEF_TEST(Point3, reporter) {
    110     test_eq_ops(reporter);
    111     test_ops(reporter);
    112     test_dot(reporter);
    113 
    114     static const struct {
    115         SkScalar fX;
    116         SkScalar fY;
    117         SkScalar fZ;
    118         SkScalar fLength;
    119     } gRec[] = {
    120         { 0.0f, 0.0f, 0.0f, 0.0f },
    121         { 0.3f, 0.4f, 0.5f, SK_ScalarRoot2Over2 },
    122         { 1.0e-37f, 1.0e-37f, 1.0e-37f, 0.0f },  // underflows
    123         { 3.4e38f, 0.0f, 0.0f, 3.4e38f }         // overflows
    124     };
    125 
    126     for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
    127         test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fZ, gRec[i].fLength);
    128         test_normalize(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fZ, gRec[i].fLength);
    129     }
    130 }
    131