3D数学 数学通用函数库源代码(附中文注释)

/////////////////////////////////////////////////////////////////////////////
// 
// 3D数学基础:游戏与图形开发
// 3D Math Primer for Games and Graphics Development
//
// MathUtil.h - 各种数学通用程序声明
// MathUtil.h - Declarations for miscellaneous math utilities
//
// 登录gamemath.com以获得该文件的最新版本
// Visit gamemath.com for the latest version of this file.
//
/////////////////////////////////////////////////////////////////////////////

#ifndef __MATHUTIL_H_INCLUDED__
#define __MATHUTIL_H_INCLUDED__

#include <math.h>

// 声明pi以及一些倍数的全局常量
// Declare a global constant for pi and a few multiples.

const float kPi = 3.14159265f;
const float k2Pi = kPi * 2.0f;
const float kPiOver2 = kPi / 2.0f;
const float k1OverPi = 1.0f / kPi;
const float k1Over2Pi = 1.0f / k2Pi;
const float kPiOver180 = kPi / 180.0f;
const float k180OverPi = 180.0f / kPi;

// 使一个角度回绕到-pi到pi,通过添加2pi的乘法修正
// "Wrap" an angle in range -pi...pi by adding the correct multiple
// of 2 pi

extern float wrapPi(float radian);

// "安全"反三角函数
// "Safe" inverse trig functions

extern float safeAcos(float x);

// 在角度和弧度直接转换
// Convert between degrees and radians

inline float    degToRad(float deg) { return deg * kPiOver180; }
inline float    radToDeg(float rad) { return rad * k180OverPi; }

// 计算一个角度的sin值和cos值。在一些平台如果我们知道它需要两个值,同时计算比跟开计算要快得多。
// Compute the sin and cosine of an angle.  On some platforms, if we know
// that we need both values, it can be computed faster than computing
// the two values seperately.

inline void sinCos(float *returnSin, float *returnCos, float theta) {

    // 出于化简,我们只是使用常规的三角函数。
    // 注意某些平台可能做的更好。
    // For simplicity, we'll just use the normal trig functions.
    // Note that on some platforms we may be able to do better

    *returnSin = sin(theta);
    *returnCos = cos(theta);
}

// 下面两个函数本人暂时未学到。迟些再补充。
// Convert between "field of view" and "zoom"  See section 15.2.4.
// The FOV angle is specified in radians.

inline float    fovToZoom(float fov) { return 1.0f / tan(fov * .5f); }
inline float    zoomToFov(float zoom) { return 2.0f * atan(1.0f / zoom); }

/////////////////////////////////////////////////////////////////////////////
#endif // #ifndef __MATHUTIL_H_INCLUDED__
/////////////////////////////////////////////////////////////////////////////
//
// 3D数学基础:游戏与图形开发
// 3D Math Primer for Games and Graphics Development
//
// MathUtil.cpp - 各种数学通用程序
// MathUtil.cpp - Miscellaneous math utilities
//
// 登录gamemath.com以获得该文件的最新版本
// Visit gamemath.com for the latest version of this file.
//
/////////////////////////////////////////////////////////////////////////////

#include <math.h>

#include "MathUtil.h"
#include "Vector3.h"

const Vector3 kZeroVector(0.0f, 0.0f, 0.0f);

//---------------------------------------------------------------------------
// 使一个角度回绕到-pi到pi,通过添加2pi的乘法修正
// "Wrap" an angle in range -pi...pi by adding the correct multiple
// of 2 pi

float wrapPi(float radian) {
    radian += kPi;
    radian -= floor(radian * k1Over2Pi) * k2Pi;
    radian -= kPi;
    return radian;
}

//---------------------------------------------------------------------------
// safeAcos
//
// 和acos(x)一样,不过如果x超出范围的话,它会夹紧最近的可用值。
// 和标准c函数acos(x)一致,这个值返回0到pi。
// Same as acos(x), but if x is out of range, it is "clamped" to the nearest
// valid value.  The value returned is in range 0...pi, the same as the
// standard C acos() function

float safeAcos(float x) {

    // 检查限制条件
    // Check limit conditions

    if (x <= -1.0f) {
        return kPi;
    }
    if (x >= 1.0f) {
        return 0.0f;
    }

    // 值在作用域范围内-直接使用标准C函数
    // Value is in the domain - use standard C function

    return acos(x);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值