001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:


























































































































































































































































































































































































































































































































































































/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_MATH_T_HPP)
#define OMNI_MATH_T_HPP 1
#include <omni/defs/global.hpp>

#if defined(OMNI_OS_WIN)
    /* DEV_NOTE: MSVC emits warning 4351 about the dimensional class having it's array value
    initialied in the member init list, e.g. `dimensional() : m_vals() {}`. This is considered
    "new" behavior by MSVC but is perfectly fine behavior per the C++ standard, this 0-init's
    the values in the array (as if memset were called). This is the behavior we want and since
    we are targetting C++03 and above, this is a non-issue. Additionally, testing this behavior
    against MSVC 2008 and above (and inspecting the asm) shows we get the results we want. */
    #if !defined(OMNI_WIN_NO_PRAGMA_DISABLE_4351)
        #pragma warning (disable:4351)
    #endif
#endif

#if !defined(OMNI_PI_F) // float
    #if defined (OMNI_USE_M_PI) && defined(M_PI)
        #define OMNI_PI_F static_cast<float>(M_PI)
    #else
        #define OMNI_PI_F 3.1415926F
    #endif
    #define OMNI_PI_F_180 0.0174533F
    #define OMNI_180_PI_F 57.2957805F
#endif

#if !defined(OMNI_PI) // double
    #if defined (OMNI_USE_M_PI) && defined(M_PI)
        #define OMNI_PI M_PI
    #else
        #define OMNI_PI 3.14159265358979323846
    #endif
    #define OMNI_PI_180 0.01745329251994329577
    #define OMNI_180_PI 57.29577951308232087684
#endif

#if !defined(OMNI_PI_L) // long double
    #if defined (OMNI_USE_M_PI_L) && defined(M_PIl)
        #define OMNI_PI_L M_PIl
    #else
        #define OMNI_PI_L 3.141592653589793238462643383279502884L
    #endif
    #define OMNI_PI_L_180 0.017453292519943295769236907684886127L
    #define OMNI_180_PI_L 57.295779513082320876798154814105170336L
#endif

namespace omni {
    namespace math {
        #if !defined(OMNI_NO_EXTERN_CONSTS)
            extern const double     PI;
            extern const long double PI_L;
            extern const float     PI_F;
            extern const double     PI_180;
            extern const long double PI_L_180;
            extern const float     PI_F_180;
            extern const double     RADS[361];
            extern const double     RADS_SIN[361];
            extern const double     RADS_COS[361];
        #else
            const double     PI = OMNI_PI;
            const long double PI_L = OMNI_PI_L;
            const float     PI_F = OMNI_PI_F;
            const double     PI_180 = OMNI_PI_180;
            const long double PI_L_180 = OMNI_PI_L_180;
            const float     PI_F_180 = OMNI_PI_F_180;
            #define OMNI_MATH_RAD_MAP_FW
            #include <omni/xx/math_radmap.hxx>
            #undef OMNI_MATH_RAD_MAP_FW
        #endif

        class ordinal_name {
            public:
                typedef enum enum_t {
                    Z_AXIS = -3,
                    Y_AXIS = -2,
                    X_AXIS = -1,
                    ORIGIN = 0,
                    I = 1,
                    II = 2,
                    III = 4,
                    IV = 8,
                    V = 16,
                    VI = 32,
                    VII = 64,
                    VIII = 128,
                    I_IV = (I | IV),
                    V_VIII = (V | VIII),
                    II_III = (II | III),
                    VI_VII = (VI | VII),
                    I_II = (I | II),
                    V_VI = (V | VI),
                    III_IV = (III | IV),
                    VII_VIII = (VII | VIII)
                } enum_t;
                
                static inline unsigned short COUNT()
                {
                    return 20;
                }
                
                static inline enum_t DEFAULT_VALUE()
                {
                    return ORIGIN;
                }

                static std::string to_string(enum_t v)
                {
                    return _to_val<std::stringstream>(v);
                }
            
                static std::wstring to_wstring(enum_t v)
                {
                    return _to_val<std::wstringstream>(v);
                }

                static enum_t parse(const std::string& val)
                {
                    return _parse(val);
                }

                static enum_t parse(const std::wstring& val)
                {
                    return _parse(val);
                }

                static bool try_parse(const std::string& val, enum_t& out)
                {
                    return _try_parse(val, out);
                }

                static bool try_parse(const std::wstring& val, enum_t& out)
                {
                    return _try_parse(val, out);
                }

                static bool try_parse(const std::string& val, ordinal_name& out)
                {
                    return _try_parse(val, out);
                }

                static bool try_parse(const std::wstring& val, ordinal_name& out)
                {
                    return _try_parse(val, out);
                }

                static bool is_valid(int val)
                {
                    return _valid(val);
                }
                
                ordinal_name() :
                    OMNI_CTOR_FW(omni::math::ordinal_name)
                    m_val(DEFAULT_VALUE())
                { }

