Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DT_CBox.h « complex « src « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7fc7c5df4dba8c46c4dd30a4a0ec4c38d4e5e3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#ifndef DT_CBOX_H
#define DT_CBOX_H

#include "MT_BBox.h"

struct DT_CBox {
    DT_CBox() {}
    DT_CBox(const MT_Point3& center, const MT_Vector3& extent) 
      : m_center(center),
        m_extent(extent)
    {}

    explicit DT_CBox(const MT_BBox& bbox) { set(bbox); }

    const MT_Point3& getCenter() const { return m_center; }
    const MT_Vector3& getExtent() const { return m_extent; }

    void set(const MT_BBox& bbox)
    {
        m_center = bbox.getCenter();
        m_extent = bbox.getExtent();
    }
 
    MT_BBox get() const
    {
        return MT_BBox(m_center - m_extent, m_center + m_extent);
    }

    MT_Scalar size() const  
    {
        return GEN_max(GEN_max(m_extent[0], m_extent[1]), m_extent[2]);
    }


    DT_CBox& operator+=(const DT_CBox& box)
    {
        m_center += box.getCenter();
        m_extent += box.getExtent();
        return *this;
    }
    
    int longestAxis() const { return m_extent.closestAxis(); }
        
    DT_CBox hull(const DT_CBox& b) const 
    {
        return DT_CBox(this->get().hull(b.get()));
    }

    bool overlaps(const DT_CBox& b) const 
    {
        return MT_abs(m_center[0] - b.m_center[0]) <= m_extent[0] + b.m_extent[0] &&
               MT_abs(m_center[1] - b.m_center[1]) <= m_extent[1] + b.m_extent[1] &&
               MT_abs(m_center[2] - b.m_center[2]) <= m_extent[2] + b.m_extent[2];
    }
    
    bool overlapsLineSegment(const MT_Point3& p, const MT_Point3& q) const 
    {
        MT_Vector3 r = q - p;   
        MT_Vector3 r_abs = r.absolute();
        
        if (!overlaps(DT_CBox(p + r * MT_Scalar(0.5), r_abs * MT_Scalar(0.5))))
        {
            return false;
        }
        
        MT_Vector3 s = p - m_center;

        if (MT_abs(r[2] * s[1] - r[1] * s[2]) > r_abs[2] * m_extent[1] + r_abs[1] * m_extent[2])
        {
            return false;
        }
                    
        if (MT_abs(r[0] * s[2] - r[2] * s[0]) > r_abs[0] * m_extent[2] + r_abs[2] * m_extent[0])
        {
            return false;
        }
                    
        if (MT_abs(r[1] * s[0] - r[0] * s[1]) > r_abs[1] * m_extent[0] + r_abs[0] * m_extent[1])
        {
            return false;
        }
            
        return true;
    }
    
    MT_Point3 support(const MT_Vector3& v) const 
    {
        return m_center + MT_Vector3(v[0] < MT_Scalar(0.0) ? -m_extent[0] : m_extent[0],
                                     v[1] < MT_Scalar(0.0) ? -m_extent[1] : m_extent[1],
                                     v[2] < MT_Scalar(0.0) ? -m_extent[2] : m_extent[2]); 
    
    }

private:
    MT_Point3  m_center;
    MT_Vector3 m_extent;
};

inline DT_CBox operator+(const DT_CBox& b1, const DT_CBox& b2) 
{
    return DT_CBox(b1.getCenter() + b2.getCenter(), 
                   b1.getExtent() + b2.getExtent());
}

inline DT_CBox operator-(const DT_CBox& b1, const DT_CBox& b2) 
{
    return DT_CBox(b1.getCenter() - b2.getCenter(), 
                   b1.getExtent() + b2.getExtent());
}

#endif