Scalar Field Type Requirements
Algebraically speaking, a vector space is defined over an underlying scalar field. A field offers the operations and properties of basic arithmetics; common examples are rational, real or complex numbers. See the vector space algebra page for a detailed treatment. For the purpose of implementing a scalar field type to be used with the templatized geometry, a more pragmatic breakdown of syntactic and semantic requirements is more appropriate.
Fundamental Interface Requirements
Any data type to be used as ScalarParam template parameter for any geometry class or function needs to provide the following interface. Note: In all methods and functions, parameters of type ScalarParam can be replaced with parameters of type const ScalarParam&, and all operators can be implemented as either methods or functions.
Construction and Assignment
- ScalarParam::ScalarParam(void)
- Dummy constructor. The geometry library does not make any assumptions about objects created this way.
- ScalarParam::ScalarParam(int)
- Conversion constructor from integer. This constructor is only used to create the two identity elements "zero" and "one" by writing ScalarParam(0) or ScalarParam(1), respectively.
- ScalarParam::ScalarParam(ScalarParam)
- Copy constructor. After this, the created object and the source object must be identical (return true when compared with operator==, but separate, i.e., changes to one do not affect the others. This usually means performing a "deep copy."
- ScalarParam& ScalarParam::operator=(ScalarParam)
- Assignment operator. The assignment operator must be idempotent, i.e., assigning an object to itself must not change/affect the object in any way, and it must return a non-const reference to the object being assigned to. Furthermore, operator= has the same post-condition as the copy constructor, i.e., the two involved objects must be equal but separate after assignment.
Relational Operators
- bool operator==(ScalarParam,ScalarParam)
- Equality operator.
- bool operator!=(ScalarParam,ScalarParam)
- Inequality operator. For any ScalarParam objects a and b, a!=b must always be equivalent to !(a==b).
Arithmetic Operators
The arithmetic operators must follow the semantics of a field, see the vector space algebra page for exact definitions. If ScalarParam does not model a field, but only a ring with unity, i.e., if no division operator is provided, or division fails if the divisor does not have a multiplicative inverse, most geometry operations will still work as expected. For example, it often makes perfect sense to work with vectors of integers. Many more complex operations, notably transformations, make implicit use of division and will fail with unexpected results. Your mileage may vary.
- ScalarParam operator+(ScalarParam,ScalarParam)
- Addition operator.
- ScalarParam operator-(ScalarParam)
- Negation operator.
- ScalarParam operator-(ScalarParam,ScalarParam)
- Subtraction operator.
- ScalarParam operator*(ScalarParam,ScalarParam)
- Multiplication operator.
- ScalarParam operator/(ScalarParam,ScalarParam)
- Division operator. The templatized geometry library does not expect a specific behaviour on attempted division by zero (global error flag, thrown exception, NAN or nonsense results, termination, system crash, imploding monitor are all appropriate).
Arithmetic Assignment Operators
The arithmetic assignment operators must follow both the semantics of a field and the semantics of the assignment operator, in other words, for any ScalarParam objects a and b and for any <op> in {+, -, *, /}, a <op> =b must be equivalent to a=a <op> b. The templatized geometry library uses these operators whereever possible for increased performance.
- ScalarParam& ScalarParam::operator+=(ScalarParam)
- Addition assignment operator.
- ScalarParam& ScalarParam::operator-=(ScalarParam)
- Subtraction assignment operator.
- ScalarParam& ScalarParam::operator*=(ScalarParam)
- Multiplication assignment operator.
- ScalarParam& ScalarParam::operator/=(ScalarParam)
- Division assignment operator.
Optional Interface
Some geometry operations require additional methods or functions from a scalar field type. These additional methods are usually defined in namespace Math, mostly to provide a uniform generic interface for built-in data types. The following functions are defined in namespace Math. Not all of these functions are meaningful for all field types; if a specific function is not provided, geometry operations requiring that function will cause a compiler error when used. Special requirements of geometry operations are noted on their respective documentation pages.
- ScalarParam Math::sqr(ScalarParam)
- Square function. For any ScalarParam object a, Math::sqr(a) must be equivalent to a*a. This function is used whereever a scalar is multiplied with itself.
- ScalarParam Math::sqrt(ScalarParam)
- Square root function. Mainly used to calculate the Euclidean vector norm and point metric, and to normalize coordinate systems.
- ScalarParam Math::abs(ScalarParam)
- Absolute value function. Mainly used to calculate the L1 vector norm and point metric.
- ScalarParam Math::floor(ScalarParam)
- Floor function. Used inside the wrapRad function.
- ScalarParam Math::wrapRad(ScalarParam)
- Function to wrap an angle argument in radians to the half-open interval [0, 2*pi). Used as a helper function in creating 2D rotations.
- ScalarParam Math::cos(ScalarParam)
- Cosine function with angle argument in radians. Used in rotations.
- ScalarParam Math::sin(ScalarParam)
- Sine function with angle argument in radians. Used in rotations.