                ordinal_name(const ordinal_name& cp) :
                    OMNI_CPCTOR_FW(cp)
                    m_val(cp.m_val)
                { }

                ordinal_name(enum_t val) :
                    OMNI_CTOR_FW(omni::math::ordinal_name)
                    m_val(val)
                { }

                ~ordinal_name()
                {
                    OMNI_TRY_FW
                    OMNI_DTOR_FW
                    OMNI_CATCH_FW
                    OMNI_D5_FW("destroyed");
                }

                enum_t value() const
                {
                    return this->m_val;
                }

                std::string to_string() const
                {
                    return to_string(this->m_val);
                }

                std::wstring to_wstring() const
                {
                    return to_wstring(this->m_val);
                }

                bool operator!=(const ordinal_name& val) const
                {
                    return !(*this == val);
                }
                
                bool operator!=(enum_t val) const
                {
                    return (this->m_val != val);
                }
                
                ordinal_name& operator=(const ordinal_name& val)
                {
                    if (this != &val) {
                        OMNI_ASSIGN_FW(val)
                        this->m_val = val.m_val;
                    }
                    return *this;
                }

                ordinal_name& operator=(enum_t val)
                {
                    this->m_val = val;
                    return *this;
                }

                ordinal_name& operator=(int val)
                {
                    if (!ordinal_name::is_valid(val)) {
                        OMNI_ERR_RET_FW("Invalid enumeration value specified.", omni::exceptions::invalid_enum(val));
                    } else {
                        this->m_val = static_cast<enum_t>(val);
                    }
                    return *this;
                }

                bool operator<(const ordinal_name& val) const
                {
                    return this->m_val < val.m_val;
                }

                bool operator<(enum_t val) const
                {
                    return this->m_val < val;
                }

                bool operator<(int val) const
                {
                    return this->m_val < static_cast<enum_t>(val);
                }

                bool operator>(const ordinal_name& val) const
                {
                    return this->m_val > val.m_val;
                }

                bool operator>(enum_t val) const
                {
                    return this->m_val > val;
                }

                bool operator>(int val) const
                {
                    return this->m_val > val;
                }

                bool operator==(const ordinal_name& val) const
                {
                    if (this == &val) { return true; }
                    return this->m_val == val.m_val
                            OMNI_EQUAL_FW(val);
                }

                bool operator==(enum_t val) const
                {
                    return this->m_val == val;
                }

                bool operator==(int val) const
                {
                    return this->m_val == val;
                }

                operator enum_t() const
                {
                    return this->m_val;
                }

                operator int32_t() const
                {
                    return static_cast<int32_t>(this->m_val);
                }

                operator std::string() const
                {
                    return this->to_string();
                }

                operator std::wstring() const
                {
                    return this->to_wstring();
                }

                OMNI_MEMBERS_FW(omni::math::ordinal_name) // disposing,name,type(),hash()
                
                OMNI_OSTREAM_FW(omni::math::ordinal_name)
                OMNI_OSTREAM_FN_FW(enum_t)

            private:
                enum_t m_val;

                template < typename S >
                static enum_t _parse(const S& val)
                {
                    enum_t ret;
                    if (_try_parse(val, ret)) { return ret; }
                    OMNI_ERR_FW("invalid enum parse", omni::exceptions::invalid_enum())
                    return DEFAULT_VALUE();
                }

                template < typename S >
                static bool _try_parse(const S& str, enum_t& out)
                {
                    return _try_parse(omni::string::util::to_upper(str), out);
                }

                template < typename S >
                static bool _try_parse(const S& val, ordinal_name& out)
                {
                    enum_t tmp;
                    if (_try_parse(val, tmp)) {
                        out.m_val = tmp;
                        return true;
                    }
                    return false;
                }

                static bool _try_parse(const std::wstring& val, enum_t& out)
                {
                    return _try_parse(omni::string::util::to_string(val), out);
                }

                static bool _try_parse(const std::string& val, enum_t& out)
                {
                    if (!val.empty()) {
                        OMNI_S2E_FW(Z_AXIS)
                        OMNI_S2E_FW(Y_AXIS)
                        OMNI_S2E_FW(X_AXIS)
                        OMNI_S2E_FW(ORIGIN)
                        OMNI_S2E_FW(I)
                        OMNI_S2E_FW(II)
                        OMNI_S2E_FW(III)
                        OMNI_S2E_FW(IV)
                        OMNI_S2E_FW(V)
                        OMNI_S2E_FW(VI)
                        OMNI_S2E_FW(VII)
                        OMNI_S2E_FW(VIII)
                        OMNI_S2E_FW(I_IV)
                        OMNI_S2E_FW(V_VIII)
                        OMNI_S2E_FW(II_III)
                        OMNI_S2E_FW(VI_VII)
                        OMNI_S2E_FW(I_II)
                        OMNI_S2E_FW(V_VI)
                        OMNI_S2E_FW(III_IV)
                        OMNI_S2E_FW(VII_VIII)
                    }
                    return false;
                }

