官术网_书友最值得收藏!

Comparison operations

Comparing two quaternions can be done component-wise. Two quaternions can represent the same rotation even if they are not identical on a component level. This happens because a quaternion and its inverse rotate to the same spot but they take different routes:

  1. Overload the == and != operators in quat.cpp. Add the declaration for these functions to quat.h:

    bool operator==(const quat& left, const quat& right) {

        return (fabsf(left.x - right.x) <= QUAT_EPSILON &&

                fabsf(left.y - right.y) <= QUAT_EPSILON &&

                fabsf(left.z - right.z) <= QUAT_EPSILON &&

                fabsf(left.w - right.w) <= QUAT_EPSILON);

    }

    bool operator!=(const quat& a, const quat& b) {

        return !(a == b);

    }

  2. To test whether two quaternions represent the same rotation, the absolute difference between the two needs to be tested. Implement the sameOrientation function in quat.cpp. Add the function declaration to quat.h:

    bool sameOrientation(const quat&l, const quat&r) {

        return (fabsf(l.x - r.x) <= QUAT_EPSILON  &&

                fabsf(l.y - r.y) <= QUAT_EPSILON  &&

                fabsf(l.z - r.z) <= QUAT_EPSILON  &&

                fabsf(l.w - r.w) <= QUAT_EPSILON) ||

               (fabsf(l.x + r.x) <= QUAT_EPSILON  &&

                fabsf(l.y + r.y) <= QUAT_EPSILON  &&

                fabsf(l.z + r.z) <= QUAT_EPSILON  &&

                fabsf(l.w + r.w) <= QUAT_EPSILON);

    }

Most of the time, you will want to use the equality operator to compare quaternions. The sameOrientation function is not as useful because the rotation that a quaternion takes can be changed if the quaternion is inverted.

In the next section, you will learn how to implement a quaternion dot product.

主站蜘蛛池模板: 博白县| 图们市| 区。| 南平市| 仙居县| 游戏| 霸州市| 盐津县| 廉江市| 扶余县| 施秉县| 辉县市| 榆树市| 乡城县| 崇文区| 双牌县| 凉城县| 廉江市| 防城港市| 铜山县| 临潭县| 鸡东县| 高碑店市| 盐边县| 沁水县| 洪湖市| 通城县| 祥云县| 通山县| 上饶县| 罗山县| 宝兴县| 平昌县| 广西| 西吉县| 伊吾县| 宁乡县| 竹北市| 苗栗县| 阳春市| 南昌县|