                template < typename S >
                static std::basic_string< typename S::char_type > _to_val(enum_t v)
                {
                    S ss;
                    switch (v) {
                        OMNI_E2SS_FW(Z_AXIS);
                        OMNI_E2SS_FW(Y_AXIS);
                        OMNI_E2SS_FW(X_AXIS);
                        OMNI_E2SS_FW(ORIGIN);
                        OMNI_E2SS_FW(I);
                        OMNI_E2SS_FW(II);
                        OMNI_E2SS_FW(III);
                        OMNI_E2SS_FW(IV);
                        OMNI_E2SS_FW(V);
                        OMNI_E2SS_FW(VI);
                        OMNI_E2SS_FW(VII);
                        OMNI_E2SS_FW(VIII);
                        OMNI_E2SS_FW(I_IV);
                        OMNI_E2SS_FW(V_VIII);
                        OMNI_E2SS_FW(II_III);
                        OMNI_E2SS_FW(VI_VII);
                        OMNI_E2SS_FW(I_II);
                        OMNI_E2SS_FW(V_VI);
                        OMNI_E2SS_FW(III_IV);
                        OMNI_E2SS_FW(VII_VIII);
                        default:
                            ss << "UNKNOWN (" << static_cast<int>(v) << ")";
                            break;
                    }
                    return ss.str();
                }

                static bool _valid(int val)
                {
                    return (val ==
                        Z_AXIS ||
                        Y_AXIS ||
                        X_AXIS ||
                        ORIGIN ||
                        I ||
                        II ||
                        III ||
                        IV ||
                        V ||
                        VI ||
                        VII ||
                        VIII ||
                        I_IV ||
                        V_VIII ||
                        II_III ||
                        VI_VII ||
                        I_II ||
                        V_VI ||
                        III_IV ||
                        VII_VIII
                    );
                }
        };

        template < typename T, size_t SZ >
        class dimensional
        {
            public:
                typedef T dimension_t;

                dimensional() : m_vals() {}

                dimensional(const dimensional& cp) : m_vals()
                {
                    this->copy(cp);
                }

                dimensional(T vals[SZ]) : m_vals()
                {
                    for (std::size_t i = 0; i < SZ; ++i) {
                        this->m_vals[i] = vals[i];
                    }
                }

                ~dimensional() {}

                T at(std::size_t index)
                {
                    if (index > SZ) {
                        OMNI_ERR_RETV_FW("Index out of range", omni::exceptions::index_out_of_range(), T());
                    }
                    return this->m_vals[index];
                }

                void copy(const dimensional& o)
                {
                    for (size_t i = 0; i < SZ; ++i) {
                        this->m_vals[i] = o.m_vals[i];
                    }
                }

                size_t size() const
                {
                    return SZ;
                }

                void swap(const dimensional& o)
                {
                    for (size_t i = 0; i < SZ; ++i) {
                        OMNI_XOR_SWAP(this->m_vals[i], o.m_vals[i]);
                    }
                }

                dimensional& operator=(const dimensional& o)
                {
                    this->copy(o);
                    return *this;
                }

                bool operator==(const dimensional& o)
                {
                    for (size_t i = 0; i < SZ; ++i) {
                        if (this->m_vals[i] != o.m_vals[i]) {
                            return false;
                        }
                    }
                    return true;
                }

                T& operator[](std::size_t idx)
                {
                    return this->m_vals[idx];
                }

                T operator[](std::size_t idx) const
                {
                    return this->m_vals[idx];
                }

                std::string to_string() const
                {
                    std::stringstream ss;
                    ss << "{";
                    for (std::size_t i = 0; i < SZ; ++i) {
                        ss << this->m_vals[i];
                        if ((i + 1) < SZ) { ss << ","; }
                    }
                    ss << "}";
                    return ss.str();
                }

                std::wstring to_wstring() const
                {
                    std::wstringstream ss;
                    ss << "{";
                    for (std::size_t i = 0; i < SZ; ++i) {
                        ss << this->m_vals[i];
                        if ((i + 1) < SZ) { ss << ","; }
                    }
                    ss << "}";
                    return ss.str();
                }
                
                operator std::string()
                {
                    return this->to_string();
                }

                operator std::wstring()
                {
                    return this->to_wstring();
                }

                friend std::ostream& operator<<(std::ostream& os, const dimensional& v)
                {
                    os << v.to_string();
                    return os;
                }

                friend std::wostream& operator<<(std::wostream& os, const dimensional& v)
                {
                    os << v.to_wstring();
                    return os;
                }

            private:
                mutable T m_vals[SZ];
        };
    }
}

namespace std {
    template < typename T, size_t SZ >
    inline void swap(omni::math::dimensional<T, SZ>& ot1, omni::math::dimensional<T, SZ>& ot2)
    {
        ot1.swap(ot2);
    }
}

#endif // OMNI_MATH_T_HPP